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