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 *****************************************************************************/
34 #include <vlc_plugin.h>
41 #define FRAME_SIZE ((int)p_aout->output.output.i_rate/20) /* Size in samples */
42 #define FRAMES_NUM 8 /* Needs to be > 3 */
44 /*****************************************************************************
46 * Defining them here allows us to get rid of the dxguid library during
48 *****************************************************************************/
51 /*****************************************************************************
53 *****************************************************************************/
54 #ifndef WAVE_FORMAT_IEEE_FLOAT
55 # define WAVE_FORMAT_IEEE_FLOAT 0x0003
58 #ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
59 # define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
62 #ifndef WAVE_FORMAT_EXTENSIBLE
63 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
66 #ifndef SPEAKER_FRONT_LEFT
67 # define SPEAKER_FRONT_LEFT 0x1
68 # define SPEAKER_FRONT_RIGHT 0x2
69 # define SPEAKER_FRONT_CENTER 0x4
70 # define SPEAKER_LOW_FREQUENCY 0x8
71 # define SPEAKER_BACK_LEFT 0x10
72 # define SPEAKER_BACK_RIGHT 0x20
73 # define SPEAKER_FRONT_LEFT_OF_CENTER 0x40
74 # define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80
75 # define SPEAKER_BACK_CENTER 0x100
76 # define SPEAKER_SIDE_LEFT 0x200
77 # define SPEAKER_SIDE_RIGHT 0x400
78 # define SPEAKER_TOP_CENTER 0x800
79 # define SPEAKER_TOP_FRONT_LEFT 0x1000
80 # define SPEAKER_TOP_FRONT_CENTER 0x2000
81 # define SPEAKER_TOP_FRONT_RIGHT 0x4000
82 # define SPEAKER_TOP_BACK_LEFT 0x8000
83 # define SPEAKER_TOP_BACK_CENTER 0x10000
84 # define SPEAKER_TOP_BACK_RIGHT 0x20000
85 # define SPEAKER_RESERVED 0x80000000
88 #ifndef DSSPEAKER_DSSPEAKER_DIRECTOUT
89 # define DSSPEAKER_DSSPEAKER_DIRECTOUT 0x00000000
91 #ifndef DSSPEAKER_HEADPHONE
92 # define DSSPEAKER_HEADPHONE 0x00000001
94 #ifndef DSSPEAKER_MONO
95 # define DSSPEAKER_MONO 0x00000002
97 #ifndef DSSPEAKER_QUAD
98 # define DSSPEAKER_QUAD 0x00000003
100 #ifndef DSSPEAKER_STEREO
101 # define DSSPEAKER_STEREO 0x00000004
103 #ifndef DSSPEAKER_SURROUND
104 # define DSSPEAKER_SURROUND 0x00000005
106 #ifndef DSSPEAKER_5POINT1
107 # define DSSPEAKER_5POINT1 0x00000006
109 #ifndef DSSPEAKER_7POINT1
110 # define DSSPEAKER_7POINT1 0x00000007
112 #ifndef DSSPEAKER_7POINT1_SURROUND
113 # define DSSPEAKER_7POINT1_SURROUND 0x00000008
115 #ifndef DSSPEAKER_7POINT1_WIDE
116 # define DSSPEAKER_7POINT1_WIDE DSSPEAKER_7POINT1
119 #ifndef _WAVEFORMATEXTENSIBLE_
123 WORD wValidBitsPerSample; /* bits of precision */
124 WORD wSamplesPerBlock; /* valid if wBitsPerSample==0 */
125 WORD wReserved; /* If neither applies, set to zero. */
127 DWORD dwChannelMask; /* which channels are */
128 /* present in stream */
130 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
133 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
134 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_PCM, WAVE_FORMAT_PCM, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
135 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF, WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
137 /*****************************************************************************
138 * notification_thread_t: DirectX event thread
139 *****************************************************************************/
140 typedef struct notification_thread_t
144 aout_instance_t *p_aout;
145 int i_frame_size; /* size in bytes of one frame */
146 int i_write_slot; /* current write position in our circular buffer */
151 } notification_thread_t;
153 /*****************************************************************************
154 * aout_sys_t: directx audio output method descriptor
155 *****************************************************************************
156 * This structure is part of the audio output thread descriptor.
157 * It describes the direct sound specific properties of an audio device.
158 *****************************************************************************/
161 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
163 int i_device_id; /* user defined device */
164 LPGUID p_device_guid;
166 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
167 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
168 * takes care of mixing all the
169 * secondary buffers into the primary) */
171 notification_thread_t *p_notif; /* DirectSoundThread id */
173 int b_playing; /* playing status */
175 int i_frame_size; /* Size in bytes of one frame */
177 bool b_chan_reorder; /* do we need channel reordering */
178 int pi_chan_table[AOUT_CHAN_MAX];
179 uint32_t i_channel_mask;
180 uint32_t i_bits_per_sample;
184 static const uint32_t pi_channels_src[] =
185 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
186 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
187 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
188 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
189 static const uint32_t pi_channels_in[] =
190 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
191 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
192 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
193 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
194 static const uint32_t pi_channels_out[] =
195 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
196 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
197 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
198 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
200 /*****************************************************************************
202 *****************************************************************************/
203 static int OpenAudio ( vlc_object_t * );
204 static void CloseAudio ( vlc_object_t * );
205 static void Play ( aout_instance_t * );
207 /* local functions */
208 static void Probe ( aout_instance_t * );
209 static int InitDirectSound ( aout_instance_t * );
210 static int CreateDSBuffer ( aout_instance_t *, int, int, int, int, int, bool );
211 static int CreateDSBufferPCM ( aout_instance_t *, int*, int, int, int, bool );
212 static void DestroyDSBuffer ( aout_instance_t * );
213 static void DirectSoundThread ( notification_thread_t * );
214 static int FillBuffer ( aout_instance_t *, int, aout_buffer_t * );
216 /*****************************************************************************
218 *****************************************************************************/
219 #define DEVICE_TEXT N_("Output device")
220 #define DEVICE_LONGTEXT N_( \
221 "DirectX device number: 0 default device, 1..N device by number" \
222 "(Note that the default device appears as 0 AND another number)." )
223 #define FLOAT_TEXT N_("Use float32 output")
224 #define FLOAT_LONGTEXT N_( \
225 "The option allows you to enable or disable the high-quality float32 " \
226 "audio output mode (which is not well supported by some soundcards)." )
229 set_description( _("DirectX audio output") );
230 set_shortname( "DirectX" );
231 set_capability( "audio output", 100 );
232 set_category( CAT_AUDIO );
233 set_subcategory( SUBCAT_AUDIO_AOUT );
234 add_shortcut( "directx" );
235 add_integer( "directx-audio-device", 0, NULL, DEVICE_TEXT,
236 DEVICE_LONGTEXT, true );
237 add_bool( "directx-audio-float32", 0, 0, FLOAT_TEXT,
238 FLOAT_LONGTEXT, true );
239 set_callbacks( OpenAudio, CloseAudio );
242 /*****************************************************************************
243 * OpenAudio: open the audio device
244 *****************************************************************************
245 * This function opens and setups Direct Sound.
246 *****************************************************************************/
247 static int OpenAudio( vlc_object_t *p_this )
249 aout_instance_t * p_aout = (aout_instance_t *)p_this;
252 msg_Dbg( p_aout, "OpenAudio" );
254 /* Allocate structure */
255 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
256 if( p_aout->output.p_sys == NULL )
258 msg_Err( p_aout, "out of memory" );
262 /* Initialize some variables */
263 p_aout->output.p_sys->p_dsobject = NULL;
264 p_aout->output.p_sys->p_dsbuffer = NULL;
265 p_aout->output.p_sys->p_notif = NULL;
266 p_aout->output.p_sys->b_playing = 0;
268 p_aout->output.pf_play = Play;
269 aout_VolumeSoftInit( p_aout );
271 /* Retrieve config values */
272 var_Create( p_aout, "directx-audio-float32",
273 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
274 var_Create( p_aout, "directx-audio-device",
275 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
276 var_Get( p_aout, "directx-audio-device", &val );
277 p_aout->output.p_sys->i_device_id = val.i_int;
278 p_aout->output.p_sys->p_device_guid = 0;
280 /* Initialise DirectSound */
281 if( InitDirectSound( p_aout ) )
283 msg_Err( p_aout, "cannot initialize DirectSound" );
287 if( var_Type( p_aout, "audio-device" ) == 0 )
292 if( var_Get( p_aout, "audio-device", &val ) < 0 )
294 /* Probe() has failed. */
298 /* Open the device */
299 if( val.i_int == AOUT_VAR_SPDIF )
301 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
303 /* Calculate the frame size in bytes */
304 p_aout->output.i_nb_samples = A52_FRAME_NB;
305 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
306 p_aout->output.output.i_frame_length = A52_FRAME_NB;
307 p_aout->output.p_sys->i_frame_size =
308 p_aout->output.output.i_bytes_per_frame;
310 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
311 p_aout->output.output.i_physical_channels,
312 aout_FormatNbChannels( &p_aout->output.output ),
313 p_aout->output.output.i_rate,
314 p_aout->output.p_sys->i_frame_size, false )
317 msg_Err( p_aout, "cannot open directx audio device" );
318 free( p_aout->output.p_sys );
322 aout_VolumeNoneInit( p_aout );
326 if( val.i_int == AOUT_VAR_5_1 )
328 p_aout->output.output.i_physical_channels
329 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
330 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
333 else if( val.i_int == AOUT_VAR_7_1 )
335 p_aout->output.output.i_physical_channels
336 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
337 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
338 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
341 else if( val.i_int == AOUT_VAR_3F2R )
343 p_aout->output.output.i_physical_channels
344 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
345 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
347 else if( val.i_int == AOUT_VAR_2F2R )
349 p_aout->output.output.i_physical_channels
350 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
351 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
353 else if( val.i_int == AOUT_VAR_MONO )
355 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
359 p_aout->output.output.i_physical_channels
360 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
363 if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,
364 p_aout->output.output.i_physical_channels,
365 aout_FormatNbChannels( &p_aout->output.output ),
366 p_aout->output.output.i_rate, false )
369 msg_Err( p_aout, "cannot open directx audio device" );
370 free( p_aout->output.p_sys );
374 /* Calculate the frame size in bytes */
375 p_aout->output.i_nb_samples = FRAME_SIZE;
376 aout_FormatPrepare( &p_aout->output.output );
377 aout_VolumeSoftInit( p_aout );
380 /* Now we need to setup our DirectSound play notification structure */
381 p_aout->output.p_sys->p_notif =
382 vlc_object_create( p_aout, sizeof(notification_thread_t) );
383 p_aout->output.p_sys->p_notif->p_aout = p_aout;
385 p_aout->output.p_sys->p_notif->event = CreateEvent( 0, FALSE, FALSE, 0 );
386 p_aout->output.p_sys->p_notif->i_frame_size =
387 p_aout->output.p_sys->i_frame_size;
389 /* then launch the notification thread */
390 msg_Dbg( p_aout, "creating DirectSoundThread" );
391 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
392 "DirectSound Notification Thread",
394 VLC_THREAD_PRIORITY_HIGHEST, false ) )
396 msg_Err( p_aout, "cannot create DirectSoundThread" );
397 CloseHandle( p_aout->output.p_sys->p_notif->event );
398 vlc_object_release( p_aout->output.p_sys->p_notif );
399 p_aout->output.p_sys->p_notif = NULL;
403 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
408 CloseAudio( VLC_OBJECT(p_aout) );
412 /*****************************************************************************
413 * Probe: probe the audio device for available formats and channels
414 *****************************************************************************/
415 static void Probe( aout_instance_t * p_aout )
417 vlc_value_t val, text;
419 unsigned int i_physical_channels;
420 DWORD ui_speaker_config;
422 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
423 text.psz_string = _("Audio Device");
424 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
426 /* Test for 5.1 support */
427 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
428 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
429 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
430 if( p_aout->output.output.i_physical_channels == i_physical_channels )
432 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
433 p_aout->output.output.i_rate, true )
436 val.i_int = AOUT_VAR_5_1;
437 text.psz_string = "5.1";
438 var_Change( p_aout, "audio-device",
439 VLC_VAR_ADDCHOICE, &val, &text );
440 msg_Dbg( p_aout, "device supports 5.1 channels" );
444 /* Test for 7.1 support */
445 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
446 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
447 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
448 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
449 if( p_aout->output.output.i_physical_channels == i_physical_channels )
451 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
452 p_aout->output.output.i_rate, true )
455 val.i_int = AOUT_VAR_7_1;
456 text.psz_string = "7.1";
457 var_Change( p_aout, "audio-device",
458 VLC_VAR_ADDCHOICE, &val, &text );
459 msg_Dbg( p_aout, "device supports 7.1 channels" );
463 /* Test for 3 Front 2 Rear support */
464 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
465 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
467 if( p_aout->output.output.i_physical_channels == i_physical_channels )
469 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
470 p_aout->output.output.i_rate, true )
473 val.i_int = AOUT_VAR_3F2R;
474 text.psz_string = N_("3 Front 2 Rear");
475 var_Change( p_aout, "audio-device",
476 VLC_VAR_ADDCHOICE, &val, &text );
477 msg_Dbg( p_aout, "device supports 5 channels" );
481 /* Test for 2 Front 2 Rear support */
482 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
483 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
484 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
485 == i_physical_channels )
487 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
488 p_aout->output.output.i_rate, true )
491 val.i_int = AOUT_VAR_2F2R;
492 text.psz_string = N_("2 Front 2 Rear");
493 var_Change( p_aout, "audio-device",
494 VLC_VAR_ADDCHOICE, &val, &text );
495 msg_Dbg( p_aout, "device supports 4 channels" );
499 /* Test for stereo support */
500 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
501 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
502 p_aout->output.output.i_rate, true )
505 val.i_int = AOUT_VAR_STEREO;
506 text.psz_string = N_("Stereo");
507 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
508 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
509 msg_Dbg( p_aout, "device supports 2 channels" );
512 /* Test for mono support */
513 i_physical_channels = AOUT_CHAN_CENTER;
514 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
515 p_aout->output.output.i_rate, true )
518 val.i_int = AOUT_VAR_MONO;
519 text.psz_string = N_("Mono");
520 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
521 msg_Dbg( p_aout, "device supports 1 channel" );
524 /* Check the speaker configuration to determine which channel config should
526 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
527 &ui_speaker_config ) )
529 ui_speaker_config = DSSPEAKER_STEREO;
531 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
533 case DSSPEAKER_7POINT1:
534 val.i_int = AOUT_VAR_7_1;
536 case DSSPEAKER_5POINT1:
537 val.i_int = AOUT_VAR_5_1;
540 val.i_int = AOUT_VAR_2F2R;
542 #if 0 /* Lots of people just get their settings wrong and complain that
543 * this is a problem with VLC so just don't ever set mono by default. */
545 val.i_int = AOUT_VAR_MONO;
548 case DSSPEAKER_SURROUND:
549 case DSSPEAKER_STEREO:
551 val.i_int = AOUT_VAR_STEREO;
554 var_Set( p_aout, "audio-device", val );
556 /* Test for SPDIF support */
557 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
559 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
560 p_aout->output.output.i_physical_channels,
561 aout_FormatNbChannels( &p_aout->output.output ),
562 p_aout->output.output.i_rate,
563 AOUT_SPDIF_SIZE, true )
566 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
567 val.i_int = AOUT_VAR_SPDIF;
568 text.psz_string = N_("A/52 over S/PDIF");
569 var_Change( p_aout, "audio-device",
570 VLC_VAR_ADDCHOICE, &val, &text );
571 if( config_GetInt( p_aout, "spdif" ) )
572 var_Set( p_aout, "audio-device", val );
576 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
579 /* Probe() has failed. */
580 var_Destroy( p_aout, "audio-device" );
584 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
587 var_Set( p_aout, "intf-change", val );
590 /*****************************************************************************
591 * Play: we'll start playing the directsound buffer here because at least here
592 * we know the first buffer has been put in the aout fifo and we also
594 *****************************************************************************/
595 static void Play( aout_instance_t *p_aout )
597 if( !p_aout->output.p_sys->b_playing )
599 aout_buffer_t *p_buffer;
602 p_aout->output.p_sys->b_playing = 1;
604 /* get the playing date of the first aout buffer */
605 p_aout->output.p_sys->p_notif->start_date =
606 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
608 /* fill in the first samples */
609 for( i = 0; i < FRAMES_NUM; i++ )
611 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
612 if( !p_buffer ) break;
613 FillBuffer( p_aout, i, p_buffer );
616 /* wake up the audio output thread */
617 SetEvent( p_aout->output.p_sys->p_notif->event );
621 /*****************************************************************************
622 * CloseAudio: close the audio device
623 *****************************************************************************/
624 static void CloseAudio( vlc_object_t *p_this )
626 aout_instance_t * p_aout = (aout_instance_t *)p_this;
627 aout_sys_t *p_sys = p_aout->output.p_sys;
629 msg_Dbg( p_aout, "closing audio device" );
631 /* kill the position notification thread, if any */
634 vlc_object_detach( p_sys->p_notif );
635 vlc_object_kill( p_sys->p_notif );
636 /* wake up the audio thread if needed */
637 if( !p_sys->b_playing ) SetEvent( p_sys->p_notif->event );
639 vlc_thread_join( p_sys->p_notif );
640 vlc_object_release( p_sys->p_notif );
643 /* release the secondary buffer */
644 DestroyDSBuffer( p_aout );
646 /* finally release the DirectSound object */
647 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
649 /* free DSOUND.DLL */
650 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
652 free( p_aout->output.p_sys->p_device_guid );
656 /*****************************************************************************
657 * CallBackDirectSoundEnum: callback to enumerate available devices
658 *****************************************************************************/
659 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCSTR psz_desc,
660 LPCSTR psz_mod, LPVOID _p_aout )
662 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
664 msg_Dbg( p_aout, "found device: %s", psz_desc );
666 if( p_aout->output.p_sys->i_device_id == 0 && p_guid )
668 p_aout->output.p_sys->p_device_guid = malloc( sizeof( GUID ) );
669 *p_aout->output.p_sys->p_device_guid = *p_guid;
670 msg_Dbg( p_aout, "using device: %s", psz_desc );
673 p_aout->output.p_sys->i_device_id--;
677 /*****************************************************************************
678 * InitDirectSound: handle all the gory details of DirectSound initialisation
679 *****************************************************************************/
680 static int InitDirectSound( aout_instance_t *p_aout )
682 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
683 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACK, LPVOID);
685 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
686 if( p_aout->output.p_sys->hdsound_dll == NULL )
688 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
692 OurDirectSoundCreate = (void *)
693 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
694 "DirectSoundCreate" );
695 if( OurDirectSoundCreate == NULL )
697 msg_Warn( p_aout, "GetProcAddress FAILED" );
701 /* Get DirectSoundEnumerate */
702 OurDirectSoundEnumerate = (void *)
703 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
704 "DirectSoundEnumerateA" );
705 if( OurDirectSoundEnumerate )
707 /* Attempt enumeration */
708 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
711 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
715 /* Create the direct sound object */
716 if FAILED( OurDirectSoundCreate( p_aout->output.p_sys->p_device_guid,
717 &p_aout->output.p_sys->p_dsobject,
720 msg_Warn( p_aout, "cannot create a direct sound device" );
724 /* Set DirectSound Cooperative level, ie what control we want over Windows
725 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
726 * settings of the primary buffer, but also that only the sound of our
727 * application will be hearable when it will have the focus.
728 * !!! (this is not really working as intended yet because to set the
729 * cooperative level you need the window handle of your application, and
730 * I don't know of any easy way to get it. Especially since we might play
731 * sound without any video, and so what window handle should we use ???
732 * The hack for now is to use the Desktop window handle - it seems to be
734 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
738 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
744 p_aout->output.p_sys->p_dsobject = NULL;
745 if( p_aout->output.p_sys->hdsound_dll )
747 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
748 p_aout->output.p_sys->hdsound_dll = NULL;
754 /*****************************************************************************
755 * CreateDSBuffer: Creates a direct sound buffer of the required format.
756 *****************************************************************************
757 * This function creates the buffer we'll use to play audio.
758 * In DirectSound there are two kinds of buffers:
759 * - the primary buffer: which is the actual buffer that the soundcard plays
760 * - the secondary buffer(s): these buffers are the one actually used by
761 * applications and DirectSound takes care of mixing them into the primary.
763 * Once you create a secondary buffer, you cannot change its format anymore so
764 * you have to release the current one and create another.
765 *****************************************************************************/
766 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
767 int i_channels, int i_nb_channels, int i_rate,
768 int i_bytes_per_frame, bool b_probe )
770 WAVEFORMATEXTENSIBLE waveformat;
771 DSBUFFERDESC dsbdesc;
774 /* First set the sound buffer format */
775 waveformat.dwChannelMask = 0;
776 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
778 if( i_channels & pi_channels_src[i] )
779 waveformat.dwChannelMask |= pi_channels_in[i];
784 case VLC_FOURCC('s','p','d','i'):
786 /* To prevent channel re-ordering */
787 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
788 waveformat.Format.wBitsPerSample = 16;
789 waveformat.Samples.wValidBitsPerSample =
790 waveformat.Format.wBitsPerSample;
791 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
792 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
795 case VLC_FOURCC('f','l','3','2'):
796 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
797 waveformat.Samples.wValidBitsPerSample =
798 waveformat.Format.wBitsPerSample;
799 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
800 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
803 case VLC_FOURCC('s','1','6','l'):
804 waveformat.Format.wBitsPerSample = 16;
805 waveformat.Samples.wValidBitsPerSample =
806 waveformat.Format.wBitsPerSample;
807 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
808 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
812 waveformat.Format.nChannels = i_nb_channels;
813 waveformat.Format.nSamplesPerSec = i_rate;
814 waveformat.Format.nBlockAlign =
815 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
816 waveformat.Format.nAvgBytesPerSec =
817 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
819 p_aout->output.p_sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
820 p_aout->output.p_sys->i_channels = i_nb_channels;
822 /* Then fill in the direct sound descriptor */
823 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
824 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
825 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
826 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
828 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
829 if( i_nb_channels <= 2 )
831 waveformat.Format.cbSize = 0;
835 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
836 waveformat.Format.cbSize =
837 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
839 /* Needed for 5.1 on emu101k */
840 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
843 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
844 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
846 if FAILED( IDirectSound_CreateSoundBuffer(
847 p_aout->output.p_sys->p_dsobject, &dsbdesc,
848 &p_aout->output.p_sys->p_dsbuffer, NULL) )
850 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
852 /* Try without DSBCAPS_LOCHARDWARE */
853 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
854 if FAILED( IDirectSound_CreateSoundBuffer(
855 p_aout->output.p_sys->p_dsobject, &dsbdesc,
856 &p_aout->output.p_sys->p_dsbuffer, NULL) )
861 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
869 /* Stop here if we were just probing */
872 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
873 p_aout->output.p_sys->p_dsbuffer = NULL;
877 p_aout->output.p_sys->i_frame_size = i_bytes_per_frame;
878 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
879 p_aout->output.p_sys->b_chan_reorder =
880 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
881 waveformat.dwChannelMask, i_nb_channels,
882 p_aout->output.p_sys->pi_chan_table );
884 if( p_aout->output.p_sys->b_chan_reorder )
886 msg_Dbg( p_aout, "channel reordering needed" );
892 /*****************************************************************************
893 * CreateDSBufferPCM: creates a PCM direct sound buffer.
894 *****************************************************************************
895 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
896 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
897 ****************************************************************************/
898 static int CreateDSBufferPCM( aout_instance_t *p_aout, int *i_format,
899 int i_channels, int i_nb_channels, int i_rate,
904 var_Get( p_aout, "directx-audio-float32", &val );
906 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
908 if( !val.b_bool || i_nb_channels > 2 ||
909 CreateDSBuffer( p_aout, VLC_FOURCC('f','l','3','2'),
910 i_channels, i_nb_channels, i_rate,
911 FRAME_SIZE * 4 * i_nb_channels, b_probe )
914 if ( CreateDSBuffer( p_aout, VLC_FOURCC('s','1','6','l'),
915 i_channels, i_nb_channels, i_rate,
916 FRAME_SIZE * 2 * i_nb_channels, b_probe )
923 *i_format = VLC_FOURCC('s','1','6','l');
929 *i_format = VLC_FOURCC('f','l','3','2');
934 /*****************************************************************************
936 *****************************************************************************
937 * This function destroys the secondary buffer.
938 *****************************************************************************/
939 static void DestroyDSBuffer( aout_instance_t *p_aout )
941 if( p_aout->output.p_sys->p_dsbuffer )
943 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
944 p_aout->output.p_sys->p_dsbuffer = NULL;
948 /*****************************************************************************
949 * FillBuffer: Fill in one of the direct sound frame buffers.
950 *****************************************************************************
951 * Returns VLC_SUCCESS on success.
952 *****************************************************************************/
953 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
954 aout_buffer_t *p_buffer )
956 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
957 aout_sys_t *p_sys = p_aout->output.p_sys;
958 void *p_write_position, *p_wrap_around;
959 long l_bytes1, l_bytes2;
962 /* Before copying anything, we have to lock the buffer */
963 dsresult = IDirectSoundBuffer_Lock(
964 p_sys->p_dsbuffer, /* DS buffer */
965 i_frame * p_notif->i_frame_size, /* Start offset */
966 p_notif->i_frame_size, /* Number of bytes */
967 &p_write_position, /* Address of lock start */
968 &l_bytes1, /* Count of bytes locked before wrap around */
969 &p_wrap_around, /* Buffer adress (if wrap around) */
970 &l_bytes2, /* Count of bytes after wrap around */
972 if( dsresult == DSERR_BUFFERLOST )
974 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
975 dsresult = IDirectSoundBuffer_Lock(
977 i_frame * p_notif->i_frame_size,
978 p_notif->i_frame_size,
985 if( dsresult != DS_OK )
987 msg_Warn( p_notif, "cannot lock buffer" );
988 if( p_buffer ) aout_BufferFree( p_buffer );
992 if( p_buffer == NULL )
994 memset( p_write_position, 0, l_bytes1 );
998 if( p_sys->b_chan_reorder )
1000 /* Do the channel reordering here */
1001 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
1002 p_sys->i_channels, p_sys->pi_chan_table,
1003 p_sys->i_bits_per_sample );
1006 vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
1007 aout_BufferFree( p_buffer );
1010 /* Now the data has been copied, unlock the buffer */
1011 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1012 p_wrap_around, l_bytes2 );
1014 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1018 /*****************************************************************************
1019 * DirectSoundThread: this thread will capture play notification events.
1020 *****************************************************************************
1021 * We use this thread to emulate a callback mechanism. The thread probes for
1022 * event notification and fills up the DS secondary buffer when needed.
1023 *****************************************************************************/
1024 static void DirectSoundThread( notification_thread_t *p_notif )
1026 aout_instance_t *p_aout = p_notif->p_aout;
1032 /* We don't want any resampling when using S/PDIF output */
1033 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
1035 /* Tell the main thread that we are ready */
1036 vlc_thread_ready( p_notif );
1038 msg_Dbg( p_notif, "DirectSoundThread ready" );
1040 /* Wait here until Play() is called */
1041 WaitForSingleObject( p_notif->event, INFINITE );
1043 if( !p_notif->b_die )
1045 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
1047 /* start playing the buffer */
1048 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
1051 DSBPLAY_LOOPING ); /* Flags */
1052 if( dsresult == DSERR_BUFFERLOST )
1054 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
1055 dsresult = IDirectSoundBuffer_Play(
1056 p_aout->output.p_sys->p_dsbuffer,
1059 DSBPLAY_LOOPING ); /* Flags */
1061 if( dsresult != DS_OK )
1063 msg_Err( p_aout, "cannot start playing buffer" );
1066 last_time = mdate();
1068 while( !p_notif->b_die )
1070 long l_read, l_free_slots;
1071 mtime_t mtime = mdate();
1075 * Fill in as much audio data as we can in our circular buffer
1078 /* Find out current play position */
1079 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1080 p_aout->output.p_sys->p_dsbuffer, &l_read, NULL ) )
1082 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1086 /* Detect underruns */
1087 if( l_queued && mtime - last_time >
1088 INT64_C(1000000) * l_queued / p_aout->output.output.i_rate )
1090 msg_Dbg( p_aout, "detected underrun!" );
1094 /* Try to fill in as many frame buffers as possible */
1095 l_read /= p_aout->output.output.i_bytes_per_frame;
1096 l_queued = p_notif->i_write_slot * FRAME_SIZE - l_read;
1097 if( l_queued < 0 ) l_queued += (FRAME_SIZE * FRAMES_NUM);
1098 l_free_slots = (FRAMES_NUM * FRAME_SIZE - l_queued) / FRAME_SIZE;
1100 for( i = 0; i < l_free_slots; i++ )
1102 aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout,
1103 mtime + INT64_C(1000000) * (i * FRAME_SIZE + l_queued) /
1104 p_aout->output.output.i_rate, b_sleek );
1106 /* If there is no audio data available and we have some buffered
1107 * already, then just wait for the next time */
1108 if( !p_buffer && (i || l_queued / FRAME_SIZE) ) break;
1110 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1111 p_buffer ) != VLC_SUCCESS ) break;
1114 /* Sleep a reasonable amount of time */
1115 l_queued += (i * FRAME_SIZE);
1116 msleep( INT64_C(1000000) * l_queued / p_aout->output.output.i_rate / 2 );
1119 /* make sure the buffer isn't playing */
1120 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
1122 /* free the event */
1123 CloseHandle( p_notif->event );
1125 msg_Dbg( p_notif, "DirectSoundThread exiting" );