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