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