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 *****************************************************************************/
27 #include <string.h> /* strerror() */
29 #include <stdlib.h> /* calloc(), malloc(), free() */
38 #define FRAME_SIZE ((int)p_aout->output.output.i_rate/20) /* Size in samples */
39 #define FRAMES_NUM 8 /* Needs to be > 3 */
41 /*****************************************************************************
43 * Defining them here allows us to get rid of the dxguid library during
45 *****************************************************************************/
48 /*****************************************************************************
50 *****************************************************************************/
51 #ifndef WAVE_FORMAT_IEEE_FLOAT
52 # define WAVE_FORMAT_IEEE_FLOAT 0x0003
55 #ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
56 # define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
59 #ifndef WAVE_FORMAT_EXTENSIBLE
60 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
63 #ifndef SPEAKER_FRONT_LEFT
64 # define SPEAKER_FRONT_LEFT 0x1
65 # define SPEAKER_FRONT_RIGHT 0x2
66 # define SPEAKER_FRONT_CENTER 0x4
67 # define SPEAKER_LOW_FREQUENCY 0x8
68 # define SPEAKER_BACK_LEFT 0x10
69 # define SPEAKER_BACK_RIGHT 0x20
70 # define SPEAKER_FRONT_LEFT_OF_CENTER 0x40
71 # define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80
72 # define SPEAKER_BACK_CENTER 0x100
73 # define SPEAKER_SIDE_LEFT 0x200
74 # define SPEAKER_SIDE_RIGHT 0x400
75 # define SPEAKER_TOP_CENTER 0x800
76 # define SPEAKER_TOP_FRONT_LEFT 0x1000
77 # define SPEAKER_TOP_FRONT_CENTER 0x2000
78 # define SPEAKER_TOP_FRONT_RIGHT 0x4000
79 # define SPEAKER_TOP_BACK_LEFT 0x8000
80 # define SPEAKER_TOP_BACK_CENTER 0x10000
81 # define SPEAKER_TOP_BACK_RIGHT 0x20000
82 # define SPEAKER_RESERVED 0x80000000
85 #ifndef DSSPEAKER_HEADPHONE
86 # define DSSPEAKER_HEADPHONE 0x00000001
88 #ifndef DSSPEAKER_MONO
89 # define DSSPEAKER_MONO 0x00000002
91 #ifndef DSSPEAKER_QUAD
92 # define DSSPEAKER_QUAD 0x00000003
94 #ifndef DSSPEAKER_STEREO
95 # define DSSPEAKER_STEREO 0x00000004
97 #ifndef DSSPEAKER_SURROUND
98 # define DSSPEAKER_SURROUND 0x00000005
100 #ifndef DSSPEAKER_5POINT1
101 # define DSSPEAKER_5POINT1 0x00000006
104 #ifndef _WAVEFORMATEXTENSIBLE_
108 WORD wValidBitsPerSample; /* bits of precision */
109 WORD wSamplesPerBlock; /* valid if wBitsPerSample==0 */
110 WORD wReserved; /* If neither applies, set to zero. */
112 DWORD dwChannelMask; /* which channels are */
113 /* present in stream */
115 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
118 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
119 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_PCM, WAVE_FORMAT_PCM, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
120 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF, WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
122 /*****************************************************************************
123 * notification_thread_t: DirectX event thread
124 *****************************************************************************/
125 typedef struct notification_thread_t
129 aout_instance_t *p_aout;
130 int i_frame_size; /* size in bytes of one frame */
131 int i_write_slot; /* current write position in our circular buffer */
136 } notification_thread_t;
138 /*****************************************************************************
139 * aout_sys_t: directx audio output method descriptor
140 *****************************************************************************
141 * This structure is part of the audio output thread descriptor.
142 * It describes the direct sound specific properties of an audio device.
143 *****************************************************************************/
146 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
148 int i_device_id; /* user defined device */
149 LPGUID p_device_guid;
151 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
152 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
153 * takes care of mixing all the
154 * secondary buffers into the primary) */
156 notification_thread_t *p_notif; /* DirectSoundThread id */
158 int b_playing; /* playing status */
160 int i_frame_size; /* Size in bytes of one frame */
162 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
163 int pi_chan_table[AOUT_CHAN_MAX];
164 uint32_t i_channel_mask;
165 uint32_t i_bits_per_sample;
169 static const uint32_t pi_channels_src[] =
170 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
171 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
172 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
173 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
174 static const uint32_t pi_channels_in[] =
175 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
176 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
177 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
178 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
179 static const uint32_t pi_channels_out[] =
180 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
181 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
182 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
183 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
185 /*****************************************************************************
187 *****************************************************************************/
188 static int OpenAudio ( vlc_object_t * );
189 static void CloseAudio ( vlc_object_t * );
190 static void Play ( aout_instance_t * );
192 /* local functions */
193 static void Probe ( aout_instance_t * );
194 static int InitDirectSound ( aout_instance_t * );
195 static int CreateDSBuffer ( aout_instance_t *, int, int, int, int, int, vlc_bool_t );
196 static int CreateDSBufferPCM ( aout_instance_t *, int*, int, int, int, vlc_bool_t );
197 static void DestroyDSBuffer ( aout_instance_t * );
198 static void DirectSoundThread ( notification_thread_t * );
199 static int FillBuffer ( aout_instance_t *, int, aout_buffer_t * );
201 /*****************************************************************************
203 *****************************************************************************/
204 #define DEVICE_TEXT N_("Output device")
205 #define DEVICE_LONGTEXT N_( \
206 "DirectX device number: 0 default device, 1..N device by number" \
207 "(Note that the default device appears as 0 AND another number)." )
208 #define FLOAT_TEXT N_("Use float32 output")
209 #define FLOAT_LONGTEXT N_( \
210 "The option allows you to enable or disable the high-quality float32 " \
211 "audio output mode (which is not well supported by some soundcards)." )
214 set_description( _("DirectX audio output") );
215 set_shortname( "DirectX" );
216 set_capability( "audio output", 100 );
217 set_category( CAT_AUDIO );
218 set_subcategory( SUBCAT_AUDIO_AOUT );
219 add_shortcut( "directx" );
220 add_integer( "directx-audio-device", 0, NULL, DEVICE_TEXT,
221 DEVICE_LONGTEXT, VLC_TRUE );
222 add_bool( "directx-audio-float32", 0, 0, FLOAT_TEXT,
223 FLOAT_LONGTEXT, VLC_TRUE );
224 set_callbacks( OpenAudio, CloseAudio );
227 /*****************************************************************************
228 * OpenAudio: open the audio device
229 *****************************************************************************
230 * This function opens and setups Direct Sound.
231 *****************************************************************************/
232 static int OpenAudio( vlc_object_t *p_this )
234 aout_instance_t * p_aout = (aout_instance_t *)p_this;
237 msg_Dbg( p_aout, "OpenAudio" );
239 /* Allocate structure */
240 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
241 if( p_aout->output.p_sys == NULL )
243 msg_Err( p_aout, "out of memory" );
247 /* Initialize some variables */
248 p_aout->output.p_sys->p_dsobject = NULL;
249 p_aout->output.p_sys->p_dsbuffer = NULL;
250 p_aout->output.p_sys->p_notif = NULL;
251 p_aout->output.p_sys->b_playing = 0;
253 p_aout->output.pf_play = Play;
254 aout_VolumeSoftInit( p_aout );
256 /* Retrieve config values */
257 var_Create( p_aout, "directx-audio-float32",
258 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
259 var_Create( p_aout, "directx-audio-device",
260 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
261 var_Get( p_aout, "directx-audio-device", &val );
262 p_aout->output.p_sys->i_device_id = val.i_int;
263 p_aout->output.p_sys->p_device_guid = 0;
265 /* Initialise DirectSound */
266 if( InitDirectSound( p_aout ) )
268 msg_Err( p_aout, "cannot initialize DirectSound" );
272 if( var_Type( p_aout, "audio-device" ) == 0 )
277 if( var_Get( p_aout, "audio-device", &val ) < 0 )
279 /* Probe() has failed. */
283 /* Open the device */
284 if( val.i_int == AOUT_VAR_SPDIF )
286 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
288 /* Calculate the frame size in bytes */
289 p_aout->output.i_nb_samples = A52_FRAME_NB;
290 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
291 p_aout->output.output.i_frame_length = A52_FRAME_NB;
292 p_aout->output.p_sys->i_frame_size =
293 p_aout->output.output.i_bytes_per_frame;
295 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
296 p_aout->output.output.i_physical_channels,
297 aout_FormatNbChannels( &p_aout->output.output ),
298 p_aout->output.output.i_rate,
299 p_aout->output.p_sys->i_frame_size, VLC_FALSE )
302 msg_Err( p_aout, "cannot open directx audio device" );
303 free( p_aout->output.p_sys );
307 aout_VolumeNoneInit( p_aout );
311 if( val.i_int == AOUT_VAR_5_1 )
313 p_aout->output.output.i_physical_channels
314 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
315 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
318 else if( val.i_int == AOUT_VAR_3F2R )
320 p_aout->output.output.i_physical_channels
321 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
322 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
324 else if( val.i_int == AOUT_VAR_2F2R )
326 p_aout->output.output.i_physical_channels
327 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
328 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
330 else if( val.i_int == AOUT_VAR_MONO )
332 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
336 p_aout->output.output.i_physical_channels
337 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
340 if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,
341 p_aout->output.output.i_physical_channels,
342 aout_FormatNbChannels( &p_aout->output.output ),
343 p_aout->output.output.i_rate, VLC_FALSE )
346 msg_Err( p_aout, "cannot open directx audio device" );
347 free( p_aout->output.p_sys );
351 /* Calculate the frame size in bytes */
352 p_aout->output.i_nb_samples = FRAME_SIZE;
353 aout_FormatPrepare( &p_aout->output.output );
354 aout_VolumeSoftInit( p_aout );
357 /* Now we need to setup our DirectSound play notification structure */
358 p_aout->output.p_sys->p_notif =
359 vlc_object_create( p_aout, sizeof(notification_thread_t) );
360 p_aout->output.p_sys->p_notif->p_aout = p_aout;
362 p_aout->output.p_sys->p_notif->event = CreateEvent( 0, FALSE, FALSE, 0 );
363 p_aout->output.p_sys->p_notif->i_frame_size =
364 p_aout->output.p_sys->i_frame_size;
366 /* then launch the notification thread */
367 msg_Dbg( p_aout, "creating DirectSoundThread" );
368 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
369 "DirectSound Notification Thread",
371 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
373 msg_Err( p_aout, "cannot create DirectSoundThread" );
374 CloseHandle( p_aout->output.p_sys->p_notif->event );
375 vlc_object_destroy( p_aout->output.p_sys->p_notif );
376 p_aout->output.p_sys->p_notif = 0;
380 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
385 CloseAudio( VLC_OBJECT(p_aout) );
389 /*****************************************************************************
390 * Probe: probe the audio device for available formats and channels
391 *****************************************************************************/
392 static void Probe( aout_instance_t * p_aout )
394 vlc_value_t val, text;
396 unsigned int i_physical_channels;
397 DWORD ui_speaker_config;
399 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
400 text.psz_string = _("Audio Device");
401 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
403 /* Test for 5.1 support */
404 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
405 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
406 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
407 if( p_aout->output.output.i_physical_channels == i_physical_channels )
409 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
410 p_aout->output.output.i_rate, VLC_TRUE )
413 val.i_int = AOUT_VAR_5_1;
414 text.psz_string = "5.1";
415 var_Change( p_aout, "audio-device",
416 VLC_VAR_ADDCHOICE, &val, &text );
417 msg_Dbg( p_aout, "device supports 5.1 channels" );
421 /* Test for 3 Front 2 Rear support */
422 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
423 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
425 if( p_aout->output.output.i_physical_channels == i_physical_channels )
427 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
428 p_aout->output.output.i_rate, VLC_TRUE )
431 val.i_int = AOUT_VAR_3F2R;
432 text.psz_string = N_("3 Front 2 Rear");
433 var_Change( p_aout, "audio-device",
434 VLC_VAR_ADDCHOICE, &val, &text );
435 msg_Dbg( p_aout, "device supports 5 channels" );
439 /* Test for 2 Front 2 Rear support */
440 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
441 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
442 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
443 == i_physical_channels )
445 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
446 p_aout->output.output.i_rate, VLC_TRUE )
449 val.i_int = AOUT_VAR_2F2R;
450 text.psz_string = N_("2 Front 2 Rear");
451 var_Change( p_aout, "audio-device",
452 VLC_VAR_ADDCHOICE, &val, &text );
453 msg_Dbg( p_aout, "device supports 4 channels" );
457 /* Test for stereo support */
458 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
459 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
460 p_aout->output.output.i_rate, VLC_TRUE )
463 val.i_int = AOUT_VAR_STEREO;
464 text.psz_string = N_("Stereo");
465 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
466 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
467 msg_Dbg( p_aout, "device supports 2 channels" );
470 /* Test for mono support */
471 i_physical_channels = AOUT_CHAN_CENTER;
472 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
473 p_aout->output.output.i_rate, VLC_TRUE )
476 val.i_int = AOUT_VAR_MONO;
477 text.psz_string = N_("Mono");
478 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
479 msg_Dbg( p_aout, "device supports 1 channel" );
482 /* Check the speaker configuration to determine which channel config should
484 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
485 &ui_speaker_config ) )
487 ui_speaker_config = DSSPEAKER_STEREO;
489 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
491 case DSSPEAKER_5POINT1:
492 val.i_int = AOUT_VAR_5_1;
495 val.i_int = AOUT_VAR_2F2R;
497 #if 0 /* Lots of people just get their settings wrong and complain that
498 * this is a problem with VLC so just don't ever set mono by default. */
500 val.i_int = AOUT_VAR_MONO;
503 case DSSPEAKER_SURROUND:
504 case DSSPEAKER_STEREO:
506 val.i_int = AOUT_VAR_STEREO;
509 var_Set( p_aout, "audio-device", val );
511 /* Test for SPDIF support */
512 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
514 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
515 p_aout->output.output.i_physical_channels,
516 aout_FormatNbChannels( &p_aout->output.output ),
517 p_aout->output.output.i_rate,
518 AOUT_SPDIF_SIZE, VLC_TRUE )
521 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
522 val.i_int = AOUT_VAR_SPDIF;
523 text.psz_string = N_("A/52 over S/PDIF");
524 var_Change( p_aout, "audio-device",
525 VLC_VAR_ADDCHOICE, &val, &text );
526 if( config_GetInt( p_aout, "spdif" ) )
527 var_Set( p_aout, "audio-device", val );
531 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
534 /* Probe() has failed. */
535 var_Destroy( p_aout, "audio-device" );
539 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
541 val.b_bool = VLC_TRUE;
542 var_Set( p_aout, "intf-change", val );
545 /*****************************************************************************
546 * Play: we'll start playing the directsound buffer here because at least here
547 * we know the first buffer has been put in the aout fifo and we also
549 *****************************************************************************/
550 static void Play( aout_instance_t *p_aout )
552 if( !p_aout->output.p_sys->b_playing )
554 aout_buffer_t *p_buffer;
557 p_aout->output.p_sys->b_playing = 1;
559 /* get the playing date of the first aout buffer */
560 p_aout->output.p_sys->p_notif->start_date =
561 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
563 /* fill in the first samples */
564 for( i = 0; i < FRAMES_NUM; i++ )
566 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
567 if( !p_buffer ) break;
568 FillBuffer( p_aout, i, p_buffer );
571 /* wake up the audio output thread */
572 SetEvent( p_aout->output.p_sys->p_notif->event );
576 /*****************************************************************************
577 * CloseAudio: close the audio device
578 *****************************************************************************/
579 static void CloseAudio( vlc_object_t *p_this )
581 aout_instance_t * p_aout = (aout_instance_t *)p_this;
582 aout_sys_t *p_sys = p_aout->output.p_sys;
584 msg_Dbg( p_aout, "closing audio device" );
586 /* kill the position notification thread, if any */
589 vlc_object_detach( p_sys->p_notif );
590 if( p_sys->p_notif->b_thread )
592 p_sys->p_notif->b_die = 1;
594 /* wake up the audio thread if needed */
595 if( !p_sys->b_playing ) SetEvent( p_sys->p_notif->event );
597 vlc_thread_join( p_sys->p_notif );
599 vlc_object_destroy( p_sys->p_notif );
602 /* release the secondary buffer */
603 DestroyDSBuffer( p_aout );
605 /* finally release the DirectSound object */
606 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
608 /* free DSOUND.DLL */
609 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
611 if( p_aout->output.p_sys->p_device_guid )
612 free( p_aout->output.p_sys->p_device_guid );
617 /*****************************************************************************
618 * CallBackDirectSoundEnum: callback to enumerate available devices
619 *****************************************************************************/
620 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCSTR psz_desc,
621 LPCSTR psz_mod, LPVOID _p_aout )
623 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
625 msg_Dbg( p_aout, "found device: %s", psz_desc );
627 if( p_aout->output.p_sys->i_device_id == 0 && p_guid )
629 p_aout->output.p_sys->p_device_guid = malloc( sizeof( GUID ) );
630 *p_aout->output.p_sys->p_device_guid = *p_guid;
631 msg_Dbg( p_aout, "using device: %s", psz_desc );
634 p_aout->output.p_sys->i_device_id--;
638 /*****************************************************************************
639 * InitDirectSound: handle all the gory details of DirectSound initialisation
640 *****************************************************************************/
641 static int InitDirectSound( aout_instance_t *p_aout )
643 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
644 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACK, LPVOID);
646 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
647 if( p_aout->output.p_sys->hdsound_dll == NULL )
649 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
653 OurDirectSoundCreate = (void *)
654 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
655 "DirectSoundCreate" );
656 if( OurDirectSoundCreate == NULL )
658 msg_Warn( p_aout, "GetProcAddress FAILED" );
662 /* Get DirectSoundEnumerate */
663 OurDirectSoundEnumerate = (void *)
664 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
665 "DirectSoundEnumerateA" );
666 if( OurDirectSoundEnumerate )
668 /* Attempt enumeration */
669 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
672 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
676 /* Create the direct sound object */
677 if FAILED( OurDirectSoundCreate( p_aout->output.p_sys->p_device_guid,
678 &p_aout->output.p_sys->p_dsobject,
681 msg_Warn( p_aout, "cannot create a direct sound device" );
685 /* Set DirectSound Cooperative level, ie what control we want over Windows
686 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
687 * settings of the primary buffer, but also that only the sound of our
688 * application will be hearable when it will have the focus.
689 * !!! (this is not really working as intended yet because to set the
690 * cooperative level you need the window handle of your application, and
691 * I don't know of any easy way to get it. Especially since we might play
692 * sound without any video, and so what window handle should we use ???
693 * The hack for now is to use the Desktop window handle - it seems to be
695 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
699 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
705 p_aout->output.p_sys->p_dsobject = NULL;
706 if( p_aout->output.p_sys->hdsound_dll )
708 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
709 p_aout->output.p_sys->hdsound_dll = NULL;
715 /*****************************************************************************
716 * CreateDSBuffer: Creates a direct sound buffer of the required format.
717 *****************************************************************************
718 * This function creates the buffer we'll use to play audio.
719 * In DirectSound there are two kinds of buffers:
720 * - the primary buffer: which is the actual buffer that the soundcard plays
721 * - the secondary buffer(s): these buffers are the one actually used by
722 * applications and DirectSound takes care of mixing them into the primary.
724 * Once you create a secondary buffer, you cannot change its format anymore so
725 * you have to release the current one and create another.
726 *****************************************************************************/
727 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
728 int i_channels, int i_nb_channels, int i_rate,
729 int i_bytes_per_frame, vlc_bool_t b_probe )
731 WAVEFORMATEXTENSIBLE waveformat;
732 DSBUFFERDESC dsbdesc;
735 /* First set the sound buffer format */
736 waveformat.dwChannelMask = 0;
737 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
739 if( i_channels & pi_channels_src[i] )
740 waveformat.dwChannelMask |= pi_channels_in[i];
745 case VLC_FOURCC('s','p','d','i'):
747 /* To prevent channel re-ordering */
748 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
749 waveformat.Format.wBitsPerSample = 16;
750 waveformat.Samples.wValidBitsPerSample =
751 waveformat.Format.wBitsPerSample;
752 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
753 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
756 case VLC_FOURCC('f','l','3','2'):
757 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
758 waveformat.Samples.wValidBitsPerSample =
759 waveformat.Format.wBitsPerSample;
760 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
761 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
764 case VLC_FOURCC('s','1','6','l'):
765 waveformat.Format.wBitsPerSample = 16;
766 waveformat.Samples.wValidBitsPerSample =
767 waveformat.Format.wBitsPerSample;
768 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
769 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
773 waveformat.Format.nChannels = i_nb_channels;
774 waveformat.Format.nSamplesPerSec = i_rate;
775 waveformat.Format.nBlockAlign =
776 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
777 waveformat.Format.nAvgBytesPerSec =
778 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
780 p_aout->output.p_sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
781 p_aout->output.p_sys->i_channels = i_nb_channels;
783 /* Then fill in the direct sound descriptor */
784 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
785 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
786 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
787 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
789 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
790 if( i_nb_channels <= 2 )
792 waveformat.Format.cbSize = 0;
796 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
797 waveformat.Format.cbSize =
798 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
800 /* Needed for 5.1 on emu101k */
801 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
804 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
805 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
807 if FAILED( IDirectSound_CreateSoundBuffer(
808 p_aout->output.p_sys->p_dsobject, &dsbdesc,
809 &p_aout->output.p_sys->p_dsbuffer, NULL) )
811 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
813 /* Try without DSBCAPS_LOCHARDWARE */
814 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
815 if FAILED( IDirectSound_CreateSoundBuffer(
816 p_aout->output.p_sys->p_dsobject, &dsbdesc,
817 &p_aout->output.p_sys->p_dsbuffer, NULL) )
822 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
830 /* Stop here if we were just probing */
833 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
834 p_aout->output.p_sys->p_dsbuffer = NULL;
838 p_aout->output.p_sys->i_frame_size = i_bytes_per_frame;
839 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
840 p_aout->output.p_sys->b_chan_reorder =
841 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
842 waveformat.dwChannelMask, i_nb_channels,
843 p_aout->output.p_sys->pi_chan_table );
845 if( p_aout->output.p_sys->b_chan_reorder )
847 msg_Dbg( p_aout, "channel reordering needed" );
853 /*****************************************************************************
854 * CreateDSBufferPCM: creates a PCM direct sound buffer.
855 *****************************************************************************
856 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
857 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
858 ****************************************************************************/
859 static int CreateDSBufferPCM( aout_instance_t *p_aout, int *i_format,
860 int i_channels, int i_nb_channels, int i_rate,
865 var_Get( p_aout, "directx-audio-float32", &val );
867 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
869 if( !val.b_bool || i_nb_channels > 2 ||
870 CreateDSBuffer( p_aout, VLC_FOURCC('f','l','3','2'),
871 i_channels, i_nb_channels, i_rate,
872 FRAME_SIZE * 4 * i_nb_channels, b_probe )
875 if ( CreateDSBuffer( p_aout, VLC_FOURCC('s','1','6','l'),
876 i_channels, i_nb_channels, i_rate,
877 FRAME_SIZE * 2 * i_nb_channels, b_probe )
884 *i_format = VLC_FOURCC('s','1','6','l');
890 *i_format = VLC_FOURCC('f','l','3','2');
895 /*****************************************************************************
897 *****************************************************************************
898 * This function destroys the secondary buffer.
899 *****************************************************************************/
900 static void DestroyDSBuffer( aout_instance_t *p_aout )
902 if( p_aout->output.p_sys->p_dsbuffer )
904 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
905 p_aout->output.p_sys->p_dsbuffer = NULL;
909 /*****************************************************************************
910 * FillBuffer: Fill in one of the direct sound frame buffers.
911 *****************************************************************************
912 * Returns VLC_SUCCESS on success.
913 *****************************************************************************/
914 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
915 aout_buffer_t *p_buffer )
917 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
918 aout_sys_t *p_sys = p_aout->output.p_sys;
919 void *p_write_position, *p_wrap_around;
920 long l_bytes1, l_bytes2;
923 /* Before copying anything, we have to lock the buffer */
924 dsresult = IDirectSoundBuffer_Lock(
925 p_sys->p_dsbuffer, /* DS buffer */
926 i_frame * p_notif->i_frame_size, /* Start offset */
927 p_notif->i_frame_size, /* Number of bytes */
928 &p_write_position, /* Address of lock start */
929 &l_bytes1, /* Count of bytes locked before wrap around */
930 &p_wrap_around, /* Buffer adress (if wrap around) */
931 &l_bytes2, /* Count of bytes after wrap around */
933 if( dsresult == DSERR_BUFFERLOST )
935 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
936 dsresult = IDirectSoundBuffer_Lock(
938 i_frame * p_notif->i_frame_size,
939 p_notif->i_frame_size,
946 if( dsresult != DS_OK )
948 msg_Warn( p_notif, "cannot lock buffer" );
949 if( p_buffer ) aout_BufferFree( p_buffer );
953 if( p_buffer == NULL )
955 memset( p_write_position, 0, l_bytes1 );
959 if( p_sys->b_chan_reorder )
961 /* Do the channel reordering here */
962 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
963 p_sys->i_channels, p_sys->pi_chan_table,
964 p_sys->i_bits_per_sample );
967 p_aout->p_libvlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
969 aout_BufferFree( p_buffer );
972 /* Now the data has been copied, unlock the buffer */
973 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
974 p_wrap_around, l_bytes2 );
976 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
980 /*****************************************************************************
981 * DirectSoundThread: this thread will capture play notification events.
982 *****************************************************************************
983 * We use this thread to emulate a callback mechanism. The thread probes for
984 * event notification and fills up the DS secondary buffer when needed.
985 *****************************************************************************/
986 static void DirectSoundThread( notification_thread_t *p_notif )
988 aout_instance_t *p_aout = p_notif->p_aout;
994 /* We don't want any resampling when using S/PDIF output */
995 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
997 /* Tell the main thread that we are ready */
998 vlc_thread_ready( p_notif );
1000 msg_Dbg( p_notif, "DirectSoundThread ready" );
1002 /* Wait here until Play() is called */
1003 WaitForSingleObject( p_notif->event, INFINITE );
1005 if( !p_notif->b_die )
1007 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
1009 /* start playing the buffer */
1010 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
1013 DSBPLAY_LOOPING ); /* Flags */
1014 if( dsresult == DSERR_BUFFERLOST )
1016 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
1017 dsresult = IDirectSoundBuffer_Play(
1018 p_aout->output.p_sys->p_dsbuffer,
1021 DSBPLAY_LOOPING ); /* Flags */
1023 if( dsresult != DS_OK )
1025 msg_Err( p_aout, "cannot start playing buffer" );
1028 last_time = mdate();
1030 while( !p_notif->b_die )
1032 long l_read, l_free_slots;
1033 mtime_t mtime = mdate();
1037 * Fill in as much audio data as we can in our circular buffer
1040 /* Find out current play position */
1041 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1042 p_aout->output.p_sys->p_dsbuffer, &l_read, NULL ) )
1044 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1048 /* Detect underruns */
1049 if( l_queued && mtime - last_time >
1050 I64C(1000000) * l_queued / p_aout->output.output.i_rate )
1052 msg_Dbg( p_aout, "detected underrun!" );
1056 /* Try to fill in as many frame buffers as possible */
1057 l_read /= p_aout->output.output.i_bytes_per_frame;
1058 l_queued = p_notif->i_write_slot * FRAME_SIZE - l_read;
1059 if( l_queued < 0 ) l_queued += (FRAME_SIZE * FRAMES_NUM);
1060 l_free_slots = (FRAMES_NUM * FRAME_SIZE - l_queued) / FRAME_SIZE;
1062 for( i = 0; i < l_free_slots; i++ )
1064 aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout,
1065 mtime + I64C(1000000) * (i * FRAME_SIZE + l_queued) /
1066 p_aout->output.output.i_rate, b_sleek );
1068 /* If there is no audio data available and we have some buffered
1069 * already, then just wait for the next time */
1070 if( !p_buffer && (i || l_queued / FRAME_SIZE) ) break;
1072 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1073 p_buffer ) != VLC_SUCCESS ) break;
1076 /* Sleep a reasonable amount of time */
1077 l_queued += (i * FRAME_SIZE);
1078 msleep( I64C(1000000) * l_queued / p_aout->output.output.i_rate / 2 );
1081 /* make sure the buffer isn't playing */
1082 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
1084 /* free the event */
1085 CloseHandle( p_notif->event );
1087 msg_Dbg( p_notif, "DirectSoundThread exiting" );