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, block_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 p_aout->pf_volume_set = NULL;
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 );
556 /*****************************************************************************
557 * Play: we'll start playing the directsound buffer here because at least here
558 * we know the first buffer has been put in the aout fifo and we also
560 *****************************************************************************/
561 static void Play( audio_output_t *p_aout, block_t *p_buffer )
563 /* get the playing date of the first aout buffer */
564 p_aout->sys->notif.start_date = p_buffer->i_pts;
566 /* fill in the first samples (zeroes) */
567 FillBuffer( p_aout, 0, NULL );
569 /* wake up the audio output thread */
570 SetEvent( p_aout->sys->notif.event );
572 aout_PacketPlay( p_aout, p_buffer );
573 p_aout->pf_play = aout_PacketPlay;
576 /*****************************************************************************
577 * VolumeSet: change audio device volume
578 *****************************************************************************/
579 static int VolumeSet( audio_output_t *p_aout, float vol, bool mute )
581 aout_sys_t *sys = p_aout->sys;
583 /* Convert UI volume to linear factor (cube) */
584 vol = vol * vol * vol;
586 /* millibels from linear amplification */
587 LONG mb = lroundf(2000.f * log10f(vol));
589 /* Clamp to allowed DirectSound range */
590 static_assert( DSBVOLUME_MIN < DSBVOLUME_MAX, "DSBVOLUME_* confused" );
591 if( mb >= DSBVOLUME_MAX )
593 if( mb <= DSBVOLUME_MIN )
596 InterlockedExchange(&sys->volume, mute ? DSBVOLUME_MIN : mb);
598 /* Convert back to UI volume */
599 vol = cbrtf(powf(10.f, ((float)mb) / -2000.f));
600 aout_VolumeHardSet( p_aout, vol, mute );
604 /*****************************************************************************
605 * CloseAudio: close the audio device
606 *****************************************************************************/
607 static void CloseAudio( vlc_object_t *p_this )
609 audio_output_t * p_aout = (audio_output_t *)p_this;
610 aout_sys_t *p_sys = p_aout->sys;
612 msg_Dbg( p_aout, "closing audio device" );
614 /* kill the position notification thread, if any */
615 if( p_sys->notif.event != NULL )
617 vlc_atomic_set(&p_aout->sys->notif.abort, 1);
618 /* wake up the audio thread if needed */
619 if( p_aout->pf_play == Play )
620 SetEvent( p_sys->notif.event );
622 vlc_join( p_sys->notif.thread, NULL );
623 CloseHandle( p_sys->notif.event );
624 aout_PacketDestroy( p_aout );
627 /* release the secondary buffer */
628 DestroyDSBuffer( p_aout );
630 /* finally release the DirectSound object */
631 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
633 /* free DSOUND.DLL */
634 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
636 free( p_aout->sys->p_device_guid );
640 /*****************************************************************************
641 * CallBackDirectSoundEnum: callback to enumerate available devices
642 *****************************************************************************/
643 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
644 LPCWSTR psz_mod, LPVOID _p_aout )
646 VLC_UNUSED( psz_mod );
648 audio_output_t *p_aout = (audio_output_t *)_p_aout;
650 char *psz_device = FromWide( psz_desc );
651 msg_Dbg( p_aout, "found device: %s", psz_device );
653 if( p_aout->sys->psz_device &&
654 !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
656 /* Use the device corresponding to psz_device */
657 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
658 *p_aout->sys->p_device_guid = *p_guid;
659 msg_Dbg( p_aout, "using device: %s", psz_device );
663 /* If no default device has been selected, chose the first one */
664 if( !p_aout->sys->psz_device && p_guid )
666 p_aout->sys->psz_device = strdup( psz_device );
667 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
668 *p_aout->sys->p_device_guid = *p_guid;
669 msg_Dbg( p_aout, "using device: %s", psz_device );
677 /*****************************************************************************
678 * InitDirectSound: handle all the gory details of DirectSound initialisation
679 *****************************************************************************/
680 static int InitDirectSound( audio_output_t *p_aout )
682 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
683 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
685 p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
686 if( p_aout->sys->hdsound_dll == NULL )
688 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
692 OurDirectSoundCreate = (void *)
693 GetProcAddress( p_aout->sys->hdsound_dll,
694 "DirectSoundCreate" );
695 if( OurDirectSoundCreate == NULL )
697 msg_Warn( p_aout, "GetProcAddress FAILED" );
701 /* Get DirectSoundEnumerate */
702 OurDirectSoundEnumerate = (void *)
703 GetProcAddress( p_aout->sys->hdsound_dll,
704 "DirectSoundEnumerateW" );
705 if( OurDirectSoundEnumerate )
707 p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device");
708 /* Attempt enumeration */
709 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
712 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
716 /* Create the direct sound object */
717 if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
718 &p_aout->sys->p_dsobject,
721 msg_Warn( p_aout, "cannot create a direct sound device" );
725 /* Set DirectSound Cooperative level, ie what control we want over Windows
726 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
727 * settings of the primary buffer, but also that only the sound of our
728 * application will be hearable when it will have the focus.
729 * !!! (this is not really working as intended yet because to set the
730 * cooperative level you need the window handle of your application, and
731 * I don't know of any easy way to get it. Especially since we might play
732 * sound without any video, and so what window handle should we use ???
733 * The hack for now is to use the Desktop window handle - it seems to be
735 if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
739 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
745 p_aout->sys->p_dsobject = NULL;
746 if( p_aout->sys->hdsound_dll )
748 FreeLibrary( p_aout->sys->hdsound_dll );
749 p_aout->sys->hdsound_dll = NULL;
755 /*****************************************************************************
756 * CreateDSBuffer: Creates a direct sound buffer of the required format.
757 *****************************************************************************
758 * This function creates the buffer we'll use to play audio.
759 * In DirectSound there are two kinds of buffers:
760 * - the primary buffer: which is the actual buffer that the soundcard plays
761 * - the secondary buffer(s): these buffers are the one actually used by
762 * applications and DirectSound takes care of mixing them into the primary.
764 * Once you create a secondary buffer, you cannot change its format anymore so
765 * you have to release the current one and create another.
766 *****************************************************************************/
767 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
768 int i_channels, int i_nb_channels, int i_rate,
769 int i_bytes_per_frame, bool b_probe )
771 WAVEFORMATEXTENSIBLE waveformat;
772 DSBUFFERDESC dsbdesc;
775 /* First set the sound buffer format */
776 waveformat.dwChannelMask = 0;
777 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
779 if( i_channels & pi_channels_src[i] )
780 waveformat.dwChannelMask |= pi_channels_in[i];
785 case VLC_CODEC_SPDIFL:
787 /* To prevent channel re-ordering */
788 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
789 waveformat.Format.wBitsPerSample = 16;
790 waveformat.Samples.wValidBitsPerSample =
791 waveformat.Format.wBitsPerSample;
792 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
793 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
797 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
798 waveformat.Samples.wValidBitsPerSample =
799 waveformat.Format.wBitsPerSample;
800 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
801 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
805 waveformat.Format.wBitsPerSample = 16;
806 waveformat.Samples.wValidBitsPerSample =
807 waveformat.Format.wBitsPerSample;
808 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
809 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
813 waveformat.Format.nChannels = i_nb_channels;
814 waveformat.Format.nSamplesPerSec = i_rate;
815 waveformat.Format.nBlockAlign =
816 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
817 waveformat.Format.nAvgBytesPerSec =
818 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
820 p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
821 p_aout->sys->i_channels = i_nb_channels;
823 /* Then fill in the direct sound descriptor */
824 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
825 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
826 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
827 | DSBCAPS_GLOBALFOCUS /* Allows background playing */
828 | DSBCAPS_CTRLVOLUME; /* Allows volume control */
830 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
831 if( i_nb_channels <= 2 )
833 waveformat.Format.cbSize = 0;
837 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
838 waveformat.Format.cbSize =
839 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
841 /* Needed for 5.1 on emu101k */
842 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
845 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
846 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
848 if FAILED( IDirectSound_CreateSoundBuffer(
849 p_aout->sys->p_dsobject, &dsbdesc,
850 &p_aout->sys->p_dsbuffer, NULL) )
852 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
854 /* Try without DSBCAPS_LOCHARDWARE */
855 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
856 if FAILED( IDirectSound_CreateSoundBuffer(
857 p_aout->sys->p_dsobject, &dsbdesc,
858 &p_aout->sys->p_dsbuffer, NULL) )
863 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
871 /* Stop here if we were just probing */
874 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
875 p_aout->sys->p_dsbuffer = NULL;
879 p_aout->sys->i_frame_size = i_bytes_per_frame;
880 p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
881 p_aout->sys->b_chan_reorder =
882 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
883 waveformat.dwChannelMask, i_nb_channels,
884 p_aout->sys->pi_chan_table );
886 if( p_aout->sys->b_chan_reorder )
888 msg_Dbg( p_aout, "channel reordering needed" );
894 /*****************************************************************************
895 * CreateDSBufferPCM: creates a PCM direct sound buffer.
896 *****************************************************************************
897 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
898 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
899 ****************************************************************************/
900 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
901 int i_channels, int i_nb_channels, int i_rate,
904 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
905 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
907 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
908 i_channels, i_nb_channels, i_rate,
909 FRAME_SIZE * 4 * i_nb_channels, b_probe )
912 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
913 i_channels, i_nb_channels, i_rate,
914 FRAME_SIZE * 2 * i_nb_channels, b_probe )
921 *i_format = VLC_CODEC_S16L;
927 *i_format = VLC_CODEC_FL32;
932 /*****************************************************************************
934 *****************************************************************************
935 * This function destroys the secondary buffer.
936 *****************************************************************************/
937 static void DestroyDSBuffer( audio_output_t *p_aout )
939 if( p_aout->sys->p_dsbuffer )
941 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
942 p_aout->sys->p_dsbuffer = NULL;
946 /*****************************************************************************
947 * FillBuffer: Fill in one of the direct sound frame buffers.
948 *****************************************************************************
949 * Returns VLC_SUCCESS on success.
950 *****************************************************************************/
951 static int FillBuffer( audio_output_t *p_aout, int i_frame, block_t *p_buffer )
953 aout_sys_t *p_sys = p_aout->sys;
954 notification_thread_t *p_notif = &p_sys->notif;
955 void *p_write_position, *p_wrap_around;
956 unsigned long l_bytes1, l_bytes2;
959 /* Before copying anything, we have to lock the buffer */
960 dsresult = IDirectSoundBuffer_Lock(
961 p_sys->p_dsbuffer, /* DS buffer */
962 i_frame * p_notif->i_frame_size, /* Start offset */
963 p_notif->i_frame_size, /* Number of bytes */
964 &p_write_position, /* Address of lock start */
965 &l_bytes1, /* Count of bytes locked before wrap around */
966 &p_wrap_around, /* Buffer address (if wrap around) */
967 &l_bytes2, /* Count of bytes after wrap around */
969 if( dsresult == DSERR_BUFFERLOST )
971 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
972 dsresult = IDirectSoundBuffer_Lock(
974 i_frame * p_notif->i_frame_size,
975 p_notif->i_frame_size,
982 if( dsresult != DS_OK )
984 msg_Warn( p_aout, "cannot lock buffer" );
985 if( p_buffer ) block_Release( p_buffer );
989 if( p_buffer == NULL )
991 memset( p_write_position, 0, l_bytes1 );
995 if( p_sys->b_chan_reorder )
997 /* Do the channel reordering here */
998 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
999 p_sys->i_channels, p_sys->pi_chan_table,
1000 p_sys->i_bits_per_sample );
1003 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
1004 block_Release( p_buffer );
1007 /* Now the data has been copied, unlock the buffer */
1008 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1009 p_wrap_around, l_bytes2 );
1011 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1015 /*****************************************************************************
1016 * DirectSoundThread: this thread will capture play notification events.
1017 *****************************************************************************
1018 * We use this thread to emulate a callback mechanism. The thread probes for
1019 * event notification and fills up the DS secondary buffer when needed.
1020 *****************************************************************************/
1021 static void* DirectSoundThread( void *data )
1023 audio_output_t *p_aout = (audio_output_t *)data;
1024 notification_thread_t *p_notif = &p_aout->sys->notif;
1027 msg_Dbg( p_aout, "DirectSoundThread ready" );
1029 /* Wait here until Play() is called */
1030 WaitForSingleObject( p_notif->event, INFINITE );
1032 if( !vlc_atomic_get( &p_notif->abort) )
1035 mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1037 /* start playing the buffer */
1038 dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1041 DSBPLAY_LOOPING ); /* Flags */
1042 if( dsresult == DSERR_BUFFERLOST )
1044 IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1045 dsresult = IDirectSoundBuffer_Play(
1046 p_aout->sys->p_dsbuffer,
1049 DSBPLAY_LOOPING ); /* Flags */
1051 if( dsresult != DS_OK )
1053 msg_Err( p_aout, "cannot start playing buffer" );
1056 last_time = mdate();
1058 while( !vlc_atomic_get( &p_notif->abort ) )
1061 int l_queued = 0, l_free_slots;
1062 unsigned i_frame_siz = p_aout->sys->packet.samples;
1063 mtime_t mtime = mdate();
1066 /* Update volume if required */
1067 LONG volume = InterlockedExchange( &p_aout->sys->volume, -1 );
1068 if( unlikely(volume != -1) )
1069 IDirectSoundBuffer_SetVolume( p_aout->sys->p_dsbuffer, volume );
1072 * Fill in as much audio data as we can in our circular buffer
1075 /* Find out current play position */
1076 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1077 p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1079 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1083 /* Detect underruns */
1084 if( l_queued && mtime - last_time >
1085 INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1087 msg_Dbg( p_aout, "detected underrun!" );
1091 /* Try to fill in as many frame buffers as possible */
1092 l_read /= (p_aout->format.i_bytes_per_frame /
1093 p_aout->format.i_frame_length);
1094 l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1095 if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1096 l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1098 for( i = 0; i < l_free_slots; i++ )
1100 block_t *p_buffer = aout_PacketNext( p_aout,
1101 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1102 p_aout->format.i_rate );
1104 /* If there is no audio data available and we have some buffered
1105 * already, then just wait for the next time */
1106 if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1108 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1109 p_buffer ) != VLC_SUCCESS ) break;
1112 /* Sleep a reasonable amount of time */
1113 l_queued += (i * i_frame_siz);
1114 msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1117 /* make sure the buffer isn't playing */
1118 IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1120 msg_Dbg( p_aout, "DirectSoundThread exiting" );
1124 /*****************************************************************************
1125 * CallBackConfigNBEnum: callback to get the number of available devices
1126 *****************************************************************************/
1127 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1128 LPCWSTR psz_mod, LPVOID p_nb )
1130 VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1132 int * a = (int *)p_nb;
1137 /*****************************************************************************
1138 * CallBackConfigEnum: callback to add available devices to the preferences list
1139 *****************************************************************************/
1140 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1141 LPCWSTR psz_mod, LPVOID _p_item )
1143 VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1145 module_config_t *p_item = (module_config_t *) _p_item;
1147 p_item->ppsz_list[p_item->i_list] = FromWide( psz_desc );
1148 p_item->ppsz_list_text[p_item->i_list] = FromWide( psz_desc );
1153 /*****************************************************************************
1154 * ReloadDirectXDevices: store the list of devices in preferences
1155 *****************************************************************************/
1156 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1157 vlc_value_t newval, vlc_value_t oldval, void *data )
1159 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1161 module_config_t *p_item = config_FindConfig( p_this, psz_name );
1162 if( !p_item ) return VLC_SUCCESS;
1164 /* Clear-up the current list */
1165 if( p_item->i_list )
1167 for( int i = 0; i < p_item->i_list; i++ )
1169 free((char *)(p_item->ppsz_list[i]) );
1170 free((char *)(p_item->ppsz_list_text[i]) );
1174 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
1176 HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1177 if( hdsound_dll == NULL )
1179 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1183 /* Get DirectSoundEnumerate */
1184 OurDirectSoundEnumerate = (void *)
1185 GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1187 if( OurDirectSoundEnumerate == NULL )
1191 OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1192 msg_Dbg(p_this,"found %d devices", nb_devices);
1194 p_item->ppsz_list = xrealloc( p_item->ppsz_list,
1195 nb_devices * sizeof(char *) );
1196 p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
1197 nb_devices * sizeof(char *) );
1200 OurDirectSoundEnumerate(CallBackConfigEnum, p_item);
1203 FreeLibrary(hdsound_dll);