]> git.sesse.net Git - vlc/blob - modules/audio_output/directx.c
OSX: fix crash when no SD are found
[vlc] / modules / audio_output / directx.c
1 /*****************************************************************************
2  * directx.c: Windows DirectX audio output method
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 <math.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_aout.h>
37 #include <vlc_charset.h>
38
39 #include "windows_audio_common.h"
40
41 #define DS_BUF_SIZE (4*1024*1024)
42
43 /*****************************************************************************
44  * aout_sys_t: directx audio output method descriptor
45  *****************************************************************************
46  * This structure is part of the audio output thread descriptor.
47  * It describes the direct sound specific properties of an audio device.
48  *****************************************************************************/
49 struct aout_sys_t
50 {
51     HINSTANCE           hdsound_dll;  /* handle of the opened dsound dll */
52
53     LPDIRECTSOUND       p_dsobject;   /* main Direct Sound object */
54     LPDIRECTSOUNDBUFFER p_dsbuffer;   /* the sound buffer we use (direct sound
55                                        * takes care of mixing all the
56                                        * secondary buffers into the primary) */
57
58     LPDIRECTSOUNDNOTIFY p_notify;
59     HANDLE hnotify_evt;
60     struct
61     {
62         float            volume;
63         LONG             mb;
64         bool             mute;
65     } volume;
66
67     int      i_bytes_per_sample;      /* Size in bytes of one frame */
68     int      i_rate;                  /* Sample rate */
69
70     uint8_t  chans_to_reorder;        /* do we need channel reordering */
71     uint8_t  chan_table[AOUT_CHAN_MAX];
72     uint32_t i_channel_mask;
73     vlc_fourcc_t format;
74
75     size_t  i_write;
76 };
77
78 /*****************************************************************************
79  * Local prototypes.
80  *****************************************************************************/
81 static int  Open( vlc_object_t * );
82 static void Close( vlc_object_t * );
83 static void Stop( audio_output_t * );
84 static void Play( audio_output_t *, block_t * );
85 static int  VolumeSet( audio_output_t *, float );
86 static int  MuteSet( audio_output_t *, bool );
87 static void Flush( audio_output_t *, bool );
88 static void Pause( audio_output_t *, bool, mtime_t );
89 static int  TimeGet( audio_output_t *, mtime_t *);
90
91 /* local functions */
92 static int  InitDirectSound   ( audio_output_t * );
93 static int  CreateDSBuffer    ( audio_output_t *, int, int, int, int, bool );
94 static int  CreateDSBufferPCM ( audio_output_t *, vlc_fourcc_t*, int, int, bool );
95 static void DestroyDSBuffer   ( audio_output_t * );
96 static int  FillBuffer        ( audio_output_t *, block_t * );
97
98 static int ReloadDirectXDevices( vlc_object_t *, const char *,
99                                  char ***, char *** );
100
101 /* Speaker setup override options list */
102 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
103                                             "Quad", "5.1", "7.1" };
104
105 /*****************************************************************************
106  * Module descriptor
107  *****************************************************************************/
108 #define DEVICE_TEXT N_("Output device")
109 #define DEVICE_LONGTEXT N_("Select your audio output device")
110
111 #define SPEAKER_TEXT N_("Speaker configuration")
112 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
113     "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
114
115 #define VOLUME_TEXT N_("Audio volume")
116 #define VOLUME_LONGTEXT N_("Audio volume in hundredths of decibels (dB).")
117
118 vlc_module_begin ()
119     set_description( N_("DirectX audio output") )
120     set_shortname( "DirectX" )
121     set_capability( "audio output", 100 )
122     set_category( CAT_AUDIO )
123     set_subcategory( SUBCAT_AUDIO_AOUT )
124     add_shortcut( "directx", "aout_directx" )
125
126     add_string( "directx-audio-device", NULL,
127              DEVICE_TEXT, DEVICE_LONGTEXT, false )
128         change_string_cb( ReloadDirectXDevices )
129     add_obsolete_string( "directx-audio-device-name")
130     add_bool( "directx-audio-float32", true, FLOAT_TEXT,
131               FLOAT_LONGTEXT, true )
132     add_string( "directx-audio-speaker", "Windows default",
133                  SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
134         change_string_list( speaker_list, speaker_list )
135     add_float( "directx-volume", 1.0f,
136                  VOLUME_TEXT, VOLUME_LONGTEXT, true )
137         change_integer_range( DSBVOLUME_MIN, DSBVOLUME_MAX )
138
139     set_callbacks( Open, Close )
140 vlc_module_end ()
141
142 /*****************************************************************************
143  * OpenAudio: open the audio device
144  *****************************************************************************
145  * This function opens and setups Direct Sound.
146  *****************************************************************************/
147 static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
148 {
149     char * psz_speaker;
150     int i = 0;
151
152     const char * const * ppsz_compare = speaker_list;
153
154     msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
155
156     /* Retrieve config values */
157     var_Create( p_aout, "directx-audio-float32",
158                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
159     psz_speaker = var_CreateGetString( p_aout, "directx-audio-speaker" );
160
161     while ( *ppsz_compare != NULL )
162     {
163         if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
164         {
165             break;
166         }
167         ppsz_compare++; i++;
168     }
169
170     if ( *ppsz_compare == NULL )
171     {
172         msg_Err( p_aout, "(%s) isn't valid speaker setup option", psz_speaker );
173         msg_Err( p_aout, "Defaulting to Windows default speaker config");
174         i = 0;
175     }
176     free( psz_speaker );
177
178     /* Initialise DirectSound */
179     if( InitDirectSound( p_aout ) )
180     {
181         msg_Err( p_aout, "cannot initialize DirectSound" );
182         goto error;
183     }
184
185     if( i == 0 )
186     {
187         DWORD ui_speaker_config;
188
189         /* Check the speaker configuration to determine which channel config
190          * should be the default */
191         if( FAILED( IDirectSound_GetSpeakerConfig( p_aout->sys->p_dsobject,
192                                               &ui_speaker_config ) ) )
193         {
194             ui_speaker_config = DSSPEAKER_STEREO;
195             msg_Dbg( p_aout, "GetSpeakerConfig failed" );
196         }
197         fmt->i_physical_channels = AOUT_CHANS_2_0;
198
199         const char *name = "Unknown";
200         switch( DSSPEAKER_CONFIG(ui_speaker_config) )
201         {
202         case DSSPEAKER_7POINT1:
203         case DSSPEAKER_7POINT1_SURROUND:
204             name = "7.1";
205             fmt->i_physical_channels = AOUT_CHANS_7_1;
206             break;
207         case DSSPEAKER_5POINT1:
208         case DSSPEAKER_5POINT1_SURROUND:
209             name = "5.1";
210             fmt->i_physical_channels = AOUT_CHANS_5_1;
211             break;
212         case DSSPEAKER_QUAD:
213             name = "Quad";
214             fmt->i_physical_channels = AOUT_CHANS_4_0;
215             break;
216 #if 0 /* Lots of people just get their settings wrong and complain that
217        * this is a problem with VLC so just don't ever set mono by default. */
218         case DSSPEAKER_MONO:
219             name = "Mono";
220             fmt->i_physical_channels = AOUT_CHAN_CENTER;
221             break;
222 #endif
223         case DSSPEAKER_SURROUND: /* XXX: stereo, really? -- Courmisch */
224             name = "Surround";
225             break;
226         case DSSPEAKER_STEREO:
227             name = "Stereo";
228             break;
229         }
230         msg_Dbg( p_aout, "%s speaker config: %s", "Windows", name );
231     }
232     else
233     {   /* Overriden speaker configuration */
234         const char *name = "Non-existant";
235         switch( i )
236         {
237         case 1: /* Mono */
238             name = "Mono";
239             fmt->i_physical_channels = AOUT_CHAN_CENTER;
240             break;
241         case 2: /* Stereo */
242             name = "Stereo";
243             fmt->i_physical_channels = AOUT_CHANS_2_0;
244             break;
245         case 3: /* Quad */
246             name = "Quad";
247             fmt->i_physical_channels = AOUT_CHANS_4_0;
248             break;
249         case 4: /* 5.1 */
250             name = "5.1";
251             fmt->i_physical_channels = AOUT_CHANS_5_1;
252             break;
253         case 5: /* 7.1 */
254             name = "7.1";
255             fmt->i_physical_channels = AOUT_CHANS_7_1;
256             break;
257         }
258         msg_Dbg( p_aout, "%s speaker config: %s", "VLC", name );
259     }
260
261     /* Open the device */
262     if ( AOUT_FMT_SPDIF( fmt )
263      && var_InheritBool( p_aout, "spdif" )
264      && CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL, fmt->i_physical_channels,
265                         aout_FormatNbChannels( fmt ), fmt->i_rate, true )
266                                                                == VLC_SUCCESS )
267     {
268         msg_Dbg( p_aout, "using A/52 pass-through over S/PDIF" );
269         fmt->i_format = VLC_CODEC_SPDIFL;
270
271         /* Calculate the frame size in bytes */
272         fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
273         fmt->i_frame_length = A52_FRAME_NB;
274     }
275     else
276     {
277         aout_FormatPrepare( fmt );
278
279         if( CreateDSBufferPCM( p_aout, &fmt->i_format,
280                                fmt->i_physical_channels, fmt->i_rate, false )
281             != VLC_SUCCESS )
282         {
283             msg_Err( p_aout, "cannot open directx audio device" );
284             goto error;
285         }
286     }
287     p_aout->sys->i_write = 0;
288
289     /* Force volume update */
290     VolumeSet( p_aout, p_aout->sys->volume.volume );
291     MuteSet( p_aout, p_aout->sys->volume.mute );
292
293     /* then launch the notification thread */
294     p_aout->time_get = TimeGet;
295     p_aout->play = Play;
296     p_aout->pause = Pause;
297     p_aout->flush = Flush;
298
299     return VLC_SUCCESS;
300
301  error:
302     Stop( p_aout );
303     return VLC_EGENERIC;
304 }
305
306 /*****************************************************************************
307  * Play: we'll start playing the directsound buffer here because at least here
308  *       we know the first buffer has been put in the aout fifo and we also
309  *       know its date.
310  *****************************************************************************/
311 static void Play( audio_output_t *p_aout, block_t *p_buffer )
312 {
313     if(  FillBuffer( p_aout, p_buffer ) ==  VLC_SUCCESS )
314     {
315         /* start playing the buffer */
316         HRESULT dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
317                                                     0, 0, DSBPLAY_LOOPING );
318         if( dsresult == DSERR_BUFFERLOST )
319         {
320             IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
321             dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
322                                                 0, 0, DSBPLAY_LOOPING );
323         }
324         if( dsresult != DS_OK )
325             msg_Err( p_aout, "cannot start playing buffer" );
326     }
327 }
328
329 static int VolumeSet( audio_output_t *p_aout, float volume )
330 {
331     aout_sys_t *sys = p_aout->sys;
332     int ret = 0;
333
334     /* Convert UI volume to linear factor (cube) */
335     float vol = volume * volume * volume;
336
337     /* millibels from linear amplification */
338     LONG mb = lroundf(2000.f * log10f(vol));
339
340     /* Clamp to allowed DirectSound range */
341     static_assert( DSBVOLUME_MIN < DSBVOLUME_MAX, "DSBVOLUME_* confused" );
342     if( mb > DSBVOLUME_MAX )
343     {
344         mb = DSBVOLUME_MAX;
345         ret = -1;
346     }
347     if( mb <= DSBVOLUME_MIN )
348         mb = DSBVOLUME_MIN;
349
350     sys->volume.mb = mb;
351     sys->volume.volume = volume;
352     if( !sys->volume.mute && sys->p_dsbuffer &&
353         IDirectSoundBuffer_SetVolume( sys->p_dsbuffer, mb ) != DS_OK )
354         return -1;
355     /* Convert back to UI volume */
356     aout_VolumeReport( p_aout, volume );
357
358     if( var_InheritBool( p_aout, "volume-save" ) )
359         config_PutFloat( p_aout, "directx-volume", volume );
360     return ret;
361 }
362
363 static int MuteSet( audio_output_t *p_aout, bool mute )
364 {
365     HRESULT res = DS_OK;
366     aout_sys_t *sys = p_aout->sys;
367
368     sys->volume.mute = mute;
369
370     if( sys->p_dsbuffer )
371         res = IDirectSoundBuffer_SetVolume( sys->p_dsbuffer,
372                                             mute? DSBVOLUME_MIN : sys->volume.mb );
373
374     aout_MuteReport( p_aout, mute );
375     return (res != DS_OK);
376 }
377
378 /*****************************************************************************
379  * CloseAudio: close the audio device
380  *****************************************************************************/
381 static void Stop( audio_output_t *p_aout )
382 {
383     aout_sys_t *p_sys = p_aout->sys;
384     msg_Dbg( p_aout, "closing audio device" );
385
386     if( p_sys->p_notify )
387         IDirectSoundNotify_Release(p_sys->p_notify );
388     p_sys->p_notify = NULL;
389
390     IDirectSoundBuffer_Stop( p_sys->p_dsbuffer );
391     /* release the secondary buffer */
392     DestroyDSBuffer( p_aout );
393
394     /* finally release the DirectSound object */
395     if( p_sys->p_dsobject )
396         IDirectSound_Release( p_sys->p_dsobject );
397 }
398
399 /*****************************************************************************
400  * InitDirectSound: handle all the gory details of DirectSound initialisation
401  *****************************************************************************/
402 static int InitDirectSound( audio_output_t *p_aout )
403 {
404     aout_sys_t *sys = p_aout->sys;
405     GUID guid, *p_guid = NULL;
406     HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
407
408     OurDirectSoundCreate = (void *)
409         GetProcAddress( p_aout->sys->hdsound_dll,
410                         "DirectSoundCreate" );
411     if( OurDirectSoundCreate == NULL )
412     {
413         msg_Warn( p_aout, "GetProcAddress FAILED" );
414         goto error;
415     }
416
417     char *dev = var_GetNonEmptyString( p_aout, "directx-audio-device" );
418     if( dev != NULL )
419     {
420         LPOLESTR lpsz = ToWide( dev );
421         free( dev );
422
423         if( SUCCEEDED( IIDFromString( lpsz, &guid ) ) )
424             p_guid = &guid;
425         else
426             msg_Err( p_aout, "bad device GUID: %ls", lpsz );
427         free( lpsz );
428     }
429
430     /* Create the direct sound object */
431     if FAILED( OurDirectSoundCreate( p_guid, &sys->p_dsobject, NULL ) )
432     {
433         msg_Warn( p_aout, "cannot create a direct sound device" );
434         goto error;
435     }
436
437     /* Set DirectSound Cooperative level, ie what control we want over Windows
438      * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
439      * settings of the primary buffer, but also that only the sound of our
440      * application will be hearable when it will have the focus.
441      * !!! (this is not really working as intended yet because to set the
442      * cooperative level you need the window handle of your application, and
443      * I don't know of any easy way to get it. Especially since we might play
444      * sound without any video, and so what window handle should we use ???
445      * The hack for now is to use the Desktop window handle - it seems to be
446      * working */
447 #if !VLC_WINSTORE_APP
448     if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
449                                           GetDesktopWindow(),
450                                           DSSCL_EXCLUSIVE) )
451     {
452         msg_Warn( p_aout, "cannot set direct sound cooperative level" );
453     }
454 #endif
455     return VLC_SUCCESS;
456
457  error:
458     sys->p_dsobject = NULL;
459     return VLC_EGENERIC;
460
461 }
462
463 /*****************************************************************************
464  * CreateDSBuffer: Creates a direct sound buffer of the required format.
465  *****************************************************************************
466  * This function creates the buffer we'll use to play audio.
467  * In DirectSound there are two kinds of buffers:
468  * - the primary buffer: which is the actual buffer that the soundcard plays
469  * - the secondary buffer(s): these buffers are the one actually used by
470  *    applications and DirectSound takes care of mixing them into the primary.
471  *
472  * Once you create a secondary buffer, you cannot change its format anymore so
473  * you have to release the current one and create another.
474  *****************************************************************************/
475 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
476                            int i_channels, int i_nb_channels, int i_rate,
477                            bool b_probe )
478 {
479     WAVEFORMATEXTENSIBLE waveformat;
480     DSBUFFERDESC         dsbdesc;
481
482     /* First set the sound buffer format */
483     waveformat.dwChannelMask = 0;
484     for( unsigned i = 0; pi_vlc_chan_order_wg4[i]; i++ )
485         if( i_channels & pi_vlc_chan_order_wg4[i] )
486             waveformat.dwChannelMask |= pi_channels_in[i];
487
488     switch( i_format )
489     {
490     case VLC_CODEC_SPDIFL:
491         i_nb_channels = 2;
492         /* To prevent channel re-ordering */
493         waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
494         waveformat.Format.wBitsPerSample = 16;
495         waveformat.Samples.wValidBitsPerSample =
496             waveformat.Format.wBitsPerSample;
497         waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
498         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
499         break;
500
501     case VLC_CODEC_FL32:
502         waveformat.Format.wBitsPerSample = sizeof(float) * 8;
503         waveformat.Samples.wValidBitsPerSample =
504             waveformat.Format.wBitsPerSample;
505         waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
506         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
507         break;
508
509     case VLC_CODEC_S16N:
510         waveformat.Format.wBitsPerSample = 16;
511         waveformat.Samples.wValidBitsPerSample =
512             waveformat.Format.wBitsPerSample;
513         waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
514         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
515         break;
516     }
517
518     waveformat.Format.nChannels = i_nb_channels;
519     waveformat.Format.nSamplesPerSec = i_rate;
520     waveformat.Format.nBlockAlign =
521         waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
522     waveformat.Format.nAvgBytesPerSec =
523         waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
524
525     p_aout->sys->i_bytes_per_sample = waveformat.Format.nBlockAlign;
526     p_aout->sys->format = i_format;
527
528     /* Then fill in the direct sound descriptor */
529     memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
530     dsbdesc.dwSize = sizeof(DSBUFFERDESC);
531     dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 /* Better position accuracy */
532                     | DSBCAPS_GLOBALFOCUS         /* Allows background playing */
533                     | DSBCAPS_CTRLVOLUME          /* Allows volume control */
534                     | DSBCAPS_CTRLPOSITIONNOTIFY; /* Allow position notifications */
535
536     /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
537     if( i_nb_channels <= 2 )
538     {
539         waveformat.Format.cbSize = 0;
540     }
541     else
542     {
543         waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
544         waveformat.Format.cbSize =
545             sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
546
547         /* Needed for 5.1 on emu101k */
548         dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
549     }
550
551     dsbdesc.dwBufferBytes = DS_BUF_SIZE; /* buffer size */
552     dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
553
554     if FAILED( IDirectSound_CreateSoundBuffer(
555                    p_aout->sys->p_dsobject, &dsbdesc,
556                    &p_aout->sys->p_dsbuffer, NULL) )
557     {
558         if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
559         {
560             /* Try without DSBCAPS_LOCHARDWARE */
561             dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
562             if FAILED( IDirectSound_CreateSoundBuffer(
563                    p_aout->sys->p_dsobject, &dsbdesc,
564                    &p_aout->sys->p_dsbuffer, NULL) )
565             {
566                 return VLC_EGENERIC;
567             }
568             if( !b_probe )
569                 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
570         }
571         else
572         {
573             return VLC_EGENERIC;
574         }
575     }
576
577     /* Stop here if we were just probing */
578     if( b_probe )
579     {
580         IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
581         p_aout->sys->p_dsbuffer = NULL;
582         return VLC_SUCCESS;
583     }
584
585     p_aout->sys->i_rate = i_rate;
586     p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
587     p_aout->sys->chans_to_reorder =
588         aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
589                                   waveformat.dwChannelMask,
590                                   p_aout->sys->chan_table );
591     if( p_aout->sys->chans_to_reorder )
592     {
593         msg_Dbg( p_aout, "channel reordering needed" );
594     }
595
596     if( IDirectSoundBuffer_QueryInterface( p_aout->sys->p_dsbuffer,
597                                            &IID_IDirectSoundNotify,
598                                            (void **) &p_aout->sys->p_notify )
599             != DS_OK )
600     {
601
602         msg_Err(p_aout, "Couldn't query IDirectSoundNotify");
603         p_aout->sys->p_notify = NULL;
604     }
605
606     FillBuffer(p_aout,NULL);
607
608     return VLC_SUCCESS;
609 }
610
611 /*****************************************************************************
612  * CreateDSBufferPCM: creates a PCM direct sound buffer.
613  *****************************************************************************
614  * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
615  * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
616  ****************************************************************************/
617 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
618                               int i_channels, int i_rate, bool b_probe )
619 {
620     unsigned i_nb_channels = popcount( i_channels );
621
622     if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
623         CreateDSBuffer( p_aout, VLC_CODEC_FL32,
624                         i_channels, i_nb_channels, i_rate, b_probe )
625         != VLC_SUCCESS )
626     {
627         if ( CreateDSBuffer( p_aout, VLC_CODEC_S16N,
628                              i_channels, i_nb_channels, i_rate, b_probe )
629              != VLC_SUCCESS )
630         {
631             return VLC_EGENERIC;
632         }
633         else
634         {
635             *i_format = VLC_CODEC_S16N;
636             return VLC_SUCCESS;
637         }
638     }
639     else
640     {
641         *i_format = VLC_CODEC_FL32;
642         return VLC_SUCCESS;
643     }
644 }
645
646 /*****************************************************************************
647  * DestroyDSBuffer
648  *****************************************************************************
649  * This function destroys the secondary buffer.
650  *****************************************************************************/
651 static void DestroyDSBuffer( audio_output_t *p_aout )
652 {
653     if( p_aout->sys->p_dsbuffer )
654     {
655         IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
656         p_aout->sys->p_dsbuffer = NULL;
657     }
658 }
659
660 /*****************************************************************************
661  * FillBuffer: Fill in one of the direct sound frame buffers.
662  *****************************************************************************
663  * Returns VLC_SUCCESS on success.
664  *****************************************************************************/
665 static int FillBuffer( audio_output_t *p_aout, block_t *p_buffer )
666 {
667     aout_sys_t *p_sys = p_aout->sys;
668
669     size_t towrite = (p_buffer)?p_buffer->i_buffer:DS_BUF_SIZE;
670     void *p_write_position, *p_wrap_around;
671     unsigned long l_bytes1, l_bytes2;
672     HRESULT dsresult;
673
674     /* Before copying anything, we have to lock the buffer */
675     dsresult = IDirectSoundBuffer_Lock(
676                 p_sys->p_dsbuffer,         /* DS buffer */
677                 p_aout->sys->i_write,      /* Start offset */
678                 towrite,                   /* Number of bytes */
679                 &p_write_position,         /* Address of lock start */
680                 &l_bytes1,                 /* Count of bytes locked before wrap around */
681                 &p_wrap_around,            /* Buffer address (if wrap around) */
682                 &l_bytes2,                 /* Count of bytes after wrap around */
683                 0 );                       /* Flags: DSBLOCK_FROMWRITECURSOR is buggy */
684     if( dsresult == DSERR_BUFFERLOST )
685     {
686         IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
687         dsresult = IDirectSoundBuffer_Lock(
688                                p_sys->p_dsbuffer,
689                                p_aout->sys->i_write,
690                                towrite,
691                                &p_write_position,
692                                &l_bytes1,
693                                &p_wrap_around,
694                                &l_bytes2,
695                                0 );
696     }
697     if( dsresult != DS_OK )
698     {
699         msg_Warn( p_aout, "cannot lock buffer" );
700         if( p_buffer ) block_Release( p_buffer );
701         return VLC_EGENERIC;
702     }
703
704     if( p_buffer == NULL )
705     {
706         memset( p_write_position, 0, l_bytes1 );
707     }
708     else
709     {
710         if( p_sys->chans_to_reorder )
711             /* Do the channel reordering here */
712             aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
713                                  p_sys->chans_to_reorder, p_sys->chan_table,
714                                  p_sys->format );
715
716         memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
717         if( l_bytes1 < p_buffer->i_buffer)
718             memcpy(p_wrap_around, p_buffer->p_buffer + l_bytes1, l_bytes2);
719         block_Release( p_buffer );
720     }
721
722     /* Now the data has been copied, unlock the buffer */
723     IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
724                                p_wrap_around, l_bytes2 );
725
726     p_sys->i_write += towrite;
727     p_sys->i_write %= DS_BUF_SIZE;
728
729     return VLC_SUCCESS;
730 }
731
732 typedef struct
733 {
734     unsigned count;
735     char **ids;
736     char **names;
737 } ds_list_t;
738
739 static int CALLBACK DeviceEnumCallback( LPGUID guid, LPCWSTR desc,
740                                         LPCWSTR mod, LPVOID data )
741 {
742     ds_list_t *list = data;
743     OLECHAR buf[48];
744
745     StringFromGUID2( guid, buf, 48 );
746
747     list->count++;
748     list->ids = xrealloc( list->ids, list->count * sizeof(char *) );
749     list->names = xrealloc( list->names, list->count * sizeof(char *) );
750     list->ids[list->count - 1] = FromWide( buf );
751     list->names[list->count - 1] = FromWide( desc );
752     if( list->ids == NULL || list->names == NULL )
753         abort();
754
755     (void) mod;
756     return true;
757 }
758
759 /*****************************************************************************
760  * ReloadDirectXDevices: store the list of devices in preferences
761  *****************************************************************************/
762 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
763                                  char ***values, char ***descs )
764 {
765     ds_list_t list = {
766         .count = 1,
767         .ids = xmalloc(sizeof (char *)),
768         .names = xmalloc(sizeof (char *)),
769     };
770     list.ids[0] = xstrdup("");
771     list.names[0] = xstrdup(_("Default"));
772
773     (void) psz_name;
774
775     HANDLE hdsound_dll = LoadLibrary(_T("DSOUND.DLL"));
776     if( hdsound_dll == NULL )
777     {
778         msg_Warn( p_this, "cannot open DSOUND.DLL" );
779         goto out;
780     }
781
782     /* Get DirectSoundEnumerate */
783     HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID) =
784             (void *)GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
785     if( OurDirectSoundEnumerate != NULL )
786     {
787         OurDirectSoundEnumerate( DeviceEnumCallback, &list );
788         msg_Dbg( p_this, "found %u devices", list.count );
789     }
790     FreeLibrary(hdsound_dll);
791
792 out:
793     *values = list.ids;
794     *descs = list.names;
795     return list.count;
796 }
797
798 static int DeviceSelect (audio_output_t *aout, const char *id)
799 {
800     var_SetString(aout, "directx-audio-device", (id != NULL) ? id : "");
801     aout_DeviceReport (aout, id);
802     aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
803     return 0;
804 }
805
806 static int Open(vlc_object_t *obj)
807 {
808     audio_output_t *aout = (audio_output_t *)obj;
809     aout_sys_t *sys = calloc(1, sizeof (*sys));
810     if (unlikely(sys == NULL))
811         return VLC_ENOMEM;
812
813     sys->hdsound_dll = LoadLibrary(_T("DSOUND.DLL"));
814     if (sys->hdsound_dll == NULL)
815     {
816         msg_Warn(aout, "cannot open DSOUND.DLL");
817         free(sys);
818         return VLC_EGENERIC;
819     }
820
821     aout->sys = sys;
822     aout->start = Start;
823     aout->stop = Stop;
824     aout->volume_set = VolumeSet;
825     aout->mute_set = MuteSet;
826     aout->device_select = DeviceSelect;
827
828     /* Volume */
829     sys->volume.volume = var_InheritFloat(aout, "directx-volume");
830     aout_VolumeReport(aout, sys->volume.volume );
831     MuteSet(aout, var_InheritBool(aout, "mute"));
832
833     sys->hnotify_evt = CreateEvent(NULL, FALSE, TRUE, NULL);
834     if( !sys->hnotify_evt )
835     {
836         msg_Err(aout, "cannot create Event");
837         FreeLibrary(sys->hdsound_dll);
838         free(sys);
839         return VLC_EGENERIC;
840     }
841
842     /* DirectSound does not support hot-plug events (unless with WASAPI) */
843     char **ids, **names;
844     int count = ReloadDirectXDevices(obj, NULL, &ids, &names);
845     if (count >= 0)
846     {
847         for (int i = 0; i < count; i++)
848         {
849             aout_HotplugReport(aout, ids[i], names[i]);
850             free(names[i]);
851             free(ids[i]);
852         }
853         free(names);
854         free(ids);
855     }
856
857     char *dev = var_CreateGetNonEmptyString(aout, "directx-audio-device");
858     aout_DeviceReport(aout, dev);
859     free(dev);
860
861     return VLC_SUCCESS;
862 }
863
864 static void Close(vlc_object_t *obj)
865 {
866     audio_output_t *aout = (audio_output_t *)obj;
867     aout_sys_t *sys = aout->sys;
868
869     var_Destroy(aout, "directx-audio-device");
870     CloseHandle(sys->hnotify_evt);
871     FreeLibrary(sys->hdsound_dll); /* free DSOUND.DLL */
872     free(sys);
873 }
874
875 static void Flush ( audio_output_t * aout, bool drain )
876 {
877     aout_sys_t *sys = aout->sys;
878     size_t read;
879     if( drain )
880     {
881         if( sys->p_notify )
882         {
883             DSBPOSITIONNOTIFY notif = {.dwOffset = aout->sys->i_write, .hEventNotify = sys->hnotify_evt } ;
884             if( IDirectSoundNotify_SetNotificationPositions( sys->p_notify, 1, &notif ) ==  DS_OK )
885             {
886                 WaitForSingleObject( sys->hnotify_evt, INFINITE );
887                 IDirectSoundBuffer_Stop( aout->sys->p_dsbuffer );
888             }
889         }
890         else
891             while( IDirectSoundBuffer_GetCurrentPosition( aout->sys->p_dsbuffer,(LPDWORD) &read, NULL) ==  DS_OK )
892             {
893                 read %= DS_BUF_SIZE;
894                 if( read == aout->sys->i_write )
895                     break;
896                 msleep(10000);
897             }
898     }
899     else
900     {
901         IDirectSoundBuffer_Stop( aout->sys->p_dsbuffer );
902         IDirectSoundBuffer_SetCurrentPosition( aout->sys->p_dsbuffer,
903                                                aout->sys->i_write );
904     }
905 }
906
907 static void Pause( audio_output_t * aout, bool pause, mtime_t date )
908 {
909     (void) date;
910     if( pause )
911         IDirectSoundBuffer_Stop( aout->sys->p_dsbuffer );
912     else
913         IDirectSoundBuffer_Play( aout->sys->p_dsbuffer, 0, 0, DSBPLAY_LOOPING );
914 }
915
916
917 static int TimeGet( audio_output_t * aout, mtime_t * delay )
918 {
919     uint32_t read;
920     mtime_t size;
921
922     if( IDirectSoundBuffer_GetCurrentPosition( aout->sys->p_dsbuffer, (LPDWORD) &read, NULL) !=  DS_OK )
923         return 1;
924
925     read %= DS_BUF_SIZE;
926
927     size = (mtime_t)aout->sys->i_write - (mtime_t) read;
928     if( size < 0 )
929         size += DS_BUF_SIZE;
930
931     *delay = ( size / aout->sys->i_bytes_per_sample ) * CLOCK_FREQ / aout->sys->i_rate;
932     return 0;
933 }