1 /*****************************************************************************
2 * directx.c: Windows DirectX audio output method
3 *****************************************************************************
4 * Copyright (C) 2001-2009 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_charset.h>
36 #include <vlc_atomic.h>
38 #include "windows_audio_common.h"
40 #define FRAME_SIZE ((int)p_aout->format.i_rate/20) /* Size in samples */
42 /*****************************************************************************
43 * notification_thread_t: DirectX event thread
44 *****************************************************************************/
45 typedef struct notification_thread_t
47 audio_output_t *p_aout;
48 int i_frame_size; /* size in bytes of one frame */
49 int i_write_slot; /* current write position in our circular buffer */
57 } notification_thread_t;
59 /*****************************************************************************
60 * aout_sys_t: directx audio output method descriptor
61 *****************************************************************************
62 * This structure is part of the audio output thread descriptor.
63 * It describes the direct sound specific properties of an audio device.
64 *****************************************************************************/
68 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
70 char * psz_device; /* user defined device name */
73 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
74 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
75 * takes care of mixing all the
76 * secondary buffers into the primary) */
78 notification_thread_t *p_notif; /* DirectSoundThread id */
80 int i_frame_size; /* Size in bytes of one frame */
82 int i_speaker_setup; /* Speaker setup override */
84 bool b_chan_reorder; /* do we need channel reordering */
85 int pi_chan_table[AOUT_CHAN_MAX];
86 uint32_t i_channel_mask;
87 uint32_t i_bits_per_sample;
91 /*****************************************************************************
93 *****************************************************************************/
94 static int OpenAudio ( vlc_object_t * );
95 static void CloseAudio ( vlc_object_t * );
96 static void Play ( audio_output_t *, block_t * );
99 static void Probe ( audio_output_t * );
100 static int InitDirectSound ( audio_output_t * );
101 static int CreateDSBuffer ( audio_output_t *, int, int, int, int, int, bool );
102 static int CreateDSBufferPCM ( audio_output_t *, vlc_fourcc_t*, int, int, int, bool );
103 static void DestroyDSBuffer ( audio_output_t * );
104 static void* DirectSoundThread( void * );
105 static int FillBuffer ( audio_output_t *, int, aout_buffer_t * );
107 static int ReloadDirectXDevices( vlc_object_t *, char const *,
108 vlc_value_t, vlc_value_t, void * );
110 /* Speaker setup override options list */
111 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
112 "Quad", "5.1", "7.1" };
113 static const char *const ppsz_adev[] = {"default", };
114 static const char *const ppsz_adev_text[] = {"default", };
116 /*****************************************************************************
118 *****************************************************************************/
119 #define DEVICE_TEXT N_("Output device")
120 #define DEVICE_LONGTEXT N_("Select your audio output device")
122 #define SPEAKER_TEXT N_("Speaker configuration")
123 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
124 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
127 set_description( N_("DirectX audio output") )
128 set_shortname( "DirectX" )
129 set_capability( "audio output", 100 )
130 set_category( CAT_AUDIO )
131 set_subcategory( SUBCAT_AUDIO_AOUT )
132 add_shortcut( "directx", "directsound" )
134 add_string( "directx-audio-device-name", "default",
135 DEVICE_TEXT, DEVICE_LONGTEXT, false )
136 add_deprecated_alias( "directx-audio-device" ) /* Since 1.1.0 */
137 change_string_list( ppsz_adev, ppsz_adev_text, ReloadDirectXDevices )
138 change_action_add( ReloadDirectXDevices, N_("Refresh list") )
139 add_bool( "directx-audio-float32", false, FLOAT_TEXT,
140 FLOAT_LONGTEXT, true )
141 add_string( "directx-audio-speaker", "Windows default",
142 SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
143 change_string_list( speaker_list, 0, 0 )
145 set_callbacks( OpenAudio, CloseAudio )
148 /*****************************************************************************
149 * OpenAudio: open the audio device
150 *****************************************************************************
151 * This function opens and setups Direct Sound.
152 *****************************************************************************/
153 static int OpenAudio( vlc_object_t *p_this )
155 audio_output_t * p_aout = (audio_output_t *)p_this;
160 const char * const * ppsz_compare = speaker_list;
162 msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
164 /* Allocate structure */
165 p_aout->sys = malloc( sizeof( aout_sys_t ) );
166 if( p_aout->sys == NULL )
169 /* Initialize some variables */
170 p_aout->sys->p_dsobject = NULL;
171 p_aout->sys->p_dsbuffer = NULL;
172 p_aout->sys->p_notif = NULL;
174 p_aout->pf_play = Play;
175 p_aout->pf_pause = aout_PacketPause;
176 p_aout->pf_flush = aout_PacketFlush;
177 aout_VolumeSoftInit( p_aout );
179 /* Retrieve config values */
180 var_Create( p_aout, "directx-audio-float32",
181 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
182 psz_speaker = var_CreateGetString( p_aout, "directx-audio-speaker" );
184 while ( *ppsz_compare != NULL )
186 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
193 if ( *ppsz_compare == NULL )
195 msg_Err( p_aout, "(%s) isn't valid speaker setup option", psz_speaker );
196 msg_Err( p_aout, "Defaulting to Windows default speaker config");
200 p_aout->sys->i_speaker_setup = i;
202 p_aout->sys->p_device_guid = 0;
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 /* Probe() has 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" );
244 aout_PacketInit( p_aout, &p_aout->sys->packet, A52_FRAME_NB );
245 aout_VolumeNoneInit( p_aout );
249 if( val.i_int == AOUT_VAR_5_1 )
251 p_aout->format.i_physical_channels
252 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
253 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
256 else if( val.i_int == AOUT_VAR_7_1 )
258 p_aout->format.i_physical_channels
259 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
260 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
261 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
264 else if( val.i_int == AOUT_VAR_3F2R )
266 p_aout->format.i_physical_channels
267 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
268 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
270 else if( val.i_int == AOUT_VAR_2F2R )
272 p_aout->format.i_physical_channels
273 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
274 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
276 else if( val.i_int == AOUT_VAR_MONO )
278 p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
282 p_aout->format.i_physical_channels
283 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
286 if( CreateDSBufferPCM( p_aout, &p_aout->format.i_format,
287 p_aout->format.i_physical_channels,
288 aout_FormatNbChannels( &p_aout->format ),
289 p_aout->format.i_rate, false )
292 msg_Err( p_aout, "cannot open directx audio device" );
297 /* Calculate the frame size in bytes */
298 aout_FormatPrepare( &p_aout->format );
299 aout_PacketInit( p_aout, &p_aout->sys->packet, FRAME_SIZE );
300 aout_VolumeSoftInit( p_aout );
303 /* Now we need to setup our DirectSound play notification structure */
304 p_aout->sys->p_notif = calloc( 1, sizeof( *p_aout->sys->p_notif ) );
305 p_aout->sys->p_notif->p_aout = p_aout;
307 vlc_atomic_set(&p_aout->sys->p_notif->abort, 0);
308 p_aout->sys->p_notif->event = CreateEvent( 0, FALSE, FALSE, 0 );
309 p_aout->sys->p_notif->i_frame_size = p_aout->sys->i_frame_size;
311 /* then launch the notification thread */
312 msg_Dbg( p_aout, "creating DirectSoundThread" );
313 if( vlc_clone( &p_aout->sys->p_notif->thread,
314 DirectSoundThread, p_aout->sys->p_notif,
315 VLC_THREAD_PRIORITY_HIGHEST ) )
317 msg_Err( p_aout, "cannot create DirectSoundThread" );
318 CloseHandle( p_aout->sys->p_notif->event );
319 free( p_aout->sys->p_notif );
320 p_aout->sys->p_notif = NULL;
327 CloseAudio( VLC_OBJECT(p_aout) );
331 /*****************************************************************************
332 * Probe: probe the audio device for available formats and channels
333 *****************************************************************************/
334 static void Probe( audio_output_t * p_aout )
336 vlc_value_t val, text;
337 vlc_fourcc_t i_format;
338 unsigned int i_physical_channels;
339 DWORD ui_speaker_config;
340 bool is_default_output_set = false;
342 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
343 text.psz_string = _("Audio Device");
344 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
346 /* Test for 5.1 support */
347 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
348 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
349 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
350 if( p_aout->format.i_physical_channels == i_physical_channels )
352 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
353 p_aout->format.i_rate, true )
356 val.i_int = AOUT_VAR_5_1;
357 text.psz_string = (char*) "5.1";
358 var_Change( p_aout, "audio-device",
359 VLC_VAR_ADDCHOICE, &val, &text );
360 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
361 is_default_output_set = true;
362 msg_Dbg( p_aout, "device supports 5.1 channels" );
366 /* Test for 7.1 support */
367 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
368 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
369 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
370 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
371 if( p_aout->format.i_physical_channels == i_physical_channels )
373 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
374 p_aout->format.i_rate, true )
377 val.i_int = AOUT_VAR_7_1;
378 text.psz_string = (char*) "7.1";
379 var_Change( p_aout, "audio-device",
380 VLC_VAR_ADDCHOICE, &val, &text );
381 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
382 is_default_output_set = true;
383 msg_Dbg( p_aout, "device supports 7.1 channels" );
387 /* Test for 3 Front 2 Rear support */
388 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
389 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
391 if( p_aout->format.i_physical_channels == i_physical_channels )
393 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
394 p_aout->format.i_rate, true )
397 val.i_int = AOUT_VAR_3F2R;
398 text.psz_string = _("3 Front 2 Rear");
399 var_Change( p_aout, "audio-device",
400 VLC_VAR_ADDCHOICE, &val, &text );
401 if(!is_default_output_set)
403 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
404 is_default_output_set = true;
406 msg_Dbg( p_aout, "device supports 5 channels" );
410 /* Test for 2 Front 2 Rear support */
411 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
412 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
413 if( ( p_aout->format.i_physical_channels & i_physical_channels )
414 == i_physical_channels )
416 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
417 p_aout->format.i_rate, true )
420 val.i_int = AOUT_VAR_2F2R;
421 text.psz_string = _("2 Front 2 Rear");
422 var_Change( p_aout, "audio-device",
423 VLC_VAR_ADDCHOICE, &val, &text );
424 if(!is_default_output_set)
426 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
427 is_default_output_set = true;
429 msg_Dbg( p_aout, "device supports 4 channels" );
433 /* Test for stereo support */
434 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
435 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
436 p_aout->format.i_rate, true )
439 val.i_int = AOUT_VAR_STEREO;
440 text.psz_string = _("Stereo");
441 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
442 if(!is_default_output_set)
444 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
445 is_default_output_set = true;
446 msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
448 else msg_Dbg( p_aout, "device supports 2 channels" );
451 /* Test for mono support */
452 i_physical_channels = AOUT_CHAN_CENTER;
453 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
454 p_aout->format.i_rate, true )
457 val.i_int = AOUT_VAR_MONO;
458 text.psz_string = _("Mono");
459 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
460 msg_Dbg( p_aout, "device supports 1 channel" );
463 /* Check the speaker configuration to determine which channel config should
465 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->sys->p_dsobject,
466 &ui_speaker_config ) )
468 ui_speaker_config = DSSPEAKER_STEREO;
469 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
471 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
473 case DSSPEAKER_7POINT1:
474 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
475 val.i_int = AOUT_VAR_7_1;
477 case DSSPEAKER_5POINT1:
478 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
479 val.i_int = AOUT_VAR_5_1;
482 msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
483 val.i_int = AOUT_VAR_2F2R;
485 #if 0 /* Lots of people just get their settings wrong and complain that
486 * this is a problem with VLC so just don't ever set mono by default. */
488 val.i_int = AOUT_VAR_MONO;
491 case DSSPEAKER_SURROUND:
492 msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
493 case DSSPEAKER_STEREO:
494 msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
496 /* If nothing else is found, choose stereo output */
497 val.i_int = AOUT_VAR_STEREO;
501 /* Check if we want to override speaker config */
502 switch( p_aout->sys->i_speaker_setup )
504 case 0: /* Default value aka Windows default speaker setup */
507 msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
508 val.i_int = AOUT_VAR_MONO;
511 msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
512 val.i_int = AOUT_VAR_STEREO;
515 msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
516 val.i_int = AOUT_VAR_2F2R;
519 msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
520 val.i_int = AOUT_VAR_5_1;
523 msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
524 val.i_int = AOUT_VAR_7_1;
527 msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
531 var_Set( p_aout, "audio-device", val );
533 /* Test for SPDIF support */
534 if ( AOUT_FMT_NON_LINEAR( &p_aout->format ) )
536 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
537 p_aout->format.i_physical_channels,
538 aout_FormatNbChannels( &p_aout->format ),
539 p_aout->format.i_rate,
540 AOUT_SPDIF_SIZE, true )
543 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
544 val.i_int = AOUT_VAR_SPDIF;
545 text.psz_string = _("A/52 over S/PDIF");
546 var_Change( p_aout, "audio-device",
547 VLC_VAR_ADDCHOICE, &val, &text );
548 if( var_InheritBool( p_aout, "spdif" ) )
549 var_Set( p_aout, "audio-device", val );
553 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
556 /* Probe() has failed. */
557 var_Destroy( p_aout, "audio-device" );
561 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
562 var_TriggerCallback( p_aout, "intf-change" );
565 /*****************************************************************************
566 * Play: we'll start playing the directsound buffer here because at least here
567 * we know the first buffer has been put in the aout fifo and we also
569 *****************************************************************************/
570 static void Play( audio_output_t *p_aout, block_t *p_buffer )
572 /* get the playing date of the first aout buffer */
573 p_aout->sys->p_notif->start_date = p_buffer->i_pts;
575 /* fill in the first samples (zeroes) */
576 FillBuffer( p_aout, 0, NULL );
578 /* wake up the audio output thread */
579 SetEvent( p_aout->sys->p_notif->event );
581 aout_PacketPlay( p_aout, p_buffer );
582 p_aout->pf_play = aout_PacketPlay;
585 /*****************************************************************************
586 * CloseAudio: close the audio device
587 *****************************************************************************/
588 static void CloseAudio( vlc_object_t *p_this )
590 audio_output_t * p_aout = (audio_output_t *)p_this;
591 aout_sys_t *p_sys = p_aout->sys;
593 msg_Dbg( p_aout, "closing audio device" );
595 /* kill the position notification thread, if any */
598 vlc_atomic_set(&p_aout->sys->p_notif->abort, 1);
599 /* wake up the audio thread if needed */
600 if( p_aout->pf_play == Play )
601 SetEvent( p_sys->p_notif->event );
603 vlc_join( p_sys->p_notif->thread, NULL );
604 free( p_sys->p_notif );
607 /* release the secondary buffer */
608 DestroyDSBuffer( p_aout );
610 /* finally release the DirectSound object */
611 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
613 /* free DSOUND.DLL */
614 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
616 free( p_aout->sys->p_device_guid );
617 aout_PacketDestroy( p_aout );
621 /*****************************************************************************
622 * CallBackDirectSoundEnum: callback to enumerate available devices
623 *****************************************************************************/
624 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
625 LPCWSTR psz_mod, LPVOID _p_aout )
627 VLC_UNUSED( psz_mod );
629 audio_output_t *p_aout = (audio_output_t *)_p_aout;
631 char *psz_device = FromWide( psz_desc );
632 msg_Dbg( p_aout, "found device: %s", psz_device );
634 if( p_aout->sys->psz_device &&
635 !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
637 /* Use the device corresponding to psz_device */
638 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
639 *p_aout->sys->p_device_guid = *p_guid;
640 msg_Dbg( p_aout, "using device: %s", psz_device );
644 /* If no default device has been selected, chose the first one */
645 if( !p_aout->sys->psz_device && p_guid )
647 p_aout->sys->psz_device = strdup( psz_device );
648 p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
649 *p_aout->sys->p_device_guid = *p_guid;
650 msg_Dbg( p_aout, "using device: %s", psz_device );
658 /*****************************************************************************
659 * InitDirectSound: handle all the gory details of DirectSound initialisation
660 *****************************************************************************/
661 static int InitDirectSound( audio_output_t *p_aout )
663 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
664 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
666 p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
667 if( p_aout->sys->hdsound_dll == NULL )
669 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
673 OurDirectSoundCreate = (void *)
674 GetProcAddress( p_aout->sys->hdsound_dll,
675 "DirectSoundCreate" );
676 if( OurDirectSoundCreate == NULL )
678 msg_Warn( p_aout, "GetProcAddress FAILED" );
682 /* Get DirectSoundEnumerate */
683 OurDirectSoundEnumerate = (void *)
684 GetProcAddress( p_aout->sys->hdsound_dll,
685 "DirectSoundEnumerateW" );
686 if( OurDirectSoundEnumerate )
688 p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device-name");
689 /* Attempt enumeration */
690 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
693 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
697 /* Create the direct sound object */
698 if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
699 &p_aout->sys->p_dsobject,
702 msg_Warn( p_aout, "cannot create a direct sound device" );
706 /* Set DirectSound Cooperative level, ie what control we want over Windows
707 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
708 * settings of the primary buffer, but also that only the sound of our
709 * application will be hearable when it will have the focus.
710 * !!! (this is not really working as intended yet because to set the
711 * cooperative level you need the window handle of your application, and
712 * I don't know of any easy way to get it. Especially since we might play
713 * sound without any video, and so what window handle should we use ???
714 * The hack for now is to use the Desktop window handle - it seems to be
716 if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
720 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
726 p_aout->sys->p_dsobject = NULL;
727 if( p_aout->sys->hdsound_dll )
729 FreeLibrary( p_aout->sys->hdsound_dll );
730 p_aout->sys->hdsound_dll = NULL;
736 /*****************************************************************************
737 * CreateDSBuffer: Creates a direct sound buffer of the required format.
738 *****************************************************************************
739 * This function creates the buffer we'll use to play audio.
740 * In DirectSound there are two kinds of buffers:
741 * - the primary buffer: which is the actual buffer that the soundcard plays
742 * - the secondary buffer(s): these buffers are the one actually used by
743 * applications and DirectSound takes care of mixing them into the primary.
745 * Once you create a secondary buffer, you cannot change its format anymore so
746 * you have to release the current one and create another.
747 *****************************************************************************/
748 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
749 int i_channels, int i_nb_channels, int i_rate,
750 int i_bytes_per_frame, bool b_probe )
752 WAVEFORMATEXTENSIBLE waveformat;
753 DSBUFFERDESC dsbdesc;
756 /* First set the sound buffer format */
757 waveformat.dwChannelMask = 0;
758 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
760 if( i_channels & pi_channels_src[i] )
761 waveformat.dwChannelMask |= pi_channels_in[i];
766 case VLC_CODEC_SPDIFL:
768 /* To prevent channel re-ordering */
769 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
770 waveformat.Format.wBitsPerSample = 16;
771 waveformat.Samples.wValidBitsPerSample =
772 waveformat.Format.wBitsPerSample;
773 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
774 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
778 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
779 waveformat.Samples.wValidBitsPerSample =
780 waveformat.Format.wBitsPerSample;
781 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
782 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
786 waveformat.Format.wBitsPerSample = 16;
787 waveformat.Samples.wValidBitsPerSample =
788 waveformat.Format.wBitsPerSample;
789 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
790 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
794 waveformat.Format.nChannels = i_nb_channels;
795 waveformat.Format.nSamplesPerSec = i_rate;
796 waveformat.Format.nBlockAlign =
797 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
798 waveformat.Format.nAvgBytesPerSec =
799 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
801 p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
802 p_aout->sys->i_channels = i_nb_channels;
804 /* Then fill in the direct sound descriptor */
805 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
806 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
807 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
808 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
810 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
811 if( i_nb_channels <= 2 )
813 waveformat.Format.cbSize = 0;
817 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
818 waveformat.Format.cbSize =
819 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
821 /* Needed for 5.1 on emu101k */
822 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
825 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
826 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
828 if FAILED( IDirectSound_CreateSoundBuffer(
829 p_aout->sys->p_dsobject, &dsbdesc,
830 &p_aout->sys->p_dsbuffer, NULL) )
832 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
834 /* Try without DSBCAPS_LOCHARDWARE */
835 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
836 if FAILED( IDirectSound_CreateSoundBuffer(
837 p_aout->sys->p_dsobject, &dsbdesc,
838 &p_aout->sys->p_dsbuffer, NULL) )
843 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
851 /* Stop here if we were just probing */
854 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
855 p_aout->sys->p_dsbuffer = NULL;
859 p_aout->sys->i_frame_size = i_bytes_per_frame;
860 p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
861 p_aout->sys->b_chan_reorder =
862 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
863 waveformat.dwChannelMask, i_nb_channels,
864 p_aout->sys->pi_chan_table );
866 if( p_aout->sys->b_chan_reorder )
868 msg_Dbg( p_aout, "channel reordering needed" );
874 /*****************************************************************************
875 * CreateDSBufferPCM: creates a PCM direct sound buffer.
876 *****************************************************************************
877 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
878 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
879 ****************************************************************************/
880 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
881 int i_channels, int i_nb_channels, int i_rate,
884 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
885 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
887 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
888 i_channels, i_nb_channels, i_rate,
889 FRAME_SIZE * 4 * i_nb_channels, b_probe )
892 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
893 i_channels, i_nb_channels, i_rate,
894 FRAME_SIZE * 2 * i_nb_channels, b_probe )
901 *i_format = VLC_CODEC_S16L;
907 *i_format = VLC_CODEC_FL32;
912 /*****************************************************************************
914 *****************************************************************************
915 * This function destroys the secondary buffer.
916 *****************************************************************************/
917 static void DestroyDSBuffer( audio_output_t *p_aout )
919 if( p_aout->sys->p_dsbuffer )
921 IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
922 p_aout->sys->p_dsbuffer = NULL;
926 /*****************************************************************************
927 * FillBuffer: Fill in one of the direct sound frame buffers.
928 *****************************************************************************
929 * Returns VLC_SUCCESS on success.
930 *****************************************************************************/
931 static int FillBuffer( audio_output_t *p_aout, int i_frame,
932 aout_buffer_t *p_buffer )
934 notification_thread_t *p_notif = p_aout->sys->p_notif;
935 aout_sys_t *p_sys = p_aout->sys;
936 void *p_write_position, *p_wrap_around;
937 unsigned long l_bytes1, l_bytes2;
940 /* Before copying anything, we have to lock the buffer */
941 dsresult = IDirectSoundBuffer_Lock(
942 p_sys->p_dsbuffer, /* DS buffer */
943 i_frame * p_notif->i_frame_size, /* Start offset */
944 p_notif->i_frame_size, /* Number of bytes */
945 &p_write_position, /* Address of lock start */
946 &l_bytes1, /* Count of bytes locked before wrap around */
947 &p_wrap_around, /* Buffer address (if wrap around) */
948 &l_bytes2, /* Count of bytes after wrap around */
950 if( dsresult == DSERR_BUFFERLOST )
952 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
953 dsresult = IDirectSoundBuffer_Lock(
955 i_frame * p_notif->i_frame_size,
956 p_notif->i_frame_size,
963 if( dsresult != DS_OK )
965 msg_Warn( p_aout, "cannot lock buffer" );
966 if( p_buffer ) aout_BufferFree( p_buffer );
970 if( p_buffer == NULL )
972 memset( p_write_position, 0, l_bytes1 );
976 if( p_sys->b_chan_reorder )
978 /* Do the channel reordering here */
979 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
980 p_sys->i_channels, p_sys->pi_chan_table,
981 p_sys->i_bits_per_sample );
984 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
985 aout_BufferFree( p_buffer );
988 /* Now the data has been copied, unlock the buffer */
989 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
990 p_wrap_around, l_bytes2 );
992 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
996 /*****************************************************************************
997 * DirectSoundThread: this thread will capture play notification events.
998 *****************************************************************************
999 * We use this thread to emulate a callback mechanism. The thread probes for
1000 * event notification and fills up the DS secondary buffer when needed.
1001 *****************************************************************************/
1002 static void* DirectSoundThread( void *data )
1004 notification_thread_t *p_notif = (notification_thread_t*)data;
1005 audio_output_t *p_aout = p_notif->p_aout;
1007 int canc = vlc_savecancel ();
1009 msg_Dbg( p_aout, "DirectSoundThread ready" );
1011 /* Wait here until Play() is called */
1012 WaitForSingleObject( p_notif->event, INFINITE );
1014 if( !vlc_atomic_get( &p_notif->abort) )
1017 mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1019 /* start playing the buffer */
1020 dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1023 DSBPLAY_LOOPING ); /* Flags */
1024 if( dsresult == DSERR_BUFFERLOST )
1026 IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1027 dsresult = IDirectSoundBuffer_Play(
1028 p_aout->sys->p_dsbuffer,
1031 DSBPLAY_LOOPING ); /* Flags */
1033 if( dsresult != DS_OK )
1035 msg_Err( p_aout, "cannot start playing buffer" );
1038 last_time = mdate();
1040 while( !vlc_atomic_get( &p_notif->abort ) )
1043 int l_queued = 0, l_free_slots;
1044 unsigned i_frame_siz = p_aout->sys->packet.samples;
1045 mtime_t mtime = mdate();
1049 * Fill in as much audio data as we can in our circular buffer
1052 /* Find out current play position */
1053 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1054 p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1056 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1060 /* Detect underruns */
1061 if( l_queued && mtime - last_time >
1062 INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1064 msg_Dbg( p_aout, "detected underrun!" );
1068 /* Try to fill in as many frame buffers as possible */
1069 l_read /= (p_aout->format.i_bytes_per_frame /
1070 p_aout->format.i_frame_length);
1071 l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1072 if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1073 l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1075 for( i = 0; i < l_free_slots; i++ )
1077 aout_buffer_t *p_buffer = aout_PacketNext( p_aout,
1078 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1079 p_aout->format.i_rate );
1081 /* If there is no audio data available and we have some buffered
1082 * already, then just wait for the next time */
1083 if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1085 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1086 p_buffer ) != VLC_SUCCESS ) break;
1089 /* Sleep a reasonable amount of time */
1090 l_queued += (i * i_frame_siz);
1091 msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1094 /* make sure the buffer isn't playing */
1095 IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1097 /* free the event */
1098 CloseHandle( p_notif->event );
1100 vlc_restorecancel (canc);
1101 msg_Dbg( p_aout, "DirectSoundThread exiting" );
1105 /*****************************************************************************
1106 * CallBackConfigNBEnum: callback to get the number of available devices
1107 *****************************************************************************/
1108 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1109 LPCWSTR psz_mod, LPVOID p_nb )
1111 VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1113 int * a = (int *)p_nb;
1118 /*****************************************************************************
1119 * CallBackConfigEnum: callback to add available devices to the preferences list
1120 *****************************************************************************/
1121 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1122 LPCWSTR psz_mod, LPVOID _p_item )
1124 VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1126 module_config_t *p_item = (module_config_t *) _p_item;
1128 p_item->ppsz_list[p_item->i_list] = FromWide( psz_desc );
1129 p_item->ppsz_list_text[p_item->i_list] = FromWide( psz_desc );
1134 /*****************************************************************************
1135 * ReloadDirectXDevices: store the list of devices in preferences
1136 *****************************************************************************/
1137 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1138 vlc_value_t newval, vlc_value_t oldval, void *data )
1140 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1142 module_config_t *p_item = config_FindConfig( p_this, psz_name );
1143 if( !p_item ) return VLC_SUCCESS;
1145 /* Clear-up the current list */
1146 if( p_item->i_list )
1148 for( int i = 0; i < p_item->i_list; i++ )
1150 free((char *)(p_item->ppsz_list[i]) );
1151 free((char *)(p_item->ppsz_list_text[i]) );
1155 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
1157 HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1158 if( hdsound_dll == NULL )
1160 msg_Warn( p_this, "cannot open DSOUND.DLL" );
1164 /* Get DirectSoundEnumerate */
1165 OurDirectSoundEnumerate = (void *)
1166 GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1168 if( OurDirectSoundEnumerate == NULL )
1172 OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1173 msg_Dbg(p_this,"found %d devices", nb_devices);
1175 p_item->ppsz_list = xrealloc( p_item->ppsz_list,
1176 nb_devices * sizeof(char *) );
1177 p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
1178 nb_devices * sizeof(char *) );
1181 OurDirectSoundEnumerate(CallBackConfigEnum, p_item);
1183 /* Signal change to the interface */
1184 p_item->b_dirty = true;
1187 FreeLibrary(hdsound_dll);