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