1 /*****************************************************************************
2 * directsound.c: DirectSound audio output plugin for VLC
3 *****************************************************************************
4 * Copyright (C) 2001-2009 VLC authors and VideoLAN
5 * $Id: 048d107bb17e9c67fca91f4a8e321a6ea0a8b6e6 $
6 *
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
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 Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation, Inc.,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
23
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <assert.h>
33 #include <math.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_aout.h>
38 #include <vlc_charset.h>
39 #include <vlc_memory.h>
40
41 #include "audio_output/windows_audio_common.h"
42 #include "audio_output/mmdevice.h"
43 #include <mmdeviceapi.h>
44
45 #define DS_BUF_SIZE (6*1024*1024)
46
47 static int Open( vlc_object_t * );
48 static void Close( vlc_object_t * );
49 static HRESULT StreamStart( aout_stream_t *, audio_sample_format_t *,
50 const GUID * );
51 static HRESULT StreamStop( aout_stream_t * );
52 static int ReloadDirectXDevices( vlc_object_t *, const char *,
53 char ***, char *** );
54 static void * PlayedDataEraser( void * );
55 /* Speaker setup override options list */
56 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
57 "Quad", "5.1", "7.1" };
58
59 /*****************************************************************************
60 * Module descriptor
61 *****************************************************************************/
62 #define DEVICE_TEXT N_("Output device")
63 #define DEVICE_LONGTEXT N_("Select your audio output device")
64
65 #define SPEAKER_TEXT N_("Speaker configuration")
66 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
67 "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
68
69 #define VOLUME_TEXT N_("Audio volume")
70 #define VOLUME_LONGTEXT N_("Audio volume in hundredths of decibels (dB).")
71
72 vlc_module_begin ()
73 set_description( N_("DirectX audio output") )
74 set_shortname( "DirectX" )
75 set_capability( "audio output", 100 )
76 set_category( CAT_AUDIO )
77 set_subcategory( SUBCAT_AUDIO_AOUT )
78 add_shortcut( "directx", "aout_directx", "directsound", "dsound" )
79
80 add_string( "directx-audio-device", NULL,
81 DEVICE_TEXT, DEVICE_LONGTEXT, false )
82 change_string_cb( ReloadDirectXDevices )
83 add_obsolete_string( "directx-audio-device-name")
84 add_bool( "directx-audio-float32", true, FLOAT_TEXT,
85 FLOAT_LONGTEXT, true )
86 add_string( "directx-audio-speaker", "Windows default",
87 SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
88 change_string_list( speaker_list, speaker_list )
89 add_float( "directx-volume", 1.0f,
90 VOLUME_TEXT, VOLUME_LONGTEXT, true )
91 change_float_range( 0.f, 2.f )
92
93 set_callbacks( Open, Close )
94
95 add_submodule()
96 set_capability( "aout stream", 30 )
97 set_callbacks( StreamStart, StreamStop )
98 vlc_module_end ()
99
100 typedef struct aout_stream_sys
101 {
102 LPDIRECTSOUND p_dsobject; /*< main Direct Sound object */
103 LPDIRECTSOUNDBUFFER p_dsbuffer; /*< the sound buffer we use (direct sound
104 takes care of mixing all the secondary
105 buffers into the primary) */
106 LPDIRECTSOUNDNOTIFY p_notify;
107
108 int i_bytes_per_sample; /*< Size in bytes of one frame */
109 int i_rate; /*< Sample rate */
110
111 uint8_t chans_to_reorder; /*< Do we need channel reordering? */
112 uint8_t chan_table[AOUT_CHAN_MAX];
113 uint32_t i_channel_mask;
114 vlc_fourcc_t format;
115
116 size_t i_write;
117 size_t i_last_read;
118 int64_t i_data;
119
120 bool b_playing;
121 vlc_mutex_t lock;
122 vlc_cond_t cond;
123 vlc_thread_t eraser_thread;
124 } aout_stream_sys_t;
125
126 /**
127 * DirectSound audio output method descriptor
128 *
129 * This structure is part of the audio output thread descriptor.
130 * It describes the direct sound specific properties of an audio device.
131 */
132 struct aout_sys_t
133 {
134 aout_stream_sys_t s;
135 struct
136 {
137 float volume;
138 LONG mb;
139 bool mute;
140 } volume;
141 };
142
143 static HRESULT Flush( aout_stream_sys_t *sys );
TimeGet(aout_stream_sys_t * sys,mtime_t * delay)144 static HRESULT TimeGet( aout_stream_sys_t *sys, mtime_t *delay )
145 {
146 DWORD read, status;
147 HRESULT hr;
148 mtime_t size;
149
150 hr = IDirectSoundBuffer_GetStatus( sys->p_dsbuffer, &status );
151 if( hr != DS_OK )
152 return hr;
153 if( !(status & DSBSTATUS_PLAYING) )
154 return DSERR_INVALIDCALL ;
155
156 hr = IDirectSoundBuffer_GetCurrentPosition( sys->p_dsbuffer, &read, NULL );
157 if( hr != DS_OK )
158 return hr;
159
160 size = (mtime_t)read - sys->i_last_read;
161
162 /* GetCurrentPosition cannot be trusted if the return doesn't change
163 * Just return an error */
164 if( size == 0 )
165 return DSERR_GENERIC ;
166 else if( size < 0 )
167 size += DS_BUF_SIZE;
168
169 sys->i_data -= size;
170 sys->i_last_read = read;
171
172 if( sys->i_data < 0 )
173 /* underrun */
174 Flush(sys);
175
176 *delay = ( sys->i_data / sys->i_bytes_per_sample ) * CLOCK_FREQ / sys->i_rate;
177
178 return DS_OK;
179 }
180
StreamTimeGet(aout_stream_t * s,mtime_t * delay)181 static HRESULT StreamTimeGet( aout_stream_t *s, mtime_t *delay )
182 {
183 return TimeGet( s->sys, delay );
184 }
185
OutputTimeGet(audio_output_t * aout,mtime_t * delay)186 static int OutputTimeGet( audio_output_t *aout, mtime_t *delay )
187 {
188 return (TimeGet( &aout->sys->s, delay ) == DS_OK) ? 0 : -1;
189 }
190
191 /**
192 * Fills in one of the DirectSound frame buffers.
193 *
194 * @return VLC_SUCCESS on success.
195 */
FillBuffer(vlc_object_t * obj,aout_stream_sys_t * p_sys,block_t * p_buffer)196 static HRESULT FillBuffer( vlc_object_t *obj, aout_stream_sys_t *p_sys,
197 block_t *p_buffer )
198 {
199 size_t towrite = (p_buffer != NULL) ? p_buffer->i_buffer : DS_BUF_SIZE;
200 void *p_write_position, *p_wrap_around;
201 unsigned long l_bytes1, l_bytes2;
202 HRESULT dsresult;
203
204 vlc_mutex_lock( &p_sys->lock );
205
206 /* Before copying anything, we have to lock the buffer */
207 dsresult = IDirectSoundBuffer_Lock(
208 p_sys->p_dsbuffer, /* DS buffer */
209 p_sys->i_write, /* Start offset */
210 towrite, /* Number of bytes */
211 &p_write_position, /* Address of lock start */
212 &l_bytes1, /* Count of bytes locked before wrap around */
213 &p_wrap_around, /* Buffer address (if wrap around) */
214 &l_bytes2, /* Count of bytes after wrap around */
215 0 ); /* Flags: DSBLOCK_FROMWRITECURSOR is buggy */
216 if( dsresult == DSERR_BUFFERLOST )
217 {
218 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
219 dsresult = IDirectSoundBuffer_Lock(
220 p_sys->p_dsbuffer,
221 p_sys->i_write,
222 towrite,
223 &p_write_position,
224 &l_bytes1,
225 &p_wrap_around,
226 &l_bytes2,
227 0 );
228 }
229 if( dsresult != DS_OK )
230 {
231 msg_Warn( obj, "cannot lock buffer" );
232 if( p_buffer != NULL )
233 block_Release( p_buffer );
234 vlc_mutex_unlock( &p_sys->lock );
235 return dsresult;
236 }
237
238 if( p_buffer == NULL )
239 {
240 memset( p_write_position, 0, l_bytes1 );
241 memset( p_wrap_around, 0, l_bytes2 );
242 }
243 else
244 {
245 if( p_sys->chans_to_reorder ) /* Do the channel reordering here */
246 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
247 p_sys->chans_to_reorder, p_sys->chan_table,
248 p_sys->format );
249
250 memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
251 if( p_wrap_around && l_bytes2 )
252 memcpy( p_wrap_around, p_buffer->p_buffer + l_bytes1, l_bytes2 );
253
254 if( unlikely( ( l_bytes1 + l_bytes2 ) < p_buffer->i_buffer ) )
255 msg_Err( obj, "Buffer overrun");
256
257 block_Release( p_buffer );
258 }
259
260 /* Now the data has been copied, unlock the buffer */
261 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
262 p_wrap_around, l_bytes2 );
263
264 p_sys->i_write += towrite;
265 p_sys->i_write %= DS_BUF_SIZE;
266 p_sys->i_data += towrite;
267 vlc_mutex_unlock( &p_sys->lock );
268
269 return DS_OK;
270 }
271
Play(vlc_object_t * obj,aout_stream_sys_t * sys,block_t * p_buffer)272 static HRESULT Play( vlc_object_t *obj, aout_stream_sys_t *sys,
273 block_t *p_buffer )
274 {
275 HRESULT dsresult;
276 dsresult = FillBuffer( obj, sys, p_buffer );
277 if( dsresult != DS_OK )
278 return dsresult;
279
280 /* start playing the buffer */
281 dsresult = IDirectSoundBuffer_Play( sys->p_dsbuffer, 0, 0,
282 DSBPLAY_LOOPING );
283 if( dsresult == DSERR_BUFFERLOST )
284 {
285 IDirectSoundBuffer_Restore( sys->p_dsbuffer );
286 dsresult = IDirectSoundBuffer_Play( sys->p_dsbuffer,
287 0, 0, DSBPLAY_LOOPING );
288 }
289 if( dsresult != DS_OK )
290 msg_Err( obj, "cannot start playing buffer: (hr=0x%0lx)", dsresult );
291 else
292 {
293 vlc_mutex_lock( &sys->lock );
294 sys->b_playing = true;
295 vlc_cond_signal(&sys->cond);
296 vlc_mutex_unlock( &sys->lock );
297
298 }
299 return dsresult;
300 }
301
StreamPlay(aout_stream_t * s,block_t * block)302 static HRESULT StreamPlay( aout_stream_t *s, block_t *block )
303 {
304 return Play( VLC_OBJECT(s), s->sys, block );
305 }
306
OutputPlay(audio_output_t * aout,block_t * block)307 static void OutputPlay( audio_output_t *aout, block_t *block )
308 {
309 Play( VLC_OBJECT(aout), &aout->sys->s, block );
310 }
311
Pause(aout_stream_sys_t * sys,bool pause)312 static HRESULT Pause( aout_stream_sys_t *sys, bool pause )
313 {
314 HRESULT hr;
315
316 if( pause )
317 hr = IDirectSoundBuffer_Stop( sys->p_dsbuffer );
318 else
319 hr = IDirectSoundBuffer_Play( sys->p_dsbuffer, 0, 0, DSBPLAY_LOOPING );
320 if( hr == DS_OK )
321 {
322 vlc_mutex_lock( &sys->lock );
323 sys->b_playing = !pause;
324 if( sys->b_playing )
325 vlc_cond_signal( &sys->cond );
326 vlc_mutex_unlock( &sys->lock );
327 }
328 return hr;
329 }
330
StreamPause(aout_stream_t * s,bool pause)331 static HRESULT StreamPause( aout_stream_t *s, bool pause )
332 {
333 return Pause( s->sys, pause );
334 }
335
OutputPause(audio_output_t * aout,bool pause,mtime_t date)336 static void OutputPause( audio_output_t *aout, bool pause, mtime_t date )
337 {
338 Pause( &aout->sys->s, pause );
339 (void) date;
340 }
341
Flush(aout_stream_sys_t * sys)342 static HRESULT Flush( aout_stream_sys_t *sys )
343 {
344 HRESULT ret = IDirectSoundBuffer_Stop( sys->p_dsbuffer );
345 if( ret == DS_OK )
346 {
347 vlc_mutex_lock(&sys->lock);
348 sys->i_data = 0;
349 sys->i_last_read = sys->i_write;
350 IDirectSoundBuffer_SetCurrentPosition( sys->p_dsbuffer, sys->i_write);
351 sys->b_playing = false;
352 vlc_mutex_unlock(&sys->lock);
353 }
354 return ret;
355 }
356
StreamFlush(aout_stream_t * s)357 static HRESULT StreamFlush( aout_stream_t *s )
358 {
359 return Flush( s->sys );
360 }
361
OutputFlush(audio_output_t * aout,bool drain)362 static void OutputFlush( audio_output_t *aout, bool drain )
363 {
364 aout_sys_t *sys = aout->sys;
365 if (drain)
366 { /* Loosy drain emulation */
367 mtime_t delay;
368
369 if (OutputTimeGet(aout, &delay) == 0 && delay <= INT64_C(5000000))
370 Sleep((delay / (CLOCK_FREQ / 1000)) + 1);
371 }
372 else
373 Flush( &sys->s );
374 }
375
376 /**
377 * Creates a DirectSound buffer of the required format.
378 *
379 * This function creates the buffer we'll use to play audio.
380 * In DirectSound there are two kinds of buffers:
381 * - the primary buffer: which is the actual buffer that the soundcard plays
382 * - the secondary buffer(s): these buffers are the one actually used by
383 * applications and DirectSound takes care of mixing them into the primary.
384 *
385 * Once you create a secondary buffer, you cannot change its format anymore so
386 * you have to release the current one and create another.
387 */
CreateDSBuffer(vlc_object_t * obj,aout_stream_sys_t * sys,int i_format,int i_channels,int i_nb_channels,int i_rate,bool b_probe)388 static HRESULT CreateDSBuffer( vlc_object_t *obj, aout_stream_sys_t *sys,
389 int i_format, int i_channels, int i_nb_channels,
390 int i_rate, bool b_probe )
391 {
392 WAVEFORMATEXTENSIBLE waveformat;
393 DSBUFFERDESC dsbdesc;
394 HRESULT hr;
395
396 /* First set the sound buffer format */
397 waveformat.dwChannelMask = 0;
398 for( unsigned i = 0; pi_vlc_chan_order_wg4[i]; i++ )
399 if( i_channels & pi_vlc_chan_order_wg4[i] )
400 waveformat.dwChannelMask |= pi_channels_in[i];
401
402 switch( i_format )
403 {
404 case VLC_CODEC_SPDIFL:
405 i_nb_channels = 2;
406 /* To prevent channel re-ordering */
407 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
408 waveformat.Format.wBitsPerSample = 16;
409 waveformat.Samples.wValidBitsPerSample =
410 waveformat.Format.wBitsPerSample;
411 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
412 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
413 break;
414
415 case VLC_CODEC_FL32:
416 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
417 waveformat.Samples.wValidBitsPerSample =
418 waveformat.Format.wBitsPerSample;
419 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
420 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
421 break;
422
423 case VLC_CODEC_S16N:
424 waveformat.Format.wBitsPerSample = 16;
425 waveformat.Samples.wValidBitsPerSample =
426 waveformat.Format.wBitsPerSample;
427 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
428 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
429 break;
430 }
431
432 waveformat.Format.nChannels = i_nb_channels;
433 waveformat.Format.nSamplesPerSec = i_rate;
434 waveformat.Format.nBlockAlign =
435 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
436 waveformat.Format.nAvgBytesPerSec =
437 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
438
439 sys->i_bytes_per_sample = waveformat.Format.nBlockAlign;
440 sys->format = i_format;
441
442 /* Then fill in the direct sound descriptor */
443 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
444 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
445 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 /* Better position accuracy */
446 | DSBCAPS_GLOBALFOCUS /* Allows background playing */
447 | DSBCAPS_CTRLVOLUME /* Allows volume control */
448 | DSBCAPS_CTRLPOSITIONNOTIFY; /* Allow position notifications */
449
450 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
451 if( i_nb_channels <= 2 )
452 {
453 waveformat.Format.cbSize = 0;
454 }
455 else
456 {
457 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
458 waveformat.Format.cbSize =
459 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
460
461 /* Needed for 5.1 on emu101k */
462 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
463 }
464
465 dsbdesc.dwBufferBytes = DS_BUF_SIZE; /* buffer size */
466 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
467
468 /* CreateSoundBuffer doesn't allow volume control for non-PCM buffers */
469 if ( i_format == VLC_CODEC_SPDIFL )
470 dsbdesc.dwFlags &= ~DSBCAPS_CTRLVOLUME;
471
472 hr = IDirectSound_CreateSoundBuffer( sys->p_dsobject, &dsbdesc,
473 &sys->p_dsbuffer, NULL );
474 if( FAILED(hr) )
475 {
476 if( !(dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE) )
477 return hr;
478
479 /* Try without DSBCAPS_LOCHARDWARE */
480 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
481 hr = IDirectSound_CreateSoundBuffer( sys->p_dsobject, &dsbdesc,
482 &sys->p_dsbuffer, NULL );
483 if( FAILED(hr) )
484 return hr;
485 if( !b_probe )
486 msg_Dbg( obj, "couldn't use hardware sound buffer" );
487 }
488
489 /* Stop here if we were just probing */
490 if( b_probe )
491 {
492 IDirectSoundBuffer_Release( sys->p_dsbuffer );
493 sys->p_dsbuffer = NULL;
494 return DS_OK;
495 }
496
497 sys->i_rate = i_rate;
498 sys->i_channel_mask = waveformat.dwChannelMask;
499 sys->chans_to_reorder =
500 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
501 waveformat.dwChannelMask, sys->chan_table );
502 if( sys->chans_to_reorder )
503 msg_Dbg( obj, "channel reordering needed" );
504
505 hr = IDirectSoundBuffer_QueryInterface( sys->p_dsbuffer,
506 &IID_IDirectSoundNotify,
507 (void **) &sys->p_notify );
508 if( hr != DS_OK )
509 {
510 msg_Err( obj, "Couldn't query IDirectSoundNotify" );
511 sys->p_notify = NULL;
512 }
513
514 FillBuffer( obj, sys, NULL );
515 return DS_OK;
516 }
517
518 /**
519 * Creates a PCM DirectSound buffer.
520 *
521 * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
522 * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
523 */
CreateDSBufferPCM(vlc_object_t * obj,aout_stream_sys_t * sys,vlc_fourcc_t * i_format,int i_channels,int i_rate,bool b_probe)524 static HRESULT CreateDSBufferPCM( vlc_object_t *obj, aout_stream_sys_t *sys,
525 vlc_fourcc_t *i_format, int i_channels,
526 int i_rate, bool b_probe )
527 {
528 HRESULT hr;
529 unsigned i_nb_channels = popcount( i_channels );
530
531 if( var_GetBool( obj, "directx-audio-float32" ) )
532 {
533 hr = CreateDSBuffer( obj, sys, VLC_CODEC_FL32, i_channels,
534 i_nb_channels, i_rate, b_probe );
535 if( hr == DS_OK )
536 {
537 *i_format = VLC_CODEC_FL32;
538 return DS_OK;
539 }
540 }
541
542 hr = CreateDSBuffer( obj, sys, VLC_CODEC_S16N, i_channels, i_nb_channels,
543 i_rate, b_probe );
544 if( hr == DS_OK )
545 {
546 *i_format = VLC_CODEC_S16N;
547 return DS_OK;
548 }
549
550 return hr;
551 }
552
553 /**
554 * Closes the audio device.
555 */
Stop(aout_stream_sys_t * p_sys)556 static HRESULT Stop( aout_stream_sys_t *p_sys )
557 {
558 vlc_mutex_lock( &p_sys->lock );
559 p_sys->b_playing = true;
560 vlc_cond_signal( &p_sys->cond );
561 vlc_mutex_unlock( &p_sys->lock );
562 vlc_cancel( p_sys->eraser_thread );
563 vlc_join( p_sys->eraser_thread, NULL );
564 vlc_cond_destroy( &p_sys->cond );
565 vlc_mutex_destroy( &p_sys->lock );
566
567 if( p_sys->p_notify != NULL )
568 {
569 IDirectSoundNotify_Release(p_sys->p_notify );
570 p_sys->p_notify = NULL;
571 }
572 if( p_sys->p_dsbuffer != NULL )
573 {
574 IDirectSoundBuffer_Stop( p_sys->p_dsbuffer );
575 IDirectSoundBuffer_Release( p_sys->p_dsbuffer );
576 p_sys->p_dsbuffer = NULL;
577 }
578 if( p_sys->p_dsobject != NULL )
579 {
580 IDirectSound_Release( p_sys->p_dsobject );
581 p_sys->p_dsobject = NULL;
582 }
583 return DS_OK;
584 }
585
StreamStop(aout_stream_t * s)586 static HRESULT StreamStop( aout_stream_t *s )
587 {
588 HRESULT hr;
589
590 hr = Stop( s->sys );
591 free( s->sys );
592 return hr;
593 }
594
OutputStop(audio_output_t * aout)595 static void OutputStop( audio_output_t *aout )
596 {
597 msg_Dbg( aout, "closing audio device" );
598 Stop( &aout->sys->s );
599 }
600
Start(vlc_object_t * obj,aout_stream_sys_t * sys,audio_sample_format_t * restrict pfmt)601 static HRESULT Start( vlc_object_t *obj, aout_stream_sys_t *sys,
602 audio_sample_format_t *restrict pfmt )
603 {
604 if( aout_FormatNbChannels( pfmt ) == 0 )
605 return E_FAIL;
606
607 #if !VLC_WINSTORE_APP
608 /* Set DirectSound Cooperative level, ie what control we want over Windows
609 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
610 * settings of the primary buffer, but also that only the sound of our
611 * application will be hearable when it will have the focus.
612 * !!! (this is not really working as intended yet because to set the
613 * cooperative level you need the window handle of your application, and
614 * I don't know of any easy way to get it. Especially since we might play
615 * sound without any video, and so what window handle should we use ???
616 * The hack for now is to use the Desktop window handle - it seems to be
617 * working */
618 if( IDirectSound_SetCooperativeLevel( sys->p_dsobject, GetDesktopWindow(),
619 DSSCL_EXCLUSIVE) )
620 msg_Warn( obj, "cannot set direct sound cooperative level" );
621 #endif
622
623 if( AOUT_FMT_HDMI( pfmt ) )
624 return E_FAIL;
625
626 audio_sample_format_t fmt = *pfmt;
627 const char *const *ppsz_compare = speaker_list;
628 char *psz_speaker;
629 int i = 0;
630 HRESULT hr = DSERR_UNSUPPORTED;
631
632 /* Retrieve config values */
633 var_Create( obj, "directx-audio-float32",
634 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
635 psz_speaker = var_CreateGetString( obj, "directx-audio-speaker" );
636
637 while ( *ppsz_compare != NULL )
638 {
639 if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
640 {
641 break;
642 }
643 ppsz_compare++; i++;
644 }
645
646 if ( *ppsz_compare == NULL )
647 {
648 msg_Err( obj, "(%s) isn't valid speaker setup option", psz_speaker );
649 msg_Err( obj, "Defaulting to Windows default speaker config");
650 i = 0;
651 }
652 free( psz_speaker );
653
654 vlc_mutex_init(&sys->lock);
655 vlc_cond_init(&sys->cond);
656
657 if( AOUT_FMT_SPDIF( &fmt ) )
658 {
659 if( var_InheritBool( obj, "spdif" ) )
660 hr = CreateDSBuffer( obj, sys, VLC_CODEC_SPDIFL,
661 fmt.i_physical_channels,
662 aout_FormatNbChannels(&fmt), fmt.i_rate, false );
663
664 if( hr == DS_OK )
665 {
666 msg_Dbg( obj, "using A/52 pass-through over S/PDIF" );
667 fmt.i_format = VLC_CODEC_SPDIFL;
668
669 /* Calculate the frame size in bytes */
670 fmt.i_bytes_per_frame = AOUT_SPDIF_SIZE;
671 fmt.i_frame_length = A52_FRAME_NB;
672 }
673 else
674 {
675 vlc_mutex_destroy(&sys->lock);
676 vlc_cond_destroy(&sys->cond);
677 return E_FAIL;
678 }
679 }
680
681 if( hr != DS_OK )
682 {
683 if( i == 0 )
684 {
685 DWORD ui_speaker_config;
686 int i_channels = 2; /* Default to stereo */
687 int i_orig_channels = aout_FormatNbChannels( &fmt );
688
689 /* Check the speaker configuration to determine which channel
690 * config should be the default */
691 hr = IDirectSound_GetSpeakerConfig( sys->p_dsobject,
692 &ui_speaker_config );
693 if( FAILED(hr) )
694 {
695 ui_speaker_config = DSSPEAKER_STEREO;
696 msg_Dbg( obj, "GetSpeakerConfig failed" );
697 }
698
699 const char *name = "Unknown";
700 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
701 {
702 case DSSPEAKER_7POINT1:
703 case DSSPEAKER_7POINT1_SURROUND:
704 name = "7.1";
705 i_channels = 8;
706 break;
707 case DSSPEAKER_5POINT1:
708 case DSSPEAKER_5POINT1_SURROUND:
709 name = "5.1";
710 i_channels = 6;
711 break;
712 case DSSPEAKER_QUAD:
713 name = "Quad";
714 i_channels = 4;
715 break;
716 #if 0 /* Lots of people just get their settings wrong and complain that
717 * this is a problem with VLC so just don't ever set mono by default. */
718 case DSSPEAKER_MONO:
719 name = "Mono";
720 i_channels = 1;
721 break;
722 #endif
723 case DSSPEAKER_SURROUND:
724 name = "Surround";
725 i_channels = 4;
726 break;
727 case DSSPEAKER_STEREO:
728 name = "Stereo";
729 i_channels = 2;
730 break;
731 }
732
733 if( i_channels >= i_orig_channels )
734 i_channels = i_orig_channels;
735
736 msg_Dbg( obj, "%s speaker config: %s and stream has "
737 "%d channels, using %d channels", "Windows", name,
738 i_orig_channels, i_channels );
739
740 switch( i_channels )
741 {
742 case 8:
743 fmt.i_physical_channels = AOUT_CHANS_7_1;
744 break;
745 case 7:
746 case 6:
747 fmt.i_physical_channels = AOUT_CHANS_5_1;
748 break;
749 case 5:
750 case 4:
751 fmt.i_physical_channels = AOUT_CHANS_4_0;
752 break;
753 default:
754 fmt.i_physical_channels = AOUT_CHANS_2_0;
755 break;
756 }
757 }
758 else
759 { /* Overriden speaker configuration */
760 const char *name = "Non-existant";
761 switch( i )
762 {
763 case 1: /* Mono */
764 name = "Mono";
765 fmt.i_physical_channels = AOUT_CHAN_CENTER;
766 break;
767 case 2: /* Stereo */
768 name = "Stereo";
769 fmt.i_physical_channels = AOUT_CHANS_2_0;
770 break;
771 case 3: /* Quad */
772 name = "Quad";
773 fmt.i_physical_channels = AOUT_CHANS_4_0;
774 break;
775 case 4: /* 5.1 */
776 name = "5.1";
777 fmt.i_physical_channels = AOUT_CHANS_5_1;
778 break;
779 case 5: /* 7.1 */
780 name = "7.1";
781 fmt.i_physical_channels = AOUT_CHANS_7_1;
782 break;
783 }
784 msg_Dbg( obj, "%s speaker config: %s", "VLC", name );
785 }
786
787 /* Open the device */
788 aout_FormatPrepare( &fmt );
789
790 hr = CreateDSBufferPCM( obj, sys, &fmt.i_format,
791 fmt.i_physical_channels, fmt.i_rate, false );
792 if( hr != DS_OK )
793 {
794 msg_Err( obj, "cannot open directx audio device" );
795 goto error;
796 }
797 }
798
799 int ret = vlc_clone(&sys->eraser_thread, PlayedDataEraser, (void*) obj,
800 VLC_THREAD_PRIORITY_LOW);
801 if( unlikely( ret ) )
802 {
803 if( ret != ENOMEM )
804 msg_Err( obj, "Couldn't start eraser thread" );
805
806 hr = E_FAIL;
807 goto error;
808 }
809
810 fmt.channel_type = AUDIO_CHANNEL_TYPE_BITMAP;
811
812 *pfmt = fmt;
813 sys->b_playing = false;
814 sys->i_write = 0;
815 sys->i_last_read = 0;
816 sys->i_data = 0;
817
818 return DS_OK;
819
820 error:
821 vlc_cond_destroy(&sys->cond);
822 vlc_mutex_destroy(&sys->lock);
823
824 if( sys->p_notify != NULL )
825 {
826 IDirectSoundNotify_Release( sys->p_notify );
827 sys->p_notify = NULL;
828 }
829 if( sys->p_dsbuffer != NULL )
830 {
831 IDirectSoundBuffer_Release( sys->p_dsbuffer );
832 sys->p_dsbuffer = NULL;
833 }
834 IDirectSound_Release( sys->p_dsobject );
835 sys->p_dsobject = NULL;
836 return hr;
837 }
838
StreamStart(aout_stream_t * s,audio_sample_format_t * restrict fmt,const GUID * sid)839 static HRESULT StreamStart( aout_stream_t *s,
840 audio_sample_format_t *restrict fmt,
841 const GUID *sid )
842 {
843 aout_stream_sys_t *sys = calloc( 1, sizeof( *sys ) );
844 if( unlikely(sys == NULL) )
845 return E_OUTOFMEMORY;
846
847 void *pv;
848 HRESULT hr;
849 if( sid )
850 {
851 DIRECTX_AUDIO_ACTIVATION_PARAMS params = {
852 .cbDirectXAudioActivationParams = sizeof( params ),
853 .guidAudioSession = *sid,
854 .dwAudioStreamFlags = 0,
855 };
856 PROPVARIANT prop;
857
858 PropVariantInit( &prop );
859 prop.vt = VT_BLOB;
860 prop.blob.cbSize = sizeof( params );
861 prop.blob.pBlobData = (BYTE *)¶ms;
862
863 hr = aout_stream_Activate( s, &IID_IDirectSound, &prop, &pv );
864 }
865 else
866 hr = aout_stream_Activate( s, &IID_IDirectSound, NULL, &pv );
867 if( FAILED(hr) )
868 goto error;
869
870 sys->p_dsobject = pv;
871
872 hr = Start( VLC_OBJECT(s), sys, fmt );
873 if( FAILED(hr) )
874 goto error;
875
876 s->sys = sys;
877 s->time_get = StreamTimeGet;
878 s->play = StreamPlay;
879 s->pause = StreamPause;
880 s->flush = StreamFlush;
881 return S_OK;
882 error:
883 free( sys );
884 return hr;
885 }
886
887 /**
888 * Handles all the gory details of DirectSound initialization.
889 */
InitDirectSound(audio_output_t * p_aout)890 static int InitDirectSound( audio_output_t *p_aout )
891 {
892 aout_sys_t *sys = p_aout->sys;
893 GUID guid, *p_guid = NULL;
894
895 char *dev = var_GetNonEmptyString( p_aout, "directx-audio-device" );
896 if( dev != NULL )
897 {
898 LPOLESTR lpsz = ToWide( dev );
899 free( dev );
900
901 if( SUCCEEDED( IIDFromString( lpsz, &guid ) ) )
902 p_guid = &guid;
903 else
904 msg_Err( p_aout, "bad device GUID: %ls", lpsz );
905 free( lpsz );
906 }
907
908 /* Create the direct sound object */
909 if FAILED( DirectSoundCreate( p_guid, &sys->s.p_dsobject, NULL ) )
910 {
911 msg_Warn( p_aout, "cannot create a direct sound device" );
912 goto error;
913 }
914
915 return VLC_SUCCESS;
916
917 error:
918 sys->s.p_dsobject = NULL;
919 return VLC_EGENERIC;
920
921 }
922
VolumeSet(audio_output_t * p_aout,float volume)923 static int VolumeSet( audio_output_t *p_aout, float volume )
924 {
925 aout_sys_t *sys = p_aout->sys;
926 int ret = 0;
927
928 /* Directsound doesn't support amplification, so we use software
929 gain if we need it and only for this */
930 float gain = volume > 1.f ? volume * volume * volume : 1.f;
931 aout_GainRequest( p_aout, gain );
932
933 /* millibels from linear amplification */
934 LONG mb = lroundf( 6000.f * log10f( __MIN( volume, 1.f ) ));
935
936 /* Clamp to allowed DirectSound range */
937 static_assert( DSBVOLUME_MIN < DSBVOLUME_MAX, "DSBVOLUME_* confused" );
938 if( mb > DSBVOLUME_MAX )
939 {
940 mb = DSBVOLUME_MAX;
941 ret = -1;
942 }
943 if( mb <= DSBVOLUME_MIN )
944 mb = DSBVOLUME_MIN;
945
946 sys->volume.mb = mb;
947 sys->volume.volume = volume;
948 if( !sys->volume.mute && sys->s.p_dsbuffer != NULL &&
949 IDirectSoundBuffer_SetVolume( sys->s.p_dsbuffer, mb ) != DS_OK )
950 return -1;
951 /* Convert back to UI volume */
952 aout_VolumeReport( p_aout, volume );
953
954 if( var_InheritBool( p_aout, "volume-save" ) )
955 config_PutFloat( p_aout, "directx-volume", volume );
956 return ret;
957 }
958
MuteSet(audio_output_t * p_aout,bool mute)959 static int MuteSet( audio_output_t *p_aout, bool mute )
960 {
961 HRESULT res = DS_OK;
962 aout_sys_t *sys = p_aout->sys;
963
964 sys->volume.mute = mute;
965
966 if( sys->s.p_dsbuffer != NULL )
967 res = IDirectSoundBuffer_SetVolume( sys->s.p_dsbuffer,
968 mute? DSBVOLUME_MIN : sys->volume.mb );
969
970 aout_MuteReport( p_aout, mute );
971 return (res != DS_OK);
972 }
973
OutputStart(audio_output_t * p_aout,audio_sample_format_t * restrict fmt)974 static int OutputStart( audio_output_t *p_aout,
975 audio_sample_format_t *restrict fmt )
976 {
977 msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
978
979 /* Initialise DirectSound */
980 if( InitDirectSound( p_aout ) )
981 {
982 msg_Err( p_aout, "cannot initialize DirectSound" );
983 return -1;
984 }
985
986 HRESULT hr = Start( VLC_OBJECT(p_aout), &p_aout->sys->s, fmt );
987 if( FAILED(hr) )
988 return -1;
989
990 /* Force volume update */
991 VolumeSet( p_aout, p_aout->sys->volume.volume );
992 MuteSet( p_aout, p_aout->sys->volume.mute );
993
994 /* then launch the notification thread */
995 p_aout->time_get = OutputTimeGet;
996 p_aout->play = OutputPlay;
997 p_aout->pause = OutputPause;
998 p_aout->flush = OutputFlush;
999
1000 return 0;
1001 }
1002
1003 typedef struct
1004 {
1005 unsigned count;
1006 char **ids;
1007 char **names;
1008 } ds_list_t;
1009
DeviceEnumCallback(LPGUID guid,LPCWSTR desc,LPCWSTR mod,LPVOID data)1010 static int CALLBACK DeviceEnumCallback( LPGUID guid, LPCWSTR desc,
1011 LPCWSTR mod, LPVOID data )
1012 {
1013 ds_list_t *list = data;
1014 OLECHAR buf[48];
1015
1016 if( StringFromGUID2( guid, buf, 48 ) <= 0 )
1017 return true;
1018
1019 list->count++;
1020 list->ids = realloc_or_free( list->ids, list->count * sizeof(char *) );
1021 if( list->ids == NULL )
1022 return false;
1023 list->names = realloc_or_free( list->names, list->count * sizeof(char *) );
1024 if( list->names == NULL )
1025 {
1026 free( list->ids );
1027 return false;
1028 }
1029 list->ids[list->count - 1] = FromWide( buf );
1030 list->names[list->count - 1] = FromWide( desc );
1031
1032 (void) mod;
1033 return true;
1034 }
1035
1036 /**
1037 * Stores the list of devices in preferences
1038 */
ReloadDirectXDevices(vlc_object_t * p_this,char const * psz_name,char *** values,char *** descs)1039 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1040 char ***values, char ***descs )
1041 {
1042 ds_list_t list = {
1043 .count = 1,
1044 .ids = xmalloc(sizeof (char *)),
1045 .names = xmalloc(sizeof (char *)),
1046 };
1047 list.ids[0] = xstrdup("");
1048 list.names[0] = xstrdup(_("Default"));
1049
1050 (void) psz_name;
1051
1052 DirectSoundEnumerate( DeviceEnumCallback, &list );
1053 msg_Dbg( p_this, "found %u devices", list.count );
1054
1055 *values = list.ids;
1056 *descs = list.names;
1057 return list.count;
1058 }
1059
DeviceSelect(audio_output_t * aout,const char * id)1060 static int DeviceSelect (audio_output_t *aout, const char *id)
1061 {
1062 var_SetString(aout, "directx-audio-device", (id != NULL) ? id : "");
1063 aout_DeviceReport (aout, id);
1064 aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
1065 return 0;
1066 }
1067
Open(vlc_object_t * obj)1068 static int Open(vlc_object_t *obj)
1069 {
1070 audio_output_t *aout = (audio_output_t *)obj;
1071 aout_sys_t *sys = calloc(1, sizeof (*sys));
1072 if (unlikely(sys == NULL))
1073 return VLC_ENOMEM;
1074
1075 aout->sys = sys;
1076 aout->start = OutputStart;
1077 aout->stop = OutputStop;
1078 aout->volume_set = VolumeSet;
1079 aout->mute_set = MuteSet;
1080 aout->device_select = DeviceSelect;
1081
1082 /* Volume */
1083 sys->volume.volume = var_InheritFloat(aout, "directx-volume");
1084 aout_VolumeReport(aout, sys->volume.volume );
1085 MuteSet(aout, var_InheritBool(aout, "mute"));
1086
1087 /* DirectSound does not support hot-plug events (unless with WASAPI) */
1088 char **ids, **names;
1089 int count = ReloadDirectXDevices(obj, NULL, &ids, &names);
1090 if (count >= 0)
1091 {
1092 for (int i = 0; i < count; i++)
1093 {
1094 aout_HotplugReport(aout, ids[i], names[i]);
1095 free(names[i]);
1096 free(ids[i]);
1097 }
1098 free(names);
1099 free(ids);
1100 }
1101
1102 char *dev = var_CreateGetNonEmptyString(aout, "directx-audio-device");
1103 aout_DeviceReport(aout, dev);
1104 free(dev);
1105
1106 return VLC_SUCCESS;
1107 }
1108
Close(vlc_object_t * obj)1109 static void Close(vlc_object_t *obj)
1110 {
1111 audio_output_t *aout = (audio_output_t *)obj;
1112 aout_sys_t *sys = aout->sys;
1113
1114 var_Destroy(aout, "directx-audio-device");
1115 free(sys);
1116 }
1117
PlayedDataEraser(void * data)1118 static void * PlayedDataEraser( void * data )
1119 {
1120 const audio_output_t *aout = (audio_output_t *) data;
1121 aout_stream_sys_t *p_sys = &aout->sys->s;
1122 void *p_write_position, *p_wrap_around;
1123 unsigned long l_bytes1, l_bytes2;
1124 DWORD i_read;
1125 int64_t toerase, tosleep;
1126 HRESULT dsresult;
1127
1128 for(;;)
1129 {
1130 int canc = vlc_savecancel();
1131 vlc_mutex_lock( &p_sys->lock );
1132
1133 while( !p_sys->b_playing )
1134 vlc_cond_wait( &p_sys->cond, &p_sys->lock );
1135
1136 toerase = 0;
1137 tosleep = 0;
1138
1139 dsresult = IDirectSoundBuffer_GetCurrentPosition( p_sys->p_dsbuffer,
1140 &i_read, NULL );
1141 if( dsresult == DS_OK )
1142 {
1143 int64_t max = (int64_t) i_read - (int64_t) p_sys->i_write;
1144 tosleep = -max;
1145 if( max <= 0 )
1146 max += DS_BUF_SIZE;
1147 else
1148 tosleep += DS_BUF_SIZE;
1149 toerase = max;
1150 tosleep = ( tosleep / p_sys->i_bytes_per_sample ) * CLOCK_FREQ / p_sys->i_rate;
1151 }
1152
1153 tosleep = __MAX( tosleep, 20000 );
1154 dsresult = IDirectSoundBuffer_Lock( p_sys->p_dsbuffer,
1155 p_sys->i_write,
1156 toerase,
1157 &p_write_position,
1158 &l_bytes1,
1159 &p_wrap_around,
1160 &l_bytes2,
1161 0 );
1162 if( dsresult == DSERR_BUFFERLOST )
1163 {
1164 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
1165 dsresult = IDirectSoundBuffer_Lock( p_sys->p_dsbuffer,
1166 p_sys->i_write,
1167 toerase,
1168 &p_write_position,
1169 &l_bytes1,
1170 &p_wrap_around,
1171 &l_bytes2,
1172 0 );
1173 }
1174 if( dsresult != DS_OK )
1175 goto wait;
1176
1177 memset( p_write_position, 0, l_bytes1 );
1178 memset( p_wrap_around, 0, l_bytes2 );
1179
1180 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1181 p_wrap_around, l_bytes2 );
1182 wait:
1183 vlc_mutex_unlock(&p_sys->lock);
1184 vlc_restorecancel(canc);
1185 msleep(tosleep);
1186 }
1187 return NULL;
1188 }
1189