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
21 * Foundation, Inc., 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>
37 #include "windows_audio_common.h"
41 #define FRAME_SIZE ((int)p_aout->output.output.i_rate/20) /* Size in samples */
43 /*****************************************************************************
44 * notification_thread_t: DirectX event thread
45 *****************************************************************************/
46 typedef struct notification_thread_t
50 aout_instance_t *p_aout;
51 int i_frame_size; /* size in bytes of one frame */
52 int i_write_slot; /* current write position in our circular buffer */
57 } notification_thread_t;
59 /*****************************************************************************
60 * aout_sys_t: directx audio output method descriptor
61 *****************************************************************************
62 * This structure is part of the audio output thread descriptor.
63 * It describes the direct sound specific properties of an audio device.
64 *****************************************************************************/
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 *p_notif; /* DirectSoundThread id */
79 int b_playing; /* playing status */
81 int i_frame_size; /* Size in bytes of one frame */
83 int i_speaker_setup; /* Speaker setup override */
85 bool b_chan_reorder; /* do we need channel reordering */
86 int pi_chan_table[AOUT_CHAN_MAX];
87 uint32_t i_channel_mask;
88 uint32_t i_bits_per_sample;
92 /*****************************************************************************
94 *****************************************************************************/
95 static int OpenAudio ( vlc_object_t * );
96 static void CloseAudio ( vlc_object_t * );
97 static void Play ( aout_instance_t * );
100 static void Probe ( aout_instance_t * );
101 static int InitDirectSound ( aout_instance_t * );
102 static int CreateDSBuffer ( aout_instance_t *, int, int, int, int, int, bool );
103 static int CreateDSBufferPCM ( aout_instance_t *, vlc_fourcc_t*, int, int, int, bool );
104 static void DestroyDSBuffer ( aout_instance_t * );
105 static void* DirectSoundThread( vlc_object_t * );
106 static int FillBuffer ( aout_instance_t *, int, aout_buffer_t * );
108 static int ReloadDirectXDevices( vlc_object_t *, char const *,
109 vlc_value_t, vlc_value_t, void * );
111 /* Speaker setup override options list */
112 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
113 "Quad", "5.1", "7.1" };
114 static const char *const ppsz_adev[] = {"default", };
115 static const char *const ppsz_adev_text[] = {"default", };
117 /*****************************************************************************
119 *****************************************************************************/
120 #define DEVICE_TEXT N_("Output device")
121 #define DEVICE_LONGTEXT N_("Select your audio output device")
123 #define SPEAKER_TEXT N_("Speaker configuration")
124 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
125 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
128 set_description( N_("DirectX audio output") )
129 set_shortname( "DirectX" )
130 set_capability( "audio output", 100 )
131 set_category( CAT_AUDIO )
132 set_subcategory( SUBCAT_AUDIO_AOUT )
133 add_shortcut( "directx" )
134 add_string( "directx-audio-device-name", "default", NULL,
135 DEVICE_TEXT, DEVICE_LONGTEXT, false )
136 change_string_list( ppsz_adev, ppsz_adev_text, ReloadDirectXDevices )
137 change_action_add( ReloadDirectXDevices, N_("Refresh list") )
138 add_bool( "directx-audio-float32", false, NULL, FLOAT_TEXT,
139 FLOAT_LONGTEXT, true )
140 add_string( "directx-audio-speaker", "Windows default", NULL,
141 SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
142 change_string_list( speaker_list, 0, 0 )
143 change_need_restart ()
145 set_callbacks( OpenAudio, CloseAudio )
148 /*****************************************************************************
149 * OpenAudio: open the audio device
150 *****************************************************************************
151 * This function opens and setups Direct Sound.
152 *****************************************************************************/
153 static int OpenAudio( vlc_object_t *p_this )
155 aout_instance_t * p_aout = (aout_instance_t *)p_this;
160 const char * const * ppsz_compare = speaker_list;
162 msg_Dbg( p_aout, "OpenAudio" );
164 /* Allocate structure */
165 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
166 if( p_aout->output.p_sys == NULL )
169 /* Initialize some variables */
170 p_aout->output.p_sys->p_dsobject = NULL;
171 p_aout->output.p_sys->p_dsbuffer = NULL;
172 p_aout->output.p_sys->p_notif = NULL;
173 p_aout->output.p_sys->b_playing = 0;
175 p_aout->output.pf_play = Play;
176 aout_VolumeSoftInit( p_aout );
178 /* Retrieve config values */
179 var_Create( p_aout, "directx-audio-float32",
180 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
181 psz_speaker = var_CreateGetString( p_aout, "directx-audio-speaker" );
183 while ( *ppsz_compare != NULL )
185 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
192 if ( *ppsz_compare == NULL )
194 msg_Err( p_aout, "(%s) isn't valid speaker setup option",
196 msg_Err( p_aout, "Defaulting to Windows default speaker config");
200 p_aout->output.p_sys->i_speaker_setup = i;
202 p_aout->output.p_sys->p_device_guid = 0;
204 /* Initialise DirectSound */
205 if( InitDirectSound( p_aout ) )
207 msg_Err( p_aout, "cannot initialize DirectSound" );
211 if( var_Type( p_aout, "audio-device" ) == 0 )
216 if( var_Get( p_aout, "audio-device", &val ) < 0 )
218 /* Probe() has failed. */
222 /* Open the device */
223 if( val.i_int == AOUT_VAR_SPDIF )
225 p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
227 /* Calculate the frame size in bytes */
228 p_aout->output.i_nb_samples = A52_FRAME_NB;
229 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
230 p_aout->output.output.i_frame_length = A52_FRAME_NB;
231 p_aout->output.p_sys->i_frame_size =
232 p_aout->output.output.i_bytes_per_frame;
234 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
235 p_aout->output.output.i_physical_channels,
236 aout_FormatNbChannels( &p_aout->output.output ),
237 p_aout->output.output.i_rate,
238 p_aout->output.p_sys->i_frame_size, false )
241 msg_Err( p_aout, "cannot open directx audio device" );
242 free( p_aout->output.p_sys );
246 aout_VolumeNoneInit( p_aout );
250 if( val.i_int == AOUT_VAR_5_1 )
252 p_aout->output.output.i_physical_channels
253 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
254 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
257 else if( val.i_int == AOUT_VAR_7_1 )
259 p_aout->output.output.i_physical_channels
260 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
261 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
262 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
265 else if( val.i_int == AOUT_VAR_3F2R )
267 p_aout->output.output.i_physical_channels
268 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
269 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
271 else if( val.i_int == AOUT_VAR_2F2R )
273 p_aout->output.output.i_physical_channels
274 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
275 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
277 else if( val.i_int == AOUT_VAR_MONO )
279 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
283 p_aout->output.output.i_physical_channels
284 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
287 if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,
288 p_aout->output.output.i_physical_channels,
289 aout_FormatNbChannels( &p_aout->output.output ),
290 p_aout->output.output.i_rate, false )
293 msg_Err( p_aout, "cannot open directx audio device" );
294 free( p_aout->output.p_sys );
298 /* Calculate the frame size in bytes */
299 p_aout->output.i_nb_samples = FRAME_SIZE;
300 aout_FormatPrepare( &p_aout->output.output );
301 aout_VolumeSoftInit( p_aout );
304 /* Now we need to setup our DirectSound play notification structure */
305 p_aout->output.p_sys->p_notif =
306 vlc_object_create( p_aout, sizeof(notification_thread_t) );
307 p_aout->output.p_sys->p_notif->p_aout = p_aout;
309 p_aout->output.p_sys->p_notif->event = CreateEvent( 0, FALSE, FALSE, 0 );
310 p_aout->output.p_sys->p_notif->i_frame_size =
311 p_aout->output.p_sys->i_frame_size;
313 /* then launch the notification thread */
314 msg_Dbg( p_aout, "creating DirectSoundThread" );
315 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
316 "DirectSound Notification Thread",
318 VLC_THREAD_PRIORITY_HIGHEST ) )
320 msg_Err( p_aout, "cannot create DirectSoundThread" );
321 CloseHandle( p_aout->output.p_sys->p_notif->event );
322 vlc_object_release( p_aout->output.p_sys->p_notif );
323 p_aout->output.p_sys->p_notif = NULL;
327 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
332 CloseAudio( VLC_OBJECT(p_aout) );
336 /*****************************************************************************
337 * Probe: probe the audio device for available formats and channels
338 *****************************************************************************/
339 static void Probe( aout_instance_t * p_aout )
341 vlc_value_t val, text;
342 vlc_fourcc_t i_format;
343 unsigned int i_physical_channels;
344 DWORD ui_speaker_config;
345 bool is_default_output_set = false;
347 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
348 text.psz_string = _("Audio Device");
349 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
351 /* Test for 5.1 support */
352 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
353 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
354 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
355 if( p_aout->output.output.i_physical_channels == i_physical_channels )
357 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
358 p_aout->output.output.i_rate, true )
361 val.i_int = AOUT_VAR_5_1;
362 text.psz_string = (char*) "5.1";
363 var_Change( p_aout, "audio-device",
364 VLC_VAR_ADDCHOICE, &val, &text );
365 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
366 is_default_output_set = true;
367 msg_Dbg( p_aout, "device supports 5.1 channels" );
371 /* Test for 7.1 support */
372 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
373 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
374 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
375 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
376 if( p_aout->output.output.i_physical_channels == i_physical_channels )
378 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
379 p_aout->output.output.i_rate, true )
382 val.i_int = AOUT_VAR_7_1;
383 text.psz_string = (char*) "7.1";
384 var_Change( p_aout, "audio-device",
385 VLC_VAR_ADDCHOICE, &val, &text );
386 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
387 is_default_output_set = true;
388 msg_Dbg( p_aout, "device supports 7.1 channels" );
392 /* Test for 3 Front 2 Rear support */
393 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
394 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
396 if( p_aout->output.output.i_physical_channels == i_physical_channels )
398 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
399 p_aout->output.output.i_rate, true )
402 val.i_int = AOUT_VAR_3F2R;
403 text.psz_string = _("3 Front 2 Rear");
404 var_Change( p_aout, "audio-device",
405 VLC_VAR_ADDCHOICE, &val, &text );
406 if(!is_default_output_set)
408 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
409 is_default_output_set = true;
411 msg_Dbg( p_aout, "device supports 5 channels" );
415 /* Test for 2 Front 2 Rear support */
416 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
417 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
418 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
419 == i_physical_channels )
421 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
422 p_aout->output.output.i_rate, true )
425 val.i_int = AOUT_VAR_2F2R;
426 text.psz_string = _("2 Front 2 Rear");
427 var_Change( p_aout, "audio-device",
428 VLC_VAR_ADDCHOICE, &val, &text );
429 if(!is_default_output_set)
431 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
432 is_default_output_set = true;
434 msg_Dbg( p_aout, "device supports 4 channels" );
438 /* Test for stereo support */
439 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
440 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
441 p_aout->output.output.i_rate, true )
444 val.i_int = AOUT_VAR_STEREO;
445 text.psz_string = _("Stereo");
446 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
447 if(!is_default_output_set)
449 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
450 is_default_output_set = true;
451 msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
453 else msg_Dbg( p_aout, "device supports 2 channels" );
456 /* Test for mono support */
457 i_physical_channels = AOUT_CHAN_CENTER;
458 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
459 p_aout->output.output.i_rate, true )
462 val.i_int = AOUT_VAR_MONO;
463 text.psz_string = _("Mono");
464 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
465 msg_Dbg( p_aout, "device supports 1 channel" );
468 /* Check the speaker configuration to determine which channel config should
470 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
471 &ui_speaker_config ) )
473 ui_speaker_config = DSSPEAKER_STEREO;
474 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
476 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
478 case DSSPEAKER_7POINT1:
479 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
480 val.i_int = AOUT_VAR_7_1;
482 case DSSPEAKER_5POINT1:
483 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
484 val.i_int = AOUT_VAR_5_1;
487 msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
488 val.i_int = AOUT_VAR_2F2R;
490 #if 0 /* Lots of people just get their settings wrong and complain that
491 * this is a problem with VLC so just don't ever set mono by default. */
493 val.i_int = AOUT_VAR_MONO;
496 case DSSPEAKER_SURROUND:
497 msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
498 case DSSPEAKER_STEREO:
499 msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
501 /* If nothing else is found, choose stereo output */
502 val.i_int = AOUT_VAR_STEREO;
506 /* Check if we want to override speaker config */
507 switch( p_aout->output.p_sys->i_speaker_setup )
509 case 0: /* Default value aka Windows default speaker setup */
512 msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
513 val.i_int = AOUT_VAR_MONO;
516 msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
517 val.i_int = AOUT_VAR_STEREO;
520 msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
521 val.i_int = AOUT_VAR_2F2R;
524 msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
525 val.i_int = AOUT_VAR_5_1;
528 msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
529 val.i_int = AOUT_VAR_7_1;
532 msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
536 var_Set( p_aout, "audio-device", val );
538 /* Test for SPDIF support */
539 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
541 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
542 p_aout->output.output.i_physical_channels,
543 aout_FormatNbChannels( &p_aout->output.output ),
544 p_aout->output.output.i_rate,
545 AOUT_SPDIF_SIZE, true )
548 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
549 val.i_int = AOUT_VAR_SPDIF;
550 text.psz_string = _("A/52 over S/PDIF");
551 var_Change( p_aout, "audio-device",
552 VLC_VAR_ADDCHOICE, &val, &text );
553 if( config_GetInt( p_aout, "spdif" ) )
554 var_Set( p_aout, "audio-device", val );
558 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
561 /* Probe() has failed. */
562 var_Destroy( p_aout, "audio-device" );
566 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
567 var_SetBool( p_aout, "intf-change", true );
570 /*****************************************************************************
571 * Play: we'll start playing the directsound buffer here because at least here
572 * we know the first buffer has been put in the aout fifo and we also
574 *****************************************************************************/
575 static void Play( aout_instance_t *p_aout )
577 if( !p_aout->output.p_sys->b_playing )
579 aout_buffer_t *p_buffer;
582 p_aout->output.p_sys->b_playing = 1;
584 /* get the playing date of the first aout buffer */
585 p_aout->output.p_sys->p_notif->start_date =
586 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
588 /* fill in the first samples */
589 for( i = 0; i < FRAMES_NUM; i++ )
591 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
592 if( !p_buffer ) break;
593 FillBuffer( p_aout, i, p_buffer );
596 /* wake up the audio output thread */
597 SetEvent( p_aout->output.p_sys->p_notif->event );
601 /*****************************************************************************
602 * CloseAudio: close the audio device
603 *****************************************************************************/
604 static void CloseAudio( vlc_object_t *p_this )
606 aout_instance_t * p_aout = (aout_instance_t *)p_this;
607 aout_sys_t *p_sys = p_aout->output.p_sys;
609 msg_Dbg( p_aout, "closing audio device" );
611 /* kill the position notification thread, if any */
614 vlc_object_detach( p_sys->p_notif );
615 vlc_object_kill( p_sys->p_notif );
616 /* wake up the audio thread if needed */
617 if( !p_sys->b_playing ) SetEvent( p_sys->p_notif->event );
619 vlc_thread_join( p_sys->p_notif );
620 vlc_object_release( p_sys->p_notif );
623 /* release the secondary buffer */
624 DestroyDSBuffer( p_aout );
626 /* finally release the DirectSound object */
627 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
629 /* free DSOUND.DLL */
630 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
632 free( p_aout->output.p_sys->p_device_guid );
636 /*****************************************************************************
637 * CallBackDirectSoundEnum: callback to enumerate available devices
638 *****************************************************************************/
639 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCSTR psz_desc,
640 LPCSTR psz_mod, LPVOID _p_aout )
642 VLC_UNUSED( psz_mod );
644 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
646 msg_Dbg( p_aout, "found device: %s", psz_desc );
648 if( p_aout->output.p_sys->psz_device && !strcmp(p_aout->output.p_sys->psz_device, psz_desc) && p_guid )
650 /* Use the device corresponding to psz_device */
651 p_aout->output.p_sys->p_device_guid = malloc( sizeof( GUID ) );
652 *p_aout->output.p_sys->p_device_guid = *p_guid;
653 msg_Dbg( p_aout, "using device: %s", psz_desc );
657 /* If no default device has been selected, chose the first one */
658 if( !p_aout->output.p_sys->psz_device && p_guid )
660 p_aout->output.p_sys->psz_device = strdup( psz_desc );
661 p_aout->output.p_sys->p_device_guid = malloc( sizeof( GUID ) );
662 *p_aout->output.p_sys->p_device_guid = *p_guid;
663 msg_Dbg( p_aout, "using device: %s", psz_desc );
669 /*****************************************************************************
670 * InitDirectSound: handle all the gory details of DirectSound initialisation
671 *****************************************************************************/
672 static int InitDirectSound( aout_instance_t *p_aout )
674 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
675 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACK, LPVOID);
677 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
678 if( p_aout->output.p_sys->hdsound_dll == NULL )
680 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
684 OurDirectSoundCreate = (void *)
685 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
686 "DirectSoundCreate" );
687 if( OurDirectSoundCreate == NULL )
689 msg_Warn( p_aout, "GetProcAddress FAILED" );
693 /* Get DirectSoundEnumerate */
694 OurDirectSoundEnumerate = (void *)
695 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
696 "DirectSoundEnumerateA" );
697 if( OurDirectSoundEnumerate )
699 p_aout->output.p_sys->psz_device = config_GetPsz(p_aout, "directx-audio-device-name");
700 /* Attempt enumeration */
701 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
704 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
708 /* Create the direct sound object */
709 if FAILED( OurDirectSoundCreate( p_aout->output.p_sys->p_device_guid,
710 &p_aout->output.p_sys->p_dsobject,
713 msg_Warn( p_aout, "cannot create a direct sound device" );
717 /* Set DirectSound Cooperative level, ie what control we want over Windows
718 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
719 * settings of the primary buffer, but also that only the sound of our
720 * application will be hearable when it will have the focus.
721 * !!! (this is not really working as intended yet because to set the
722 * cooperative level you need the window handle of your application, and
723 * I don't know of any easy way to get it. Especially since we might play
724 * sound without any video, and so what window handle should we use ???
725 * The hack for now is to use the Desktop window handle - it seems to be
727 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
731 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
737 p_aout->output.p_sys->p_dsobject = NULL;
738 if( p_aout->output.p_sys->hdsound_dll )
740 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
741 p_aout->output.p_sys->hdsound_dll = NULL;
747 /*****************************************************************************
748 * CreateDSBuffer: Creates a direct sound buffer of the required format.
749 *****************************************************************************
750 * This function creates the buffer we'll use to play audio.
751 * In DirectSound there are two kinds of buffers:
752 * - the primary buffer: which is the actual buffer that the soundcard plays
753 * - the secondary buffer(s): these buffers are the one actually used by
754 * applications and DirectSound takes care of mixing them into the primary.
756 * Once you create a secondary buffer, you cannot change its format anymore so
757 * you have to release the current one and create another.
758 *****************************************************************************/
759 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
760 int i_channels, int i_nb_channels, int i_rate,
761 int i_bytes_per_frame, bool b_probe )
763 WAVEFORMATEXTENSIBLE waveformat;
764 DSBUFFERDESC dsbdesc;
767 /* First set the sound buffer format */
768 waveformat.dwChannelMask = 0;
769 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
771 if( i_channels & pi_channels_src[i] )
772 waveformat.dwChannelMask |= pi_channels_in[i];
777 case VLC_CODEC_SPDIFL:
779 /* To prevent channel re-ordering */
780 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
781 waveformat.Format.wBitsPerSample = 16;
782 waveformat.Samples.wValidBitsPerSample =
783 waveformat.Format.wBitsPerSample;
784 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
785 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
789 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
790 waveformat.Samples.wValidBitsPerSample =
791 waveformat.Format.wBitsPerSample;
792 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
793 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
797 waveformat.Format.wBitsPerSample = 16;
798 waveformat.Samples.wValidBitsPerSample =
799 waveformat.Format.wBitsPerSample;
800 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
801 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
805 waveformat.Format.nChannels = i_nb_channels;
806 waveformat.Format.nSamplesPerSec = i_rate;
807 waveformat.Format.nBlockAlign =
808 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
809 waveformat.Format.nAvgBytesPerSec =
810 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
812 p_aout->output.p_sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
813 p_aout->output.p_sys->i_channels = i_nb_channels;
815 /* Then fill in the direct sound descriptor */
816 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
817 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
818 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
819 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
821 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
822 if( i_nb_channels <= 2 )
824 waveformat.Format.cbSize = 0;
828 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
829 waveformat.Format.cbSize =
830 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
832 /* Needed for 5.1 on emu101k */
833 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
836 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
837 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
839 if FAILED( IDirectSound_CreateSoundBuffer(
840 p_aout->output.p_sys->p_dsobject, &dsbdesc,
841 &p_aout->output.p_sys->p_dsbuffer, NULL) )
843 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
845 /* Try without DSBCAPS_LOCHARDWARE */
846 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
847 if FAILED( IDirectSound_CreateSoundBuffer(
848 p_aout->output.p_sys->p_dsobject, &dsbdesc,
849 &p_aout->output.p_sys->p_dsbuffer, NULL) )
854 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
862 /* Stop here if we were just probing */
865 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
866 p_aout->output.p_sys->p_dsbuffer = NULL;
870 p_aout->output.p_sys->i_frame_size = i_bytes_per_frame;
871 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
872 p_aout->output.p_sys->b_chan_reorder =
873 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
874 waveformat.dwChannelMask, i_nb_channels,
875 p_aout->output.p_sys->pi_chan_table );
877 if( p_aout->output.p_sys->b_chan_reorder )
879 msg_Dbg( p_aout, "channel reordering needed" );
885 /*****************************************************************************
886 * CreateDSBufferPCM: creates a PCM direct sound buffer.
887 *****************************************************************************
888 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
889 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
890 ****************************************************************************/
891 static int CreateDSBufferPCM( aout_instance_t *p_aout, vlc_fourcc_t *i_format,
892 int i_channels, int i_nb_channels, int i_rate,
895 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
896 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
898 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
899 i_channels, i_nb_channels, i_rate,
900 FRAME_SIZE * 4 * i_nb_channels, b_probe )
903 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
904 i_channels, i_nb_channels, i_rate,
905 FRAME_SIZE * 2 * i_nb_channels, b_probe )
912 *i_format = VLC_CODEC_S16L;
918 *i_format = VLC_CODEC_FL32;
923 /*****************************************************************************
925 *****************************************************************************
926 * This function destroys the secondary buffer.
927 *****************************************************************************/
928 static void DestroyDSBuffer( aout_instance_t *p_aout )
930 if( p_aout->output.p_sys->p_dsbuffer )
932 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
933 p_aout->output.p_sys->p_dsbuffer = NULL;
937 /*****************************************************************************
938 * FillBuffer: Fill in one of the direct sound frame buffers.
939 *****************************************************************************
940 * Returns VLC_SUCCESS on success.
941 *****************************************************************************/
942 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
943 aout_buffer_t *p_buffer )
945 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
946 aout_sys_t *p_sys = p_aout->output.p_sys;
947 void *p_write_position, *p_wrap_around;
948 unsigned long l_bytes1, l_bytes2;
951 /* Before copying anything, we have to lock the buffer */
952 dsresult = IDirectSoundBuffer_Lock(
953 p_sys->p_dsbuffer, /* DS buffer */
954 i_frame * p_notif->i_frame_size, /* Start offset */
955 p_notif->i_frame_size, /* Number of bytes */
956 &p_write_position, /* Address of lock start */
957 &l_bytes1, /* Count of bytes locked before wrap around */
958 &p_wrap_around, /* Buffer adress (if wrap around) */
959 &l_bytes2, /* Count of bytes after wrap around */
961 if( dsresult == DSERR_BUFFERLOST )
963 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
964 dsresult = IDirectSoundBuffer_Lock(
966 i_frame * p_notif->i_frame_size,
967 p_notif->i_frame_size,
974 if( dsresult != DS_OK )
976 msg_Warn( p_notif, "cannot lock buffer" );
977 if( p_buffer ) aout_BufferFree( p_buffer );
981 if( p_buffer == NULL )
983 memset( p_write_position, 0, l_bytes1 );
987 if( p_sys->b_chan_reorder )
989 /* Do the channel reordering here */
990 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
991 p_sys->i_channels, p_sys->pi_chan_table,
992 p_sys->i_bits_per_sample );
995 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
996 aout_BufferFree( p_buffer );
999 /* Now the data has been copied, unlock the buffer */
1000 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1001 p_wrap_around, l_bytes2 );
1003 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1007 /*****************************************************************************
1008 * DirectSoundThread: this thread will capture play notification events.
1009 *****************************************************************************
1010 * We use this thread to emulate a callback mechanism. The thread probes for
1011 * event notification and fills up the DS secondary buffer when needed.
1012 *****************************************************************************/
1013 static void* DirectSoundThread( vlc_object_t *p_this )
1015 notification_thread_t *p_notif = (notification_thread_t*)p_this;
1016 aout_instance_t *p_aout = p_notif->p_aout;
1018 int canc = vlc_savecancel ();
1020 /* We don't want any resampling when using S/PDIF output */
1021 bool b_sleek = (p_aout->output.output.i_format == VLC_CODEC_SPDIFL);
1023 msg_Dbg( p_notif, "DirectSoundThread ready" );
1025 /* Wait here until Play() is called */
1026 WaitForSingleObject( p_notif->event, INFINITE );
1028 if( vlc_object_alive (p_notif) )
1031 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
1033 /* start playing the buffer */
1034 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
1037 DSBPLAY_LOOPING ); /* Flags */
1038 if( dsresult == DSERR_BUFFERLOST )
1040 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
1041 dsresult = IDirectSoundBuffer_Play(
1042 p_aout->output.p_sys->p_dsbuffer,
1045 DSBPLAY_LOOPING ); /* Flags */
1047 if( dsresult != DS_OK )
1049 msg_Err( p_aout, "cannot start playing buffer" );
1052 last_time = mdate();
1054 while( vlc_object_alive (p_notif) )
1057 int l_queued = 0, l_free_slots;
1058 unsigned i_frame_siz = p_aout->output.i_nb_samples;
1059 mtime_t mtime = mdate();
1063 * Fill in as much audio data as we can in our circular buffer
1066 /* Find out current play position */
1067 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1068 p_aout->output.p_sys->p_dsbuffer, &l_read, NULL ) )
1070 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1074 /* Detect underruns */
1075 if( l_queued && mtime - last_time >
1076 INT64_C(1000000) * l_queued / p_aout->output.output.i_rate )
1078 msg_Dbg( p_aout, "detected underrun!" );
1082 /* Try to fill in as many frame buffers as possible */
1083 l_read /= (p_aout->output.output.i_bytes_per_frame /
1084 p_aout->output.output.i_frame_length);
1085 l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1086 if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1087 l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1089 for( i = 0; i < l_free_slots; i++ )
1091 aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout,
1092 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1093 p_aout->output.output.i_rate, b_sleek );
1095 /* If there is no audio data available and we have some buffered
1096 * already, then just wait for the next time */
1097 if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1099 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1100 p_buffer ) != VLC_SUCCESS ) break;
1103 /* Sleep a reasonable amount of time */
1104 l_queued += (i * i_frame_siz);
1105 msleep( INT64_C(1000000) * l_queued / p_aout->output.output.i_rate / 2 );
1108 /* make sure the buffer isn't playing */
1109 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
1111 /* free the event */
1112 CloseHandle( p_notif->event );
1114 vlc_restorecancel (canc);
1115 msg_Dbg( p_notif, "DirectSoundThread exiting" );
1119 /*****************************************************************************
1120 * CallBackConfigNBEnum: callback to get the number of available devices
1121 *****************************************************************************/
1122 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCSTR psz_desc,
1123 LPCSTR psz_mod, LPVOID p_nb )
1125 VLC_UNUSED( psz_mod );
1126 VLC_UNUSED( psz_desc );
1127 VLC_UNUSED( p_guid );
1128 int * a = (int *)p_nb;
1133 /*****************************************************************************
1134 * CallBackConfigEnum: callback to add available devices to the preferences list
1135 *****************************************************************************/
1136 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCSTR psz_desc,
1137 LPCSTR psz_mod, LPVOID _p_item )
1139 VLC_UNUSED( psz_mod );
1140 VLC_UNUSED( p_guid );
1142 module_config_t *p_item = (module_config_t *) _p_item;
1144 p_item->ppsz_list[p_item->i_list] = FromLocaleDup(psz_desc);
1145 p_item->ppsz_list_text[p_item->i_list] = FromLocaleDup(psz_desc);
1146 p_item->i_list = p_item->i_list +1;
1150 /*****************************************************************************
1151 * ReloadDirectXDevices: store the list of devices in preferences
1152 *****************************************************************************/
1153 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1154 vlc_value_t newval, vlc_value_t oldval, void *data )
1156 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1157 module_config_t *p_item = config_FindConfig( p_this, psz_name );
1158 if( !p_item ) return VLC_SUCCESS;
1160 /* Clear-up the current list */
1161 if( p_item->i_list )
1164 for( i = 0; i < p_item->i_list; i++ )
1166 free((char *)(p_item->ppsz_list[i]) );
1167 free((char *)(p_item->ppsz_list_text[i]) );
1171 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACK, LPVOID);
1173 HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1174 if( hdsound_dll == NULL )
1176 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1179 /* Get DirectSoundEnumerate */
1180 OurDirectSoundEnumerate = (void *)
1181 GetProcAddress( hdsound_dll,
1182 "DirectSoundEnumerateA" );
1184 OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1185 msg_Dbg(p_this,"found %d devices", nb_devices);
1187 p_item->ppsz_list = xrealloc( p_item->ppsz_list,
1188 nb_devices * sizeof(char *) );
1189 p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
1190 nb_devices * sizeof(char *) );
1193 OurDirectSoundEnumerate(CallBackConfigEnum, p_item);
1195 FreeLibrary(hdsound_dll);
1197 /* Signal change to the interface */
1198 p_item->b_dirty = true;