1 /*****************************************************************************
2 * directx.c: Windows DirectX audio output method
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: directx.c,v 1.16 2003/03/30 18:14:36 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 #ifndef KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
127 DEFINE_GUID( KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
129 #ifndef KSDATAFORMAT_SUBTYPE_PCM
130 DEFINE_GUID( KSDATAFORMAT_SUBTYPE_PCM, WAVE_FORMAT_PCM, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
132 #ifndef KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF
133 DEFINE_GUID( KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF, WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
136 /*****************************************************************************
137 * notification_thread_t: DirectX event thread
138 *****************************************************************************/
139 typedef struct notification_thread_t
143 aout_instance_t * p_aout;
144 int i_frame_status[FRAMES_NUM]; /* status of each frame buffer */
145 DSBPOSITIONNOTIFY p_events[FRAMES_NUM]; /* play notification events */
146 int i_frame_size; /* Size in bytes of one frame */
150 } notification_thread_t;
152 /*****************************************************************************
153 * aout_sys_t: directx audio output method descriptor
154 *****************************************************************************
155 * This structure is part of the audio output thread descriptor.
156 * It describes the direct sound specific properties of an audio device.
157 *****************************************************************************/
160 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
161 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
162 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
163 * takes care of mixing all the
164 * secondary buffers into the primary) */
166 LPDIRECTSOUNDNOTIFY p_dsnotify; /* the position notify interface */
167 notification_thread_t *p_notif; /* DirectSoundThread id */
169 int b_playing; /* playing status */
171 int i_frame_size; /* Size in bytes of one frame */
173 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
175 uint32_t i_channel_mask;
178 static const uint32_t pi_channels_in[] =
179 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
180 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
181 AOUT_CHAN_CENTER, AOUT_CHAN_LFE };
182 static const uint32_t pi_channels_out[] =
183 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
184 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
185 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY };
186 static const uint32_t pi_channels_ordered[] =
187 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_FRONT_CENTER,
188 SPEAKER_LOW_FREQUENCY,
189 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT };
191 /*****************************************************************************
193 *****************************************************************************/
194 static int OpenAudio ( vlc_object_t * );
195 static void CloseAudio ( vlc_object_t * );
196 static void Play ( aout_instance_t * );
198 /* local functions */
199 static void Probe ( aout_instance_t * );
200 static int InitDirectSound ( aout_instance_t * );
201 static int CreateDSBuffer ( aout_instance_t *, int, int, int, int, int, vlc_bool_t );
202 static int CreateDSBufferPCM ( aout_instance_t *, int*, int, int, int, vlc_bool_t );
203 static void DestroyDSBuffer ( aout_instance_t * );
204 static void DirectSoundThread ( notification_thread_t * );
205 static int FillBuffer ( aout_instance_t *, int, aout_buffer_t * );
207 static void CheckReordering ( aout_instance_t *, int );
208 static void InterleaveFloat32 ( float *, float *, int *, int );
209 static void InterleaveS16 ( int16_t *, int16_t *, int *, int );
211 /*****************************************************************************
213 *****************************************************************************/
215 set_description( _("DirectX audio output") );
216 set_capability( "audio output", 100 );
217 add_shortcut( "directx" );
218 set_callbacks( OpenAudio, CloseAudio );
221 /*****************************************************************************
222 * OpenAudio: open the audio device
223 *****************************************************************************
224 * This function opens and setups Direct Sound.
225 *****************************************************************************/
226 static int OpenAudio( vlc_object_t *p_this )
228 aout_instance_t * p_aout = (aout_instance_t *)p_this;
232 msg_Dbg( p_aout, "OpenAudio" );
234 /* Allocate structure */
235 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
236 if( p_aout->output.p_sys == NULL )
238 msg_Err( p_aout, "out of memory" );
242 /* Initialize some variables */
243 p_aout->output.p_sys->p_dsobject = NULL;
244 p_aout->output.p_sys->p_dsbuffer = NULL;
245 p_aout->output.p_sys->p_dsnotify = NULL;
246 p_aout->output.p_sys->p_notif = NULL;
247 p_aout->output.p_sys->b_playing = 0;
248 p_aout->output.p_sys->pi_chan_table = NULL;
250 p_aout->output.pf_play = Play;
251 aout_VolumeSoftInit( p_aout );
253 /* Initialise DirectSound */
254 if( InitDirectSound( p_aout ) )
256 msg_Err( p_aout, "cannot initialize DirectSound" );
260 /* Now we need to setup our DirectSound play notification structure */
261 p_aout->output.p_sys->p_notif =
262 vlc_object_create( p_aout, sizeof(notification_thread_t) );
263 p_aout->output.p_sys->p_notif->p_aout = p_aout;
265 if( var_Type( p_aout, "audio-device" ) == 0 )
270 if( var_Get( p_aout, "audio-device", &val ) < 0 )
272 /* Probe() has failed. */
273 free( p_aout->output.p_sys );
277 /* Then create the notification events */
278 for( i = 0; i < FRAMES_NUM; i++ )
279 p_aout->output.p_sys->p_notif->p_events[i].hEventNotify =
280 CreateEvent( NULL, FALSE, FALSE, NULL );
282 /* Open the device */
283 if( !strcmp( val.psz_string, N_("A/52 over S/PDIF") ) )
285 free( val.psz_string );
286 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
288 /* Calculate the frame size in bytes */
289 p_aout->output.i_nb_samples = A52_FRAME_NB;
290 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
291 p_aout->output.output.i_frame_length = A52_FRAME_NB;
292 p_aout->output.p_sys->i_frame_size =
293 p_aout->output.output.i_bytes_per_frame;
295 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
296 p_aout->output.output.i_physical_channels,
297 aout_FormatNbChannels( &p_aout->output.output ),
298 p_aout->output.output.i_rate,
299 p_aout->output.p_sys->i_frame_size, VLC_FALSE )
302 msg_Err( p_aout, "cannot open waveout audio device" );
303 free( p_aout->output.p_sys );
307 aout_VolumeNoneInit( p_aout );
311 if( !strcmp( val.psz_string, N_("5.1") ) )
313 p_aout->output.output.i_physical_channels
314 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
315 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
318 else if( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
320 p_aout->output.output.i_physical_channels
321 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
322 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
324 else if( !strcmp( val.psz_string, "Mono" ) )
326 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
330 p_aout->output.output.i_physical_channels
331 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
333 free( val.psz_string );
335 if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,
336 p_aout->output.output.i_physical_channels,
337 aout_FormatNbChannels( &p_aout->output.output ),
338 p_aout->output.output.i_rate, VLC_FALSE )
341 msg_Err( p_aout, "cannot open waveout audio device" );
342 free( p_aout->output.p_sys );
346 /* Calculate the frame size in bytes */
347 p_aout->output.i_nb_samples = FRAME_SIZE;
348 aout_FormatPrepare( &p_aout->output.output );
349 p_aout->output.p_sys->i_frame_size =
350 FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
352 aout_VolumeSoftInit( p_aout );
355 /* then launch the notification thread */
356 msg_Dbg( p_aout, "creating DirectSoundThread" );
357 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
358 "DirectSound Notification Thread",
360 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
362 msg_Err( p_aout, "cannot create DirectSoundThread" );
366 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
371 CloseAudio( VLC_OBJECT(p_aout) );
375 /*****************************************************************************
376 * Probe: probe the audio device for available formats and channels
377 *****************************************************************************/
378 static void Probe( aout_instance_t * p_aout )
382 unsigned int i_physical_channels;
383 DWORD ui_speaker_config;
385 var_Create( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
387 /* Test for 5.1 support */
388 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
389 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
390 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
391 if( p_aout->output.output.i_physical_channels == i_physical_channels )
393 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
394 p_aout->output.output.i_rate, VLC_TRUE )
397 val.psz_string = N_("5.1");
398 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
399 msg_Dbg( p_aout, "device supports 5.1 channels" );
403 /* Test for 2 Front 2 Rear support */
404 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
405 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
406 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
407 == i_physical_channels )
409 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
410 p_aout->output.output.i_rate, VLC_TRUE )
413 val.psz_string = N_("2 Front 2 Rear");
414 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
415 msg_Dbg( p_aout, "device supports 4 channels" );
419 /* Test for stereo support */
420 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
421 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
422 p_aout->output.output.i_rate, VLC_TRUE )
425 val.psz_string = N_("Stereo");
426 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
427 msg_Dbg( p_aout, "device supports 2 channels" );
430 /* Test for mono support */
431 i_physical_channels = AOUT_CHAN_CENTER;
432 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
433 p_aout->output.output.i_rate, VLC_TRUE )
436 val.psz_string = N_("Mono");
437 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
438 msg_Dbg( p_aout, "device supports 1 channel" );
441 /* Check the speaker configuration to determine which channel config should
443 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
444 &ui_speaker_config ) )
446 ui_speaker_config = DSSPEAKER_STEREO;
448 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
450 case DSSPEAKER_5POINT1:
451 val.psz_string = N_("5.1");
454 val.psz_string = N_("2 Front 2 Rear");
457 val.psz_string = N_("Mono");
459 case DSSPEAKER_SURROUND:
460 case DSSPEAKER_STEREO:
462 val.psz_string = N_("Stereo");
465 var_Set( p_aout, "audio-device", val );
467 /* Test for SPDIF support */
468 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
470 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
471 p_aout->output.output.i_physical_channels,
472 aout_FormatNbChannels( &p_aout->output.output ),
473 p_aout->output.output.i_rate,
474 AOUT_SPDIF_SIZE, VLC_TRUE )
477 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
478 val.psz_string = N_("A/52 over S/PDIF");
479 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
480 if( config_GetInt( p_aout, "spdif" ) )
481 var_Set( p_aout, "audio-device", val );
485 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
487 val.b_bool = VLC_TRUE;
488 var_Set( p_aout, "intf-change", val );
491 /*****************************************************************************
492 * Play: we'll start playing the directsound buffer here because at least here
493 * we know the first buffer has been put in the aout fifo and we also
495 *****************************************************************************/
496 static void Play( aout_instance_t *p_aout )
498 if( !p_aout->output.p_sys->b_playing )
500 aout_buffer_t *p_buffer;
502 p_aout->output.p_sys->b_playing = 1;
504 /* get the playing date of the first aout buffer */
505 p_aout->output.p_sys->p_notif->start_date =
506 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
508 /* fill in the first samples */
509 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
510 FillBuffer( p_aout, 0, p_buffer );
512 /* wake up the audio output thread */
513 SetEvent( p_aout->output.p_sys->p_notif->p_events[0].hEventNotify );
517 /*****************************************************************************
518 * CloseAudio: close the audio device
519 *****************************************************************************/
520 static void CloseAudio( vlc_object_t *p_this )
522 aout_instance_t * p_aout = (aout_instance_t *)p_this;
524 msg_Dbg( p_aout, "CloseAudio" );
526 /* kill the position notification thread, if any */
527 if( p_aout->output.p_sys->p_notif )
529 vlc_object_detach( p_aout->output.p_sys->p_notif );
530 if( p_aout->output.p_sys->p_notif->b_thread )
532 p_aout->output.p_sys->p_notif->b_die = 1;
534 if( !p_aout->output.p_sys->b_playing )
535 /* wake up the audio thread */
537 p_aout->output.p_sys->p_notif->p_events[0].hEventNotify );
539 vlc_thread_join( p_aout->output.p_sys->p_notif );
541 vlc_object_destroy( p_aout->output.p_sys->p_notif );
544 /* release the secondary buffer */
545 DestroyDSBuffer( p_aout );
547 /* finally release the DirectSound object */
548 if( p_aout->output.p_sys->p_dsobject )
549 IDirectSound_Release( p_aout->output.p_sys->p_dsobject );
551 /* free DSOUND.DLL */
552 if( p_aout->output.p_sys->hdsound_dll )
553 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
555 if( p_aout->output.p_sys->pi_chan_table )
556 free( p_aout->output.p_sys->pi_chan_table );
558 free( p_aout->output.p_sys );
561 /*****************************************************************************
562 * InitDirectSound: handle all the gory details of DirectSound initialisation
563 *****************************************************************************/
564 static int InitDirectSound( aout_instance_t *p_aout )
566 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
568 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
569 if( p_aout->output.p_sys->hdsound_dll == NULL )
571 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
575 OurDirectSoundCreate = (void *)GetProcAddress(
576 p_aout->output.p_sys->hdsound_dll,
577 "DirectSoundCreate" );
578 if( OurDirectSoundCreate == NULL )
580 msg_Warn( p_aout, "GetProcAddress FAILED" );
584 /* Create the direct sound object */
585 if FAILED( OurDirectSoundCreate( NULL, &p_aout->output.p_sys->p_dsobject,
588 msg_Warn( p_aout, "cannot create a direct sound device" );
592 /* Set DirectSound Cooperative level, ie what control we want over Windows
593 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
594 * settings of the primary buffer, but also that only the sound of our
595 * application will be hearable when it will have the focus.
596 * !!! (this is not really working as intended yet because to set the
597 * cooperative level you need the window handle of your application, and
598 * I don't know of any easy way to get it. Especially since we might play
599 * sound without any video, and so what window handle should we use ???
600 * The hack for now is to use the Desktop window handle - it seems to be
602 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
606 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
612 p_aout->output.p_sys->p_dsobject = NULL;
613 if( p_aout->output.p_sys->hdsound_dll )
615 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
616 p_aout->output.p_sys->hdsound_dll = NULL;
622 /*****************************************************************************
623 * CreateDSBuffer: Creates a direct sound buffer of the required format.
624 *****************************************************************************
625 * This function creates the buffer we'll use to play audio.
626 * In DirectSound there are two kinds of buffers:
627 * - the primary buffer: which is the actual buffer that the soundcard plays
628 * - the secondary buffer(s): these buffers are the one actually used by
629 * applications and DirectSound takes care of mixing them into the primary.
631 * Once you create a secondary buffer, you cannot change its format anymore so
632 * you have to release the current one and create another.
633 *****************************************************************************/
634 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
635 int i_channels, int i_nb_channels, int i_rate,
636 int i_bytes_per_frame, vlc_bool_t b_probe )
638 WAVEFORMATEXTENSIBLE waveformat;
639 DSBUFFERDESC dsbdesc;
642 /* First set the sound buffer format */
643 waveformat.dwChannelMask = 0;
644 for( i = 0; i < sizeof(pi_channels_in)/sizeof(uint32_t); i++ )
646 if( i_channels & pi_channels_in[i] )
647 waveformat.dwChannelMask |= pi_channels_out[i];
652 case VLC_FOURCC('s','p','d','i'):
654 /* To prevent channel re-ordering */
655 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
656 waveformat.Format.wBitsPerSample = 16;
657 waveformat.Samples.wValidBitsPerSample =
658 waveformat.Format.wBitsPerSample;
659 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
660 waveformat.SubFormat = KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
663 case VLC_FOURCC('f','l','3','2'):
664 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
665 waveformat.Samples.wValidBitsPerSample =
666 waveformat.Format.wBitsPerSample;
667 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
668 waveformat.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
671 case VLC_FOURCC('s','1','6','l'):
672 waveformat.Format.wBitsPerSample = 16;
673 waveformat.Samples.wValidBitsPerSample =
674 waveformat.Format.wBitsPerSample;
675 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
676 waveformat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
680 waveformat.Format.nChannels = i_nb_channels;
681 waveformat.Format.nSamplesPerSec = i_rate;
682 waveformat.Format.nBlockAlign =
683 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
684 waveformat.Format.nAvgBytesPerSec =
685 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
687 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
688 if( i_nb_channels <= 2 )
690 waveformat.Format.cbSize = 0;
694 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
695 waveformat.Format.cbSize =
696 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
700 /* Then fill in the direct sound descriptor */
701 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
702 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
703 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
704 | DSBCAPS_CTRLPOSITIONNOTIFY /* We need notification */
705 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
706 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame; /* buffer size */
707 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
709 if FAILED( IDirectSound_CreateSoundBuffer(
710 p_aout->output.p_sys->p_dsobject, &dsbdesc,
711 &p_aout->output.p_sys->p_dsbuffer, NULL) )
716 /* Stop here if we were just probing */
719 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
720 p_aout->output.p_sys->p_dsbuffer = NULL;
724 /* Backup the size of a frame */
725 p_aout->output.p_sys->p_notif->i_frame_size = i_bytes_per_frame;
727 /* Now the secondary buffer is created, we need to setup its position
729 for( i = 0; i < FRAMES_NUM; i++ )
731 p_aout->output.p_sys->p_notif->p_events[i].dwOffset = i *
732 p_aout->output.p_sys->p_notif->i_frame_size;
734 p_aout->output.p_sys->p_notif->i_frame_status[i] = FRAME_EMPTY;
737 /* Get the IDirectSoundNotify interface */
738 if FAILED( IDirectSoundBuffer_QueryInterface(
739 p_aout->output.p_sys->p_dsbuffer,
740 &IID_IDirectSoundNotify,
741 (LPVOID *)&p_aout->output.p_sys->p_dsnotify ) )
743 msg_Err( p_aout, "cannot get IDirectSoundNotify interface" );
747 if FAILED( IDirectSoundNotify_SetNotificationPositions(
748 p_aout->output.p_sys->p_dsnotify,
750 p_aout->output.p_sys->p_notif->p_events ) )
752 msg_Err( p_aout, "cannot set position notification" );
756 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
757 CheckReordering( p_aout, i_nb_channels );
762 if( p_aout->output.p_sys->p_dsbuffer )
764 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
765 p_aout->output.p_sys->p_dsbuffer = NULL;
767 if( p_aout->output.p_sys->p_dsnotify )
769 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
770 p_aout->output.p_sys->p_dsnotify = NULL;
775 /*****************************************************************************
776 * CreateDSBufferPCM: creates a PCM direct sound buffer.
777 *****************************************************************************
778 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
779 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
780 ****************************************************************************/
781 static int CreateDSBufferPCM( aout_instance_t *p_aout, int *i_format,
782 int i_channels, int i_nb_channels, int i_rate,
785 if( CreateDSBuffer( p_aout, VLC_FOURCC('f','l','3','2'),
786 i_channels, i_nb_channels, i_rate,
787 FRAME_SIZE * 4 * i_nb_channels, b_probe )
790 if ( CreateDSBuffer( p_aout, VLC_FOURCC('s','1','6','l'),
791 i_channels, i_nb_channels, i_rate,
792 FRAME_SIZE * 2 * i_nb_channels, b_probe )
799 *i_format = VLC_FOURCC('s','1','6','l');
805 *i_format = VLC_FOURCC('f','l','3','2');
810 /*****************************************************************************
812 *****************************************************************************
813 * This function destroys the secondary buffer.
814 *****************************************************************************/
815 static void DestroyDSBuffer( aout_instance_t *p_aout )
817 if( p_aout->output.p_sys->p_dsnotify )
819 IDirectSoundNotify_Release( p_aout->output.p_sys->p_dsnotify );
820 p_aout->output.p_sys->p_dsnotify = NULL;
823 if( p_aout->output.p_sys->p_dsbuffer )
825 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
826 p_aout->output.p_sys->p_dsbuffer = NULL;
830 /*****************************************************************************
831 * FillBuffer: Fill in one of the direct sound frame buffers.
832 *****************************************************************************
833 * Returns VLC_SUCCESS on success.
834 *****************************************************************************/
835 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
836 aout_buffer_t *p_buffer )
838 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
839 void *p_write_position, *p_wrap_around;
840 long l_bytes1, l_bytes2;
843 /* Before copying anything, we have to lock the buffer */
844 dsresult = IDirectSoundBuffer_Lock(
845 p_aout->output.p_sys->p_dsbuffer, /* DS buffer */
846 i_frame * p_notif->i_frame_size, /* Start offset */
847 p_notif->i_frame_size, /* Number of bytes */
848 &p_write_position, /* Address of lock start */
849 &l_bytes1, /* Count of bytes locked before wrap around */
850 &p_wrap_around, /* Buffer adress (if wrap around) */
851 &l_bytes2, /* Count of bytes after wrap around */
853 if( dsresult == DSERR_BUFFERLOST )
855 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
856 dsresult = IDirectSoundBuffer_Lock(
857 p_aout->output.p_sys->p_dsbuffer,
858 i_frame * p_notif->i_frame_size,
859 p_notif->i_frame_size,
866 if( dsresult != DS_OK )
868 msg_Warn( p_notif, "cannot lock buffer" );
869 if( p_buffer ) aout_BufferFree( p_buffer );
873 if( p_buffer == NULL )
875 memset( p_write_position, 0, l_bytes1 );
877 else if( p_aout->output.p_sys->b_chan_reorder )
879 /* Do the channel reordering here */
881 if( p_aout->output.output.i_format == VLC_FOURCC('s','1','6','l') )
882 InterleaveS16( (int16_t *)p_buffer->p_buffer,
883 (int16_t *)p_write_position,
884 p_aout->output.p_sys->pi_chan_table,
885 aout_FormatNbChannels( &p_aout->output.output ) );
887 InterleaveFloat32( (float *)p_buffer->p_buffer,
888 (float *)p_write_position,
889 p_aout->output.p_sys->pi_chan_table,
890 aout_FormatNbChannels( &p_aout->output.output ) );
894 p_aout->p_vlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
896 aout_BufferFree( p_buffer );
899 /* Now the data has been copied, unlock the buffer */
900 IDirectSoundBuffer_Unlock( p_aout->output.p_sys->p_dsbuffer,
901 p_write_position, l_bytes1,
902 p_wrap_around, l_bytes2 );
907 /*****************************************************************************
908 * DirectSoundThread: this thread will capture play notification events.
909 *****************************************************************************
910 * We use this thread to emulate a callback mechanism. The thread probes for
911 * event notification and fills up the DS secondary buffer when needed.
912 *****************************************************************************/
913 static void DirectSoundThread( notification_thread_t *p_notif )
915 HANDLE notification_events[FRAMES_NUM];
917 aout_instance_t *p_aout = p_notif->p_aout;
918 int i, i_which_frame, i_last_frame, i_next_frame;
922 for( i = 0; i < FRAMES_NUM; i++ )
923 notification_events[i] = p_notif->p_events[i].hEventNotify;
925 /* We don't want any resampling when using S/PDIF output */
926 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
928 /* Tell the main thread that we are ready */
929 vlc_thread_ready( p_notif );
931 msg_Dbg( p_notif, "DirectSoundThread ready" );
933 /* Wait here until Play() is called */
934 WaitForSingleObject( notification_events[0], INFINITE );
936 if( !p_notif->b_die )
938 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
940 /* start playing the buffer */
941 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
944 DSBPLAY_LOOPING ); /* Flags */
945 if( dsresult == DSERR_BUFFERLOST )
947 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
948 dsresult = IDirectSoundBuffer_Play(
949 p_aout->output.p_sys->p_dsbuffer,
952 DSBPLAY_LOOPING ); /* Flags */
954 if( dsresult != DS_OK )
956 msg_Err( p_aout, "cannot start playing buffer" );
960 while( !p_notif->b_die )
962 aout_buffer_t *p_buffer;
965 /* wait for the position notification */
966 i_which_frame = WaitForMultipleObjects( FRAMES_NUM,
967 notification_events, 0,
968 INFINITE ) - WAIT_OBJECT_0;
975 /* We take into account the current latency */
976 if SUCCEEDED( IDirectSoundBuffer_GetCurrentPosition(
977 p_aout->output.p_sys->p_dsbuffer,
980 if( l_latency > (i_which_frame * FRAME_SIZE)
981 && l_latency < ((i_which_frame+1) * FRAME_SIZE) )
983 l_latency = - ( l_latency /
984 p_aout->output.output.i_bytes_per_frame %
989 l_latency = FRAME_SIZE - ( l_latency /
990 p_aout->output.output.i_bytes_per_frame %
999 /* Mark last frame as empty */
1000 i_last_frame = (i_which_frame + FRAMES_NUM -1) % FRAMES_NUM;
1001 i_next_frame = (i_which_frame + 1) % FRAMES_NUM;
1002 p_notif->i_frame_status[i_last_frame] = FRAME_EMPTY;
1004 /* Try to fill in as many frame buffers as possible */
1005 for( i = i_next_frame; (i % FRAMES_NUM) != i_which_frame; i++ )
1008 /* Check if frame buf is already filled */
1009 if( p_notif->i_frame_status[i % FRAMES_NUM] == FRAME_QUEUED )
1012 p_buffer = aout_OutputNextBuffer( p_aout,
1013 mtime + 1000000 / p_aout->output.output.i_rate *
1014 ((i - i_next_frame + 1) * FRAME_SIZE + l_latency), b_sleek );
1016 /* If there is no audio data available and we have some buffered
1017 * already, then just wait for the next time */
1018 if( !p_buffer && (i != i_next_frame) )
1020 //msg_Err( p_aout, "only %i frame buffers filled!",
1021 // i - i_next_frame );
1025 if( FillBuffer( p_aout, (i%FRAMES_NUM), p_buffer )
1029 /* Mark the frame buffer as QUEUED */
1030 p_notif->i_frame_status[i%FRAMES_NUM] = FRAME_QUEUED;
1035 /* make sure the buffer isn't playing */
1036 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
1038 /* free the events */
1039 for( i = 0; i < FRAMES_NUM; i++ )
1040 CloseHandle( notification_events[i] );
1042 msg_Dbg( p_notif, "DirectSoundThread exiting" );
1045 /*****************************************************************************
1046 * CheckReordering: Check if we need to do some channel re-ordering (the ac3
1047 * channel order is different from the one chosen by
1049 *****************************************************************************/
1050 static void CheckReordering( aout_instance_t *p_aout, int i_nb_channels )
1054 #define i_channel_mask p_aout->output.p_sys->i_channel_mask
1055 #define pi_chan_table p_aout->output.p_sys->pi_chan_table
1057 p_aout->output.p_sys->b_chan_reorder = VLC_FALSE;
1059 pi_chan_table = malloc( i_nb_channels * sizeof(int) );
1060 if( !pi_chan_table )
1066 i < (int)(sizeof(pi_channels_out)/sizeof(uint32_t)); i++ )
1068 if( i_channel_mask & pi_channels_out[i] )
1071 pi_channels_out[i] != pi_channels_ordered[k]; k++ )
1073 if( i_channel_mask & pi_channels_ordered[k] )
1079 pi_chan_table[j] = l;
1085 for( i = 0; i < i_nb_channels; i++ )
1087 if( pi_chan_table[i] != i )
1089 p_aout->output.p_sys->b_chan_reorder = VLC_TRUE;
1093 if( p_aout->output.p_sys->b_chan_reorder )
1095 msg_Dbg( p_aout, "channel reordering needed" );
1098 #undef pi_chan_table
1102 /*****************************************************************************
1103 * InterleaveFloat32/S16: change the channel order to the Microsoft one.
1104 *****************************************************************************/
1105 static void InterleaveFloat32( float *p_buf, float *p_buf_out,
1106 int *pi_chan_table, int i_nb_channels )
1110 for( i = 0; i < FRAME_SIZE; i++ )
1112 for( j = 0; j < i_nb_channels; j++ )
1114 p_buf_out[i*i_nb_channels + pi_chan_table[j]] =
1115 p_buf[i*i_nb_channels + j];
1120 static void InterleaveS16( int16_t *p_buf, int16_t *p_buf_out,
1121 int *pi_chan_table, int i_nb_channels )
1125 for( i = 0; i < FRAME_SIZE; i++ )
1127 for( j = 0; j < i_nb_channels; j++ )
1129 p_buf_out[ i*i_nb_channels + pi_chan_table[j]] =
1130 p_buf[i*i_nb_channels + j];