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