]> git.sesse.net Git - vlc/blob - modules/audio_output/directx.c
aout: split out packet-oriented output support code
[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      i_frame_size;                     /* Size in bytes of one frame */
80
81     int      i_speaker_setup;                      /* Speaker setup override */
82
83     bool     b_chan_reorder;                /* do we need channel reordering */
84     int      pi_chan_table[AOUT_CHAN_MAX];
85     uint32_t i_channel_mask;
86     uint32_t i_bits_per_sample;
87     uint32_t i_channels;
88 };
89
90 /*****************************************************************************
91  * Local prototypes.
92  *****************************************************************************/
93 static int  OpenAudio  ( vlc_object_t * );
94 static void CloseAudio ( vlc_object_t * );
95 static void Play       ( audio_output_t *, block_t * );
96
97 /* local functions */
98 static void Probe             ( audio_output_t * );
99 static int  InitDirectSound   ( audio_output_t * );
100 static int  CreateDSBuffer    ( audio_output_t *, int, int, int, int, int, bool );
101 static int  CreateDSBufferPCM ( audio_output_t *, vlc_fourcc_t*, int, int, int, bool );
102 static void DestroyDSBuffer   ( audio_output_t * );
103 static void* DirectSoundThread( void * );
104 static int  FillBuffer        ( audio_output_t *, int, aout_buffer_t * );
105
106 static int ReloadDirectXDevices( vlc_object_t *, char const *,
107                                 vlc_value_t, vlc_value_t, void * );
108
109 /* Speaker setup override options list */
110 static const char *const speaker_list[] = { "Windows default", "Mono", "Stereo",
111                                             "Quad", "5.1", "7.1" };
112 static const char *const ppsz_adev[] = {"default",  };
113 static const char *const ppsz_adev_text[] = {"default", };
114
115 /*****************************************************************************
116  * Module descriptor
117  *****************************************************************************/
118 #define DEVICE_TEXT N_("Output device")
119 #define DEVICE_LONGTEXT N_("Select your audio output device")
120
121 #define SPEAKER_TEXT N_("Speaker configuration")
122 #define SPEAKER_LONGTEXT N_("Select speaker configuration you want to use. " \
123     "This option doesn't upmix! So NO e.g. Stereo -> 5.1 conversion." )
124
125 vlc_module_begin ()
126     set_description( N_("DirectX audio output") )
127     set_shortname( "DirectX" )
128     set_capability( "audio output", 100 )
129     set_category( CAT_AUDIO )
130     set_subcategory( SUBCAT_AUDIO_AOUT )
131     add_shortcut( "directx", "directsound" )
132
133     add_string( "directx-audio-device-name", "default",
134              DEVICE_TEXT, DEVICE_LONGTEXT, false )
135         add_deprecated_alias( "directx-audio-device" ) /* Since 1.1.0 */
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->i_nb_samples = A52_FRAME_NB;
228         p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
229         p_aout->format.i_frame_length = A52_FRAME_NB;
230         p_aout->sys->i_frame_size = p_aout->format.i_bytes_per_frame;
231
232         if( CreateDSBuffer( p_aout, VLC_CODEC_SPDIFL,
233                             p_aout->format.i_physical_channels,
234                             aout_FormatNbChannels( &p_aout->format ),
235                             p_aout->format.i_rate,
236                             p_aout->sys->i_frame_size, false )
237             != VLC_SUCCESS )
238         {
239             msg_Err( p_aout, "cannot open directx audio device" );
240             free( p_aout->sys );
241             return VLC_EGENERIC;
242         }
243
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         p_aout->i_nb_samples = FRAME_SIZE;
298         aout_FormatPrepare( &p_aout->format );
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_NON_LINEAR( &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     free( p_sys );
617 }
618
619 /*****************************************************************************
620  * CallBackDirectSoundEnum: callback to enumerate available devices
621  *****************************************************************************/
622 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
623                                              LPCWSTR psz_mod, LPVOID _p_aout )
624 {
625     VLC_UNUSED( psz_mod );
626
627     audio_output_t *p_aout = (audio_output_t *)_p_aout;
628
629     char *psz_device = FromWide( psz_desc );
630     msg_Dbg( p_aout, "found device: %s", psz_device );
631
632     if( p_aout->sys->psz_device &&
633         !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
634     {
635         /* Use the device corresponding to psz_device */
636         p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
637         *p_aout->sys->p_device_guid = *p_guid;
638         msg_Dbg( p_aout, "using device: %s", psz_device );
639     }
640     else
641     {
642         /* If no default device has been selected, chose the first one */
643         if( !p_aout->sys->psz_device && p_guid )
644         {
645             p_aout->sys->psz_device = strdup( psz_device );
646             p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
647             *p_aout->sys->p_device_guid = *p_guid;
648             msg_Dbg( p_aout, "using device: %s", psz_device );
649         }
650     }
651
652     free( psz_device );
653     return true;
654 }
655
656 /*****************************************************************************
657  * InitDirectSound: handle all the gory details of DirectSound initialisation
658  *****************************************************************************/
659 static int InitDirectSound( audio_output_t *p_aout )
660 {
661     HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
662     HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
663
664     p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
665     if( p_aout->sys->hdsound_dll == NULL )
666     {
667         msg_Warn( p_aout, "cannot open DSOUND.DLL" );
668         goto error;
669     }
670
671     OurDirectSoundCreate = (void *)
672         GetProcAddress( p_aout->sys->hdsound_dll,
673                         "DirectSoundCreate" );
674     if( OurDirectSoundCreate == NULL )
675     {
676         msg_Warn( p_aout, "GetProcAddress FAILED" );
677         goto error;
678     }
679
680     /* Get DirectSoundEnumerate */
681     OurDirectSoundEnumerate = (void *)
682        GetProcAddress( p_aout->sys->hdsound_dll,
683                        "DirectSoundEnumerateW" );
684     if( OurDirectSoundEnumerate )
685     {
686         p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device-name");
687         /* Attempt enumeration */
688         if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
689                                              p_aout ) ) )
690         {
691             msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
692         }
693     }
694
695     /* Create the direct sound object */
696     if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
697                                      &p_aout->sys->p_dsobject,
698                                      NULL ) )
699     {
700         msg_Warn( p_aout, "cannot create a direct sound device" );
701         goto error;
702     }
703
704     /* Set DirectSound Cooperative level, ie what control we want over Windows
705      * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
706      * settings of the primary buffer, but also that only the sound of our
707      * application will be hearable when it will have the focus.
708      * !!! (this is not really working as intended yet because to set the
709      * cooperative level you need the window handle of your application, and
710      * I don't know of any easy way to get it. Especially since we might play
711      * sound without any video, and so what window handle should we use ???
712      * The hack for now is to use the Desktop window handle - it seems to be
713      * working */
714     if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
715                                           GetDesktopWindow(),
716                                           DSSCL_EXCLUSIVE) )
717     {
718         msg_Warn( p_aout, "cannot set direct sound cooperative level" );
719     }
720
721     return VLC_SUCCESS;
722
723  error:
724     p_aout->sys->p_dsobject = NULL;
725     if( p_aout->sys->hdsound_dll )
726     {
727         FreeLibrary( p_aout->sys->hdsound_dll );
728         p_aout->sys->hdsound_dll = NULL;
729     }
730     return VLC_EGENERIC;
731
732 }
733
734 /*****************************************************************************
735  * CreateDSBuffer: Creates a direct sound buffer of the required format.
736  *****************************************************************************
737  * This function creates the buffer we'll use to play audio.
738  * In DirectSound there are two kinds of buffers:
739  * - the primary buffer: which is the actual buffer that the soundcard plays
740  * - the secondary buffer(s): these buffers are the one actually used by
741  *    applications and DirectSound takes care of mixing them into the primary.
742  *
743  * Once you create a secondary buffer, you cannot change its format anymore so
744  * you have to release the current one and create another.
745  *****************************************************************************/
746 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
747                            int i_channels, int i_nb_channels, int i_rate,
748                            int i_bytes_per_frame, bool b_probe )
749 {
750     WAVEFORMATEXTENSIBLE waveformat;
751     DSBUFFERDESC         dsbdesc;
752     unsigned int         i;
753
754     /* First set the sound buffer format */
755     waveformat.dwChannelMask = 0;
756     for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
757     {
758         if( i_channels & pi_channels_src[i] )
759             waveformat.dwChannelMask |= pi_channels_in[i];
760     }
761
762     switch( i_format )
763     {
764     case VLC_CODEC_SPDIFL:
765         i_nb_channels = 2;
766         /* To prevent channel re-ordering */
767         waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
768         waveformat.Format.wBitsPerSample = 16;
769         waveformat.Samples.wValidBitsPerSample =
770             waveformat.Format.wBitsPerSample;
771         waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
772         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
773         break;
774
775     case VLC_CODEC_FL32:
776         waveformat.Format.wBitsPerSample = sizeof(float) * 8;
777         waveformat.Samples.wValidBitsPerSample =
778             waveformat.Format.wBitsPerSample;
779         waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
780         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
781         break;
782
783     case VLC_CODEC_S16L:
784         waveformat.Format.wBitsPerSample = 16;
785         waveformat.Samples.wValidBitsPerSample =
786             waveformat.Format.wBitsPerSample;
787         waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
788         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
789         break;
790     }
791
792     waveformat.Format.nChannels = i_nb_channels;
793     waveformat.Format.nSamplesPerSec = i_rate;
794     waveformat.Format.nBlockAlign =
795         waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
796     waveformat.Format.nAvgBytesPerSec =
797         waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
798
799     p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
800     p_aout->sys->i_channels = i_nb_channels;
801
802     /* Then fill in the direct sound descriptor */
803     memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
804     dsbdesc.dwSize = sizeof(DSBUFFERDESC);
805     dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
806                     | DSBCAPS_GLOBALFOCUS;      /* Allows background playing */
807
808     /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
809     if( i_nb_channels <= 2 )
810     {
811         waveformat.Format.cbSize = 0;
812     }
813     else
814     {
815         waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
816         waveformat.Format.cbSize =
817             sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
818
819         /* Needed for 5.1 on emu101k */
820         dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
821     }
822
823     dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame;   /* buffer size */
824     dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
825
826     if FAILED( IDirectSound_CreateSoundBuffer(
827                    p_aout->sys->p_dsobject, &dsbdesc,
828                    &p_aout->sys->p_dsbuffer, NULL) )
829     {
830         if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
831         {
832             /* Try without DSBCAPS_LOCHARDWARE */
833             dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
834             if FAILED( IDirectSound_CreateSoundBuffer(
835                    p_aout->sys->p_dsobject, &dsbdesc,
836                    &p_aout->sys->p_dsbuffer, NULL) )
837             {
838                 return VLC_EGENERIC;
839             }
840             if( !b_probe )
841                 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
842         }
843         else
844         {
845             return VLC_EGENERIC;
846         }
847     }
848
849     /* Stop here if we were just probing */
850     if( b_probe )
851     {
852         IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
853         p_aout->sys->p_dsbuffer = NULL;
854         return VLC_SUCCESS;
855     }
856
857     p_aout->sys->i_frame_size = i_bytes_per_frame;
858     p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
859     p_aout->sys->b_chan_reorder =
860         aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
861                                   waveformat.dwChannelMask, i_nb_channels,
862                                   p_aout->sys->pi_chan_table );
863
864     if( p_aout->sys->b_chan_reorder )
865     {
866         msg_Dbg( p_aout, "channel reordering needed" );
867     }
868
869     return VLC_SUCCESS;
870 }
871
872 /*****************************************************************************
873  * CreateDSBufferPCM: creates a PCM direct sound buffer.
874  *****************************************************************************
875  * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
876  * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
877  ****************************************************************************/
878 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
879                               int i_channels, int i_nb_channels, int i_rate,
880                               bool b_probe )
881 {
882     /* Float32 audio samples are not supported for 5.1 output on the emu101k */
883     if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
884         i_nb_channels > 2 ||
885         CreateDSBuffer( p_aout, VLC_CODEC_FL32,
886                         i_channels, i_nb_channels, i_rate,
887                         FRAME_SIZE * 4 * i_nb_channels, b_probe )
888         != VLC_SUCCESS )
889     {
890         if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
891                              i_channels, i_nb_channels, i_rate,
892                              FRAME_SIZE * 2 * i_nb_channels, b_probe )
893              != VLC_SUCCESS )
894         {
895             return VLC_EGENERIC;
896         }
897         else
898         {
899             *i_format = VLC_CODEC_S16L;
900             return VLC_SUCCESS;
901         }
902     }
903     else
904     {
905         *i_format = VLC_CODEC_FL32;
906         return VLC_SUCCESS;
907     }
908 }
909
910 /*****************************************************************************
911  * DestroyDSBuffer
912  *****************************************************************************
913  * This function destroys the secondary buffer.
914  *****************************************************************************/
915 static void DestroyDSBuffer( audio_output_t *p_aout )
916 {
917     if( p_aout->sys->p_dsbuffer )
918     {
919         IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
920         p_aout->sys->p_dsbuffer = NULL;
921     }
922 }
923
924 /*****************************************************************************
925  * FillBuffer: Fill in one of the direct sound frame buffers.
926  *****************************************************************************
927  * Returns VLC_SUCCESS on success.
928  *****************************************************************************/
929 static int FillBuffer( audio_output_t *p_aout, int i_frame,
930                        aout_buffer_t *p_buffer )
931 {
932     notification_thread_t *p_notif = p_aout->sys->p_notif;
933     aout_sys_t *p_sys = p_aout->sys;
934     void *p_write_position, *p_wrap_around;
935     unsigned long l_bytes1, l_bytes2;
936     HRESULT dsresult;
937
938     /* Before copying anything, we have to lock the buffer */
939     dsresult = IDirectSoundBuffer_Lock(
940                 p_sys->p_dsbuffer,                              /* DS buffer */
941                 i_frame * p_notif->i_frame_size,             /* Start offset */
942                 p_notif->i_frame_size,                    /* Number of bytes */
943                 &p_write_position,                  /* Address of lock start */
944                 &l_bytes1,       /* Count of bytes locked before wrap around */
945                 &p_wrap_around,           /* Buffer address (if wrap around) */
946                 &l_bytes2,               /* Count of bytes after wrap around */
947                 0 );                                                /* Flags */
948     if( dsresult == DSERR_BUFFERLOST )
949     {
950         IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
951         dsresult = IDirectSoundBuffer_Lock(
952                                p_sys->p_dsbuffer,
953                                i_frame * p_notif->i_frame_size,
954                                p_notif->i_frame_size,
955                                &p_write_position,
956                                &l_bytes1,
957                                &p_wrap_around,
958                                &l_bytes2,
959                                0 );
960     }
961     if( dsresult != DS_OK )
962     {
963         msg_Warn( p_aout, "cannot lock buffer" );
964         if( p_buffer ) aout_BufferFree( p_buffer );
965         return VLC_EGENERIC;
966     }
967
968     if( p_buffer == NULL )
969     {
970         memset( p_write_position, 0, l_bytes1 );
971     }
972     else
973     {
974         if( p_sys->b_chan_reorder )
975         {
976             /* Do the channel reordering here */
977             aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
978                                  p_sys->i_channels, p_sys->pi_chan_table,
979                                  p_sys->i_bits_per_sample );
980         }
981
982         vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
983         aout_BufferFree( p_buffer );
984     }
985
986     /* Now the data has been copied, unlock the buffer */
987     IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
988                                p_wrap_around, l_bytes2 );
989
990     p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
991     return VLC_SUCCESS;
992 }
993
994 /*****************************************************************************
995  * DirectSoundThread: this thread will capture play notification events.
996  *****************************************************************************
997  * We use this thread to emulate a callback mechanism. The thread probes for
998  * event notification and fills up the DS secondary buffer when needed.
999  *****************************************************************************/
1000 static void* DirectSoundThread( void *data )
1001 {
1002     notification_thread_t *p_notif = (notification_thread_t*)data;
1003     audio_output_t *p_aout = p_notif->p_aout;
1004     mtime_t last_time;
1005     int canc = vlc_savecancel ();
1006
1007     /* We don't want any resampling when using S/PDIF output */
1008     bool b_sleek = (p_aout->format.i_format == VLC_CODEC_SPDIFL);
1009
1010     msg_Dbg( p_aout, "DirectSoundThread ready" );
1011
1012     /* Wait here until Play() is called */
1013     WaitForSingleObject( p_notif->event, INFINITE );
1014
1015     if( !vlc_atomic_get( &p_notif->abort) )
1016     {
1017         HRESULT dsresult;
1018         mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1019
1020         /* start playing the buffer */
1021         dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1022                                         0,                         /* Unused */
1023                                         0,                         /* Unused */
1024                                         DSBPLAY_LOOPING );          /* Flags */
1025         if( dsresult == DSERR_BUFFERLOST )
1026         {
1027             IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1028             dsresult = IDirectSoundBuffer_Play(
1029                                             p_aout->sys->p_dsbuffer,
1030                                             0,                     /* Unused */
1031                                             0,                     /* Unused */
1032                                             DSBPLAY_LOOPING );      /* Flags */
1033         }
1034         if( dsresult != DS_OK )
1035         {
1036             msg_Err( p_aout, "cannot start playing buffer" );
1037         }
1038     }
1039     last_time = mdate();
1040
1041     while( !vlc_atomic_get( &p_notif->abort ) )
1042     {
1043         DWORD l_read;
1044         int l_queued = 0, l_free_slots;
1045         unsigned i_frame_siz = p_aout->i_nb_samples;
1046         mtime_t mtime = mdate();
1047         int i;
1048
1049         /*
1050          * Fill in as much audio data as we can in our circular buffer
1051          */
1052
1053         /* Find out current play position */
1054         if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1055                    p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1056         {
1057             msg_Err( p_aout, "GetCurrentPosition() failed!" );
1058             l_read = 0;
1059         }
1060
1061         /* Detect underruns */
1062         if( l_queued && mtime - last_time >
1063             INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1064         {
1065             msg_Dbg( p_aout, "detected underrun!" );
1066         }
1067         last_time = mtime;
1068
1069         /* Try to fill in as many frame buffers as possible */
1070         l_read /= (p_aout->format.i_bytes_per_frame /
1071             p_aout->format.i_frame_length);
1072         l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1073         if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1074         l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1075
1076         for( i = 0; i < l_free_slots; i++ )
1077         {
1078             aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout,
1079                 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1080                 p_aout->format.i_rate, b_sleek );
1081
1082             /* If there is no audio data available and we have some buffered
1083              * already, then just wait for the next time */
1084             if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1085
1086             if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1087                             p_buffer ) != VLC_SUCCESS ) break;
1088         }
1089
1090         /* Sleep a reasonable amount of time */
1091         l_queued += (i * i_frame_siz);
1092         msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1093     }
1094
1095     /* make sure the buffer isn't playing */
1096     IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1097
1098     /* free the event */
1099     CloseHandle( p_notif->event );
1100
1101     vlc_restorecancel (canc);
1102     msg_Dbg( p_aout, "DirectSoundThread exiting" );
1103     return NULL;
1104 }
1105
1106 /*****************************************************************************
1107  * CallBackConfigNBEnum: callback to get the number of available devices
1108  *****************************************************************************/
1109 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1110                                              LPCWSTR psz_mod, LPVOID p_nb )
1111 {
1112     VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1113
1114     int * a = (int *)p_nb;
1115     (*a)++;
1116     return true;
1117 }
1118
1119 /*****************************************************************************
1120  * CallBackConfigEnum: callback to add available devices to the preferences list
1121  *****************************************************************************/
1122 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1123                                              LPCWSTR psz_mod, LPVOID _p_item )
1124 {
1125     VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1126
1127     module_config_t *p_item = (module_config_t *) _p_item;
1128
1129     p_item->ppsz_list[p_item->i_list] = FromWide( psz_desc );
1130     p_item->ppsz_list_text[p_item->i_list] = FromWide( psz_desc );
1131     p_item->i_list++;
1132     return true;
1133 }
1134
1135 /*****************************************************************************
1136  * ReloadDirectXDevices: store the list of devices in preferences
1137  *****************************************************************************/
1138 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1139                                  vlc_value_t newval, vlc_value_t oldval, void *data )
1140 {
1141     VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1142
1143     module_config_t *p_item = config_FindConfig( p_this, psz_name );
1144     if( !p_item ) return VLC_SUCCESS;
1145
1146     /* Clear-up the current list */
1147     if( p_item->i_list )
1148     {
1149         for( int i = 0; i < p_item->i_list; i++ )
1150         {
1151             free((char *)(p_item->ppsz_list[i]) );
1152             free((char *)(p_item->ppsz_list_text[i]) );
1153         }
1154     }
1155
1156     HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
1157
1158     HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1159     if( hdsound_dll == NULL )
1160     {
1161         msg_Warn( p_this, "cannot open DSOUND.DLL" );
1162         return VLC_SUCCESS;
1163     }
1164
1165     /* Get DirectSoundEnumerate */
1166     OurDirectSoundEnumerate = (void *)
1167                     GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1168
1169     if( OurDirectSoundEnumerate == NULL )
1170         goto error;
1171
1172     int nb_devices = 0;
1173     OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1174     msg_Dbg(p_this,"found %d devices", nb_devices);
1175
1176     p_item->ppsz_list = xrealloc( p_item->ppsz_list,
1177                                   nb_devices * sizeof(char *) );
1178     p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
1179                                   nb_devices * sizeof(char *) );
1180
1181     p_item->i_list = 0;
1182     OurDirectSoundEnumerate(CallBackConfigEnum, p_item);
1183
1184     /* Signal change to the interface */
1185     p_item->b_dirty = true;
1186
1187 error:
1188     FreeLibrary(hdsound_dll);
1189
1190     return VLC_SUCCESS;
1191 }
1192