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