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 *****************************************************************************/
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_HEADPHONE
88 # define DSSPEAKER_HEADPHONE 0x00000001
90 #ifndef DSSPEAKER_MONO
91 # define DSSPEAKER_MONO 0x00000002
93 #ifndef DSSPEAKER_QUAD
94 # define DSSPEAKER_QUAD 0x00000003
96 #ifndef DSSPEAKER_STEREO
97 # define DSSPEAKER_STEREO 0x00000004
99 #ifndef DSSPEAKER_SURROUND
100 # define DSSPEAKER_SURROUND 0x00000005
102 #ifndef DSSPEAKER_5POINT1
103 # define DSSPEAKER_5POINT1 0x00000006
106 #ifndef _WAVEFORMATEXTENSIBLE_
110 WORD wValidBitsPerSample; /* bits of precision */
111 WORD wSamplesPerBlock; /* valid if wBitsPerSample==0 */
112 WORD wReserved; /* If neither applies, set to zero. */
114 DWORD dwChannelMask; /* which channels are */
115 /* present in stream */
117 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
120 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
121 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_PCM, WAVE_FORMAT_PCM, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
122 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF, WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
124 /*****************************************************************************
125 * notification_thread_t: DirectX event thread
126 *****************************************************************************/
127 typedef struct notification_thread_t
131 aout_instance_t *p_aout;
132 int i_frame_size; /* size in bytes of one frame */
133 int i_write_slot; /* current write position in our circular buffer */
138 } notification_thread_t;
140 /*****************************************************************************
141 * aout_sys_t: directx audio output method descriptor
142 *****************************************************************************
143 * This structure is part of the audio output thread descriptor.
144 * It describes the direct sound specific properties of an audio device.
145 *****************************************************************************/
148 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
150 int i_device_id; /* user defined device */
151 LPGUID p_device_guid;
153 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
154 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
155 * takes care of mixing all the
156 * secondary buffers into the primary) */
158 notification_thread_t *p_notif; /* DirectSoundThread id */
160 int b_playing; /* playing status */
162 int i_frame_size; /* Size in bytes of one frame */
164 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
165 int pi_chan_table[AOUT_CHAN_MAX];
166 uint32_t i_channel_mask;
167 uint32_t i_bits_per_sample;
171 static const uint32_t pi_channels_src[] =
172 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
173 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
174 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
175 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
176 static const uint32_t pi_channels_in[] =
177 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
178 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
179 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
180 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
181 static const uint32_t pi_channels_out[] =
182 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
183 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
184 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
185 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
187 /*****************************************************************************
189 *****************************************************************************/
190 static int OpenAudio ( vlc_object_t * );
191 static void CloseAudio ( vlc_object_t * );
192 static void Play ( aout_instance_t * );
194 /* local functions */
195 static void Probe ( aout_instance_t * );
196 static int InitDirectSound ( aout_instance_t * );
197 static int CreateDSBuffer ( aout_instance_t *, int, int, int, int, int, vlc_bool_t );
198 static int CreateDSBufferPCM ( aout_instance_t *, int*, int, int, int, vlc_bool_t );
199 static void DestroyDSBuffer ( aout_instance_t * );
200 static void DirectSoundThread ( notification_thread_t * );
201 static int FillBuffer ( aout_instance_t *, int, aout_buffer_t * );
203 /*****************************************************************************
205 *****************************************************************************/
206 #define DEVICE_TEXT N_("Output device")
207 #define DEVICE_LONGTEXT N_( \
208 "DirectX device number: 0 default device, 1..N device by number" \
209 "(Note that the default device appears as 0 AND another number)." )
210 #define FLOAT_TEXT N_("Use float32 output")
211 #define FLOAT_LONGTEXT N_( \
212 "The option allows you to enable or disable the high-quality float32 " \
213 "audio output mode (which is not well supported by some soundcards)." )
216 set_description( _("DirectX audio output") );
217 set_shortname( "DirectX" );
218 set_capability( "audio output", 100 );
219 set_category( CAT_AUDIO );
220 set_subcategory( SUBCAT_AUDIO_AOUT );
221 add_shortcut( "directx" );
222 add_integer( "directx-audio-device", 0, NULL, DEVICE_TEXT,
223 DEVICE_LONGTEXT, VLC_TRUE );
224 add_bool( "directx-audio-float32", 0, 0, FLOAT_TEXT,
225 FLOAT_LONGTEXT, VLC_TRUE );
226 set_callbacks( OpenAudio, CloseAudio );
229 /*****************************************************************************
230 * OpenAudio: open the audio device
231 *****************************************************************************
232 * This function opens and setups Direct Sound.
233 *****************************************************************************/
234 static int OpenAudio( vlc_object_t *p_this )
236 aout_instance_t * p_aout = (aout_instance_t *)p_this;
239 msg_Dbg( p_aout, "OpenAudio" );
241 /* Allocate structure */
242 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
243 if( p_aout->output.p_sys == NULL )
245 msg_Err( p_aout, "out of memory" );
249 /* Initialize some variables */
250 p_aout->output.p_sys->p_dsobject = NULL;
251 p_aout->output.p_sys->p_dsbuffer = NULL;
252 p_aout->output.p_sys->p_notif = NULL;
253 p_aout->output.p_sys->b_playing = 0;
255 p_aout->output.pf_play = Play;
256 aout_VolumeSoftInit( p_aout );
258 /* Retrieve config values */
259 var_Create( p_aout, "directx-audio-float32",
260 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
261 var_Create( p_aout, "directx-audio-device",
262 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
263 var_Get( p_aout, "directx-audio-device", &val );
264 p_aout->output.p_sys->i_device_id = val.i_int;
265 p_aout->output.p_sys->p_device_guid = 0;
267 /* Initialise DirectSound */
268 if( InitDirectSound( p_aout ) )
270 msg_Err( p_aout, "cannot initialize DirectSound" );
274 if( var_Type( p_aout, "audio-device" ) == 0 )
279 if( var_Get( p_aout, "audio-device", &val ) < 0 )
281 /* Probe() has failed. */
285 /* Open the device */
286 if( val.i_int == AOUT_VAR_SPDIF )
288 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
290 /* Calculate the frame size in bytes */
291 p_aout->output.i_nb_samples = A52_FRAME_NB;
292 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
293 p_aout->output.output.i_frame_length = A52_FRAME_NB;
294 p_aout->output.p_sys->i_frame_size =
295 p_aout->output.output.i_bytes_per_frame;
297 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
298 p_aout->output.output.i_physical_channels,
299 aout_FormatNbChannels( &p_aout->output.output ),
300 p_aout->output.output.i_rate,
301 p_aout->output.p_sys->i_frame_size, VLC_FALSE )
304 msg_Err( p_aout, "cannot open directx audio device" );
305 free( p_aout->output.p_sys );
309 aout_VolumeNoneInit( p_aout );
313 if( val.i_int == AOUT_VAR_5_1 )
315 p_aout->output.output.i_physical_channels
316 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
317 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
320 else if( val.i_int == AOUT_VAR_3F2R )
322 p_aout->output.output.i_physical_channels
323 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
324 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
326 else if( val.i_int == AOUT_VAR_2F2R )
328 p_aout->output.output.i_physical_channels
329 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
330 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
332 else if( val.i_int == AOUT_VAR_MONO )
334 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
338 p_aout->output.output.i_physical_channels
339 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
342 if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,
343 p_aout->output.output.i_physical_channels,
344 aout_FormatNbChannels( &p_aout->output.output ),
345 p_aout->output.output.i_rate, VLC_FALSE )
348 msg_Err( p_aout, "cannot open directx audio device" );
349 free( p_aout->output.p_sys );
353 /* Calculate the frame size in bytes */
354 p_aout->output.i_nb_samples = FRAME_SIZE;
355 aout_FormatPrepare( &p_aout->output.output );
356 aout_VolumeSoftInit( p_aout );
359 /* Now we need to setup our DirectSound play notification structure */
360 p_aout->output.p_sys->p_notif =
361 vlc_object_create( p_aout, sizeof(notification_thread_t) );
362 p_aout->output.p_sys->p_notif->p_aout = p_aout;
364 p_aout->output.p_sys->p_notif->event = CreateEvent( 0, FALSE, FALSE, 0 );
365 p_aout->output.p_sys->p_notif->i_frame_size =
366 p_aout->output.p_sys->i_frame_size;
368 /* then launch the notification thread */
369 msg_Dbg( p_aout, "creating DirectSoundThread" );
370 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
371 "DirectSound Notification Thread",
373 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
375 msg_Err( p_aout, "cannot create DirectSoundThread" );
376 CloseHandle( p_aout->output.p_sys->p_notif->event );
377 vlc_object_destroy( p_aout->output.p_sys->p_notif );
378 p_aout->output.p_sys->p_notif = NULL;
382 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
387 CloseAudio( VLC_OBJECT(p_aout) );
391 /*****************************************************************************
392 * Probe: probe the audio device for available formats and channels
393 *****************************************************************************/
394 static void Probe( aout_instance_t * p_aout )
396 vlc_value_t val, text;
398 unsigned int i_physical_channels;
399 DWORD ui_speaker_config;
401 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
402 text.psz_string = _("Audio Device");
403 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
405 /* Test for 5.1 support */
406 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
407 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
408 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
409 if( p_aout->output.output.i_physical_channels == i_physical_channels )
411 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
412 p_aout->output.output.i_rate, VLC_TRUE )
415 val.i_int = AOUT_VAR_5_1;
416 text.psz_string = "5.1";
417 var_Change( p_aout, "audio-device",
418 VLC_VAR_ADDCHOICE, &val, &text );
419 msg_Dbg( p_aout, "device supports 5.1 channels" );
423 /* Test for 3 Front 2 Rear support */
424 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
425 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
427 if( p_aout->output.output.i_physical_channels == i_physical_channels )
429 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
430 p_aout->output.output.i_rate, VLC_TRUE )
433 val.i_int = AOUT_VAR_3F2R;
434 text.psz_string = N_("3 Front 2 Rear");
435 var_Change( p_aout, "audio-device",
436 VLC_VAR_ADDCHOICE, &val, &text );
437 msg_Dbg( p_aout, "device supports 5 channels" );
441 /* Test for 2 Front 2 Rear support */
442 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
443 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
444 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
445 == i_physical_channels )
447 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
448 p_aout->output.output.i_rate, VLC_TRUE )
451 val.i_int = AOUT_VAR_2F2R;
452 text.psz_string = N_("2 Front 2 Rear");
453 var_Change( p_aout, "audio-device",
454 VLC_VAR_ADDCHOICE, &val, &text );
455 msg_Dbg( p_aout, "device supports 4 channels" );
459 /* Test for stereo support */
460 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
461 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
462 p_aout->output.output.i_rate, VLC_TRUE )
465 val.i_int = AOUT_VAR_STEREO;
466 text.psz_string = N_("Stereo");
467 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
468 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
469 msg_Dbg( p_aout, "device supports 2 channels" );
472 /* Test for mono support */
473 i_physical_channels = AOUT_CHAN_CENTER;
474 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
475 p_aout->output.output.i_rate, VLC_TRUE )
478 val.i_int = AOUT_VAR_MONO;
479 text.psz_string = N_("Mono");
480 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
481 msg_Dbg( p_aout, "device supports 1 channel" );
484 /* Check the speaker configuration to determine which channel config should
486 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
487 &ui_speaker_config ) )
489 ui_speaker_config = DSSPEAKER_STEREO;
491 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
493 case DSSPEAKER_5POINT1:
494 val.i_int = AOUT_VAR_5_1;
497 val.i_int = AOUT_VAR_2F2R;
499 #if 0 /* Lots of people just get their settings wrong and complain that
500 * this is a problem with VLC so just don't ever set mono by default. */
502 val.i_int = AOUT_VAR_MONO;
505 case DSSPEAKER_SURROUND:
506 case DSSPEAKER_STEREO:
508 val.i_int = AOUT_VAR_STEREO;
511 var_Set( p_aout, "audio-device", val );
513 /* Test for SPDIF support */
514 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
516 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
517 p_aout->output.output.i_physical_channels,
518 aout_FormatNbChannels( &p_aout->output.output ),
519 p_aout->output.output.i_rate,
520 AOUT_SPDIF_SIZE, VLC_TRUE )
523 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
524 val.i_int = AOUT_VAR_SPDIF;
525 text.psz_string = N_("A/52 over S/PDIF");
526 var_Change( p_aout, "audio-device",
527 VLC_VAR_ADDCHOICE, &val, &text );
528 if( config_GetInt( p_aout, "spdif" ) )
529 var_Set( p_aout, "audio-device", val );
533 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
536 /* Probe() has failed. */
537 var_Destroy( p_aout, "audio-device" );
541 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
543 val.b_bool = VLC_TRUE;
544 var_Set( p_aout, "intf-change", val );
547 /*****************************************************************************
548 * Play: we'll start playing the directsound buffer here because at least here
549 * we know the first buffer has been put in the aout fifo and we also
551 *****************************************************************************/
552 static void Play( aout_instance_t *p_aout )
554 if( !p_aout->output.p_sys->b_playing )
556 aout_buffer_t *p_buffer;
559 p_aout->output.p_sys->b_playing = 1;
561 /* get the playing date of the first aout buffer */
562 p_aout->output.p_sys->p_notif->start_date =
563 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
565 /* fill in the first samples */
566 for( i = 0; i < FRAMES_NUM; i++ )
568 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
569 if( !p_buffer ) break;
570 FillBuffer( p_aout, i, p_buffer );
573 /* wake up the audio output thread */
574 SetEvent( p_aout->output.p_sys->p_notif->event );
578 /*****************************************************************************
579 * CloseAudio: close the audio device
580 *****************************************************************************/
581 static void CloseAudio( vlc_object_t *p_this )
583 aout_instance_t * p_aout = (aout_instance_t *)p_this;
584 aout_sys_t *p_sys = p_aout->output.p_sys;
586 msg_Dbg( p_aout, "closing audio device" );
588 /* kill the position notification thread, if any */
591 vlc_object_detach( p_sys->p_notif );
592 vlc_object_kill( p_sys->p_notif );
593 /* wake up the audio thread if needed */
594 if( !p_sys->b_playing ) SetEvent( p_sys->p_notif->event );
596 vlc_thread_join( p_sys->p_notif );
597 vlc_object_destroy( p_sys->p_notif );
600 /* release the secondary buffer */
601 DestroyDSBuffer( p_aout );
603 /* finally release the DirectSound object */
604 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
606 /* free DSOUND.DLL */
607 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
609 free( p_aout->output.p_sys->p_device_guid );
613 /*****************************************************************************
614 * CallBackDirectSoundEnum: callback to enumerate available devices
615 *****************************************************************************/
616 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCSTR psz_desc,
617 LPCSTR psz_mod, LPVOID _p_aout )
619 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
621 msg_Dbg( p_aout, "found device: %s", psz_desc );
623 if( p_aout->output.p_sys->i_device_id == 0 && p_guid )
625 p_aout->output.p_sys->p_device_guid = malloc( sizeof( GUID ) );
626 *p_aout->output.p_sys->p_device_guid = *p_guid;
627 msg_Dbg( p_aout, "using device: %s", psz_desc );
630 p_aout->output.p_sys->i_device_id--;
634 /*****************************************************************************
635 * InitDirectSound: handle all the gory details of DirectSound initialisation
636 *****************************************************************************/
637 static int InitDirectSound( aout_instance_t *p_aout )
639 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
640 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACK, LPVOID);
642 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
643 if( p_aout->output.p_sys->hdsound_dll == NULL )
645 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
649 OurDirectSoundCreate = (void *)
650 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
651 "DirectSoundCreate" );
652 if( OurDirectSoundCreate == NULL )
654 msg_Warn( p_aout, "GetProcAddress FAILED" );
658 /* Get DirectSoundEnumerate */
659 OurDirectSoundEnumerate = (void *)
660 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
661 "DirectSoundEnumerateA" );
662 if( OurDirectSoundEnumerate )
664 /* Attempt enumeration */
665 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
668 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
672 /* Create the direct sound object */
673 if FAILED( OurDirectSoundCreate( p_aout->output.p_sys->p_device_guid,
674 &p_aout->output.p_sys->p_dsobject,
677 msg_Warn( p_aout, "cannot create a direct sound device" );
681 /* Set DirectSound Cooperative level, ie what control we want over Windows
682 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
683 * settings of the primary buffer, but also that only the sound of our
684 * application will be hearable when it will have the focus.
685 * !!! (this is not really working as intended yet because to set the
686 * cooperative level you need the window handle of your application, and
687 * I don't know of any easy way to get it. Especially since we might play
688 * sound without any video, and so what window handle should we use ???
689 * The hack for now is to use the Desktop window handle - it seems to be
691 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
695 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
701 p_aout->output.p_sys->p_dsobject = NULL;
702 if( p_aout->output.p_sys->hdsound_dll )
704 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
705 p_aout->output.p_sys->hdsound_dll = NULL;
711 /*****************************************************************************
712 * CreateDSBuffer: Creates a direct sound buffer of the required format.
713 *****************************************************************************
714 * This function creates the buffer we'll use to play audio.
715 * In DirectSound there are two kinds of buffers:
716 * - the primary buffer: which is the actual buffer that the soundcard plays
717 * - the secondary buffer(s): these buffers are the one actually used by
718 * applications and DirectSound takes care of mixing them into the primary.
720 * Once you create a secondary buffer, you cannot change its format anymore so
721 * you have to release the current one and create another.
722 *****************************************************************************/
723 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
724 int i_channels, int i_nb_channels, int i_rate,
725 int i_bytes_per_frame, vlc_bool_t b_probe )
727 WAVEFORMATEXTENSIBLE waveformat;
728 DSBUFFERDESC dsbdesc;
731 /* First set the sound buffer format */
732 waveformat.dwChannelMask = 0;
733 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
735 if( i_channels & pi_channels_src[i] )
736 waveformat.dwChannelMask |= pi_channels_in[i];
741 case VLC_FOURCC('s','p','d','i'):
743 /* To prevent channel re-ordering */
744 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
745 waveformat.Format.wBitsPerSample = 16;
746 waveformat.Samples.wValidBitsPerSample =
747 waveformat.Format.wBitsPerSample;
748 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
749 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
752 case VLC_FOURCC('f','l','3','2'):
753 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
754 waveformat.Samples.wValidBitsPerSample =
755 waveformat.Format.wBitsPerSample;
756 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
757 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
760 case VLC_FOURCC('s','1','6','l'):
761 waveformat.Format.wBitsPerSample = 16;
762 waveformat.Samples.wValidBitsPerSample =
763 waveformat.Format.wBitsPerSample;
764 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
765 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
769 waveformat.Format.nChannels = i_nb_channels;
770 waveformat.Format.nSamplesPerSec = i_rate;
771 waveformat.Format.nBlockAlign =
772 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
773 waveformat.Format.nAvgBytesPerSec =
774 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
776 p_aout->output.p_sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
777 p_aout->output.p_sys->i_channels = i_nb_channels;
779 /* Then fill in the direct sound descriptor */
780 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
781 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
782 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
783 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
785 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
786 if( i_nb_channels <= 2 )
788 waveformat.Format.cbSize = 0;
792 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
793 waveformat.Format.cbSize =
794 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
796 /* Needed for 5.1 on emu101k */
797 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
800 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
801 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
803 if FAILED( IDirectSound_CreateSoundBuffer(
804 p_aout->output.p_sys->p_dsobject, &dsbdesc,
805 &p_aout->output.p_sys->p_dsbuffer, NULL) )
807 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
809 /* Try without DSBCAPS_LOCHARDWARE */
810 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
811 if FAILED( IDirectSound_CreateSoundBuffer(
812 p_aout->output.p_sys->p_dsobject, &dsbdesc,
813 &p_aout->output.p_sys->p_dsbuffer, NULL) )
818 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
826 /* Stop here if we were just probing */
829 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
830 p_aout->output.p_sys->p_dsbuffer = NULL;
834 p_aout->output.p_sys->i_frame_size = i_bytes_per_frame;
835 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
836 p_aout->output.p_sys->b_chan_reorder =
837 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
838 waveformat.dwChannelMask, i_nb_channels,
839 p_aout->output.p_sys->pi_chan_table );
841 if( p_aout->output.p_sys->b_chan_reorder )
843 msg_Dbg( p_aout, "channel reordering needed" );
849 /*****************************************************************************
850 * CreateDSBufferPCM: creates a PCM direct sound buffer.
851 *****************************************************************************
852 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
853 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
854 ****************************************************************************/
855 static int CreateDSBufferPCM( aout_instance_t *p_aout, int *i_format,
856 int i_channels, int i_nb_channels, int i_rate,
861 var_Get( p_aout, "directx-audio-float32", &val );
863 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
865 if( !val.b_bool || i_nb_channels > 2 ||
866 CreateDSBuffer( p_aout, VLC_FOURCC('f','l','3','2'),
867 i_channels, i_nb_channels, i_rate,
868 FRAME_SIZE * 4 * i_nb_channels, b_probe )
871 if ( CreateDSBuffer( p_aout, VLC_FOURCC('s','1','6','l'),
872 i_channels, i_nb_channels, i_rate,
873 FRAME_SIZE * 2 * i_nb_channels, b_probe )
880 *i_format = VLC_FOURCC('s','1','6','l');
886 *i_format = VLC_FOURCC('f','l','3','2');
891 /*****************************************************************************
893 *****************************************************************************
894 * This function destroys the secondary buffer.
895 *****************************************************************************/
896 static void DestroyDSBuffer( aout_instance_t *p_aout )
898 if( p_aout->output.p_sys->p_dsbuffer )
900 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
901 p_aout->output.p_sys->p_dsbuffer = NULL;
905 /*****************************************************************************
906 * FillBuffer: Fill in one of the direct sound frame buffers.
907 *****************************************************************************
908 * Returns VLC_SUCCESS on success.
909 *****************************************************************************/
910 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
911 aout_buffer_t *p_buffer )
913 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
914 aout_sys_t *p_sys = p_aout->output.p_sys;
915 void *p_write_position, *p_wrap_around;
916 long l_bytes1, l_bytes2;
919 /* Before copying anything, we have to lock the buffer */
920 dsresult = IDirectSoundBuffer_Lock(
921 p_sys->p_dsbuffer, /* DS buffer */
922 i_frame * p_notif->i_frame_size, /* Start offset */
923 p_notif->i_frame_size, /* Number of bytes */
924 &p_write_position, /* Address of lock start */
925 &l_bytes1, /* Count of bytes locked before wrap around */
926 &p_wrap_around, /* Buffer adress (if wrap around) */
927 &l_bytes2, /* Count of bytes after wrap around */
929 if( dsresult == DSERR_BUFFERLOST )
931 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
932 dsresult = IDirectSoundBuffer_Lock(
934 i_frame * p_notif->i_frame_size,
935 p_notif->i_frame_size,
942 if( dsresult != DS_OK )
944 msg_Warn( p_notif, "cannot lock buffer" );
945 if( p_buffer ) aout_BufferFree( p_buffer );
949 if( p_buffer == NULL )
951 memset( p_write_position, 0, l_bytes1 );
955 if( p_sys->b_chan_reorder )
957 /* Do the channel reordering here */
958 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
959 p_sys->i_channels, p_sys->pi_chan_table,
960 p_sys->i_bits_per_sample );
963 p_aout->p_libvlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
965 aout_BufferFree( p_buffer );
968 /* Now the data has been copied, unlock the buffer */
969 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
970 p_wrap_around, l_bytes2 );
972 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
976 /*****************************************************************************
977 * DirectSoundThread: this thread will capture play notification events.
978 *****************************************************************************
979 * We use this thread to emulate a callback mechanism. The thread probes for
980 * event notification and fills up the DS secondary buffer when needed.
981 *****************************************************************************/
982 static void DirectSoundThread( notification_thread_t *p_notif )
984 aout_instance_t *p_aout = p_notif->p_aout;
990 /* We don't want any resampling when using S/PDIF output */
991 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
993 /* Tell the main thread that we are ready */
994 vlc_thread_ready( p_notif );
996 msg_Dbg( p_notif, "DirectSoundThread ready" );
998 /* Wait here until Play() is called */
999 WaitForSingleObject( p_notif->event, INFINITE );
1001 if( !p_notif->b_die )
1003 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
1005 /* start playing the buffer */
1006 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
1009 DSBPLAY_LOOPING ); /* Flags */
1010 if( dsresult == DSERR_BUFFERLOST )
1012 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
1013 dsresult = IDirectSoundBuffer_Play(
1014 p_aout->output.p_sys->p_dsbuffer,
1017 DSBPLAY_LOOPING ); /* Flags */
1019 if( dsresult != DS_OK )
1021 msg_Err( p_aout, "cannot start playing buffer" );
1024 last_time = mdate();
1026 while( !p_notif->b_die )
1028 long l_read, l_free_slots;
1029 mtime_t mtime = mdate();
1033 * Fill in as much audio data as we can in our circular buffer
1036 /* Find out current play position */
1037 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1038 p_aout->output.p_sys->p_dsbuffer, &l_read, NULL ) )
1040 msg_Err( p_aout, "GetCurrentPosition() failed!" );
1044 /* Detect underruns */
1045 if( l_queued && mtime - last_time >
1046 I64C(1000000) * l_queued / p_aout->output.output.i_rate )
1048 msg_Dbg( p_aout, "detected underrun!" );
1052 /* Try to fill in as many frame buffers as possible */
1053 l_read /= p_aout->output.output.i_bytes_per_frame;
1054 l_queued = p_notif->i_write_slot * FRAME_SIZE - l_read;
1055 if( l_queued < 0 ) l_queued += (FRAME_SIZE * FRAMES_NUM);
1056 l_free_slots = (FRAMES_NUM * FRAME_SIZE - l_queued) / FRAME_SIZE;
1058 for( i = 0; i < l_free_slots; i++ )
1060 aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout,
1061 mtime + I64C(1000000) * (i * FRAME_SIZE + l_queued) /
1062 p_aout->output.output.i_rate, b_sleek );
1064 /* If there is no audio data available and we have some buffered
1065 * already, then just wait for the next time */
1066 if( !p_buffer && (i || l_queued / FRAME_SIZE) ) break;
1068 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1069 p_buffer ) != VLC_SUCCESS ) break;
1072 /* Sleep a reasonable amount of time */
1073 l_queued += (i * FRAME_SIZE);
1074 msleep( I64C(1000000) * l_queued / p_aout->output.output.i_rate / 2 );
1077 /* make sure the buffer isn't playing */
1078 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
1080 /* free the event */
1081 CloseHandle( p_notif->event );
1083 msg_Dbg( p_notif, "DirectSoundThread exiting" );