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 audio_output_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 *****************************************************************************/
68 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
70 char * psz_device; /* user defined device name */
73 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
74 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
75 * takes care of mixing all the
76 * secondary buffers into the primary) */
78 notification_thread_t *p_notif; /* DirectSoundThread id */
80 int i_frame_size; /* Size in bytes of one frame */
82 int i_speaker_setup; /* Speaker setup override */
84 bool b_chan_reorder; /* do we need channel reordering */
85 int pi_chan_table[AOUT_CHAN_MAX];
86 uint32_t i_channel_mask;
87 uint32_t i_bits_per_sample;
91 /*****************************************************************************
93 *****************************************************************************/
94 static int OpenAudio ( vlc_object_t * );
95 static void CloseAudio ( vlc_object_t * );
96 static void Play ( audio_output_t *, block_t * );
99 static void Probe ( audio_output_t * );
100 static int InitDirectSound ( audio_output_t * );
101 static int CreateDSBuffer ( audio_output_t *, int, int, int, int, int, bool );
102 static int CreateDSBufferPCM ( audio_output_t *, vlc_fourcc_t*, int, int, int, bool );
103 static void DestroyDSBuffer ( audio_output_t * );
104 static void* DirectSoundThread( void * );
105 static int FillBuffer ( audio_output_t *, int, aout_buffer_t * );
107 static int ReloadDirectXDevices( vlc_object_t *, char const *,
108 vlc_value_t, vlc_value_t, void * );
110 /* Speaker setup override options list */
111 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
112 "Quad", "5.1", "7.1" };
113 static const char *const ppsz_adev[] = {"default", };
114 static const char *const ppsz_adev_text[] = {"default", };
116 /*****************************************************************************
118 *****************************************************************************/
119 #define DEVICE_TEXT N_("Output device")
120 #define DEVICE_LONGTEXT N_("Select your audio output device")
122 #define SPEAKER_TEXT N_("Speaker configuration")
123 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
124 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
127 set_description( N_("DirectX audio output") )
128 set_shortname( "DirectX" )
129 set_capability( "audio output", 100 )
130 set_category( CAT_AUDIO )
131 set_subcategory( SUBCAT_AUDIO_AOUT )
132 add_shortcut( "directx", "directsound" )
134 add_string( "directx-audio-device-name", "default",
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, FLOAT_TEXT,
139 FLOAT_LONGTEXT, true )
140 add_string( "directx-audio-speaker", "Windows default",
141 SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
142 change_string_list( speaker_list, 0, 0 )
144 set_callbacks( OpenAudio, CloseAudio )
147 /*****************************************************************************
148 * OpenAudio: open the audio device
149 *****************************************************************************
150 * This function opens and setups Direct Sound.
151 *****************************************************************************/
152 static int OpenAudio( vlc_object_t *p_this )
154 audio_output_t * p_aout = (audio_output_t *)p_this;
159 const char * const * ppsz_compare = speaker_list;
161 msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
163 /* Allocate structure */
164 p_aout->sys = malloc( sizeof( aout_sys_t ) );
165 if( p_aout->sys == NULL )
168 /* Initialize some variables */
169 p_aout->sys->p_dsobject = NULL;
170 p_aout->sys->p_dsbuffer = NULL;
171 p_aout->sys->p_notif = NULL;
173 p_aout->pf_play = Play;
174 p_aout->pf_pause = aout_PacketPause;
175 p_aout->pf_flush = aout_PacketFlush;
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", psz_speaker );
195 msg_Err( p_aout, "Defaulting to Windows default speaker config");
199 p_aout->sys->i_speaker_setup = i;
201 p_aout->sys->p_device_guid = 0;
203 /* Initialise DirectSound */
204 if( InitDirectSound( p_aout ) )
206 msg_Err( p_aout, "cannot initialize DirectSound" );
210 if( var_Type( p_aout, "audio-device" ) == 0 )
215 if( var_Get( p_aout, "audio-device", &val ) < 0 )
217 /* Probe() has failed. */
221 /* Open the device */
222 if( val.i_int == AOUT_VAR_SPDIF )
224 p_aout->format.i_format = VLC_CODEC_SPDIFL;
226 /* Calculate the frame size in bytes */
227 p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
228 p_aout->format.i_frame_length = A52_FRAME_NB;
229 p_aout->sys->i_frame_size = p_aout->format.i_bytes_per_frame;
231 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
232 p_aout->format.i_physical_channels,
233 aout_FormatNbChannels( &p_aout->format ),
234 p_aout->format.i_rate,
235 p_aout->sys->i_frame_size, false )
238 msg_Err( p_aout, "cannot open directx audio device" );
243 aout_PacketInit( p_aout, &p_aout->sys->packet, A52_FRAME_NB );
244 aout_VolumeNoneInit( p_aout );
248 if( val.i_int == AOUT_VAR_5_1 )
250 p_aout->format.i_physical_channels
251 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
252 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
255 else if( val.i_int == AOUT_VAR_7_1 )
257 p_aout->format.i_physical_channels
258 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
259 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
260 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
263 else if( val.i_int == AOUT_VAR_3F2R )
265 p_aout->format.i_physical_channels
266 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
267 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
269 else if( val.i_int == AOUT_VAR_2F2R )
271 p_aout->format.i_physical_channels
272 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
273 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
275 else if( val.i_int == AOUT_VAR_MONO )
277 p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
281 p_aout->format.i_physical_channels
282 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
285 if( CreateDSBufferPCM( p_aout, &p_aout->format.i_format,
286 p_aout->format.i_physical_channels,
287 aout_FormatNbChannels( &p_aout->format ),
288 p_aout->format.i_rate, false )
291 msg_Err( p_aout, "cannot open directx audio device" );
296 /* Calculate the frame size in bytes */
297 aout_FormatPrepare( &p_aout->format );
298 aout_PacketInit( p_aout, &p_aout->sys->packet, FRAME_SIZE );
299 aout_VolumeSoftInit( p_aout );
302 /* Now we need to setup our DirectSound play notification structure */
303 p_aout->sys->p_notif = calloc( 1, sizeof( *p_aout->sys->p_notif ) );
304 p_aout->sys->p_notif->p_aout = p_aout;
306 vlc_atomic_set(&p_aout->sys->p_notif->abort, 0);
307 p_aout->sys->p_notif->event = CreateEvent( 0, FALSE, FALSE, 0 );
308 p_aout->sys->p_notif->i_frame_size = p_aout->sys->i_frame_size;
310 /* then launch the notification thread */
311 msg_Dbg( p_aout, "creating DirectSoundThread" );
312 if( vlc_clone( &p_aout->sys->p_notif->thread,
313 DirectSoundThread, p_aout->sys->p_notif,
314 VLC_THREAD_PRIORITY_HIGHEST ) )
316 msg_Err( p_aout, "cannot create DirectSoundThread" );
317 CloseHandle( p_aout->sys->p_notif->event );
318 free( p_aout->sys->p_notif );
319 p_aout->sys->p_notif = NULL;
326 CloseAudio( VLC_OBJECT(p_aout) );
330 /*****************************************************************************
331 * Probe: probe the audio device for available formats and channels
332 *****************************************************************************/
333 static void Probe( audio_output_t * p_aout )
335 vlc_value_t val, text;
336 vlc_fourcc_t i_format;
337 unsigned int i_physical_channels;
338 DWORD ui_speaker_config;
339 bool is_default_output_set = false;
341 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
342 text.psz_string = _("Audio Device");
343 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
345 /* Test for 5.1 support */
346 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
347 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
348 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
349 if( p_aout->format.i_physical_channels == i_physical_channels )
351 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
352 p_aout->format.i_rate, true )
355 val.i_int = AOUT_VAR_5_1;
356 text.psz_string = (char*) "5.1";
357 var_Change( p_aout, "audio-device",
358 VLC_VAR_ADDCHOICE, &val, &text );
359 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
360 is_default_output_set = true;
361 msg_Dbg( p_aout, "device supports 5.1 channels" );
365 /* Test for 7.1 support */
366 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
367 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
368 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
369 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
370 if( p_aout->format.i_physical_channels == i_physical_channels )
372 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
373 p_aout->format.i_rate, true )
376 val.i_int = AOUT_VAR_7_1;
377 text.psz_string = (char*) "7.1";
378 var_Change( p_aout, "audio-device",
379 VLC_VAR_ADDCHOICE, &val, &text );
380 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
381 is_default_output_set = true;
382 msg_Dbg( p_aout, "device supports 7.1 channels" );
386 /* Test for 3 Front 2 Rear support */
387 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
388 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
390 if( p_aout->format.i_physical_channels == i_physical_channels )
392 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
393 p_aout->format.i_rate, true )
396 val.i_int = AOUT_VAR_3F2R;
397 text.psz_string = _("3 Front 2 Rear");
398 var_Change( p_aout, "audio-device",
399 VLC_VAR_ADDCHOICE, &val, &text );
400 if(!is_default_output_set)
402 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
403 is_default_output_set = true;
405 msg_Dbg( p_aout, "device supports 5 channels" );
409 /* Test for 2 Front 2 Rear support */
410 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
411 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
412 if( ( p_aout->format.i_physical_channels & i_physical_channels )
413 == i_physical_channels )
415 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
416 p_aout->format.i_rate, true )
419 val.i_int = AOUT_VAR_2F2R;
420 text.psz_string = _("2 Front 2 Rear");
421 var_Change( p_aout, "audio-device",
422 VLC_VAR_ADDCHOICE, &val, &text );
423 if(!is_default_output_set)
425 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
426 is_default_output_set = true;
428 msg_Dbg( p_aout, "device supports 4 channels" );
432 /* Test for stereo support */
433 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
434 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
435 p_aout->format.i_rate, true )
438 val.i_int = AOUT_VAR_STEREO;
439 text.psz_string = _("Stereo");
440 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
441 if(!is_default_output_set)
443 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
444 is_default_output_set = true;
445 msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
447 else msg_Dbg( p_aout, "device supports 2 channels" );
450 /* Test for mono support */
451 i_physical_channels = AOUT_CHAN_CENTER;
452 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
453 p_aout->format.i_rate, true )
456 val.i_int = AOUT_VAR_MONO;
457 text.psz_string = _("Mono");
458 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
459 msg_Dbg( p_aout, "device supports 1 channel" );
462 /* Check the speaker configuration to determine which channel config should
464 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->sys->p_dsobject,
465 &ui_speaker_config ) )
467 ui_speaker_config = DSSPEAKER_STEREO;
468 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
470 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
472 case DSSPEAKER_7POINT1:
473 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
474 val.i_int = AOUT_VAR_7_1;
476 case DSSPEAKER_5POINT1:
477 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
478 val.i_int = AOUT_VAR_5_1;
481 msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
482 val.i_int = AOUT_VAR_2F2R;
484 #if 0 /* Lots of people just get their settings wrong and complain that
485 * this is a problem with VLC so just don't ever set mono by default. */
487 val.i_int = AOUT_VAR_MONO;
490 case DSSPEAKER_SURROUND:
491 msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
492 case DSSPEAKER_STEREO:
493 msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
495 /* If nothing else is found, choose stereo output */
496 val.i_int = AOUT_VAR_STEREO;
500 /* Check if we want to override speaker config */
501 switch( p_aout->sys->i_speaker_setup )
503 case 0: /* Default value aka Windows default speaker setup */
506 msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
507 val.i_int = AOUT_VAR_MONO;
510 msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
511 val.i_int = AOUT_VAR_STEREO;
514 msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
515 val.i_int = AOUT_VAR_2F2R;
518 msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
519 val.i_int = AOUT_VAR_5_1;
522 msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
523 val.i_int = AOUT_VAR_7_1;
526 msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
530 var_Set( p_aout, "audio-device", val );
532 /* Test for SPDIF support */
533 if ( AOUT_FMT_SPDIF( &p_aout->format ) )
535 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
536 p_aout->format.i_physical_channels,
537 aout_FormatNbChannels( &p_aout->format ),
538 p_aout->format.i_rate,
539 AOUT_SPDIF_SIZE, true )
542 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
543 val.i_int = AOUT_VAR_SPDIF;
544 text.psz_string = _("A/52 over S/PDIF");
545 var_Change( p_aout, "audio-device",
546 VLC_VAR_ADDCHOICE, &val, &text );
547 if( var_InheritBool( p_aout, "spdif" ) )
548 var_Set( p_aout, "audio-device", val );
552 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
555 /* Probe() has failed. */
556 var_Destroy( p_aout, "audio-device" );
560 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
561 var_TriggerCallback( p_aout, "intf-change" );
564 /*****************************************************************************
565 * Play: we'll start playing the directsound buffer here because at least here
566 * we know the first buffer has been put in the aout fifo and we also
568 *****************************************************************************/
569 static void Play( audio_output_t *p_aout, block_t *p_buffer )
571 /* get the playing date of the first aout buffer */
572 p_aout->sys->p_notif->start_date = p_buffer->i_pts;
574 /* fill in the first samples (zeroes) */
575 FillBuffer( p_aout, 0, NULL );
577 /* wake up the audio output thread */
578 SetEvent( p_aout->sys->p_notif->event );
580 aout_PacketPlay( p_aout, p_buffer );
581 p_aout->pf_play = aout_PacketPlay;
584 /*****************************************************************************
585 * CloseAudio: close the audio device
586 *****************************************************************************/
587 static void CloseAudio( vlc_object_t *p_this )
589 audio_output_t * p_aout = (audio_output_t *)p_this;
590 aout_sys_t *p_sys = p_aout->sys;
592 msg_Dbg( p_aout, "closing audio device" );
594 /* kill the position notification thread, if any */
597 vlc_atomic_set(&p_aout->sys->p_notif->abort, 1);
598 /* wake up the audio thread if needed */
599 if( p_aout->pf_play == Play )
600 SetEvent( p_sys->p_notif->event );
602 vlc_join( p_sys->p_notif->thread, NULL );
603 free( p_sys->p_notif );
606 /* release the secondary buffer */
607 DestroyDSBuffer( p_aout );
609 /* finally release the DirectSound object */
610 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
612 /* free DSOUND.DLL */
613 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
615 free( p_aout->sys->p_device_guid );
616 aout_PacketDestroy( p_aout );
620 /*****************************************************************************
621 * CallBackDirectSoundEnum: callback to enumerate available devices
622 *****************************************************************************/
623 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
624 LPCWSTR psz_mod, LPVOID _p_aout )
626 VLC_UNUSED( psz_mod );
628 audio_output_t *p_aout = (audio_output_t *)_p_aout;
630 char *psz_device = FromWide( psz_desc );
631 msg_Dbg( p_aout, "found device: %s", psz_device );
633 if( p_aout->sys->psz_device &&
634 !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
636 /* Use the device corresponding to psz_device */
637 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
638 *p_aout->sys->p_device_guid = *p_guid;
639 msg_Dbg( p_aout, "using device: %s", psz_device );
643 /* If no default device has been selected, chose the first one */
644 if( !p_aout->sys->psz_device && p_guid )
646 p_aout->sys->psz_device = strdup( psz_device );
647 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
648 *p_aout->sys->p_device_guid = *p_guid;
649 msg_Dbg( p_aout, "using device: %s", psz_device );
657 /*****************************************************************************
658 * InitDirectSound: handle all the gory details of DirectSound initialisation
659 *****************************************************************************/
660 static int InitDirectSound( audio_output_t *p_aout )
662 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
663 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
665 p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
666 if( p_aout->sys->hdsound_dll == NULL )
668 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
672 OurDirectSoundCreate = (void *)
673 GetProcAddress( p_aout->sys->hdsound_dll,
674 "DirectSoundCreate" );
675 if( OurDirectSoundCreate == NULL )
677 msg_Warn( p_aout, "GetProcAddress FAILED" );
681 /* Get DirectSoundEnumerate */
682 OurDirectSoundEnumerate = (void *)
683 GetProcAddress( p_aout->sys->hdsound_dll,
684 "DirectSoundEnumerateW" );
685 if( OurDirectSoundEnumerate )
687 p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device-name");
688 /* Attempt enumeration */
689 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
692 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
696 /* Create the direct sound object */
697 if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
698 &p_aout->sys->p_dsobject,
701 msg_Warn( p_aout, "cannot create a direct sound device" );
705 /* Set DirectSound Cooperative level, ie what control we want over Windows
706 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
707 * settings of the primary buffer, but also that only the sound of our
708 * application will be hearable when it will have the focus.
709 * !!! (this is not really working as intended yet because to set the
710 * cooperative level you need the window handle of your application, and
711 * I don't know of any easy way to get it. Especially since we might play
712 * sound without any video, and so what window handle should we use ???
713 * The hack for now is to use the Desktop window handle - it seems to be
715 if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
719 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
725 p_aout->sys->p_dsobject = NULL;
726 if( p_aout->sys->hdsound_dll )
728 FreeLibrary( p_aout->sys->hdsound_dll );
729 p_aout->sys->hdsound_dll = NULL;
735 /*****************************************************************************
736 * CreateDSBuffer: Creates a direct sound buffer of the required format.
737 *****************************************************************************
738 * This function creates the buffer we'll use to play audio.
739 * In DirectSound there are two kinds of buffers:
740 * - the primary buffer: which is the actual buffer that the soundcard plays
741 * - the secondary buffer(s): these buffers are the one actually used by
742 * applications and DirectSound takes care of mixing them into the primary.
744 * Once you create a secondary buffer, you cannot change its format anymore so
745 * you have to release the current one and create another.
746 *****************************************************************************/
747 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
748 int i_channels, int i_nb_channels, int i_rate,
749 int i_bytes_per_frame, bool b_probe )
751 WAVEFORMATEXTENSIBLE waveformat;
752 DSBUFFERDESC dsbdesc;
755 /* First set the sound buffer format */
756 waveformat.dwChannelMask = 0;
757 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
759 if( i_channels & pi_channels_src[i] )
760 waveformat.dwChannelMask |= pi_channels_in[i];
765 case VLC_CODEC_SPDIFL:
767 /* To prevent channel re-ordering */
768 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
769 waveformat.Format.wBitsPerSample = 16;
770 waveformat.Samples.wValidBitsPerSample =
771 waveformat.Format.wBitsPerSample;
772 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
773 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
777 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
778 waveformat.Samples.wValidBitsPerSample =
779 waveformat.Format.wBitsPerSample;
780 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
781 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
785 waveformat.Format.wBitsPerSample = 16;
786 waveformat.Samples.wValidBitsPerSample =
787 waveformat.Format.wBitsPerSample;
788 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
789 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
793 waveformat.Format.nChannels = i_nb_channels;
794 waveformat.Format.nSamplesPerSec = i_rate;
795 waveformat.Format.nBlockAlign =
796 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
797 waveformat.Format.nAvgBytesPerSec =
798 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
800 p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
801 p_aout->sys->i_channels = i_nb_channels;
803 /* Then fill in the direct sound descriptor */
804 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
805 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
806 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
807 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
809 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
810 if( i_nb_channels <= 2 )
812 waveformat.Format.cbSize = 0;
816 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
817 waveformat.Format.cbSize =
818 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
820 /* Needed for 5.1 on emu101k */
821 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
824 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
825 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
827 if FAILED( IDirectSound_CreateSoundBuffer(
828 p_aout->sys->p_dsobject, &dsbdesc,
829 &p_aout->sys->p_dsbuffer, NULL) )
831 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
833 /* Try without DSBCAPS_LOCHARDWARE */
834 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
835 if FAILED( IDirectSound_CreateSoundBuffer(
836 p_aout->sys->p_dsobject, &dsbdesc,
837 &p_aout->sys->p_dsbuffer, NULL) )
842 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
850 /* Stop here if we were just probing */
853 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
854 p_aout->sys->p_dsbuffer = NULL;
858 p_aout->sys->i_frame_size = i_bytes_per_frame;
859 p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
860 p_aout->sys->b_chan_reorder =
861 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
862 waveformat.dwChannelMask, i_nb_channels,
863 p_aout->sys->pi_chan_table );
865 if( p_aout->sys->b_chan_reorder )
867 msg_Dbg( p_aout, "channel reordering needed" );
873 /*****************************************************************************
874 * CreateDSBufferPCM: creates a PCM direct sound buffer.
875 *****************************************************************************
876 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
877 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
878 ****************************************************************************/
879 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
880 int i_channels, int i_nb_channels, int i_rate,
883 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
884 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
886 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
887 i_channels, i_nb_channels, i_rate,
888 FRAME_SIZE * 4 * i_nb_channels, b_probe )
891 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
892 i_channels, i_nb_channels, i_rate,
893 FRAME_SIZE * 2 * i_nb_channels, b_probe )
900 *i_format = VLC_CODEC_S16L;
906 *i_format = VLC_CODEC_FL32;
911 /*****************************************************************************
913 *****************************************************************************
914 * This function destroys the secondary buffer.
915 *****************************************************************************/
916 static void DestroyDSBuffer( audio_output_t *p_aout )
918 if( p_aout->sys->p_dsbuffer )
920 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
921 p_aout->sys->p_dsbuffer = NULL;
925 /*****************************************************************************
926 * FillBuffer: Fill in one of the direct sound frame buffers.
927 *****************************************************************************
928 * Returns VLC_SUCCESS on success.
929 *****************************************************************************/
930 static int FillBuffer( audio_output_t *p_aout, int i_frame,
931 aout_buffer_t *p_buffer )
933 notification_thread_t *p_notif = p_aout->sys->p_notif;
934 aout_sys_t *p_sys = p_aout->sys;
935 void *p_write_position, *p_wrap_around;
936 unsigned long l_bytes1, l_bytes2;
939 /* Before copying anything, we have to lock the buffer */
940 dsresult = IDirectSoundBuffer_Lock(
941 p_sys->p_dsbuffer, /* DS buffer */
942 i_frame * p_notif->i_frame_size, /* Start offset */
943 p_notif->i_frame_size, /* Number of bytes */
944 &p_write_position, /* Address of lock start */
945 &l_bytes1, /* Count of bytes locked before wrap around */
946 &p_wrap_around, /* Buffer address (if wrap around) */
947 &l_bytes2, /* Count of bytes after wrap around */
949 if( dsresult == DSERR_BUFFERLOST )
951 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
952 dsresult = IDirectSoundBuffer_Lock(
954 i_frame * p_notif->i_frame_size,
955 p_notif->i_frame_size,
962 if( dsresult != DS_OK )
964 msg_Warn( p_aout, "cannot lock buffer" );
965 if( p_buffer ) aout_BufferFree( p_buffer );
969 if( p_buffer == NULL )
971 memset( p_write_position, 0, l_bytes1 );
975 if( p_sys->b_chan_reorder )
977 /* Do the channel reordering here */
978 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
979 p_sys->i_channels, p_sys->pi_chan_table,
980 p_sys->i_bits_per_sample );
983 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
984 aout_BufferFree( p_buffer );
987 /* Now the data has been copied, unlock the buffer */
988 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
989 p_wrap_around, l_bytes2 );
991 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
995 /*****************************************************************************
996 * DirectSoundThread: this thread will capture play notification events.
997 *****************************************************************************
998 * We use this thread to emulate a callback mechanism. The thread probes for
999 * event notification and fills up the DS secondary buffer when needed.
1000 *****************************************************************************/
1001 static void* DirectSoundThread( void *data )
1003 notification_thread_t *p_notif = (notification_thread_t*)data;
1004 audio_output_t *p_aout = p_notif->p_aout;
1006 int canc = vlc_savecancel ();
1008 msg_Dbg( p_aout, "DirectSoundThread ready" );
1010 /* Wait here until Play() is called */
1011 WaitForSingleObject( p_notif->event, INFINITE );
1013 if( !vlc_atomic_get( &p_notif->abort) )
1016 mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1018 /* start playing the buffer */
1019 dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1022 DSBPLAY_LOOPING ); /* Flags */
1023 if( dsresult == DSERR_BUFFERLOST )
1025 IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1026 dsresult = IDirectSoundBuffer_Play(
1027 p_aout->sys->p_dsbuffer,
1030 DSBPLAY_LOOPING ); /* Flags */
1032 if( dsresult != DS_OK )
1034 msg_Err( p_aout, "cannot start playing buffer" );
1037 last_time = mdate();
1039 while( !vlc_atomic_get( &p_notif->abort ) )
1042 int l_queued = 0, l_free_slots;
1043 unsigned i_frame_siz = p_aout->sys->packet.samples;
1044 mtime_t mtime = mdate();
1048 * Fill in as much audio data as we can in our circular buffer
1051 /* Find out current play position */
1052 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1053 p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1055 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1059 /* Detect underruns */
1060 if( l_queued && mtime - last_time >
1061 INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1063 msg_Dbg( p_aout, "detected underrun!" );
1067 /* Try to fill in as many frame buffers as possible */
1068 l_read /= (p_aout->format.i_bytes_per_frame /
1069 p_aout->format.i_frame_length);
1070 l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1071 if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1072 l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1074 for( i = 0; i < l_free_slots; i++ )
1076 aout_buffer_t *p_buffer = aout_PacketNext( p_aout,
1077 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1078 p_aout->format.i_rate );
1080 /* If there is no audio data available and we have some buffered
1081 * already, then just wait for the next time */
1082 if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1084 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1085 p_buffer ) != VLC_SUCCESS ) break;
1088 /* Sleep a reasonable amount of time */
1089 l_queued += (i * i_frame_siz);
1090 msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1093 /* make sure the buffer isn't playing */
1094 IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1096 /* free the event */
1097 CloseHandle( p_notif->event );
1099 vlc_restorecancel (canc);
1100 msg_Dbg( p_aout, "DirectSoundThread exiting" );
1104 /*****************************************************************************
1105 * CallBackConfigNBEnum: callback to get the number of available devices
1106 *****************************************************************************/
1107 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1108 LPCWSTR psz_mod, LPVOID p_nb )
1110 VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1112 int * a = (int *)p_nb;
1117 /*****************************************************************************
1118 * CallBackConfigEnum: callback to add available devices to the preferences list
1119 *****************************************************************************/
1120 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1121 LPCWSTR psz_mod, LPVOID _p_item )
1123 VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1125 module_config_t *p_item = (module_config_t *) _p_item;
1127 p_item->ppsz_list[p_item->i_list] = FromWide( psz_desc );
1128 p_item->ppsz_list_text[p_item->i_list] = FromWide( psz_desc );
1133 /*****************************************************************************
1134 * ReloadDirectXDevices: store the list of devices in preferences
1135 *****************************************************************************/
1136 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1137 vlc_value_t newval, vlc_value_t oldval, void *data )
1139 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1141 module_config_t *p_item = config_FindConfig( p_this, psz_name );
1142 if( !p_item ) return VLC_SUCCESS;
1144 /* Clear-up the current list */
1145 if( p_item->i_list )
1147 for( int i = 0; i < p_item->i_list; i++ )
1149 free((char *)(p_item->ppsz_list[i]) );
1150 free((char *)(p_item->ppsz_list_text[i]) );
1154 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
1156 HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1157 if( hdsound_dll == NULL )
1159 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1163 /* Get DirectSoundEnumerate */
1164 OurDirectSoundEnumerate = (void *)
1165 GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1167 if( OurDirectSoundEnumerate == NULL )
1171 OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1172 msg_Dbg(p_this,"found %d devices", nb_devices);
1174 p_item->ppsz_list = xrealloc( p_item->ppsz_list,
1175 nb_devices * sizeof(char *) );
1176 p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
1177 nb_devices * sizeof(char *) );
1180 OurDirectSoundEnumerate(CallBackConfigEnum, p_item);
1182 /* Signal change to the interface */
1183 p_item->b_dirty = true;
1186 FreeLibrary(hdsound_dll);