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