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 *, char const *,
117 vlc_value_t, vlc_value_t, void * );
119 /* Speaker setup override options list */
120 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
121 "Quad", "5.1", "7.1" };
122 static const char *const ppsz_adev[] = {"default", };
123 static const char *const ppsz_adev_text[] = {"default", };
125 /*****************************************************************************
127 *****************************************************************************/
128 #define DEVICE_TEXT N_("Output device")
129 #define DEVICE_LONGTEXT N_("Select your audio output device")
131 #define SPEAKER_TEXT N_("Speaker configuration")
132 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
133 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
136 set_description( N_("DirectX audio output") )
137 set_shortname( "DirectX" )
138 set_capability( "audio output", 100 )
139 set_category( CAT_AUDIO )
140 set_subcategory( SUBCAT_AUDIO_AOUT )
141 add_shortcut( "directx", "aout_directx" )
143 add_string( "directx-audio-device", "default",
144 DEVICE_TEXT, DEVICE_LONGTEXT, false )
145 change_string_list( ppsz_adev, ppsz_adev_text, ReloadDirectXDevices )
146 change_action_add( ReloadDirectXDevices, N_("Refresh list") )
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, 0, 0 )
154 set_callbacks( OpenAudio, CloseAudio )
157 /*****************************************************************************
158 * OpenAudio: open the audio device
159 *****************************************************************************
160 * This function opens and setups Direct Sound.
161 *****************************************************************************/
162 static int OpenAudio( vlc_object_t *p_this )
164 audio_output_t * p_aout = (audio_output_t *)p_this;
169 const char * const * ppsz_compare = speaker_list;
171 msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
173 /* Allocate structure */
174 p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
175 if( unlikely( p_aout->sys == NULL ) )
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 /* Initialise DirectSound */
202 if( InitDirectSound( p_aout ) )
204 msg_Err( p_aout, "cannot initialize DirectSound" );
208 if( var_Type( p_aout, "audio-device" ) == 0 )
213 if( var_Get( p_aout, "audio-device", &val ) < 0 )
215 msg_Err( p_aout, "DirectSound Probe failed()" );
219 /* Open the device */
220 if( val.i_int == AOUT_VAR_SPDIF )
222 p_aout->format.i_format = VLC_CODEC_SPDIFL;
224 /* Calculate the frame size in bytes */
225 p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
226 p_aout->format.i_frame_length = A52_FRAME_NB;
227 p_aout->sys->i_frame_size = p_aout->format.i_bytes_per_frame;
229 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
230 p_aout->format.i_physical_channels,
231 aout_FormatNbChannels( &p_aout->format ),
232 p_aout->format.i_rate,
233 p_aout->sys->i_frame_size, false )
236 msg_Err( p_aout, "cannot open directx audio device" );
240 aout_PacketInit( p_aout, &p_aout->sys->packet, A52_FRAME_NB );
244 if( val.i_int == AOUT_VAR_5_1 )
246 p_aout->format.i_physical_channels
247 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
248 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
251 else if( val.i_int == AOUT_VAR_7_1 )
253 p_aout->format.i_physical_channels
254 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
255 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
256 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
259 else if( val.i_int == AOUT_VAR_3F2R )
261 p_aout->format.i_physical_channels
262 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
263 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
265 else if( val.i_int == AOUT_VAR_2F2R )
267 p_aout->format.i_physical_channels
268 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
269 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
271 else if( val.i_int == AOUT_VAR_MONO )
273 p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
277 p_aout->format.i_physical_channels
278 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
281 if( CreateDSBufferPCM( p_aout, &p_aout->format.i_format,
282 p_aout->format.i_physical_channels,
283 aout_FormatNbChannels( &p_aout->format ),
284 p_aout->format.i_rate, false )
287 msg_Err( p_aout, "cannot open directx audio device" );
291 /* Calculate the frame size in bytes */
292 aout_FormatPrepare( &p_aout->format );
293 aout_PacketInit( p_aout, &p_aout->sys->packet, FRAME_SIZE );
296 p_aout->sys->volume.volume = -1;
298 /* Now we need to setup our DirectSound play notification structure */
299 vlc_atomic_set(&p_aout->sys->notif.abort, 0);
300 p_aout->sys->notif.event = CreateEvent( 0, FALSE, FALSE, 0 );
301 if( unlikely(p_aout->sys->notif.event == NULL) )
303 p_aout->sys->notif.i_frame_size = p_aout->sys->i_frame_size;
305 /* then launch the notification thread */
306 msg_Dbg( p_aout, "creating DirectSoundThread" );
307 if( vlc_clone( &p_aout->sys->notif.thread, DirectSoundThread, p_aout,
308 VLC_THREAD_PRIORITY_HIGHEST ) )
310 msg_Err( p_aout, "cannot create DirectSoundThread" );
311 CloseHandle( p_aout->sys->notif.event );
312 p_aout->sys->notif.event = NULL;
313 aout_PacketDestroy( p_aout );
317 p_aout->pf_play = Play;
318 p_aout->pf_pause = aout_PacketPause;
319 p_aout->pf_flush = aout_PacketFlush;
322 if( val.i_int == AOUT_VAR_SPDIF )
324 p_aout->volume_set = NULL;
325 p_aout->mute_set = NULL;
329 p_aout->sys->volume.mb = 0;
330 p_aout->sys->volume.mute = false;
331 p_aout->volume_set = VolumeSet;
332 p_aout->mute_set = MuteSet;
337 CloseAudio( VLC_OBJECT(p_aout) );
341 /*****************************************************************************
342 * Probe: probe the audio device for available formats and channels
343 *****************************************************************************/
344 static void Probe( audio_output_t * p_aout )
346 vlc_value_t val, text;
347 vlc_fourcc_t i_format;
348 unsigned int i_physical_channels;
349 DWORD ui_speaker_config;
350 bool is_default_output_set = false;
352 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
353 text.psz_string = _("Audio Device");
354 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
356 /* Test for 5.1 support */
357 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
358 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
359 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
360 if( p_aout->format.i_physical_channels == i_physical_channels )
362 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
363 p_aout->format.i_rate, true )
366 val.i_int = AOUT_VAR_5_1;
367 text.psz_string = (char*) "5.1";
368 var_Change( p_aout, "audio-device",
369 VLC_VAR_ADDCHOICE, &val, &text );
370 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
371 is_default_output_set = true;
372 msg_Dbg( p_aout, "device supports 5.1 channels" );
376 /* Test for 7.1 support */
377 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
378 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
379 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
380 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
381 if( p_aout->format.i_physical_channels == i_physical_channels )
383 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
384 p_aout->format.i_rate, true )
387 val.i_int = AOUT_VAR_7_1;
388 text.psz_string = (char*) "7.1";
389 var_Change( p_aout, "audio-device",
390 VLC_VAR_ADDCHOICE, &val, &text );
391 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
392 is_default_output_set = true;
393 msg_Dbg( p_aout, "device supports 7.1 channels" );
397 /* Test for 3 Front 2 Rear support */
398 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
399 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
401 if( p_aout->format.i_physical_channels == i_physical_channels )
403 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
404 p_aout->format.i_rate, true )
407 val.i_int = AOUT_VAR_3F2R;
408 text.psz_string = _("3 Front 2 Rear");
409 var_Change( p_aout, "audio-device",
410 VLC_VAR_ADDCHOICE, &val, &text );
411 if(!is_default_output_set)
413 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
414 is_default_output_set = true;
416 msg_Dbg( p_aout, "device supports 5 channels" );
420 /* Test for 2 Front 2 Rear support */
421 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
422 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
423 if( ( p_aout->format.i_physical_channels & i_physical_channels )
424 == i_physical_channels )
426 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
427 p_aout->format.i_rate, true )
430 val.i_int = AOUT_VAR_2F2R;
431 text.psz_string = _("2 Front 2 Rear");
432 var_Change( p_aout, "audio-device",
433 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;
439 msg_Dbg( p_aout, "device supports 4 channels" );
443 /* Test for stereo support */
444 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
445 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
446 p_aout->format.i_rate, true )
449 val.i_int = AOUT_VAR_STEREO;
450 text.psz_string = _("Stereo");
451 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
452 if(!is_default_output_set)
454 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
455 is_default_output_set = true;
456 msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
458 else msg_Dbg( p_aout, "device supports 2 channels" );
461 /* Test for mono support */
462 i_physical_channels = AOUT_CHAN_CENTER;
463 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
464 p_aout->format.i_rate, true )
467 val.i_int = AOUT_VAR_MONO;
468 text.psz_string = _("Mono");
469 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
470 msg_Dbg( p_aout, "device supports 1 channel" );
473 /* Check the speaker configuration to determine which channel config should
475 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->sys->p_dsobject,
476 &ui_speaker_config ) )
478 ui_speaker_config = DSSPEAKER_STEREO;
479 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
481 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
483 case DSSPEAKER_7POINT1:
484 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
485 val.i_int = AOUT_VAR_7_1;
487 case DSSPEAKER_5POINT1:
488 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
489 val.i_int = AOUT_VAR_5_1;
492 msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
493 val.i_int = AOUT_VAR_2F2R;
495 #if 0 /* Lots of people just get their settings wrong and complain that
496 * this is a problem with VLC so just don't ever set mono by default. */
498 val.i_int = AOUT_VAR_MONO;
501 case DSSPEAKER_SURROUND:
502 msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
503 case DSSPEAKER_STEREO:
504 msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
506 /* If nothing else is found, choose stereo output */
507 val.i_int = AOUT_VAR_STEREO;
511 /* Check if we want to override speaker config */
512 switch( p_aout->sys->i_speaker_setup )
514 case 0: /* Default value aka Windows default speaker setup */
517 msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
518 val.i_int = AOUT_VAR_MONO;
521 msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
522 val.i_int = AOUT_VAR_STEREO;
525 msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
526 val.i_int = AOUT_VAR_2F2R;
529 msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
530 val.i_int = AOUT_VAR_5_1;
533 msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
534 val.i_int = AOUT_VAR_7_1;
537 msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
541 var_Set( p_aout, "audio-device", val );
543 /* Test for SPDIF support */
544 if ( AOUT_FMT_SPDIF( &p_aout->format ) )
546 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
547 p_aout->format.i_physical_channels,
548 aout_FormatNbChannels( &p_aout->format ),
549 p_aout->format.i_rate,
550 AOUT_SPDIF_SIZE, true )
553 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
554 val.i_int = AOUT_VAR_SPDIF;
555 text.psz_string = _("A/52 over S/PDIF");
556 var_Change( p_aout, "audio-device",
557 VLC_VAR_ADDCHOICE, &val, &text );
558 if( var_InheritBool( p_aout, "spdif" ) )
559 var_Set( p_aout, "audio-device", val );
563 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
566 /* Probe() has failed. */
567 var_Destroy( p_aout, "audio-device" );
571 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
574 /*****************************************************************************
575 * Play: we'll start playing the directsound buffer here because at least here
576 * we know the first buffer has been put in the aout fifo and we also
578 *****************************************************************************/
579 static void Play( audio_output_t *p_aout, block_t *p_buffer,
580 mtime_t *restrict drift )
582 /* get the playing date of the first aout buffer */
583 p_aout->sys->notif.start_date = p_buffer->i_pts;
585 /* fill in the first samples (zeroes) */
586 FillBuffer( p_aout, 0, NULL );
588 /* wake up the audio output thread */
589 SetEvent( p_aout->sys->notif.event );
591 aout_PacketPlay( p_aout, p_buffer, drift );
592 p_aout->pf_play = aout_PacketPlay;
595 static int VolumeSet( audio_output_t *p_aout, float vol )
597 aout_sys_t *sys = p_aout->sys;
600 /* Convert UI volume to linear factor (cube) */
601 vol = vol * vol * vol;
603 /* millibels from linear amplification */
604 LONG mb = lroundf(2000.f * log10f(vol));
606 /* Clamp to allowed DirectSound range */
607 static_assert( DSBVOLUME_MIN < DSBVOLUME_MAX, "DSBVOLUME_* confused" );
608 if( mb >= DSBVOLUME_MAX )
613 if( mb <= DSBVOLUME_MIN )
617 if (!sys->volume.mute)
618 InterlockedExchange(&sys->volume.volume, mb);
620 /* Convert back to UI volume */
621 vol = cbrtf(powf(10.f, ((float)mb) / 2000.f));
622 aout_VolumeReport( p_aout, vol );
626 static int MuteSet( audio_output_t *p_aout, bool mute )
628 aout_sys_t *sys = p_aout->sys;
630 sys->volume.mute = mute;
631 InterlockedExchange(&sys->volume.volume,
632 mute ? DSBVOLUME_MIN : sys->volume.mb);
634 aout_MuteReport( p_aout, mute );
638 /*****************************************************************************
639 * CloseAudio: close the audio device
640 *****************************************************************************/
641 static void CloseAudio( vlc_object_t *p_this )
643 audio_output_t * p_aout = (audio_output_t *)p_this;
644 aout_sys_t *p_sys = p_aout->sys;
646 msg_Dbg( p_aout, "closing audio device" );
648 /* kill the position notification thread, if any */
649 if( p_sys->notif.event != NULL )
651 vlc_atomic_set(&p_aout->sys->notif.abort, 1);
652 /* wake up the audio thread if needed */
653 if( p_aout->pf_play == Play )
654 SetEvent( p_sys->notif.event );
656 vlc_join( p_sys->notif.thread, NULL );
657 CloseHandle( p_sys->notif.event );
658 aout_PacketDestroy( p_aout );
661 /* release the secondary buffer */
662 DestroyDSBuffer( p_aout );
664 /* finally release the DirectSound object */
665 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
667 /* free DSOUND.DLL */
668 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
670 free( p_aout->sys->p_device_guid );
674 /*****************************************************************************
675 * CallBackDirectSoundEnum: callback to enumerate available devices
676 *****************************************************************************/
677 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
678 LPCWSTR psz_mod, LPVOID _p_aout )
680 VLC_UNUSED( psz_mod );
682 audio_output_t *p_aout = (audio_output_t *)_p_aout;
684 char *psz_device = FromWide( psz_desc );
685 msg_Dbg( p_aout, "found device: %s", psz_device );
687 if( p_aout->sys->psz_device &&
688 !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
690 /* Use the device corresponding to psz_device */
691 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
692 *p_aout->sys->p_device_guid = *p_guid;
693 msg_Dbg( p_aout, "using device: %s", psz_device );
697 /* If no default device has been selected, chose the first one */
698 if( !p_aout->sys->psz_device && p_guid )
700 p_aout->sys->psz_device = strdup( psz_device );
701 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
702 *p_aout->sys->p_device_guid = *p_guid;
703 msg_Dbg( p_aout, "using device: %s", psz_device );
711 /*****************************************************************************
712 * InitDirectSound: handle all the gory details of DirectSound initialisation
713 *****************************************************************************/
714 static int InitDirectSound( audio_output_t *p_aout )
716 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
717 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
719 p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
720 if( p_aout->sys->hdsound_dll == NULL )
722 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
726 OurDirectSoundCreate = (void *)
727 GetProcAddress( p_aout->sys->hdsound_dll,
728 "DirectSoundCreate" );
729 if( OurDirectSoundCreate == NULL )
731 msg_Warn( p_aout, "GetProcAddress FAILED" );
735 /* Get DirectSoundEnumerate */
736 OurDirectSoundEnumerate = (void *)
737 GetProcAddress( p_aout->sys->hdsound_dll,
738 "DirectSoundEnumerateW" );
739 if( OurDirectSoundEnumerate )
741 p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device");
742 /* Attempt enumeration */
743 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
746 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
750 /* Create the direct sound object */
751 if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
752 &p_aout->sys->p_dsobject,
755 msg_Warn( p_aout, "cannot create a direct sound device" );
759 /* Set DirectSound Cooperative level, ie what control we want over Windows
760 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
761 * settings of the primary buffer, but also that only the sound of our
762 * application will be hearable when it will have the focus.
763 * !!! (this is not really working as intended yet because to set the
764 * cooperative level you need the window handle of your application, and
765 * I don't know of any easy way to get it. Especially since we might play
766 * sound without any video, and so what window handle should we use ???
767 * The hack for now is to use the Desktop window handle - it seems to be
769 if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
773 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
779 p_aout->sys->p_dsobject = NULL;
780 if( p_aout->sys->hdsound_dll )
782 FreeLibrary( p_aout->sys->hdsound_dll );
783 p_aout->sys->hdsound_dll = NULL;
789 /*****************************************************************************
790 * CreateDSBuffer: Creates a direct sound buffer of the required format.
791 *****************************************************************************
792 * This function creates the buffer we'll use to play audio.
793 * In DirectSound there are two kinds of buffers:
794 * - the primary buffer: which is the actual buffer that the soundcard plays
795 * - the secondary buffer(s): these buffers are the one actually used by
796 * applications and DirectSound takes care of mixing them into the primary.
798 * Once you create a secondary buffer, you cannot change its format anymore so
799 * you have to release the current one and create another.
800 *****************************************************************************/
801 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
802 int i_channels, int i_nb_channels, int i_rate,
803 int i_bytes_per_frame, bool b_probe )
805 WAVEFORMATEXTENSIBLE waveformat;
806 DSBUFFERDESC dsbdesc;
809 /* First set the sound buffer format */
810 waveformat.dwChannelMask = 0;
811 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
813 if( i_channels & pi_channels_src[i] )
814 waveformat.dwChannelMask |= pi_channels_in[i];
819 case VLC_CODEC_SPDIFL:
821 /* To prevent channel re-ordering */
822 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
823 waveformat.Format.wBitsPerSample = 16;
824 waveformat.Samples.wValidBitsPerSample =
825 waveformat.Format.wBitsPerSample;
826 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
827 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
831 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
832 waveformat.Samples.wValidBitsPerSample =
833 waveformat.Format.wBitsPerSample;
834 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
835 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
839 waveformat.Format.wBitsPerSample = 16;
840 waveformat.Samples.wValidBitsPerSample =
841 waveformat.Format.wBitsPerSample;
842 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
843 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
847 waveformat.Format.nChannels = i_nb_channels;
848 waveformat.Format.nSamplesPerSec = i_rate;
849 waveformat.Format.nBlockAlign =
850 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
851 waveformat.Format.nAvgBytesPerSec =
852 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
854 p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
855 p_aout->sys->i_channels = i_nb_channels;
857 /* Then fill in the direct sound descriptor */
858 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
859 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
860 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
861 | DSBCAPS_GLOBALFOCUS /* Allows background playing */
862 | DSBCAPS_CTRLVOLUME; /* Allows volume control */
864 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
865 if( i_nb_channels <= 2 )
867 waveformat.Format.cbSize = 0;
871 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
872 waveformat.Format.cbSize =
873 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
875 /* Needed for 5.1 on emu101k */
876 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
879 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
880 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
882 if FAILED( IDirectSound_CreateSoundBuffer(
883 p_aout->sys->p_dsobject, &dsbdesc,
884 &p_aout->sys->p_dsbuffer, NULL) )
886 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
888 /* Try without DSBCAPS_LOCHARDWARE */
889 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
890 if FAILED( IDirectSound_CreateSoundBuffer(
891 p_aout->sys->p_dsobject, &dsbdesc,
892 &p_aout->sys->p_dsbuffer, NULL) )
897 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
905 /* Stop here if we were just probing */
908 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
909 p_aout->sys->p_dsbuffer = NULL;
913 p_aout->sys->i_frame_size = i_bytes_per_frame;
914 p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
915 p_aout->sys->b_chan_reorder =
916 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
917 waveformat.dwChannelMask, i_nb_channels,
918 p_aout->sys->pi_chan_table );
920 if( p_aout->sys->b_chan_reorder )
922 msg_Dbg( p_aout, "channel reordering needed" );
928 /*****************************************************************************
929 * CreateDSBufferPCM: creates a PCM direct sound buffer.
930 *****************************************************************************
931 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
932 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
933 ****************************************************************************/
934 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
935 int i_channels, int i_nb_channels, int i_rate,
938 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
939 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
941 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
942 i_channels, i_nb_channels, i_rate,
943 FRAME_SIZE * 4 * i_nb_channels, b_probe )
946 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
947 i_channels, i_nb_channels, i_rate,
948 FRAME_SIZE * 2 * i_nb_channels, b_probe )
955 *i_format = VLC_CODEC_S16L;
961 *i_format = VLC_CODEC_FL32;
966 /*****************************************************************************
968 *****************************************************************************
969 * This function destroys the secondary buffer.
970 *****************************************************************************/
971 static void DestroyDSBuffer( audio_output_t *p_aout )
973 if( p_aout->sys->p_dsbuffer )
975 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
976 p_aout->sys->p_dsbuffer = NULL;
980 /*****************************************************************************
981 * FillBuffer: Fill in one of the direct sound frame buffers.
982 *****************************************************************************
983 * Returns VLC_SUCCESS on success.
984 *****************************************************************************/
985 static int FillBuffer( audio_output_t *p_aout, int i_frame, block_t *p_buffer )
987 aout_sys_t *p_sys = p_aout->sys;
988 notification_thread_t *p_notif = &p_sys->notif;
989 void *p_write_position, *p_wrap_around;
990 unsigned long l_bytes1, l_bytes2;
993 /* Before copying anything, we have to lock the buffer */
994 dsresult = IDirectSoundBuffer_Lock(
995 p_sys->p_dsbuffer, /* DS buffer */
996 i_frame * p_notif->i_frame_size, /* Start offset */
997 p_notif->i_frame_size, /* Number of bytes */
998 &p_write_position, /* Address of lock start */
999 &l_bytes1, /* Count of bytes locked before wrap around */
1000 &p_wrap_around, /* Buffer address (if wrap around) */
1001 &l_bytes2, /* Count of bytes after wrap around */
1003 if( dsresult == DSERR_BUFFERLOST )
1005 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
1006 dsresult = IDirectSoundBuffer_Lock(
1008 i_frame * p_notif->i_frame_size,
1009 p_notif->i_frame_size,
1016 if( dsresult != DS_OK )
1018 msg_Warn( p_aout, "cannot lock buffer" );
1019 if( p_buffer ) block_Release( p_buffer );
1020 return VLC_EGENERIC;
1023 if( p_buffer == NULL )
1025 memset( p_write_position, 0, l_bytes1 );
1029 if( p_sys->b_chan_reorder )
1031 /* Do the channel reordering here */
1032 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
1033 p_sys->i_channels, p_sys->pi_chan_table,
1034 p_sys->i_bits_per_sample );
1037 memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
1038 block_Release( p_buffer );
1041 /* Now the data has been copied, unlock the buffer */
1042 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1043 p_wrap_around, l_bytes2 );
1045 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1049 /*****************************************************************************
1050 * DirectSoundThread: this thread will capture play notification events.
1051 *****************************************************************************
1052 * We use this thread to emulate a callback mechanism. The thread probes for
1053 * event notification and fills up the DS secondary buffer when needed.
1054 *****************************************************************************/
1055 static void* DirectSoundThread( void *data )
1057 audio_output_t *p_aout = (audio_output_t *)data;
1058 notification_thread_t *p_notif = &p_aout->sys->notif;
1061 msg_Dbg( p_aout, "DirectSoundThread ready" );
1063 /* Wait here until Play() is called */
1064 WaitForSingleObject( p_notif->event, INFINITE );
1066 if( !vlc_atomic_get( &p_notif->abort) )
1069 mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1071 /* start playing the buffer */
1072 dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1075 DSBPLAY_LOOPING ); /* Flags */
1076 if( dsresult == DSERR_BUFFERLOST )
1078 IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1079 dsresult = IDirectSoundBuffer_Play(
1080 p_aout->sys->p_dsbuffer,
1083 DSBPLAY_LOOPING ); /* Flags */
1085 if( dsresult != DS_OK )
1087 msg_Err( p_aout, "cannot start playing buffer" );
1090 last_time = mdate();
1092 while( !vlc_atomic_get( &p_notif->abort ) )
1095 int l_queued = 0, l_free_slots;
1096 unsigned i_frame_siz = p_aout->sys->packet.samples;
1097 mtime_t mtime = mdate();
1100 /* Update volume if required */
1101 LONG volume = InterlockedExchange( &p_aout->sys->volume.volume, -1 );
1102 if( unlikely(volume != -1) )
1103 IDirectSoundBuffer_SetVolume( p_aout->sys->p_dsbuffer, volume );
1106 * Fill in as much audio data as we can in our circular buffer
1109 /* Find out current play position */
1110 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1111 p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1113 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1117 /* Detect underruns */
1118 if( l_queued && mtime - last_time >
1119 INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1121 msg_Dbg( p_aout, "detected underrun!" );
1125 /* Try to fill in as many frame buffers as possible */
1126 l_read /= (p_aout->format.i_bytes_per_frame /
1127 p_aout->format.i_frame_length);
1128 l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1129 if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1130 l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1132 for( i = 0; i < l_free_slots; i++ )
1134 block_t *p_buffer = aout_PacketNext( p_aout,
1135 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1136 p_aout->format.i_rate );
1138 /* If there is no audio data available and we have some buffered
1139 * already, then just wait for the next time */
1140 if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1142 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1143 p_buffer ) != VLC_SUCCESS ) break;
1146 /* Sleep a reasonable amount of time */
1147 l_queued += (i * i_frame_siz);
1148 msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1151 /* make sure the buffer isn't playing */
1152 IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1154 msg_Dbg( p_aout, "DirectSoundThread exiting" );
1158 /*****************************************************************************
1159 * CallBackConfigNBEnum: callback to get the number of available devices
1160 *****************************************************************************/
1161 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1162 LPCWSTR psz_mod, LPVOID p_nb )
1164 VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1166 int * a = (int *)p_nb;
1171 /*****************************************************************************
1172 * CallBackConfigEnum: callback to add available devices to the preferences list
1173 *****************************************************************************/
1174 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1175 LPCWSTR psz_mod, LPVOID _p_item )
1177 VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1179 module_config_t *p_item = (module_config_t *) _p_item;
1181 p_item->ppsz_list[p_item->i_list] = FromWide( psz_desc );
1182 p_item->ppsz_list_text[p_item->i_list] = FromWide( psz_desc );
1187 /*****************************************************************************
1188 * ReloadDirectXDevices: store the list of devices in preferences
1189 *****************************************************************************/
1190 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1191 vlc_value_t newval, vlc_value_t oldval, void *data )
1193 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1195 module_config_t *p_item = config_FindConfig( p_this, psz_name );
1196 if( !p_item ) return VLC_SUCCESS;
1198 /* Clear-up the current list */
1199 if( p_item->i_list )
1201 for( int i = 0; i < p_item->i_list; i++ )
1203 free((char *)(p_item->ppsz_list[i]) );
1204 free((char *)(p_item->ppsz_list_text[i]) );
1208 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
1210 HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1211 if( hdsound_dll == NULL )
1213 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1217 /* Get DirectSoundEnumerate */
1218 OurDirectSoundEnumerate = (void *)
1219 GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1221 if( OurDirectSoundEnumerate == NULL )
1225 OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1226 msg_Dbg(p_this,"found %d devices", nb_devices);
1228 p_item->ppsz_list = xrealloc( p_item->ppsz_list,
1229 nb_devices * sizeof(char *) );
1230 p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
1231 nb_devices * sizeof(char *) );
1234 OurDirectSoundEnumerate(CallBackConfigEnum, p_item);
1237 FreeLibrary(hdsound_dll);