1 /*****************************************************************************
2 * directx.c: Windows DirectX audio output method
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: directx.c,v 1.19 2003/05/04 22:42:15 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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 */
169 uint32_t i_channel_mask;
172 static const uint32_t pi_channels_in[] =
173 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
174 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
175 AOUT_CHAN_CENTER, AOUT_CHAN_LFE };
176 static const uint32_t pi_channels_out[] =
177 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
178 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
179 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY };
180 static const uint32_t pi_channels_ordered[] =
181 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_FRONT_CENTER,
182 SPEAKER_LOW_FREQUENCY,
183 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT };
185 /*****************************************************************************
187 *****************************************************************************/
188 static int OpenAudio ( vlc_object_t * );
189 static void CloseAudio ( vlc_object_t * );
190 static void Play ( aout_instance_t * );
192 /* local functions */
193 static void Probe ( aout_instance_t * );
194 static int InitDirectSound ( aout_instance_t * );
195 static int CreateDSBuffer ( aout_instance_t *, int, int, int, int, int, vlc_bool_t );
196 static int CreateDSBufferPCM ( aout_instance_t *, int*, int, int, int, vlc_bool_t );
197 static void DestroyDSBuffer ( aout_instance_t * );
198 static void DirectSoundThread ( notification_thread_t * );
199 static int FillBuffer ( aout_instance_t *, int, aout_buffer_t * );
201 static void CheckReordering ( aout_instance_t *, int );
202 static void InterleaveFloat32 ( float *, float *, int *, int );
203 static void InterleaveS16 ( int16_t *, int16_t *, int *, int );
205 /*****************************************************************************
207 *****************************************************************************/
209 set_description( _("DirectX audio output") );
210 set_capability( "audio output", 100 );
211 add_shortcut( "directx" );
212 set_callbacks( OpenAudio, CloseAudio );
215 /*****************************************************************************
216 * OpenAudio: open the audio device
217 *****************************************************************************
218 * This function opens and setups Direct Sound.
219 *****************************************************************************/
220 static int OpenAudio( vlc_object_t *p_this )
222 aout_instance_t * p_aout = (aout_instance_t *)p_this;
226 msg_Dbg( p_aout, "OpenAudio" );
228 /* Allocate structure */
229 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
230 if( p_aout->output.p_sys == NULL )
232 msg_Err( p_aout, "out of memory" );
236 /* Initialize some variables */
237 p_aout->output.p_sys->p_dsobject = NULL;
238 p_aout->output.p_sys->p_dsbuffer = NULL;
239 p_aout->output.p_sys->p_dsnotify = NULL;
240 p_aout->output.p_sys->p_notif = NULL;
241 p_aout->output.p_sys->b_playing = 0;
242 p_aout->output.p_sys->pi_chan_table = NULL;
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 /* Now we need to setup our DirectSound play notification structure */
255 p_aout->output.p_sys->p_notif =
256 vlc_object_create( p_aout, sizeof(notification_thread_t) );
257 p_aout->output.p_sys->p_notif->p_aout = p_aout;
259 if( var_Type( p_aout, "audio-device" ) == 0 )
264 if( var_Get( p_aout, "audio-device", &val ) < 0 )
266 /* Probe() has failed. */
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_2F2R )
312 p_aout->output.output.i_physical_channels
313 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
314 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
316 else if( val.i_int == AOUT_VAR_MONO )
318 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
322 p_aout->output.output.i_physical_channels
323 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
326 if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,
327 p_aout->output.output.i_physical_channels,
328 aout_FormatNbChannels( &p_aout->output.output ),
329 p_aout->output.output.i_rate, VLC_FALSE )
332 msg_Err( p_aout, "cannot open directx audio device" );
333 free( p_aout->output.p_sys );
337 /* Calculate the frame size in bytes */
338 p_aout->output.i_nb_samples = FRAME_SIZE;
339 aout_FormatPrepare( &p_aout->output.output );
340 p_aout->output.p_sys->i_frame_size =
341 FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
343 aout_VolumeSoftInit( p_aout );
346 /* then launch the notification thread */
347 msg_Dbg( p_aout, "creating DirectSoundThread" );
348 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
349 "DirectSound Notification Thread",
351 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
353 msg_Err( p_aout, "cannot create DirectSoundThread" );
357 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
362 CloseAudio( VLC_OBJECT(p_aout) );
366 /*****************************************************************************
367 * Probe: probe the audio device for available formats and channels
368 *****************************************************************************/
369 static void Probe( aout_instance_t * p_aout )
371 vlc_value_t val, text;
373 unsigned int i_physical_channels;
374 DWORD ui_speaker_config;
376 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
377 text.psz_string = _("Audio device");
378 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
380 /* Test for 5.1 support */
381 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
382 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
383 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
384 if( p_aout->output.output.i_physical_channels == i_physical_channels )
386 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
387 p_aout->output.output.i_rate, VLC_TRUE )
390 val.i_int = AOUT_VAR_5_1;
391 text.psz_string = N_("5.1");
392 var_Change( p_aout, "audio-device",
393 VLC_VAR_ADDCHOICE, &val, &text );
394 msg_Dbg( p_aout, "device supports 5.1 channels" );
398 /* Test for 2 Front 2 Rear support */
399 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
400 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
401 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
402 == i_physical_channels )
404 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
405 p_aout->output.output.i_rate, VLC_TRUE )
408 val.i_int = AOUT_VAR_2F2R;
409 text.psz_string = N_("2 Front 2 Rear");
410 var_Change( p_aout, "audio-device",
411 VLC_VAR_ADDCHOICE, &val, &text );
412 msg_Dbg( p_aout, "device supports 4 channels" );
416 /* Test for stereo support */
417 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
418 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
419 p_aout->output.output.i_rate, VLC_TRUE )
422 val.i_int = AOUT_VAR_STEREO;
423 text.psz_string = N_("Stereo");
424 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
425 msg_Dbg( p_aout, "device supports 2 channels" );
428 /* Test for mono support */
429 i_physical_channels = AOUT_CHAN_CENTER;
430 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
431 p_aout->output.output.i_rate, VLC_TRUE )
434 val.i_int = AOUT_VAR_MONO;
435 text.psz_string = N_("Mono");
436 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
437 msg_Dbg( p_aout, "device supports 1 channel" );
440 /* Check the speaker configuration to determine which channel config should
442 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
443 &ui_speaker_config ) )
445 ui_speaker_config = DSSPEAKER_STEREO;
447 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
449 case DSSPEAKER_5POINT1:
450 val.i_int = AOUT_VAR_5_1;
453 val.i_int = AOUT_VAR_2F2R;
456 val.i_int = AOUT_VAR_MONO;
458 case DSSPEAKER_SURROUND:
459 case DSSPEAKER_STEREO:
461 val.i_int = AOUT_VAR_STEREO;
464 var_Set( p_aout, "audio-device", val );
466 /* Test for SPDIF support */
467 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
469 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
470 p_aout->output.output.i_physical_channels,
471 aout_FormatNbChannels( &p_aout->output.output ),
472 p_aout->output.output.i_rate,
473 AOUT_SPDIF_SIZE, VLC_TRUE )
476 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
477 val.i_int = AOUT_VAR_SPDIF;
478 text.psz_string = N_("A/52 over S/PDIF");
479 var_Change( p_aout, "audio-device",
480 VLC_VAR_ADDCHOICE, &val, &text );
481 if( config_GetInt( p_aout, "spdif" ) )
482 var_Set( p_aout, "audio-device", val );
486 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
488 val.b_bool = VLC_TRUE;
489 var_Set( p_aout, "intf-change", val );
492 /*****************************************************************************
493 * Play: we'll start playing the directsound buffer here because at least here
494 * we know the first buffer has been put in the aout fifo and we also
496 *****************************************************************************/
497 static void Play( aout_instance_t *p_aout )
499 if( !p_aout->output.p_sys->b_playing )
501 aout_buffer_t *p_buffer;
503 p_aout->output.p_sys->b_playing = 1;
505 /* get the playing date of the first aout buffer */
506 p_aout->output.p_sys->p_notif->start_date =
507 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
509 /* fill in the first samples */
510 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
511 FillBuffer( p_aout, 0, p_buffer );
513 /* wake up the audio output thread */
514 SetEvent( p_aout->output.p_sys->p_notif->p_events[0].hEventNotify );
518 /*****************************************************************************
519 * CloseAudio: close the audio device
520 *****************************************************************************/
521 static void CloseAudio( vlc_object_t *p_this )
523 aout_instance_t * p_aout = (aout_instance_t *)p_this;
525 msg_Dbg( p_aout, "CloseAudio" );
527 /* kill the position notification thread, if any */
528 if( p_aout->output.p_sys->p_notif )
530 vlc_object_detach( p_aout->output.p_sys->p_notif );
531 if( p_aout->output.p_sys->p_notif->b_thread )
533 p_aout->output.p_sys->p_notif->b_die = 1;
535 if( !p_aout->output.p_sys->b_playing )
536 /* wake up the audio thread */
538 p_aout->output.p_sys->p_notif->p_events[0].hEventNotify );
540 vlc_thread_join( p_aout->output.p_sys->p_notif );
542 vlc_object_destroy( p_aout->output.p_sys->p_notif );
545 /* release the secondary buffer */
546 DestroyDSBuffer( p_aout );
548 /* finally release the DirectSound object */
549 if( p_aout->output.p_sys->p_dsobject )
550 IDirectSound_Release( p_aout->output.p_sys->p_dsobject );
552 /* free DSOUND.DLL */
553 if( p_aout->output.p_sys->hdsound_dll )
554 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
556 if( p_aout->output.p_sys->pi_chan_table )
557 free( p_aout->output.p_sys->pi_chan_table );
559 free( p_aout->output.p_sys );
562 /*****************************************************************************
563 * InitDirectSound: handle all the gory details of DirectSound initialisation
564 *****************************************************************************/
565 static int InitDirectSound( aout_instance_t *p_aout )
567 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
569 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
570 if( p_aout->output.p_sys->hdsound_dll == NULL )
572 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
576 OurDirectSoundCreate = (void *)GetProcAddress(
577 p_aout->output.p_sys->hdsound_dll,
578 "DirectSoundCreate" );
579 if( OurDirectSoundCreate == NULL )
581 msg_Warn( p_aout, "GetProcAddress FAILED" );
585 /* Create the direct sound object */
586 if FAILED( OurDirectSoundCreate( NULL, &p_aout->output.p_sys->p_dsobject,
589 msg_Warn( p_aout, "cannot create a direct sound device" );
593 /* Set DirectSound Cooperative level, ie what control we want over Windows
594 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
595 * settings of the primary buffer, but also that only the sound of our
596 * application will be hearable when it will have the focus.
597 * !!! (this is not really working as intended yet because to set the
598 * cooperative level you need the window handle of your application, and
599 * I don't know of any easy way to get it. Especially since we might play
600 * sound without any video, and so what window handle should we use ???
601 * The hack for now is to use the Desktop window handle - it seems to be
603 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
607 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
613 p_aout->output.p_sys->p_dsobject = NULL;
614 if( p_aout->output.p_sys->hdsound_dll )
616 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
617 p_aout->output.p_sys->hdsound_dll = NULL;
623 /*****************************************************************************
624 * CreateDSBuffer: Creates a direct sound buffer of the required format.
625 *****************************************************************************
626 * This function creates the buffer we'll use to play audio.
627 * In DirectSound there are two kinds of buffers:
628 * - the primary buffer: which is the actual buffer that the soundcard plays
629 * - the secondary buffer(s): these buffers are the one actually used by
630 * applications and DirectSound takes care of mixing them into the primary.
632 * Once you create a secondary buffer, you cannot change its format anymore so
633 * you have to release the current one and create another.
634 *****************************************************************************/
635 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
636 int i_channels, int i_nb_channels, int i_rate,
637 int i_bytes_per_frame, vlc_bool_t b_probe )
639 WAVEFORMATEXTENSIBLE waveformat;
640 DSBUFFERDESC dsbdesc;
643 /* First set the sound buffer format */
644 waveformat.dwChannelMask = 0;
645 for( i = 0; i < sizeof(pi_channels_in)/sizeof(uint32_t); i++ )
647 if( i_channels & pi_channels_in[i] )
648 waveformat.dwChannelMask |= pi_channels_out[i];
653 case VLC_FOURCC('s','p','d','i'):
655 /* To prevent channel re-ordering */
656 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
657 waveformat.Format.wBitsPerSample = 16;
658 waveformat.Samples.wValidBitsPerSample =
659 waveformat.Format.wBitsPerSample;
660 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
661 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
664 case VLC_FOURCC('f','l','3','2'):
665 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
666 waveformat.Samples.wValidBitsPerSample =
667 waveformat.Format.wBitsPerSample;
668 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
669 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
672 case VLC_FOURCC('s','1','6','l'):
673 waveformat.Format.wBitsPerSample = 16;
674 waveformat.Samples.wValidBitsPerSample =
675 waveformat.Format.wBitsPerSample;
676 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
677 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
681 waveformat.Format.nChannels = i_nb_channels;
682 waveformat.Format.nSamplesPerSec = i_rate;
683 waveformat.Format.nBlockAlign =
684 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
685 waveformat.Format.nAvgBytesPerSec =
686 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
688 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
689 if( i_nb_channels <= 2 )
691 waveformat.Format.cbSize = 0;
695 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
696 waveformat.Format.cbSize =
697 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
701 /* Then fill in the direct sound descriptor */
702 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
703 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
704 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
705 | DSBCAPS_CTRLPOSITIONNOTIFY /* We need notification */
706 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
707 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
708 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
710 if FAILED( IDirectSound_CreateSoundBuffer(
711 p_aout->output.p_sys->p_dsobject, &dsbdesc,
712 &p_aout->output.p_sys->p_dsbuffer, NULL) )
717 /* Stop here if we were just probing */
720 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
721 p_aout->output.p_sys->p_dsbuffer = NULL;
725 /* Backup the size of a frame */
726 p_aout->output.p_sys->p_notif->i_frame_size = i_bytes_per_frame;
728 /* Now the secondary buffer is created, we need to setup its position
730 for( i = 0; i < FRAMES_NUM; i++ )
732 p_aout->output.p_sys->p_notif->p_events[i].dwOffset = i *
733 p_aout->output.p_sys->p_notif->i_frame_size;
735 p_aout->output.p_sys->p_notif->i_frame_status[i] = FRAME_EMPTY;
738 /* Get the IDirectSoundNotify interface */
739 if FAILED( IDirectSoundBuffer_QueryInterface(
740 p_aout->output.p_sys->p_dsbuffer,
741 &IID_IDirectSoundNotify,
742 (LPVOID *)&p_aout->output.p_sys->p_dsnotify ) )
744 msg_Err( p_aout, "cannot get IDirectSoundNotify interface" );
748 if FAILED( IDirectSoundNotify_SetNotificationPositions(
749 p_aout->output.p_sys->p_dsnotify,
751 p_aout->output.p_sys->p_notif->p_events ) )
753 msg_Err( p_aout, "cannot set position notification" );
757 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
758 CheckReordering( p_aout, i_nb_channels );
763 DestroyDSBuffer( p_aout );
767 /*****************************************************************************
768 * CreateDSBufferPCM: creates a PCM direct sound buffer.
769 *****************************************************************************
770 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
771 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
772 ****************************************************************************/
773 static int CreateDSBufferPCM( aout_instance_t *p_aout, int *i_format,
774 int i_channels, int i_nb_channels, int i_rate,
777 if( CreateDSBuffer( p_aout, VLC_FOURCC('f','l','3','2'),
778 i_channels, i_nb_channels, i_rate,
779 FRAME_SIZE * 4 * i_nb_channels, b_probe )
782 if ( CreateDSBuffer( p_aout, VLC_FOURCC('s','1','6','l'),
783 i_channels, i_nb_channels, i_rate,
784 FRAME_SIZE * 2 * i_nb_channels, b_probe )
791 *i_format = VLC_FOURCC('s','1','6','l');
797 *i_format = VLC_FOURCC('f','l','3','2');
802 /*****************************************************************************
804 *****************************************************************************
805 * This function destroys the secondary buffer.
806 *****************************************************************************/
807 static void DestroyDSBuffer( aout_instance_t *p_aout )
809 if( p_aout->output.p_sys->p_dsnotify )
811 IDirectSoundNotify_Release( p_aout->output.p_sys->p_dsnotify );
812 p_aout->output.p_sys->p_dsnotify = NULL;
815 if( p_aout->output.p_sys->p_dsbuffer )
817 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
818 p_aout->output.p_sys->p_dsbuffer = NULL;
822 /*****************************************************************************
823 * FillBuffer: Fill in one of the direct sound frame buffers.
824 *****************************************************************************
825 * Returns VLC_SUCCESS on success.
826 *****************************************************************************/
827 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
828 aout_buffer_t *p_buffer )
830 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
831 void *p_write_position, *p_wrap_around;
832 long l_bytes1, l_bytes2;
835 /* Before copying anything, we have to lock the buffer */
836 dsresult = IDirectSoundBuffer_Lock(
837 p_aout->output.p_sys->p_dsbuffer, /* DS buffer */
838 i_frame * p_notif->i_frame_size, /* Start offset */
839 p_notif->i_frame_size, /* Number of bytes */
840 &p_write_position, /* Address of lock start */
841 &l_bytes1, /* Count of bytes locked before wrap around */
842 &p_wrap_around, /* Buffer adress (if wrap around) */
843 &l_bytes2, /* Count of bytes after wrap around */
845 if( dsresult == DSERR_BUFFERLOST )
847 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
848 dsresult = IDirectSoundBuffer_Lock(
849 p_aout->output.p_sys->p_dsbuffer,
850 i_frame * p_notif->i_frame_size,
851 p_notif->i_frame_size,
858 if( dsresult != DS_OK )
860 msg_Warn( p_notif, "cannot lock buffer" );
861 if( p_buffer ) aout_BufferFree( p_buffer );
865 if( p_buffer == NULL )
867 memset( p_write_position, 0, l_bytes1 );
869 else if( p_aout->output.p_sys->b_chan_reorder )
871 /* Do the channel reordering here */
873 if( p_aout->output.output.i_format == VLC_FOURCC('s','1','6','l') )
874 InterleaveS16( (int16_t *)p_buffer->p_buffer,
875 (int16_t *)p_write_position,
876 p_aout->output.p_sys->pi_chan_table,
877 aout_FormatNbChannels( &p_aout->output.output ) );
879 InterleaveFloat32( (float *)p_buffer->p_buffer,
880 (float *)p_write_position,
881 p_aout->output.p_sys->pi_chan_table,
882 aout_FormatNbChannels( &p_aout->output.output ) );
886 p_aout->p_vlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
888 aout_BufferFree( p_buffer );
891 /* Now the data has been copied, unlock the buffer */
892 IDirectSoundBuffer_Unlock( p_aout->output.p_sys->p_dsbuffer,
893 p_write_position, l_bytes1,
894 p_wrap_around, l_bytes2 );
899 /*****************************************************************************
900 * DirectSoundThread: this thread will capture play notification events.
901 *****************************************************************************
902 * We use this thread to emulate a callback mechanism. The thread probes for
903 * event notification and fills up the DS secondary buffer when needed.
904 *****************************************************************************/
905 static void DirectSoundThread( notification_thread_t *p_notif )
907 HANDLE notification_events[FRAMES_NUM];
909 aout_instance_t *p_aout = p_notif->p_aout;
910 int i, i_which_frame, i_last_frame, i_next_frame;
914 for( i = 0; i < FRAMES_NUM; i++ )
915 notification_events[i] = p_notif->p_events[i].hEventNotify;
917 /* We don't want any resampling when using S/PDIF output */
918 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
920 /* Tell the main thread that we are ready */
921 vlc_thread_ready( p_notif );
923 msg_Dbg( p_notif, "DirectSoundThread ready" );
925 /* Wait here until Play() is called */
926 WaitForSingleObject( notification_events[0], INFINITE );
928 if( !p_notif->b_die )
930 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
932 /* start playing the buffer */
933 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
936 DSBPLAY_LOOPING ); /* Flags */
937 if( dsresult == DSERR_BUFFERLOST )
939 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
940 dsresult = IDirectSoundBuffer_Play(
941 p_aout->output.p_sys->p_dsbuffer,
944 DSBPLAY_LOOPING ); /* Flags */
946 if( dsresult != DS_OK )
948 msg_Err( p_aout, "cannot start playing buffer" );
952 while( !p_notif->b_die )
954 aout_buffer_t *p_buffer;
957 /* wait for the position notification */
958 i_which_frame = WaitForMultipleObjects( FRAMES_NUM,
959 notification_events, 0,
960 INFINITE ) - WAIT_OBJECT_0;
967 /* We take into account the current latency */
968 if SUCCEEDED( IDirectSoundBuffer_GetCurrentPosition(
969 p_aout->output.p_sys->p_dsbuffer,
972 if( l_latency > (i_which_frame * FRAME_SIZE)
973 && l_latency < ((i_which_frame+1) * FRAME_SIZE) )
975 l_latency = - ( l_latency /
976 p_aout->output.output.i_bytes_per_frame %
981 l_latency = FRAME_SIZE - ( l_latency /
982 p_aout->output.output.i_bytes_per_frame %
991 /* Mark last frame as empty */
992 i_last_frame = (i_which_frame + FRAMES_NUM -1) % FRAMES_NUM;
993 i_next_frame = (i_which_frame + 1) % FRAMES_NUM;
994 p_notif->i_frame_status[i_last_frame] = FRAME_EMPTY;
996 /* Try to fill in as many frame buffers as possible */
997 for( i = i_next_frame; (i % FRAMES_NUM) != i_which_frame; i++ )
1000 /* Check if frame buf is already filled */
1001 if( p_notif->i_frame_status[i % FRAMES_NUM] == FRAME_QUEUED )
1004 p_buffer = aout_OutputNextBuffer( p_aout,
1005 mtime + 1000000 / p_aout->output.output.i_rate *
1006 ((i - i_next_frame + 1) * FRAME_SIZE + l_latency), b_sleek );
1008 /* If there is no audio data available and we have some buffered
1009 * already, then just wait for the next time */
1010 if( !p_buffer && (i != i_next_frame) )
1012 //msg_Err( p_aout, "only %i frame buffers filled!",
1013 // i - i_next_frame );
1017 if( FillBuffer( p_aout, (i%FRAMES_NUM), p_buffer )
1021 /* Mark the frame buffer as QUEUED */
1022 p_notif->i_frame_status[i%FRAMES_NUM] = FRAME_QUEUED;
1027 /* make sure the buffer isn't playing */
1028 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
1030 /* free the events */
1031 for( i = 0; i < FRAMES_NUM; i++ )
1032 CloseHandle( notification_events[i] );
1034 msg_Dbg( p_notif, "DirectSoundThread exiting" );
1037 /*****************************************************************************
1038 * CheckReordering: Check if we need to do some channel re-ordering (the ac3
1039 * channel order is different from the one chosen by
1041 *****************************************************************************/
1042 static void CheckReordering( aout_instance_t *p_aout, int i_nb_channels )
1046 #define i_channel_mask p_aout->output.p_sys->i_channel_mask
1047 #define pi_chan_table p_aout->output.p_sys->pi_chan_table
1049 p_aout->output.p_sys->b_chan_reorder = VLC_FALSE;
1051 pi_chan_table = malloc( i_nb_channels * sizeof(int) );
1052 if( !pi_chan_table )
1058 i < (int)(sizeof(pi_channels_out)/sizeof(uint32_t)); i++ )
1060 if( i_channel_mask & pi_channels_out[i] )
1063 pi_channels_out[i] != pi_channels_ordered[k]; k++ )
1065 if( i_channel_mask & pi_channels_ordered[k] )
1071 pi_chan_table[j] = l;
1077 for( i = 0; i < i_nb_channels; i++ )
1079 if( pi_chan_table[i] != i )
1081 p_aout->output.p_sys->b_chan_reorder = VLC_TRUE;
1085 if( p_aout->output.p_sys->b_chan_reorder )
1087 msg_Dbg( p_aout, "channel reordering needed" );
1090 #undef pi_chan_table
1094 /*****************************************************************************
1095 * InterleaveFloat32/S16: change the channel order to the Microsoft one.
1096 *****************************************************************************/
1097 static void InterleaveFloat32( float *p_buf, float *p_buf_out,
1098 int *pi_chan_table, int i_nb_channels )
1102 for( i = 0; i < FRAME_SIZE; i++ )
1104 for( j = 0; j < i_nb_channels; j++ )
1106 p_buf_out[i*i_nb_channels + pi_chan_table[j]] =
1107 p_buf[i*i_nb_channels + j];
1112 static void InterleaveS16( int16_t *p_buf, int16_t *p_buf_out,
1113 int *pi_chan_table, int i_nb_channels )
1117 for( i = 0; i < FRAME_SIZE; i++ )
1119 for( j = 0; j < i_nb_channels; j++ )
1121 p_buf_out[ i*i_nb_channels + pi_chan_table[j]] =
1122 p_buf[i*i_nb_channels + j];