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 );
151 if(hr != DS_OK || !(status & DSBSTATUS_PLAYING))
154 hr = IDirectSoundBuffer_GetCurrentPosition( sys->p_dsbuffer, &read, NULL );
158 size = (mtime_t)read - sys->i_last_read;
160 /* GetCurrentPosition cannot be trusted if the return doesn't change
161 * Just return an error */
168 sys->i_last_read = read;
170 if( sys->i_data < 0 )
174 *delay = ( sys->i_data / sys->i_bytes_per_sample ) * CLOCK_FREQ / sys->i_rate;
179 static HRESULT StreamTimeGet( aout_stream_t *s, mtime_t *delay )
181 return TimeGet( s->sys, delay );
184 static int OutputTimeGet( audio_output_t *aout, mtime_t *delay )
186 return (TimeGet( &aout->sys->s, delay ) == DS_OK) ? 0 : -1;
190 * Fills in one of the DirectSound frame buffers.
192 * @return VLC_SUCCESS on success.
194 static HRESULT FillBuffer( vlc_object_t *obj, aout_stream_sys_t *p_sys,
197 size_t towrite = (p_buffer != NULL) ? p_buffer->i_buffer : DS_BUF_SIZE;
198 void *p_write_position, *p_wrap_around;
199 unsigned long l_bytes1, l_bytes2;
202 vlc_mutex_lock( &p_sys->lock );
204 /* Before copying anything, we have to lock the buffer */
205 dsresult = IDirectSoundBuffer_Lock(
206 p_sys->p_dsbuffer, /* DS buffer */
207 p_sys->i_write, /* Start offset */
208 towrite, /* Number of bytes */
209 &p_write_position, /* Address of lock start */
210 &l_bytes1, /* Count of bytes locked before wrap around */
211 &p_wrap_around, /* Buffer address (if wrap around) */
212 &l_bytes2, /* Count of bytes after wrap around */
213 0 ); /* Flags: DSBLOCK_FROMWRITECURSOR is buggy */
214 if( dsresult == DSERR_BUFFERLOST )
216 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
217 dsresult = IDirectSoundBuffer_Lock(
227 if( dsresult != DS_OK )
229 msg_Warn( obj, "cannot lock buffer" );
230 if( p_buffer != NULL )
231 block_Release( p_buffer );
232 vlc_mutex_unlock( &p_sys->lock );
236 if( p_buffer == NULL )
238 memset( p_write_position, 0, l_bytes1 );
239 memset( p_wrap_around, 0, l_bytes2 );
243 if( p_sys->chans_to_reorder ) /* Do the channel reordering here */
244 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
245 p_sys->chans_to_reorder, p_sys->chan_table,
248 memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
249 if( p_wrap_around && l_bytes2 )
250 memcpy( p_wrap_around, p_buffer->p_buffer + l_bytes1, l_bytes2 );
252 if( unlikely( ( l_bytes1 + l_bytes2 ) < p_buffer->i_buffer ) )
253 msg_Err( obj, "Buffer overrun");
255 block_Release( p_buffer );
258 /* Now the data has been copied, unlock the buffer */
259 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
260 p_wrap_around, l_bytes2 );
262 p_sys->i_write += towrite;
263 p_sys->i_write %= DS_BUF_SIZE;
264 p_sys->i_data += towrite;
265 vlc_mutex_unlock( &p_sys->lock );
270 static HRESULT Play( vlc_object_t *obj, aout_stream_sys_t *sys,
274 dsresult = FillBuffer( obj, sys, p_buffer );
275 if( dsresult != DS_OK )
278 /* start playing the buffer */
279 dsresult = IDirectSoundBuffer_Play( sys->p_dsbuffer, 0, 0,
281 if( dsresult == DSERR_BUFFERLOST )
283 IDirectSoundBuffer_Restore( sys->p_dsbuffer );
284 dsresult = IDirectSoundBuffer_Play( sys->p_dsbuffer,
285 0, 0, DSBPLAY_LOOPING );
287 if( dsresult != DS_OK )
288 msg_Err( obj, "cannot start playing buffer" );
291 vlc_mutex_lock( &sys->lock );
292 sys->b_playing = true;
293 vlc_cond_signal(&sys->cond);
294 vlc_mutex_unlock( &sys->lock );
300 static HRESULT StreamPlay( aout_stream_t *s, block_t *block )
302 return Play( VLC_OBJECT(s), s->sys, block );
305 static void OutputPlay( audio_output_t *aout, block_t *block )
307 Play( VLC_OBJECT(aout), &aout->sys->s, block );
310 static HRESULT Pause( aout_stream_sys_t *sys, bool pause )
315 hr = IDirectSoundBuffer_Stop( sys->p_dsbuffer );
317 hr = IDirectSoundBuffer_Play( sys->p_dsbuffer, 0, 0, DSBPLAY_LOOPING );
320 vlc_mutex_lock( &sys->lock );
321 sys->b_playing = !pause;
323 vlc_cond_signal( &sys->cond );
324 vlc_mutex_unlock( &sys->lock );
329 static HRESULT StreamPause( aout_stream_t *s, bool pause )
331 return Pause( s->sys, pause );
334 static void OutputPause( audio_output_t *aout, bool pause, mtime_t date )
336 Pause( &aout->sys->s, pause );
340 static HRESULT Flush( aout_stream_sys_t *sys, bool drain)
342 HRESULT ret = IDirectSoundBuffer_Stop( sys->p_dsbuffer );
343 if( ret == DS_OK && !drain )
345 vlc_mutex_lock(&sys->lock);
347 sys->i_last_read = sys->i_write;
348 IDirectSoundBuffer_SetCurrentPosition( sys->p_dsbuffer, sys->i_write);
349 sys->b_playing = false;
350 vlc_mutex_unlock(&sys->lock);
355 static HRESULT StreamFlush( aout_stream_t *s )
357 return Flush( s->sys, false );
360 static void OutputFlush( audio_output_t *aout, bool drain )
362 aout_stream_sys_t *sys = &aout->sys->s;
367 * Creates a DirectSound buffer of the required format.
369 * This function creates the buffer we'll use to play audio.
370 * In DirectSound there are two kinds of buffers:
371 * - the primary buffer: which is the actual buffer that the soundcard plays
372 * - the secondary buffer(s): these buffers are the one actually used by
373 * applications and DirectSound takes care of mixing them into the primary.
375 * Once you create a secondary buffer, you cannot change its format anymore so
376 * you have to release the current one and create another.
378 static HRESULT CreateDSBuffer( vlc_object_t *obj, aout_stream_sys_t *sys,
379 int i_format, int i_channels, int i_nb_channels,
380 int i_rate, bool b_probe )
382 WAVEFORMATEXTENSIBLE waveformat;
383 DSBUFFERDESC dsbdesc;
386 /* First set the sound buffer format */
387 waveformat.dwChannelMask = 0;
388 for( unsigned i = 0; pi_vlc_chan_order_wg4[i]; i++ )
389 if( i_channels & pi_vlc_chan_order_wg4[i] )
390 waveformat.dwChannelMask |= pi_channels_in[i];
394 case VLC_CODEC_SPDIFL:
396 /* To prevent channel re-ordering */
397 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
398 waveformat.Format.wBitsPerSample = 16;
399 waveformat.Samples.wValidBitsPerSample =
400 waveformat.Format.wBitsPerSample;
401 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
402 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
406 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
407 waveformat.Samples.wValidBitsPerSample =
408 waveformat.Format.wBitsPerSample;
409 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
410 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
414 waveformat.Format.wBitsPerSample = 16;
415 waveformat.Samples.wValidBitsPerSample =
416 waveformat.Format.wBitsPerSample;
417 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
418 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
422 waveformat.Format.nChannels = i_nb_channels;
423 waveformat.Format.nSamplesPerSec = i_rate;
424 waveformat.Format.nBlockAlign =
425 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
426 waveformat.Format.nAvgBytesPerSec =
427 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
429 sys->i_bytes_per_sample = waveformat.Format.nBlockAlign;
430 sys->format = i_format;
432 /* Then fill in the direct sound descriptor */
433 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
434 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
435 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 /* Better position accuracy */
436 | DSBCAPS_GLOBALFOCUS /* Allows background playing */
437 | DSBCAPS_CTRLVOLUME /* Allows volume control */
438 | DSBCAPS_CTRLPOSITIONNOTIFY; /* Allow position notifications */
440 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
441 if( i_nb_channels <= 2 )
443 waveformat.Format.cbSize = 0;
447 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
448 waveformat.Format.cbSize =
449 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
451 /* Needed for 5.1 on emu101k */
452 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
455 dsbdesc.dwBufferBytes = DS_BUF_SIZE; /* buffer size */
456 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
458 /* CreateSoundBuffer doesn't allow volume control for non-PCM buffers */
459 if ( i_format == VLC_CODEC_SPDIFL )
460 dsbdesc.dwFlags &= ~DSBCAPS_CTRLVOLUME;
462 hr = IDirectSound_CreateSoundBuffer( sys->p_dsobject, &dsbdesc,
463 &sys->p_dsbuffer, NULL );
466 if( !(dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE) )
469 /* Try without DSBCAPS_LOCHARDWARE */
470 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
471 hr = IDirectSound_CreateSoundBuffer( sys->p_dsobject, &dsbdesc,
472 &sys->p_dsbuffer, NULL );
476 msg_Dbg( obj, "couldn't use hardware sound buffer" );
479 /* Stop here if we were just probing */
482 IDirectSoundBuffer_Release( sys->p_dsbuffer );
483 sys->p_dsbuffer = NULL;
487 sys->i_rate = i_rate;
488 sys->i_channel_mask = waveformat.dwChannelMask;
489 sys->chans_to_reorder =
490 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
491 waveformat.dwChannelMask, sys->chan_table );
492 if( sys->chans_to_reorder )
493 msg_Dbg( obj, "channel reordering needed" );
495 hr = IDirectSoundBuffer_QueryInterface( sys->p_dsbuffer,
496 &IID_IDirectSoundNotify,
497 (void **) &sys->p_notify );
500 msg_Err( obj, "Couldn't query IDirectSoundNotify" );
501 sys->p_notify = NULL;
504 FillBuffer( obj, sys, NULL );
509 * Creates a PCM DirectSound buffer.
511 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
512 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
514 static HRESULT CreateDSBufferPCM( vlc_object_t *obj, aout_stream_sys_t *sys,
515 vlc_fourcc_t *i_format, int i_channels,
516 int i_rate, bool b_probe )
519 unsigned i_nb_channels = popcount( i_channels );
521 if( var_GetBool( obj, "directx-audio-float32" ) )
523 hr = CreateDSBuffer( obj, sys, VLC_CODEC_FL32, i_channels,
524 i_nb_channels, i_rate, b_probe );
527 *i_format = VLC_CODEC_FL32;
532 hr = CreateDSBuffer( obj, sys, VLC_CODEC_S16N, i_channels, i_nb_channels,
536 *i_format = VLC_CODEC_S16N;
544 * Closes the audio device.
546 static HRESULT Stop( aout_stream_sys_t *p_sys )
548 vlc_mutex_lock( &p_sys->lock );
549 p_sys->b_playing = true;
550 vlc_cond_signal( &p_sys->cond );
551 vlc_mutex_unlock( &p_sys->lock );
552 vlc_cancel( p_sys->eraser_thread );
553 vlc_join( p_sys->eraser_thread, NULL );
554 vlc_cond_destroy( &p_sys->cond );
555 vlc_mutex_destroy( &p_sys->lock );
557 if( p_sys->p_notify != NULL )
559 IDirectSoundNotify_Release(p_sys->p_notify );
560 p_sys->p_notify = NULL;
562 if( p_sys->p_dsbuffer != NULL )
564 IDirectSoundBuffer_Stop( p_sys->p_dsbuffer );
565 IDirectSoundBuffer_Release( p_sys->p_dsbuffer );
566 p_sys->p_dsbuffer = NULL;
568 if( p_sys->p_dsobject != NULL )
570 IDirectSound_Release( p_sys->p_dsobject );
571 p_sys->p_dsobject = NULL;
576 static HRESULT StreamStop( aout_stream_t *s )
585 static void OutputStop( audio_output_t *aout )
587 msg_Dbg( aout, "closing audio device" );
588 Stop( &aout->sys->s );
591 static HRESULT Start( vlc_object_t *obj, aout_stream_sys_t *sys,
592 audio_sample_format_t *restrict fmt )
594 #if !VLC_WINSTORE_APP
595 /* Set DirectSound Cooperative level, ie what control we want over Windows
596 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
597 * settings of the primary buffer, but also that only the sound of our
598 * application will be hearable when it will have the focus.
599 * !!! (this is not really working as intended yet because to set the
600 * cooperative level you need the window handle of your application, and
601 * I don't know of any easy way to get it. Especially since we might play
602 * sound without any video, and so what window handle should we use ???
603 * The hack for now is to use the Desktop window handle - it seems to be
605 if( IDirectSound_SetCooperativeLevel( sys->p_dsobject, GetDesktopWindow(),
607 msg_Warn( obj, "cannot set direct sound cooperative level" );
610 const char *const *ppsz_compare = speaker_list;
615 /* Retrieve config values */
616 var_Create( obj, "directx-audio-float32",
617 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
618 psz_speaker = var_CreateGetString( obj, "directx-audio-speaker" );
620 while ( *ppsz_compare != NULL )
622 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
629 if ( *ppsz_compare == NULL )
631 msg_Err( obj, "(%s) isn't valid speaker setup option", psz_speaker );
632 msg_Err( obj, "Defaulting to Windows default speaker config");
637 vlc_mutex_init(&sys->lock);
638 vlc_cond_init(&sys->cond);
640 if( AOUT_FMT_SPDIF( fmt ) && var_InheritBool( obj, "spdif" ) )
642 hr = CreateDSBuffer( obj, sys, VLC_CODEC_SPDIFL,
643 fmt->i_physical_channels,
644 aout_FormatNbChannels(fmt), fmt->i_rate, false );
647 msg_Dbg( obj, "using A/52 pass-through over S/PDIF" );
648 fmt->i_format = VLC_CODEC_SPDIFL;
650 /* Calculate the frame size in bytes */
651 fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
652 fmt->i_frame_length = A52_FRAME_NB;
656 hr = DSERR_UNSUPPORTED;
662 DWORD ui_speaker_config;
663 int i_channels = 2; /* Default to stereo */
664 int i_orig_channels = aout_FormatNbChannels( fmt );
666 /* Check the speaker configuration to determine which channel
667 * config should be the default */
668 hr = IDirectSound_GetSpeakerConfig( sys->p_dsobject,
669 &ui_speaker_config );
672 ui_speaker_config = DSSPEAKER_STEREO;
673 msg_Dbg( obj, "GetSpeakerConfig failed" );
676 const char *name = "Unknown";
677 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
679 case DSSPEAKER_7POINT1:
680 case DSSPEAKER_7POINT1_SURROUND:
684 case DSSPEAKER_5POINT1:
685 case DSSPEAKER_5POINT1_SURROUND:
693 #if 0 /* Lots of people just get their settings wrong and complain that
694 * this is a problem with VLC so just don't ever set mono by default. */
700 case DSSPEAKER_SURROUND:
704 case DSSPEAKER_STEREO:
710 if( i_channels >= i_orig_channels )
711 i_channels = i_orig_channels;
713 msg_Dbg( obj, "%s speaker config: %s and stream has "
714 "%d channels, using %d channels", "Windows", name,
715 i_orig_channels, i_channels );
720 fmt->i_physical_channels = AOUT_CHANS_7_1;
724 fmt->i_physical_channels = AOUT_CHANS_5_1;
728 fmt->i_physical_channels = AOUT_CHANS_4_0;
731 fmt->i_physical_channels = AOUT_CHANS_2_0;
736 { /* Overriden speaker configuration */
737 const char *name = "Non-existant";
742 fmt->i_physical_channels = AOUT_CHAN_CENTER;
746 fmt->i_physical_channels = AOUT_CHANS_2_0;
750 fmt->i_physical_channels = AOUT_CHANS_4_0;
754 fmt->i_physical_channels = AOUT_CHANS_5_1;
758 fmt->i_physical_channels = AOUT_CHANS_7_1;
761 msg_Dbg( obj, "%s speaker config: %s", "VLC", name );
764 /* Open the device */
765 aout_FormatPrepare( fmt );
767 hr = CreateDSBufferPCM( obj, sys, &fmt->i_format,
768 fmt->i_physical_channels, fmt->i_rate, false );
771 msg_Err( obj, "cannot open directx audio device" );
776 fmt->i_original_channels = fmt->i_physical_channels;
778 int ret = vlc_clone(&sys->eraser_thread, PlayedDataEraser, (void*) obj,
779 VLC_THREAD_PRIORITY_LOW);
780 if( unlikely( ret ) )
783 msg_Err( obj, "Couldn't start eraser thread" );
785 vlc_cond_destroy(&sys->cond);
786 vlc_mutex_destroy(&sys->lock);
788 if( sys->p_notify != NULL )
790 IDirectSoundNotify_Release( sys->p_notify );
791 sys->p_notify = NULL;
793 IDirectSoundBuffer_Release( sys->p_dsbuffer );
794 sys->p_dsbuffer = NULL;
795 IDirectSound_Release( sys->p_dsobject );
796 sys->p_dsobject = NULL;
799 sys->b_playing = false;
801 sys->i_last_read = 0;
811 static HRESULT StreamStart( aout_stream_t *s,
812 audio_sample_format_t *restrict fmt,
815 aout_stream_sys_t *sys = calloc( 1, sizeof( *sys ) );
816 if( unlikely(sys == NULL) )
817 return E_OUTOFMEMORY;
819 DIRECTX_AUDIO_ACTIVATION_PARAMS params = {
820 .cbDirectXAudioActivationParams = sizeof( params ),
821 .guidAudioSession = *sid,
822 .dwAudioStreamFlags = 0,
826 PropVariantInit( &prop );
828 prop.blob.cbSize = sizeof( params );
829 prop.blob.pBlobData = (BYTE *)¶ms;
832 HRESULT hr = aout_stream_Activate( s, &IID_IDirectSound, &prop, &pv );
836 sys->p_dsobject = pv;
838 hr = Start( VLC_OBJECT(s), sys, fmt );
843 s->time_get = StreamTimeGet;
844 s->play = StreamPlay;
845 s->pause = StreamPause;
846 s->flush = StreamFlush;
854 * Handles all the gory details of DirectSound initialization.
856 static int InitDirectSound( audio_output_t *p_aout )
858 aout_sys_t *sys = p_aout->sys;
859 GUID guid, *p_guid = NULL;
860 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
862 OurDirectSoundCreate = (void *)
863 GetProcAddress( p_aout->sys->hdsound_dll,
864 "DirectSoundCreate" );
865 if( OurDirectSoundCreate == NULL )
867 msg_Warn( p_aout, "GetProcAddress FAILED" );
871 char *dev = var_GetNonEmptyString( p_aout, "directx-audio-device" );
874 LPOLESTR lpsz = ToWide( dev );
877 if( SUCCEEDED( IIDFromString( lpsz, &guid ) ) )
880 msg_Err( p_aout, "bad device GUID: %ls", lpsz );
884 /* Create the direct sound object */
885 if FAILED( OurDirectSoundCreate( p_guid, &sys->s.p_dsobject, NULL ) )
887 msg_Warn( p_aout, "cannot create a direct sound device" );
894 sys->s.p_dsobject = NULL;
899 static int VolumeSet( audio_output_t *p_aout, float volume )
901 aout_sys_t *sys = p_aout->sys;
904 /* Directsound doesn't support amplification, so we use software
905 gain if we need it and only for this */
906 float gain = volume > 1.f ? volume * volume * volume : 1.f;
907 aout_GainRequest( p_aout, gain );
909 /* millibels from linear amplification */
910 LONG mb = lroundf( 6000.f * log10f( __MIN( volume, 1.f ) ));
912 /* Clamp to allowed DirectSound range */
913 static_assert( DSBVOLUME_MIN < DSBVOLUME_MAX, "DSBVOLUME_* confused" );
914 if( mb > DSBVOLUME_MAX )
919 if( mb <= DSBVOLUME_MIN )
923 sys->volume.volume = volume;
924 if( !sys->volume.mute && sys->s.p_dsbuffer != NULL &&
925 IDirectSoundBuffer_SetVolume( sys->s.p_dsbuffer, mb ) != DS_OK )
927 /* Convert back to UI volume */
928 aout_VolumeReport( p_aout, volume );
930 if( var_InheritBool( p_aout, "volume-save" ) )
931 config_PutFloat( p_aout, "directx-volume", volume );
935 static int MuteSet( audio_output_t *p_aout, bool mute )
938 aout_sys_t *sys = p_aout->sys;
940 sys->volume.mute = mute;
942 if( sys->s.p_dsbuffer != NULL )
943 res = IDirectSoundBuffer_SetVolume( sys->s.p_dsbuffer,
944 mute? DSBVOLUME_MIN : sys->volume.mb );
946 aout_MuteReport( p_aout, mute );
947 return (res != DS_OK);
950 static int OutputStart( audio_output_t *p_aout,
951 audio_sample_format_t *restrict fmt )
953 msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
955 /* Initialise DirectSound */
956 if( InitDirectSound( p_aout ) )
958 msg_Err( p_aout, "cannot initialize DirectSound" );
962 HRESULT hr = Start( VLC_OBJECT(p_aout), &p_aout->sys->s, fmt );
966 /* Force volume update */
967 VolumeSet( p_aout, p_aout->sys->volume.volume );
968 MuteSet( p_aout, p_aout->sys->volume.mute );
970 /* then launch the notification thread */
971 p_aout->time_get = OutputTimeGet;
972 p_aout->play = OutputPlay;
973 p_aout->pause = OutputPause;
974 p_aout->flush = OutputFlush;
986 static int CALLBACK DeviceEnumCallback( LPGUID guid, LPCWSTR desc,
987 LPCWSTR mod, LPVOID data )
989 ds_list_t *list = data;
992 if( StringFromGUID2( guid, buf, 48 ) <= 0 )
996 list->ids = xrealloc( list->ids, list->count * sizeof(char *) );
997 list->names = xrealloc( list->names, list->count * sizeof(char *) );
998 list->ids[list->count - 1] = FromWide( buf );
999 list->names[list->count - 1] = FromWide( desc );
1000 if( list->ids == NULL || list->names == NULL )
1008 * Stores the list of devices in preferences
1010 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1011 char ***values, char ***descs )
1015 .ids = xmalloc(sizeof (char *)),
1016 .names = xmalloc(sizeof (char *)),
1018 list.ids[0] = xstrdup("");
1019 list.names[0] = xstrdup(_("Default"));
1023 HANDLE hdsound_dll = LoadLibrary(_T("DSOUND.DLL"));
1024 if( hdsound_dll == NULL )
1026 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1030 /* Get DirectSoundEnumerate */
1031 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID) =
1032 (void *)GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1033 if( OurDirectSoundEnumerate != NULL )
1035 OurDirectSoundEnumerate( DeviceEnumCallback, &list );
1036 msg_Dbg( p_this, "found %u devices", list.count );
1038 FreeLibrary(hdsound_dll);
1042 *descs = list.names;
1046 static int DeviceSelect (audio_output_t *aout, const char *id)
1048 var_SetString(aout, "directx-audio-device", (id != NULL) ? id : "");
1049 aout_DeviceReport (aout, id);
1050 aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
1054 static int Open(vlc_object_t *obj)
1056 audio_output_t *aout = (audio_output_t *)obj;
1058 HINSTANCE hdsound_dll = LoadLibrary(_T("DSOUND.DLL"));
1059 if (hdsound_dll == NULL)
1061 msg_Warn(aout, "cannot open DSOUND.DLL");
1062 return VLC_EGENERIC;
1065 aout_sys_t *sys = calloc(1, sizeof (*sys));
1066 if (unlikely(sys == NULL))
1069 sys->hdsound_dll = hdsound_dll;
1072 aout->start = OutputStart;
1073 aout->stop = OutputStop;
1074 aout->volume_set = VolumeSet;
1075 aout->mute_set = MuteSet;
1076 aout->device_select = DeviceSelect;
1079 sys->volume.volume = var_InheritFloat(aout, "directx-volume");
1080 aout_VolumeReport(aout, sys->volume.volume );
1081 MuteSet(aout, var_InheritBool(aout, "mute"));
1083 /* DirectSound does not support hot-plug events (unless with WASAPI) */
1084 char **ids, **names;
1085 int count = ReloadDirectXDevices(obj, NULL, &ids, &names);
1088 for (int i = 0; i < count; i++)
1090 aout_HotplugReport(aout, ids[i], names[i]);
1098 char *dev = var_CreateGetNonEmptyString(aout, "directx-audio-device");
1099 aout_DeviceReport(aout, dev);
1105 static void Close(vlc_object_t *obj)
1107 audio_output_t *aout = (audio_output_t *)obj;
1108 aout_sys_t *sys = aout->sys;
1110 var_Destroy(aout, "directx-audio-device");
1111 FreeLibrary(sys->hdsound_dll); /* free DSOUND.DLL */
1115 static void * PlayedDataEraser( void * data )
1117 const audio_output_t *aout = (audio_output_t *) data;
1118 aout_stream_sys_t *p_sys = &aout->sys->s;
1119 void *p_write_position, *p_wrap_around;
1120 unsigned long l_bytes1, l_bytes2;
1122 int64_t toerase, tosleep;
1127 int canc = vlc_savecancel();
1128 vlc_mutex_lock( &p_sys->lock );
1130 while( !p_sys->b_playing )
1131 vlc_cond_wait( &p_sys->cond, &p_sys->lock );
1136 dsresult = IDirectSoundBuffer_GetCurrentPosition( p_sys->p_dsbuffer,
1138 if( dsresult == DS_OK )
1140 int64_t max = (int64_t) i_read - (int64_t) p_sys->i_write;
1145 tosleep += DS_BUF_SIZE;
1147 tosleep = ( tosleep / p_sys->i_bytes_per_sample ) * CLOCK_FREQ / p_sys->i_rate;
1150 tosleep = __MAX( tosleep, 20000 );
1151 dsresult = IDirectSoundBuffer_Lock( p_sys->p_dsbuffer,
1159 if( dsresult == DSERR_BUFFERLOST )
1161 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
1162 dsresult = IDirectSoundBuffer_Lock( p_sys->p_dsbuffer,
1171 if( dsresult != DS_OK )
1174 memset( p_write_position, 0, l_bytes1 );
1175 memset( p_wrap_around, 0, l_bytes2 );
1177 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1178 p_wrap_around, l_bytes2 );
1180 vlc_mutex_unlock(&p_sys->lock);
1181 vlc_restorecancel(canc);