1 /*****************************************************************************
2 * directx.c: Windows DirectX audio output method
3 *****************************************************************************
4 * Copyright (C) 2001 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 *, int*, 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_("Select 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", 0, 0, 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 );
289 var_Create( p_aout, "directx-audio-speaker",
290 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
291 var_Get( p_this, "directx-audio-speaker", &val );
292 psz_speaker = val.psz_string;
294 while ( *ppsz_compare != NULL )
296 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
303 if ( *ppsz_compare == NULL )
305 msg_Err( p_aout, "(%s) isn't valid speaker setup option",
307 msg_Err( p_aout, "Defaulting to Windows default speaker config");
310 p_aout->output.p_sys->i_speaker_setup = i;
312 var_Create( p_aout, "directx-audio-device",
313 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
314 var_Get( p_aout, "directx-audio-device", &val );
315 p_aout->output.p_sys->i_device_id = val.i_int;
316 p_aout->output.p_sys->p_device_guid = 0;
318 /* Initialise DirectSound */
319 if( InitDirectSound( p_aout ) )
321 msg_Err( p_aout, "cannot initialize DirectSound" );
325 if( var_Type( p_aout, "audio-device" ) == 0 )
330 if( var_Get( p_aout, "audio-device", &val ) < 0 )
332 /* Probe() has failed. */
336 /* Open the device */
337 if( val.i_int == AOUT_VAR_SPDIF )
339 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
341 /* Calculate the frame size in bytes */
342 p_aout->output.i_nb_samples = A52_FRAME_NB;
343 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
344 p_aout->output.output.i_frame_length = A52_FRAME_NB;
345 p_aout->output.p_sys->i_frame_size =
346 p_aout->output.output.i_bytes_per_frame;
348 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
349 p_aout->output.output.i_physical_channels,
350 aout_FormatNbChannels( &p_aout->output.output ),
351 p_aout->output.output.i_rate,
352 p_aout->output.p_sys->i_frame_size, false )
355 msg_Err( p_aout, "cannot open directx audio device" );
356 free( p_aout->output.p_sys );
360 aout_VolumeNoneInit( p_aout );
364 if( val.i_int == AOUT_VAR_5_1 )
366 p_aout->output.output.i_physical_channels
367 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
368 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
371 else if( val.i_int == AOUT_VAR_7_1 )
373 p_aout->output.output.i_physical_channels
374 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
375 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
376 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
379 else if( val.i_int == AOUT_VAR_3F2R )
381 p_aout->output.output.i_physical_channels
382 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
383 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
385 else if( val.i_int == AOUT_VAR_2F2R )
387 p_aout->output.output.i_physical_channels
388 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
389 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
391 else if( val.i_int == AOUT_VAR_MONO )
393 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
397 p_aout->output.output.i_physical_channels
398 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
401 if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,
402 p_aout->output.output.i_physical_channels,
403 aout_FormatNbChannels( &p_aout->output.output ),
404 p_aout->output.output.i_rate, false )
407 msg_Err( p_aout, "cannot open directx audio device" );
408 free( p_aout->output.p_sys );
412 /* Calculate the frame size in bytes */
413 p_aout->output.i_nb_samples = FRAME_SIZE;
414 aout_FormatPrepare( &p_aout->output.output );
415 aout_VolumeSoftInit( p_aout );
418 /* Now we need to setup our DirectSound play notification structure */
419 p_aout->output.p_sys->p_notif =
420 vlc_object_create( p_aout, sizeof(notification_thread_t) );
421 p_aout->output.p_sys->p_notif->p_aout = p_aout;
423 p_aout->output.p_sys->p_notif->event = CreateEvent( 0, FALSE, FALSE, 0 );
424 p_aout->output.p_sys->p_notif->i_frame_size =
425 p_aout->output.p_sys->i_frame_size;
427 /* then launch the notification thread */
428 msg_Dbg( p_aout, "creating DirectSoundThread" );
429 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
430 "DirectSound Notification Thread",
432 VLC_THREAD_PRIORITY_HIGHEST ) )
434 msg_Err( p_aout, "cannot create DirectSoundThread" );
435 CloseHandle( p_aout->output.p_sys->p_notif->event );
436 vlc_object_release( p_aout->output.p_sys->p_notif );
437 p_aout->output.p_sys->p_notif = NULL;
441 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
446 CloseAudio( VLC_OBJECT(p_aout) );
450 /*****************************************************************************
451 * Probe: probe the audio device for available formats and channels
452 *****************************************************************************/
453 static void Probe( aout_instance_t * p_aout )
455 vlc_value_t val, text;
457 unsigned int i_physical_channels;
458 DWORD ui_speaker_config;
459 bool is_default_output_set = false;
461 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
462 text.psz_string = _("Audio Device");
463 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
465 /* Test for 5.1 support */
466 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
467 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
468 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
469 if( p_aout->output.output.i_physical_channels == i_physical_channels )
471 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
472 p_aout->output.output.i_rate, true )
475 val.i_int = AOUT_VAR_5_1;
476 text.psz_string = (char*) "5.1";
477 var_Change( p_aout, "audio-device",
478 VLC_VAR_ADDCHOICE, &val, &text );
479 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
480 is_default_output_set = true;
481 msg_Dbg( p_aout, "device supports 5.1 channels" );
485 /* Test for 7.1 support */
486 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
487 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
488 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
489 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
490 if( p_aout->output.output.i_physical_channels == i_physical_channels )
492 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
493 p_aout->output.output.i_rate, true )
496 val.i_int = AOUT_VAR_7_1;
497 text.psz_string = (char*) "7.1";
498 var_Change( p_aout, "audio-device",
499 VLC_VAR_ADDCHOICE, &val, &text );
500 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
501 is_default_output_set = true;
502 msg_Dbg( p_aout, "device supports 7.1 channels" );
506 /* Test for 3 Front 2 Rear support */
507 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
508 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
510 if( p_aout->output.output.i_physical_channels == i_physical_channels )
512 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
513 p_aout->output.output.i_rate, true )
516 val.i_int = AOUT_VAR_3F2R;
517 text.psz_string = _("3 Front 2 Rear");
518 var_Change( p_aout, "audio-device",
519 VLC_VAR_ADDCHOICE, &val, &text );
520 if(!is_default_output_set)
522 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
523 is_default_output_set = true;
525 msg_Dbg( p_aout, "device supports 5 channels" );
529 /* Test for 2 Front 2 Rear support */
530 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
531 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
532 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
533 == i_physical_channels )
535 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
536 p_aout->output.output.i_rate, true )
539 val.i_int = AOUT_VAR_2F2R;
540 text.psz_string = _("2 Front 2 Rear");
541 var_Change( p_aout, "audio-device",
542 VLC_VAR_ADDCHOICE, &val, &text );
543 if(!is_default_output_set)
545 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
546 is_default_output_set = true;
548 msg_Dbg( p_aout, "device supports 4 channels" );
552 /* Test for stereo support */
553 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
554 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
555 p_aout->output.output.i_rate, true )
558 val.i_int = AOUT_VAR_STEREO;
559 text.psz_string = _("Stereo");
560 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
561 if(!is_default_output_set)
563 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
564 is_default_output_set = true;
565 msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
567 else msg_Dbg( p_aout, "device supports 2 channels" );
570 /* Test for mono support */
571 i_physical_channels = AOUT_CHAN_CENTER;
572 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
573 p_aout->output.output.i_rate, true )
576 val.i_int = AOUT_VAR_MONO;
577 text.psz_string = _("Mono");
578 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
579 msg_Dbg( p_aout, "device supports 1 channel" );
582 /* Check the speaker configuration to determine which channel config should
584 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
585 &ui_speaker_config ) )
587 ui_speaker_config = DSSPEAKER_STEREO;
588 msg_Dbg( p_aout, "GetSpeakerConfig failed" );
590 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
592 case DSSPEAKER_7POINT1:
593 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
594 val.i_int = AOUT_VAR_7_1;
596 case DSSPEAKER_5POINT1:
597 msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
598 val.i_int = AOUT_VAR_5_1;
601 msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
602 val.i_int = AOUT_VAR_2F2R;
604 #if 0 /* Lots of people just get their settings wrong and complain that
605 * this is a problem with VLC so just don't ever set mono by default. */
607 val.i_int = AOUT_VAR_MONO;
610 case DSSPEAKER_SURROUND:
611 msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
612 case DSSPEAKER_STEREO:
613 msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
615 /* If nothing else is found, choose stereo output */
616 val.i_int = AOUT_VAR_STEREO;
620 /* Check if we want to override speaker config */
621 switch( p_aout->output.p_sys->i_speaker_setup )
623 case 0: /* Default value aka Windows default speaker setup */
626 msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
627 val.i_int = AOUT_VAR_MONO;
630 msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
631 val.i_int = AOUT_VAR_STEREO;
634 msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
635 val.i_int = AOUT_VAR_2F2R;
638 msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
639 val.i_int = AOUT_VAR_5_1;
642 msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
643 val.i_int = AOUT_VAR_7_1;
646 msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
650 var_Set( p_aout, "audio-device", val );
652 /* Test for SPDIF support */
653 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
655 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
656 p_aout->output.output.i_physical_channels,
657 aout_FormatNbChannels( &p_aout->output.output ),
658 p_aout->output.output.i_rate,
659 AOUT_SPDIF_SIZE, true )
662 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
663 val.i_int = AOUT_VAR_SPDIF;
664 text.psz_string = _("A/52 over S/PDIF");
665 var_Change( p_aout, "audio-device",
666 VLC_VAR_ADDCHOICE, &val, &text );
667 if( config_GetInt( p_aout, "spdif" ) )
668 var_Set( p_aout, "audio-device", val );
672 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
675 /* Probe() has failed. */
676 var_Destroy( p_aout, "audio-device" );
680 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
683 var_Set( p_aout, "intf-change", val );
686 /*****************************************************************************
687 * Play: we'll start playing the directsound buffer here because at least here
688 * we know the first buffer has been put in the aout fifo and we also
690 *****************************************************************************/
691 static void Play( aout_instance_t *p_aout )
693 if( !p_aout->output.p_sys->b_playing )
695 aout_buffer_t *p_buffer;
698 p_aout->output.p_sys->b_playing = 1;
700 /* get the playing date of the first aout buffer */
701 p_aout->output.p_sys->p_notif->start_date =
702 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
704 /* fill in the first samples */
705 for( i = 0; i < FRAMES_NUM; i++ )
707 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
708 if( !p_buffer ) break;
709 FillBuffer( p_aout, i, p_buffer );
712 /* wake up the audio output thread */
713 SetEvent( p_aout->output.p_sys->p_notif->event );
717 /*****************************************************************************
718 * CloseAudio: close the audio device
719 *****************************************************************************/
720 static void CloseAudio( vlc_object_t *p_this )
722 aout_instance_t * p_aout = (aout_instance_t *)p_this;
723 aout_sys_t *p_sys = p_aout->output.p_sys;
725 msg_Dbg( p_aout, "closing audio device" );
727 /* kill the position notification thread, if any */
730 vlc_object_detach( p_sys->p_notif );
731 vlc_object_kill( p_sys->p_notif );
732 /* wake up the audio thread if needed */
733 if( !p_sys->b_playing ) SetEvent( p_sys->p_notif->event );
735 vlc_thread_join( p_sys->p_notif );
736 vlc_object_release( p_sys->p_notif );
739 /* release the secondary buffer */
740 DestroyDSBuffer( p_aout );
742 /* finally release the DirectSound object */
743 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
745 /* free DSOUND.DLL */
746 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
748 free( p_aout->output.p_sys->p_device_guid );
752 /*****************************************************************************
753 * CallBackDirectSoundEnum: callback to enumerate available devices
754 *****************************************************************************/
755 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCSTR psz_desc,
756 LPCSTR psz_mod, LPVOID _p_aout )
758 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
760 msg_Dbg( p_aout, "found device: %s", psz_desc );
762 if( p_aout->output.p_sys->i_device_id == 0 && p_guid )
764 p_aout->output.p_sys->p_device_guid = malloc( sizeof( GUID ) );
765 *p_aout->output.p_sys->p_device_guid = *p_guid;
766 msg_Dbg( p_aout, "using device: %s", psz_desc );
769 p_aout->output.p_sys->i_device_id--;
773 /*****************************************************************************
774 * InitDirectSound: handle all the gory details of DirectSound initialisation
775 *****************************************************************************/
776 static int InitDirectSound( aout_instance_t *p_aout )
778 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
779 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACK, LPVOID);
781 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
782 if( p_aout->output.p_sys->hdsound_dll == NULL )
784 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
788 OurDirectSoundCreate = (void *)
789 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
790 "DirectSoundCreate" );
791 if( OurDirectSoundCreate == NULL )
793 msg_Warn( p_aout, "GetProcAddress FAILED" );
797 /* Get DirectSoundEnumerate */
798 OurDirectSoundEnumerate = (void *)
799 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
800 "DirectSoundEnumerateA" );
801 if( OurDirectSoundEnumerate )
803 /* Attempt enumeration */
804 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
807 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
811 /* Create the direct sound object */
812 if FAILED( OurDirectSoundCreate( p_aout->output.p_sys->p_device_guid,
813 &p_aout->output.p_sys->p_dsobject,
816 msg_Warn( p_aout, "cannot create a direct sound device" );
820 /* Set DirectSound Cooperative level, ie what control we want over Windows
821 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
822 * settings of the primary buffer, but also that only the sound of our
823 * application will be hearable when it will have the focus.
824 * !!! (this is not really working as intended yet because to set the
825 * cooperative level you need the window handle of your application, and
826 * I don't know of any easy way to get it. Especially since we might play
827 * sound without any video, and so what window handle should we use ???
828 * The hack for now is to use the Desktop window handle - it seems to be
830 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
834 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
840 p_aout->output.p_sys->p_dsobject = NULL;
841 if( p_aout->output.p_sys->hdsound_dll )
843 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
844 p_aout->output.p_sys->hdsound_dll = NULL;
850 /*****************************************************************************
851 * CreateDSBuffer: Creates a direct sound buffer of the required format.
852 *****************************************************************************
853 * This function creates the buffer we'll use to play audio.
854 * In DirectSound there are two kinds of buffers:
855 * - the primary buffer: which is the actual buffer that the soundcard plays
856 * - the secondary buffer(s): these buffers are the one actually used by
857 * applications and DirectSound takes care of mixing them into the primary.
859 * Once you create a secondary buffer, you cannot change its format anymore so
860 * you have to release the current one and create another.
861 *****************************************************************************/
862 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
863 int i_channels, int i_nb_channels, int i_rate,
864 int i_bytes_per_frame, bool b_probe )
866 WAVEFORMATEXTENSIBLE waveformat;
867 DSBUFFERDESC dsbdesc;
870 /* First set the sound buffer format */
871 waveformat.dwChannelMask = 0;
872 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
874 if( i_channels & pi_channels_src[i] )
875 waveformat.dwChannelMask |= pi_channels_in[i];
880 case VLC_FOURCC('s','p','d','i'):
882 /* To prevent channel re-ordering */
883 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
884 waveformat.Format.wBitsPerSample = 16;
885 waveformat.Samples.wValidBitsPerSample =
886 waveformat.Format.wBitsPerSample;
887 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
888 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
891 case VLC_FOURCC('f','l','3','2'):
892 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
893 waveformat.Samples.wValidBitsPerSample =
894 waveformat.Format.wBitsPerSample;
895 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
896 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
899 case VLC_FOURCC('s','1','6','l'):
900 waveformat.Format.wBitsPerSample = 16;
901 waveformat.Samples.wValidBitsPerSample =
902 waveformat.Format.wBitsPerSample;
903 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
904 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
908 waveformat.Format.nChannels = i_nb_channels;
909 waveformat.Format.nSamplesPerSec = i_rate;
910 waveformat.Format.nBlockAlign =
911 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
912 waveformat.Format.nAvgBytesPerSec =
913 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
915 p_aout->output.p_sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
916 p_aout->output.p_sys->i_channels = i_nb_channels;
918 /* Then fill in the direct sound descriptor */
919 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
920 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
921 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
922 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
924 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
925 if( i_nb_channels <= 2 )
927 waveformat.Format.cbSize = 0;
931 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
932 waveformat.Format.cbSize =
933 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
935 /* Needed for 5.1 on emu101k */
936 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
939 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
940 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
942 if FAILED( IDirectSound_CreateSoundBuffer(
943 p_aout->output.p_sys->p_dsobject, &dsbdesc,
944 &p_aout->output.p_sys->p_dsbuffer, NULL) )
946 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
948 /* Try without DSBCAPS_LOCHARDWARE */
949 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
950 if FAILED( IDirectSound_CreateSoundBuffer(
951 p_aout->output.p_sys->p_dsobject, &dsbdesc,
952 &p_aout->output.p_sys->p_dsbuffer, NULL) )
957 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
965 /* Stop here if we were just probing */
968 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
969 p_aout->output.p_sys->p_dsbuffer = NULL;
973 p_aout->output.p_sys->i_frame_size = i_bytes_per_frame;
974 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
975 p_aout->output.p_sys->b_chan_reorder =
976 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
977 waveformat.dwChannelMask, i_nb_channels,
978 p_aout->output.p_sys->pi_chan_table );
980 if( p_aout->output.p_sys->b_chan_reorder )
982 msg_Dbg( p_aout, "channel reordering needed" );
988 /*****************************************************************************
989 * CreateDSBufferPCM: creates a PCM direct sound buffer.
990 *****************************************************************************
991 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
992 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
993 ****************************************************************************/
994 static int CreateDSBufferPCM( aout_instance_t *p_aout, int *i_format,
995 int i_channels, int i_nb_channels, int i_rate,
1000 var_Get( p_aout, "directx-audio-float32", &val );
1002 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
1004 if( !val.b_bool || i_nb_channels > 2 ||
1005 CreateDSBuffer( p_aout, VLC_FOURCC('f','l','3','2'),
1006 i_channels, i_nb_channels, i_rate,
1007 FRAME_SIZE * 4 * i_nb_channels, b_probe )
1010 if ( CreateDSBuffer( p_aout, VLC_FOURCC('s','1','6','l'),
1011 i_channels, i_nb_channels, i_rate,
1012 FRAME_SIZE * 2 * i_nb_channels, b_probe )
1015 return VLC_EGENERIC;
1019 *i_format = VLC_FOURCC('s','1','6','l');
1025 *i_format = VLC_FOURCC('f','l','3','2');
1030 /*****************************************************************************
1032 *****************************************************************************
1033 * This function destroys the secondary buffer.
1034 *****************************************************************************/
1035 static void DestroyDSBuffer( aout_instance_t *p_aout )
1037 if( p_aout->output.p_sys->p_dsbuffer )
1039 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
1040 p_aout->output.p_sys->p_dsbuffer = NULL;
1044 /*****************************************************************************
1045 * FillBuffer: Fill in one of the direct sound frame buffers.
1046 *****************************************************************************
1047 * Returns VLC_SUCCESS on success.
1048 *****************************************************************************/
1049 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
1050 aout_buffer_t *p_buffer )
1052 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
1053 aout_sys_t *p_sys = p_aout->output.p_sys;
1054 void *p_write_position, *p_wrap_around;
1055 long l_bytes1, l_bytes2;
1058 /* Before copying anything, we have to lock the buffer */
1059 dsresult = IDirectSoundBuffer_Lock(
1060 p_sys->p_dsbuffer, /* DS buffer */
1061 i_frame * p_notif->i_frame_size, /* Start offset */
1062 p_notif->i_frame_size, /* Number of bytes */
1063 &p_write_position, /* Address of lock start */
1064 &l_bytes1, /* Count of bytes locked before wrap around */
1065 &p_wrap_around, /* Buffer adress (if wrap around) */
1066 &l_bytes2, /* Count of bytes after wrap around */
1068 if( dsresult == DSERR_BUFFERLOST )
1070 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
1071 dsresult = IDirectSoundBuffer_Lock(
1073 i_frame * p_notif->i_frame_size,
1074 p_notif->i_frame_size,
1081 if( dsresult != DS_OK )
1083 msg_Warn( p_notif, "cannot lock buffer" );
1084 if( p_buffer ) aout_BufferFree( p_buffer );
1085 return VLC_EGENERIC;
1088 if( p_buffer == NULL )
1090 memset( p_write_position, 0, l_bytes1 );
1094 if( p_sys->b_chan_reorder )
1096 /* Do the channel reordering here */
1097 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
1098 p_sys->i_channels, p_sys->pi_chan_table,
1099 p_sys->i_bits_per_sample );
1102 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
1103 aout_BufferFree( p_buffer );
1106 /* Now the data has been copied, unlock the buffer */
1107 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1108 p_wrap_around, l_bytes2 );
1110 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1114 /*****************************************************************************
1115 * DirectSoundThread: this thread will capture play notification events.
1116 *****************************************************************************
1117 * We use this thread to emulate a callback mechanism. The thread probes for
1118 * event notification and fills up the DS secondary buffer when needed.
1119 *****************************************************************************/
1120 static void* DirectSoundThread( vlc_object_t *p_this )
1122 notification_thread_t *p_notif = (notification_thread_t*)p_this;
1123 aout_instance_t *p_aout = p_notif->p_aout;
1128 int canc = vlc_savecancel ();
1130 /* We don't want any resampling when using S/PDIF output */
1131 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
1133 msg_Dbg( p_notif, "DirectSoundThread ready" );
1135 /* Wait here until Play() is called */
1136 WaitForSingleObject( p_notif->event, INFINITE );
1138 if( vlc_object_alive (p_notif) )
1140 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
1142 /* start playing the buffer */
1143 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
1146 DSBPLAY_LOOPING ); /* Flags */
1147 if( dsresult == DSERR_BUFFERLOST )
1149 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
1150 dsresult = IDirectSoundBuffer_Play(
1151 p_aout->output.p_sys->p_dsbuffer,
1154 DSBPLAY_LOOPING ); /* Flags */
1156 if( dsresult != DS_OK )
1158 msg_Err( p_aout, "cannot start playing buffer" );
1161 last_time = mdate();
1163 while( vlc_object_alive (p_notif) )
1165 long l_read, l_free_slots;
1166 mtime_t mtime = mdate();
1170 * Fill in as much audio data as we can in our circular buffer
1173 /* Find out current play position */
1174 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1175 p_aout->output.p_sys->p_dsbuffer, &l_read, NULL ) )
1177 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1181 /* Detect underruns */
1182 if( l_queued && mtime - last_time >
1183 INT64_C(1000000) * l_queued / p_aout->output.output.i_rate )
1185 msg_Dbg( p_aout, "detected underrun!" );
1189 /* Try to fill in as many frame buffers as possible */
1190 l_read /= p_aout->output.output.i_bytes_per_frame;
1191 l_queued = p_notif->i_write_slot * FRAME_SIZE - l_read;
1192 if( l_queued < 0 ) l_queued += (FRAME_SIZE * FRAMES_NUM);
1193 l_free_slots = (FRAMES_NUM * FRAME_SIZE - l_queued) / FRAME_SIZE;
1195 for( i = 0; i < l_free_slots; i++ )
1197 aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout,
1198 mtime + INT64_C(1000000) * (i * FRAME_SIZE + l_queued) /
1199 p_aout->output.output.i_rate, b_sleek );
1201 /* If there is no audio data available and we have some buffered
1202 * already, then just wait for the next time */
1203 if( !p_buffer && (i || l_queued / FRAME_SIZE) ) break;
1205 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1206 p_buffer ) != VLC_SUCCESS ) break;
1209 /* Sleep a reasonable amount of time */
1210 l_queued += (i * FRAME_SIZE);
1211 msleep( INT64_C(1000000) * l_queued / p_aout->output.output.i_rate / 2 );
1214 /* make sure the buffer isn't playing */
1215 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
1217 /* free the event */
1218 CloseHandle( p_notif->event );
1220 vlc_restorecancel (canc);
1221 msg_Dbg( p_notif, "DirectSoundThread exiting" );