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