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 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
37 #include <vlc_charset.h>
38 #include <vlc_atomic.h>
40 #include "windows_audio_common.h"
42 #define FRAME_SIZE ((int)p_aout->format.i_rate/20) /* Size in samples */
44 /*****************************************************************************
45 * notification_thread_t: DirectX event thread
46 *****************************************************************************/
47 typedef struct notification_thread_t
49 int i_frame_size; /* size in bytes of one frame */
50 int i_write_slot; /* current write position in our circular buffer */
58 } notification_thread_t;
60 /*****************************************************************************
61 * aout_sys_t: directx audio output method descriptor
62 *****************************************************************************
63 * This structure is part of the audio output thread descriptor.
64 * It describes the direct sound specific properties of an audio device.
65 *****************************************************************************/
69 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
71 char * psz_device; /* user defined device name */
74 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
75 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
76 * takes care of mixing all the
77 * secondary buffers into the primary) */
80 notification_thread_t notif; /* DirectSoundThread id */
82 int i_frame_size; /* Size in bytes of one frame */
84 int i_speaker_setup; /* Speaker setup override */
86 bool b_chan_reorder; /* do we need channel reordering */
87 int pi_chan_table[AOUT_CHAN_MAX];
88 uint32_t i_channel_mask;
89 uint32_t i_bits_per_sample;
93 /*****************************************************************************
95 *****************************************************************************/
96 static int OpenAudio ( vlc_object_t * );
97 static void CloseAudio ( vlc_object_t * );
98 static void Play ( audio_output_t *, block_t * );
99 static int VolumeSet ( audio_output_t *, float, bool );
101 /* local functions */
102 static void Probe ( audio_output_t * );
103 static int InitDirectSound ( audio_output_t * );
104 static int CreateDSBuffer ( audio_output_t *, int, int, int, int, int, bool );
105 static int CreateDSBufferPCM ( audio_output_t *, vlc_fourcc_t*, int, int, int, bool );
106 static void DestroyDSBuffer ( audio_output_t * );
107 static void* DirectSoundThread( void * );
108 static int FillBuffer ( audio_output_t *, int, aout_buffer_t * );
110 static int ReloadDirectXDevices( vlc_object_t *, char const *,
111 vlc_value_t, vlc_value_t, void * );
113 /* Speaker setup override options list */
114 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
115 "Quad", "5.1", "7.1" };
116 static const char *const ppsz_adev[] = {"default", };
117 static const char *const ppsz_adev_text[] = {"default", };
119 /*****************************************************************************
121 *****************************************************************************/
122 #define DEVICE_TEXT N_("Output device")
123 #define DEVICE_LONGTEXT N_("Select your audio output device")
125 #define SPEAKER_TEXT N_("Speaker configuration")
126 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
127 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
130 set_description( N_("DirectX audio output") )
131 set_shortname( "DirectX" )
132 set_capability( "audio output", 100 )
133 set_category( CAT_AUDIO )
134 set_subcategory( SUBCAT_AUDIO_AOUT )
135 add_shortcut( "directx", "directsound" )
137 add_string( "directx-audio-device", "default",
138 DEVICE_TEXT, DEVICE_LONGTEXT, false )
139 change_string_list( ppsz_adev, ppsz_adev_text, ReloadDirectXDevices )
140 change_action_add( ReloadDirectXDevices, N_("Refresh list") )
141 add_obsolete_string( "directx-audio-device-name")
142 add_bool( "directx-audio-float32", false, FLOAT_TEXT,
143 FLOAT_LONGTEXT, true )
144 add_string( "directx-audio-speaker", "Windows default",
145 SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
146 change_string_list( speaker_list, 0, 0 )
148 set_callbacks( OpenAudio, CloseAudio )
151 /*****************************************************************************
152 * OpenAudio: open the audio device
153 *****************************************************************************
154 * This function opens and setups Direct Sound.
155 *****************************************************************************/
156 static int OpenAudio( vlc_object_t *p_this )
158 audio_output_t * p_aout = (audio_output_t *)p_this;
163 const char * const * ppsz_compare = speaker_list;
165 msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
167 /* Allocate structure */
168 p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
169 if( unlikely( p_aout->sys == NULL ) )
172 /* Retrieve config values */
173 var_Create( p_aout, "directx-audio-float32",
174 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
175 psz_speaker = var_CreateGetString( p_aout, "directx-audio-speaker" );
177 while ( *ppsz_compare != NULL )
179 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
186 if ( *ppsz_compare == NULL )
188 msg_Err( p_aout, "(%s) isn't valid speaker setup option", psz_speaker );
189 msg_Err( p_aout, "Defaulting to Windows default speaker config");
193 p_aout->sys->i_speaker_setup = i;
195 /* Initialise DirectSound */
196 if( InitDirectSound( p_aout ) )
198 msg_Err( p_aout, "cannot initialize DirectSound" );
202 if( var_Type( p_aout, "audio-device" ) == 0 )
207 if( var_Get( p_aout, "audio-device", &val ) < 0 )
209 msg_Err( p_aout, "DirectSound Probe failed()" );
213 /* Open the device */
214 if( val.i_int == AOUT_VAR_SPDIF )
216 p_aout->format.i_format = VLC_CODEC_SPDIFL;
218 /* Calculate the frame size in bytes */
219 p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
220 p_aout->format.i_frame_length = A52_FRAME_NB;
221 p_aout->sys->i_frame_size = p_aout->format.i_bytes_per_frame;
223 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
224 p_aout->format.i_physical_channels,
225 aout_FormatNbChannels( &p_aout->format ),
226 p_aout->format.i_rate,
227 p_aout->sys->i_frame_size, false )
230 msg_Err( p_aout, "cannot open directx audio device" );
234 aout_PacketInit( p_aout, &p_aout->sys->packet, A52_FRAME_NB );
235 p_aout->sys->volume = -1;
236 aout_VolumeNoneInit( p_aout );
240 if( val.i_int == AOUT_VAR_5_1 )
242 p_aout->format.i_physical_channels
243 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
244 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
247 else if( val.i_int == AOUT_VAR_7_1 )
249 p_aout->format.i_physical_channels
250 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
251 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
252 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
255 else if( val.i_int == AOUT_VAR_3F2R )
257 p_aout->format.i_physical_channels
258 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
259 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
261 else if( val.i_int == AOUT_VAR_2F2R )
263 p_aout->format.i_physical_channels
264 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
265 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
267 else if( val.i_int == AOUT_VAR_MONO )
269 p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
273 p_aout->format.i_physical_channels
274 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
277 if( CreateDSBufferPCM( p_aout, &p_aout->format.i_format,
278 p_aout->format.i_physical_channels,
279 aout_FormatNbChannels( &p_aout->format ),
280 p_aout->format.i_rate, false )
283 msg_Err( p_aout, "cannot open directx audio device" );
287 /* Calculate the frame size in bytes */
288 aout_FormatPrepare( &p_aout->format );
289 aout_PacketInit( p_aout, &p_aout->sys->packet, FRAME_SIZE );
290 aout_VolumeHardInit( p_aout, VolumeSet, true );
293 /* Now we need to setup our DirectSound play notification structure */
294 vlc_atomic_set(&p_aout->sys->notif.abort, 0);
295 p_aout->sys->notif.event = CreateEvent( 0, FALSE, FALSE, 0 );
296 if( unlikely(p_aout->sys->notif.event == NULL) )
298 p_aout->sys->notif.i_frame_size = p_aout->sys->i_frame_size;
300 /* then launch the notification thread */
301 msg_Dbg( p_aout, "creating DirectSoundThread" );
302 if( vlc_clone( &p_aout->sys->notif.thread, DirectSoundThread, p_aout,
303 VLC_THREAD_PRIORITY_HIGHEST ) )
305 msg_Err( p_aout, "cannot create DirectSoundThread" );
306 CloseHandle( p_aout->sys->notif.event );
307 p_aout->sys->notif.event = NULL;
308 aout_PacketDestroy( p_aout );
312 p_aout->pf_play = Play;
313 p_aout->pf_pause = aout_PacketPause;
314 p_aout->pf_flush = aout_PacketFlush;
319 CloseAudio( VLC_OBJECT(p_aout) );
323 /*****************************************************************************
324 * Probe: probe the audio device for available formats and channels
325 *****************************************************************************/
326 static void Probe( audio_output_t * p_aout )
328 vlc_value_t val, text;
329 vlc_fourcc_t i_format;
330 unsigned int i_physical_channels;
331 DWORD ui_speaker_config;
332 bool is_default_output_set = false;
334 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
335 text.psz_string = _("Audio Device");
336 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
338 /* Test for 5.1 support */
339 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
340 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
341 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
342 if( p_aout->format.i_physical_channels == i_physical_channels )
344 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
345 p_aout->format.i_rate, true )
348 val.i_int = AOUT_VAR_5_1;
349 text.psz_string = (char*) "5.1";
350 var_Change( p_aout, "audio-device",
351 VLC_VAR_ADDCHOICE, &val, &text );
352 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
353 is_default_output_set = true;
354 msg_Dbg( p_aout, "device supports 5.1 channels" );
358 /* Test for 7.1 support */
359 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
360 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
361 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
362 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
363 if( p_aout->format.i_physical_channels == i_physical_channels )
365 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
366 p_aout->format.i_rate, true )
369 val.i_int = AOUT_VAR_7_1;
370 text.psz_string = (char*) "7.1";
371 var_Change( p_aout, "audio-device",
372 VLC_VAR_ADDCHOICE, &val, &text );
373 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
374 is_default_output_set = true;
375 msg_Dbg( p_aout, "device supports 7.1 channels" );
379 /* Test for 3 Front 2 Rear support */
380 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
381 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
383 if( p_aout->format.i_physical_channels == i_physical_channels )
385 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
386 p_aout->format.i_rate, true )
389 val.i_int = AOUT_VAR_3F2R;
390 text.psz_string = _("3 Front 2 Rear");
391 var_Change( p_aout, "audio-device",
392 VLC_VAR_ADDCHOICE, &val, &text );
393 if(!is_default_output_set)
395 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
396 is_default_output_set = true;
398 msg_Dbg( p_aout, "device supports 5 channels" );
402 /* Test for 2 Front 2 Rear support */
403 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
404 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
405 if( ( p_aout->format.i_physical_channels & i_physical_channels )
406 == i_physical_channels )
408 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
409 p_aout->format.i_rate, true )
412 val.i_int = AOUT_VAR_2F2R;
413 text.psz_string = _("2 Front 2 Rear");
414 var_Change( p_aout, "audio-device",
415 VLC_VAR_ADDCHOICE, &val, &text );
416 if(!is_default_output_set)
418 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
419 is_default_output_set = true;
421 msg_Dbg( p_aout, "device supports 4 channels" );
425 /* Test for stereo support */
426 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
427 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
428 p_aout->format.i_rate, true )
431 val.i_int = AOUT_VAR_STEREO;
432 text.psz_string = _("Stereo");
433 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
434 if(!is_default_output_set)
436 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
437 is_default_output_set = true;
438 msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
440 else msg_Dbg( p_aout, "device supports 2 channels" );
443 /* Test for mono support */
444 i_physical_channels = AOUT_CHAN_CENTER;
445 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
446 p_aout->format.i_rate, true )
449 val.i_int = AOUT_VAR_MONO;
450 text.psz_string = _("Mono");
451 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
452 msg_Dbg( p_aout, "device supports 1 channel" );
455 /* Check the speaker configuration to determine which channel config should
457 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->sys->p_dsobject,
458 &ui_speaker_config ) )
460 ui_speaker_config = DSSPEAKER_STEREO;
461 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
463 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
465 case DSSPEAKER_7POINT1:
466 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
467 val.i_int = AOUT_VAR_7_1;
469 case DSSPEAKER_5POINT1:
470 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
471 val.i_int = AOUT_VAR_5_1;
474 msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
475 val.i_int = AOUT_VAR_2F2R;
477 #if 0 /* Lots of people just get their settings wrong and complain that
478 * this is a problem with VLC so just don't ever set mono by default. */
480 val.i_int = AOUT_VAR_MONO;
483 case DSSPEAKER_SURROUND:
484 msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
485 case DSSPEAKER_STEREO:
486 msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
488 /* If nothing else is found, choose stereo output */
489 val.i_int = AOUT_VAR_STEREO;
493 /* Check if we want to override speaker config */
494 switch( p_aout->sys->i_speaker_setup )
496 case 0: /* Default value aka Windows default speaker setup */
499 msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
500 val.i_int = AOUT_VAR_MONO;
503 msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
504 val.i_int = AOUT_VAR_STEREO;
507 msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
508 val.i_int = AOUT_VAR_2F2R;
511 msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
512 val.i_int = AOUT_VAR_5_1;
515 msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
516 val.i_int = AOUT_VAR_7_1;
519 msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
523 var_Set( p_aout, "audio-device", val );
525 /* Test for SPDIF support */
526 if ( AOUT_FMT_SPDIF( &p_aout->format ) )
528 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
529 p_aout->format.i_physical_channels,
530 aout_FormatNbChannels( &p_aout->format ),
531 p_aout->format.i_rate,
532 AOUT_SPDIF_SIZE, true )
535 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
536 val.i_int = AOUT_VAR_SPDIF;
537 text.psz_string = _("A/52 over S/PDIF");
538 var_Change( p_aout, "audio-device",
539 VLC_VAR_ADDCHOICE, &val, &text );
540 if( var_InheritBool( p_aout, "spdif" ) )
541 var_Set( p_aout, "audio-device", val );
545 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
548 /* Probe() has failed. */
549 var_Destroy( p_aout, "audio-device" );
553 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
554 var_TriggerCallback( p_aout, "intf-change" );
557 /*****************************************************************************
558 * Play: we'll start playing the directsound buffer here because at least here
559 * we know the first buffer has been put in the aout fifo and we also
561 *****************************************************************************/
562 static void Play( audio_output_t *p_aout, block_t *p_buffer )
564 /* get the playing date of the first aout buffer */
565 p_aout->sys->notif.start_date = p_buffer->i_pts;
567 /* fill in the first samples (zeroes) */
568 FillBuffer( p_aout, 0, NULL );
570 /* wake up the audio output thread */
571 SetEvent( p_aout->sys->notif.event );
573 aout_PacketPlay( p_aout, p_buffer );
574 p_aout->pf_play = aout_PacketPlay;
577 /*****************************************************************************
578 * VolumeSet: change audio device volume
579 *****************************************************************************/
580 static int VolumeSet( audio_output_t *p_aout, float vol, bool mute )
582 aout_sys_t *sys = p_aout->sys;
588 /* Convert UI volume percentage to linear factor (cube) */
591 /* "DirectSound does not support amplification." -- MSDN */
595 /* millibels from linear amplification */
596 if( f <= powf(10., DSBVOLUME_MIN / -2000.) )
597 volume = DSBVOLUME_MIN;
599 volume = lroundf(-2000. * log10f(f));
601 InterlockedExchange(&sys->volume, volume);
602 aout_VolumeHardSet( p_aout, vol, mute );
606 /*****************************************************************************
607 * CloseAudio: close the audio device
608 *****************************************************************************/
609 static void CloseAudio( vlc_object_t *p_this )
611 audio_output_t * p_aout = (audio_output_t *)p_this;
612 aout_sys_t *p_sys = p_aout->sys;
614 msg_Dbg( p_aout, "closing audio device" );
616 /* kill the position notification thread, if any */
617 if( p_sys->notif.event != NULL )
619 vlc_atomic_set(&p_aout->sys->notif.abort, 1);
620 /* wake up the audio thread if needed */
621 if( p_aout->pf_play == Play )
622 SetEvent( p_sys->notif.event );
624 vlc_join( p_sys->notif.thread, NULL );
625 CloseHandle( p_sys->notif.event );
626 aout_PacketDestroy( p_aout );
629 /* release the secondary buffer */
630 DestroyDSBuffer( p_aout );
632 /* finally release the DirectSound object */
633 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
635 /* free DSOUND.DLL */
636 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
638 free( p_aout->sys->p_device_guid );
642 /*****************************************************************************
643 * CallBackDirectSoundEnum: callback to enumerate available devices
644 *****************************************************************************/
645 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
646 LPCWSTR psz_mod, LPVOID _p_aout )
648 VLC_UNUSED( psz_mod );
650 audio_output_t *p_aout = (audio_output_t *)_p_aout;
652 char *psz_device = FromWide( psz_desc );
653 msg_Dbg( p_aout, "found device: %s", psz_device );
655 if( p_aout->sys->psz_device &&
656 !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
658 /* Use the device corresponding to psz_device */
659 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
660 *p_aout->sys->p_device_guid = *p_guid;
661 msg_Dbg( p_aout, "using device: %s", psz_device );
665 /* If no default device has been selected, chose the first one */
666 if( !p_aout->sys->psz_device && p_guid )
668 p_aout->sys->psz_device = strdup( psz_device );
669 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
670 *p_aout->sys->p_device_guid = *p_guid;
671 msg_Dbg( p_aout, "using device: %s", psz_device );
679 /*****************************************************************************
680 * InitDirectSound: handle all the gory details of DirectSound initialisation
681 *****************************************************************************/
682 static int InitDirectSound( audio_output_t *p_aout )
684 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
685 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
687 p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
688 if( p_aout->sys->hdsound_dll == NULL )
690 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
694 OurDirectSoundCreate = (void *)
695 GetProcAddress( p_aout->sys->hdsound_dll,
696 "DirectSoundCreate" );
697 if( OurDirectSoundCreate == NULL )
699 msg_Warn( p_aout, "GetProcAddress FAILED" );
703 /* Get DirectSoundEnumerate */
704 OurDirectSoundEnumerate = (void *)
705 GetProcAddress( p_aout->sys->hdsound_dll,
706 "DirectSoundEnumerateW" );
707 if( OurDirectSoundEnumerate )
709 p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device");
710 /* Attempt enumeration */
711 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
714 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
718 /* Create the direct sound object */
719 if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
720 &p_aout->sys->p_dsobject,
723 msg_Warn( p_aout, "cannot create a direct sound device" );
727 /* Set DirectSound Cooperative level, ie what control we want over Windows
728 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
729 * settings of the primary buffer, but also that only the sound of our
730 * application will be hearable when it will have the focus.
731 * !!! (this is not really working as intended yet because to set the
732 * cooperative level you need the window handle of your application, and
733 * I don't know of any easy way to get it. Especially since we might play
734 * sound without any video, and so what window handle should we use ???
735 * The hack for now is to use the Desktop window handle - it seems to be
737 if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
741 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
747 p_aout->sys->p_dsobject = NULL;
748 if( p_aout->sys->hdsound_dll )
750 FreeLibrary( p_aout->sys->hdsound_dll );
751 p_aout->sys->hdsound_dll = NULL;
757 /*****************************************************************************
758 * CreateDSBuffer: Creates a direct sound buffer of the required format.
759 *****************************************************************************
760 * This function creates the buffer we'll use to play audio.
761 * In DirectSound there are two kinds of buffers:
762 * - the primary buffer: which is the actual buffer that the soundcard plays
763 * - the secondary buffer(s): these buffers are the one actually used by
764 * applications and DirectSound takes care of mixing them into the primary.
766 * Once you create a secondary buffer, you cannot change its format anymore so
767 * you have to release the current one and create another.
768 *****************************************************************************/
769 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
770 int i_channels, int i_nb_channels, int i_rate,
771 int i_bytes_per_frame, bool b_probe )
773 WAVEFORMATEXTENSIBLE waveformat;
774 DSBUFFERDESC dsbdesc;
777 /* First set the sound buffer format */
778 waveformat.dwChannelMask = 0;
779 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
781 if( i_channels & pi_channels_src[i] )
782 waveformat.dwChannelMask |= pi_channels_in[i];
787 case VLC_CODEC_SPDIFL:
789 /* To prevent channel re-ordering */
790 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
791 waveformat.Format.wBitsPerSample = 16;
792 waveformat.Samples.wValidBitsPerSample =
793 waveformat.Format.wBitsPerSample;
794 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
795 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
799 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
800 waveformat.Samples.wValidBitsPerSample =
801 waveformat.Format.wBitsPerSample;
802 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
803 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
807 waveformat.Format.wBitsPerSample = 16;
808 waveformat.Samples.wValidBitsPerSample =
809 waveformat.Format.wBitsPerSample;
810 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
811 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
815 waveformat.Format.nChannels = i_nb_channels;
816 waveformat.Format.nSamplesPerSec = i_rate;
817 waveformat.Format.nBlockAlign =
818 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
819 waveformat.Format.nAvgBytesPerSec =
820 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
822 p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
823 p_aout->sys->i_channels = i_nb_channels;
825 /* Then fill in the direct sound descriptor */
826 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
827 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
828 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
829 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
831 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
832 if( i_nb_channels <= 2 )
834 waveformat.Format.cbSize = 0;
838 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
839 waveformat.Format.cbSize =
840 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
842 /* Needed for 5.1 on emu101k */
843 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
846 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
847 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
849 if FAILED( IDirectSound_CreateSoundBuffer(
850 p_aout->sys->p_dsobject, &dsbdesc,
851 &p_aout->sys->p_dsbuffer, NULL) )
853 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
855 /* Try without DSBCAPS_LOCHARDWARE */
856 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
857 if FAILED( IDirectSound_CreateSoundBuffer(
858 p_aout->sys->p_dsobject, &dsbdesc,
859 &p_aout->sys->p_dsbuffer, NULL) )
864 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
872 /* Stop here if we were just probing */
875 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
876 p_aout->sys->p_dsbuffer = NULL;
880 p_aout->sys->i_frame_size = i_bytes_per_frame;
881 p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
882 p_aout->sys->b_chan_reorder =
883 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
884 waveformat.dwChannelMask, i_nb_channels,
885 p_aout->sys->pi_chan_table );
887 if( p_aout->sys->b_chan_reorder )
889 msg_Dbg( p_aout, "channel reordering needed" );
895 /*****************************************************************************
896 * CreateDSBufferPCM: creates a PCM direct sound buffer.
897 *****************************************************************************
898 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
899 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
900 ****************************************************************************/
901 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
902 int i_channels, int i_nb_channels, int i_rate,
905 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
906 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
908 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
909 i_channels, i_nb_channels, i_rate,
910 FRAME_SIZE * 4 * i_nb_channels, b_probe )
913 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
914 i_channels, i_nb_channels, i_rate,
915 FRAME_SIZE * 2 * i_nb_channels, b_probe )
922 *i_format = VLC_CODEC_S16L;
928 *i_format = VLC_CODEC_FL32;
933 /*****************************************************************************
935 *****************************************************************************
936 * This function destroys the secondary buffer.
937 *****************************************************************************/
938 static void DestroyDSBuffer( audio_output_t *p_aout )
940 if( p_aout->sys->p_dsbuffer )
942 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
943 p_aout->sys->p_dsbuffer = NULL;
947 /*****************************************************************************
948 * FillBuffer: Fill in one of the direct sound frame buffers.
949 *****************************************************************************
950 * Returns VLC_SUCCESS on success.
951 *****************************************************************************/
952 static int FillBuffer( audio_output_t *p_aout, int i_frame,
953 aout_buffer_t *p_buffer )
955 aout_sys_t *p_sys = p_aout->sys;
956 notification_thread_t *p_notif = &p_sys->notif;
957 void *p_write_position, *p_wrap_around;
958 unsigned long l_bytes1, l_bytes2;
961 /* Before copying anything, we have to lock the buffer */
962 dsresult = IDirectSoundBuffer_Lock(
963 p_sys->p_dsbuffer, /* DS buffer */
964 i_frame * p_notif->i_frame_size, /* Start offset */
965 p_notif->i_frame_size, /* Number of bytes */
966 &p_write_position, /* Address of lock start */
967 &l_bytes1, /* Count of bytes locked before wrap around */
968 &p_wrap_around, /* Buffer address (if wrap around) */
969 &l_bytes2, /* Count of bytes after wrap around */
971 if( dsresult == DSERR_BUFFERLOST )
973 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
974 dsresult = IDirectSoundBuffer_Lock(
976 i_frame * p_notif->i_frame_size,
977 p_notif->i_frame_size,
984 if( dsresult != DS_OK )
986 msg_Warn( p_aout, "cannot lock buffer" );
987 if( p_buffer ) aout_BufferFree( p_buffer );
991 if( p_buffer == NULL )
993 memset( p_write_position, 0, l_bytes1 );
997 if( p_sys->b_chan_reorder )
999 /* Do the channel reordering here */
1000 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
1001 p_sys->i_channels, p_sys->pi_chan_table,
1002 p_sys->i_bits_per_sample );
1005 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
1006 aout_BufferFree( p_buffer );
1009 /* Now the data has been copied, unlock the buffer */
1010 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1011 p_wrap_around, l_bytes2 );
1013 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1017 /*****************************************************************************
1018 * DirectSoundThread: this thread will capture play notification events.
1019 *****************************************************************************
1020 * We use this thread to emulate a callback mechanism. The thread probes for
1021 * event notification and fills up the DS secondary buffer when needed.
1022 *****************************************************************************/
1023 static void* DirectSoundThread( void *data )
1025 audio_output_t *p_aout = (audio_output_t *)data;
1026 notification_thread_t *p_notif = &p_aout->sys->notif;
1029 msg_Dbg( p_aout, "DirectSoundThread ready" );
1031 /* Wait here until Play() is called */
1032 WaitForSingleObject( p_notif->event, INFINITE );
1034 if( !vlc_atomic_get( &p_notif->abort) )
1037 mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1039 /* start playing the buffer */
1040 dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1043 DSBPLAY_LOOPING ); /* Flags */
1044 if( dsresult == DSERR_BUFFERLOST )
1046 IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1047 dsresult = IDirectSoundBuffer_Play(
1048 p_aout->sys->p_dsbuffer,
1051 DSBPLAY_LOOPING ); /* Flags */
1053 if( dsresult != DS_OK )
1055 msg_Err( p_aout, "cannot start playing buffer" );
1058 last_time = mdate();
1060 while( !vlc_atomic_get( &p_notif->abort ) )
1063 int l_queued = 0, l_free_slots;
1064 unsigned i_frame_siz = p_aout->sys->packet.samples;
1065 mtime_t mtime = mdate();
1068 /* Update volume if required */
1069 LONG volume = InterlockedExchange( &p_aout->sys->volume, -1 );
1070 if( unlikely(volume != -1) )
1071 IDirectSoundBuffer_SetVolume( p_aout->sys->p_dsbuffer, volume );
1074 * Fill in as much audio data as we can in our circular buffer
1077 /* Find out current play position */
1078 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1079 p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1081 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1085 /* Detect underruns */
1086 if( l_queued && mtime - last_time >
1087 INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1089 msg_Dbg( p_aout, "detected underrun!" );
1093 /* Try to fill in as many frame buffers as possible */
1094 l_read /= (p_aout->format.i_bytes_per_frame /
1095 p_aout->format.i_frame_length);
1096 l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1097 if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1098 l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1100 for( i = 0; i < l_free_slots; i++ )
1102 aout_buffer_t *p_buffer = aout_PacketNext( p_aout,
1103 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1104 p_aout->format.i_rate );
1106 /* If there is no audio data available and we have some buffered
1107 * already, then just wait for the next time */
1108 if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1110 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1111 p_buffer ) != VLC_SUCCESS ) break;
1114 /* Sleep a reasonable amount of time */
1115 l_queued += (i * i_frame_siz);
1116 msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1119 /* make sure the buffer isn't playing */
1120 IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1122 msg_Dbg( p_aout, "DirectSoundThread exiting" );
1126 /*****************************************************************************
1127 * CallBackConfigNBEnum: callback to get the number of available devices
1128 *****************************************************************************/
1129 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1130 LPCWSTR psz_mod, LPVOID p_nb )
1132 VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1134 int * a = (int *)p_nb;
1139 /*****************************************************************************
1140 * CallBackConfigEnum: callback to add available devices to the preferences list
1141 *****************************************************************************/
1142 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1143 LPCWSTR psz_mod, LPVOID _p_item )
1145 VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1147 module_config_t *p_item = (module_config_t *) _p_item;
1149 p_item->ppsz_list[p_item->i_list] = FromWide( psz_desc );
1150 p_item->ppsz_list_text[p_item->i_list] = FromWide( psz_desc );
1155 /*****************************************************************************
1156 * ReloadDirectXDevices: store the list of devices in preferences
1157 *****************************************************************************/
1158 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1159 vlc_value_t newval, vlc_value_t oldval, void *data )
1161 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1163 module_config_t *p_item = config_FindConfig( p_this, psz_name );
1164 if( !p_item ) return VLC_SUCCESS;
1166 /* Clear-up the current list */
1167 if( p_item->i_list )
1169 for( int i = 0; i < p_item->i_list; i++ )
1171 free((char *)(p_item->ppsz_list[i]) );
1172 free((char *)(p_item->ppsz_list_text[i]) );
1176 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
1178 HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1179 if( hdsound_dll == NULL )
1181 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1185 /* Get DirectSoundEnumerate */
1186 OurDirectSoundEnumerate = (void *)
1187 GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1189 if( OurDirectSoundEnumerate == NULL )
1193 OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1194 msg_Dbg(p_this,"found %d devices", nb_devices);
1196 p_item->ppsz_list = xrealloc( p_item->ppsz_list,
1197 nb_devices * sizeof(char *) );
1198 p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
1199 nb_devices * sizeof(char *) );
1202 OurDirectSoundEnumerate(CallBackConfigEnum, p_item);
1205 FreeLibrary(hdsound_dll);