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->output.output.i_rate/20) /* Size in samples */
42 /*****************************************************************************
43 * notification_thread_t: DirectX event thread
44 *****************************************************************************/
45 typedef struct notification_thread_t
47 aout_instance_t *p_aout;
48 int i_frame_size; /* size in bytes of one frame */
49 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( void * );
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", "directsound" )
135 add_string( "directx-audio-device-name", "default",
136 DEVICE_TEXT, DEVICE_LONGTEXT, false )
137 add_deprecated_alias( "directx-audio-device" ) /* Since 1.1.0 */
138 change_string_list( ppsz_adev, ppsz_adev_text, ReloadDirectXDevices )
139 change_action_add( ReloadDirectXDevices, N_("Refresh list") )
140 add_bool( "directx-audio-float32", false, FLOAT_TEXT,
141 FLOAT_LONGTEXT, true )
142 add_string( "directx-audio-speaker", "Windows default",
143 SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
144 change_string_list( speaker_list, 0, 0 )
146 set_callbacks( OpenAudio, CloseAudio )
149 /*****************************************************************************
150 * OpenAudio: open the audio device
151 *****************************************************************************
152 * This function opens and setups Direct Sound.
153 *****************************************************************************/
154 static int OpenAudio( vlc_object_t *p_this )
156 aout_instance_t * p_aout = (aout_instance_t *)p_this;
161 const char * const * ppsz_compare = speaker_list;
163 msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
165 /* Allocate structure */
166 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
167 if( p_aout->output.p_sys == NULL )
170 /* Initialize some variables */
171 p_aout->output.p_sys->p_dsobject = NULL;
172 p_aout->output.p_sys->p_dsbuffer = NULL;
173 p_aout->output.p_sys->p_notif = NULL;
174 p_aout->output.p_sys->b_playing = 0;
176 p_aout->output.pf_play = Play;
177 aout_VolumeSoftInit( p_aout );
179 /* Retrieve config values */
180 var_Create( p_aout, "directx-audio-float32",
181 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
182 psz_speaker = var_CreateGetString( p_aout, "directx-audio-speaker" );
184 while ( *ppsz_compare != NULL )
186 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
193 if ( *ppsz_compare == NULL )
195 msg_Err( p_aout, "(%s) isn't valid speaker setup option", psz_speaker );
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 = calloc( 1, sizeof( *p_aout->output.p_sys->p_notif ) );
306 p_aout->output.p_sys->p_notif->p_aout = p_aout;
308 vlc_atomic_set(&p_aout->output.p_sys->p_notif->abort, 0);
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_clone( &p_aout->output.p_sys->p_notif->thread,
316 DirectSoundThread, p_aout->output.p_sys->p_notif,
317 VLC_THREAD_PRIORITY_HIGHEST ) )
319 msg_Err( p_aout, "cannot create DirectSoundThread" );
320 CloseHandle( p_aout->output.p_sys->p_notif->event );
321 free( p_aout->output.p_sys->p_notif );
322 p_aout->output.p_sys->p_notif = NULL;
329 CloseAudio( VLC_OBJECT(p_aout) );
333 /*****************************************************************************
334 * Probe: probe the audio device for available formats and channels
335 *****************************************************************************/
336 static void Probe( aout_instance_t * p_aout )
338 vlc_value_t val, text;
339 vlc_fourcc_t i_format;
340 unsigned int i_physical_channels;
341 DWORD ui_speaker_config;
342 bool is_default_output_set = false;
344 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
345 text.psz_string = _("Audio Device");
346 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
348 /* Test for 5.1 support */
349 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
350 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
351 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
352 if( p_aout->output.output.i_physical_channels == i_physical_channels )
354 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
355 p_aout->output.output.i_rate, true )
358 val.i_int = AOUT_VAR_5_1;
359 text.psz_string = (char*) "5.1";
360 var_Change( p_aout, "audio-device",
361 VLC_VAR_ADDCHOICE, &val, &text );
362 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
363 is_default_output_set = true;
364 msg_Dbg( p_aout, "device supports 5.1 channels" );
368 /* Test for 7.1 support */
369 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
370 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
371 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
372 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
373 if( p_aout->output.output.i_physical_channels == i_physical_channels )
375 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
376 p_aout->output.output.i_rate, true )
379 val.i_int = AOUT_VAR_7_1;
380 text.psz_string = (char*) "7.1";
381 var_Change( p_aout, "audio-device",
382 VLC_VAR_ADDCHOICE, &val, &text );
383 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
384 is_default_output_set = true;
385 msg_Dbg( p_aout, "device supports 7.1 channels" );
389 /* Test for 3 Front 2 Rear support */
390 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
391 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
393 if( p_aout->output.output.i_physical_channels == i_physical_channels )
395 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
396 p_aout->output.output.i_rate, true )
399 val.i_int = AOUT_VAR_3F2R;
400 text.psz_string = _("3 Front 2 Rear");
401 var_Change( p_aout, "audio-device",
402 VLC_VAR_ADDCHOICE, &val, &text );
403 if(!is_default_output_set)
405 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
406 is_default_output_set = true;
408 msg_Dbg( p_aout, "device supports 5 channels" );
412 /* Test for 2 Front 2 Rear support */
413 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
414 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
415 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
416 == i_physical_channels )
418 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
419 p_aout->output.output.i_rate, true )
422 val.i_int = AOUT_VAR_2F2R;
423 text.psz_string = _("2 Front 2 Rear");
424 var_Change( p_aout, "audio-device",
425 VLC_VAR_ADDCHOICE, &val, &text );
426 if(!is_default_output_set)
428 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
429 is_default_output_set = true;
431 msg_Dbg( p_aout, "device supports 4 channels" );
435 /* Test for stereo support */
436 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
437 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
438 p_aout->output.output.i_rate, true )
441 val.i_int = AOUT_VAR_STEREO;
442 text.psz_string = _("Stereo");
443 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
444 if(!is_default_output_set)
446 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
447 is_default_output_set = true;
448 msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
450 else msg_Dbg( p_aout, "device supports 2 channels" );
453 /* Test for mono support */
454 i_physical_channels = AOUT_CHAN_CENTER;
455 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
456 p_aout->output.output.i_rate, true )
459 val.i_int = AOUT_VAR_MONO;
460 text.psz_string = _("Mono");
461 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
462 msg_Dbg( p_aout, "device supports 1 channel" );
465 /* Check the speaker configuration to determine which channel config should
467 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
468 &ui_speaker_config ) )
470 ui_speaker_config = DSSPEAKER_STEREO;
471 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
473 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
475 case DSSPEAKER_7POINT1:
476 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
477 val.i_int = AOUT_VAR_7_1;
479 case DSSPEAKER_5POINT1:
480 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
481 val.i_int = AOUT_VAR_5_1;
484 msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
485 val.i_int = AOUT_VAR_2F2R;
487 #if 0 /* Lots of people just get their settings wrong and complain that
488 * this is a problem with VLC so just don't ever set mono by default. */
490 val.i_int = AOUT_VAR_MONO;
493 case DSSPEAKER_SURROUND:
494 msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
495 case DSSPEAKER_STEREO:
496 msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
498 /* If nothing else is found, choose stereo output */
499 val.i_int = AOUT_VAR_STEREO;
503 /* Check if we want to override speaker config */
504 switch( p_aout->output.p_sys->i_speaker_setup )
506 case 0: /* Default value aka Windows default speaker setup */
509 msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
510 val.i_int = AOUT_VAR_MONO;
513 msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
514 val.i_int = AOUT_VAR_STEREO;
517 msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
518 val.i_int = AOUT_VAR_2F2R;
521 msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
522 val.i_int = AOUT_VAR_5_1;
525 msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
526 val.i_int = AOUT_VAR_7_1;
529 msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
533 var_Set( p_aout, "audio-device", val );
535 /* Test for SPDIF support */
536 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
538 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
539 p_aout->output.output.i_physical_channels,
540 aout_FormatNbChannels( &p_aout->output.output ),
541 p_aout->output.output.i_rate,
542 AOUT_SPDIF_SIZE, true )
545 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
546 val.i_int = AOUT_VAR_SPDIF;
547 text.psz_string = _("A/52 over S/PDIF");
548 var_Change( p_aout, "audio-device",
549 VLC_VAR_ADDCHOICE, &val, &text );
550 if( var_InheritBool( p_aout, "spdif" ) )
551 var_Set( p_aout, "audio-device", val );
555 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
558 /* Probe() has failed. */
559 var_Destroy( p_aout, "audio-device" );
563 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
564 var_TriggerCallback( p_aout, "intf-change" );
567 /*****************************************************************************
568 * Play: we'll start playing the directsound buffer here because at least here
569 * we know the first buffer has been put in the aout fifo and we also
571 *****************************************************************************/
572 static void Play( aout_instance_t *p_aout )
574 if( !p_aout->output.p_sys->b_playing )
576 aout_buffer_t *p_buffer;
578 p_aout->output.p_sys->b_playing = 1;
580 /* get the playing date of the first aout buffer */
581 p_aout->output.p_sys->p_notif->start_date =
582 aout_FifoFirstDate( &p_aout->output.fifo );
584 /* fill in the first samples */
585 for( int i = 0; i < FRAMES_NUM; i++ )
587 p_buffer = aout_FifoPop( &p_aout->output.fifo );
588 if( !p_buffer ) break;
589 FillBuffer( p_aout, i, p_buffer );
592 /* wake up the audio output thread */
593 SetEvent( p_aout->output.p_sys->p_notif->event );
597 /*****************************************************************************
598 * CloseAudio: close the audio device
599 *****************************************************************************/
600 static void CloseAudio( vlc_object_t *p_this )
602 aout_instance_t * p_aout = (aout_instance_t *)p_this;
603 aout_sys_t *p_sys = p_aout->output.p_sys;
605 msg_Dbg( p_aout, "closing audio device" );
607 /* kill the position notification thread, if any */
610 vlc_atomic_set(&p_aout->output.p_sys->p_notif->abort, 1);
611 /* wake up the audio thread if needed */
612 if( !p_sys->b_playing ) SetEvent( p_sys->p_notif->event );
614 vlc_join( p_sys->p_notif->thread, NULL );
615 free( p_sys->p_notif );
618 /* release the secondary buffer */
619 DestroyDSBuffer( p_aout );
621 /* finally release the DirectSound object */
622 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
624 /* free DSOUND.DLL */
625 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
627 free( p_aout->output.p_sys->p_device_guid );
631 /*****************************************************************************
632 * CallBackDirectSoundEnum: callback to enumerate available devices
633 *****************************************************************************/
634 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
635 LPCWSTR psz_mod, LPVOID _p_aout )
637 VLC_UNUSED( psz_mod );
639 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
641 char *psz_device = FromWide( psz_desc );
642 msg_Dbg( p_aout, "found device: %s", psz_device );
644 if( p_aout->output.p_sys->psz_device &&
645 !strcmp(p_aout->output.p_sys->psz_device, psz_device) && p_guid )
647 /* Use the device corresponding to psz_device */
648 p_aout->output.p_sys->p_device_guid = malloc( sizeof( GUID ) );
649 *p_aout->output.p_sys->p_device_guid = *p_guid;
650 msg_Dbg( p_aout, "using device: %s", psz_device );
654 /* If no default device has been selected, chose the first one */
655 if( !p_aout->output.p_sys->psz_device && p_guid )
657 p_aout->output.p_sys->psz_device = strdup( psz_device );
658 p_aout->output.p_sys->p_device_guid = malloc( sizeof( GUID ) );
659 *p_aout->output.p_sys->p_device_guid = *p_guid;
660 msg_Dbg( p_aout, "using device: %s", psz_device );
668 /*****************************************************************************
669 * InitDirectSound: handle all the gory details of DirectSound initialisation
670 *****************************************************************************/
671 static int InitDirectSound( aout_instance_t *p_aout )
673 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
674 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
676 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
677 if( p_aout->output.p_sys->hdsound_dll == NULL )
679 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
683 OurDirectSoundCreate = (void *)
684 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
685 "DirectSoundCreate" );
686 if( OurDirectSoundCreate == NULL )
688 msg_Warn( p_aout, "GetProcAddress FAILED" );
692 /* Get DirectSoundEnumerate */
693 OurDirectSoundEnumerate = (void *)
694 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
695 "DirectSoundEnumerateW" );
696 if( OurDirectSoundEnumerate )
698 p_aout->output.p_sys->psz_device = var_InheritString(p_aout, "directx-audio-device-name");
699 /* Attempt enumeration */
700 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
703 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
707 /* Create the direct sound object */
708 if FAILED( OurDirectSoundCreate( p_aout->output.p_sys->p_device_guid,
709 &p_aout->output.p_sys->p_dsobject,
712 msg_Warn( p_aout, "cannot create a direct sound device" );
716 /* Set DirectSound Cooperative level, ie what control we want over Windows
717 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
718 * settings of the primary buffer, but also that only the sound of our
719 * application will be hearable when it will have the focus.
720 * !!! (this is not really working as intended yet because to set the
721 * cooperative level you need the window handle of your application, and
722 * I don't know of any easy way to get it. Especially since we might play
723 * sound without any video, and so what window handle should we use ???
724 * The hack for now is to use the Desktop window handle - it seems to be
726 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
730 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
736 p_aout->output.p_sys->p_dsobject = NULL;
737 if( p_aout->output.p_sys->hdsound_dll )
739 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
740 p_aout->output.p_sys->hdsound_dll = NULL;
746 /*****************************************************************************
747 * CreateDSBuffer: Creates a direct sound buffer of the required format.
748 *****************************************************************************
749 * This function creates the buffer we'll use to play audio.
750 * In DirectSound there are two kinds of buffers:
751 * - the primary buffer: which is the actual buffer that the soundcard plays
752 * - the secondary buffer(s): these buffers are the one actually used by
753 * applications and DirectSound takes care of mixing them into the primary.
755 * Once you create a secondary buffer, you cannot change its format anymore so
756 * you have to release the current one and create another.
757 *****************************************************************************/
758 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
759 int i_channels, int i_nb_channels, int i_rate,
760 int i_bytes_per_frame, bool b_probe )
762 WAVEFORMATEXTENSIBLE waveformat;
763 DSBUFFERDESC dsbdesc;
766 /* First set the sound buffer format */
767 waveformat.dwChannelMask = 0;
768 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
770 if( i_channels & pi_channels_src[i] )
771 waveformat.dwChannelMask |= pi_channels_in[i];
776 case VLC_CODEC_SPDIFL:
778 /* To prevent channel re-ordering */
779 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
780 waveformat.Format.wBitsPerSample = 16;
781 waveformat.Samples.wValidBitsPerSample =
782 waveformat.Format.wBitsPerSample;
783 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
784 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
788 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
789 waveformat.Samples.wValidBitsPerSample =
790 waveformat.Format.wBitsPerSample;
791 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
792 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
796 waveformat.Format.wBitsPerSample = 16;
797 waveformat.Samples.wValidBitsPerSample =
798 waveformat.Format.wBitsPerSample;
799 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
800 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
804 waveformat.Format.nChannels = i_nb_channels;
805 waveformat.Format.nSamplesPerSec = i_rate;
806 waveformat.Format.nBlockAlign =
807 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
808 waveformat.Format.nAvgBytesPerSec =
809 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
811 p_aout->output.p_sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
812 p_aout->output.p_sys->i_channels = i_nb_channels;
814 /* Then fill in the direct sound descriptor */
815 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
816 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
817 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
818 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
820 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
821 if( i_nb_channels <= 2 )
823 waveformat.Format.cbSize = 0;
827 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
828 waveformat.Format.cbSize =
829 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
831 /* Needed for 5.1 on emu101k */
832 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
835 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
836 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
838 if FAILED( IDirectSound_CreateSoundBuffer(
839 p_aout->output.p_sys->p_dsobject, &dsbdesc,
840 &p_aout->output.p_sys->p_dsbuffer, NULL) )
842 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
844 /* Try without DSBCAPS_LOCHARDWARE */
845 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
846 if FAILED( IDirectSound_CreateSoundBuffer(
847 p_aout->output.p_sys->p_dsobject, &dsbdesc,
848 &p_aout->output.p_sys->p_dsbuffer, NULL) )
853 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
861 /* Stop here if we were just probing */
864 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
865 p_aout->output.p_sys->p_dsbuffer = NULL;
869 p_aout->output.p_sys->i_frame_size = i_bytes_per_frame;
870 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
871 p_aout->output.p_sys->b_chan_reorder =
872 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
873 waveformat.dwChannelMask, i_nb_channels,
874 p_aout->output.p_sys->pi_chan_table );
876 if( p_aout->output.p_sys->b_chan_reorder )
878 msg_Dbg( p_aout, "channel reordering needed" );
884 /*****************************************************************************
885 * CreateDSBufferPCM: creates a PCM direct sound buffer.
886 *****************************************************************************
887 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
888 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
889 ****************************************************************************/
890 static int CreateDSBufferPCM( aout_instance_t *p_aout, vlc_fourcc_t *i_format,
891 int i_channels, int i_nb_channels, int i_rate,
894 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
895 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
897 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
898 i_channels, i_nb_channels, i_rate,
899 FRAME_SIZE * 4 * i_nb_channels, b_probe )
902 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
903 i_channels, i_nb_channels, i_rate,
904 FRAME_SIZE * 2 * i_nb_channels, b_probe )
911 *i_format = VLC_CODEC_S16L;
917 *i_format = VLC_CODEC_FL32;
922 /*****************************************************************************
924 *****************************************************************************
925 * This function destroys the secondary buffer.
926 *****************************************************************************/
927 static void DestroyDSBuffer( aout_instance_t *p_aout )
929 if( p_aout->output.p_sys->p_dsbuffer )
931 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
932 p_aout->output.p_sys->p_dsbuffer = NULL;
936 /*****************************************************************************
937 * FillBuffer: Fill in one of the direct sound frame buffers.
938 *****************************************************************************
939 * Returns VLC_SUCCESS on success.
940 *****************************************************************************/
941 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
942 aout_buffer_t *p_buffer )
944 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
945 aout_sys_t *p_sys = p_aout->output.p_sys;
946 void *p_write_position, *p_wrap_around;
947 unsigned long l_bytes1, l_bytes2;
950 /* Before copying anything, we have to lock the buffer */
951 dsresult = IDirectSoundBuffer_Lock(
952 p_sys->p_dsbuffer, /* DS buffer */
953 i_frame * p_notif->i_frame_size, /* Start offset */
954 p_notif->i_frame_size, /* Number of bytes */
955 &p_write_position, /* Address of lock start */
956 &l_bytes1, /* Count of bytes locked before wrap around */
957 &p_wrap_around, /* Buffer address (if wrap around) */
958 &l_bytes2, /* Count of bytes after wrap around */
960 if( dsresult == DSERR_BUFFERLOST )
962 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
963 dsresult = IDirectSoundBuffer_Lock(
965 i_frame * p_notif->i_frame_size,
966 p_notif->i_frame_size,
973 if( dsresult != DS_OK )
975 msg_Warn( p_aout, "cannot lock buffer" );
976 if( p_buffer ) aout_BufferFree( p_buffer );
980 if( p_buffer == NULL )
982 memset( p_write_position, 0, l_bytes1 );
986 if( p_sys->b_chan_reorder )
988 /* Do the channel reordering here */
989 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
990 p_sys->i_channels, p_sys->pi_chan_table,
991 p_sys->i_bits_per_sample );
994 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
995 aout_BufferFree( p_buffer );
998 /* Now the data has been copied, unlock the buffer */
999 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1000 p_wrap_around, l_bytes2 );
1002 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1006 /*****************************************************************************
1007 * DirectSoundThread: this thread will capture play notification events.
1008 *****************************************************************************
1009 * We use this thread to emulate a callback mechanism. The thread probes for
1010 * event notification and fills up the DS secondary buffer when needed.
1011 *****************************************************************************/
1012 static void* DirectSoundThread( void *data )
1014 notification_thread_t *p_notif = (notification_thread_t*)data;
1015 aout_instance_t *p_aout = p_notif->p_aout;
1017 int canc = vlc_savecancel ();
1019 /* We don't want any resampling when using S/PDIF output */
1020 bool b_sleek = (p_aout->output.output.i_format == VLC_CODEC_SPDIFL);
1022 msg_Dbg( p_aout, "DirectSoundThread ready" );
1024 /* Wait here until Play() is called */
1025 WaitForSingleObject( p_notif->event, INFINITE );
1027 if( !vlc_atomic_get( &p_notif->abort) )
1030 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
1032 /* start playing the buffer */
1033 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
1036 DSBPLAY_LOOPING ); /* Flags */
1037 if( dsresult == DSERR_BUFFERLOST )
1039 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
1040 dsresult = IDirectSoundBuffer_Play(
1041 p_aout->output.p_sys->p_dsbuffer,
1044 DSBPLAY_LOOPING ); /* Flags */
1046 if( dsresult != DS_OK )
1048 msg_Err( p_aout, "cannot start playing buffer" );
1051 last_time = mdate();
1053 while( !vlc_atomic_get( &p_notif->abort ) )
1056 int l_queued = 0, l_free_slots;
1057 unsigned i_frame_siz = p_aout->output.i_nb_samples;
1058 mtime_t mtime = mdate();
1062 * Fill in as much audio data as we can in our circular buffer
1065 /* Find out current play position */
1066 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1067 p_aout->output.p_sys->p_dsbuffer, &l_read, NULL ) )
1069 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1073 /* Detect underruns */
1074 if( l_queued && mtime - last_time >
1075 INT64_C(1000000) * l_queued / p_aout->output.output.i_rate )
1077 msg_Dbg( p_aout, "detected underrun!" );
1081 /* Try to fill in as many frame buffers as possible */
1082 l_read /= (p_aout->output.output.i_bytes_per_frame /
1083 p_aout->output.output.i_frame_length);
1084 l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1085 if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1086 l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1088 for( i = 0; i < l_free_slots; i++ )
1090 aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout,
1091 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1092 p_aout->output.output.i_rate, b_sleek );
1094 /* If there is no audio data available and we have some buffered
1095 * already, then just wait for the next time */
1096 if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1098 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1099 p_buffer ) != VLC_SUCCESS ) break;
1102 /* Sleep a reasonable amount of time */
1103 l_queued += (i * i_frame_siz);
1104 msleep( INT64_C(1000000) * l_queued / p_aout->output.output.i_rate / 2 );
1107 /* make sure the buffer isn't playing */
1108 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
1110 /* free the event */
1111 CloseHandle( p_notif->event );
1113 vlc_restorecancel (canc);
1114 msg_Dbg( p_aout, "DirectSoundThread exiting" );
1118 /*****************************************************************************
1119 * CallBackConfigNBEnum: callback to get the number of available devices
1120 *****************************************************************************/
1121 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1122 LPCWSTR psz_mod, LPVOID p_nb )
1124 VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1126 int * a = (int *)p_nb;
1131 /*****************************************************************************
1132 * CallBackConfigEnum: callback to add available devices to the preferences list
1133 *****************************************************************************/
1134 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1135 LPCWSTR psz_mod, LPVOID _p_item )
1137 VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1139 module_config_t *p_item = (module_config_t *) _p_item;
1141 p_item->ppsz_list[p_item->i_list] = FromWide( psz_desc );
1142 p_item->ppsz_list_text[p_item->i_list] = FromWide( psz_desc );
1147 /*****************************************************************************
1148 * ReloadDirectXDevices: store the list of devices in preferences
1149 *****************************************************************************/
1150 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1151 vlc_value_t newval, vlc_value_t oldval, void *data )
1153 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1155 module_config_t *p_item = config_FindConfig( p_this, psz_name );
1156 if( !p_item ) return VLC_SUCCESS;
1158 /* Clear-up the current list */
1159 if( p_item->i_list )
1161 for( int i = 0; i < p_item->i_list; i++ )
1163 free((char *)(p_item->ppsz_list[i]) );
1164 free((char *)(p_item->ppsz_list_text[i]) );
1168 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
1170 HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1171 if( hdsound_dll == NULL )
1173 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1177 /* Get DirectSoundEnumerate */
1178 OurDirectSoundEnumerate = (void *)
1179 GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1181 if( OurDirectSoundEnumerate == NULL )
1185 OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1186 msg_Dbg(p_this,"found %d devices", nb_devices);
1188 p_item->ppsz_list = xrealloc( p_item->ppsz_list,
1189 nb_devices * sizeof(char *) );
1190 p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
1191 nb_devices * sizeof(char *) );
1194 OurDirectSoundEnumerate(CallBackConfigEnum, p_item);
1196 /* Signal change to the interface */
1197 p_item->b_dirty = true;
1200 FreeLibrary(hdsound_dll);