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
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
40 #define FRAME_SIZE ((int)p_aout->output.output.i_rate/20) /* Size in samples */
41 #define FRAMES_NUM 8 /* Needs to be > 3 */
43 /*****************************************************************************
45 * Defining them here allows us to get rid of the dxguid library during
47 *****************************************************************************/
50 /*****************************************************************************
52 *****************************************************************************/
53 #ifndef WAVE_FORMAT_IEEE_FLOAT
54 # define WAVE_FORMAT_IEEE_FLOAT 0x0003
57 #ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
58 # define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
61 #ifndef WAVE_FORMAT_EXTENSIBLE
62 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
65 #ifndef SPEAKER_FRONT_LEFT
66 # define SPEAKER_FRONT_LEFT 0x1
67 # define SPEAKER_FRONT_RIGHT 0x2
68 # define SPEAKER_FRONT_CENTER 0x4
69 # define SPEAKER_LOW_FREQUENCY 0x8
70 # define SPEAKER_BACK_LEFT 0x10
71 # define SPEAKER_BACK_RIGHT 0x20
72 # define SPEAKER_FRONT_LEFT_OF_CENTER 0x40
73 # define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80
74 # define SPEAKER_BACK_CENTER 0x100
75 # define SPEAKER_SIDE_LEFT 0x200
76 # define SPEAKER_SIDE_RIGHT 0x400
77 # define SPEAKER_TOP_CENTER 0x800
78 # define SPEAKER_TOP_FRONT_LEFT 0x1000
79 # define SPEAKER_TOP_FRONT_CENTER 0x2000
80 # define SPEAKER_TOP_FRONT_RIGHT 0x4000
81 # define SPEAKER_TOP_BACK_LEFT 0x8000
82 # define SPEAKER_TOP_BACK_CENTER 0x10000
83 # define SPEAKER_TOP_BACK_RIGHT 0x20000
84 # define SPEAKER_RESERVED 0x80000000
87 #ifndef DSSPEAKER_DSSPEAKER_DIRECTOUT
88 # define DSSPEAKER_DSSPEAKER_DIRECTOUT 0x00000000
90 #ifndef DSSPEAKER_HEADPHONE
91 # define DSSPEAKER_HEADPHONE 0x00000001
93 #ifndef DSSPEAKER_MONO
94 # define DSSPEAKER_MONO 0x00000002
96 #ifndef DSSPEAKER_QUAD
97 # define DSSPEAKER_QUAD 0x00000003
99 #ifndef DSSPEAKER_STEREO
100 # define DSSPEAKER_STEREO 0x00000004
102 #ifndef DSSPEAKER_SURROUND
103 # define DSSPEAKER_SURROUND 0x00000005
105 #ifndef DSSPEAKER_5POINT1
106 # define DSSPEAKER_5POINT1 0x00000006
108 #ifndef DSSPEAKER_7POINT1
109 # define DSSPEAKER_7POINT1 0x00000007
111 #ifndef DSSPEAKER_7POINT1_SURROUND
112 # define DSSPEAKER_7POINT1_SURROUND 0x00000008
114 #ifndef DSSPEAKER_7POINT1_WIDE
115 # define DSSPEAKER_7POINT1_WIDE DSSPEAKER_7POINT1
118 #ifndef _WAVEFORMATEXTENSIBLE_
122 WORD wValidBitsPerSample; /* bits of precision */
123 WORD wSamplesPerBlock; /* valid if wBitsPerSample==0 */
124 WORD wReserved; /* If neither applies, set to zero. */
126 DWORD dwChannelMask; /* which channels are */
127 /* present in stream */
129 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
132 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
133 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_PCM, WAVE_FORMAT_PCM, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
134 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF, WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
136 /*****************************************************************************
137 * notification_thread_t: DirectX event thread
138 *****************************************************************************/
139 typedef struct notification_thread_t
143 aout_instance_t *p_aout;
144 int i_frame_size; /* size in bytes of one frame */
145 int i_write_slot; /* current write position in our circular buffer */
150 } notification_thread_t;
152 /*****************************************************************************
153 * aout_sys_t: directx audio output method descriptor
154 *****************************************************************************
155 * This structure is part of the audio output thread descriptor.
156 * It describes the direct sound specific properties of an audio device.
157 *****************************************************************************/
160 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
162 int i_device_id; /* user defined device */
163 LPGUID p_device_guid;
165 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
166 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
167 * takes care of mixing all the
168 * secondary buffers into the primary) */
170 notification_thread_t *p_notif; /* DirectSoundThread id */
172 int b_playing; /* playing status */
174 int i_frame_size; /* Size in bytes of one frame */
176 int i_speaker_setup; /* Speaker setup override */
178 bool b_chan_reorder; /* do we need channel reordering */
179 int pi_chan_table[AOUT_CHAN_MAX];
180 uint32_t i_channel_mask;
181 uint32_t i_bits_per_sample;
185 static const uint32_t pi_channels_src[] =
186 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
187 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
188 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_REARCENTER,
189 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
190 static const uint32_t pi_channels_in[] =
191 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
192 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
193 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT, SPEAKER_BACK_CENTER,
194 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
195 static const uint32_t pi_channels_out[] =
196 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
197 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
198 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
200 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
202 /*****************************************************************************
204 *****************************************************************************/
205 static int OpenAudio ( vlc_object_t * );
206 static void CloseAudio ( vlc_object_t * );
207 static void Play ( aout_instance_t * );
209 /* local functions */
210 static void Probe ( aout_instance_t * );
211 static int InitDirectSound ( aout_instance_t * );
212 static int CreateDSBuffer ( aout_instance_t *, int, int, int, int, int, bool );
213 static int CreateDSBufferPCM ( aout_instance_t *, vlc_fourcc_t*, int, int, int, bool );
214 static void DestroyDSBuffer ( aout_instance_t * );
215 static void* DirectSoundThread( vlc_object_t * );
216 static int FillBuffer ( aout_instance_t *, int, aout_buffer_t * );
218 /* Speaker setup override options list */
219 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
220 "Quad", "5.1", "7.1" };
222 /*****************************************************************************
224 *****************************************************************************/
225 #define DEVICE_TEXT N_("Output device")
226 #define DEVICE_LONGTEXT N_( \
227 "DirectX device number: 0 default device, 1..N device by number" \
228 "(Note that the default device appears as 0 AND another number)." )
229 #define FLOAT_TEXT N_("Use float32 output")
230 #define FLOAT_LONGTEXT N_( \
231 "The option allows you to enable or disable the high-quality float32 " \
232 "audio output mode (which is not well supported by some soundcards)." )
233 #define SPEAKER_TEXT N_("Speaker configuration")
234 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
235 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
238 set_description( N_("DirectX audio output") )
239 set_shortname( "DirectX" )
240 set_capability( "audio output", 100 )
241 set_category( CAT_AUDIO )
242 set_subcategory( SUBCAT_AUDIO_AOUT )
243 add_shortcut( "directx" )
244 add_integer( "directx-audio-device", 0, NULL, DEVICE_TEXT,
245 DEVICE_LONGTEXT, true )
246 add_bool( "directx-audio-float32", false, NULL, FLOAT_TEXT,
247 FLOAT_LONGTEXT, true )
248 add_string( "directx-audio-speaker", "Windows default", NULL,
249 SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
250 change_string_list( speaker_list, 0, 0 )
251 change_need_restart ()
252 set_callbacks( OpenAudio, CloseAudio )
255 /*****************************************************************************
256 * OpenAudio: open the audio device
257 *****************************************************************************
258 * This function opens and setups Direct Sound.
259 *****************************************************************************/
260 static int OpenAudio( vlc_object_t *p_this )
262 aout_instance_t * p_aout = (aout_instance_t *)p_this;
267 const char * const * ppsz_compare = speaker_list;
269 msg_Dbg( p_aout, "OpenAudio" );
271 /* Allocate structure */
272 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
273 if( p_aout->output.p_sys == NULL )
276 /* Initialize some variables */
277 p_aout->output.p_sys->p_dsobject = NULL;
278 p_aout->output.p_sys->p_dsbuffer = NULL;
279 p_aout->output.p_sys->p_notif = NULL;
280 p_aout->output.p_sys->b_playing = 0;
282 p_aout->output.pf_play = Play;
283 aout_VolumeSoftInit( p_aout );
285 /* Retrieve config values */
286 var_Create( p_aout, "directx-audio-float32",
287 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
288 psz_speaker = var_CreateGetString( p_aout, "directx-audio-speaker" );
290 while ( *ppsz_compare != NULL )
292 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
299 if ( *ppsz_compare == NULL )
301 msg_Err( p_aout, "(%s) isn't valid speaker setup option",
303 msg_Err( p_aout, "Defaulting to Windows default speaker config");
307 p_aout->output.p_sys->i_speaker_setup = i;
309 p_aout->output.p_sys->i_device_id = var_CreateGetInteger( p_aout,
310 "directx-audio-device" );
311 p_aout->output.p_sys->p_device_guid = 0;
313 /* Initialise DirectSound */
314 if( InitDirectSound( p_aout ) )
316 msg_Err( p_aout, "cannot initialize DirectSound" );
320 if( var_Type( p_aout, "audio-device" ) == 0 )
325 if( var_Get( p_aout, "audio-device", &val ) < 0 )
327 /* Probe() has failed. */
331 /* Open the device */
332 if( val.i_int == AOUT_VAR_SPDIF )
334 p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
336 /* Calculate the frame size in bytes */
337 p_aout->output.i_nb_samples = A52_FRAME_NB;
338 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
339 p_aout->output.output.i_frame_length = A52_FRAME_NB;
340 p_aout->output.p_sys->i_frame_size =
341 p_aout->output.output.i_bytes_per_frame;
343 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
344 p_aout->output.output.i_physical_channels,
345 aout_FormatNbChannels( &p_aout->output.output ),
346 p_aout->output.output.i_rate,
347 p_aout->output.p_sys->i_frame_size, false )
350 msg_Err( p_aout, "cannot open directx audio device" );
351 free( p_aout->output.p_sys );
355 aout_VolumeNoneInit( p_aout );
359 if( val.i_int == AOUT_VAR_5_1 )
361 p_aout->output.output.i_physical_channels
362 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
363 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
366 else if( val.i_int == AOUT_VAR_7_1 )
368 p_aout->output.output.i_physical_channels
369 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
370 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
371 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
374 else if( val.i_int == AOUT_VAR_3F2R )
376 p_aout->output.output.i_physical_channels
377 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
378 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
380 else if( val.i_int == AOUT_VAR_2F2R )
382 p_aout->output.output.i_physical_channels
383 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
384 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
386 else if( val.i_int == AOUT_VAR_MONO )
388 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
392 p_aout->output.output.i_physical_channels
393 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
396 if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,
397 p_aout->output.output.i_physical_channels,
398 aout_FormatNbChannels( &p_aout->output.output ),
399 p_aout->output.output.i_rate, false )
402 msg_Err( p_aout, "cannot open directx audio device" );
403 free( p_aout->output.p_sys );
407 /* Calculate the frame size in bytes */
408 p_aout->output.i_nb_samples = FRAME_SIZE;
409 aout_FormatPrepare( &p_aout->output.output );
410 aout_VolumeSoftInit( p_aout );
413 /* Now we need to setup our DirectSound play notification structure */
414 p_aout->output.p_sys->p_notif =
415 vlc_object_create( p_aout, sizeof(notification_thread_t) );
416 p_aout->output.p_sys->p_notif->p_aout = p_aout;
418 p_aout->output.p_sys->p_notif->event = CreateEvent( 0, FALSE, FALSE, 0 );
419 p_aout->output.p_sys->p_notif->i_frame_size =
420 p_aout->output.p_sys->i_frame_size;
422 /* then launch the notification thread */
423 msg_Dbg( p_aout, "creating DirectSoundThread" );
424 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
425 "DirectSound Notification Thread",
427 VLC_THREAD_PRIORITY_HIGHEST ) )
429 msg_Err( p_aout, "cannot create DirectSoundThread" );
430 CloseHandle( p_aout->output.p_sys->p_notif->event );
431 vlc_object_release( p_aout->output.p_sys->p_notif );
432 p_aout->output.p_sys->p_notif = NULL;
436 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
441 CloseAudio( VLC_OBJECT(p_aout) );
445 /*****************************************************************************
446 * Probe: probe the audio device for available formats and channels
447 *****************************************************************************/
448 static void Probe( aout_instance_t * p_aout )
450 vlc_value_t val, text;
451 vlc_fourcc_t i_format;
452 unsigned int i_physical_channels;
453 DWORD ui_speaker_config;
454 bool is_default_output_set = false;
456 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
457 text.psz_string = _("Audio Device");
458 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
460 /* Test for 5.1 support */
461 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
462 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
463 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
464 if( p_aout->output.output.i_physical_channels == i_physical_channels )
466 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
467 p_aout->output.output.i_rate, true )
470 val.i_int = AOUT_VAR_5_1;
471 text.psz_string = (char*) "5.1";
472 var_Change( p_aout, "audio-device",
473 VLC_VAR_ADDCHOICE, &val, &text );
474 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
475 is_default_output_set = true;
476 msg_Dbg( p_aout, "device supports 5.1 channels" );
480 /* Test for 7.1 support */
481 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
482 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
483 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
484 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
485 if( p_aout->output.output.i_physical_channels == i_physical_channels )
487 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
488 p_aout->output.output.i_rate, true )
491 val.i_int = AOUT_VAR_7_1;
492 text.psz_string = (char*) "7.1";
493 var_Change( p_aout, "audio-device",
494 VLC_VAR_ADDCHOICE, &val, &text );
495 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
496 is_default_output_set = true;
497 msg_Dbg( p_aout, "device supports 7.1 channels" );
501 /* Test for 3 Front 2 Rear support */
502 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
503 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
505 if( p_aout->output.output.i_physical_channels == i_physical_channels )
507 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
508 p_aout->output.output.i_rate, true )
511 val.i_int = AOUT_VAR_3F2R;
512 text.psz_string = _("3 Front 2 Rear");
513 var_Change( p_aout, "audio-device",
514 VLC_VAR_ADDCHOICE, &val, &text );
515 if(!is_default_output_set)
517 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
518 is_default_output_set = true;
520 msg_Dbg( p_aout, "device supports 5 channels" );
524 /* Test for 2 Front 2 Rear support */
525 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
526 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
527 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
528 == i_physical_channels )
530 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
531 p_aout->output.output.i_rate, true )
534 val.i_int = AOUT_VAR_2F2R;
535 text.psz_string = _("2 Front 2 Rear");
536 var_Change( p_aout, "audio-device",
537 VLC_VAR_ADDCHOICE, &val, &text );
538 if(!is_default_output_set)
540 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
541 is_default_output_set = true;
543 msg_Dbg( p_aout, "device supports 4 channels" );
547 /* Test for stereo support */
548 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
549 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
550 p_aout->output.output.i_rate, true )
553 val.i_int = AOUT_VAR_STEREO;
554 text.psz_string = _("Stereo");
555 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
556 if(!is_default_output_set)
558 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
559 is_default_output_set = true;
560 msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
562 else msg_Dbg( p_aout, "device supports 2 channels" );
565 /* Test for mono support */
566 i_physical_channels = AOUT_CHAN_CENTER;
567 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
568 p_aout->output.output.i_rate, true )
571 val.i_int = AOUT_VAR_MONO;
572 text.psz_string = _("Mono");
573 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
574 msg_Dbg( p_aout, "device supports 1 channel" );
577 /* Check the speaker configuration to determine which channel config should
579 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
580 &ui_speaker_config ) )
582 ui_speaker_config = DSSPEAKER_STEREO;
583 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
585 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
587 case DSSPEAKER_7POINT1:
588 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
589 val.i_int = AOUT_VAR_7_1;
591 case DSSPEAKER_5POINT1:
592 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
593 val.i_int = AOUT_VAR_5_1;
596 msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
597 val.i_int = AOUT_VAR_2F2R;
599 #if 0 /* Lots of people just get their settings wrong and complain that
600 * this is a problem with VLC so just don't ever set mono by default. */
602 val.i_int = AOUT_VAR_MONO;
605 case DSSPEAKER_SURROUND:
606 msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
607 case DSSPEAKER_STEREO:
608 msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
610 /* If nothing else is found, choose stereo output */
611 val.i_int = AOUT_VAR_STEREO;
615 /* Check if we want to override speaker config */
616 switch( p_aout->output.p_sys->i_speaker_setup )
618 case 0: /* Default value aka Windows default speaker setup */
621 msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
622 val.i_int = AOUT_VAR_MONO;
625 msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
626 val.i_int = AOUT_VAR_STEREO;
629 msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
630 val.i_int = AOUT_VAR_2F2R;
633 msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
634 val.i_int = AOUT_VAR_5_1;
637 msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
638 val.i_int = AOUT_VAR_7_1;
641 msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
645 var_Set( p_aout, "audio-device", val );
647 /* Test for SPDIF support */
648 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
650 if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
651 p_aout->output.output.i_physical_channels,
652 aout_FormatNbChannels( &p_aout->output.output ),
653 p_aout->output.output.i_rate,
654 AOUT_SPDIF_SIZE, true )
657 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
658 val.i_int = AOUT_VAR_SPDIF;
659 text.psz_string = _("A/52 over S/PDIF");
660 var_Change( p_aout, "audio-device",
661 VLC_VAR_ADDCHOICE, &val, &text );
662 if( config_GetInt( p_aout, "spdif" ) )
663 var_Set( p_aout, "audio-device", val );
667 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
670 /* Probe() has failed. */
671 var_Destroy( p_aout, "audio-device" );
675 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
676 var_SetBool( p_aout, "intf-change", true );
679 /*****************************************************************************
680 * Play: we'll start playing the directsound buffer here because at least here
681 * we know the first buffer has been put in the aout fifo and we also
683 *****************************************************************************/
684 static void Play( aout_instance_t *p_aout )
686 if( !p_aout->output.p_sys->b_playing )
688 aout_buffer_t *p_buffer;
691 p_aout->output.p_sys->b_playing = 1;
693 /* get the playing date of the first aout buffer */
694 p_aout->output.p_sys->p_notif->start_date =
695 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
697 /* fill in the first samples */
698 for( i = 0; i < FRAMES_NUM; i++ )
700 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
701 if( !p_buffer ) break;
702 FillBuffer( p_aout, i, p_buffer );
705 /* wake up the audio output thread */
706 SetEvent( p_aout->output.p_sys->p_notif->event );
710 /*****************************************************************************
711 * CloseAudio: close the audio device
712 *****************************************************************************/
713 static void CloseAudio( vlc_object_t *p_this )
715 aout_instance_t * p_aout = (aout_instance_t *)p_this;
716 aout_sys_t *p_sys = p_aout->output.p_sys;
718 msg_Dbg( p_aout, "closing audio device" );
720 /* kill the position notification thread, if any */
723 vlc_object_detach( p_sys->p_notif );
724 vlc_object_kill( p_sys->p_notif );
725 /* wake up the audio thread if needed */
726 if( !p_sys->b_playing ) SetEvent( p_sys->p_notif->event );
728 vlc_thread_join( p_sys->p_notif );
729 vlc_object_release( p_sys->p_notif );
732 /* release the secondary buffer */
733 DestroyDSBuffer( p_aout );
735 /* finally release the DirectSound object */
736 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
738 /* free DSOUND.DLL */
739 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
741 free( p_aout->output.p_sys->p_device_guid );
745 /*****************************************************************************
746 * CallBackDirectSoundEnum: callback to enumerate available devices
747 *****************************************************************************/
748 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCSTR psz_desc,
749 LPCSTR psz_mod, LPVOID _p_aout )
751 VLC_UNUSED( psz_mod );
753 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
755 msg_Dbg( p_aout, "found device: %s", psz_desc );
757 if( p_aout->output.p_sys->i_device_id == 0 && p_guid )
759 p_aout->output.p_sys->p_device_guid = malloc( sizeof( GUID ) );
760 *p_aout->output.p_sys->p_device_guid = *p_guid;
761 msg_Dbg( p_aout, "using device: %s", psz_desc );
764 p_aout->output.p_sys->i_device_id--;
768 /*****************************************************************************
769 * InitDirectSound: handle all the gory details of DirectSound initialisation
770 *****************************************************************************/
771 static int InitDirectSound( aout_instance_t *p_aout )
773 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
774 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACK, LPVOID);
776 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
777 if( p_aout->output.p_sys->hdsound_dll == NULL )
779 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
783 OurDirectSoundCreate = (void *)
784 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
785 "DirectSoundCreate" );
786 if( OurDirectSoundCreate == NULL )
788 msg_Warn( p_aout, "GetProcAddress FAILED" );
792 /* Get DirectSoundEnumerate */
793 OurDirectSoundEnumerate = (void *)
794 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
795 "DirectSoundEnumerateA" );
796 if( OurDirectSoundEnumerate )
798 /* Attempt enumeration */
799 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
802 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
806 /* Create the direct sound object */
807 if FAILED( OurDirectSoundCreate( p_aout->output.p_sys->p_device_guid,
808 &p_aout->output.p_sys->p_dsobject,
811 msg_Warn( p_aout, "cannot create a direct sound device" );
815 /* Set DirectSound Cooperative level, ie what control we want over Windows
816 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
817 * settings of the primary buffer, but also that only the sound of our
818 * application will be hearable when it will have the focus.
819 * !!! (this is not really working as intended yet because to set the
820 * cooperative level you need the window handle of your application, and
821 * I don't know of any easy way to get it. Especially since we might play
822 * sound without any video, and so what window handle should we use ???
823 * The hack for now is to use the Desktop window handle - it seems to be
825 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
829 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
835 p_aout->output.p_sys->p_dsobject = NULL;
836 if( p_aout->output.p_sys->hdsound_dll )
838 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
839 p_aout->output.p_sys->hdsound_dll = NULL;
845 /*****************************************************************************
846 * CreateDSBuffer: Creates a direct sound buffer of the required format.
847 *****************************************************************************
848 * This function creates the buffer we'll use to play audio.
849 * In DirectSound there are two kinds of buffers:
850 * - the primary buffer: which is the actual buffer that the soundcard plays
851 * - the secondary buffer(s): these buffers are the one actually used by
852 * applications and DirectSound takes care of mixing them into the primary.
854 * Once you create a secondary buffer, you cannot change its format anymore so
855 * you have to release the current one and create another.
856 *****************************************************************************/
857 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
858 int i_channels, int i_nb_channels, int i_rate,
859 int i_bytes_per_frame, bool b_probe )
861 WAVEFORMATEXTENSIBLE waveformat;
862 DSBUFFERDESC dsbdesc;
865 /* First set the sound buffer format */
866 waveformat.dwChannelMask = 0;
867 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
869 if( i_channels & pi_channels_src[i] )
870 waveformat.dwChannelMask |= pi_channels_in[i];
875 case VLC_CODEC_SPDIFL:
877 /* To prevent channel re-ordering */
878 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
879 waveformat.Format.wBitsPerSample = 16;
880 waveformat.Samples.wValidBitsPerSample =
881 waveformat.Format.wBitsPerSample;
882 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
883 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
887 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
888 waveformat.Samples.wValidBitsPerSample =
889 waveformat.Format.wBitsPerSample;
890 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
891 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
895 waveformat.Format.wBitsPerSample = 16;
896 waveformat.Samples.wValidBitsPerSample =
897 waveformat.Format.wBitsPerSample;
898 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
899 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
903 waveformat.Format.nChannels = i_nb_channels;
904 waveformat.Format.nSamplesPerSec = i_rate;
905 waveformat.Format.nBlockAlign =
906 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
907 waveformat.Format.nAvgBytesPerSec =
908 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
910 p_aout->output.p_sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
911 p_aout->output.p_sys->i_channels = i_nb_channels;
913 /* Then fill in the direct sound descriptor */
914 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
915 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
916 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
917 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
919 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
920 if( i_nb_channels <= 2 )
922 waveformat.Format.cbSize = 0;
926 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
927 waveformat.Format.cbSize =
928 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
930 /* Needed for 5.1 on emu101k */
931 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
934 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
935 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
937 if FAILED( IDirectSound_CreateSoundBuffer(
938 p_aout->output.p_sys->p_dsobject, &dsbdesc,
939 &p_aout->output.p_sys->p_dsbuffer, NULL) )
941 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
943 /* Try without DSBCAPS_LOCHARDWARE */
944 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
945 if FAILED( IDirectSound_CreateSoundBuffer(
946 p_aout->output.p_sys->p_dsobject, &dsbdesc,
947 &p_aout->output.p_sys->p_dsbuffer, NULL) )
952 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
960 /* Stop here if we were just probing */
963 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
964 p_aout->output.p_sys->p_dsbuffer = NULL;
968 p_aout->output.p_sys->i_frame_size = i_bytes_per_frame;
969 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
970 p_aout->output.p_sys->b_chan_reorder =
971 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
972 waveformat.dwChannelMask, i_nb_channels,
973 p_aout->output.p_sys->pi_chan_table );
975 if( p_aout->output.p_sys->b_chan_reorder )
977 msg_Dbg( p_aout, "channel reordering needed" );
983 /*****************************************************************************
984 * CreateDSBufferPCM: creates a PCM direct sound buffer.
985 *****************************************************************************
986 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
987 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
988 ****************************************************************************/
989 static int CreateDSBufferPCM( aout_instance_t *p_aout, vlc_fourcc_t *i_format,
990 int i_channels, int i_nb_channels, int i_rate,
993 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
994 if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
996 CreateDSBuffer( p_aout, VLC_CODEC_FL32,
997 i_channels, i_nb_channels, i_rate,
998 FRAME_SIZE * 4 * i_nb_channels, b_probe )
1001 if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
1002 i_channels, i_nb_channels, i_rate,
1003 FRAME_SIZE * 2 * i_nb_channels, b_probe )
1006 return VLC_EGENERIC;
1010 *i_format = VLC_CODEC_S16L;
1016 *i_format = VLC_CODEC_FL32;
1021 /*****************************************************************************
1023 *****************************************************************************
1024 * This function destroys the secondary buffer.
1025 *****************************************************************************/
1026 static void DestroyDSBuffer( aout_instance_t *p_aout )
1028 if( p_aout->output.p_sys->p_dsbuffer )
1030 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
1031 p_aout->output.p_sys->p_dsbuffer = NULL;
1035 /*****************************************************************************
1036 * FillBuffer: Fill in one of the direct sound frame buffers.
1037 *****************************************************************************
1038 * Returns VLC_SUCCESS on success.
1039 *****************************************************************************/
1040 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
1041 aout_buffer_t *p_buffer )
1043 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
1044 aout_sys_t *p_sys = p_aout->output.p_sys;
1045 void *p_write_position, *p_wrap_around;
1046 unsigned long l_bytes1, l_bytes2;
1049 /* Before copying anything, we have to lock the buffer */
1050 dsresult = IDirectSoundBuffer_Lock(
1051 p_sys->p_dsbuffer, /* DS buffer */
1052 i_frame * p_notif->i_frame_size, /* Start offset */
1053 p_notif->i_frame_size, /* Number of bytes */
1054 &p_write_position, /* Address of lock start */
1055 &l_bytes1, /* Count of bytes locked before wrap around */
1056 &p_wrap_around, /* Buffer adress (if wrap around) */
1057 &l_bytes2, /* Count of bytes after wrap around */
1059 if( dsresult == DSERR_BUFFERLOST )
1061 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
1062 dsresult = IDirectSoundBuffer_Lock(
1064 i_frame * p_notif->i_frame_size,
1065 p_notif->i_frame_size,
1072 if( dsresult != DS_OK )
1074 msg_Warn( p_notif, "cannot lock buffer" );
1075 if( p_buffer ) aout_BufferFree( p_buffer );
1076 return VLC_EGENERIC;
1079 if( p_buffer == NULL )
1081 memset( p_write_position, 0, l_bytes1 );
1085 if( p_sys->b_chan_reorder )
1087 /* Do the channel reordering here */
1088 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
1089 p_sys->i_channels, p_sys->pi_chan_table,
1090 p_sys->i_bits_per_sample );
1093 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
1094 aout_BufferFree( p_buffer );
1097 /* Now the data has been copied, unlock the buffer */
1098 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1099 p_wrap_around, l_bytes2 );
1101 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1105 /*****************************************************************************
1106 * DirectSoundThread: this thread will capture play notification events.
1107 *****************************************************************************
1108 * We use this thread to emulate a callback mechanism. The thread probes for
1109 * event notification and fills up the DS secondary buffer when needed.
1110 *****************************************************************************/
1111 static void* DirectSoundThread( vlc_object_t *p_this )
1113 notification_thread_t *p_notif = (notification_thread_t*)p_this;
1114 aout_instance_t *p_aout = p_notif->p_aout;
1119 int canc = vlc_savecancel ();
1121 /* We don't want any resampling when using S/PDIF output */
1122 b_sleek = p_aout->output.output.i_format == VLC_CODEC_SPDIFL;
1124 msg_Dbg( p_notif, "DirectSoundThread ready" );
1126 /* Wait here until Play() is called */
1127 WaitForSingleObject( p_notif->event, INFINITE );
1129 if( vlc_object_alive (p_notif) )
1131 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
1133 /* start playing the buffer */
1134 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
1137 DSBPLAY_LOOPING ); /* Flags */
1138 if( dsresult == DSERR_BUFFERLOST )
1140 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
1141 dsresult = IDirectSoundBuffer_Play(
1142 p_aout->output.p_sys->p_dsbuffer,
1145 DSBPLAY_LOOPING ); /* Flags */
1147 if( dsresult != DS_OK )
1149 msg_Err( p_aout, "cannot start playing buffer" );
1152 last_time = mdate();
1154 while( vlc_object_alive (p_notif) )
1156 long l_read, l_free_slots;
1157 mtime_t mtime = mdate();
1161 * Fill in as much audio data as we can in our circular buffer
1164 /* Find out current play position */
1165 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1166 p_aout->output.p_sys->p_dsbuffer, &l_read, NULL ) )
1168 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1172 /* Detect underruns */
1173 if( l_queued && mtime - last_time >
1174 INT64_C(1000000) * l_queued / p_aout->output.output.i_rate )
1176 msg_Dbg( p_aout, "detected underrun!" );
1180 /* Try to fill in as many frame buffers as possible */
1181 l_read /= p_aout->output.output.i_bytes_per_frame;
1182 l_queued = p_notif->i_write_slot * FRAME_SIZE - l_read;
1183 if( l_queued < 0 ) l_queued += (FRAME_SIZE * FRAMES_NUM);
1184 l_free_slots = (FRAMES_NUM * FRAME_SIZE - l_queued) / FRAME_SIZE;
1186 for( i = 0; i < l_free_slots; i++ )
1188 aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout,
1189 mtime + INT64_C(1000000) * (i * FRAME_SIZE + l_queued) /
1190 p_aout->output.output.i_rate, b_sleek );
1192 /* If there is no audio data available and we have some buffered
1193 * already, then just wait for the next time */
1194 if( !p_buffer && (i || l_queued / FRAME_SIZE) ) break;
1196 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1197 p_buffer ) != VLC_SUCCESS ) break;
1200 /* Sleep a reasonable amount of time */
1201 l_queued += (i * FRAME_SIZE);
1202 msleep( INT64_C(1000000) * l_queued / p_aout->output.output.i_rate / 2 );
1205 /* make sure the buffer isn't playing */
1206 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
1208 /* free the event */
1209 CloseHandle( p_notif->event );
1211 vlc_restorecancel (canc);
1212 msg_Dbg( p_notif, "DirectSoundThread exiting" );