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) */
85 notification_thread_t notif; /* DirectSoundThread id */
87 int i_frame_size; /* Size in bytes of one frame */
89 int i_speaker_setup; /* Speaker setup override */
91 bool b_chan_reorder; /* do we need channel reordering */
92 int pi_chan_table[AOUT_CHAN_MAX];
93 uint32_t i_channel_mask;
94 uint32_t i_bits_per_sample;
98 /*****************************************************************************
100 *****************************************************************************/
101 static int OpenAudio ( vlc_object_t * );
102 static void CloseAudio ( vlc_object_t * );
103 static void Play ( audio_output_t *, block_t *, mtime_t * );
104 static int VolumeSet ( audio_output_t *, float );
105 static int MuteSet ( audio_output_t *, bool );
107 /* local functions */
108 static void Probe ( audio_output_t * );
109 static int InitDirectSound ( audio_output_t * );
110 static int CreateDSBuffer ( audio_output_t *, int, int, int, int, int, bool );
111 static int CreateDSBufferPCM ( audio_output_t *, vlc_fourcc_t*, int, int, int, bool );
112 static void DestroyDSBuffer ( audio_output_t * );
113 static void* DirectSoundThread( void * );
114 static int FillBuffer ( audio_output_t *, int, block_t * );
116 static int ReloadDirectXDevices( vlc_object_t *, const char *,
117 char ***, char *** );
119 /* Speaker setup override options list */
120 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
121 "Quad", "5.1", "7.1" };
123 /*****************************************************************************
125 *****************************************************************************/
126 #define DEVICE_TEXT N_("Output device")
127 #define DEVICE_LONGTEXT N_("Select your audio output device")
129 #define SPEAKER_TEXT N_("Speaker configuration")
130 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
131 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
133 #define VOLUME_TEXT N_("Audio volume")
134 #define VOLUME_LONGTEXT N_("Audio volume in hundredths of decibels (dB).")
137 set_description( N_("DirectX audio output") )
138 set_shortname( "DirectX" )
139 set_capability( "audio output", 100 )
140 set_category( CAT_AUDIO )
141 set_subcategory( SUBCAT_AUDIO_AOUT )
142 add_shortcut( "directx", "aout_directx" )
144 add_string( "directx-audio-device", "default",
145 DEVICE_TEXT, DEVICE_LONGTEXT, false )
146 change_string_cb( ReloadDirectXDevices )
147 add_obsolete_string( "directx-audio-device-name")
148 add_bool( "directx-audio-float32", false, FLOAT_TEXT,
149 FLOAT_LONGTEXT, true )
150 add_string( "directx-audio-speaker", "Windows default",
151 SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
152 change_string_list( speaker_list, speaker_list )
153 add_integer( "directx-volume", DSBVOLUME_MAX,
154 VOLUME_TEXT, VOLUME_LONGTEXT, true )
155 change_integer_range( DSBVOLUME_MIN, DSBVOLUME_MAX )
157 set_callbacks( OpenAudio, CloseAudio )
160 /*****************************************************************************
161 * OpenAudio: open the audio device
162 *****************************************************************************
163 * This function opens and setups Direct Sound.
164 *****************************************************************************/
165 static int OpenAudio( vlc_object_t *p_this )
167 audio_output_t * p_aout = (audio_output_t *)p_this;
172 const char * const * ppsz_compare = speaker_list;
174 msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
176 /* Allocate structure */
177 p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
178 if( unlikely( p_aout->sys == NULL ) )
181 /* Retrieve config values */
182 var_Create( p_aout, "directx-audio-float32",
183 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
184 psz_speaker = var_CreateGetString( p_aout, "directx-audio-speaker" );
186 while ( *ppsz_compare != NULL )
188 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
195 if ( *ppsz_compare == NULL )
197 msg_Err( p_aout, "(%s) isn't valid speaker setup option", psz_speaker );
198 msg_Err( p_aout, "Defaulting to Windows default speaker config");
202 p_aout->sys->i_speaker_setup = i;
204 /* Initialise DirectSound */
205 if( InitDirectSound( p_aout ) )
207 msg_Err( p_aout, "cannot initialize DirectSound" );
211 if( var_Type( p_aout, "audio-device" ) == 0 )
216 if( var_Get( p_aout, "audio-device", &val ) < 0 )
218 msg_Err( p_aout, "DirectSound Probe failed()" );
222 /* Open the device */
223 if( val.i_int == AOUT_VAR_SPDIF )
225 p_aout->format.i_format = VLC_CODEC_SPDIFL;
227 /* Calculate the frame size in bytes */
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" );
243 aout_PacketInit( p_aout, &p_aout->sys->packet, A52_FRAME_NB );
247 if( val.i_int == AOUT_VAR_5_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
254 else if( val.i_int == AOUT_VAR_7_1 )
256 p_aout->format.i_physical_channels
257 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
258 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
259 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
262 else if( val.i_int == AOUT_VAR_3F2R )
264 p_aout->format.i_physical_channels
265 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
266 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
268 else if( val.i_int == AOUT_VAR_2F2R )
270 p_aout->format.i_physical_channels
271 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
272 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
274 else if( val.i_int == AOUT_VAR_MONO )
276 p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
280 p_aout->format.i_physical_channels
281 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
284 if( CreateDSBufferPCM( p_aout, &p_aout->format.i_format,
285 p_aout->format.i_physical_channels,
286 aout_FormatNbChannels( &p_aout->format ),
287 p_aout->format.i_rate, false )
290 msg_Err( p_aout, "cannot open directx audio device" );
294 /* Calculate the frame size in bytes */
295 aout_FormatPrepare( &p_aout->format );
296 aout_PacketInit( p_aout, &p_aout->sys->packet, FRAME_SIZE );
299 p_aout->sys->volume.volume = -1;
301 /* Now we need to setup our DirectSound play notification structure */
302 vlc_atomic_set(&p_aout->sys->notif.abort, 0);
303 p_aout->sys->notif.event = CreateEvent( 0, FALSE, FALSE, 0 );
304 if( unlikely(p_aout->sys->notif.event == NULL) )
306 p_aout->sys->notif.i_frame_size = p_aout->sys->i_frame_size;
308 /* then launch the notification thread */
309 msg_Dbg( p_aout, "creating DirectSoundThread" );
310 if( vlc_clone( &p_aout->sys->notif.thread, DirectSoundThread, p_aout,
311 VLC_THREAD_PRIORITY_HIGHEST ) )
313 msg_Err( p_aout, "cannot create DirectSoundThread" );
314 CloseHandle( p_aout->sys->notif.event );
315 p_aout->sys->notif.event = NULL;
316 aout_PacketDestroy( p_aout );
320 p_aout->pf_play = Play;
321 p_aout->pf_pause = aout_PacketPause;
322 p_aout->pf_flush = aout_PacketFlush;
325 if( val.i_int == AOUT_VAR_SPDIF )
327 p_aout->volume_set = NULL;
328 p_aout->mute_set = NULL;
332 LONG mb = var_InheritInteger( p_aout, "directx-volume" );
334 p_aout->volume_set = VolumeSet;
335 p_aout->mute_set = MuteSet;
336 p_aout->sys->volume.mb = mb;
337 aout_VolumeReport( p_aout, cbrtf(powf(10.f, ((float)mb) / 2000.f)) );
338 MuteSet( p_aout, var_InheritBool( p_aout, "mute" ) );
343 CloseAudio( VLC_OBJECT(p_aout) );
347 /*****************************************************************************
348 * Probe: probe the audio device for available formats and channels
349 *****************************************************************************/
350 static void Probe( audio_output_t * p_aout )
352 vlc_value_t val, text;
353 vlc_fourcc_t i_format;
354 unsigned int i_physical_channels;
355 DWORD ui_speaker_config;
356 bool is_default_output_set = false;
358 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
359 text.psz_string = _("Audio Device");
360 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
362 /* Test for 5.1 support */
363 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
364 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
365 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
366 if( p_aout->format.i_physical_channels == i_physical_channels )
368 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
369 p_aout->format.i_rate, true )
372 val.i_int = AOUT_VAR_5_1;
373 text.psz_string = (char*) "5.1";
374 var_Change( p_aout, "audio-device",
375 VLC_VAR_ADDCHOICE, &val, &text );
376 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
377 is_default_output_set = true;
378 msg_Dbg( p_aout, "device supports 5.1 channels" );
382 /* Test for 7.1 support */
383 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
384 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
385 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
386 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
387 if( p_aout->format.i_physical_channels == i_physical_channels )
389 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
390 p_aout->format.i_rate, true )
393 val.i_int = AOUT_VAR_7_1;
394 text.psz_string = (char*) "7.1";
395 var_Change( p_aout, "audio-device",
396 VLC_VAR_ADDCHOICE, &val, &text );
397 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
398 is_default_output_set = true;
399 msg_Dbg( p_aout, "device supports 7.1 channels" );
403 /* Test for 3 Front 2 Rear support */
404 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
405 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
407 if( p_aout->format.i_physical_channels == i_physical_channels )
409 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
410 p_aout->format.i_rate, true )
413 val.i_int = AOUT_VAR_3F2R;
414 text.psz_string = _("3 Front 2 Rear");
415 var_Change( p_aout, "audio-device",
416 VLC_VAR_ADDCHOICE, &val, &text );
417 if(!is_default_output_set)
419 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
420 is_default_output_set = true;
422 msg_Dbg( p_aout, "device supports 5 channels" );
426 /* Test for 2 Front 2 Rear support */
427 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
428 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
429 if( ( p_aout->format.i_physical_channels & i_physical_channels )
430 == i_physical_channels )
432 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
433 p_aout->format.i_rate, true )
436 val.i_int = AOUT_VAR_2F2R;
437 text.psz_string = _("2 Front 2 Rear");
438 var_Change( p_aout, "audio-device",
439 VLC_VAR_ADDCHOICE, &val, &text );
440 if(!is_default_output_set)
442 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
443 is_default_output_set = true;
445 msg_Dbg( p_aout, "device supports 4 channels" );
449 /* Test for stereo support */
450 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
451 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
452 p_aout->format.i_rate, true )
455 val.i_int = AOUT_VAR_STEREO;
456 text.psz_string = _("Stereo");
457 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
458 if(!is_default_output_set)
460 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
461 is_default_output_set = true;
462 msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
464 else msg_Dbg( p_aout, "device supports 2 channels" );
467 /* Test for mono support */
468 i_physical_channels = AOUT_CHAN_CENTER;
469 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
470 p_aout->format.i_rate, true )
473 val.i_int = AOUT_VAR_MONO;
474 text.psz_string = _("Mono");
475 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
476 msg_Dbg( p_aout, "device supports 1 channel" );
479 /* Check the speaker configuration to determine which channel config should
481 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->sys->p_dsobject,
482 &ui_speaker_config ) )
484 ui_speaker_config = DSSPEAKER_STEREO;
485 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
487 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
489 case DSSPEAKER_7POINT1:
490 case DSSPEAKER_7POINT1_SURROUND:
491 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
492 val.i_int = AOUT_VAR_7_1;
494 case DSSPEAKER_5POINT1:
495 case DSSPEAKER_5POINT1_SURROUND:
496 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
497 val.i_int = AOUT_VAR_5_1;
500 msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
501 val.i_int = AOUT_VAR_2F2R;
503 #if 0 /* Lots of people just get their settings wrong and complain that
504 * this is a problem with VLC so just don't ever set mono by default. */
506 val.i_int = AOUT_VAR_MONO;
509 case DSSPEAKER_SURROUND:
510 msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
511 case DSSPEAKER_STEREO:
512 msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
514 /* If nothing else is found, choose stereo output */
515 val.i_int = AOUT_VAR_STEREO;
519 /* Check if we want to override speaker config */
520 switch( p_aout->sys->i_speaker_setup )
522 case 0: /* Default value aka Windows default speaker setup */
525 msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
526 val.i_int = AOUT_VAR_MONO;
529 msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
530 val.i_int = AOUT_VAR_STEREO;
533 msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
534 val.i_int = AOUT_VAR_2F2R;
537 msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
538 val.i_int = AOUT_VAR_5_1;
541 msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
542 val.i_int = AOUT_VAR_7_1;
545 msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
549 var_Set( p_aout, "audio-device", val );
551 /* Test for SPDIF support */
552 if ( AOUT_FMT_SPDIF( &p_aout->format ) )
554 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
555 p_aout->format.i_physical_channels,
556 aout_FormatNbChannels( &p_aout->format ),
557 p_aout->format.i_rate,
558 AOUT_SPDIF_SIZE, true )
561 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
562 val.i_int = AOUT_VAR_SPDIF;
563 text.psz_string = _("A/52 over S/PDIF");
564 var_Change( p_aout, "audio-device",
565 VLC_VAR_ADDCHOICE, &val, &text );
566 if( var_InheritBool( p_aout, "spdif" ) )
567 var_Set( p_aout, "audio-device", val );
571 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
574 /* Probe() has failed. */
575 var_Destroy( p_aout, "audio-device" );
579 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
582 /*****************************************************************************
583 * Play: we'll start playing the directsound buffer here because at least here
584 * we know the first buffer has been put in the aout fifo and we also
586 *****************************************************************************/
587 static void Play( audio_output_t *p_aout, block_t *p_buffer,
588 mtime_t *restrict drift )
590 /* get the playing date of the first aout buffer */
591 p_aout->sys->notif.start_date = p_buffer->i_pts;
593 /* fill in the first samples (zeroes) */
594 FillBuffer( p_aout, 0, NULL );
596 /* wake up the audio output thread */
597 SetEvent( p_aout->sys->notif.event );
599 aout_PacketPlay( p_aout, p_buffer, drift );
600 p_aout->pf_play = aout_PacketPlay;
603 static int VolumeSet( audio_output_t *p_aout, float vol )
605 aout_sys_t *sys = p_aout->sys;
608 /* Convert UI volume to linear factor (cube) */
609 vol = vol * vol * vol;
611 /* millibels from linear amplification */
612 LONG mb = lroundf(2000.f * log10f(vol));
614 /* Clamp to allowed DirectSound range */
615 static_assert( DSBVOLUME_MIN < DSBVOLUME_MAX, "DSBVOLUME_* confused" );
616 if( mb >= DSBVOLUME_MAX )
621 if( mb <= DSBVOLUME_MIN )
625 if (!sys->volume.mute)
626 InterlockedExchange(&sys->volume.volume, mb);
628 /* Convert back to UI volume */
629 vol = cbrtf(powf(10.f, ((float)mb) / 2000.f));
630 aout_VolumeReport( p_aout, vol );
632 if( var_InheritBool( p_aout, "volume-save" ) )
633 config_PutInt( p_aout, "directx-volume", mb );
637 static int MuteSet( audio_output_t *p_aout, bool mute )
639 aout_sys_t *sys = p_aout->sys;
641 sys->volume.mute = mute;
642 InterlockedExchange(&sys->volume.volume,
643 mute ? DSBVOLUME_MIN : sys->volume.mb);
645 aout_MuteReport( p_aout, mute );
649 /*****************************************************************************
650 * CloseAudio: close the audio device
651 *****************************************************************************/
652 static void CloseAudio( vlc_object_t *p_this )
654 audio_output_t * p_aout = (audio_output_t *)p_this;
655 aout_sys_t *p_sys = p_aout->sys;
657 msg_Dbg( p_aout, "closing audio device" );
659 /* kill the position notification thread, if any */
660 if( p_sys->notif.event != NULL )
662 vlc_atomic_set(&p_aout->sys->notif.abort, 1);
663 /* wake up the audio thread if needed */
664 if( p_aout->pf_play == Play )
665 SetEvent( p_sys->notif.event );
667 vlc_join( p_sys->notif.thread, NULL );
668 CloseHandle( p_sys->notif.event );
669 aout_PacketDestroy( p_aout );
672 /* release the secondary buffer */
673 DestroyDSBuffer( p_aout );
675 /* finally release the DirectSound object */
676 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
678 /* free DSOUND.DLL */
679 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
681 free( p_aout->sys->p_device_guid );
685 /*****************************************************************************
686 * CallBackDirectSoundEnum: callback to enumerate available devices
687 *****************************************************************************/
688 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
689 LPCWSTR psz_mod, LPVOID _p_aout )
691 VLC_UNUSED( psz_mod );
693 audio_output_t *p_aout = (audio_output_t *)_p_aout;
695 char *psz_device = FromWide( psz_desc );
696 msg_Dbg( p_aout, "found device: %s", psz_device );
698 if( p_aout->sys->psz_device &&
699 !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
701 /* Use the device corresponding to psz_device */
702 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
703 *p_aout->sys->p_device_guid = *p_guid;
704 msg_Dbg( p_aout, "using device: %s", psz_device );
708 /* If no default device has been selected, chose the first one */
709 if( !p_aout->sys->psz_device && p_guid )
711 p_aout->sys->psz_device = strdup( psz_device );
712 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
713 *p_aout->sys->p_device_guid = *p_guid;
714 msg_Dbg( p_aout, "using device: %s", psz_device );
722 /*****************************************************************************
723 * InitDirectSound: handle all the gory details of DirectSound initialisation
724 *****************************************************************************/
725 static int InitDirectSound( audio_output_t *p_aout )
727 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
728 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
730 p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
731 if( p_aout->sys->hdsound_dll == NULL )
733 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
737 OurDirectSoundCreate = (void *)
738 GetProcAddress( p_aout->sys->hdsound_dll,
739 "DirectSoundCreate" );
740 if( OurDirectSoundCreate == NULL )
742 msg_Warn( p_aout, "GetProcAddress FAILED" );
746 /* Get DirectSoundEnumerate */
747 OurDirectSoundEnumerate = (void *)
748 GetProcAddress( p_aout->sys->hdsound_dll,
749 "DirectSoundEnumerateW" );
750 if( OurDirectSoundEnumerate )
752 p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device");
753 /* Attempt enumeration */
754 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
757 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
761 /* Create the direct sound object */
762 if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
763 &p_aout->sys->p_dsobject,
766 msg_Warn( p_aout, "cannot create a direct sound device" );
770 /* Set DirectSound Cooperative level, ie what control we want over Windows
771 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
772 * settings of the primary buffer, but also that only the sound of our
773 * application will be hearable when it will have the focus.
774 * !!! (this is not really working as intended yet because to set the
775 * cooperative level you need the window handle of your application, and
776 * I don't know of any easy way to get it. Especially since we might play
777 * sound without any video, and so what window handle should we use ???
778 * The hack for now is to use the Desktop window handle - it seems to be
780 if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
784 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
790 p_aout->sys->p_dsobject = NULL;
791 if( p_aout->sys->hdsound_dll )
793 FreeLibrary( p_aout->sys->hdsound_dll );
794 p_aout->sys->hdsound_dll = NULL;
800 /*****************************************************************************
801 * CreateDSBuffer: Creates a direct sound buffer of the required format.
802 *****************************************************************************
803 * This function creates the buffer we'll use to play audio.
804 * In DirectSound there are two kinds of buffers:
805 * - the primary buffer: which is the actual buffer that the soundcard plays
806 * - the secondary buffer(s): these buffers are the one actually used by
807 * applications and DirectSound takes care of mixing them into the primary.
809 * Once you create a secondary buffer, you cannot change its format anymore so
810 * you have to release the current one and create another.
811 *****************************************************************************/
812 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
813 int i_channels, int i_nb_channels, int i_rate,
814 int i_bytes_per_frame, bool b_probe )
816 WAVEFORMATEXTENSIBLE waveformat;
817 DSBUFFERDESC dsbdesc;
819 /* First set the sound buffer format */
820 waveformat.dwChannelMask = 0;
821 for( unsigned i = 0; pi_vlc_chan_order_wg4[i]; i++ )
822 if( i_channels & pi_vlc_chan_order_wg4[i] )
823 waveformat.dwChannelMask |= pi_channels_in[i];
827 case VLC_CODEC_SPDIFL:
829 /* To prevent channel re-ordering */
830 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
831 waveformat.Format.wBitsPerSample = 16;
832 waveformat.Samples.wValidBitsPerSample =
833 waveformat.Format.wBitsPerSample;
834 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
835 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
839 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
840 waveformat.Samples.wValidBitsPerSample =
841 waveformat.Format.wBitsPerSample;
842 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
843 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
847 waveformat.Format.wBitsPerSample = 16;
848 waveformat.Samples.wValidBitsPerSample =
849 waveformat.Format.wBitsPerSample;
850 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
851 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
855 waveformat.Format.nChannels = i_nb_channels;
856 waveformat.Format.nSamplesPerSec = i_rate;
857 waveformat.Format.nBlockAlign =
858 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
859 waveformat.Format.nAvgBytesPerSec =
860 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
862 p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
863 p_aout->sys->i_channels = i_nb_channels;
865 /* Then fill in the direct sound descriptor */
866 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
867 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
868 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
869 | DSBCAPS_GLOBALFOCUS /* Allows background playing */
870 | DSBCAPS_CTRLVOLUME; /* Allows volume control */
872 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
873 if( i_nb_channels <= 2 )
875 waveformat.Format.cbSize = 0;
879 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
880 waveformat.Format.cbSize =
881 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
883 /* Needed for 5.1 on emu101k */
884 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
887 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
888 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
890 if FAILED( IDirectSound_CreateSoundBuffer(
891 p_aout->sys->p_dsobject, &dsbdesc,
892 &p_aout->sys->p_dsbuffer, NULL) )
894 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
896 /* Try without DSBCAPS_LOCHARDWARE */
897 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
898 if FAILED( IDirectSound_CreateSoundBuffer(
899 p_aout->sys->p_dsobject, &dsbdesc,
900 &p_aout->sys->p_dsbuffer, NULL) )
905 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
913 /* Stop here if we were just probing */
916 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
917 p_aout->sys->p_dsbuffer = NULL;
921 p_aout->sys->i_frame_size = i_bytes_per_frame;
922 p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
923 p_aout->sys->b_chan_reorder =
924 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
925 waveformat.dwChannelMask, i_nb_channels,
926 p_aout->sys->pi_chan_table );
928 if( p_aout->sys->b_chan_reorder )
930 msg_Dbg( p_aout, "channel reordering needed" );
936 /*****************************************************************************
937 * CreateDSBufferPCM: creates a PCM direct sound buffer.
938 *****************************************************************************
939 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
940 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
941 ****************************************************************************/
942 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
943 int i_channels, int i_nb_channels, int i_rate,
946 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
947 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
949 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
950 i_channels, i_nb_channels, i_rate,
951 FRAME_SIZE * 4 * i_nb_channels, b_probe )
954 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
955 i_channels, i_nb_channels, i_rate,
956 FRAME_SIZE * 2 * i_nb_channels, b_probe )
963 *i_format = VLC_CODEC_S16L;
969 *i_format = VLC_CODEC_FL32;
974 /*****************************************************************************
976 *****************************************************************************
977 * This function destroys the secondary buffer.
978 *****************************************************************************/
979 static void DestroyDSBuffer( audio_output_t *p_aout )
981 if( p_aout->sys->p_dsbuffer )
983 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
984 p_aout->sys->p_dsbuffer = NULL;
988 /*****************************************************************************
989 * FillBuffer: Fill in one of the direct sound frame buffers.
990 *****************************************************************************
991 * Returns VLC_SUCCESS on success.
992 *****************************************************************************/
993 static int FillBuffer( audio_output_t *p_aout, int i_frame, block_t *p_buffer )
995 aout_sys_t *p_sys = p_aout->sys;
996 notification_thread_t *p_notif = &p_sys->notif;
997 void *p_write_position, *p_wrap_around;
998 unsigned long l_bytes1, l_bytes2;
1001 /* Before copying anything, we have to lock the buffer */
1002 dsresult = IDirectSoundBuffer_Lock(
1003 p_sys->p_dsbuffer, /* DS buffer */
1004 i_frame * p_notif->i_frame_size, /* Start offset */
1005 p_notif->i_frame_size, /* Number of bytes */
1006 &p_write_position, /* Address of lock start */
1007 &l_bytes1, /* Count of bytes locked before wrap around */
1008 &p_wrap_around, /* Buffer address (if wrap around) */
1009 &l_bytes2, /* Count of bytes after wrap around */
1011 if( dsresult == DSERR_BUFFERLOST )
1013 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
1014 dsresult = IDirectSoundBuffer_Lock(
1016 i_frame * p_notif->i_frame_size,
1017 p_notif->i_frame_size,
1024 if( dsresult != DS_OK )
1026 msg_Warn( p_aout, "cannot lock buffer" );
1027 if( p_buffer ) block_Release( p_buffer );
1028 return VLC_EGENERIC;
1031 if( p_buffer == NULL )
1033 memset( p_write_position, 0, l_bytes1 );
1037 if( p_sys->b_chan_reorder )
1039 /* Do the channel reordering here */
1040 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
1041 p_sys->i_channels, p_sys->pi_chan_table,
1042 p_sys->i_bits_per_sample );
1045 memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
1046 block_Release( p_buffer );
1049 /* Now the data has been copied, unlock the buffer */
1050 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1051 p_wrap_around, l_bytes2 );
1053 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1057 /*****************************************************************************
1058 * DirectSoundThread: this thread will capture play notification events.
1059 *****************************************************************************
1060 * We use this thread to emulate a callback mechanism. The thread probes for
1061 * event notification and fills up the DS secondary buffer when needed.
1062 *****************************************************************************/
1063 static void* DirectSoundThread( void *data )
1065 audio_output_t *p_aout = (audio_output_t *)data;
1066 notification_thread_t *p_notif = &p_aout->sys->notif;
1069 msg_Dbg( p_aout, "DirectSoundThread ready" );
1071 /* Wait here until Play() is called */
1072 WaitForSingleObject( p_notif->event, INFINITE );
1074 if( !vlc_atomic_get( &p_notif->abort) )
1077 mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1079 /* start playing the buffer */
1080 dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1083 DSBPLAY_LOOPING ); /* Flags */
1084 if( dsresult == DSERR_BUFFERLOST )
1086 IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1087 dsresult = IDirectSoundBuffer_Play(
1088 p_aout->sys->p_dsbuffer,
1091 DSBPLAY_LOOPING ); /* Flags */
1093 if( dsresult != DS_OK )
1095 msg_Err( p_aout, "cannot start playing buffer" );
1098 last_time = mdate();
1100 while( !vlc_atomic_get( &p_notif->abort ) )
1103 int l_queued = 0, l_free_slots;
1104 unsigned i_frame_siz = p_aout->sys->packet.samples;
1105 mtime_t mtime = mdate();
1108 /* Update volume if required */
1109 LONG volume = InterlockedExchange( &p_aout->sys->volume.volume, -1 );
1110 if( unlikely(volume != -1) )
1111 IDirectSoundBuffer_SetVolume( p_aout->sys->p_dsbuffer, volume );
1114 * Fill in as much audio data as we can in our circular buffer
1117 /* Find out current play position */
1118 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1119 p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1121 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1125 /* Detect underruns */
1126 if( l_queued && mtime - last_time >
1127 INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1129 msg_Dbg( p_aout, "detected underrun!" );
1133 /* Try to fill in as many frame buffers as possible */
1134 l_read /= (p_aout->format.i_bytes_per_frame /
1135 p_aout->format.i_frame_length);
1136 l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1137 if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1138 l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1140 for( i = 0; i < l_free_slots; i++ )
1142 block_t *p_buffer = aout_PacketNext( p_aout,
1143 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1144 p_aout->format.i_rate );
1146 /* If there is no audio data available and we have some buffered
1147 * already, then just wait for the next time */
1148 if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1150 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1151 p_buffer ) != VLC_SUCCESS ) break;
1154 /* Sleep a reasonable amount of time */
1155 l_queued += (i * i_frame_siz);
1156 msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1159 /* make sure the buffer isn't playing */
1160 IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1162 msg_Dbg( p_aout, "DirectSoundThread exiting" );
1166 /*****************************************************************************
1167 * CallBackConfigNBEnum: callback to get the number of available devices
1168 *****************************************************************************/
1169 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1170 LPCWSTR psz_mod, LPVOID data )
1175 VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1179 /*****************************************************************************
1180 * CallBackConfigEnum: callback to add available devices to the preferences list
1181 *****************************************************************************/
1182 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1183 LPCWSTR psz_mod, LPVOID data )
1185 char **values = data;
1187 while( *values != NULL )
1189 *values = FromWide( psz_desc );
1191 VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1195 /*****************************************************************************
1196 * ReloadDirectXDevices: store the list of devices in preferences
1197 *****************************************************************************/
1198 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1199 char ***values, char ***descs )
1205 HANDLE hdsound_dll = LoadLibrary(_T("DSOUND.DLL"));
1206 if( hdsound_dll == NULL )
1208 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1212 /* Get DirectSoundEnumerate */
1213 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID) =
1214 (void *)GetProcAddress( hdsound_dll, _T("DirectSoundEnumerateW") );
1215 if( OurDirectSoundEnumerate == NULL )
1218 OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1219 msg_Dbg(p_this, "found %d devices", nb_devices);
1221 *values = xcalloc( nb_devices, sizeof(char *) );
1222 OurDirectSoundEnumerate(CallBackConfigEnum, *values);
1223 *descs = xcalloc( nb_devices, sizeof(char *) );
1224 OurDirectSoundEnumerate(CallBackConfigEnum, *descs);
1226 FreeLibrary(hdsound_dll);