1 /*****************************************************************************
2 * directx.c: Windows DirectX audio output method
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN (Centrale Réseaux) and its contributors
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <fcntl.h> /* open(), O_WRONLY */
29 #include <string.h> /* strerror() */
31 #include <stdlib.h> /* calloc(), malloc(), free() */
35 #include "aout_internal.h"
41 #define FRAME_SIZE 2048 /* The size is in samples, not in bytes */
44 /* frame buffer status */
45 #define FRAME_QUEUED 0
48 /*****************************************************************************
50 * Defining them here allows us to get rid of the dxguid library during
52 *****************************************************************************/
54 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf, 0x8, 0x0, 0xa0, 0xc9, 0x25, 0xcd, 0x16);
56 /*****************************************************************************
58 *****************************************************************************/
59 #ifndef WAVE_FORMAT_IEEE_FLOAT
60 # define WAVE_FORMAT_IEEE_FLOAT 0x0003
63 #ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
64 # define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
67 #ifndef WAVE_FORMAT_EXTENSIBLE
68 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
71 #ifndef SPEAKER_FRONT_LEFT
72 # define SPEAKER_FRONT_LEFT 0x1
73 # define SPEAKER_FRONT_RIGHT 0x2
74 # define SPEAKER_FRONT_CENTER 0x4
75 # define SPEAKER_LOW_FREQUENCY 0x8
76 # define SPEAKER_BACK_LEFT 0x10
77 # define SPEAKER_BACK_RIGHT 0x20
78 # define SPEAKER_FRONT_LEFT_OF_CENTER 0x40
79 # define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80
80 # define SPEAKER_BACK_CENTER 0x100
81 # define SPEAKER_SIDE_LEFT 0x200
82 # define SPEAKER_SIDE_RIGHT 0x400
83 # define SPEAKER_TOP_CENTER 0x800
84 # define SPEAKER_TOP_FRONT_LEFT 0x1000
85 # define SPEAKER_TOP_FRONT_CENTER 0x2000
86 # define SPEAKER_TOP_FRONT_RIGHT 0x4000
87 # define SPEAKER_TOP_BACK_LEFT 0x8000
88 # define SPEAKER_TOP_BACK_CENTER 0x10000
89 # define SPEAKER_TOP_BACK_RIGHT 0x20000
90 # define SPEAKER_RESERVED 0x80000000
93 #ifndef DSSPEAKER_HEADPHONE
94 # define DSSPEAKER_HEADPHONE 0x00000001
96 #ifndef DSSPEAKER_MONO
97 # define DSSPEAKER_MONO 0x00000002
99 #ifndef DSSPEAKER_QUAD
100 # define DSSPEAKER_QUAD 0x00000003
102 #ifndef DSSPEAKER_STEREO
103 # define DSSPEAKER_STEREO 0x00000004
105 #ifndef DSSPEAKER_SURROUND
106 # define DSSPEAKER_SURROUND 0x00000005
108 #ifndef DSSPEAKER_5POINT1
109 # define DSSPEAKER_5POINT1 0x00000006
112 #ifndef _WAVEFORMATEXTENSIBLE_
116 WORD wValidBitsPerSample; /* bits of precision */
117 WORD wSamplesPerBlock; /* valid if wBitsPerSample==0 */
118 WORD wReserved; /* If neither applies, set to zero. */
120 DWORD dwChannelMask; /* which channels are */
121 /* present in stream */
123 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
126 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
127 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_PCM, WAVE_FORMAT_PCM, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
128 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF, WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
130 /*****************************************************************************
131 * notification_thread_t: DirectX event thread
132 *****************************************************************************/
133 typedef struct notification_thread_t
137 aout_instance_t * p_aout;
138 int i_frame_status[FRAMES_NUM]; /* status of each frame buffer */
139 DSBPOSITIONNOTIFY p_events[FRAMES_NUM]; /* play notification events */
140 int i_frame_size; /* Size in bytes of one frame */
144 } notification_thread_t;
146 /*****************************************************************************
147 * aout_sys_t: directx audio output method descriptor
148 *****************************************************************************
149 * This structure is part of the audio output thread descriptor.
150 * It describes the direct sound specific properties of an audio device.
151 *****************************************************************************/
154 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
156 int i_device_id; /* user defined device */
157 LPGUID p_device_guid;
159 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
160 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
161 * takes care of mixing all the
162 * secondary buffers into the primary) */
164 LPDIRECTSOUNDNOTIFY p_dsnotify; /* the position notify interface */
165 notification_thread_t *p_notif; /* DirectSoundThread id */
167 int b_playing; /* playing status */
169 int i_frame_size; /* Size in bytes of one frame */
171 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
172 int pi_chan_table[AOUT_CHAN_MAX];
173 uint32_t i_channel_mask;
174 uint32_t i_bits_per_sample;
178 static const uint32_t pi_channels_src[] =
179 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
180 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
181 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
182 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
183 static const uint32_t pi_channels_in[] =
184 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
185 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
186 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
187 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
188 static const uint32_t pi_channels_out[] =
189 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
190 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
191 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
192 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
194 /*****************************************************************************
196 *****************************************************************************/
197 static int OpenAudio ( vlc_object_t * );
198 static void CloseAudio ( vlc_object_t * );
199 static void Play ( aout_instance_t * );
201 /* local functions */
202 static void Probe ( aout_instance_t * );
203 static int InitDirectSound ( aout_instance_t * );
204 static int CreateDSBuffer ( aout_instance_t *, int, int, int, int, int, vlc_bool_t );
205 static int CreateDSBufferPCM ( aout_instance_t *, int*, int, int, int, vlc_bool_t );
206 static void DestroyDSBuffer ( aout_instance_t * );
207 static void DirectSoundThread ( notification_thread_t * );
208 static int FillBuffer ( aout_instance_t *, int, aout_buffer_t * );
210 /*****************************************************************************
212 *****************************************************************************/
213 #define DEVICE_TEXT N_("Output device")
214 #define DEVICE_LONGTEXT N_( \
215 "DirectX device number: 0 default device, 1..N device by number" \
216 "(Note that the default device appears as 0 AND another number)." )
217 #define FLOAT_TEXT N_("Use float32 output")
218 #define FLOAT_LONGTEXT N_( \
219 "The option allows you to enable or disable the high-quality float32 " \
220 "audio output mode (which is not well supported by some soundcards)." )
223 set_description( _("DirectX audio output") );
224 set_shortname( "DirectX" );
225 set_capability( "audio output", 100 );
226 set_category( CAT_AUDIO );
227 set_subcategory( SUBCAT_AUDIO_AOUT );
228 add_shortcut( "directx" );
229 add_integer( "directx-audio-device", 0, NULL, DEVICE_TEXT,
230 DEVICE_LONGTEXT, VLC_TRUE );
231 add_bool( "directx-audio-float32", 1, 0, FLOAT_TEXT,
232 FLOAT_LONGTEXT, VLC_TRUE );
233 set_callbacks( OpenAudio, CloseAudio );
236 /*****************************************************************************
237 * OpenAudio: open the audio device
238 *****************************************************************************
239 * This function opens and setups Direct Sound.
240 *****************************************************************************/
241 static int OpenAudio( vlc_object_t *p_this )
243 aout_instance_t * p_aout = (aout_instance_t *)p_this;
247 msg_Dbg( p_aout, "OpenAudio" );
249 /* Allocate structure */
250 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
251 if( p_aout->output.p_sys == NULL )
253 msg_Err( p_aout, "out of memory" );
257 /* Initialize some variables */
258 p_aout->output.p_sys->p_dsobject = NULL;
259 p_aout->output.p_sys->p_dsbuffer = NULL;
260 p_aout->output.p_sys->p_dsnotify = NULL;
261 p_aout->output.p_sys->p_notif = NULL;
262 p_aout->output.p_sys->b_playing = 0;
264 p_aout->output.pf_play = Play;
265 aout_VolumeSoftInit( p_aout );
267 /* Retrieve config values */
268 var_Create( p_aout, "directx-audio-float32",
269 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
270 var_Create( p_aout, "directx-audio-device",
271 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
272 var_Get( p_aout, "directx-audio-device", &val );
273 p_aout->output.p_sys->i_device_id = val.i_int;
274 p_aout->output.p_sys->p_device_guid = 0;
276 /* Initialise DirectSound */
277 if( InitDirectSound( p_aout ) )
279 msg_Err( p_aout, "cannot initialize DirectSound" );
283 if( var_Type( p_aout, "audio-device" ) == 0 )
288 if( var_Get( p_aout, "audio-device", &val ) < 0 )
290 /* Probe() has failed. */
294 /* Now we need to setup our DirectSound play notification structure */
295 p_aout->output.p_sys->p_notif =
296 vlc_object_create( p_aout, sizeof(notification_thread_t) );
297 p_aout->output.p_sys->p_notif->p_aout = p_aout;
299 /* Then create the notification events */
300 for( i = 0; i < FRAMES_NUM; i++ )
301 p_aout->output.p_sys->p_notif->p_events[i].hEventNotify =
302 CreateEvent( NULL, FALSE, FALSE, NULL );
304 /* Open the device */
305 if( val.i_int == AOUT_VAR_SPDIF )
307 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
309 /* Calculate the frame size in bytes */
310 p_aout->output.i_nb_samples = A52_FRAME_NB;
311 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
312 p_aout->output.output.i_frame_length = A52_FRAME_NB;
313 p_aout->output.p_sys->i_frame_size =
314 p_aout->output.output.i_bytes_per_frame;
316 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
317 p_aout->output.output.i_physical_channels,
318 aout_FormatNbChannels( &p_aout->output.output ),
319 p_aout->output.output.i_rate,
320 p_aout->output.p_sys->i_frame_size, VLC_FALSE )
323 msg_Err( p_aout, "cannot open directx audio device" );
324 free( p_aout->output.p_sys );
328 aout_VolumeNoneInit( p_aout );
332 if( val.i_int == AOUT_VAR_5_1 )
334 p_aout->output.output.i_physical_channels
335 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
336 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
339 else if( val.i_int == AOUT_VAR_3F2R )
341 p_aout->output.output.i_physical_channels
342 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
343 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
345 else if( val.i_int == AOUT_VAR_2F2R )
347 p_aout->output.output.i_physical_channels
348 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
349 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
351 else if( val.i_int == AOUT_VAR_MONO )
353 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
357 p_aout->output.output.i_physical_channels
358 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
361 if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,
362 p_aout->output.output.i_physical_channels,
363 aout_FormatNbChannels( &p_aout->output.output ),
364 p_aout->output.output.i_rate, VLC_FALSE )
367 msg_Err( p_aout, "cannot open directx audio device" );
368 free( p_aout->output.p_sys );
372 /* Calculate the frame size in bytes */
373 p_aout->output.i_nb_samples = FRAME_SIZE;
374 aout_FormatPrepare( &p_aout->output.output );
375 p_aout->output.p_sys->i_frame_size =
376 FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
378 aout_VolumeSoftInit( p_aout );
381 /* then launch the notification thread */
382 msg_Dbg( p_aout, "creating DirectSoundThread" );
383 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
384 "DirectSound Notification Thread",
386 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
388 msg_Err( p_aout, "cannot create DirectSoundThread" );
392 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
397 CloseAudio( VLC_OBJECT(p_aout) );
401 /*****************************************************************************
402 * Probe: probe the audio device for available formats and channels
403 *****************************************************************************/
404 static void Probe( aout_instance_t * p_aout )
406 vlc_value_t val, text;
408 unsigned int i_physical_channels;
409 DWORD ui_speaker_config;
411 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
412 text.psz_string = _("Audio Device");
413 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
415 /* Test for 5.1 support */
416 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
417 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
418 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
419 if( p_aout->output.output.i_physical_channels == i_physical_channels )
421 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
422 p_aout->output.output.i_rate, VLC_TRUE )
425 val.i_int = AOUT_VAR_5_1;
426 text.psz_string = N_("5.1");
427 var_Change( p_aout, "audio-device",
428 VLC_VAR_ADDCHOICE, &val, &text );
429 msg_Dbg( p_aout, "device supports 5.1 channels" );
433 /* Test for 3 Front 2 Rear support */
434 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
435 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
437 if( p_aout->output.output.i_physical_channels == i_physical_channels )
439 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
440 p_aout->output.output.i_rate, VLC_TRUE )
443 val.i_int = AOUT_VAR_3F2R;
444 text.psz_string = N_("3 Front 2 Rear");
445 var_Change( p_aout, "audio-device",
446 VLC_VAR_ADDCHOICE, &val, &text );
447 msg_Dbg( p_aout, "device supports 5 channels" );
451 /* Test for 2 Front 2 Rear support */
452 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
453 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
454 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
455 == i_physical_channels )
457 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
458 p_aout->output.output.i_rate, VLC_TRUE )
461 val.i_int = AOUT_VAR_2F2R;
462 text.psz_string = N_("2 Front 2 Rear");
463 var_Change( p_aout, "audio-device",
464 VLC_VAR_ADDCHOICE, &val, &text );
465 msg_Dbg( p_aout, "device supports 4 channels" );
469 /* Test for stereo support */
470 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
471 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
472 p_aout->output.output.i_rate, VLC_TRUE )
475 val.i_int = AOUT_VAR_STEREO;
476 text.psz_string = N_("Stereo");
477 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
478 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
479 msg_Dbg( p_aout, "device supports 2 channels" );
482 /* Test for mono support */
483 i_physical_channels = AOUT_CHAN_CENTER;
484 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
485 p_aout->output.output.i_rate, VLC_TRUE )
488 val.i_int = AOUT_VAR_MONO;
489 text.psz_string = N_("Mono");
490 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
491 msg_Dbg( p_aout, "device supports 1 channel" );
494 /* Check the speaker configuration to determine which channel config should
496 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
497 &ui_speaker_config ) )
499 ui_speaker_config = DSSPEAKER_STEREO;
501 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
503 case DSSPEAKER_5POINT1:
504 val.i_int = AOUT_VAR_5_1;
507 val.i_int = AOUT_VAR_2F2R;
509 #if 0 /* Lots of people just get their settings wrong and complain that
510 * this is a problem with VLC so just don't ever set mono by default. */
512 val.i_int = AOUT_VAR_MONO;
515 case DSSPEAKER_SURROUND:
516 case DSSPEAKER_STEREO:
518 val.i_int = AOUT_VAR_STEREO;
521 var_Set( p_aout, "audio-device", val );
523 /* Test for SPDIF support */
524 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
526 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
527 p_aout->output.output.i_physical_channels,
528 aout_FormatNbChannels( &p_aout->output.output ),
529 p_aout->output.output.i_rate,
530 AOUT_SPDIF_SIZE, VLC_TRUE )
533 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
534 val.i_int = AOUT_VAR_SPDIF;
535 text.psz_string = N_("A/52 over S/PDIF");
536 var_Change( p_aout, "audio-device",
537 VLC_VAR_ADDCHOICE, &val, &text );
538 if( config_GetInt( p_aout, "spdif" ) )
539 var_Set( p_aout, "audio-device", val );
543 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
546 /* Probe() has failed. */
547 var_Destroy( p_aout, "audio-device" );
551 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
553 val.b_bool = VLC_TRUE;
554 var_Set( p_aout, "intf-change", val );
557 /*****************************************************************************
558 * Play: we'll start playing the directsound buffer here because at least here
559 * we know the first buffer has been put in the aout fifo and we also
561 *****************************************************************************/
562 static void Play( aout_instance_t *p_aout )
564 if( !p_aout->output.p_sys->b_playing )
566 aout_buffer_t *p_buffer;
568 p_aout->output.p_sys->b_playing = 1;
570 /* get the playing date of the first aout buffer */
571 p_aout->output.p_sys->p_notif->start_date =
572 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
574 /* fill in the first samples */
575 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
576 FillBuffer( p_aout, 0, p_buffer );
578 /* wake up the audio output thread */
579 SetEvent( p_aout->output.p_sys->p_notif->p_events[0].hEventNotify );
583 /*****************************************************************************
584 * CloseAudio: close the audio device
585 *****************************************************************************/
586 static void CloseAudio( vlc_object_t *p_this )
588 aout_instance_t * p_aout = (aout_instance_t *)p_this;
589 aout_sys_t *p_sys = p_aout->output.p_sys;
591 msg_Dbg( p_aout, "CloseAudio" );
593 /* kill the position notification thread, if any */
596 vlc_object_detach( p_sys->p_notif );
597 if( p_sys->p_notif->b_thread )
599 p_sys->p_notif->b_die = 1;
601 if( !p_sys->b_playing )
602 /* wake up the audio thread */
603 SetEvent( p_sys->p_notif->p_events[0].hEventNotify );
605 vlc_thread_join( p_sys->p_notif );
607 vlc_object_destroy( p_sys->p_notif );
610 /* release the secondary buffer */
611 DestroyDSBuffer( p_aout );
613 /* finally release the DirectSound object */
614 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
616 /* free DSOUND.DLL */
617 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
619 if( p_aout->output.p_sys->p_device_guid )
620 free( p_aout->output.p_sys->p_device_guid );
625 /*****************************************************************************
626 * CallBackDirectSoundEnum: callback to enumerate available devices
627 *****************************************************************************/
628 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCSTR psz_desc,
629 LPCSTR psz_mod, LPVOID _p_aout )
631 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
633 msg_Dbg( p_aout, "found device: %s", psz_desc );
635 if( p_aout->output.p_sys->i_device_id == 0 && p_guid )
637 p_aout->output.p_sys->p_device_guid = malloc( sizeof( GUID ) );
638 *p_aout->output.p_sys->p_device_guid = *p_guid;
639 msg_Dbg( p_aout, "using device: %s", psz_desc );
642 p_aout->output.p_sys->i_device_id--;
646 /*****************************************************************************
647 * InitDirectSound: handle all the gory details of DirectSound initialisation
648 *****************************************************************************/
649 static int InitDirectSound( aout_instance_t *p_aout )
651 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
652 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACK, LPVOID);
654 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
655 if( p_aout->output.p_sys->hdsound_dll == NULL )
657 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
661 OurDirectSoundCreate = (void *)
662 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
663 "DirectSoundCreate" );
664 if( OurDirectSoundCreate == NULL )
666 msg_Warn( p_aout, "GetProcAddress FAILED" );
670 /* Get DirectSoundEnumerate */
671 OurDirectSoundEnumerate = (void *)
672 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
673 "DirectSoundEnumerateA" );
674 if( OurDirectSoundEnumerate )
676 /* Attempt enumeration */
677 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
680 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
684 /* Create the direct sound object */
685 if FAILED( OurDirectSoundCreate( p_aout->output.p_sys->p_device_guid,
686 &p_aout->output.p_sys->p_dsobject,
689 msg_Warn( p_aout, "cannot create a direct sound device" );
693 /* Set DirectSound Cooperative level, ie what control we want over Windows
694 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
695 * settings of the primary buffer, but also that only the sound of our
696 * application will be hearable when it will have the focus.
697 * !!! (this is not really working as intended yet because to set the
698 * cooperative level you need the window handle of your application, and
699 * I don't know of any easy way to get it. Especially since we might play
700 * sound without any video, and so what window handle should we use ???
701 * The hack for now is to use the Desktop window handle - it seems to be
703 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
707 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
713 p_aout->output.p_sys->p_dsobject = NULL;
714 if( p_aout->output.p_sys->hdsound_dll )
716 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
717 p_aout->output.p_sys->hdsound_dll = NULL;
723 /*****************************************************************************
724 * CreateDSBuffer: Creates a direct sound buffer of the required format.
725 *****************************************************************************
726 * This function creates the buffer we'll use to play audio.
727 * In DirectSound there are two kinds of buffers:
728 * - the primary buffer: which is the actual buffer that the soundcard plays
729 * - the secondary buffer(s): these buffers are the one actually used by
730 * applications and DirectSound takes care of mixing them into the primary.
732 * Once you create a secondary buffer, you cannot change its format anymore so
733 * you have to release the current one and create another.
734 *****************************************************************************/
735 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
736 int i_channels, int i_nb_channels, int i_rate,
737 int i_bytes_per_frame, vlc_bool_t b_probe )
739 WAVEFORMATEXTENSIBLE waveformat;
740 DSBUFFERDESC dsbdesc;
743 /* First set the sound buffer format */
744 waveformat.dwChannelMask = 0;
745 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
747 if( i_channels & pi_channels_src[i] )
748 waveformat.dwChannelMask |= pi_channels_in[i];
753 case VLC_FOURCC('s','p','d','i'):
755 /* To prevent channel re-ordering */
756 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
757 waveformat.Format.wBitsPerSample = 16;
758 waveformat.Samples.wValidBitsPerSample =
759 waveformat.Format.wBitsPerSample;
760 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
761 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
764 case VLC_FOURCC('f','l','3','2'):
765 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
766 waveformat.Samples.wValidBitsPerSample =
767 waveformat.Format.wBitsPerSample;
768 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
769 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
772 case VLC_FOURCC('s','1','6','l'):
773 waveformat.Format.wBitsPerSample = 16;
774 waveformat.Samples.wValidBitsPerSample =
775 waveformat.Format.wBitsPerSample;
776 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
777 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
781 waveformat.Format.nChannels = i_nb_channels;
782 waveformat.Format.nSamplesPerSec = i_rate;
783 waveformat.Format.nBlockAlign =
784 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
785 waveformat.Format.nAvgBytesPerSec =
786 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
788 p_aout->output.p_sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
789 p_aout->output.p_sys->i_channels = i_nb_channels;
791 /* Then fill in the direct sound descriptor */
792 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
793 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
794 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
795 | DSBCAPS_CTRLPOSITIONNOTIFY /* We need notification */
796 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
798 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
799 if( i_nb_channels <= 2 )
801 waveformat.Format.cbSize = 0;
805 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
806 waveformat.Format.cbSize =
807 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
809 /* Needed for 5.1 on emu101k */
810 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
813 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
814 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
816 if FAILED( IDirectSound_CreateSoundBuffer(
817 p_aout->output.p_sys->p_dsobject, &dsbdesc,
818 &p_aout->output.p_sys->p_dsbuffer, NULL) )
820 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
822 /* Try without DSBCAPS_LOCHARDWARE */
823 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
824 if FAILED( IDirectSound_CreateSoundBuffer(
825 p_aout->output.p_sys->p_dsobject, &dsbdesc,
826 &p_aout->output.p_sys->p_dsbuffer, NULL) )
831 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
839 /* Stop here if we were just probing */
842 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
843 p_aout->output.p_sys->p_dsbuffer = NULL;
847 /* Backup the size of a frame */
848 p_aout->output.p_sys->p_notif->i_frame_size = i_bytes_per_frame;
850 /* Now the secondary buffer is created, we need to setup its position
852 for( i = 0; i < FRAMES_NUM; i++ )
854 p_aout->output.p_sys->p_notif->p_events[i].dwOffset = i *
855 p_aout->output.p_sys->p_notif->i_frame_size;
857 p_aout->output.p_sys->p_notif->i_frame_status[i] = FRAME_EMPTY;
860 /* Get the IDirectSoundNotify interface */
861 if FAILED( IDirectSoundBuffer_QueryInterface(
862 p_aout->output.p_sys->p_dsbuffer,
863 &IID_IDirectSoundNotify,
864 (LPVOID *)&p_aout->output.p_sys->p_dsnotify ) )
866 msg_Err( p_aout, "cannot get IDirectSoundNotify interface" );
870 if FAILED( IDirectSoundNotify_SetNotificationPositions(
871 p_aout->output.p_sys->p_dsnotify,
873 p_aout->output.p_sys->p_notif->p_events ) )
875 msg_Err( p_aout, "cannot set position notification" );
879 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
881 p_aout->output.p_sys->b_chan_reorder =
882 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
883 waveformat.dwChannelMask, i_nb_channels,
884 p_aout->output.p_sys->pi_chan_table );
886 if( p_aout->output.p_sys->b_chan_reorder )
888 msg_Dbg( p_aout, "channel reordering needed" );
894 DestroyDSBuffer( p_aout );
898 /*****************************************************************************
899 * CreateDSBufferPCM: creates a PCM direct sound buffer.
900 *****************************************************************************
901 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
902 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
903 ****************************************************************************/
904 static int CreateDSBufferPCM( aout_instance_t *p_aout, int *i_format,
905 int i_channels, int i_nb_channels, int i_rate,
910 var_Get( p_aout, "directx-audio-float32", &val );
912 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
914 if( !val.b_bool || i_nb_channels > 2 ||
915 CreateDSBuffer( p_aout, VLC_FOURCC('f','l','3','2'),
916 i_channels, i_nb_channels, i_rate,
917 FRAME_SIZE * 4 * i_nb_channels, b_probe )
920 if ( CreateDSBuffer( p_aout, VLC_FOURCC('s','1','6','l'),
921 i_channels, i_nb_channels, i_rate,
922 FRAME_SIZE * 2 * i_nb_channels, b_probe )
929 *i_format = VLC_FOURCC('s','1','6','l');
935 *i_format = VLC_FOURCC('f','l','3','2');
940 /*****************************************************************************
942 *****************************************************************************
943 * This function destroys the secondary buffer.
944 *****************************************************************************/
945 static void DestroyDSBuffer( aout_instance_t *p_aout )
947 if( p_aout->output.p_sys->p_dsnotify )
949 IDirectSoundNotify_Release( p_aout->output.p_sys->p_dsnotify );
950 p_aout->output.p_sys->p_dsnotify = NULL;
953 if( p_aout->output.p_sys->p_dsbuffer )
955 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
956 p_aout->output.p_sys->p_dsbuffer = NULL;
960 /*****************************************************************************
961 * FillBuffer: Fill in one of the direct sound frame buffers.
962 *****************************************************************************
963 * Returns VLC_SUCCESS on success.
964 *****************************************************************************/
965 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
966 aout_buffer_t *p_buffer )
968 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
969 aout_sys_t *p_sys = p_aout->output.p_sys;
970 void *p_write_position, *p_wrap_around;
971 long l_bytes1, l_bytes2;
974 /* Before copying anything, we have to lock the buffer */
975 dsresult = IDirectSoundBuffer_Lock(
976 p_sys->p_dsbuffer, /* DS buffer */
977 i_frame * p_notif->i_frame_size, /* Start offset */
978 p_notif->i_frame_size, /* Number of bytes */
979 &p_write_position, /* Address of lock start */
980 &l_bytes1, /* Count of bytes locked before wrap around */
981 &p_wrap_around, /* Buffer adress (if wrap around) */
982 &l_bytes2, /* Count of bytes after wrap around */
984 if( dsresult == DSERR_BUFFERLOST )
986 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
987 dsresult = IDirectSoundBuffer_Lock(
989 i_frame * p_notif->i_frame_size,
990 p_notif->i_frame_size,
997 if( dsresult != DS_OK )
999 msg_Warn( p_notif, "cannot lock buffer" );
1000 if( p_buffer ) aout_BufferFree( p_buffer );
1001 return VLC_EGENERIC;
1004 if( p_buffer == NULL )
1006 memset( p_write_position, 0, l_bytes1 );
1010 if( p_sys->b_chan_reorder )
1012 /* Do the channel reordering here */
1013 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
1014 p_sys->i_channels, p_sys->pi_chan_table,
1015 p_sys->i_bits_per_sample );
1018 p_aout->p_vlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
1020 aout_BufferFree( p_buffer );
1023 /* Now the data has been copied, unlock the buffer */
1024 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1025 p_wrap_around, l_bytes2 );
1030 /*****************************************************************************
1031 * DirectSoundThread: this thread will capture play notification events.
1032 *****************************************************************************
1033 * We use this thread to emulate a callback mechanism. The thread probes for
1034 * event notification and fills up the DS secondary buffer when needed.
1035 *****************************************************************************/
1036 static void DirectSoundThread( notification_thread_t *p_notif )
1038 HANDLE notification_events[FRAMES_NUM];
1040 aout_instance_t *p_aout = p_notif->p_aout;
1041 int i, i_which_frame, i_last_frame, i_next_frame;
1045 for( i = 0; i < FRAMES_NUM; i++ )
1046 notification_events[i] = p_notif->p_events[i].hEventNotify;
1048 /* We don't want any resampling when using S/PDIF output */
1049 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
1051 /* Tell the main thread that we are ready */
1052 vlc_thread_ready( p_notif );
1054 msg_Dbg( p_notif, "DirectSoundThread ready" );
1056 /* Wait here until Play() is called */
1057 WaitForSingleObject( notification_events[0], INFINITE );
1059 if( !p_notif->b_die )
1061 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
1063 /* start playing the buffer */
1064 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
1067 DSBPLAY_LOOPING ); /* Flags */
1068 if( dsresult == DSERR_BUFFERLOST )
1070 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
1071 dsresult = IDirectSoundBuffer_Play(
1072 p_aout->output.p_sys->p_dsbuffer,
1075 DSBPLAY_LOOPING ); /* Flags */
1077 if( dsresult != DS_OK )
1079 msg_Err( p_aout, "cannot start playing buffer" );
1083 while( !p_notif->b_die )
1085 aout_buffer_t *p_buffer;
1088 /* wait for the position notification */
1089 i_which_frame = WaitForMultipleObjects( FRAMES_NUM,
1090 notification_events, 0,
1091 INFINITE ) - WAIT_OBJECT_0;
1093 if( p_notif->b_die )
1098 /* We take into account the current latency */
1099 if SUCCEEDED( IDirectSoundBuffer_GetCurrentPosition(
1100 p_aout->output.p_sys->p_dsbuffer,
1101 &l_latency, NULL ) )
1103 if( l_latency > (i_which_frame * FRAME_SIZE)
1104 && l_latency < ((i_which_frame+1) * FRAME_SIZE) )
1106 l_latency = - ( l_latency /
1107 p_aout->output.output.i_bytes_per_frame %
1112 l_latency = FRAME_SIZE - ( l_latency /
1113 p_aout->output.output.i_bytes_per_frame %
1122 /* Mark last frame as empty */
1123 i_last_frame = (i_which_frame + FRAMES_NUM -1) % FRAMES_NUM;
1124 i_next_frame = (i_which_frame + 1) % FRAMES_NUM;
1125 p_notif->i_frame_status[i_last_frame] = FRAME_EMPTY;
1127 /* Try to fill in as many frame buffers as possible */
1128 for( i = i_next_frame; (i % FRAMES_NUM) != i_which_frame; i++ )
1131 /* Check if frame buf is already filled */
1132 if( p_notif->i_frame_status[i % FRAMES_NUM] == FRAME_QUEUED )
1135 p_buffer = aout_OutputNextBuffer( p_aout,
1136 mtime + 1000000 / p_aout->output.output.i_rate *
1137 ((i - i_next_frame + 1) * FRAME_SIZE + l_latency), b_sleek );
1139 /* If there is no audio data available and we have some buffered
1140 * already, then just wait for the next time */
1141 if( !p_buffer && (i != i_next_frame) )
1143 //msg_Err( p_aout, "only %i frame buffers filled!",
1144 // i - i_next_frame );
1148 if( FillBuffer( p_aout, (i%FRAMES_NUM), p_buffer )
1152 /* Mark the frame buffer as QUEUED */
1153 p_notif->i_frame_status[i%FRAMES_NUM] = FRAME_QUEUED;
1158 /* make sure the buffer isn't playing */
1159 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
1161 /* free the events */
1162 for( i = 0; i < FRAMES_NUM; i++ )
1163 CloseHandle( notification_events[i] );
1165 msg_Dbg( p_notif, "DirectSoundThread exiting" );