1 /*****************************************************************************
2 * directx.c: Windows DirectX audio output method
3 *****************************************************************************
4 * Copyright (C) 2001-2009 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation, Inc.,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
37 #include <vlc_charset.h>
39 #include "windows_audio_common.h"
41 #define DS_BUF_SIZE (4*1024*1024)
43 /*****************************************************************************
44 * aout_sys_t: directx audio output method descriptor
45 *****************************************************************************
46 * This structure is part of the audio output thread descriptor.
47 * It describes the direct sound specific properties of an audio device.
48 *****************************************************************************/
51 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
53 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
54 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
55 * takes care of mixing all the
56 * secondary buffers into the primary) */
58 LPDIRECTSOUNDNOTIFY p_notify;
67 int i_bytes_per_sample; /* Size in bytes of one frame */
68 int i_rate; /* Sample rate */
70 uint8_t chans_to_reorder; /* do we need channel reordering */
71 uint8_t chan_table[AOUT_CHAN_MAX];
72 uint32_t i_channel_mask;
78 /*****************************************************************************
80 *****************************************************************************/
81 static int Open( vlc_object_t * );
82 static void Close( vlc_object_t * );
83 static void Stop( audio_output_t * );
84 static void Play( audio_output_t *, block_t * );
85 static int VolumeSet( audio_output_t *, float );
86 static int MuteSet( audio_output_t *, bool );
87 static void Flush( audio_output_t *, bool );
88 static void Pause( audio_output_t *, bool, mtime_t );
89 static int TimeGet( audio_output_t *, mtime_t *);
92 static int InitDirectSound ( audio_output_t * );
93 static int CreateDSBuffer ( audio_output_t *, int, int, int, int, bool );
94 static int CreateDSBufferPCM ( audio_output_t *, vlc_fourcc_t*, int, int, bool );
95 static void DestroyDSBuffer ( audio_output_t * );
96 static int FillBuffer ( audio_output_t *, block_t * );
98 static int ReloadDirectXDevices( vlc_object_t *, const char *,
101 /* Speaker setup override options list */
102 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
103 "Quad", "5.1", "7.1" };
105 /*****************************************************************************
107 *****************************************************************************/
108 #define DEVICE_TEXT N_("Output device")
109 #define DEVICE_LONGTEXT N_("Select your audio output device")
111 #define SPEAKER_TEXT N_("Speaker configuration")
112 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
113 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
115 #define VOLUME_TEXT N_("Audio volume")
116 #define VOLUME_LONGTEXT N_("Audio volume in hundredths of decibels (dB).")
119 set_description( N_("DirectX audio output") )
120 set_shortname( "DirectX" )
121 set_capability( "audio output", 100 )
122 set_category( CAT_AUDIO )
123 set_subcategory( SUBCAT_AUDIO_AOUT )
124 add_shortcut( "directx", "aout_directx" )
126 add_string( "directx-audio-device", NULL,
127 DEVICE_TEXT, DEVICE_LONGTEXT, false )
128 change_string_cb( ReloadDirectXDevices )
129 add_obsolete_string( "directx-audio-device-name")
130 add_bool( "directx-audio-float32", true, FLOAT_TEXT,
131 FLOAT_LONGTEXT, true )
132 add_string( "directx-audio-speaker", "Windows default",
133 SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
134 change_string_list( speaker_list, speaker_list )
135 add_float( "directx-volume", 1.0f,
136 VOLUME_TEXT, VOLUME_LONGTEXT, true )
137 change_integer_range( DSBVOLUME_MIN, DSBVOLUME_MAX )
139 set_callbacks( Open, Close )
142 /*****************************************************************************
143 * OpenAudio: open the audio device
144 *****************************************************************************
145 * This function opens and setups Direct Sound.
146 *****************************************************************************/
147 static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
152 const char * const * ppsz_compare = speaker_list;
154 msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
156 /* Retrieve config values */
157 var_Create( p_aout, "directx-audio-float32",
158 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
159 psz_speaker = var_CreateGetString( p_aout, "directx-audio-speaker" );
161 while ( *ppsz_compare != NULL )
163 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
170 if ( *ppsz_compare == NULL )
172 msg_Err( p_aout, "(%s) isn't valid speaker setup option", psz_speaker );
173 msg_Err( p_aout, "Defaulting to Windows default speaker config");
178 /* Initialise DirectSound */
179 if( InitDirectSound( p_aout ) )
181 msg_Err( p_aout, "cannot initialize DirectSound" );
187 DWORD ui_speaker_config;
189 /* Check the speaker configuration to determine which channel config
190 * should be the default */
191 if( FAILED( IDirectSound_GetSpeakerConfig( p_aout->sys->p_dsobject,
192 &ui_speaker_config ) ) )
194 ui_speaker_config = DSSPEAKER_STEREO;
195 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
197 fmt->i_physical_channels = AOUT_CHANS_2_0;
199 const char *name = "Unknown";
200 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
202 case DSSPEAKER_7POINT1:
203 case DSSPEAKER_7POINT1_SURROUND:
205 fmt->i_physical_channels = AOUT_CHANS_7_1;
207 case DSSPEAKER_5POINT1:
208 case DSSPEAKER_5POINT1_SURROUND:
210 fmt->i_physical_channels = AOUT_CHANS_5_1;
214 fmt->i_physical_channels = AOUT_CHANS_4_0;
216 #if 0 /* Lots of people just get their settings wrong and complain that
217 * this is a problem with VLC so just don't ever set mono by default. */
220 fmt->i_physical_channels = AOUT_CHAN_CENTER;
223 case DSSPEAKER_SURROUND: /* XXX: stereo, really? -- Courmisch */
226 case DSSPEAKER_STEREO:
230 msg_Dbg( p_aout, "%s speaker config: %s", "Windows", name );
233 { /* Overriden speaker configuration */
234 const char *name = "Non-existant";
239 fmt->i_physical_channels = AOUT_CHAN_CENTER;
243 fmt->i_physical_channels = AOUT_CHANS_2_0;
247 fmt->i_physical_channels = AOUT_CHANS_4_0;
251 fmt->i_physical_channels = AOUT_CHANS_5_1;
255 fmt->i_physical_channels = AOUT_CHANS_7_1;
258 msg_Dbg( p_aout, "%s speaker config: %s", "VLC", name );
261 /* Open the device */
262 if ( AOUT_FMT_SPDIF( fmt )
263 && var_InheritBool( p_aout, "spdif" )
264 && CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL, fmt->i_physical_channels,
265 aout_FormatNbChannels( fmt ), fmt->i_rate, true )
268 msg_Dbg( p_aout, "using A/52 pass-through over S/PDIF" );
269 fmt->i_format = VLC_CODEC_SPDIFL;
271 /* Calculate the frame size in bytes */
272 fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
273 fmt->i_frame_length = A52_FRAME_NB;
277 aout_FormatPrepare( fmt );
279 if( CreateDSBufferPCM( p_aout, &fmt->i_format,
280 fmt->i_physical_channels, fmt->i_rate, false )
283 msg_Err( p_aout, "cannot open directx audio device" );
287 p_aout->sys->i_write = 0;
289 /* Force volume update */
290 VolumeSet( p_aout, p_aout->sys->volume.volume );
291 MuteSet( p_aout, p_aout->sys->volume.mute );
293 /* then launch the notification thread */
294 p_aout->time_get = TimeGet;
296 p_aout->pause = Pause;
297 p_aout->flush = Flush;
306 /*****************************************************************************
307 * Play: we'll start playing the directsound buffer here because at least here
308 * we know the first buffer has been put in the aout fifo and we also
310 *****************************************************************************/
311 static void Play( audio_output_t *p_aout, block_t *p_buffer )
313 if( FillBuffer( p_aout, p_buffer ) == VLC_SUCCESS )
315 /* start playing the buffer */
316 HRESULT dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
317 0, 0, DSBPLAY_LOOPING );
318 if( dsresult == DSERR_BUFFERLOST )
320 IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
321 dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
322 0, 0, DSBPLAY_LOOPING );
324 if( dsresult != DS_OK )
325 msg_Err( p_aout, "cannot start playing buffer" );
329 static int VolumeSet( audio_output_t *p_aout, float volume )
331 aout_sys_t *sys = p_aout->sys;
334 /* Convert UI volume to linear factor (cube) */
335 float vol = volume * volume * volume;
337 /* millibels from linear amplification */
338 LONG mb = lroundf(2000.f * log10f(vol));
340 /* Clamp to allowed DirectSound range */
341 static_assert( DSBVOLUME_MIN < DSBVOLUME_MAX, "DSBVOLUME_* confused" );
342 if( mb > DSBVOLUME_MAX )
347 if( mb <= DSBVOLUME_MIN )
351 sys->volume.volume = volume;
352 if( !sys->volume.mute && sys->p_dsbuffer &&
353 IDirectSoundBuffer_SetVolume( sys->p_dsbuffer, mb ) != DS_OK )
355 /* Convert back to UI volume */
356 aout_VolumeReport( p_aout, volume );
358 if( var_InheritBool( p_aout, "volume-save" ) )
359 config_PutFloat( p_aout, "directx-volume", volume );
363 static int MuteSet( audio_output_t *p_aout, bool mute )
366 aout_sys_t *sys = p_aout->sys;
368 sys->volume.mute = mute;
370 if( sys->p_dsbuffer )
371 res = IDirectSoundBuffer_SetVolume( sys->p_dsbuffer,
372 mute? DSBVOLUME_MIN : sys->volume.mb );
374 aout_MuteReport( p_aout, mute );
375 return (res != DS_OK);
378 /*****************************************************************************
379 * CloseAudio: close the audio device
380 *****************************************************************************/
381 static void Stop( audio_output_t *p_aout )
383 aout_sys_t *p_sys = p_aout->sys;
384 msg_Dbg( p_aout, "closing audio device" );
386 if( p_sys->p_notify )
387 IDirectSoundNotify_Release(p_sys->p_notify );
388 p_sys->p_notify = NULL;
390 IDirectSoundBuffer_Stop( p_sys->p_dsbuffer );
391 /* release the secondary buffer */
392 DestroyDSBuffer( p_aout );
394 /* finally release the DirectSound object */
395 if( p_sys->p_dsobject )
396 IDirectSound_Release( p_sys->p_dsobject );
399 /*****************************************************************************
400 * InitDirectSound: handle all the gory details of DirectSound initialisation
401 *****************************************************************************/
402 static int InitDirectSound( audio_output_t *p_aout )
404 aout_sys_t *sys = p_aout->sys;
405 GUID guid, *p_guid = NULL;
406 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
408 OurDirectSoundCreate = (void *)
409 GetProcAddress( p_aout->sys->hdsound_dll,
410 "DirectSoundCreate" );
411 if( OurDirectSoundCreate == NULL )
413 msg_Warn( p_aout, "GetProcAddress FAILED" );
417 char *dev = var_GetNonEmptyString( p_aout, "directx-audio-device" );
420 LPOLESTR lpsz = ToWide( dev );
423 if( SUCCEEDED( IIDFromString( lpsz, &guid ) ) )
426 msg_Err( p_aout, "bad device GUID: %ls", lpsz );
430 /* Create the direct sound object */
431 if FAILED( OurDirectSoundCreate( p_guid, &sys->p_dsobject, NULL ) )
433 msg_Warn( p_aout, "cannot create a direct sound device" );
437 /* Set DirectSound Cooperative level, ie what control we want over Windows
438 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
439 * settings of the primary buffer, but also that only the sound of our
440 * application will be hearable when it will have the focus.
441 * !!! (this is not really working as intended yet because to set the
442 * cooperative level you need the window handle of your application, and
443 * I don't know of any easy way to get it. Especially since we might play
444 * sound without any video, and so what window handle should we use ???
445 * The hack for now is to use the Desktop window handle - it seems to be
447 #if !VLC_WINSTORE_APP
448 if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
452 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
458 sys->p_dsobject = NULL;
463 /*****************************************************************************
464 * CreateDSBuffer: Creates a direct sound buffer of the required format.
465 *****************************************************************************
466 * This function creates the buffer we'll use to play audio.
467 * In DirectSound there are two kinds of buffers:
468 * - the primary buffer: which is the actual buffer that the soundcard plays
469 * - the secondary buffer(s): these buffers are the one actually used by
470 * applications and DirectSound takes care of mixing them into the primary.
472 * Once you create a secondary buffer, you cannot change its format anymore so
473 * you have to release the current one and create another.
474 *****************************************************************************/
475 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
476 int i_channels, int i_nb_channels, int i_rate,
479 WAVEFORMATEXTENSIBLE waveformat;
480 DSBUFFERDESC dsbdesc;
482 /* First set the sound buffer format */
483 waveformat.dwChannelMask = 0;
484 for( unsigned i = 0; pi_vlc_chan_order_wg4[i]; i++ )
485 if( i_channels & pi_vlc_chan_order_wg4[i] )
486 waveformat.dwChannelMask |= pi_channels_in[i];
490 case VLC_CODEC_SPDIFL:
492 /* To prevent channel re-ordering */
493 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
494 waveformat.Format.wBitsPerSample = 16;
495 waveformat.Samples.wValidBitsPerSample =
496 waveformat.Format.wBitsPerSample;
497 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
498 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
502 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
503 waveformat.Samples.wValidBitsPerSample =
504 waveformat.Format.wBitsPerSample;
505 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
506 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
510 waveformat.Format.wBitsPerSample = 16;
511 waveformat.Samples.wValidBitsPerSample =
512 waveformat.Format.wBitsPerSample;
513 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
514 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
518 waveformat.Format.nChannels = i_nb_channels;
519 waveformat.Format.nSamplesPerSec = i_rate;
520 waveformat.Format.nBlockAlign =
521 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
522 waveformat.Format.nAvgBytesPerSec =
523 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
525 p_aout->sys->i_bytes_per_sample = waveformat.Format.nBlockAlign;
526 p_aout->sys->format = i_format;
528 /* Then fill in the direct sound descriptor */
529 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
530 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
531 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 /* Better position accuracy */
532 | DSBCAPS_GLOBALFOCUS /* Allows background playing */
533 | DSBCAPS_CTRLVOLUME /* Allows volume control */
534 | DSBCAPS_CTRLPOSITIONNOTIFY; /* Allow position notifications */
536 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
537 if( i_nb_channels <= 2 )
539 waveformat.Format.cbSize = 0;
543 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
544 waveformat.Format.cbSize =
545 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
547 /* Needed for 5.1 on emu101k */
548 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
551 dsbdesc.dwBufferBytes = DS_BUF_SIZE; /* buffer size */
552 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
554 if FAILED( IDirectSound_CreateSoundBuffer(
555 p_aout->sys->p_dsobject, &dsbdesc,
556 &p_aout->sys->p_dsbuffer, NULL) )
558 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
560 /* Try without DSBCAPS_LOCHARDWARE */
561 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
562 if FAILED( IDirectSound_CreateSoundBuffer(
563 p_aout->sys->p_dsobject, &dsbdesc,
564 &p_aout->sys->p_dsbuffer, NULL) )
569 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
577 /* Stop here if we were just probing */
580 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
581 p_aout->sys->p_dsbuffer = NULL;
585 p_aout->sys->i_rate = i_rate;
586 p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
587 p_aout->sys->chans_to_reorder =
588 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
589 waveformat.dwChannelMask,
590 p_aout->sys->chan_table );
591 if( p_aout->sys->chans_to_reorder )
593 msg_Dbg( p_aout, "channel reordering needed" );
596 if( IDirectSoundBuffer_QueryInterface( p_aout->sys->p_dsbuffer,
597 &IID_IDirectSoundNotify,
598 (void **) &p_aout->sys->p_notify )
602 msg_Err(p_aout, "Couldn't query IDirectSoundNotify");
603 p_aout->sys->p_notify = NULL;
606 FillBuffer(p_aout,NULL);
611 /*****************************************************************************
612 * CreateDSBufferPCM: creates a PCM direct sound buffer.
613 *****************************************************************************
614 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
615 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
616 ****************************************************************************/
617 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
618 int i_channels, int i_rate, bool b_probe )
620 unsigned i_nb_channels = popcount( i_channels );
622 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
623 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
624 i_channels, i_nb_channels, i_rate, b_probe )
627 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16N,
628 i_channels, i_nb_channels, i_rate, b_probe )
635 *i_format = VLC_CODEC_S16N;
641 *i_format = VLC_CODEC_FL32;
646 /*****************************************************************************
648 *****************************************************************************
649 * This function destroys the secondary buffer.
650 *****************************************************************************/
651 static void DestroyDSBuffer( audio_output_t *p_aout )
653 if( p_aout->sys->p_dsbuffer )
655 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
656 p_aout->sys->p_dsbuffer = NULL;
660 /*****************************************************************************
661 * FillBuffer: Fill in one of the direct sound frame buffers.
662 *****************************************************************************
663 * Returns VLC_SUCCESS on success.
664 *****************************************************************************/
665 static int FillBuffer( audio_output_t *p_aout, block_t *p_buffer )
667 aout_sys_t *p_sys = p_aout->sys;
669 size_t towrite = (p_buffer)?p_buffer->i_buffer:DS_BUF_SIZE;
670 void *p_write_position, *p_wrap_around;
671 unsigned long l_bytes1, l_bytes2;
677 if( IDirectSoundBuffer_GetCurrentPosition( p_aout->sys->p_dsbuffer, (LPDWORD) &i_read, NULL) == DS_OK )
679 /* Compute the outer interval between the write and read pointers within the ring buffer */
680 i_buf = (mtime_t)p_aout->sys->i_write - (mtime_t)i_read;
682 i_buf += DS_BUF_SIZE;
687 /* Before copying anything, we have to lock the buffer */
688 dsresult = IDirectSoundBuffer_Lock(
689 p_sys->p_dsbuffer, /* DS buffer */
690 p_aout->sys->i_write, /* Start offset */
691 i_buf, /* Number of bytes */
692 &p_write_position, /* Address of lock start */
693 &l_bytes1, /* Count of bytes locked before wrap around */
694 &p_wrap_around, /* Buffer address (if wrap around) */
695 &l_bytes2, /* Count of bytes after wrap around */
696 0 ); /* Flags: DSBLOCK_FROMWRITECURSOR is buggy */
697 if( dsresult == DSERR_BUFFERLOST )
699 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
700 dsresult = IDirectSoundBuffer_Lock(
702 p_aout->sys->i_write,
710 if( dsresult != DS_OK )
712 msg_Warn( p_aout, "cannot lock buffer" );
713 if( p_buffer ) block_Release( p_buffer );
717 if( p_buffer == NULL )
719 memset( p_write_position, 0, l_bytes1 );
720 memset( p_wrap_around, 0, l_bytes2 );
724 if( p_sys->chans_to_reorder )
725 /* Do the channel reordering here */
726 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
727 p_sys->chans_to_reorder, p_sys->chan_table,
731 i_size = ( p_buffer->i_buffer < l_bytes1 ) ? p_buffer->i_buffer : l_bytes1;
732 memcpy( p_write_position, p_buffer->p_buffer, i_size );
733 memset( (uint8_t*) p_write_position + i_size, 0, l_bytes1 - i_size );
735 if( l_bytes1 < p_buffer->i_buffer)
737 /* Compute the remaining buffer space to be written */
738 i_buf = i_buf - i_size - (l_bytes1 - i_size);
739 i_size = ( p_buffer->i_buffer - l_bytes1 < l_bytes2 ) ? p_buffer->i_buffer - l_bytes1 : l_bytes2;
740 memcpy( p_wrap_around, p_buffer->p_buffer + l_bytes1, i_size );
741 memset( (uint8_t*) p_wrap_around + i_size, 0, i_buf - i_size );
743 block_Release( p_buffer );
746 /* Now the data has been copied, unlock the buffer */
747 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
748 p_wrap_around, l_bytes2 );
750 p_sys->i_write += towrite;
751 p_sys->i_write %= DS_BUF_SIZE;
763 static int CALLBACK DeviceEnumCallback( LPGUID guid, LPCWSTR desc,
764 LPCWSTR mod, LPVOID data )
766 ds_list_t *list = data;
769 StringFromGUID2( guid, buf, 48 );
772 list->ids = xrealloc( list->ids, list->count * sizeof(char *) );
773 list->names = xrealloc( list->names, list->count * sizeof(char *) );
774 list->ids[list->count - 1] = FromWide( buf );
775 list->names[list->count - 1] = FromWide( desc );
776 if( list->ids == NULL || list->names == NULL )
783 /*****************************************************************************
784 * ReloadDirectXDevices: store the list of devices in preferences
785 *****************************************************************************/
786 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
787 char ***values, char ***descs )
791 .ids = xmalloc(sizeof (char *)),
792 .names = xmalloc(sizeof (char *)),
794 list.ids[0] = xstrdup("");
795 list.names[0] = xstrdup(_("Default"));
799 HANDLE hdsound_dll = LoadLibrary(_T("DSOUND.DLL"));
800 if( hdsound_dll == NULL )
802 msg_Warn( p_this, "cannot open DSOUND.DLL" );
806 /* Get DirectSoundEnumerate */
807 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID) =
808 (void *)GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
809 if( OurDirectSoundEnumerate != NULL )
811 OurDirectSoundEnumerate( DeviceEnumCallback, &list );
812 msg_Dbg( p_this, "found %u devices", list.count );
814 FreeLibrary(hdsound_dll);
822 static int DeviceSelect (audio_output_t *aout, const char *id)
824 var_SetString(aout, "directx-audio-device", (id != NULL) ? id : "");
825 aout_DeviceReport (aout, id);
826 aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
830 static int Open(vlc_object_t *obj)
832 audio_output_t *aout = (audio_output_t *)obj;
833 aout_sys_t *sys = calloc(1, sizeof (*sys));
834 if (unlikely(sys == NULL))
837 sys->hdsound_dll = LoadLibrary(_T("DSOUND.DLL"));
838 if (sys->hdsound_dll == NULL)
840 msg_Warn(aout, "cannot open DSOUND.DLL");
848 aout->volume_set = VolumeSet;
849 aout->mute_set = MuteSet;
850 aout->device_select = DeviceSelect;
853 sys->volume.volume = var_InheritFloat(aout, "directx-volume");
854 aout_VolumeReport(aout, sys->volume.volume );
855 MuteSet(aout, var_InheritBool(aout, "mute"));
857 sys->hnotify_evt = CreateEvent(NULL, FALSE, TRUE, NULL);
858 if( !sys->hnotify_evt )
860 msg_Err(aout, "cannot create Event");
861 FreeLibrary(sys->hdsound_dll);
866 /* DirectSound does not support hot-plug events (unless with WASAPI) */
868 int count = ReloadDirectXDevices(obj, NULL, &ids, &names);
871 for (int i = 0; i < count; i++)
873 aout_HotplugReport(aout, ids[i], names[i]);
881 char *dev = var_CreateGetNonEmptyString(aout, "directx-audio-device");
882 aout_DeviceReport(aout, dev);
888 static void Close(vlc_object_t *obj)
890 audio_output_t *aout = (audio_output_t *)obj;
891 aout_sys_t *sys = aout->sys;
893 var_Destroy(aout, "directx-audio-device");
894 CloseHandle(sys->hnotify_evt);
895 FreeLibrary(sys->hdsound_dll); /* free DSOUND.DLL */
899 static void Flush ( audio_output_t * aout, bool drain )
901 IDirectSoundBuffer_Stop( aout->sys->p_dsbuffer );
903 IDirectSoundBuffer_SetCurrentPosition( aout->sys->p_dsbuffer,
904 aout->sys->i_write );
907 static void Pause( audio_output_t * aout, bool pause, mtime_t date )
911 IDirectSoundBuffer_Stop( aout->sys->p_dsbuffer );
913 IDirectSoundBuffer_Play( aout->sys->p_dsbuffer, 0, 0, DSBPLAY_LOOPING );
917 static int TimeGet( audio_output_t * aout, mtime_t * delay )
922 if( IDirectSoundBuffer_GetCurrentPosition( aout->sys->p_dsbuffer, (LPDWORD) &read, NULL) != DS_OK )
927 size = (mtime_t)aout->sys->i_write - (mtime_t) read;
931 *delay = ( size / aout->sys->i_bytes_per_sample ) * CLOCK_FREQ / aout->sys->i_rate;