1 /*****************************************************************************
2 * directx.c: Windows DirectX audio output method
3 *****************************************************************************
4 * Copyright (C) 2001-2009 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_charset.h>
36 #include <vlc_atomic.h>
38 #include "windows_audio_common.h"
40 #define FRAME_SIZE ((int)p_aout->format.i_rate/20) /* Size in samples */
42 /*****************************************************************************
43 * notification_thread_t: DirectX event thread
44 *****************************************************************************/
45 typedef struct notification_thread_t
47 int i_frame_size; /* size in bytes of one frame */
48 int i_write_slot; /* current write position in our circular buffer */
56 } notification_thread_t;
58 /*****************************************************************************
59 * aout_sys_t: directx audio output method descriptor
60 *****************************************************************************
61 * This structure is part of the audio output thread descriptor.
62 * It describes the direct sound specific properties of an audio device.
63 *****************************************************************************/
67 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
69 char * psz_device; /* user defined device name */
72 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
73 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
74 * takes care of mixing all the
75 * secondary buffers into the primary) */
77 notification_thread_t notif; /* DirectSoundThread id */
79 int i_frame_size; /* Size in bytes of one frame */
81 int i_speaker_setup; /* Speaker setup override */
83 bool b_chan_reorder; /* do we need channel reordering */
84 int pi_chan_table[AOUT_CHAN_MAX];
85 uint32_t i_channel_mask;
86 uint32_t i_bits_per_sample;
90 /*****************************************************************************
92 *****************************************************************************/
93 static int OpenAudio ( vlc_object_t * );
94 static void CloseAudio ( vlc_object_t * );
95 static void Play ( audio_output_t *, block_t * );
98 static void Probe ( audio_output_t * );
99 static int InitDirectSound ( audio_output_t * );
100 static int CreateDSBuffer ( audio_output_t *, int, int, int, int, int, bool );
101 static int CreateDSBufferPCM ( audio_output_t *, vlc_fourcc_t*, int, int, int, bool );
102 static void DestroyDSBuffer ( audio_output_t * );
103 static void* DirectSoundThread( void * );
104 static int FillBuffer ( audio_output_t *, int, aout_buffer_t * );
106 static int ReloadDirectXDevices( vlc_object_t *, char const *,
107 vlc_value_t, vlc_value_t, void * );
109 /* Speaker setup override options list */
110 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
111 "Quad", "5.1", "7.1" };
112 static const char *const ppsz_adev[] = {"default", };
113 static const char *const ppsz_adev_text[] = {"default", };
115 /*****************************************************************************
117 *****************************************************************************/
118 #define DEVICE_TEXT N_("Output device")
119 #define DEVICE_LONGTEXT N_("Select your audio output device")
121 #define SPEAKER_TEXT N_("Speaker configuration")
122 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
123 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
126 set_description( N_("DirectX audio output") )
127 set_shortname( "DirectX" )
128 set_capability( "audio output", 100 )
129 set_category( CAT_AUDIO )
130 set_subcategory( SUBCAT_AUDIO_AOUT )
131 add_shortcut( "directx", "directsound" )
133 add_string( "directx-audio-device-name", "default",
134 DEVICE_TEXT, DEVICE_LONGTEXT, false )
135 change_string_list( ppsz_adev, ppsz_adev_text, ReloadDirectXDevices )
136 change_action_add( ReloadDirectXDevices, N_("Refresh list") )
137 add_bool( "directx-audio-float32", false, FLOAT_TEXT,
138 FLOAT_LONGTEXT, true )
139 add_string( "directx-audio-speaker", "Windows default",
140 SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
141 change_string_list( speaker_list, 0, 0 )
143 set_callbacks( OpenAudio, CloseAudio )
146 /*****************************************************************************
147 * OpenAudio: open the audio device
148 *****************************************************************************
149 * This function opens and setups Direct Sound.
150 *****************************************************************************/
151 static int OpenAudio( vlc_object_t *p_this )
153 audio_output_t * p_aout = (audio_output_t *)p_this;
158 const char * const * ppsz_compare = speaker_list;
160 msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
162 /* Allocate structure */
163 p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
164 if( unlikely( p_aout->sys == NULL ) )
167 aout_VolumeSoftInit( p_aout );
169 /* Retrieve config values */
170 var_Create( p_aout, "directx-audio-float32",
171 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
172 psz_speaker = var_CreateGetString( p_aout, "directx-audio-speaker" );
174 while ( *ppsz_compare != NULL )
176 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
183 if ( *ppsz_compare == NULL )
185 msg_Err( p_aout, "(%s) isn't valid speaker setup option", psz_speaker );
186 msg_Err( p_aout, "Defaulting to Windows default speaker config");
190 p_aout->sys->i_speaker_setup = i;
192 /* Initialise DirectSound */
193 if( InitDirectSound( p_aout ) )
195 msg_Err( p_aout, "cannot initialize DirectSound" );
199 if( var_Type( p_aout, "audio-device" ) == 0 )
204 if( var_Get( p_aout, "audio-device", &val ) < 0 )
206 /* Probe() has failed. */
210 /* Open the device */
211 if( val.i_int == AOUT_VAR_SPDIF )
213 p_aout->format.i_format = VLC_CODEC_SPDIFL;
215 /* Calculate the frame size in bytes */
216 p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
217 p_aout->format.i_frame_length = A52_FRAME_NB;
218 p_aout->sys->i_frame_size = p_aout->format.i_bytes_per_frame;
220 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
221 p_aout->format.i_physical_channels,
222 aout_FormatNbChannels( &p_aout->format ),
223 p_aout->format.i_rate,
224 p_aout->sys->i_frame_size, false )
227 msg_Err( p_aout, "cannot open directx audio device" );
231 aout_PacketInit( p_aout, &p_aout->sys->packet, A52_FRAME_NB );
232 aout_VolumeNoneInit( p_aout );
236 if( val.i_int == AOUT_VAR_5_1 )
238 p_aout->format.i_physical_channels
239 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
240 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
243 else if( val.i_int == AOUT_VAR_7_1 )
245 p_aout->format.i_physical_channels
246 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
247 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
248 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
251 else if( val.i_int == AOUT_VAR_3F2R )
253 p_aout->format.i_physical_channels
254 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
255 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
257 else if( val.i_int == AOUT_VAR_2F2R )
259 p_aout->format.i_physical_channels
260 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
261 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
263 else if( val.i_int == AOUT_VAR_MONO )
265 p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
269 p_aout->format.i_physical_channels
270 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
273 if( CreateDSBufferPCM( p_aout, &p_aout->format.i_format,
274 p_aout->format.i_physical_channels,
275 aout_FormatNbChannels( &p_aout->format ),
276 p_aout->format.i_rate, false )
279 msg_Err( p_aout, "cannot open directx audio device" );
283 /* Calculate the frame size in bytes */
284 aout_FormatPrepare( &p_aout->format );
285 aout_PacketInit( p_aout, &p_aout->sys->packet, FRAME_SIZE );
286 aout_VolumeSoftInit( p_aout );
289 /* Now we need to setup our DirectSound play notification structure */
290 vlc_atomic_set(&p_aout->sys->notif.abort, 0);
291 p_aout->sys->notif.event = CreateEvent( 0, FALSE, FALSE, 0 );
292 if( unlikely(p_aout->sys->notif.event == NULL) )
294 p_aout->sys->notif.i_frame_size = p_aout->sys->i_frame_size;
296 /* then launch the notification thread */
297 msg_Dbg( p_aout, "creating DirectSoundThread" );
298 if( vlc_clone( &p_aout->sys->notif.thread, DirectSoundThread, p_aout,
299 VLC_THREAD_PRIORITY_HIGHEST ) )
301 msg_Err( p_aout, "cannot create DirectSoundThread" );
302 CloseHandle( p_aout->sys->notif.event );
303 p_aout->sys->notif.event = NULL;
304 aout_PacketDestroy( p_aout );
308 p_aout->pf_play = Play;
309 p_aout->pf_pause = aout_PacketPause;
310 p_aout->pf_flush = aout_PacketFlush;
315 CloseAudio( VLC_OBJECT(p_aout) );
319 /*****************************************************************************
320 * Probe: probe the audio device for available formats and channels
321 *****************************************************************************/
322 static void Probe( audio_output_t * p_aout )
324 vlc_value_t val, text;
325 vlc_fourcc_t i_format;
326 unsigned int i_physical_channels;
327 DWORD ui_speaker_config;
328 bool is_default_output_set = false;
330 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
331 text.psz_string = _("Audio Device");
332 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
334 /* Test for 5.1 support */
335 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
336 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
337 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
338 if( p_aout->format.i_physical_channels == i_physical_channels )
340 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
341 p_aout->format.i_rate, true )
344 val.i_int = AOUT_VAR_5_1;
345 text.psz_string = (char*) "5.1";
346 var_Change( p_aout, "audio-device",
347 VLC_VAR_ADDCHOICE, &val, &text );
348 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
349 is_default_output_set = true;
350 msg_Dbg( p_aout, "device supports 5.1 channels" );
354 /* Test for 7.1 support */
355 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
356 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
357 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
358 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
359 if( p_aout->format.i_physical_channels == i_physical_channels )
361 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
362 p_aout->format.i_rate, true )
365 val.i_int = AOUT_VAR_7_1;
366 text.psz_string = (char*) "7.1";
367 var_Change( p_aout, "audio-device",
368 VLC_VAR_ADDCHOICE, &val, &text );
369 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
370 is_default_output_set = true;
371 msg_Dbg( p_aout, "device supports 7.1 channels" );
375 /* Test for 3 Front 2 Rear support */
376 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
377 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
379 if( p_aout->format.i_physical_channels == i_physical_channels )
381 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
382 p_aout->format.i_rate, true )
385 val.i_int = AOUT_VAR_3F2R;
386 text.psz_string = _("3 Front 2 Rear");
387 var_Change( p_aout, "audio-device",
388 VLC_VAR_ADDCHOICE, &val, &text );
389 if(!is_default_output_set)
391 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
392 is_default_output_set = true;
394 msg_Dbg( p_aout, "device supports 5 channels" );
398 /* Test for 2 Front 2 Rear support */
399 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
400 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
401 if( ( p_aout->format.i_physical_channels & i_physical_channels )
402 == i_physical_channels )
404 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
405 p_aout->format.i_rate, true )
408 val.i_int = AOUT_VAR_2F2R;
409 text.psz_string = _("2 Front 2 Rear");
410 var_Change( p_aout, "audio-device",
411 VLC_VAR_ADDCHOICE, &val, &text );
412 if(!is_default_output_set)
414 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
415 is_default_output_set = true;
417 msg_Dbg( p_aout, "device supports 4 channels" );
421 /* Test for stereo support */
422 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
423 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
424 p_aout->format.i_rate, true )
427 val.i_int = AOUT_VAR_STEREO;
428 text.psz_string = _("Stereo");
429 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
430 if(!is_default_output_set)
432 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
433 is_default_output_set = true;
434 msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
436 else msg_Dbg( p_aout, "device supports 2 channels" );
439 /* Test for mono support */
440 i_physical_channels = AOUT_CHAN_CENTER;
441 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
442 p_aout->format.i_rate, true )
445 val.i_int = AOUT_VAR_MONO;
446 text.psz_string = _("Mono");
447 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
448 msg_Dbg( p_aout, "device supports 1 channel" );
451 /* Check the speaker configuration to determine which channel config should
453 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->sys->p_dsobject,
454 &ui_speaker_config ) )
456 ui_speaker_config = DSSPEAKER_STEREO;
457 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
459 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
461 case DSSPEAKER_7POINT1:
462 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
463 val.i_int = AOUT_VAR_7_1;
465 case DSSPEAKER_5POINT1:
466 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
467 val.i_int = AOUT_VAR_5_1;
470 msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
471 val.i_int = AOUT_VAR_2F2R;
473 #if 0 /* Lots of people just get their settings wrong and complain that
474 * this is a problem with VLC so just don't ever set mono by default. */
476 val.i_int = AOUT_VAR_MONO;
479 case DSSPEAKER_SURROUND:
480 msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
481 case DSSPEAKER_STEREO:
482 msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
484 /* If nothing else is found, choose stereo output */
485 val.i_int = AOUT_VAR_STEREO;
489 /* Check if we want to override speaker config */
490 switch( p_aout->sys->i_speaker_setup )
492 case 0: /* Default value aka Windows default speaker setup */
495 msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
496 val.i_int = AOUT_VAR_MONO;
499 msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
500 val.i_int = AOUT_VAR_STEREO;
503 msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
504 val.i_int = AOUT_VAR_2F2R;
507 msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
508 val.i_int = AOUT_VAR_5_1;
511 msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
512 val.i_int = AOUT_VAR_7_1;
515 msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
519 var_Set( p_aout, "audio-device", val );
521 /* Test for SPDIF support */
522 if ( AOUT_FMT_SPDIF( &p_aout->format ) )
524 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
525 p_aout->format.i_physical_channels,
526 aout_FormatNbChannels( &p_aout->format ),
527 p_aout->format.i_rate,
528 AOUT_SPDIF_SIZE, true )
531 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
532 val.i_int = AOUT_VAR_SPDIF;
533 text.psz_string = _("A/52 over S/PDIF");
534 var_Change( p_aout, "audio-device",
535 VLC_VAR_ADDCHOICE, &val, &text );
536 if( var_InheritBool( p_aout, "spdif" ) )
537 var_Set( p_aout, "audio-device", val );
541 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
544 /* Probe() has failed. */
545 var_Destroy( p_aout, "audio-device" );
549 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
550 var_TriggerCallback( p_aout, "intf-change" );
553 /*****************************************************************************
554 * Play: we'll start playing the directsound buffer here because at least here
555 * we know the first buffer has been put in the aout fifo and we also
557 *****************************************************************************/
558 static void Play( audio_output_t *p_aout, block_t *p_buffer )
560 /* get the playing date of the first aout buffer */
561 p_aout->sys->notif.start_date = p_buffer->i_pts;
563 /* fill in the first samples (zeroes) */
564 FillBuffer( p_aout, 0, NULL );
566 /* wake up the audio output thread */
567 SetEvent( p_aout->sys->notif.event );
569 aout_PacketPlay( p_aout, p_buffer );
570 p_aout->pf_play = aout_PacketPlay;
573 /*****************************************************************************
574 * CloseAudio: close the audio device
575 *****************************************************************************/
576 static void CloseAudio( vlc_object_t *p_this )
578 audio_output_t * p_aout = (audio_output_t *)p_this;
579 aout_sys_t *p_sys = p_aout->sys;
581 msg_Dbg( p_aout, "closing audio device" );
583 /* kill the position notification thread, if any */
584 if( p_sys->notif.event != NULL )
586 vlc_atomic_set(&p_aout->sys->notif.abort, 1);
587 /* wake up the audio thread if needed */
588 if( p_aout->pf_play == Play )
589 SetEvent( p_sys->notif.event );
591 vlc_join( p_sys->notif.thread, NULL );
592 CloseHandle( p_sys->notif.event );
593 aout_PacketDestroy( p_aout );
596 /* release the secondary buffer */
597 DestroyDSBuffer( p_aout );
599 /* finally release the DirectSound object */
600 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
602 /* free DSOUND.DLL */
603 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
605 free( p_aout->sys->p_device_guid );
609 /*****************************************************************************
610 * CallBackDirectSoundEnum: callback to enumerate available devices
611 *****************************************************************************/
612 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
613 LPCWSTR psz_mod, LPVOID _p_aout )
615 VLC_UNUSED( psz_mod );
617 audio_output_t *p_aout = (audio_output_t *)_p_aout;
619 char *psz_device = FromWide( psz_desc );
620 msg_Dbg( p_aout, "found device: %s", psz_device );
622 if( p_aout->sys->psz_device &&
623 !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
625 /* Use the device corresponding to psz_device */
626 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
627 *p_aout->sys->p_device_guid = *p_guid;
628 msg_Dbg( p_aout, "using device: %s", psz_device );
632 /* If no default device has been selected, chose the first one */
633 if( !p_aout->sys->psz_device && p_guid )
635 p_aout->sys->psz_device = strdup( psz_device );
636 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
637 *p_aout->sys->p_device_guid = *p_guid;
638 msg_Dbg( p_aout, "using device: %s", psz_device );
646 /*****************************************************************************
647 * InitDirectSound: handle all the gory details of DirectSound initialisation
648 *****************************************************************************/
649 static int InitDirectSound( audio_output_t *p_aout )
651 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
652 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
654 p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
655 if( p_aout->sys->hdsound_dll == NULL )
657 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
661 OurDirectSoundCreate = (void *)
662 GetProcAddress( p_aout->sys->hdsound_dll,
663 "DirectSoundCreate" );
664 if( OurDirectSoundCreate == NULL )
666 msg_Warn( p_aout, "GetProcAddress FAILED" );
670 /* Get DirectSoundEnumerate */
671 OurDirectSoundEnumerate = (void *)
672 GetProcAddress( p_aout->sys->hdsound_dll,
673 "DirectSoundEnumerateW" );
674 if( OurDirectSoundEnumerate )
676 p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device-name");
677 /* Attempt enumeration */
678 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
681 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
685 /* Create the direct sound object */
686 if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
687 &p_aout->sys->p_dsobject,
690 msg_Warn( p_aout, "cannot create a direct sound device" );
694 /* Set DirectSound Cooperative level, ie what control we want over Windows
695 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
696 * settings of the primary buffer, but also that only the sound of our
697 * application will be hearable when it will have the focus.
698 * !!! (this is not really working as intended yet because to set the
699 * cooperative level you need the window handle of your application, and
700 * I don't know of any easy way to get it. Especially since we might play
701 * sound without any video, and so what window handle should we use ???
702 * The hack for now is to use the Desktop window handle - it seems to be
704 if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
708 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
714 p_aout->sys->p_dsobject = NULL;
715 if( p_aout->sys->hdsound_dll )
717 FreeLibrary( p_aout->sys->hdsound_dll );
718 p_aout->sys->hdsound_dll = NULL;
724 /*****************************************************************************
725 * CreateDSBuffer: Creates a direct sound buffer of the required format.
726 *****************************************************************************
727 * This function creates the buffer we'll use to play audio.
728 * In DirectSound there are two kinds of buffers:
729 * - the primary buffer: which is the actual buffer that the soundcard plays
730 * - the secondary buffer(s): these buffers are the one actually used by
731 * applications and DirectSound takes care of mixing them into the primary.
733 * Once you create a secondary buffer, you cannot change its format anymore so
734 * you have to release the current one and create another.
735 *****************************************************************************/
736 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
737 int i_channels, int i_nb_channels, int i_rate,
738 int i_bytes_per_frame, bool b_probe )
740 WAVEFORMATEXTENSIBLE waveformat;
741 DSBUFFERDESC dsbdesc;
744 /* First set the sound buffer format */
745 waveformat.dwChannelMask = 0;
746 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
748 if( i_channels & pi_channels_src[i] )
749 waveformat.dwChannelMask |= pi_channels_in[i];
754 case VLC_CODEC_SPDIFL:
756 /* To prevent channel re-ordering */
757 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
758 waveformat.Format.wBitsPerSample = 16;
759 waveformat.Samples.wValidBitsPerSample =
760 waveformat.Format.wBitsPerSample;
761 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
762 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
766 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
767 waveformat.Samples.wValidBitsPerSample =
768 waveformat.Format.wBitsPerSample;
769 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
770 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
774 waveformat.Format.wBitsPerSample = 16;
775 waveformat.Samples.wValidBitsPerSample =
776 waveformat.Format.wBitsPerSample;
777 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
778 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
782 waveformat.Format.nChannels = i_nb_channels;
783 waveformat.Format.nSamplesPerSec = i_rate;
784 waveformat.Format.nBlockAlign =
785 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
786 waveformat.Format.nAvgBytesPerSec =
787 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
789 p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
790 p_aout->sys->i_channels = i_nb_channels;
792 /* Then fill in the direct sound descriptor */
793 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
794 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
795 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
796 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
798 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
799 if( i_nb_channels <= 2 )
801 waveformat.Format.cbSize = 0;
805 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
806 waveformat.Format.cbSize =
807 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
809 /* Needed for 5.1 on emu101k */
810 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
813 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
814 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
816 if FAILED( IDirectSound_CreateSoundBuffer(
817 p_aout->sys->p_dsobject, &dsbdesc,
818 &p_aout->sys->p_dsbuffer, NULL) )
820 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
822 /* Try without DSBCAPS_LOCHARDWARE */
823 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
824 if FAILED( IDirectSound_CreateSoundBuffer(
825 p_aout->sys->p_dsobject, &dsbdesc,
826 &p_aout->sys->p_dsbuffer, NULL) )
831 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
839 /* Stop here if we were just probing */
842 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
843 p_aout->sys->p_dsbuffer = NULL;
847 p_aout->sys->i_frame_size = i_bytes_per_frame;
848 p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
849 p_aout->sys->b_chan_reorder =
850 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
851 waveformat.dwChannelMask, i_nb_channels,
852 p_aout->sys->pi_chan_table );
854 if( p_aout->sys->b_chan_reorder )
856 msg_Dbg( p_aout, "channel reordering needed" );
862 /*****************************************************************************
863 * CreateDSBufferPCM: creates a PCM direct sound buffer.
864 *****************************************************************************
865 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
866 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
867 ****************************************************************************/
868 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
869 int i_channels, int i_nb_channels, int i_rate,
872 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
873 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
875 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
876 i_channels, i_nb_channels, i_rate,
877 FRAME_SIZE * 4 * i_nb_channels, b_probe )
880 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
881 i_channels, i_nb_channels, i_rate,
882 FRAME_SIZE * 2 * i_nb_channels, b_probe )
889 *i_format = VLC_CODEC_S16L;
895 *i_format = VLC_CODEC_FL32;
900 /*****************************************************************************
902 *****************************************************************************
903 * This function destroys the secondary buffer.
904 *****************************************************************************/
905 static void DestroyDSBuffer( audio_output_t *p_aout )
907 if( p_aout->sys->p_dsbuffer )
909 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
910 p_aout->sys->p_dsbuffer = NULL;
914 /*****************************************************************************
915 * FillBuffer: Fill in one of the direct sound frame buffers.
916 *****************************************************************************
917 * Returns VLC_SUCCESS on success.
918 *****************************************************************************/
919 static int FillBuffer( audio_output_t *p_aout, int i_frame,
920 aout_buffer_t *p_buffer )
922 aout_sys_t *p_sys = p_aout->sys;
923 notification_thread_t *p_notif = &p_sys->notif;
924 void *p_write_position, *p_wrap_around;
925 unsigned long l_bytes1, l_bytes2;
928 /* Before copying anything, we have to lock the buffer */
929 dsresult = IDirectSoundBuffer_Lock(
930 p_sys->p_dsbuffer, /* DS buffer */
931 i_frame * p_notif->i_frame_size, /* Start offset */
932 p_notif->i_frame_size, /* Number of bytes */
933 &p_write_position, /* Address of lock start */
934 &l_bytes1, /* Count of bytes locked before wrap around */
935 &p_wrap_around, /* Buffer address (if wrap around) */
936 &l_bytes2, /* Count of bytes after wrap around */
938 if( dsresult == DSERR_BUFFERLOST )
940 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
941 dsresult = IDirectSoundBuffer_Lock(
943 i_frame * p_notif->i_frame_size,
944 p_notif->i_frame_size,
951 if( dsresult != DS_OK )
953 msg_Warn( p_aout, "cannot lock buffer" );
954 if( p_buffer ) aout_BufferFree( p_buffer );
958 if( p_buffer == NULL )
960 memset( p_write_position, 0, l_bytes1 );
964 if( p_sys->b_chan_reorder )
966 /* Do the channel reordering here */
967 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
968 p_sys->i_channels, p_sys->pi_chan_table,
969 p_sys->i_bits_per_sample );
972 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
973 aout_BufferFree( p_buffer );
976 /* Now the data has been copied, unlock the buffer */
977 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
978 p_wrap_around, l_bytes2 );
980 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
984 /*****************************************************************************
985 * DirectSoundThread: this thread will capture play notification events.
986 *****************************************************************************
987 * We use this thread to emulate a callback mechanism. The thread probes for
988 * event notification and fills up the DS secondary buffer when needed.
989 *****************************************************************************/
990 static void* DirectSoundThread( void *data )
992 audio_output_t *p_aout = (audio_output_t *)data;
993 notification_thread_t *p_notif = &p_aout->sys->notif;
995 int canc = vlc_savecancel ();
997 msg_Dbg( p_aout, "DirectSoundThread ready" );
999 /* Wait here until Play() is called */
1000 WaitForSingleObject( p_notif->event, INFINITE );
1002 if( !vlc_atomic_get( &p_notif->abort) )
1005 mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1007 /* start playing the buffer */
1008 dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1011 DSBPLAY_LOOPING ); /* Flags */
1012 if( dsresult == DSERR_BUFFERLOST )
1014 IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1015 dsresult = IDirectSoundBuffer_Play(
1016 p_aout->sys->p_dsbuffer,
1019 DSBPLAY_LOOPING ); /* Flags */
1021 if( dsresult != DS_OK )
1023 msg_Err( p_aout, "cannot start playing buffer" );
1026 last_time = mdate();
1028 while( !vlc_atomic_get( &p_notif->abort ) )
1031 int l_queued = 0, l_free_slots;
1032 unsigned i_frame_siz = p_aout->sys->packet.samples;
1033 mtime_t mtime = mdate();
1037 * Fill in as much audio data as we can in our circular buffer
1040 /* Find out current play position */
1041 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1042 p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1044 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1048 /* Detect underruns */
1049 if( l_queued && mtime - last_time >
1050 INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1052 msg_Dbg( p_aout, "detected underrun!" );
1056 /* Try to fill in as many frame buffers as possible */
1057 l_read /= (p_aout->format.i_bytes_per_frame /
1058 p_aout->format.i_frame_length);
1059 l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1060 if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1061 l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1063 for( i = 0; i < l_free_slots; i++ )
1065 aout_buffer_t *p_buffer = aout_PacketNext( p_aout,
1066 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1067 p_aout->format.i_rate );
1069 /* If there is no audio data available and we have some buffered
1070 * already, then just wait for the next time */
1071 if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1073 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1074 p_buffer ) != VLC_SUCCESS ) break;
1077 /* Sleep a reasonable amount of time */
1078 l_queued += (i * i_frame_siz);
1079 msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1082 /* make sure the buffer isn't playing */
1083 IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1085 vlc_restorecancel (canc);
1086 msg_Dbg( p_aout, "DirectSoundThread exiting" );
1090 /*****************************************************************************
1091 * CallBackConfigNBEnum: callback to get the number of available devices
1092 *****************************************************************************/
1093 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1094 LPCWSTR psz_mod, LPVOID p_nb )
1096 VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1098 int * a = (int *)p_nb;
1103 /*****************************************************************************
1104 * CallBackConfigEnum: callback to add available devices to the preferences list
1105 *****************************************************************************/
1106 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1107 LPCWSTR psz_mod, LPVOID _p_item )
1109 VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1111 module_config_t *p_item = (module_config_t *) _p_item;
1113 p_item->ppsz_list[p_item->i_list] = FromWide( psz_desc );
1114 p_item->ppsz_list_text[p_item->i_list] = FromWide( psz_desc );
1119 /*****************************************************************************
1120 * ReloadDirectXDevices: store the list of devices in preferences
1121 *****************************************************************************/
1122 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1123 vlc_value_t newval, vlc_value_t oldval, void *data )
1125 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1127 module_config_t *p_item = config_FindConfig( p_this, psz_name );
1128 if( !p_item ) return VLC_SUCCESS;
1130 /* Clear-up the current list */
1131 if( p_item->i_list )
1133 for( int i = 0; i < p_item->i_list; i++ )
1135 free((char *)(p_item->ppsz_list[i]) );
1136 free((char *)(p_item->ppsz_list_text[i]) );
1140 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
1142 HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1143 if( hdsound_dll == NULL )
1145 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1149 /* Get DirectSoundEnumerate */
1150 OurDirectSoundEnumerate = (void *)
1151 GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1153 if( OurDirectSoundEnumerate == NULL )
1157 OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1158 msg_Dbg(p_this,"found %d devices", nb_devices);
1160 p_item->ppsz_list = xrealloc( p_item->ppsz_list,
1161 nb_devices * sizeof(char *) );
1162 p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
1163 nb_devices * sizeof(char *) );
1166 OurDirectSoundEnumerate(CallBackConfigEnum, p_item);
1168 /* Signal change to the interface */
1169 p_item->b_dirty = true;
1172 FreeLibrary(hdsound_dll);