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