]> git.sesse.net Git - vlc/blob - modules/audio_output/directx.c
Direct Aout: try to fix unresonspiveness of volume change
[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, block_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         p_aout->pf_volume_set = NULL;
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 }
555
556 /*****************************************************************************
557  * Play: we'll start playing the directsound buffer here because at least here
558  *       we know the first buffer has been put in the aout fifo and we also
559  *       know its date.
560  *****************************************************************************/
561 static void Play( audio_output_t *p_aout, block_t *p_buffer )
562 {
563     /* get the playing date of the first aout buffer */
564     p_aout->sys->notif.start_date = p_buffer->i_pts;
565
566     /* fill in the first samples (zeroes) */
567     FillBuffer( p_aout, 0, NULL );
568
569     /* wake up the audio output thread */
570     SetEvent( p_aout->sys->notif.event );
571
572     aout_PacketPlay( p_aout, p_buffer );
573     p_aout->pf_play = aout_PacketPlay;
574 }
575
576 /*****************************************************************************
577  * VolumeSet: change audio device volume
578  *****************************************************************************/
579 static int VolumeSet( audio_output_t *p_aout, float vol, bool mute )
580 {
581     aout_sys_t *sys = p_aout->sys;
582
583     /* Convert UI volume to linear factor (cube) */
584     vol = vol * vol * vol;
585
586     /* millibels from linear amplification */
587     LONG mb = lroundf(2000.f * log10f(vol));
588
589     /* Clamp to allowed DirectSound range */
590     static_assert( DSBVOLUME_MIN < DSBVOLUME_MAX, "DSBVOLUME_* confused" );
591     if( mb >= DSBVOLUME_MAX )
592         mb = DSBVOLUME_MAX;
593     if( mb <= DSBVOLUME_MIN )
594         mb = DSBVOLUME_MIN;
595
596     InterlockedExchange(&sys->volume, mute ? DSBVOLUME_MIN : mb);
597
598     /* Convert back to UI volume */
599     vol = cbrtf(powf(10.f, ((float)mb) / -2000.f));
600     aout_VolumeHardSet( p_aout, vol, mute );
601     return 0;
602 }
603
604 /*****************************************************************************
605  * CloseAudio: close the audio device
606  *****************************************************************************/
607 static void CloseAudio( vlc_object_t *p_this )
608 {
609     audio_output_t * p_aout = (audio_output_t *)p_this;
610     aout_sys_t *p_sys = p_aout->sys;
611
612     msg_Dbg( p_aout, "closing audio device" );
613
614     /* kill the position notification thread, if any */
615     if( p_sys->notif.event != NULL )
616     {
617         vlc_atomic_set(&p_aout->sys->notif.abort, 1);
618         /* wake up the audio thread if needed */
619         if( p_aout->pf_play == Play )
620             SetEvent( p_sys->notif.event );
621
622         vlc_join( p_sys->notif.thread, NULL );
623         CloseHandle( p_sys->notif.event );
624         aout_PacketDestroy( p_aout );
625     }
626
627     /* release the secondary buffer */
628     DestroyDSBuffer( p_aout );
629
630     /* finally release the DirectSound object */
631     if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
632
633     /* free DSOUND.DLL */
634     if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
635
636     free( p_aout->sys->p_device_guid );
637     free( p_sys );
638 }
639
640 /*****************************************************************************
641  * CallBackDirectSoundEnum: callback to enumerate available devices
642  *****************************************************************************/
643 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCWSTR psz_desc,
644                                              LPCWSTR psz_mod, LPVOID _p_aout )
645 {
646     VLC_UNUSED( psz_mod );
647
648     audio_output_t *p_aout = (audio_output_t *)_p_aout;
649
650     char *psz_device = FromWide( psz_desc );
651     msg_Dbg( p_aout, "found device: %s", psz_device );
652
653     if( p_aout->sys->psz_device &&
654         !strcmp(p_aout->sys->psz_device, psz_device) && p_guid )
655     {
656         /* Use the device corresponding to psz_device */
657         p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
658         *p_aout->sys->p_device_guid = *p_guid;
659         msg_Dbg( p_aout, "using device: %s", psz_device );
660     }
661     else
662     {
663         /* If no default device has been selected, chose the first one */
664         if( !p_aout->sys->psz_device && p_guid )
665         {
666             p_aout->sys->psz_device = strdup( psz_device );
667             p_aout->sys->p_device_guid = malloc( sizeof( GUID ) );
668             *p_aout->sys->p_device_guid = *p_guid;
669             msg_Dbg( p_aout, "using device: %s", psz_device );
670         }
671     }
672
673     free( psz_device );
674     return true;
675 }
676
677 /*****************************************************************************
678  * InitDirectSound: handle all the gory details of DirectSound initialisation
679  *****************************************************************************/
680 static int InitDirectSound( audio_output_t *p_aout )
681 {
682     HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
683     HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
684
685     p_aout->sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
686     if( p_aout->sys->hdsound_dll == NULL )
687     {
688         msg_Warn( p_aout, "cannot open DSOUND.DLL" );
689         goto error;
690     }
691
692     OurDirectSoundCreate = (void *)
693         GetProcAddress( p_aout->sys->hdsound_dll,
694                         "DirectSoundCreate" );
695     if( OurDirectSoundCreate == NULL )
696     {
697         msg_Warn( p_aout, "GetProcAddress FAILED" );
698         goto error;
699     }
700
701     /* Get DirectSoundEnumerate */
702     OurDirectSoundEnumerate = (void *)
703        GetProcAddress( p_aout->sys->hdsound_dll,
704                        "DirectSoundEnumerateW" );
705     if( OurDirectSoundEnumerate )
706     {
707         p_aout->sys->psz_device = var_InheritString(p_aout, "directx-audio-device");
708         /* Attempt enumeration */
709         if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
710                                              p_aout ) ) )
711         {
712             msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
713         }
714     }
715
716     /* Create the direct sound object */
717     if FAILED( OurDirectSoundCreate( p_aout->sys->p_device_guid,
718                                      &p_aout->sys->p_dsobject,
719                                      NULL ) )
720     {
721         msg_Warn( p_aout, "cannot create a direct sound device" );
722         goto error;
723     }
724
725     /* Set DirectSound Cooperative level, ie what control we want over Windows
726      * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
727      * settings of the primary buffer, but also that only the sound of our
728      * application will be hearable when it will have the focus.
729      * !!! (this is not really working as intended yet because to set the
730      * cooperative level you need the window handle of your application, and
731      * I don't know of any easy way to get it. Especially since we might play
732      * sound without any video, and so what window handle should we use ???
733      * The hack for now is to use the Desktop window handle - it seems to be
734      * working */
735     if( IDirectSound_SetCooperativeLevel( p_aout->sys->p_dsobject,
736                                           GetDesktopWindow(),
737                                           DSSCL_EXCLUSIVE) )
738     {
739         msg_Warn( p_aout, "cannot set direct sound cooperative level" );
740     }
741
742     return VLC_SUCCESS;
743
744  error:
745     p_aout->sys->p_dsobject = NULL;
746     if( p_aout->sys->hdsound_dll )
747     {
748         FreeLibrary( p_aout->sys->hdsound_dll );
749         p_aout->sys->hdsound_dll = NULL;
750     }
751     return VLC_EGENERIC;
752
753 }
754
755 /*****************************************************************************
756  * CreateDSBuffer: Creates a direct sound buffer of the required format.
757  *****************************************************************************
758  * This function creates the buffer we'll use to play audio.
759  * In DirectSound there are two kinds of buffers:
760  * - the primary buffer: which is the actual buffer that the soundcard plays
761  * - the secondary buffer(s): these buffers are the one actually used by
762  *    applications and DirectSound takes care of mixing them into the primary.
763  *
764  * Once you create a secondary buffer, you cannot change its format anymore so
765  * you have to release the current one and create another.
766  *****************************************************************************/
767 static int CreateDSBuffer( audio_output_t *p_aout, int i_format,
768                            int i_channels, int i_nb_channels, int i_rate,
769                            int i_bytes_per_frame, bool b_probe )
770 {
771     WAVEFORMATEXTENSIBLE waveformat;
772     DSBUFFERDESC         dsbdesc;
773     unsigned int         i;
774
775     /* First set the sound buffer format */
776     waveformat.dwChannelMask = 0;
777     for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
778     {
779         if( i_channels & pi_channels_src[i] )
780             waveformat.dwChannelMask |= pi_channels_in[i];
781     }
782
783     switch( i_format )
784     {
785     case VLC_CODEC_SPDIFL:
786         i_nb_channels = 2;
787         /* To prevent channel re-ordering */
788         waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
789         waveformat.Format.wBitsPerSample = 16;
790         waveformat.Samples.wValidBitsPerSample =
791             waveformat.Format.wBitsPerSample;
792         waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
793         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
794         break;
795
796     case VLC_CODEC_FL32:
797         waveformat.Format.wBitsPerSample = sizeof(float) * 8;
798         waveformat.Samples.wValidBitsPerSample =
799             waveformat.Format.wBitsPerSample;
800         waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
801         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
802         break;
803
804     case VLC_CODEC_S16L:
805         waveformat.Format.wBitsPerSample = 16;
806         waveformat.Samples.wValidBitsPerSample =
807             waveformat.Format.wBitsPerSample;
808         waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
809         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
810         break;
811     }
812
813     waveformat.Format.nChannels = i_nb_channels;
814     waveformat.Format.nSamplesPerSec = i_rate;
815     waveformat.Format.nBlockAlign =
816         waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
817     waveformat.Format.nAvgBytesPerSec =
818         waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
819
820     p_aout->sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
821     p_aout->sys->i_channels = i_nb_channels;
822
823     /* Then fill in the direct sound descriptor */
824     memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
825     dsbdesc.dwSize = sizeof(DSBUFFERDESC);
826     dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
827                     | DSBCAPS_GLOBALFOCUS       /* Allows background playing */
828                     | DSBCAPS_CTRLVOLUME;       /* Allows volume control */
829
830     /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
831     if( i_nb_channels <= 2 )
832     {
833         waveformat.Format.cbSize = 0;
834     }
835     else
836     {
837         waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
838         waveformat.Format.cbSize =
839             sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
840
841         /* Needed for 5.1 on emu101k */
842         dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
843     }
844
845     dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame;   /* buffer size */
846     dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
847
848     if FAILED( IDirectSound_CreateSoundBuffer(
849                    p_aout->sys->p_dsobject, &dsbdesc,
850                    &p_aout->sys->p_dsbuffer, NULL) )
851     {
852         if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
853         {
854             /* Try without DSBCAPS_LOCHARDWARE */
855             dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
856             if FAILED( IDirectSound_CreateSoundBuffer(
857                    p_aout->sys->p_dsobject, &dsbdesc,
858                    &p_aout->sys->p_dsbuffer, NULL) )
859             {
860                 return VLC_EGENERIC;
861             }
862             if( !b_probe )
863                 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
864         }
865         else
866         {
867             return VLC_EGENERIC;
868         }
869     }
870
871     /* Stop here if we were just probing */
872     if( b_probe )
873     {
874         IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
875         p_aout->sys->p_dsbuffer = NULL;
876         return VLC_SUCCESS;
877     }
878
879     p_aout->sys->i_frame_size = i_bytes_per_frame;
880     p_aout->sys->i_channel_mask = waveformat.dwChannelMask;
881     p_aout->sys->b_chan_reorder =
882         aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
883                                   waveformat.dwChannelMask, i_nb_channels,
884                                   p_aout->sys->pi_chan_table );
885
886     if( p_aout->sys->b_chan_reorder )
887     {
888         msg_Dbg( p_aout, "channel reordering needed" );
889     }
890
891     return VLC_SUCCESS;
892 }
893
894 /*****************************************************************************
895  * CreateDSBufferPCM: creates a PCM direct sound buffer.
896  *****************************************************************************
897  * We first try to create a WAVE_FORMAT_IEEE_FLOAT buffer if supported by
898  * the hardware, otherwise we create a WAVE_FORMAT_PCM buffer.
899  ****************************************************************************/
900 static int CreateDSBufferPCM( audio_output_t *p_aout, vlc_fourcc_t *i_format,
901                               int i_channels, int i_nb_channels, int i_rate,
902                               bool b_probe )
903 {
904     /* Float32 audio samples are not supported for 5.1 output on the emu101k */
905     if( !var_GetBool( p_aout, "directx-audio-float32" ) ||
906         i_nb_channels > 2 ||
907         CreateDSBuffer( p_aout, VLC_CODEC_FL32,
908                         i_channels, i_nb_channels, i_rate,
909                         FRAME_SIZE * 4 * i_nb_channels, b_probe )
910         != VLC_SUCCESS )
911     {
912         if ( CreateDSBuffer( p_aout, VLC_CODEC_S16L,
913                              i_channels, i_nb_channels, i_rate,
914                              FRAME_SIZE * 2 * i_nb_channels, b_probe )
915              != VLC_SUCCESS )
916         {
917             return VLC_EGENERIC;
918         }
919         else
920         {
921             *i_format = VLC_CODEC_S16L;
922             return VLC_SUCCESS;
923         }
924     }
925     else
926     {
927         *i_format = VLC_CODEC_FL32;
928         return VLC_SUCCESS;
929     }
930 }
931
932 /*****************************************************************************
933  * DestroyDSBuffer
934  *****************************************************************************
935  * This function destroys the secondary buffer.
936  *****************************************************************************/
937 static void DestroyDSBuffer( audio_output_t *p_aout )
938 {
939     if( p_aout->sys->p_dsbuffer )
940     {
941         IDirectSoundBuffer_Release( p_aout->sys->p_dsbuffer );
942         p_aout->sys->p_dsbuffer = NULL;
943     }
944 }
945
946 /*****************************************************************************
947  * FillBuffer: Fill in one of the direct sound frame buffers.
948  *****************************************************************************
949  * Returns VLC_SUCCESS on success.
950  *****************************************************************************/
951 static int FillBuffer( audio_output_t *p_aout, int i_frame, block_t *p_buffer )
952 {
953     aout_sys_t *p_sys = p_aout->sys;
954     notification_thread_t *p_notif = &p_sys->notif;
955     void *p_write_position, *p_wrap_around;
956     unsigned long l_bytes1, l_bytes2;
957     HRESULT dsresult;
958
959     /* Before copying anything, we have to lock the buffer */
960     dsresult = IDirectSoundBuffer_Lock(
961                 p_sys->p_dsbuffer,                              /* DS buffer */
962                 i_frame * p_notif->i_frame_size,             /* Start offset */
963                 p_notif->i_frame_size,                    /* Number of bytes */
964                 &p_write_position,                  /* Address of lock start */
965                 &l_bytes1,       /* Count of bytes locked before wrap around */
966                 &p_wrap_around,           /* Buffer address (if wrap around) */
967                 &l_bytes2,               /* Count of bytes after wrap around */
968                 0 );                                                /* Flags */
969     if( dsresult == DSERR_BUFFERLOST )
970     {
971         IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
972         dsresult = IDirectSoundBuffer_Lock(
973                                p_sys->p_dsbuffer,
974                                i_frame * p_notif->i_frame_size,
975                                p_notif->i_frame_size,
976                                &p_write_position,
977                                &l_bytes1,
978                                &p_wrap_around,
979                                &l_bytes2,
980                                0 );
981     }
982     if( dsresult != DS_OK )
983     {
984         msg_Warn( p_aout, "cannot lock buffer" );
985         if( p_buffer ) block_Release( p_buffer );
986         return VLC_EGENERIC;
987     }
988
989     if( p_buffer == NULL )
990     {
991         memset( p_write_position, 0, l_bytes1 );
992     }
993     else
994     {
995         if( p_sys->b_chan_reorder )
996         {
997             /* Do the channel reordering here */
998             aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
999                                  p_sys->i_channels, p_sys->pi_chan_table,
1000                                  p_sys->i_bits_per_sample );
1001         }
1002
1003         vlc_memcpy( p_write_position, p_buffer->p_buffer, l_bytes1 );
1004         block_Release( p_buffer );
1005     }
1006
1007     /* Now the data has been copied, unlock the buffer */
1008     IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
1009                                p_wrap_around, l_bytes2 );
1010
1011     p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
1012     return VLC_SUCCESS;
1013 }
1014
1015 /*****************************************************************************
1016  * DirectSoundThread: this thread will capture play notification events.
1017  *****************************************************************************
1018  * We use this thread to emulate a callback mechanism. The thread probes for
1019  * event notification and fills up the DS secondary buffer when needed.
1020  *****************************************************************************/
1021 static void* DirectSoundThread( void *data )
1022 {
1023     audio_output_t *p_aout = (audio_output_t *)data;
1024     notification_thread_t *p_notif = &p_aout->sys->notif;
1025     mtime_t last_time;
1026
1027     msg_Dbg( p_aout, "DirectSoundThread ready" );
1028
1029     /* Wait here until Play() is called */
1030     WaitForSingleObject( p_notif->event, INFINITE );
1031
1032     if( !vlc_atomic_get( &p_notif->abort) )
1033     {
1034         HRESULT dsresult;
1035         mwait( p_notif->start_date - AOUT_MAX_PTS_ADVANCE / 2 );
1036
1037         /* start playing the buffer */
1038         dsresult = IDirectSoundBuffer_Play( p_aout->sys->p_dsbuffer,
1039                                         0,                         /* Unused */
1040                                         0,                         /* Unused */
1041                                         DSBPLAY_LOOPING );          /* Flags */
1042         if( dsresult == DSERR_BUFFERLOST )
1043         {
1044             IDirectSoundBuffer_Restore( p_aout->sys->p_dsbuffer );
1045             dsresult = IDirectSoundBuffer_Play(
1046                                             p_aout->sys->p_dsbuffer,
1047                                             0,                     /* Unused */
1048                                             0,                     /* Unused */
1049                                             DSBPLAY_LOOPING );      /* Flags */
1050         }
1051         if( dsresult != DS_OK )
1052         {
1053             msg_Err( p_aout, "cannot start playing buffer" );
1054         }
1055     }
1056     last_time = mdate();
1057
1058     while( !vlc_atomic_get( &p_notif->abort ) )
1059     {
1060         DWORD l_read;
1061         int l_queued = 0, l_free_slots;
1062         unsigned i_frame_siz = p_aout->sys->packet.samples;
1063         mtime_t mtime = mdate();
1064         int i;
1065
1066         /* Update volume if required */
1067         LONG volume = InterlockedExchange( &p_aout->sys->volume, -1 );
1068         if( unlikely(volume != -1) )
1069             IDirectSoundBuffer_SetVolume( p_aout->sys->p_dsbuffer, volume );
1070
1071         /*
1072          * Fill in as much audio data as we can in our circular buffer
1073          */
1074
1075         /* Find out current play position */
1076         if FAILED( IDirectSoundBuffer_GetCurrentPosition(
1077                    p_aout->sys->p_dsbuffer, &l_read, NULL ) )
1078         {
1079             msg_Err( p_aout, "GetCurrentPosition() failed!" );
1080             l_read = 0;
1081         }
1082
1083         /* Detect underruns */
1084         if( l_queued && mtime - last_time >
1085             INT64_C(1000000) * l_queued / p_aout->format.i_rate )
1086         {
1087             msg_Dbg( p_aout, "detected underrun!" );
1088         }
1089         last_time = mtime;
1090
1091         /* Try to fill in as many frame buffers as possible */
1092         l_read /= (p_aout->format.i_bytes_per_frame /
1093             p_aout->format.i_frame_length);
1094         l_queued = p_notif->i_write_slot * i_frame_siz - l_read;
1095         if( l_queued < 0 ) l_queued += (i_frame_siz * FRAMES_NUM);
1096         l_free_slots = (FRAMES_NUM * i_frame_siz - l_queued) / i_frame_siz;
1097
1098         for( i = 0; i < l_free_slots; i++ )
1099         {
1100             block_t *p_buffer = aout_PacketNext( p_aout,
1101                 mtime + INT64_C(1000000) * (i * i_frame_siz + l_queued) /
1102                 p_aout->format.i_rate );
1103
1104             /* If there is no audio data available and we have some buffered
1105              * already, then just wait for the next time */
1106             if( !p_buffer && (i || l_queued / i_frame_siz) ) break;
1107
1108             if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
1109                             p_buffer ) != VLC_SUCCESS ) break;
1110         }
1111
1112         /* Sleep a reasonable amount of time */
1113         l_queued += (i * i_frame_siz);
1114         msleep( INT64_C(1000000) * l_queued / p_aout->format.i_rate / 2 );
1115     }
1116
1117     /* make sure the buffer isn't playing */
1118     IDirectSoundBuffer_Stop( p_aout->sys->p_dsbuffer );
1119
1120     msg_Dbg( p_aout, "DirectSoundThread exiting" );
1121     return NULL;
1122 }
1123
1124 /*****************************************************************************
1125  * CallBackConfigNBEnum: callback to get the number of available devices
1126  *****************************************************************************/
1127 static int CALLBACK CallBackConfigNBEnum( LPGUID p_guid, LPCWSTR psz_desc,
1128                                              LPCWSTR psz_mod, LPVOID p_nb )
1129 {
1130     VLC_UNUSED( psz_mod ); VLC_UNUSED( psz_desc ); VLC_UNUSED( p_guid );
1131
1132     int * a = (int *)p_nb;
1133     (*a)++;
1134     return true;
1135 }
1136
1137 /*****************************************************************************
1138  * CallBackConfigEnum: callback to add available devices to the preferences list
1139  *****************************************************************************/
1140 static int CALLBACK CallBackConfigEnum( LPGUID p_guid, LPCWSTR psz_desc,
1141                                              LPCWSTR psz_mod, LPVOID _p_item )
1142 {
1143     VLC_UNUSED( psz_mod ); VLC_UNUSED( p_guid );
1144
1145     module_config_t *p_item = (module_config_t *) _p_item;
1146
1147     p_item->ppsz_list[p_item->i_list] = FromWide( psz_desc );
1148     p_item->ppsz_list_text[p_item->i_list] = FromWide( psz_desc );
1149     p_item->i_list++;
1150     return true;
1151 }
1152
1153 /*****************************************************************************
1154  * ReloadDirectXDevices: store the list of devices in preferences
1155  *****************************************************************************/
1156 static int ReloadDirectXDevices( vlc_object_t *p_this, char const *psz_name,
1157                                  vlc_value_t newval, vlc_value_t oldval, void *data )
1158 {
1159     VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
1160
1161     module_config_t *p_item = config_FindConfig( p_this, psz_name );
1162     if( !p_item ) return VLC_SUCCESS;
1163
1164     /* Clear-up the current list */
1165     if( p_item->i_list )
1166     {
1167         for( int i = 0; i < p_item->i_list; i++ )
1168         {
1169             free((char *)(p_item->ppsz_list[i]) );
1170             free((char *)(p_item->ppsz_list_text[i]) );
1171         }
1172     }
1173
1174     HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKW, LPVOID);
1175
1176     HANDLE hdsound_dll = LoadLibrary("DSOUND.DLL");
1177     if( hdsound_dll == NULL )
1178     {
1179         msg_Warn( p_this, "cannot open DSOUND.DLL" );
1180         return VLC_SUCCESS;
1181     }
1182
1183     /* Get DirectSoundEnumerate */
1184     OurDirectSoundEnumerate = (void *)
1185                     GetProcAddress( hdsound_dll, "DirectSoundEnumerateW" );
1186
1187     if( OurDirectSoundEnumerate == NULL )
1188         goto error;
1189
1190     int nb_devices = 0;
1191     OurDirectSoundEnumerate(CallBackConfigNBEnum, &nb_devices);
1192     msg_Dbg(p_this,"found %d devices", nb_devices);
1193
1194     p_item->ppsz_list = xrealloc( p_item->ppsz_list,
1195                                   nb_devices * sizeof(char *) );
1196     p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
1197                                   nb_devices * sizeof(char *) );
1198
1199     p_item->i_list = 0;
1200     OurDirectSoundEnumerate(CallBackConfigEnum, p_item);
1201
1202 error:
1203     FreeLibrary(hdsound_dll);
1204
1205     return VLC_SUCCESS;
1206 }
1207