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 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->sys = malloc( sizeof( aout_sys_t ) );
167 if( p_aout->sys == NULL )
170 /* Initialize some variables */
171 p_aout->sys->p_dsobject = NULL;
172 p_aout->sys->p_dsbuffer = NULL;
173 p_aout->sys->p_notif = NULL;
174 p_aout->sys->b_playing = 0;
176 p_aout->pf_play = Play;
177 p_aout->pf_pause = NULL;
178 aout_VolumeSoftInit( p_aout );
180 /* Retrieve config values */
181 var_Create( p_aout, "directx-audio-float32",
182 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
183 psz_speaker = var_CreateGetString( p_aout, "directx-audio-speaker" );
185 while ( *ppsz_compare != NULL )
187 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
194 if ( *ppsz_compare == NULL )
196 msg_Err( p_aout, "(%s) isn't valid speaker setup option", psz_speaker );
197 msg_Err( p_aout, "Defaulting to Windows default speaker config");
201 p_aout->sys->i_speaker_setup = i;
203 p_aout->sys->p_device_guid = 0;
205 /* Initialise DirectSound */
206 if( InitDirectSound( p_aout ) )
208 msg_Err( p_aout, "cannot initialize DirectSound" );
212 if( var_Type( p_aout, "audio-device" ) == 0 )
217 if( var_Get( p_aout, "audio-device", &val ) < 0 )
219 /* Probe() has failed. */
223 /* Open the device */
224 if( val.i_int == AOUT_VAR_SPDIF )
226 p_aout->format.i_format = VLC_CODEC_SPDIFL;
228 /* Calculate the frame size in bytes */
229 p_aout->i_nb_samples = A52_FRAME_NB;
230 p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
231 p_aout->format.i_frame_length = A52_FRAME_NB;
232 p_aout->sys->i_frame_size = p_aout->format.i_bytes_per_frame;
234 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
235 p_aout->format.i_physical_channels,
236 aout_FormatNbChannels( &p_aout->format ),
237 p_aout->format.i_rate,
238 p_aout->sys->i_frame_size, false )
241 msg_Err( p_aout, "cannot open directx audio device" );
246 aout_VolumeNoneInit( p_aout );
250 if( val.i_int == AOUT_VAR_5_1 )
252 p_aout->format.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->format.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->format.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->format.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->format.i_physical_channels = AOUT_CHAN_CENTER;
283 p_aout->format.i_physical_channels
284 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
287 if( CreateDSBufferPCM( p_aout, &p_aout->format.i_format,
288 p_aout->format.i_physical_channels,
289 aout_FormatNbChannels( &p_aout->format ),
290 p_aout->format.i_rate, false )
293 msg_Err( p_aout, "cannot open directx audio device" );
298 /* Calculate the frame size in bytes */
299 p_aout->i_nb_samples = FRAME_SIZE;
300 aout_FormatPrepare( &p_aout->format );
301 aout_VolumeSoftInit( p_aout );
304 /* Now we need to setup our DirectSound play notification structure */
305 p_aout->sys->p_notif = calloc( 1, sizeof( *p_aout->sys->p_notif ) );
306 p_aout->sys->p_notif->p_aout = p_aout;
308 vlc_atomic_set(&p_aout->sys->p_notif->abort, 0);
309 p_aout->sys->p_notif->event = CreateEvent( 0, FALSE, FALSE, 0 );
310 p_aout->sys->p_notif->i_frame_size = p_aout->sys->i_frame_size;
312 /* then launch the notification thread */
313 msg_Dbg( p_aout, "creating DirectSoundThread" );
314 if( vlc_clone( &p_aout->sys->p_notif->thread,
315 DirectSoundThread, p_aout->sys->p_notif,
316 VLC_THREAD_PRIORITY_HIGHEST ) )
318 msg_Err( p_aout, "cannot create DirectSoundThread" );
319 CloseHandle( p_aout->sys->p_notif->event );
320 free( p_aout->sys->p_notif );
321 p_aout->sys->p_notif = NULL;
328 CloseAudio( VLC_OBJECT(p_aout) );
332 /*****************************************************************************
333 * Probe: probe the audio device for available formats and channels
334 *****************************************************************************/
335 static void Probe( aout_instance_t * p_aout )
337 vlc_value_t val, text;
338 vlc_fourcc_t i_format;
339 unsigned int i_physical_channels;
340 DWORD ui_speaker_config;
341 bool is_default_output_set = false;
343 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
344 text.psz_string = _("Audio Device");
345 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
347 /* Test for 5.1 support */
348 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
349 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
350 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
351 if( p_aout->format.i_physical_channels == i_physical_channels )
353 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
354 p_aout->format.i_rate, true )
357 val.i_int = AOUT_VAR_5_1;
358 text.psz_string = (char*) "5.1";
359 var_Change( p_aout, "audio-device",
360 VLC_VAR_ADDCHOICE, &val, &text );
361 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
362 is_default_output_set = true;
363 msg_Dbg( p_aout, "device supports 5.1 channels" );
367 /* Test for 7.1 support */
368 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
369 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
370 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
371 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
372 if( p_aout->format.i_physical_channels == i_physical_channels )
374 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
375 p_aout->format.i_rate, true )
378 val.i_int = AOUT_VAR_7_1;
379 text.psz_string = (char*) "7.1";
380 var_Change( p_aout, "audio-device",
381 VLC_VAR_ADDCHOICE, &val, &text );
382 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
383 is_default_output_set = true;
384 msg_Dbg( p_aout, "device supports 7.1 channels" );
388 /* Test for 3 Front 2 Rear support */
389 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
390 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
392 if( p_aout->format.i_physical_channels == i_physical_channels )
394 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
395 p_aout->format.i_rate, true )
398 val.i_int = AOUT_VAR_3F2R;
399 text.psz_string = _("3 Front 2 Rear");
400 var_Change( p_aout, "audio-device",
401 VLC_VAR_ADDCHOICE, &val, &text );
402 if(!is_default_output_set)
404 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
405 is_default_output_set = true;
407 msg_Dbg( p_aout, "device supports 5 channels" );
411 /* Test for 2 Front 2 Rear support */
412 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
413 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
414 if( ( p_aout->format.i_physical_channels & i_physical_channels )
415 == i_physical_channels )
417 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
418 p_aout->format.i_rate, true )
421 val.i_int = AOUT_VAR_2F2R;
422 text.psz_string = _("2 Front 2 Rear");
423 var_Change( p_aout, "audio-device",
424 VLC_VAR_ADDCHOICE, &val, &text );
425 if(!is_default_output_set)
427 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
428 is_default_output_set = true;
430 msg_Dbg( p_aout, "device supports 4 channels" );
434 /* Test for stereo support */
435 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
436 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
437 p_aout->format.i_rate, true )
440 val.i_int = AOUT_VAR_STEREO;
441 text.psz_string = _("Stereo");
442 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
443 if(!is_default_output_set)
445 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
446 is_default_output_set = true;
447 msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
449 else msg_Dbg( p_aout, "device supports 2 channels" );
452 /* Test for mono support */
453 i_physical_channels = AOUT_CHAN_CENTER;
454 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
455 p_aout->format.i_rate, true )
458 val.i_int = AOUT_VAR_MONO;
459 text.psz_string = _("Mono");
460 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
461 msg_Dbg( p_aout, "device supports 1 channel" );
464 /* Check the speaker configuration to determine which channel config should
466 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->sys->p_dsobject,
467 &ui_speaker_config ) )
469 ui_speaker_config = DSSPEAKER_STEREO;
470 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
472 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
474 case DSSPEAKER_7POINT1:
475 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
476 val.i_int = AOUT_VAR_7_1;
478 case DSSPEAKER_5POINT1:
479 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
480 val.i_int = AOUT_VAR_5_1;
483 msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
484 val.i_int = AOUT_VAR_2F2R;
486 #if 0 /* Lots of people just get their settings wrong and complain that
487 * this is a problem with VLC so just don't ever set mono by default. */
489 val.i_int = AOUT_VAR_MONO;
492 case DSSPEAKER_SURROUND:
493 msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
494 case DSSPEAKER_STEREO:
495 msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
497 /* If nothing else is found, choose stereo output */
498 val.i_int = AOUT_VAR_STEREO;
502 /* Check if we want to override speaker config */
503 switch( p_aout->sys->i_speaker_setup )
505 case 0: /* Default value aka Windows default speaker setup */
508 msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
509 val.i_int = AOUT_VAR_MONO;
512 msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
513 val.i_int = AOUT_VAR_STEREO;
516 msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
517 val.i_int = AOUT_VAR_2F2R;
520 msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
521 val.i_int = AOUT_VAR_5_1;
524 msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
525 val.i_int = AOUT_VAR_7_1;
528 msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
532 var_Set( p_aout, "audio-device", val );
534 /* Test for SPDIF support */
535 if ( AOUT_FMT_NON_LINEAR( &p_aout->format ) )
537 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
538 p_aout->format.i_physical_channels,
539 aout_FormatNbChannels( &p_aout->format ),
540 p_aout->format.i_rate,
541 AOUT_SPDIF_SIZE, true )
544 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
545 val.i_int = AOUT_VAR_SPDIF;
546 text.psz_string = _("A/52 over S/PDIF");
547 var_Change( p_aout, "audio-device",
548 VLC_VAR_ADDCHOICE, &val, &text );
549 if( var_InheritBool( p_aout, "spdif" ) )
550 var_Set( p_aout, "audio-device", val );
554 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
557 /* Probe() has failed. */
558 var_Destroy( p_aout, "audio-device" );
562 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
563 var_TriggerCallback( p_aout, "intf-change" );
566 /*****************************************************************************
567 * Play: we'll start playing the directsound buffer here because at least here
568 * we know the first buffer has been put in the aout fifo and we also
570 *****************************************************************************/
571 static void Play( aout_instance_t *p_aout )
573 if( !p_aout->sys->b_playing )
575 aout_buffer_t *p_buffer;
577 p_aout->sys->b_playing = 1;
579 /* get the playing date of the first aout buffer */
580 p_aout->sys->p_notif->start_date =
581 aout_FifoFirstDate( &p_aout->fifo );
583 /* fill in the first samples */
584 for( int i = 0; i < FRAMES_NUM; i++ )
586 p_buffer = aout_FifoPop( &p_aout->fifo );
587 if( !p_buffer ) break;
588 FillBuffer( p_aout, i, p_buffer );
591 /* wake up the audio output thread */
592 SetEvent( p_aout->sys->p_notif->event );
596 /*****************************************************************************
597 * CloseAudio: close the audio device
598 *****************************************************************************/
599 static void CloseAudio( vlc_object_t *p_this )
601 aout_instance_t * p_aout = (aout_instance_t *)p_this;
602 aout_sys_t *p_sys = p_aout->sys;
604 msg_Dbg( p_aout, "closing audio device" );
606 /* kill the position notification thread, if any */
609 vlc_atomic_set(&p_aout->sys->p_notif->abort, 1);
610 /* wake up the audio thread if needed */
611 if( !p_sys->b_playing ) SetEvent( p_sys->p_notif->event );
613 vlc_join( p_sys->p_notif->thread, NULL );
614 free( p_sys->p_notif );
617 /* release the secondary buffer */
618 DestroyDSBuffer( p_aout );
620 /* finally release the DirectSound object */
621 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
623 /* free DSOUND.DLL */
624 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
626 free( p_aout->sys->p_device_guid );
630 /*****************************************************************************
631 * CallBackDirectSoundEnum: callback to enumerate available devices
632 *****************************************************************************/
633 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
634 LPCWSTR psz_mod, LPVOID _p_aout )
636 VLC_UNUSED( psz_mod );
638 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
640 char *psz_device = FromWide( psz_desc );
641 msg_Dbg( p_aout, "found device: %s", psz_device );
643 if( p_aout->sys->psz_device &&
644 !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
646 /* Use the device corresponding to 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 );
653 /* If no default device has been selected, chose the first one */
654 if( !p_aout->sys->psz_device && p_guid )
656 p_aout->sys->psz_device = strdup( psz_device );
657 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
658 *p_aout->sys->p_device_guid = *p_guid;
659 msg_Dbg( p_aout, "using device: %s", psz_device );
667 /*****************************************************************************
668 * InitDirectSound: handle all the gory details of DirectSound initialisation
669 *****************************************************************************/
670 static int InitDirectSound( aout_instance_t *p_aout )
672 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
673 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
675 p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
676 if( p_aout->sys->hdsound_dll == NULL )
678 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
682 OurDirectSoundCreate = (void *)
683 GetProcAddress( p_aout->sys->hdsound_dll,
684 "DirectSoundCreate" );
685 if( OurDirectSoundCreate == NULL )
687 msg_Warn( p_aout, "GetProcAddress FAILED" );
691 /* Get DirectSoundEnumerate */
692 OurDirectSoundEnumerate = (void *)
693 GetProcAddress( p_aout->sys->hdsound_dll,
694 "DirectSoundEnumerateW" );
695 if( OurDirectSoundEnumerate )
697 p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device-name");
698 /* Attempt enumeration */
699 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
702 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
706 /* Create the direct sound object */
707 if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
708 &p_aout->sys->p_dsobject,
711 msg_Warn( p_aout, "cannot create a direct sound device" );
715 /* Set DirectSound Cooperative level, ie what control we want over Windows
716 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
717 * settings of the primary buffer, but also that only the sound of our
718 * application will be hearable when it will have the focus.
719 * !!! (this is not really working as intended yet because to set the
720 * cooperative level you need the window handle of your application, and
721 * I don't know of any easy way to get it. Especially since we might play
722 * sound without any video, and so what window handle should we use ???
723 * The hack for now is to use the Desktop window handle - it seems to be
725 if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
729 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
735 p_aout->sys->p_dsobject = NULL;
736 if( p_aout->sys->hdsound_dll )
738 FreeLibrary( p_aout->sys->hdsound_dll );
739 p_aout->sys->hdsound_dll = NULL;
745 /*****************************************************************************
746 * CreateDSBuffer: Creates a direct sound buffer of the required format.
747 *****************************************************************************
748 * This function creates the buffer we'll use to play audio.
749 * In DirectSound there are two kinds of buffers:
750 * - the primary buffer: which is the actual buffer that the soundcard plays
751 * - the secondary buffer(s): these buffers are the one actually used by
752 * applications and DirectSound takes care of mixing them into the primary.
754 * Once you create a secondary buffer, you cannot change its format anymore so
755 * you have to release the current one and create another.
756 *****************************************************************************/
757 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
758 int i_channels, int i_nb_channels, int i_rate,
759 int i_bytes_per_frame, bool b_probe )
761 WAVEFORMATEXTENSIBLE waveformat;
762 DSBUFFERDESC dsbdesc;
765 /* First set the sound buffer format */
766 waveformat.dwChannelMask = 0;
767 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
769 if( i_channels & pi_channels_src[i] )
770 waveformat.dwChannelMask |= pi_channels_in[i];
775 case VLC_CODEC_SPDIFL:
777 /* To prevent channel re-ordering */
778 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
779 waveformat.Format.wBitsPerSample = 16;
780 waveformat.Samples.wValidBitsPerSample =
781 waveformat.Format.wBitsPerSample;
782 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
783 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
787 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
788 waveformat.Samples.wValidBitsPerSample =
789 waveformat.Format.wBitsPerSample;
790 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
791 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
795 waveformat.Format.wBitsPerSample = 16;
796 waveformat.Samples.wValidBitsPerSample =
797 waveformat.Format.wBitsPerSample;
798 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
799 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
803 waveformat.Format.nChannels = i_nb_channels;
804 waveformat.Format.nSamplesPerSec = i_rate;
805 waveformat.Format.nBlockAlign =
806 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
807 waveformat.Format.nAvgBytesPerSec =
808 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
810 p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
811 p_aout->sys->i_channels = i_nb_channels;
813 /* Then fill in the direct sound descriptor */
814 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
815 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
816 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
817 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
819 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
820 if( i_nb_channels <= 2 )
822 waveformat.Format.cbSize = 0;
826 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
827 waveformat.Format.cbSize =
828 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
830 /* Needed for 5.1 on emu101k */
831 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
834 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
835 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
837 if FAILED( IDirectSound_CreateSoundBuffer(
838 p_aout->sys->p_dsobject, &dsbdesc,
839 &p_aout->sys->p_dsbuffer, NULL) )
841 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
843 /* Try without DSBCAPS_LOCHARDWARE */
844 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
845 if FAILED( IDirectSound_CreateSoundBuffer(
846 p_aout->sys->p_dsobject, &dsbdesc,
847 &p_aout->sys->p_dsbuffer, NULL) )
852 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
860 /* Stop here if we were just probing */
863 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
864 p_aout->sys->p_dsbuffer = NULL;
868 p_aout->sys->i_frame_size = i_bytes_per_frame;
869 p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
870 p_aout->sys->b_chan_reorder =
871 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
872 waveformat.dwChannelMask, i_nb_channels,
873 p_aout->sys->pi_chan_table );
875 if( p_aout->sys->b_chan_reorder )
877 msg_Dbg( p_aout, "channel reordering needed" );
883 /*****************************************************************************
884 * CreateDSBufferPCM: creates a PCM direct sound buffer.
885 *****************************************************************************
886 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
887 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
888 ****************************************************************************/
889 static int CreateDSBufferPCM( aout_instance_t *p_aout, vlc_fourcc_t *i_format,
890 int i_channels, int i_nb_channels, int i_rate,
893 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
894 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
896 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
897 i_channels, i_nb_channels, i_rate,
898 FRAME_SIZE * 4 * i_nb_channels, b_probe )
901 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
902 i_channels, i_nb_channels, i_rate,
903 FRAME_SIZE * 2 * i_nb_channels, b_probe )
910 *i_format = VLC_CODEC_S16L;
916 *i_format = VLC_CODEC_FL32;
921 /*****************************************************************************
923 *****************************************************************************
924 * This function destroys the secondary buffer.
925 *****************************************************************************/
926 static void DestroyDSBuffer( aout_instance_t *p_aout )
928 if( p_aout->sys->p_dsbuffer )
930 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
931 p_aout->sys->p_dsbuffer = NULL;
935 /*****************************************************************************
936 * FillBuffer: Fill in one of the direct sound frame buffers.
937 *****************************************************************************
938 * Returns VLC_SUCCESS on success.
939 *****************************************************************************/
940 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
941 aout_buffer_t *p_buffer )
943 notification_thread_t *p_notif = p_aout->sys->p_notif;
944 aout_sys_t *p_sys = p_aout->sys;
945 void *p_write_position, *p_wrap_around;
946 unsigned long l_bytes1, l_bytes2;
949 /* Before copying anything, we have to lock the buffer */
950 dsresult = IDirectSoundBuffer_Lock(
951 p_sys->p_dsbuffer, /* DS buffer */
952 i_frame * p_notif->i_frame_size, /* Start offset */
953 p_notif->i_frame_size, /* Number of bytes */
954 &p_write_position, /* Address of lock start */
955 &l_bytes1, /* Count of bytes locked before wrap around */
956 &p_wrap_around, /* Buffer address (if wrap around) */
957 &l_bytes2, /* Count of bytes after wrap around */
959 if( dsresult == DSERR_BUFFERLOST )
961 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
962 dsresult = IDirectSoundBuffer_Lock(
964 i_frame * p_notif->i_frame_size,
965 p_notif->i_frame_size,
972 if( dsresult != DS_OK )
974 msg_Warn( p_aout, "cannot lock buffer" );
975 if( p_buffer ) aout_BufferFree( p_buffer );
979 if( p_buffer == NULL )
981 memset( p_write_position, 0, l_bytes1 );
985 if( p_sys->b_chan_reorder )
987 /* Do the channel reordering here */
988 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
989 p_sys->i_channels, p_sys->pi_chan_table,
990 p_sys->i_bits_per_sample );
993 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
994 aout_BufferFree( p_buffer );
997 /* Now the data has been copied, unlock the buffer */
998 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
999 p_wrap_around, l_bytes2 );
1001 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1005 /*****************************************************************************
1006 * DirectSoundThread: this thread will capture play notification events.
1007 *****************************************************************************
1008 * We use this thread to emulate a callback mechanism. The thread probes for
1009 * event notification and fills up the DS secondary buffer when needed.
1010 *****************************************************************************/
1011 static void* DirectSoundThread( void *data )
1013 notification_thread_t *p_notif = (notification_thread_t*)data;
1014 aout_instance_t *p_aout = p_notif->p_aout;
1016 int canc = vlc_savecancel ();
1018 /* We don't want any resampling when using S/PDIF output */
1019 bool b_sleek = (p_aout->format.i_format == VLC_CODEC_SPDIFL);
1021 msg_Dbg( p_aout, "DirectSoundThread ready" );
1023 /* Wait here until Play() is called */
1024 WaitForSingleObject( p_notif->event, INFINITE );
1026 if( !vlc_atomic_get( &p_notif->abort) )
1029 mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1031 /* start playing the buffer */
1032 dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1035 DSBPLAY_LOOPING ); /* Flags */
1036 if( dsresult == DSERR_BUFFERLOST )
1038 IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1039 dsresult = IDirectSoundBuffer_Play(
1040 p_aout->sys->p_dsbuffer,
1043 DSBPLAY_LOOPING ); /* Flags */
1045 if( dsresult != DS_OK )
1047 msg_Err( p_aout, "cannot start playing buffer" );
1050 last_time = mdate();
1052 while( !vlc_atomic_get( &p_notif->abort ) )
1055 int l_queued = 0, l_free_slots;
1056 unsigned i_frame_siz = p_aout->i_nb_samples;
1057 mtime_t mtime = mdate();
1061 * Fill in as much audio data as we can in our circular buffer
1064 /* Find out current play position */
1065 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1066 p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1068 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1072 /* Detect underruns */
1073 if( l_queued && mtime - last_time >
1074 INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1076 msg_Dbg( p_aout, "detected underrun!" );
1080 /* Try to fill in as many frame buffers as possible */
1081 l_read /= (p_aout->format.i_bytes_per_frame /
1082 p_aout->format.i_frame_length);
1083 l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1084 if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1085 l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1087 for( i = 0; i < l_free_slots; i++ )
1089 aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout,
1090 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1091 p_aout->format.i_rate, b_sleek );
1093 /* If there is no audio data available and we have some buffered
1094 * already, then just wait for the next time */
1095 if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1097 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1098 p_buffer ) != VLC_SUCCESS ) break;
1101 /* Sleep a reasonable amount of time */
1102 l_queued += (i * i_frame_siz);
1103 msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1106 /* make sure the buffer isn't playing */
1107 IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1109 /* free the event */
1110 CloseHandle( p_notif->event );
1112 vlc_restorecancel (canc);
1113 msg_Dbg( p_aout, "DirectSoundThread exiting" );
1117 /*****************************************************************************
1118 * CallBackConfigNBEnum: callback to get the number of available devices
1119 *****************************************************************************/
1120 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1121 LPCWSTR psz_mod, LPVOID p_nb )
1123 VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1125 int * a = (int *)p_nb;
1130 /*****************************************************************************
1131 * CallBackConfigEnum: callback to add available devices to the preferences list
1132 *****************************************************************************/
1133 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1134 LPCWSTR psz_mod, LPVOID _p_item )
1136 VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1138 module_config_t *p_item = (module_config_t *) _p_item;
1140 p_item->ppsz_list[p_item->i_list] = FromWide( psz_desc );
1141 p_item->ppsz_list_text[p_item->i_list] = FromWide( psz_desc );
1146 /*****************************************************************************
1147 * ReloadDirectXDevices: store the list of devices in preferences
1148 *****************************************************************************/
1149 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1150 vlc_value_t newval, vlc_value_t oldval, void *data )
1152 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1154 module_config_t *p_item = config_FindConfig( p_this, psz_name );
1155 if( !p_item ) return VLC_SUCCESS;
1157 /* Clear-up the current list */
1158 if( p_item->i_list )
1160 for( int i = 0; i < p_item->i_list; i++ )
1162 free((char *)(p_item->ppsz_list[i]) );
1163 free((char *)(p_item->ppsz_list_text[i]) );
1167 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
1169 HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1170 if( hdsound_dll == NULL )
1172 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1176 /* Get DirectSoundEnumerate */
1177 OurDirectSoundEnumerate = (void *)
1178 GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1180 if( OurDirectSoundEnumerate == NULL )
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 /* Signal change to the interface */
1196 p_item->b_dirty = true;
1199 FreeLibrary(hdsound_dll);