]> git.sesse.net Git - vlc/blob - modules/audio_output/directx.c
CC: use c99, removed dummy Eia608Exit
[vlc] / modules / audio_output / directx.c
1 /*****************************************************************************
2  * directx.c: Windows DirectX audio output method
3  *****************************************************************************
4  * Copyright (C) 2001-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation, Inc.,
21  * 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 #include <vlc_atomic.h>
39
40 #include "windows_audio_common.h"
41
42 #define FRAME_SIZE ((int)p_aout->format.i_rate/20) /* Size in samples */
43
44 /*****************************************************************************
45  * notification_thread_t: DirectX event thread
46  *****************************************************************************/
47 typedef struct notification_thread_t
48 {
49     int i_frame_size;                          /* size in bytes of one frame */
50     int i_write_slot;       /* current write position in our circular buffer */
51
52     mtime_t start_date;
53     HANDLE event;
54
55     vlc_thread_t thread;
56     vlc_atomic_t abort;
57
58 } notification_thread_t;
59
60 /*****************************************************************************
61  * aout_sys_t: directx audio output method descriptor
62  *****************************************************************************
63  * This structure is part of the audio output thread descriptor.
64  * It describes the direct sound specific properties of an audio device.
65  *****************************************************************************/
66 struct aout_sys_t
67 {
68     aout_packet_t       packet;
69     HINSTANCE           hdsound_dll;      /* handle of the opened dsound dll */
70
71     char *              psz_device;              /* user defined device name */
72     LPGUID              p_device_guid;
73
74     LPDIRECTSOUND       p_dsobject;              /* main Direct Sound object */
75     LPDIRECTSOUNDBUFFER p_dsbuffer;   /* the sound buffer we use (direct sound
76                                        * takes care of mixing all the
77                                        * secondary buffers into the primary) */
78     struct
79     {
80         LONG             volume;
81         LONG             mb;
82         bool             mute;
83     } volume;
84
85     notification_thread_t notif;                  /* DirectSoundThread id */
86
87     int      i_frame_size;                     /* Size in bytes of one frame */
88
89     int      i_speaker_setup;                      /* Speaker setup override */
90
91     bool     b_chan_reorder;                /* do we need channel reordering */
92     int      pi_chan_table[AOUT_CHAN_MAX];
93     uint32_t i_channel_mask;
94     uint32_t i_bits_per_sample;
95     uint32_t i_channels;
96 };
97
98 /*****************************************************************************
99  * Local prototypes.
100  *****************************************************************************/
101 static int  OpenAudio  ( vlc_object_t * );
102 static void CloseAudio ( vlc_object_t * );
103 static void Play       ( audio_output_t *, block_t *, mtime_t * );
104 static int  VolumeSet  ( audio_output_t *, float );
105 static int  MuteSet    ( audio_output_t *, bool );
106
107 /* local functions */
108 static void Probe             ( audio_output_t * );
109 static int  InitDirectSound   ( audio_output_t * );
110 static int  CreateDSBuffer    ( audio_output_t *, int, int, int, int, int, bool );
111 static int  CreateDSBufferPCM ( audio_output_t *, vlc_fourcc_t*, int, int, int, bool );
112 static void DestroyDSBuffer   ( audio_output_t * );
113 static void* DirectSoundThread( void * );
114 static int  FillBuffer        ( audio_output_t *, int, block_t * );
115
116 static int ReloadDirectXDevices( vlc_object_t *, const char *,
117                                  char ***, char *** );
118
119 /* Speaker setup override options list */
120 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
121                                             "Quad", "5.1", "7.1" };
122
123 /*****************************************************************************
124  * Module descriptor
125  *****************************************************************************/
126 #define DEVICE_TEXT N_("Output device")
127 #define DEVICE_LONGTEXT N_("Select your audio output device")
128
129 #define SPEAKER_TEXT N_("Speaker configuration")
130 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
131     "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
132
133 #define VOLUME_TEXT N_("Audio volume")
134 #define VOLUME_LONGTEXT N_("Audio volume in hundredths of decibels (dB).")
135
136 vlc_module_begin ()
137     set_description( N_("DirectX audio output") )
138     set_shortname( "DirectX" )
139     set_capability( "audio output", 100 )
140     set_category( CAT_AUDIO )
141     set_subcategory( SUBCAT_AUDIO_AOUT )
142     add_shortcut( "directx", "aout_directx" )
143
144     add_string( "directx-audio-device", "default",
145              DEVICE_TEXT, DEVICE_LONGTEXT, false )
146         change_string_cb( ReloadDirectXDevices )
147     add_obsolete_string( "directx-audio-device-name")
148     add_bool( "directx-audio-float32", false, FLOAT_TEXT,
149               FLOAT_LONGTEXT, true )
150     add_string( "directx-audio-speaker", "Windows default",
151                  SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
152         change_string_list( speaker_list, speaker_list )
153     add_integer( "directx-volume", DSBVOLUME_MAX,
154                  VOLUME_TEXT, VOLUME_LONGTEXT, true )
155         change_integer_range( DSBVOLUME_MIN, DSBVOLUME_MAX )
156
157     set_callbacks( OpenAudio, CloseAudio )
158 vlc_module_end ()
159
160 /*****************************************************************************
161  * OpenAudio: open the audio device
162  *****************************************************************************
163  * This function opens and setups Direct Sound.
164  *****************************************************************************/
165 static int OpenAudio( vlc_object_t *p_this )
166 {
167     audio_output_t * p_aout = (audio_output_t *)p_this;
168     vlc_value_t val;
169     char * psz_speaker;
170     int i = 0;
171
172     const char * const * ppsz_compare = speaker_list;
173
174     msg_Dbg( p_aout, "Opening DirectSound Audio Output" );
175
176    /* Allocate structure */
177     p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
178     if( unlikely( p_aout->sys == NULL ) )
179         return VLC_ENOMEM;
180
181     /* Retrieve config values */
182     var_Create( p_aout, "directx-audio-float32",
183                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
184     psz_speaker = var_CreateGetString( p_aout, "directx-audio-speaker" );
185
186     while ( *ppsz_compare != NULL )
187     {
188         if ( !strncmp( *ppsz_compare, psz_speaker, strlen(*ppsz_compare) ) )
189         {
190             break;
191         }
192         ppsz_compare++; i++;
193     }
194
195     if ( *ppsz_compare == NULL )
196     {
197         msg_Err( p_aout, "(%s) isn't valid speaker setup option", psz_speaker );
198         msg_Err( p_aout, "Defaulting to Windows default speaker config");
199         i = 0;
200     }
201     free( psz_speaker );
202     p_aout->sys->i_speaker_setup = i;
203
204     /* Initialise DirectSound */
205     if( InitDirectSound( p_aout ) )
206     {
207         msg_Err( p_aout, "cannot initialize DirectSound" );
208         goto error;
209     }
210
211     if( var_Type( p_aout, "audio-device" ) == 0 )
212     {
213         Probe( p_aout );
214     }
215
216     if( var_Get( p_aout, "audio-device", &val ) < 0 )
217     {
218         msg_Err( p_aout, "DirectSound Probe failed()" );
219         goto error;
220     }
221
222     /* Open the device */
223     if( val.i_int == AOUT_VAR_SPDIF )
224     {
225         p_aout->format.i_format = VLC_CODEC_SPDIFL;
226
227         /* Calculate the frame size in bytes */
228         p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
229         p_aout->format.i_frame_length = A52_FRAME_NB;
230         p_aout->sys->i_frame_size = p_aout->format.i_bytes_per_frame;
231
232         if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
233                             p_aout->format.i_physical_channels,
234                             aout_FormatNbChannels( &p_aout->format ),
235                             p_aout->format.i_rate,
236                             p_aout->sys->i_frame_size, false )
237             != VLC_SUCCESS )
238         {
239             msg_Err( p_aout, "cannot open directx audio device" );
240             goto error;
241         }
242
243         aout_PacketInit( p_aout, &p_aout->sys->packet, A52_FRAME_NB );
244     }
245     else
246     {
247         if( val.i_int == AOUT_VAR_5_1 )
248         {
249             p_aout->format.i_physical_channels
250                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
251                    | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
252                    | AOUT_CHAN_LFE;
253         }
254         else if( val.i_int == AOUT_VAR_7_1 )
255         {
256                     p_aout->format.i_physical_channels
257                         = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
258                            | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
259                            | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
260                            | AOUT_CHAN_LFE;
261         }
262         else if( val.i_int == AOUT_VAR_3F2R )
263         {
264             p_aout->format.i_physical_channels
265                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
266                    | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
267         }
268         else if( val.i_int == AOUT_VAR_2F2R )
269         {
270             p_aout->format.i_physical_channels
271                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
272                    | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
273         }
274         else if( val.i_int == AOUT_VAR_MONO )
275         {
276             p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
277         }
278         else
279         {
280             p_aout->format.i_physical_channels
281                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
282         }
283
284         if( CreateDSBufferPCM( p_aout, &p_aout->format.i_format,
285                                p_aout->format.i_physical_channels,
286                                aout_FormatNbChannels( &p_aout->format ),
287                                p_aout->format.i_rate, false )
288             != VLC_SUCCESS )
289         {
290             msg_Err( p_aout, "cannot open directx audio device" );
291             goto error;
292         }
293
294         /* Calculate the frame size in bytes */
295         aout_FormatPrepare( &p_aout->format );
296         aout_PacketInit( p_aout, &p_aout->sys->packet, FRAME_SIZE );
297     }
298
299     p_aout->sys->volume.volume = -1;
300
301     /* Now we need to setup our DirectSound play notification structure */
302     vlc_atomic_set(&p_aout->sys->notif.abort, 0);
303     p_aout->sys->notif.event = CreateEvent( 0, FALSE, FALSE, 0 );
304     if( unlikely(p_aout->sys->notif.event == NULL) )
305         abort();
306     p_aout->sys->notif.i_frame_size =  p_aout->sys->i_frame_size;
307
308     /* then launch the notification thread */
309     msg_Dbg( p_aout, "creating DirectSoundThread" );
310     if( vlc_clone( &p_aout->sys->notif.thread, DirectSoundThread, p_aout,
311                    VLC_THREAD_PRIORITY_HIGHEST ) )
312     {
313         msg_Err( p_aout, "cannot create DirectSoundThread" );
314         CloseHandle( p_aout->sys->notif.event );
315         p_aout->sys->notif.event = NULL;
316         aout_PacketDestroy( p_aout );
317         goto error;
318     }
319
320     p_aout->pf_play = Play;
321     p_aout->pf_pause = aout_PacketPause;
322     p_aout->pf_flush = aout_PacketFlush;
323
324     /* Volume */
325     if( val.i_int == AOUT_VAR_SPDIF )
326     {
327         p_aout->volume_set = NULL;
328         p_aout->mute_set = NULL;
329     }
330     else
331     {
332         LONG mb = var_InheritInteger( p_aout, "directx-volume" );
333
334         p_aout->volume_set = VolumeSet;
335         p_aout->mute_set = MuteSet;
336         p_aout->sys->volume.mb = mb;
337         aout_VolumeReport( p_aout, cbrtf(powf(10.f, ((float)mb) / 2000.f)) );
338         MuteSet( p_aout, var_InheritBool( p_aout, "mute" ) );
339     }
340     return VLC_SUCCESS;
341
342  error:
343     CloseAudio( VLC_OBJECT(p_aout) );
344     return VLC_EGENERIC;
345 }
346
347 /*****************************************************************************
348  * Probe: probe the audio device for available formats and channels
349  *****************************************************************************/
350 static void Probe( audio_output_t * p_aout )
351 {
352     vlc_value_t val, text;
353     vlc_fourcc_t i_format;
354     unsigned int i_physical_channels;
355     DWORD ui_speaker_config;
356     bool is_default_output_set = false;
357
358     var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
359     text.psz_string = _("Audio Device");
360     var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
361
362     /* Test for 5.1 support */
363     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
364                           AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
365                           AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
366     if( p_aout->format.i_physical_channels == i_physical_channels )
367     {
368         if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
369                                p_aout->format.i_rate, true )
370             == VLC_SUCCESS )
371         {
372             val.i_int = AOUT_VAR_5_1;
373             text.psz_string = (char*) "5.1";
374             var_Change( p_aout, "audio-device",
375                         VLC_VAR_ADDCHOICE, &val, &text );
376             var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
377             is_default_output_set = true;
378             msg_Dbg( p_aout, "device supports 5.1 channels" );
379         }
380     }
381
382     /* Test for 7.1 support */
383     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
384                              AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
385                              AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
386                              AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
387     if( p_aout->format.i_physical_channels == i_physical_channels )
388     {
389         if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 8,
390                                   p_aout->format.i_rate, true )
391             == VLC_SUCCESS )
392         {
393             val.i_int = AOUT_VAR_7_1;
394             text.psz_string = (char*) "7.1";
395             var_Change( p_aout, "audio-device",
396                         VLC_VAR_ADDCHOICE, &val, &text );
397             var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
398             is_default_output_set = true;
399             msg_Dbg( p_aout, "device supports 7.1 channels" );
400         }
401     }
402
403     /* Test for 3 Front 2 Rear support */
404     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
405                           AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
406                           AOUT_CHAN_REARRIGHT;
407     if( p_aout->format.i_physical_channels == i_physical_channels )
408     {
409         if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
410                                p_aout->format.i_rate, true )
411             == VLC_SUCCESS )
412         {
413             val.i_int = AOUT_VAR_3F2R;
414             text.psz_string = _("3 Front 2 Rear");
415             var_Change( p_aout, "audio-device",
416                         VLC_VAR_ADDCHOICE, &val, &text );
417             if(!is_default_output_set)
418             {
419                 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
420                 is_default_output_set = true;
421             }
422             msg_Dbg( p_aout, "device supports 5 channels" );
423         }
424     }
425
426     /* Test for 2 Front 2 Rear support */
427     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
428                           AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
429     if( ( p_aout->format.i_physical_channels & i_physical_channels )
430         == i_physical_channels )
431     {
432         if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
433                                p_aout->format.i_rate, true )
434             == VLC_SUCCESS )
435         {
436             val.i_int = AOUT_VAR_2F2R;
437             text.psz_string = _("2 Front 2 Rear");
438             var_Change( p_aout, "audio-device",
439                         VLC_VAR_ADDCHOICE, &val, &text );
440             if(!is_default_output_set)
441             {
442                 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
443                 is_default_output_set = true;
444             }
445             msg_Dbg( p_aout, "device supports 4 channels" );
446         }
447     }
448
449     /* Test for stereo support */
450     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
451     if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
452                            p_aout->format.i_rate, true )
453         == VLC_SUCCESS )
454     {
455         val.i_int = AOUT_VAR_STEREO;
456         text.psz_string = _("Stereo");
457         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
458         if(!is_default_output_set)
459         {
460             var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
461             is_default_output_set = true;
462             msg_Dbg( p_aout, "device supports 2 channels (DEFAULT!)" );
463         }
464         else msg_Dbg( p_aout, "device supports 2 channels" );
465     }
466
467     /* Test for mono support */
468     i_physical_channels = AOUT_CHAN_CENTER;
469     if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
470                            p_aout->format.i_rate, true )
471         == VLC_SUCCESS )
472     {
473         val.i_int = AOUT_VAR_MONO;
474         text.psz_string = _("Mono");
475         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
476         msg_Dbg( p_aout, "device supports 1 channel" );
477     }
478
479     /* Check the speaker configuration to determine which channel config should
480      * be the default */
481     if FAILED( IDirectSound_GetSpeakerConfig( p_aout->sys->p_dsobject,
482                                               &ui_speaker_config ) )
483     {
484         ui_speaker_config = DSSPEAKER_STEREO;
485         msg_Dbg( p_aout, "GetSpeakerConfig failed" );
486     }
487     switch( DSSPEAKER_CONFIG(ui_speaker_config) )
488     {
489     case DSSPEAKER_7POINT1:
490     case DSSPEAKER_7POINT1_SURROUND:
491         msg_Dbg( p_aout, "Windows says your SpeakerConfig is 7.1" );
492         val.i_int = AOUT_VAR_7_1;
493         break;
494     case DSSPEAKER_5POINT1:
495     case DSSPEAKER_5POINT1_SURROUND:
496         msg_Dbg( p_aout, "Windows says your SpeakerConfig is 5.1" );
497         val.i_int = AOUT_VAR_5_1;
498         break;
499     case DSSPEAKER_QUAD:
500         msg_Dbg( p_aout, "Windows says your SpeakerConfig is Quad" );
501         val.i_int = AOUT_VAR_2F2R;
502         break;
503 #if 0 /* Lots of people just get their settings wrong and complain that
504        * this is a problem with VLC so just don't ever set mono by default. */
505     case DSSPEAKER_MONO:
506         val.i_int = AOUT_VAR_MONO;
507         break;
508 #endif
509     case DSSPEAKER_SURROUND:
510         msg_Dbg( p_aout, "Windows says your SpeakerConfig is surround" );
511     case DSSPEAKER_STEREO:
512         msg_Dbg( p_aout, "Windows says your SpeakerConfig is stereo" );
513     default:
514         /* If nothing else is found, choose stereo output */
515         val.i_int = AOUT_VAR_STEREO;
516         break;
517     }
518
519     /* Check if we want to override speaker config */
520     switch( p_aout->sys->i_speaker_setup )
521     {
522     case 0: /* Default value aka Windows default speaker setup */
523         break;
524     case 1: /* Mono */
525         msg_Dbg( p_aout, "SpeakerConfig is forced to Mono" );
526         val.i_int = AOUT_VAR_MONO;
527         break;
528     case 2: /* Stereo */
529         msg_Dbg( p_aout, "SpeakerConfig is forced to Stereo" );
530         val.i_int = AOUT_VAR_STEREO;
531         break;
532     case 3: /* Quad */
533         msg_Dbg( p_aout, "SpeakerConfig is forced to Quad" );
534         val.i_int = AOUT_VAR_2F2R;
535         break;
536     case 4: /* 5.1 */
537         msg_Dbg( p_aout, "SpeakerConfig is forced to 5.1" );
538         val.i_int = AOUT_VAR_5_1;
539         break;
540     case 5: /* 7.1 */
541         msg_Dbg( p_aout, "SpeakerConfig is forced to 7.1" );
542         val.i_int = AOUT_VAR_7_1;
543         break;
544     default:
545         msg_Dbg( p_aout, "SpeakerConfig is forced to non-existing value" );
546         break;
547     }
548
549     var_Set( p_aout, "audio-device", val );
550
551     /* Test for SPDIF support */
552     if ( AOUT_FMT_SPDIF( &p_aout->format ) )
553     {
554         if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
555                             p_aout->format.i_physical_channels,
556                             aout_FormatNbChannels( &p_aout->format ),
557                             p_aout->format.i_rate,
558                             AOUT_SPDIF_SIZE, true )
559             == VLC_SUCCESS )
560         {
561             msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
562             val.i_int = AOUT_VAR_SPDIF;
563             text.psz_string = _("A/52 over S/PDIF");
564             var_Change( p_aout, "audio-device",
565                         VLC_VAR_ADDCHOICE, &val, &text );
566             if( var_InheritBool( p_aout, "spdif" ) )
567                 var_Set( p_aout, "audio-device", val );
568         }
569     }
570
571     var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
572     if( val.i_int <= 0 )
573     {
574         /* Probe() has failed. */
575         var_Destroy( p_aout, "audio-device" );
576         return;
577     }
578
579     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
580 }
581
582 /*****************************************************************************
583  * Play: we'll start playing the directsound buffer here because at least here
584  *       we know the first buffer has been put in the aout fifo and we also
585  *       know its date.
586  *****************************************************************************/
587 static void Play( audio_output_t *p_aout, block_t *p_buffer,
588                   mtime_t *restrict drift )
589 {
590     /* get the playing date of the first aout buffer */
591     p_aout->sys->notif.start_date = p_buffer->i_pts;
592
593     /* fill in the first samples (zeroes) */
594     FillBuffer( p_aout, 0, NULL );
595
596     /* wake up the audio output thread */
597     SetEvent( p_aout->sys->notif.event );
598
599     aout_PacketPlay( p_aout, p_buffer, drift );
600     p_aout->pf_play = aout_PacketPlay;
601 }
602
603 static int VolumeSet( audio_output_t *p_aout, float vol )
604 {
605     aout_sys_t *sys = p_aout->sys;
606     int ret = 0;
607
608     /* Convert UI volume to linear factor (cube) */
609     vol = vol * vol * vol;
610
611     /* millibels from linear amplification */
612     LONG mb = lroundf(2000.f * log10f(vol));
613
614     /* Clamp to allowed DirectSound range */
615     static_assert( DSBVOLUME_MIN < DSBVOLUME_MAX, "DSBVOLUME_* confused" );
616     if( mb >= DSBVOLUME_MAX )
617     {
618         mb = DSBVOLUME_MAX;
619         ret = -1;
620     }
621     if( mb <= DSBVOLUME_MIN )
622         mb = DSBVOLUME_MIN;
623
624     sys->volume.mb = mb;
625     if (!sys->volume.mute)
626         InterlockedExchange(&sys->volume.volume, mb);
627
628     /* Convert back to UI volume */
629     vol = cbrtf(powf(10.f, ((float)mb) / 2000.f));
630     aout_VolumeReport( p_aout, vol );
631
632     if( var_InheritBool( p_aout, "volume-save" ) )
633         config_PutInt( p_aout, "directx-volume", mb );
634     return ret;
635 }
636
637 static int MuteSet( audio_output_t *p_aout, bool mute )
638 {
639     aout_sys_t *sys = p_aout->sys;
640
641     sys->volume.mute = mute;
642     InterlockedExchange(&sys->volume.volume,
643                         mute ? DSBVOLUME_MIN : sys->volume.mb);
644
645     aout_MuteReport( p_aout, mute );
646     return 0;
647 }
648
649 /*****************************************************************************
650  * CloseAudio: close the audio device
651  *****************************************************************************/
652 static void CloseAudio( vlc_object_t *p_this )
653 {
654     audio_output_t * p_aout = (audio_output_t *)p_this;
655     aout_sys_t *p_sys = p_aout->sys;
656
657     msg_Dbg( p_aout, "closing audio device" );
658
659     /* kill the position notification thread, if any */
660     if( p_sys->notif.event != NULL )
661     {
662         vlc_atomic_set(&p_aout->sys->notif.abort, 1);
663         /* wake up the audio thread if needed */
664         if( p_aout->pf_play == Play )
665             SetEvent( p_sys->notif.event );
666
667         vlc_join( p_sys->notif.thread, NULL );
668         CloseHandle( p_sys->notif.event );
669         aout_PacketDestroy( p_aout );
670     }
671
672     /* release the secondary buffer */
673     DestroyDSBuffer( p_aout );
674
675     /* finally release the DirectSound object */
676     if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
677
678     /* free DSOUND.DLL */
679     if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
680
681     free( p_aout->sys->p_device_guid );
682     free( p_sys );
683 }
684
685 /*****************************************************************************
686  * CallBackDirectSoundEnum: callback to enumerate available devices
687  *****************************************************************************/
688 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
689                                              LPCWSTR psz_mod, LPVOID _p_aout )
690 {
691     VLC_UNUSED( psz_mod );
692
693     audio_output_t *p_aout = (audio_output_t *)_p_aout;
694
695     char *psz_device = FromWide( psz_desc );
696     msg_Dbg( p_aout, "found device: %s", psz_device );
697
698     if( p_aout->sys->psz_device &&
699         !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
700     {
701         /* Use the device corresponding to psz_device */
702         p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
703         *p_aout->sys->p_device_guid = *p_guid;
704         msg_Dbg( p_aout, "using device: %s", psz_device );
705     }
706     else
707     {
708         /* If no default device has been selected, chose the first one */
709         if( !p_aout->sys->psz_device && p_guid )
710         {
711             p_aout->sys->psz_device = strdup( psz_device );
712             p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
713             *p_aout->sys->p_device_guid = *p_guid;
714             msg_Dbg( p_aout, "using device: %s", psz_device );
715         }
716     }
717
718     free( psz_device );
719     return true;
720 }
721
722 /*****************************************************************************
723  * InitDirectSound: handle all the gory details of DirectSound initialisation
724  *****************************************************************************/
725 static int InitDirectSound( audio_output_t *p_aout )
726 {
727     HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
728     HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
729
730     p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
731     if( p_aout->sys->hdsound_dll == NULL )
732     {
733         msg_Warn( p_aout, "cannot open DSOUND.DLL" );
734         goto error;
735     }
736
737     OurDirectSoundCreate = (void *)
738         GetProcAddress( p_aout->sys->hdsound_dll,
739                         "DirectSoundCreate" );
740     if( OurDirectSoundCreate == NULL )
741     {
742         msg_Warn( p_aout, "GetProcAddress FAILED" );
743         goto error;
744     }
745
746     /* Get DirectSoundEnumerate */
747     OurDirectSoundEnumerate = (void *)
748        GetProcAddress( p_aout->sys->hdsound_dll,
749                        "DirectSoundEnumerateW" );
750     if( OurDirectSoundEnumerate )
751     {
752         p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device");
753         /* Attempt enumeration */
754         if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
755                                              p_aout ) ) )
756         {
757             msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
758         }
759     }
760
761     /* Create the direct sound object */
762     if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
763                                      &p_aout->sys->p_dsobject,
764                                      NULL ) )
765     {
766         msg_Warn( p_aout, "cannot create a direct sound device" );
767         goto error;
768     }
769
770     /* Set DirectSound Cooperative level, ie what control we want over Windows
771      * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
772      * settings of the primary buffer, but also that only the sound of our
773      * application will be hearable when it will have the focus.
774      * !!! (this is not really working as intended yet because to set the
775      * cooperative level you need the window handle of your application, and
776      * I don't know of any easy way to get it. Especially since we might play
777      * sound without any video, and so what window handle should we use ???
778      * The hack for now is to use the Desktop window handle - it seems to be
779      * working */
780     if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
781                                           GetDesktopWindow(),
782                                           DSSCL_EXCLUSIVE) )
783     {
784         msg_Warn( p_aout, "cannot set direct sound cooperative level" );
785     }
786
787     return VLC_SUCCESS;
788
789  error:
790     p_aout->sys->p_dsobject = NULL;
791     if( p_aout->sys->hdsound_dll )
792     {
793         FreeLibrary( p_aout->sys->hdsound_dll );
794         p_aout->sys->hdsound_dll = NULL;
795     }
796     return VLC_EGENERIC;
797
798 }
799
800 /*****************************************************************************
801  * CreateDSBuffer: Creates a direct sound buffer of the required format.
802  *****************************************************************************
803  * This function creates the buffer we'll use to play audio.
804  * In DirectSound there are two kinds of buffers:
805  * - the primary buffer: which is the actual buffer that the soundcard plays
806  * - the secondary buffer(s): these buffers are the one actually used by
807  *    applications and DirectSound takes care of mixing them into the primary.
808  *
809  * Once you create a secondary buffer, you cannot change its format anymore so
810  * you have to release the current one and create another.
811  *****************************************************************************/
812 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
813                            int i_channels, int i_nb_channels, int i_rate,
814                            int i_bytes_per_frame, bool b_probe )
815 {
816     WAVEFORMATEXTENSIBLE waveformat;
817     DSBUFFERDESC         dsbdesc;
818
819     /* First set the sound buffer format */
820     waveformat.dwChannelMask = 0;
821     for( unsigned i = 0; pi_vlc_chan_order_wg4[i]; i++ )
822         if( i_channels & pi_vlc_chan_order_wg4[i] )
823             waveformat.dwChannelMask |= pi_channels_in[i];
824
825     switch( i_format )
826     {
827     case VLC_CODEC_SPDIFL:
828         i_nb_channels = 2;
829         /* To prevent channel re-ordering */
830         waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
831         waveformat.Format.wBitsPerSample = 16;
832         waveformat.Samples.wValidBitsPerSample =
833             waveformat.Format.wBitsPerSample;
834         waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
835         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
836         break;
837
838     case VLC_CODEC_FL32:
839         waveformat.Format.wBitsPerSample = sizeof(float) * 8;
840         waveformat.Samples.wValidBitsPerSample =
841             waveformat.Format.wBitsPerSample;
842         waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
843         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
844         break;
845
846     case VLC_CODEC_S16L:
847         waveformat.Format.wBitsPerSample = 16;
848         waveformat.Samples.wValidBitsPerSample =
849             waveformat.Format.wBitsPerSample;
850         waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
851         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
852         break;
853     }
854
855     waveformat.Format.nChannels = i_nb_channels;
856     waveformat.Format.nSamplesPerSec = i_rate;
857     waveformat.Format.nBlockAlign =
858         waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
859     waveformat.Format.nAvgBytesPerSec =
860         waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
861
862     p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
863     p_aout->sys->i_channels = i_nb_channels;
864
865     /* Then fill in the direct sound descriptor */
866     memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
867     dsbdesc.dwSize = sizeof(DSBUFFERDESC);
868     dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
869                     | DSBCAPS_GLOBALFOCUS       /* Allows background playing */
870                     | DSBCAPS_CTRLVOLUME;       /* Allows volume control */
871
872     /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
873     if( i_nb_channels <= 2 )
874     {
875         waveformat.Format.cbSize = 0;
876     }
877     else
878     {
879         waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
880         waveformat.Format.cbSize =
881             sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
882
883         /* Needed for 5.1 on emu101k */
884         dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
885     }
886
887     dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame;   /* buffer size */
888     dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
889
890     if FAILED( IDirectSound_CreateSoundBuffer(
891                    p_aout->sys->p_dsobject, &dsbdesc,
892                    &p_aout->sys->p_dsbuffer, NULL) )
893     {
894         if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
895         {
896             /* Try without DSBCAPS_LOCHARDWARE */
897             dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
898             if FAILED( IDirectSound_CreateSoundBuffer(
899                    p_aout->sys->p_dsobject, &dsbdesc,
900                    &p_aout->sys->p_dsbuffer, NULL) )
901             {
902                 return VLC_EGENERIC;
903             }
904             if( !b_probe )
905                 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
906         }
907         else
908         {
909             return VLC_EGENERIC;
910         }
911     }
912
913     /* Stop here if we were just probing */
914     if( b_probe )
915     {
916         IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
917         p_aout->sys->p_dsbuffer = NULL;
918         return VLC_SUCCESS;
919     }
920
921     p_aout->sys->i_frame_size = i_bytes_per_frame;
922     p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
923     p_aout->sys->b_chan_reorder =
924         aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
925                                   waveformat.dwChannelMask, i_nb_channels,
926                                   p_aout->sys->pi_chan_table );
927
928     if( p_aout->sys->b_chan_reorder )
929     {
930         msg_Dbg( p_aout, "channel reordering needed" );
931     }
932
933     return VLC_SUCCESS;
934 }
935
936 /*****************************************************************************
937  * CreateDSBufferPCM: creates a PCM direct sound buffer.
938  *****************************************************************************
939  * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
940  * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
941  ****************************************************************************/
942 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
943                               int i_channels, int i_nb_channels, int i_rate,
944                               bool b_probe )
945 {
946     /* Float32 audio samples are not supported for 5.1 output on the emu101k */
947     if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
948         i_nb_channels > 2 ||
949         CreateDSBuffer( p_aout, VLC_CODEC_FL32,
950                         i_channels, i_nb_channels, i_rate,
951                         FRAME_SIZE * 4 * i_nb_channels, b_probe )
952         != VLC_SUCCESS )
953     {
954         if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
955                              i_channels, i_nb_channels, i_rate,
956                              FRAME_SIZE * 2 * i_nb_channels, b_probe )
957              != VLC_SUCCESS )
958         {
959             return VLC_EGENERIC;
960         }
961         else
962         {
963             *i_format = VLC_CODEC_S16L;
964             return VLC_SUCCESS;
965         }
966     }
967     else
968     {
969         *i_format = VLC_CODEC_FL32;
970         return VLC_SUCCESS;
971     }
972 }
973
974 /*****************************************************************************
975  * DestroyDSBuffer
976  *****************************************************************************
977  * This function destroys the secondary buffer.
978  *****************************************************************************/
979 static void DestroyDSBuffer( audio_output_t *p_aout )
980 {
981     if( p_aout->sys->p_dsbuffer )
982     {
983         IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
984         p_aout->sys->p_dsbuffer = NULL;
985     }
986 }
987
988 /*****************************************************************************
989  * FillBuffer: Fill in one of the direct sound frame buffers.
990  *****************************************************************************
991  * Returns VLC_SUCCESS on success.
992  *****************************************************************************/
993 static int FillBuffer( audio_output_t *p_aout, int i_frame, block_t *p_buffer )
994 {
995     aout_sys_t *p_sys = p_aout->sys;
996     notification_thread_t *p_notif = &p_sys->notif;
997     void *p_write_position, *p_wrap_around;
998     unsigned long l_bytes1, l_bytes2;
999     HRESULT dsresult;
1000
1001     /* Before copying anything, we have to lock the buffer */
1002     dsresult = IDirectSoundBuffer_Lock(
1003                 p_sys->p_dsbuffer,                              /* DS buffer */
1004                 i_frame * p_notif->i_frame_size,             /* Start offset */
1005                 p_notif->i_frame_size,                    /* Number of bytes */
1006                 &p_write_position,                  /* Address of lock start */
1007                 &l_bytes1,       /* Count of bytes locked before wrap around */
1008                 &p_wrap_around,           /* Buffer address (if wrap around) */
1009                 &l_bytes2,               /* Count of bytes after wrap around */
1010                 0 );                                                /* Flags */
1011     if( dsresult == DSERR_BUFFERLOST )
1012     {
1013         IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
1014         dsresult = IDirectSoundBuffer_Lock(
1015                                p_sys->p_dsbuffer,
1016                                i_frame * p_notif->i_frame_size,
1017                                p_notif->i_frame_size,
1018                                &p_write_position,
1019                                &l_bytes1,
1020                                &p_wrap_around,
1021                                &l_bytes2,
1022                                0 );
1023     }
1024     if( dsresult != DS_OK )
1025     {
1026         msg_Warn( p_aout, "cannot lock buffer" );
1027         if( p_buffer ) block_Release( p_buffer );
1028         return VLC_EGENERIC;
1029     }
1030
1031     if( p_buffer == NULL )
1032     {
1033         memset( p_write_position, 0, l_bytes1 );
1034     }
1035     else
1036     {
1037         if( p_sys->b_chan_reorder )
1038         {
1039             /* Do the channel reordering here */
1040             aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
1041                                  p_sys->i_channels, p_sys->pi_chan_table,
1042                                  p_sys->i_bits_per_sample );
1043         }
1044
1045         memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
1046         block_Release( p_buffer );
1047     }
1048
1049     /* Now the data has been copied, unlock the buffer */
1050     IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1051                                p_wrap_around, l_bytes2 );
1052
1053     p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1054     return VLC_SUCCESS;
1055 }
1056
1057 /*****************************************************************************
1058  * DirectSoundThread: this thread will capture play notification events.
1059  *****************************************************************************
1060  * We use this thread to emulate a callback mechanism. The thread probes for
1061  * event notification and fills up the DS secondary buffer when needed.
1062  *****************************************************************************/
1063 static void* DirectSoundThread( void *data )
1064 {
1065     audio_output_t *p_aout = (audio_output_t *)data;
1066     notification_thread_t *p_notif = &p_aout->sys->notif;
1067     mtime_t last_time;
1068
1069     msg_Dbg( p_aout, "DirectSoundThread ready" );
1070
1071     /* Wait here until Play() is called */
1072     WaitForSingleObject( p_notif->event, INFINITE );
1073
1074     if( !vlc_atomic_get( &p_notif->abort) )
1075     {
1076         HRESULT dsresult;
1077         mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1078
1079         /* start playing the buffer */
1080         dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1081                                         0,                         /* Unused */
1082                                         0,                         /* Unused */
1083                                         DSBPLAY_LOOPING );          /* Flags */
1084         if( dsresult == DSERR_BUFFERLOST )
1085         {
1086             IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1087             dsresult = IDirectSoundBuffer_Play(
1088                                             p_aout->sys->p_dsbuffer,
1089                                             0,                     /* Unused */
1090                                             0,                     /* Unused */
1091                                             DSBPLAY_LOOPING );      /* Flags */
1092         }
1093         if( dsresult != DS_OK )
1094         {
1095             msg_Err( p_aout, "cannot start playing buffer" );
1096         }
1097     }
1098     last_time = mdate();
1099
1100     while( !vlc_atomic_get( &p_notif->abort ) )
1101     {
1102         DWORD l_read;
1103         int l_queued = 0, l_free_slots;
1104         unsigned i_frame_siz = p_aout->sys->packet.samples;
1105         mtime_t mtime = mdate();
1106         int i;
1107
1108         /* Update volume if required */
1109         LONG volume = InterlockedExchange( &p_aout->sys->volume.volume, -1 );
1110         if( unlikely(volume != -1) )
1111             IDirectSoundBuffer_SetVolume( p_aout->sys->p_dsbuffer, volume );
1112
1113         /*
1114          * Fill in as much audio data as we can in our circular buffer
1115          */
1116
1117         /* Find out current play position */
1118         if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1119                    p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1120         {
1121             msg_Err( p_aout, "GetCurrentPosition() failed!" );
1122             l_read = 0;
1123         }
1124
1125         /* Detect underruns */
1126         if( l_queued && mtime - last_time >
1127             INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1128         {
1129             msg_Dbg( p_aout, "detected underrun!" );
1130         }
1131         last_time = mtime;
1132
1133         /* Try to fill in as many frame buffers as possible */
1134         l_read /= (p_aout->format.i_bytes_per_frame /
1135             p_aout->format.i_frame_length);
1136         l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1137         if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1138         l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1139
1140         for( i = 0; i < l_free_slots; i++ )
1141         {
1142             block_t *p_buffer = aout_PacketNext( p_aout,
1143                 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1144                 p_aout->format.i_rate );
1145
1146             /* If there is no audio data available and we have some buffered
1147              * already, then just wait for the next time */
1148             if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1149
1150             if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1151                             p_buffer ) != VLC_SUCCESS ) break;
1152         }
1153
1154         /* Sleep a reasonable amount of time */
1155         l_queued += (i * i_frame_siz);
1156         msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1157     }
1158
1159     /* make sure the buffer isn't playing */
1160     IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1161
1162     msg_Dbg( p_aout, "DirectSoundThread exiting" );
1163     return NULL;
1164 }
1165
1166 /*****************************************************************************
1167  * CallBackConfigNBEnum: callback to get the number of available devices
1168  *****************************************************************************/
1169 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1170                                           LPCWSTR psz_mod, LPVOID data )
1171 {
1172     int *p_nb = data;
1173
1174     (*p_nb)++;
1175     VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1176     return true;
1177 }
1178
1179 /*****************************************************************************
1180  * CallBackConfigEnum: callback to add available devices to the preferences list
1181  *****************************************************************************/
1182 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1183                                         LPCWSTR psz_mod, LPVOID data )
1184 {
1185     char **values = data;
1186
1187     while( *values != NULL )
1188         values++;
1189     *values = FromWide( psz_desc );
1190
1191     VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1192     return true;
1193 }
1194
1195 /*****************************************************************************
1196  * ReloadDirectXDevices: store the list of devices in preferences
1197  *****************************************************************************/
1198 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1199                                  char ***values, char ***descs )
1200 {
1201     int nb_devices = 0;
1202
1203     (void) psz_name;
1204
1205     HANDLE hdsound_dll = LoadLibrary(_T("DSOUND.DLL"));
1206     if( hdsound_dll == NULL )
1207     {
1208         msg_Warn( p_this, "cannot open DSOUND.DLL" );
1209         goto error;
1210     }
1211
1212     /* Get DirectSoundEnumerate */
1213     HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID) =
1214             (void *)GetProcAddress( hdsound_dll, _T("DirectSoundEnumerateW") );
1215     if( OurDirectSoundEnumerate == NULL )
1216         goto error;
1217
1218     OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1219     msg_Dbg(p_this, "found %d devices", nb_devices);
1220
1221     *values = xcalloc( nb_devices, sizeof(char *) );
1222     OurDirectSoundEnumerate(CallBackConfigEnum, *values);
1223     *descs = xcalloc( nb_devices, sizeof(char *) );
1224     OurDirectSoundEnumerate(CallBackConfigEnum, *descs);
1225 error:
1226     FreeLibrary(hdsound_dll);
1227     return nb_devices;
1228 }
1229