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 *****************************************************************************/
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 i_frame_size; /* Size in bytes of one frame */
81 int i_speaker_setup; /* Speaker setup override */
83 bool b_chan_reorder; /* do we need channel reordering */
84 int pi_chan_table[AOUT_CHAN_MAX];
85 uint32_t i_channel_mask;
86 uint32_t i_bits_per_sample;
90 /*****************************************************************************
92 *****************************************************************************/
93 static int OpenAudio ( vlc_object_t * );
94 static void CloseAudio ( vlc_object_t * );
95 static void Play ( audio_output_t *, block_t * );
98 static void Probe ( audio_output_t * );
99 static int InitDirectSound ( audio_output_t * );
100 static int CreateDSBuffer ( audio_output_t *, int, int, int, int, int, bool );
101 static int CreateDSBufferPCM ( audio_output_t *, vlc_fourcc_t*, int, int, int, bool );
102 static void DestroyDSBuffer ( audio_output_t * );
103 static void* DirectSoundThread( void * );
104 static int FillBuffer ( audio_output_t *, int, aout_buffer_t * );
106 static int ReloadDirectXDevices( vlc_object_t *, char const *,
107 vlc_value_t, vlc_value_t, void * );
109 /* Speaker setup override options list */
110 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
111 "Quad", "5.1", "7.1" };
112 static const char *const ppsz_adev[] = {"default", };
113 static const char *const ppsz_adev_text[] = {"default", };
115 /*****************************************************************************
117 *****************************************************************************/
118 #define DEVICE_TEXT N_("Output device")
119 #define DEVICE_LONGTEXT N_("Select your audio output device")
121 #define SPEAKER_TEXT N_("Speaker configuration")
122 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
123 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
126 set_description( N_("DirectX audio output") )
127 set_shortname( "DirectX" )
128 set_capability( "audio output", 100 )
129 set_category( CAT_AUDIO )
130 set_subcategory( SUBCAT_AUDIO_AOUT )
131 add_shortcut( "directx", "directsound" )
133 add_string( "directx-audio-device-name", "default",
134 DEVICE_TEXT, DEVICE_LONGTEXT, false )
135 add_deprecated_alias( "directx-audio-device" ) /* Since 1.1.0 */
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->i_nb_samples = A52_FRAME_NB;
228 p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
229 p_aout->format.i_frame_length = A52_FRAME_NB;
230 p_aout->sys->i_frame_size = p_aout->format.i_bytes_per_frame;
232 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
233 p_aout->format.i_physical_channels,
234 aout_FormatNbChannels( &p_aout->format ),
235 p_aout->format.i_rate,
236 p_aout->sys->i_frame_size, false )
239 msg_Err( p_aout, "cannot open directx audio device" );
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 p_aout->i_nb_samples = FRAME_SIZE;
298 aout_FormatPrepare( &p_aout->format );
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_NON_LINEAR( &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 );
619 /*****************************************************************************
620 * CallBackDirectSoundEnum: callback to enumerate available devices
621 *****************************************************************************/
622 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
623 LPCWSTR psz_mod, LPVOID _p_aout )
625 VLC_UNUSED( psz_mod );
627 audio_output_t *p_aout = (audio_output_t *)_p_aout;
629 char *psz_device = FromWide( psz_desc );
630 msg_Dbg( p_aout, "found device: %s", psz_device );
632 if( p_aout->sys->psz_device &&
633 !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
635 /* Use the device corresponding to psz_device */
636 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
637 *p_aout->sys->p_device_guid = *p_guid;
638 msg_Dbg( p_aout, "using device: %s", psz_device );
642 /* If no default device has been selected, chose the first one */
643 if( !p_aout->sys->psz_device && p_guid )
645 p_aout->sys->psz_device = strdup( psz_device );
646 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
647 *p_aout->sys->p_device_guid = *p_guid;
648 msg_Dbg( p_aout, "using device: %s", psz_device );
656 /*****************************************************************************
657 * InitDirectSound: handle all the gory details of DirectSound initialisation
658 *****************************************************************************/
659 static int InitDirectSound( audio_output_t *p_aout )
661 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
662 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
664 p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
665 if( p_aout->sys->hdsound_dll == NULL )
667 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
671 OurDirectSoundCreate = (void *)
672 GetProcAddress( p_aout->sys->hdsound_dll,
673 "DirectSoundCreate" );
674 if( OurDirectSoundCreate == NULL )
676 msg_Warn( p_aout, "GetProcAddress FAILED" );
680 /* Get DirectSoundEnumerate */
681 OurDirectSoundEnumerate = (void *)
682 GetProcAddress( p_aout->sys->hdsound_dll,
683 "DirectSoundEnumerateW" );
684 if( OurDirectSoundEnumerate )
686 p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device-name");
687 /* Attempt enumeration */
688 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
691 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
695 /* Create the direct sound object */
696 if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
697 &p_aout->sys->p_dsobject,
700 msg_Warn( p_aout, "cannot create a direct sound device" );
704 /* Set DirectSound Cooperative level, ie what control we want over Windows
705 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
706 * settings of the primary buffer, but also that only the sound of our
707 * application will be hearable when it will have the focus.
708 * !!! (this is not really working as intended yet because to set the
709 * cooperative level you need the window handle of your application, and
710 * I don't know of any easy way to get it. Especially since we might play
711 * sound without any video, and so what window handle should we use ???
712 * The hack for now is to use the Desktop window handle - it seems to be
714 if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
718 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
724 p_aout->sys->p_dsobject = NULL;
725 if( p_aout->sys->hdsound_dll )
727 FreeLibrary( p_aout->sys->hdsound_dll );
728 p_aout->sys->hdsound_dll = NULL;
734 /*****************************************************************************
735 * CreateDSBuffer: Creates a direct sound buffer of the required format.
736 *****************************************************************************
737 * This function creates the buffer we'll use to play audio.
738 * In DirectSound there are two kinds of buffers:
739 * - the primary buffer: which is the actual buffer that the soundcard plays
740 * - the secondary buffer(s): these buffers are the one actually used by
741 * applications and DirectSound takes care of mixing them into the primary.
743 * Once you create a secondary buffer, you cannot change its format anymore so
744 * you have to release the current one and create another.
745 *****************************************************************************/
746 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
747 int i_channels, int i_nb_channels, int i_rate,
748 int i_bytes_per_frame, bool b_probe )
750 WAVEFORMATEXTENSIBLE waveformat;
751 DSBUFFERDESC dsbdesc;
754 /* First set the sound buffer format */
755 waveformat.dwChannelMask = 0;
756 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
758 if( i_channels & pi_channels_src[i] )
759 waveformat.dwChannelMask |= pi_channels_in[i];
764 case VLC_CODEC_SPDIFL:
766 /* To prevent channel re-ordering */
767 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
768 waveformat.Format.wBitsPerSample = 16;
769 waveformat.Samples.wValidBitsPerSample =
770 waveformat.Format.wBitsPerSample;
771 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
772 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
776 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
777 waveformat.Samples.wValidBitsPerSample =
778 waveformat.Format.wBitsPerSample;
779 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
780 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
784 waveformat.Format.wBitsPerSample = 16;
785 waveformat.Samples.wValidBitsPerSample =
786 waveformat.Format.wBitsPerSample;
787 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
788 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
792 waveformat.Format.nChannels = i_nb_channels;
793 waveformat.Format.nSamplesPerSec = i_rate;
794 waveformat.Format.nBlockAlign =
795 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
796 waveformat.Format.nAvgBytesPerSec =
797 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
799 p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
800 p_aout->sys->i_channels = i_nb_channels;
802 /* Then fill in the direct sound descriptor */
803 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
804 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
805 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
806 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
808 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
809 if( i_nb_channels <= 2 )
811 waveformat.Format.cbSize = 0;
815 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
816 waveformat.Format.cbSize =
817 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
819 /* Needed for 5.1 on emu101k */
820 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
823 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
824 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
826 if FAILED( IDirectSound_CreateSoundBuffer(
827 p_aout->sys->p_dsobject, &dsbdesc,
828 &p_aout->sys->p_dsbuffer, NULL) )
830 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
832 /* Try without DSBCAPS_LOCHARDWARE */
833 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
834 if FAILED( IDirectSound_CreateSoundBuffer(
835 p_aout->sys->p_dsobject, &dsbdesc,
836 &p_aout->sys->p_dsbuffer, NULL) )
841 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
849 /* Stop here if we were just probing */
852 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
853 p_aout->sys->p_dsbuffer = NULL;
857 p_aout->sys->i_frame_size = i_bytes_per_frame;
858 p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
859 p_aout->sys->b_chan_reorder =
860 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
861 waveformat.dwChannelMask, i_nb_channels,
862 p_aout->sys->pi_chan_table );
864 if( p_aout->sys->b_chan_reorder )
866 msg_Dbg( p_aout, "channel reordering needed" );
872 /*****************************************************************************
873 * CreateDSBufferPCM: creates a PCM direct sound buffer.
874 *****************************************************************************
875 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
876 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
877 ****************************************************************************/
878 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
879 int i_channels, int i_nb_channels, int i_rate,
882 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
883 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
885 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
886 i_channels, i_nb_channels, i_rate,
887 FRAME_SIZE * 4 * i_nb_channels, b_probe )
890 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
891 i_channels, i_nb_channels, i_rate,
892 FRAME_SIZE * 2 * i_nb_channels, b_probe )
899 *i_format = VLC_CODEC_S16L;
905 *i_format = VLC_CODEC_FL32;
910 /*****************************************************************************
912 *****************************************************************************
913 * This function destroys the secondary buffer.
914 *****************************************************************************/
915 static void DestroyDSBuffer( audio_output_t *p_aout )
917 if( p_aout->sys->p_dsbuffer )
919 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
920 p_aout->sys->p_dsbuffer = NULL;
924 /*****************************************************************************
925 * FillBuffer: Fill in one of the direct sound frame buffers.
926 *****************************************************************************
927 * Returns VLC_SUCCESS on success.
928 *****************************************************************************/
929 static int FillBuffer( audio_output_t *p_aout, int i_frame,
930 aout_buffer_t *p_buffer )
932 notification_thread_t *p_notif = p_aout->sys->p_notif;
933 aout_sys_t *p_sys = p_aout->sys;
934 void *p_write_position, *p_wrap_around;
935 unsigned long l_bytes1, l_bytes2;
938 /* Before copying anything, we have to lock the buffer */
939 dsresult = IDirectSoundBuffer_Lock(
940 p_sys->p_dsbuffer, /* DS buffer */
941 i_frame * p_notif->i_frame_size, /* Start offset */
942 p_notif->i_frame_size, /* Number of bytes */
943 &p_write_position, /* Address of lock start */
944 &l_bytes1, /* Count of bytes locked before wrap around */
945 &p_wrap_around, /* Buffer address (if wrap around) */
946 &l_bytes2, /* Count of bytes after wrap around */
948 if( dsresult == DSERR_BUFFERLOST )
950 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
951 dsresult = IDirectSoundBuffer_Lock(
953 i_frame * p_notif->i_frame_size,
954 p_notif->i_frame_size,
961 if( dsresult != DS_OK )
963 msg_Warn( p_aout, "cannot lock buffer" );
964 if( p_buffer ) aout_BufferFree( p_buffer );
968 if( p_buffer == NULL )
970 memset( p_write_position, 0, l_bytes1 );
974 if( p_sys->b_chan_reorder )
976 /* Do the channel reordering here */
977 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
978 p_sys->i_channels, p_sys->pi_chan_table,
979 p_sys->i_bits_per_sample );
982 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
983 aout_BufferFree( p_buffer );
986 /* Now the data has been copied, unlock the buffer */
987 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
988 p_wrap_around, l_bytes2 );
990 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
994 /*****************************************************************************
995 * DirectSoundThread: this thread will capture play notification events.
996 *****************************************************************************
997 * We use this thread to emulate a callback mechanism. The thread probes for
998 * event notification and fills up the DS secondary buffer when needed.
999 *****************************************************************************/
1000 static void* DirectSoundThread( void *data )
1002 notification_thread_t *p_notif = (notification_thread_t*)data;
1003 audio_output_t *p_aout = p_notif->p_aout;
1005 int canc = vlc_savecancel ();
1007 /* We don't want any resampling when using S/PDIF output */
1008 bool b_sleek = (p_aout->format.i_format == VLC_CODEC_SPDIFL);
1010 msg_Dbg( p_aout, "DirectSoundThread ready" );
1012 /* Wait here until Play() is called */
1013 WaitForSingleObject( p_notif->event, INFINITE );
1015 if( !vlc_atomic_get( &p_notif->abort) )
1018 mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1020 /* start playing the buffer */
1021 dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1024 DSBPLAY_LOOPING ); /* Flags */
1025 if( dsresult == DSERR_BUFFERLOST )
1027 IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1028 dsresult = IDirectSoundBuffer_Play(
1029 p_aout->sys->p_dsbuffer,
1032 DSBPLAY_LOOPING ); /* Flags */
1034 if( dsresult != DS_OK )
1036 msg_Err( p_aout, "cannot start playing buffer" );
1039 last_time = mdate();
1041 while( !vlc_atomic_get( &p_notif->abort ) )
1044 int l_queued = 0, l_free_slots;
1045 unsigned i_frame_siz = p_aout->i_nb_samples;
1046 mtime_t mtime = mdate();
1050 * Fill in as much audio data as we can in our circular buffer
1053 /* Find out current play position */
1054 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1055 p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1057 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1061 /* Detect underruns */
1062 if( l_queued && mtime - last_time >
1063 INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1065 msg_Dbg( p_aout, "detected underrun!" );
1069 /* Try to fill in as many frame buffers as possible */
1070 l_read /= (p_aout->format.i_bytes_per_frame /
1071 p_aout->format.i_frame_length);
1072 l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1073 if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1074 l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1076 for( i = 0; i < l_free_slots; i++ )
1078 aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout,
1079 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1080 p_aout->format.i_rate, b_sleek );
1082 /* If there is no audio data available and we have some buffered
1083 * already, then just wait for the next time */
1084 if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1086 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1087 p_buffer ) != VLC_SUCCESS ) break;
1090 /* Sleep a reasonable amount of time */
1091 l_queued += (i * i_frame_siz);
1092 msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1095 /* make sure the buffer isn't playing */
1096 IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1098 /* free the event */
1099 CloseHandle( p_notif->event );
1101 vlc_restorecancel (canc);
1102 msg_Dbg( p_aout, "DirectSoundThread exiting" );
1106 /*****************************************************************************
1107 * CallBackConfigNBEnum: callback to get the number of available devices
1108 *****************************************************************************/
1109 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1110 LPCWSTR psz_mod, LPVOID p_nb )
1112 VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1114 int * a = (int *)p_nb;
1119 /*****************************************************************************
1120 * CallBackConfigEnum: callback to add available devices to the preferences list
1121 *****************************************************************************/
1122 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1123 LPCWSTR psz_mod, LPVOID _p_item )
1125 VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1127 module_config_t *p_item = (module_config_t *) _p_item;
1129 p_item->ppsz_list[p_item->i_list] = FromWide( psz_desc );
1130 p_item->ppsz_list_text[p_item->i_list] = FromWide( psz_desc );
1135 /*****************************************************************************
1136 * ReloadDirectXDevices: store the list of devices in preferences
1137 *****************************************************************************/
1138 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1139 vlc_value_t newval, vlc_value_t oldval, void *data )
1141 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1143 module_config_t *p_item = config_FindConfig( p_this, psz_name );
1144 if( !p_item ) return VLC_SUCCESS;
1146 /* Clear-up the current list */
1147 if( p_item->i_list )
1149 for( int i = 0; i < p_item->i_list; i++ )
1151 free((char *)(p_item->ppsz_list[i]) );
1152 free((char *)(p_item->ppsz_list_text[i]) );
1156 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
1158 HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1159 if( hdsound_dll == NULL )
1161 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1165 /* Get DirectSoundEnumerate */
1166 OurDirectSoundEnumerate = (void *)
1167 GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1169 if( OurDirectSoundEnumerate == NULL )
1173 OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1174 msg_Dbg(p_this,"found %d devices", nb_devices);
1176 p_item->ppsz_list = xrealloc( p_item->ppsz_list,
1177 nb_devices * sizeof(char *) );
1178 p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
1179 nb_devices * sizeof(char *) );
1182 OurDirectSoundEnumerate(CallBackConfigEnum, p_item);
1184 /* Signal change to the interface */
1185 p_item->b_dirty = true;
1188 FreeLibrary(hdsound_dll);