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