1 /*****************************************************************************
2 * directx.c: Windows DirectX audio output method
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
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 */
155 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
156 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
157 * takes care of mixing all the
158 * secondary buffers into the primary) */
160 LPDIRECTSOUNDNOTIFY p_dsnotify; /* the position notify interface */
161 notification_thread_t *p_notif; /* DirectSoundThread id */
163 int b_playing; /* playing status */
165 int i_frame_size; /* Size in bytes of one frame */
167 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
168 int pi_chan_table[AOUT_CHAN_MAX];
169 uint32_t i_channel_mask;
170 uint32_t i_bits_per_sample;
174 static const uint32_t pi_channels_src[] =
175 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
176 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
177 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
178 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
179 static const uint32_t pi_channels_in[] =
180 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
181 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
182 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
183 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
184 static const uint32_t pi_channels_out[] =
185 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
186 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
187 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
188 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
190 /*****************************************************************************
192 *****************************************************************************/
193 static int OpenAudio ( vlc_object_t * );
194 static void CloseAudio ( vlc_object_t * );
195 static void Play ( aout_instance_t * );
197 /* local functions */
198 static void Probe ( aout_instance_t * );
199 static int InitDirectSound ( aout_instance_t * );
200 static int CreateDSBuffer ( aout_instance_t *, int, int, int, int, int, vlc_bool_t );
201 static int CreateDSBufferPCM ( aout_instance_t *, int*, int, int, int, vlc_bool_t );
202 static void DestroyDSBuffer ( aout_instance_t * );
203 static void DirectSoundThread ( notification_thread_t * );
204 static int FillBuffer ( aout_instance_t *, int, aout_buffer_t * );
206 /*****************************************************************************
208 *****************************************************************************/
210 set_description( _("DirectX audio output") );
211 set_capability( "audio output", 100 );
212 add_shortcut( "directx" );
213 set_callbacks( OpenAudio, CloseAudio );
216 /*****************************************************************************
217 * OpenAudio: open the audio device
218 *****************************************************************************
219 * This function opens and setups Direct Sound.
220 *****************************************************************************/
221 static int OpenAudio( vlc_object_t *p_this )
223 aout_instance_t * p_aout = (aout_instance_t *)p_this;
227 msg_Dbg( p_aout, "OpenAudio" );
229 /* Allocate structure */
230 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
231 if( p_aout->output.p_sys == NULL )
233 msg_Err( p_aout, "out of memory" );
237 /* Initialize some variables */
238 p_aout->output.p_sys->p_dsobject = NULL;
239 p_aout->output.p_sys->p_dsbuffer = NULL;
240 p_aout->output.p_sys->p_dsnotify = NULL;
241 p_aout->output.p_sys->p_notif = NULL;
242 p_aout->output.p_sys->b_playing = 0;
244 p_aout->output.pf_play = Play;
245 aout_VolumeSoftInit( p_aout );
247 /* Initialise DirectSound */
248 if( InitDirectSound( p_aout ) )
250 msg_Err( p_aout, "cannot initialize DirectSound" );
254 if( var_Type( p_aout, "audio-device" ) == 0 )
259 if( var_Get( p_aout, "audio-device", &val ) < 0 )
261 /* Probe() has failed. */
265 /* Now we need to setup our DirectSound play notification structure */
266 p_aout->output.p_sys->p_notif =
267 vlc_object_create( p_aout, sizeof(notification_thread_t) );
268 p_aout->output.p_sys->p_notif->p_aout = p_aout;
270 /* Then create the notification events */
271 for( i = 0; i < FRAMES_NUM; i++ )
272 p_aout->output.p_sys->p_notif->p_events[i].hEventNotify =
273 CreateEvent( NULL, FALSE, FALSE, NULL );
275 /* Open the device */
276 if( val.i_int == AOUT_VAR_SPDIF )
278 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
280 /* Calculate the frame size in bytes */
281 p_aout->output.i_nb_samples = A52_FRAME_NB;
282 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
283 p_aout->output.output.i_frame_length = A52_FRAME_NB;
284 p_aout->output.p_sys->i_frame_size =
285 p_aout->output.output.i_bytes_per_frame;
287 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
288 p_aout->output.output.i_physical_channels,
289 aout_FormatNbChannels( &p_aout->output.output ),
290 p_aout->output.output.i_rate,
291 p_aout->output.p_sys->i_frame_size, VLC_FALSE )
294 msg_Err( p_aout, "cannot open directx audio device" );
295 free( p_aout->output.p_sys );
299 aout_VolumeNoneInit( p_aout );
303 if( val.i_int == AOUT_VAR_5_1 )
305 p_aout->output.output.i_physical_channels
306 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
307 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
310 else if( val.i_int == AOUT_VAR_3F2R )
312 p_aout->output.output.i_physical_channels
313 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
314 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
316 else if( val.i_int == AOUT_VAR_2F2R )
318 p_aout->output.output.i_physical_channels
319 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
320 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
322 else if( val.i_int == AOUT_VAR_MONO )
324 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
328 p_aout->output.output.i_physical_channels
329 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
332 if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,
333 p_aout->output.output.i_physical_channels,
334 aout_FormatNbChannels( &p_aout->output.output ),
335 p_aout->output.output.i_rate, VLC_FALSE )
338 msg_Err( p_aout, "cannot open directx audio device" );
339 free( p_aout->output.p_sys );
343 /* Calculate the frame size in bytes */
344 p_aout->output.i_nb_samples = FRAME_SIZE;
345 aout_FormatPrepare( &p_aout->output.output );
346 p_aout->output.p_sys->i_frame_size =
347 FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
349 aout_VolumeSoftInit( p_aout );
352 /* then launch the notification thread */
353 msg_Dbg( p_aout, "creating DirectSoundThread" );
354 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
355 "DirectSound Notification Thread",
357 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
359 msg_Err( p_aout, "cannot create DirectSoundThread" );
363 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
368 CloseAudio( VLC_OBJECT(p_aout) );
372 /*****************************************************************************
373 * Probe: probe the audio device for available formats and channels
374 *****************************************************************************/
375 static void Probe( aout_instance_t * p_aout )
377 vlc_value_t val, text;
379 unsigned int i_physical_channels;
380 DWORD ui_speaker_config;
382 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
383 text.psz_string = _("Audio Device");
384 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
386 /* Test for 5.1 support */
387 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
388 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
389 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
390 if( p_aout->output.output.i_physical_channels == i_physical_channels )
392 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
393 p_aout->output.output.i_rate, VLC_TRUE )
396 val.i_int = AOUT_VAR_5_1;
397 text.psz_string = N_("5.1");
398 var_Change( p_aout, "audio-device",
399 VLC_VAR_ADDCHOICE, &val, &text );
400 msg_Dbg( p_aout, "device supports 5.1 channels" );
404 /* Test for 3 Front 2 Rear support */
405 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
406 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
408 if( p_aout->output.output.i_physical_channels == i_physical_channels )
410 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
411 p_aout->output.output.i_rate, VLC_TRUE )
414 val.i_int = AOUT_VAR_3F2R;
415 text.psz_string = N_("3 Front 2 Rear");
416 var_Change( p_aout, "audio-device",
417 VLC_VAR_ADDCHOICE, &val, &text );
418 msg_Dbg( p_aout, "device supports 5 channels" );
422 /* Test for 2 Front 2 Rear support */
423 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
424 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
425 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
426 == i_physical_channels )
428 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
429 p_aout->output.output.i_rate, VLC_TRUE )
432 val.i_int = AOUT_VAR_2F2R;
433 text.psz_string = N_("2 Front 2 Rear");
434 var_Change( p_aout, "audio-device",
435 VLC_VAR_ADDCHOICE, &val, &text );
436 msg_Dbg( p_aout, "device supports 4 channels" );
440 /* Test for stereo support */
441 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
442 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
443 p_aout->output.output.i_rate, VLC_TRUE )
446 val.i_int = AOUT_VAR_STEREO;
447 text.psz_string = N_("Stereo");
448 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
449 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
450 msg_Dbg( p_aout, "device supports 2 channels" );
453 /* Test for mono support */
454 i_physical_channels = AOUT_CHAN_CENTER;
455 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
456 p_aout->output.output.i_rate, VLC_TRUE )
459 val.i_int = AOUT_VAR_MONO;
460 text.psz_string = N_("Mono");
461 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
462 msg_Dbg( p_aout, "device supports 1 channel" );
465 /* Check the speaker configuration to determine which channel config should
467 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
468 &ui_speaker_config ) )
470 ui_speaker_config = DSSPEAKER_STEREO;
472 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
474 case DSSPEAKER_5POINT1:
475 val.i_int = AOUT_VAR_5_1;
478 val.i_int = AOUT_VAR_2F2R;
480 #if 0 /* Lots of people just get their settings wrong and complain that
481 * this is a problem with VLC so just don't ever set mono by default. */
483 val.i_int = AOUT_VAR_MONO;
486 case DSSPEAKER_SURROUND:
487 case DSSPEAKER_STEREO:
489 val.i_int = AOUT_VAR_STEREO;
492 var_Set( p_aout, "audio-device", val );
494 /* Test for SPDIF support */
495 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
497 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
498 p_aout->output.output.i_physical_channels,
499 aout_FormatNbChannels( &p_aout->output.output ),
500 p_aout->output.output.i_rate,
501 AOUT_SPDIF_SIZE, VLC_TRUE )
504 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
505 val.i_int = AOUT_VAR_SPDIF;
506 text.psz_string = N_("A/52 over S/PDIF");
507 var_Change( p_aout, "audio-device",
508 VLC_VAR_ADDCHOICE, &val, &text );
509 if( config_GetInt( p_aout, "spdif" ) )
510 var_Set( p_aout, "audio-device", val );
514 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
517 /* Probe() has failed. */
518 var_Destroy( p_aout, "audio-device" );
522 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
524 val.b_bool = VLC_TRUE;
525 var_Set( p_aout, "intf-change", val );
528 /*****************************************************************************
529 * Play: we'll start playing the directsound buffer here because at least here
530 * we know the first buffer has been put in the aout fifo and we also
532 *****************************************************************************/
533 static void Play( aout_instance_t *p_aout )
535 if( !p_aout->output.p_sys->b_playing )
537 aout_buffer_t *p_buffer;
539 p_aout->output.p_sys->b_playing = 1;
541 /* get the playing date of the first aout buffer */
542 p_aout->output.p_sys->p_notif->start_date =
543 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
545 /* fill in the first samples */
546 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
547 FillBuffer( p_aout, 0, p_buffer );
549 /* wake up the audio output thread */
550 SetEvent( p_aout->output.p_sys->p_notif->p_events[0].hEventNotify );
554 /*****************************************************************************
555 * CloseAudio: close the audio device
556 *****************************************************************************/
557 static void CloseAudio( vlc_object_t *p_this )
559 aout_instance_t * p_aout = (aout_instance_t *)p_this;
560 aout_sys_t *p_sys = p_aout->output.p_sys;
562 msg_Dbg( p_aout, "CloseAudio" );
564 /* kill the position notification thread, if any */
567 vlc_object_detach( p_sys->p_notif );
568 if( p_sys->p_notif->b_thread )
570 p_sys->p_notif->b_die = 1;
572 if( !p_sys->b_playing )
573 /* wake up the audio thread */
574 SetEvent( p_sys->p_notif->p_events[0].hEventNotify );
576 vlc_thread_join( p_sys->p_notif );
578 vlc_object_destroy( p_sys->p_notif );
581 /* release the secondary buffer */
582 DestroyDSBuffer( p_aout );
584 /* finally release the DirectSound object */
585 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
587 /* free DSOUND.DLL */
588 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
593 /*****************************************************************************
594 * InitDirectSound: handle all the gory details of DirectSound initialisation
595 *****************************************************************************/
596 static int InitDirectSound( aout_instance_t *p_aout )
598 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
600 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
601 if( p_aout->output.p_sys->hdsound_dll == NULL )
603 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
607 OurDirectSoundCreate = (void *)GetProcAddress(
608 p_aout->output.p_sys->hdsound_dll,
609 "DirectSoundCreate" );
610 if( OurDirectSoundCreate == NULL )
612 msg_Warn( p_aout, "GetProcAddress FAILED" );
616 /* Create the direct sound object */
617 if FAILED( OurDirectSoundCreate( NULL, &p_aout->output.p_sys->p_dsobject,
620 msg_Warn( p_aout, "cannot create a direct sound device" );
624 /* Set DirectSound Cooperative level, ie what control we want over Windows
625 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
626 * settings of the primary buffer, but also that only the sound of our
627 * application will be hearable when it will have the focus.
628 * !!! (this is not really working as intended yet because to set the
629 * cooperative level you need the window handle of your application, and
630 * I don't know of any easy way to get it. Especially since we might play
631 * sound without any video, and so what window handle should we use ???
632 * The hack for now is to use the Desktop window handle - it seems to be
634 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
638 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
644 p_aout->output.p_sys->p_dsobject = NULL;
645 if( p_aout->output.p_sys->hdsound_dll )
647 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
648 p_aout->output.p_sys->hdsound_dll = NULL;
654 /*****************************************************************************
655 * CreateDSBuffer: Creates a direct sound buffer of the required format.
656 *****************************************************************************
657 * This function creates the buffer we'll use to play audio.
658 * In DirectSound there are two kinds of buffers:
659 * - the primary buffer: which is the actual buffer that the soundcard plays
660 * - the secondary buffer(s): these buffers are the one actually used by
661 * applications and DirectSound takes care of mixing them into the primary.
663 * Once you create a secondary buffer, you cannot change its format anymore so
664 * you have to release the current one and create another.
665 *****************************************************************************/
666 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
667 int i_channels, int i_nb_channels, int i_rate,
668 int i_bytes_per_frame, vlc_bool_t b_probe )
670 WAVEFORMATEXTENSIBLE waveformat;
671 DSBUFFERDESC dsbdesc;
674 /* First set the sound buffer format */
675 waveformat.dwChannelMask = 0;
676 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
678 if( i_channels & pi_channels_src[i] )
679 waveformat.dwChannelMask |= pi_channels_in[i];
684 case VLC_FOURCC('s','p','d','i'):
686 /* To prevent channel re-ordering */
687 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
688 waveformat.Format.wBitsPerSample = 16;
689 waveformat.Samples.wValidBitsPerSample =
690 waveformat.Format.wBitsPerSample;
691 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
692 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
695 case VLC_FOURCC('f','l','3','2'):
696 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
697 waveformat.Samples.wValidBitsPerSample =
698 waveformat.Format.wBitsPerSample;
699 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
700 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
703 case VLC_FOURCC('s','1','6','l'):
704 waveformat.Format.wBitsPerSample = 16;
705 waveformat.Samples.wValidBitsPerSample =
706 waveformat.Format.wBitsPerSample;
707 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
708 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
712 waveformat.Format.nChannels = i_nb_channels;
713 waveformat.Format.nSamplesPerSec = i_rate;
714 waveformat.Format.nBlockAlign =
715 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
716 waveformat.Format.nAvgBytesPerSec =
717 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
719 p_aout->output.p_sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
720 p_aout->output.p_sys->i_channels = i_nb_channels;
722 /* Then fill in the direct sound descriptor */
723 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
724 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
725 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
726 | DSBCAPS_CTRLPOSITIONNOTIFY /* We need notification */
727 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
729 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
730 if( i_nb_channels <= 2 )
732 waveformat.Format.cbSize = 0;
736 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
737 waveformat.Format.cbSize =
738 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
740 /* Needed for 5.1 on emu101k */
741 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
744 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
745 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
747 if FAILED( IDirectSound_CreateSoundBuffer(
748 p_aout->output.p_sys->p_dsobject, &dsbdesc,
749 &p_aout->output.p_sys->p_dsbuffer, NULL) )
751 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
753 /* Try without DSBCAPS_LOCHARDWARE */
754 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
755 if FAILED( IDirectSound_CreateSoundBuffer(
756 p_aout->output.p_sys->p_dsobject, &dsbdesc,
757 &p_aout->output.p_sys->p_dsbuffer, NULL) )
762 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
770 /* Stop here if we were just probing */
773 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
774 p_aout->output.p_sys->p_dsbuffer = NULL;
778 /* Backup the size of a frame */
779 p_aout->output.p_sys->p_notif->i_frame_size = i_bytes_per_frame;
781 /* Now the secondary buffer is created, we need to setup its position
783 for( i = 0; i < FRAMES_NUM; i++ )
785 p_aout->output.p_sys->p_notif->p_events[i].dwOffset = i *
786 p_aout->output.p_sys->p_notif->i_frame_size;
788 p_aout->output.p_sys->p_notif->i_frame_status[i] = FRAME_EMPTY;
791 /* Get the IDirectSoundNotify interface */
792 if FAILED( IDirectSoundBuffer_QueryInterface(
793 p_aout->output.p_sys->p_dsbuffer,
794 &IID_IDirectSoundNotify,
795 (LPVOID *)&p_aout->output.p_sys->p_dsnotify ) )
797 msg_Err( p_aout, "cannot get IDirectSoundNotify interface" );
801 if FAILED( IDirectSoundNotify_SetNotificationPositions(
802 p_aout->output.p_sys->p_dsnotify,
804 p_aout->output.p_sys->p_notif->p_events ) )
806 msg_Err( p_aout, "cannot set position notification" );
810 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
812 p_aout->output.p_sys->b_chan_reorder =
813 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
814 waveformat.dwChannelMask, i_nb_channels,
815 p_aout->output.p_sys->pi_chan_table );
817 if( p_aout->output.p_sys->b_chan_reorder )
819 msg_Dbg( p_aout, "channel reordering needed" );
825 DestroyDSBuffer( p_aout );
829 /*****************************************************************************
830 * CreateDSBufferPCM: creates a PCM direct sound buffer.
831 *****************************************************************************
832 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
833 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
834 ****************************************************************************/
835 static int CreateDSBufferPCM( aout_instance_t *p_aout, int *i_format,
836 int i_channels, int i_nb_channels, int i_rate,
839 /* Float32 audio samples are not supported for 5.1 output on the emu101k */
841 if( i_nb_channels > 2 ||
842 CreateDSBuffer( p_aout, VLC_FOURCC('f','l','3','2'),
843 i_channels, i_nb_channels, i_rate,
844 FRAME_SIZE * 4 * i_nb_channels, b_probe )
847 if ( CreateDSBuffer( p_aout, VLC_FOURCC('s','1','6','l'),
848 i_channels, i_nb_channels, i_rate,
849 FRAME_SIZE * 2 * i_nb_channels, b_probe )
856 *i_format = VLC_FOURCC('s','1','6','l');
862 *i_format = VLC_FOURCC('f','l','3','2');
867 /*****************************************************************************
869 *****************************************************************************
870 * This function destroys the secondary buffer.
871 *****************************************************************************/
872 static void DestroyDSBuffer( aout_instance_t *p_aout )
874 if( p_aout->output.p_sys->p_dsnotify )
876 IDirectSoundNotify_Release( p_aout->output.p_sys->p_dsnotify );
877 p_aout->output.p_sys->p_dsnotify = NULL;
880 if( p_aout->output.p_sys->p_dsbuffer )
882 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
883 p_aout->output.p_sys->p_dsbuffer = NULL;
887 /*****************************************************************************
888 * FillBuffer: Fill in one of the direct sound frame buffers.
889 *****************************************************************************
890 * Returns VLC_SUCCESS on success.
891 *****************************************************************************/
892 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
893 aout_buffer_t *p_buffer )
895 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
896 aout_sys_t *p_sys = p_aout->output.p_sys;
897 void *p_write_position, *p_wrap_around;
898 long l_bytes1, l_bytes2;
901 /* Before copying anything, we have to lock the buffer */
902 dsresult = IDirectSoundBuffer_Lock(
903 p_sys->p_dsbuffer, /* DS buffer */
904 i_frame * p_notif->i_frame_size, /* Start offset */
905 p_notif->i_frame_size, /* Number of bytes */
906 &p_write_position, /* Address of lock start */
907 &l_bytes1, /* Count of bytes locked before wrap around */
908 &p_wrap_around, /* Buffer adress (if wrap around) */
909 &l_bytes2, /* Count of bytes after wrap around */
911 if( dsresult == DSERR_BUFFERLOST )
913 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
914 dsresult = IDirectSoundBuffer_Lock(
916 i_frame * p_notif->i_frame_size,
917 p_notif->i_frame_size,
924 if( dsresult != DS_OK )
926 msg_Warn( p_notif, "cannot lock buffer" );
927 if( p_buffer ) aout_BufferFree( p_buffer );
931 if( p_buffer == NULL )
933 memset( p_write_position, 0, l_bytes1 );
937 if( p_sys->b_chan_reorder )
939 /* Do the channel reordering here */
940 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
941 p_sys->i_channels, p_sys->pi_chan_table,
942 p_sys->i_bits_per_sample );
945 p_aout->p_vlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
947 aout_BufferFree( p_buffer );
950 /* Now the data has been copied, unlock the buffer */
951 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
952 p_wrap_around, l_bytes2 );
957 /*****************************************************************************
958 * DirectSoundThread: this thread will capture play notification events.
959 *****************************************************************************
960 * We use this thread to emulate a callback mechanism. The thread probes for
961 * event notification and fills up the DS secondary buffer when needed.
962 *****************************************************************************/
963 static void DirectSoundThread( notification_thread_t *p_notif )
965 HANDLE notification_events[FRAMES_NUM];
967 aout_instance_t *p_aout = p_notif->p_aout;
968 int i, i_which_frame, i_last_frame, i_next_frame;
972 for( i = 0; i < FRAMES_NUM; i++ )
973 notification_events[i] = p_notif->p_events[i].hEventNotify;
975 /* We don't want any resampling when using S/PDIF output */
976 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
978 /* Tell the main thread that we are ready */
979 vlc_thread_ready( p_notif );
981 msg_Dbg( p_notif, "DirectSoundThread ready" );
983 /* Wait here until Play() is called */
984 WaitForSingleObject( notification_events[0], INFINITE );
986 if( !p_notif->b_die )
988 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
990 /* start playing the buffer */
991 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
994 DSBPLAY_LOOPING ); /* Flags */
995 if( dsresult == DSERR_BUFFERLOST )
997 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
998 dsresult = IDirectSoundBuffer_Play(
999 p_aout->output.p_sys->p_dsbuffer,
1002 DSBPLAY_LOOPING ); /* Flags */
1004 if( dsresult != DS_OK )
1006 msg_Err( p_aout, "cannot start playing buffer" );
1010 while( !p_notif->b_die )
1012 aout_buffer_t *p_buffer;
1015 /* wait for the position notification */
1016 i_which_frame = WaitForMultipleObjects( FRAMES_NUM,
1017 notification_events, 0,
1018 INFINITE ) - WAIT_OBJECT_0;
1020 if( p_notif->b_die )
1025 /* We take into account the current latency */
1026 if SUCCEEDED( IDirectSoundBuffer_GetCurrentPosition(
1027 p_aout->output.p_sys->p_dsbuffer,
1028 &l_latency, NULL ) )
1030 if( l_latency > (i_which_frame * FRAME_SIZE)
1031 && l_latency < ((i_which_frame+1) * FRAME_SIZE) )
1033 l_latency = - ( l_latency /
1034 p_aout->output.output.i_bytes_per_frame %
1039 l_latency = FRAME_SIZE - ( l_latency /
1040 p_aout->output.output.i_bytes_per_frame %
1049 /* Mark last frame as empty */
1050 i_last_frame = (i_which_frame + FRAMES_NUM -1) % FRAMES_NUM;
1051 i_next_frame = (i_which_frame + 1) % FRAMES_NUM;
1052 p_notif->i_frame_status[i_last_frame] = FRAME_EMPTY;
1054 /* Try to fill in as many frame buffers as possible */
1055 for( i = i_next_frame; (i % FRAMES_NUM) != i_which_frame; i++ )
1058 /* Check if frame buf is already filled */
1059 if( p_notif->i_frame_status[i % FRAMES_NUM] == FRAME_QUEUED )
1062 p_buffer = aout_OutputNextBuffer( p_aout,
1063 mtime + 1000000 / p_aout->output.output.i_rate *
1064 ((i - i_next_frame + 1) * FRAME_SIZE + l_latency), b_sleek );
1066 /* If there is no audio data available and we have some buffered
1067 * already, then just wait for the next time */
1068 if( !p_buffer && (i != i_next_frame) )
1070 //msg_Err( p_aout, "only %i frame buffers filled!",
1071 // i - i_next_frame );
1075 if( FillBuffer( p_aout, (i%FRAMES_NUM), p_buffer )
1079 /* Mark the frame buffer as QUEUED */
1080 p_notif->i_frame_status[i%FRAMES_NUM] = FRAME_QUEUED;
1085 /* make sure the buffer isn't playing */
1086 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
1088 /* free the events */
1089 for( i = 0; i < FRAMES_NUM; i++ )
1090 CloseHandle( notification_events[i] );
1092 msg_Dbg( p_notif, "DirectSoundThread exiting" );