1 /*****************************************************************************
2 * directsound.c: DirectSound audio output plugin for VLC
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 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_charset.h>
40 #include "audio_output/windows_audio_common.h"
41 #include "audio_output/mmdevice.h"
42 #include <mmdeviceapi.h>
44 #define DS_BUF_SIZE (6*1024*1024)
46 static int Open( vlc_object_t * );
47 static void Close( vlc_object_t * );
48 static HRESULT StreamStart( aout_stream_t *, audio_sample_format_t *,
50 static HRESULT StreamStop( aout_stream_t * );
51 static int ReloadDirectXDevices( vlc_object_t *, const char *,
53 static void * PlayedDataEraser( void * );
54 /* Speaker setup override options list */
55 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
56 "Quad", "5.1", "7.1" };
58 /*****************************************************************************
60 *****************************************************************************/
61 #define DEVICE_TEXT N_("Output device")
62 #define DEVICE_LONGTEXT N_("Select your audio output device")
64 #define SPEAKER_TEXT N_("Speaker configuration")
65 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
66 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
68 #define VOLUME_TEXT N_("Audio volume")
69 #define VOLUME_LONGTEXT N_("Audio volume in hundredths of decibels (dB).")
72 set_description( N_("DirectX audio output") )
73 set_shortname( "DirectX" )
74 set_capability( "audio output", 100 )
75 set_category( CAT_AUDIO )
76 set_subcategory( SUBCAT_AUDIO_AOUT )
77 add_shortcut( "directx", "aout_directx", "directsound", "dsound" )
79 add_string( "directx-audio-device", NULL,
80 DEVICE_TEXT, DEVICE_LONGTEXT, false )
81 change_string_cb( ReloadDirectXDevices )
82 add_obsolete_string( "directx-audio-device-name")
83 add_bool( "directx-audio-float32", true, FLOAT_TEXT,
84 FLOAT_LONGTEXT, true )
85 add_string( "directx-audio-speaker", "Windows default",
86 SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
87 change_string_list( speaker_list, speaker_list )
88 add_float( "directx-volume", 1.0f,
89 VOLUME_TEXT, VOLUME_LONGTEXT, true )
90 change_integer_range( DSBVOLUME_MIN, DSBVOLUME_MAX )
92 set_callbacks( Open, Close )
95 set_capability( "aout stream", 30 )
96 set_callbacks( StreamStart, StreamStop )
99 typedef struct aout_stream_sys
101 LPDIRECTSOUND p_dsobject; /*< main Direct Sound object */
102 LPDIRECTSOUNDBUFFER p_dsbuffer; /*< the sound buffer we use (direct sound
103 takes care of mixing all the secondary
104 buffers into the primary) */
105 LPDIRECTSOUNDNOTIFY p_notify;
107 int i_bytes_per_sample; /*< Size in bytes of one frame */
108 int i_rate; /*< Sample rate */
110 uint8_t chans_to_reorder; /*< Do we need channel reordering? */
111 uint8_t chan_table[AOUT_CHAN_MAX];
112 uint32_t i_channel_mask;
122 vlc_thread_t eraser_thread;
126 * DirectSound audio output method descriptor
128 * This structure is part of the audio output thread descriptor.
129 * It describes the direct sound specific properties of an audio device.
140 HINSTANCE hdsound_dll; /*< handle of the opened dsound DLL */
143 static HRESULT Flush( aout_stream_sys_t *sys, bool drain);
144 static HRESULT TimeGet( aout_stream_sys_t *sys, mtime_t *delay )
150 hr = IDirectSoundBuffer_GetStatus( sys->p_dsbuffer, &status );
153 if( !(status & DSBSTATUS_PLAYING) )
154 return DSERR_INVALIDCALL ;
156 hr = IDirectSoundBuffer_GetCurrentPosition( sys->p_dsbuffer, &read, NULL );
160 size = (mtime_t)read - sys->i_last_read;
162 /* GetCurrentPosition cannot be trusted if the return doesn't change
163 * Just return an error */
165 return DSERR_GENERIC ;
170 sys->i_last_read = read;
172 if( sys->i_data < 0 )
176 *delay = ( sys->i_data / sys->i_bytes_per_sample ) * CLOCK_FREQ / sys->i_rate;
181 static HRESULT StreamTimeGet( aout_stream_t *s, mtime_t *delay )
183 return TimeGet( s->sys, delay );
186 static int OutputTimeGet( audio_output_t *aout, mtime_t *delay )
188 return (TimeGet( &aout->sys->s, delay ) == DS_OK) ? 0 : -1;
192 * Fills in one of the DirectSound frame buffers.
194 * @return VLC_SUCCESS on success.
196 static HRESULT FillBuffer( vlc_object_t *obj, aout_stream_sys_t *p_sys,
199 size_t towrite = (p_buffer != NULL) ? p_buffer->i_buffer : DS_BUF_SIZE;
200 void *p_write_position, *p_wrap_around;
201 unsigned long l_bytes1, l_bytes2;
204 vlc_mutex_lock( &p_sys->lock );
206 /* Before copying anything, we have to lock the buffer */
207 dsresult = IDirectSoundBuffer_Lock(
208 p_sys->p_dsbuffer, /* DS buffer */
209 p_sys->i_write, /* Start offset */
210 towrite, /* Number of bytes */
211 &p_write_position, /* Address of lock start */
212 &l_bytes1, /* Count of bytes locked before wrap around */
213 &p_wrap_around, /* Buffer address (if wrap around) */
214 &l_bytes2, /* Count of bytes after wrap around */
215 0 ); /* Flags: DSBLOCK_FROMWRITECURSOR is buggy */
216 if( dsresult == DSERR_BUFFERLOST )
218 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
219 dsresult = IDirectSoundBuffer_Lock(
229 if( dsresult != DS_OK )
231 msg_Warn( obj, "cannot lock buffer" );
232 if( p_buffer != NULL )
233 block_Release( p_buffer );
234 vlc_mutex_unlock( &p_sys->lock );
238 if( p_buffer == NULL )
240 memset( p_write_position, 0, l_bytes1 );
241 memset( p_wrap_around, 0, l_bytes2 );
245 if( p_sys->chans_to_reorder ) /* Do the channel reordering here */
246 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
247 p_sys->chans_to_reorder, p_sys->chan_table,
250 memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
251 if( p_wrap_around && l_bytes2 )
252 memcpy( p_wrap_around, p_buffer->p_buffer + l_bytes1, l_bytes2 );
254 if( unlikely( ( l_bytes1 + l_bytes2 ) < p_buffer->i_buffer ) )
255 msg_Err( obj, "Buffer overrun");
257 block_Release( p_buffer );
260 /* Now the data has been copied, unlock the buffer */
261 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
262 p_wrap_around, l_bytes2 );
264 p_sys->i_write += towrite;
265 p_sys->i_write %= DS_BUF_SIZE;
266 p_sys->i_data += towrite;
267 vlc_mutex_unlock( &p_sys->lock );
272 static HRESULT Play( vlc_object_t *obj, aout_stream_sys_t *sys,
276 dsresult = FillBuffer( obj, sys, p_buffer );
277 if( dsresult != DS_OK )
280 /* start playing the buffer */
281 dsresult = IDirectSoundBuffer_Play( sys->p_dsbuffer, 0, 0,
283 if( dsresult == DSERR_BUFFERLOST )
285 IDirectSoundBuffer_Restore( sys->p_dsbuffer );
286 dsresult = IDirectSoundBuffer_Play( sys->p_dsbuffer,
287 0, 0, DSBPLAY_LOOPING );
289 if( dsresult != DS_OK )
290 msg_Err( obj, "cannot start playing buffer" );
293 vlc_mutex_lock( &sys->lock );
294 sys->b_playing = true;
295 vlc_cond_signal(&sys->cond);
296 vlc_mutex_unlock( &sys->lock );
302 static HRESULT StreamPlay( aout_stream_t *s, block_t *block )
304 return Play( VLC_OBJECT(s), s->sys, block );
307 static void OutputPlay( audio_output_t *aout, block_t *block )
309 Play( VLC_OBJECT(aout), &aout->sys->s, block );
312 static HRESULT Pause( aout_stream_sys_t *sys, bool pause )
317 hr = IDirectSoundBuffer_Stop( sys->p_dsbuffer );
319 hr = IDirectSoundBuffer_Play( sys->p_dsbuffer, 0, 0, DSBPLAY_LOOPING );
322 vlc_mutex_lock( &sys->lock );
323 sys->b_playing = !pause;
325 vlc_cond_signal( &sys->cond );
326 vlc_mutex_unlock( &sys->lock );
331 static HRESULT StreamPause( aout_stream_t *s, bool pause )
333 return Pause( s->sys, pause );
336 static void OutputPause( audio_output_t *aout, bool pause, mtime_t date )
338 Pause( &aout->sys->s, pause );
342 static HRESULT Flush( aout_stream_sys_t *sys, bool drain)
344 HRESULT ret = IDirectSoundBuffer_Stop( sys->p_dsbuffer );
345 if( ret == DS_OK && !drain )
347 vlc_mutex_lock(&sys->lock);
349 sys->i_last_read = sys->i_write;
350 IDirectSoundBuffer_SetCurrentPosition( sys->p_dsbuffer, sys->i_write);
351 sys->b_playing = false;
352 vlc_mutex_unlock(&sys->lock);
357 static HRESULT StreamFlush( aout_stream_t *s )
359 return Flush( s->sys, false );
362 static void OutputFlush( audio_output_t *aout, bool drain )
364 aout_stream_sys_t *sys = &aout->sys->s;
369 * Creates a DirectSound buffer of the required format.
371 * This function creates the buffer we'll use to play audio.
372 * In DirectSound there are two kinds of buffers:
373 * - the primary buffer: which is the actual buffer that the soundcard plays
374 * - the secondary buffer(s): these buffers are the one actually used by
375 * applications and DirectSound takes care of mixing them into the primary.
377 * Once you create a secondary buffer, you cannot change its format anymore so
378 * you have to release the current one and create another.
380 static HRESULT CreateDSBuffer( vlc_object_t *obj, aout_stream_sys_t *sys,
381 int i_format, int i_channels, int i_nb_channels,
382 int i_rate, bool b_probe )
384 WAVEFORMATEXTENSIBLE waveformat;
385 DSBUFFERDESC dsbdesc;
388 /* First set the sound buffer format */
389 waveformat.dwChannelMask = 0;
390 for( unsigned i = 0; pi_vlc_chan_order_wg4[i]; i++ )
391 if( i_channels & pi_vlc_chan_order_wg4[i] )
392 waveformat.dwChannelMask |= pi_channels_in[i];
396 case VLC_CODEC_SPDIFL:
398 /* To prevent channel re-ordering */
399 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
400 waveformat.Format.wBitsPerSample = 16;
401 waveformat.Samples.wValidBitsPerSample =
402 waveformat.Format.wBitsPerSample;
403 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
404 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
408 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
409 waveformat.Samples.wValidBitsPerSample =
410 waveformat.Format.wBitsPerSample;
411 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
412 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
416 waveformat.Format.wBitsPerSample = 16;
417 waveformat.Samples.wValidBitsPerSample =
418 waveformat.Format.wBitsPerSample;
419 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
420 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
424 waveformat.Format.nChannels = i_nb_channels;
425 waveformat.Format.nSamplesPerSec = i_rate;
426 waveformat.Format.nBlockAlign =
427 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
428 waveformat.Format.nAvgBytesPerSec =
429 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
431 sys->i_bytes_per_sample = waveformat.Format.nBlockAlign;
432 sys->format = i_format;
434 /* Then fill in the direct sound descriptor */
435 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
436 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
437 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 /* Better position accuracy */
438 | DSBCAPS_GLOBALFOCUS /* Allows background playing */
439 | DSBCAPS_CTRLVOLUME /* Allows volume control */
440 | DSBCAPS_CTRLPOSITIONNOTIFY; /* Allow position notifications */
442 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
443 if( i_nb_channels <= 2 )
445 waveformat.Format.cbSize = 0;
449 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
450 waveformat.Format.cbSize =
451 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
453 /* Needed for 5.1 on emu101k */
454 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
457 dsbdesc.dwBufferBytes = DS_BUF_SIZE; /* buffer size */
458 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
460 /* CreateSoundBuffer doesn't allow volume control for non-PCM buffers */
461 if ( i_format == VLC_CODEC_SPDIFL )
462 dsbdesc.dwFlags &= ~DSBCAPS_CTRLVOLUME;
464 hr = IDirectSound_CreateSoundBuffer( sys->p_dsobject, &dsbdesc,
465 &sys->p_dsbuffer, NULL );
468 if( !(dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE) )
471 /* Try without DSBCAPS_LOCHARDWARE */
472 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
473 hr = IDirectSound_CreateSoundBuffer( sys->p_dsobject, &dsbdesc,
474 &sys->p_dsbuffer, NULL );
478 msg_Dbg( obj, "couldn't use hardware sound buffer" );
481 /* Stop here if we were just probing */
484 IDirectSoundBuffer_Release( sys->p_dsbuffer );
485 sys->p_dsbuffer = NULL;
489 sys->i_rate = i_rate;
490 sys->i_channel_mask = waveformat.dwChannelMask;
491 sys->chans_to_reorder =
492 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
493 waveformat.dwChannelMask, sys->chan_table );
494 if( sys->chans_to_reorder )
495 msg_Dbg( obj, "channel reordering needed" );
497 hr = IDirectSoundBuffer_QueryInterface( sys->p_dsbuffer,
498 &IID_IDirectSoundNotify,
499 (void **) &sys->p_notify );
502 msg_Err( obj, "Couldn't query IDirectSoundNotify" );
503 sys->p_notify = NULL;
506 FillBuffer( obj, sys, NULL );
511 * Creates a PCM DirectSound buffer.
513 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
514 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
516 static HRESULT CreateDSBufferPCM( vlc_object_t *obj, aout_stream_sys_t *sys,
517 vlc_fourcc_t *i_format, int i_channels,
518 int i_rate, bool b_probe )
521 unsigned i_nb_channels = popcount( i_channels );
523 if( var_GetBool( obj, "directx-audio-float32" ) )
525 hr = CreateDSBuffer( obj, sys, VLC_CODEC_FL32, i_channels,
526 i_nb_channels, i_rate, b_probe );
529 *i_format = VLC_CODEC_FL32;
534 hr = CreateDSBuffer( obj, sys, VLC_CODEC_S16N, i_channels, i_nb_channels,
538 *i_format = VLC_CODEC_S16N;
546 * Closes the audio device.
548 static HRESULT Stop( aout_stream_sys_t *p_sys )
550 vlc_mutex_lock( &p_sys->lock );
551 p_sys->b_playing = true;
552 vlc_cond_signal( &p_sys->cond );
553 vlc_mutex_unlock( &p_sys->lock );
554 vlc_cancel( p_sys->eraser_thread );
555 vlc_join( p_sys->eraser_thread, NULL );
556 vlc_cond_destroy( &p_sys->cond );
557 vlc_mutex_destroy( &p_sys->lock );
559 if( p_sys->p_notify != NULL )
561 IDirectSoundNotify_Release(p_sys->p_notify );
562 p_sys->p_notify = NULL;
564 if( p_sys->p_dsbuffer != NULL )
566 IDirectSoundBuffer_Stop( p_sys->p_dsbuffer );
567 IDirectSoundBuffer_Release( p_sys->p_dsbuffer );
568 p_sys->p_dsbuffer = NULL;
570 if( p_sys->p_dsobject != NULL )
572 IDirectSound_Release( p_sys->p_dsobject );
573 p_sys->p_dsobject = NULL;
578 static HRESULT StreamStop( aout_stream_t *s )
587 static void OutputStop( audio_output_t *aout )
589 msg_Dbg( aout, "closing audio device" );
590 Stop( &aout->sys->s );
593 static HRESULT Start( vlc_object_t *obj, aout_stream_sys_t *sys,
594 audio_sample_format_t *restrict fmt )
596 #if !VLC_WINSTORE_APP
597 /* Set DirectSound Cooperative level, ie what control we want over Windows
598 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
599 * settings of the primary buffer, but also that only the sound of our
600 * application will be hearable when it will have the focus.
601 * !!! (this is not really working as intended yet because to set the
602 * cooperative level you need the window handle of your application, and
603 * I don't know of any easy way to get it. Especially since we might play
604 * sound without any video, and so what window handle should we use ???
605 * The hack for now is to use the Desktop window handle - it seems to be
607 if( IDirectSound_SetCooperativeLevel( sys->p_dsobject, GetDesktopWindow(),
609 msg_Warn( obj, "cannot set direct sound cooperative level" );
612 const char *const *ppsz_compare = speaker_list;
617 /* Retrieve config values */
618 var_Create( obj, "directx-audio-float32",
619 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
620 psz_speaker = var_CreateGetString( obj, "directx-audio-speaker" );
622 while ( *ppsz_compare != NULL )
624 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
631 if ( *ppsz_compare == NULL )
633 msg_Err( obj, "(%s) isn't valid speaker setup option", psz_speaker );
634 msg_Err( obj, "Defaulting to Windows default speaker config");
639 vlc_mutex_init(&sys->lock);
640 vlc_cond_init(&sys->cond);
642 if( AOUT_FMT_SPDIF( fmt ) && var_InheritBool( obj, "spdif" ) )
644 hr = CreateDSBuffer( obj, sys, VLC_CODEC_SPDIFL,
645 fmt->i_physical_channels,
646 aout_FormatNbChannels(fmt), fmt->i_rate, false );
649 msg_Dbg( obj, "using A/52 pass-through over S/PDIF" );
650 fmt->i_format = VLC_CODEC_SPDIFL;
652 /* Calculate the frame size in bytes */
653 fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
654 fmt->i_frame_length = A52_FRAME_NB;
658 hr = DSERR_UNSUPPORTED;
664 DWORD ui_speaker_config;
665 int i_channels = 2; /* Default to stereo */
666 int i_orig_channels = aout_FormatNbChannels( fmt );
668 /* Check the speaker configuration to determine which channel
669 * config should be the default */
670 hr = IDirectSound_GetSpeakerConfig( sys->p_dsobject,
671 &ui_speaker_config );
674 ui_speaker_config = DSSPEAKER_STEREO;
675 msg_Dbg( obj, "GetSpeakerConfig failed" );
678 const char *name = "Unknown";
679 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
681 case DSSPEAKER_7POINT1:
682 case DSSPEAKER_7POINT1_SURROUND:
686 case DSSPEAKER_5POINT1:
687 case DSSPEAKER_5POINT1_SURROUND:
695 #if 0 /* Lots of people just get their settings wrong and complain that
696 * this is a problem with VLC so just don't ever set mono by default. */
702 case DSSPEAKER_SURROUND:
706 case DSSPEAKER_STEREO:
712 if( i_channels >= i_orig_channels )
713 i_channels = i_orig_channels;
715 msg_Dbg( obj, "%s speaker config: %s and stream has "
716 "%d channels, using %d channels", "Windows", name,
717 i_orig_channels, i_channels );
722 fmt->i_physical_channels = AOUT_CHANS_7_1;
726 fmt->i_physical_channels = AOUT_CHANS_5_1;
730 fmt->i_physical_channels = AOUT_CHANS_4_0;
733 fmt->i_physical_channels = AOUT_CHANS_2_0;
738 { /* Overriden speaker configuration */
739 const char *name = "Non-existant";
744 fmt->i_physical_channels = AOUT_CHAN_CENTER;
748 fmt->i_physical_channels = AOUT_CHANS_2_0;
752 fmt->i_physical_channels = AOUT_CHANS_4_0;
756 fmt->i_physical_channels = AOUT_CHANS_5_1;
760 fmt->i_physical_channels = AOUT_CHANS_7_1;
763 msg_Dbg( obj, "%s speaker config: %s", "VLC", name );
766 /* Open the device */
767 aout_FormatPrepare( fmt );
769 hr = CreateDSBufferPCM( obj, sys, &fmt->i_format,
770 fmt->i_physical_channels, fmt->i_rate, false );
773 msg_Err( obj, "cannot open directx audio device" );
778 fmt->i_original_channels = fmt->i_physical_channels;
780 int ret = vlc_clone(&sys->eraser_thread, PlayedDataEraser, (void*) obj,
781 VLC_THREAD_PRIORITY_LOW);
782 if( unlikely( ret ) )
785 msg_Err( obj, "Couldn't start eraser thread" );
787 vlc_cond_destroy(&sys->cond);
788 vlc_mutex_destroy(&sys->lock);
790 if( sys->p_notify != NULL )
792 IDirectSoundNotify_Release( sys->p_notify );
793 sys->p_notify = NULL;
795 IDirectSoundBuffer_Release( sys->p_dsbuffer );
796 sys->p_dsbuffer = NULL;
797 IDirectSound_Release( sys->p_dsobject );
798 sys->p_dsobject = NULL;
801 sys->b_playing = false;
803 sys->i_last_read = 0;
813 static HRESULT StreamStart( aout_stream_t *s,
814 audio_sample_format_t *restrict fmt,
817 aout_stream_sys_t *sys = calloc( 1, sizeof( *sys ) );
818 if( unlikely(sys == NULL) )
819 return E_OUTOFMEMORY;
821 DIRECTX_AUDIO_ACTIVATION_PARAMS params = {
822 .cbDirectXAudioActivationParams = sizeof( params ),
823 .guidAudioSession = *sid,
824 .dwAudioStreamFlags = 0,
828 PropVariantInit( &prop );
830 prop.blob.cbSize = sizeof( params );
831 prop.blob.pBlobData = (BYTE *)¶ms;
834 HRESULT hr = aout_stream_Activate( s, &IID_IDirectSound, &prop, &pv );
838 sys->p_dsobject = pv;
840 hr = Start( VLC_OBJECT(s), sys, fmt );
845 s->time_get = StreamTimeGet;
846 s->play = StreamPlay;
847 s->pause = StreamPause;
848 s->flush = StreamFlush;
856 * Handles all the gory details of DirectSound initialization.
858 static int InitDirectSound( audio_output_t *p_aout )
860 aout_sys_t *sys = p_aout->sys;
861 GUID guid, *p_guid = NULL;
862 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
864 OurDirectSoundCreate = (void *)
865 GetProcAddress( p_aout->sys->hdsound_dll,
866 "DirectSoundCreate" );
867 if( OurDirectSoundCreate == NULL )
869 msg_Warn( p_aout, "GetProcAddress FAILED" );
873 char *dev = var_GetNonEmptyString( p_aout, "directx-audio-device" );
876 LPOLESTR lpsz = ToWide( dev );
879 if( SUCCEEDED( IIDFromString( lpsz, &guid ) ) )
882 msg_Err( p_aout, "bad device GUID: %ls", lpsz );
886 /* Create the direct sound object */
887 if FAILED( OurDirectSoundCreate( p_guid, &sys->s.p_dsobject, NULL ) )
889 msg_Warn( p_aout, "cannot create a direct sound device" );
896 sys->s.p_dsobject = NULL;
901 static int VolumeSet( audio_output_t *p_aout, float volume )
903 aout_sys_t *sys = p_aout->sys;
906 /* Directsound doesn't support amplification, so we use software
907 gain if we need it and only for this */
908 float gain = volume > 1.f ? volume * volume * volume : 1.f;
909 aout_GainRequest( p_aout, gain );
911 /* millibels from linear amplification */
912 LONG mb = lroundf( 6000.f * log10f( __MIN( volume, 1.f ) ));
914 /* Clamp to allowed DirectSound range */
915 static_assert( DSBVOLUME_MIN < DSBVOLUME_MAX, "DSBVOLUME_* confused" );
916 if( mb > DSBVOLUME_MAX )
921 if( mb <= DSBVOLUME_MIN )
925 sys->volume.volume = volume;
926 if( !sys->volume.mute && sys->s.p_dsbuffer != NULL &&
927 IDirectSoundBuffer_SetVolume( sys->s.p_dsbuffer, mb ) != DS_OK )
929 /* Convert back to UI volume */
930 aout_VolumeReport( p_aout, volume );
932 if( var_InheritBool( p_aout, "volume-save" ) )
933 config_PutFloat( p_aout, "directx-volume", volume );
937 static int MuteSet( audio_output_t *p_aout, bool mute )
940 aout_sys_t *sys = p_aout->sys;
942 sys->volume.mute = mute;
944 if( sys->s.p_dsbuffer != NULL )
945 res = IDirectSoundBuffer_SetVolume( sys->s.p_dsbuffer,
946 mute? DSBVOLUME_MIN : sys->volume.mb );
948 aout_MuteReport( p_aout, mute );
949 return (res != DS_OK);
952 static int OutputStart( audio_output_t *p_aout,
953 audio_sample_format_t *restrict fmt )
955 msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
957 /* Initialise DirectSound */
958 if( InitDirectSound( p_aout ) )
960 msg_Err( p_aout, "cannot initialize DirectSound" );
964 HRESULT hr = Start( VLC_OBJECT(p_aout), &p_aout->sys->s, fmt );
968 /* Force volume update */
969 VolumeSet( p_aout, p_aout->sys->volume.volume );
970 MuteSet( p_aout, p_aout->sys->volume.mute );
972 /* then launch the notification thread */
973 p_aout->time_get = OutputTimeGet;
974 p_aout->play = OutputPlay;
975 p_aout->pause = OutputPause;
976 p_aout->flush = OutputFlush;
988 static int CALLBACK DeviceEnumCallback( LPGUID guid, LPCWSTR desc,
989 LPCWSTR mod, LPVOID data )
991 ds_list_t *list = data;
994 if( StringFromGUID2( guid, buf, 48 ) <= 0 )
998 list->ids = xrealloc( list->ids, list->count * sizeof(char *) );
999 list->names = xrealloc( list->names, list->count * sizeof(char *) );
1000 list->ids[list->count - 1] = FromWide( buf );
1001 list->names[list->count - 1] = FromWide( desc );
1002 if( list->ids == NULL || list->names == NULL )
1010 * Stores the list of devices in preferences
1012 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1013 char ***values, char ***descs )
1017 .ids = xmalloc(sizeof (char *)),
1018 .names = xmalloc(sizeof (char *)),
1020 list.ids[0] = xstrdup("");
1021 list.names[0] = xstrdup(_("Default"));
1025 HANDLE hdsound_dll = LoadLibrary(_T("DSOUND.DLL"));
1026 if( hdsound_dll == NULL )
1028 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1032 /* Get DirectSoundEnumerate */
1033 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID) =
1034 (void *)GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1035 if( OurDirectSoundEnumerate != NULL )
1037 OurDirectSoundEnumerate( DeviceEnumCallback, &list );
1038 msg_Dbg( p_this, "found %u devices", list.count );
1040 FreeLibrary(hdsound_dll);
1044 *descs = list.names;
1048 static int DeviceSelect (audio_output_t *aout, const char *id)
1050 var_SetString(aout, "directx-audio-device", (id != NULL) ? id : "");
1051 aout_DeviceReport (aout, id);
1052 aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
1056 static int Open(vlc_object_t *obj)
1058 audio_output_t *aout = (audio_output_t *)obj;
1060 HINSTANCE hdsound_dll = LoadLibrary(_T("DSOUND.DLL"));
1061 if (hdsound_dll == NULL)
1063 msg_Warn(aout, "cannot open DSOUND.DLL");
1064 return VLC_EGENERIC;
1067 aout_sys_t *sys = calloc(1, sizeof (*sys));
1068 if (unlikely(sys == NULL))
1071 sys->hdsound_dll = hdsound_dll;
1074 aout->start = OutputStart;
1075 aout->stop = OutputStop;
1076 aout->volume_set = VolumeSet;
1077 aout->mute_set = MuteSet;
1078 aout->device_select = DeviceSelect;
1081 sys->volume.volume = var_InheritFloat(aout, "directx-volume");
1082 aout_VolumeReport(aout, sys->volume.volume );
1083 MuteSet(aout, var_InheritBool(aout, "mute"));
1085 /* DirectSound does not support hot-plug events (unless with WASAPI) */
1086 char **ids, **names;
1087 int count = ReloadDirectXDevices(obj, NULL, &ids, &names);
1090 for (int i = 0; i < count; i++)
1092 aout_HotplugReport(aout, ids[i], names[i]);
1100 char *dev = var_CreateGetNonEmptyString(aout, "directx-audio-device");
1101 aout_DeviceReport(aout, dev);
1107 static void Close(vlc_object_t *obj)
1109 audio_output_t *aout = (audio_output_t *)obj;
1110 aout_sys_t *sys = aout->sys;
1112 var_Destroy(aout, "directx-audio-device");
1113 FreeLibrary(sys->hdsound_dll); /* free DSOUND.DLL */
1117 static void * PlayedDataEraser( void * data )
1119 const audio_output_t *aout = (audio_output_t *) data;
1120 aout_stream_sys_t *p_sys = &aout->sys->s;
1121 void *p_write_position, *p_wrap_around;
1122 unsigned long l_bytes1, l_bytes2;
1124 int64_t toerase, tosleep;
1129 int canc = vlc_savecancel();
1130 vlc_mutex_lock( &p_sys->lock );
1132 while( !p_sys->b_playing )
1133 vlc_cond_wait( &p_sys->cond, &p_sys->lock );
1138 dsresult = IDirectSoundBuffer_GetCurrentPosition( p_sys->p_dsbuffer,
1140 if( dsresult == DS_OK )
1142 int64_t max = (int64_t) i_read - (int64_t) p_sys->i_write;
1147 tosleep += DS_BUF_SIZE;
1149 tosleep = ( tosleep / p_sys->i_bytes_per_sample ) * CLOCK_FREQ / p_sys->i_rate;
1152 tosleep = __MAX( tosleep, 20000 );
1153 dsresult = IDirectSoundBuffer_Lock( p_sys->p_dsbuffer,
1161 if( dsresult == DSERR_BUFFERLOST )
1163 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
1164 dsresult = IDirectSoundBuffer_Lock( p_sys->p_dsbuffer,
1173 if( dsresult != DS_OK )
1176 memset( p_write_position, 0, l_bytes1 );
1177 memset( p_wrap_around, 0, l_bytes2 );
1179 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1180 p_wrap_around, l_bytes2 );
1182 vlc_mutex_unlock(&p_sys->lock);
1183 vlc_restorecancel(canc);