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