]> git.sesse.net Git - vlc/blob - modules/audio_output/directx.c
* modules/audio_output/directx.c: forgot an aout_BufferFree().
[vlc] / modules / audio_output / directx.c
1 /*****************************************************************************
2  * directx.c: Windows DirectX audio output method
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: directx.c,v 1.7 2002/11/01 15:43:55 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28 #include <fcntl.h>                                       /* open(), O_WRONLY */
29 #include <string.h>                                            /* strerror() */
30
31 #include <stdlib.h>                            /* calloc(), malloc(), free() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/aout.h>
35 #include "aout_internal.h"
36
37 #include <windows.h>
38 #include <mmsystem.h>
39 #include <dsound.h>
40
41 #define FRAME_SIZE 2048              /* The size is in samples, not in bytes */
42 #define FRAMES_NUM 4
43
44 /* frame buffer status */
45 #define FRAME_QUEUED 0
46 #define FRAME_EMPTY 1
47
48 /*****************************************************************************
49  * DirectSound GUIDs.
50  * Defining them here allows us to get rid of the dxguid library during
51  * the linking stage.
52  *****************************************************************************/
53 #include <initguid.h>
54 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf, 0x8, 0x0, 0xa0, 0xc9, 0x25, 0xcd, 0x16);
55
56 /*****************************************************************************
57  * Useful macros
58  *****************************************************************************/
59 #ifndef WAVE_FORMAT_IEEE_FLOAT
60 #   define WAVE_FORMAT_IEEE_FLOAT 0x0003
61 #endif
62
63 /*****************************************************************************
64  * notification_thread_t: DirectX event thread
65  *****************************************************************************/
66 typedef struct notification_thread_t
67 {
68     VLC_COMMON_MEMBERS
69
70     aout_instance_t * p_aout;
71     int i_frame_status[FRAMES_NUM];           /* status of each frame buffer */
72     DSBPOSITIONNOTIFY p_events[FRAMES_NUM];      /* play notification events */
73     int i_frame_size;                         /* Size in bytes of one frame */
74
75     mtime_t start_date;
76
77 } notification_thread_t;
78
79 /*****************************************************************************
80  * aout_sys_t: directx audio output method descriptor
81  *****************************************************************************
82  * This structure is part of the audio output thread descriptor.
83  * It describes the direct sound specific properties of an audio device.
84  *****************************************************************************/
85 struct aout_sys_t
86 {
87     LPDIRECTSOUND       p_dsobject;              /* main Direct Sound object */
88
89     LPDIRECTSOUNDBUFFER p_dsbuffer_primary;     /* the actual sound card buffer
90                                                    (not used directly) */
91
92     LPDIRECTSOUNDBUFFER p_dsbuffer;   /* the sound buffer we use (direct sound
93                                        * takes care of mixing all the
94                                        * secondary buffers into the primary) */
95
96     LPDIRECTSOUNDNOTIFY p_dsnotify;         /* the position notify interface */
97
98     HINSTANCE           hdsound_dll;      /* handle of the opened dsound dll */
99
100     notification_thread_t * p_notif;                 /* DirectSoundThread id */
101
102     int b_playing;                                         /* playing status */
103 };
104
105 /*****************************************************************************
106  * Local prototypes.
107  *****************************************************************************/
108 static int  OpenAudio  ( vlc_object_t * );
109 static void CloseAudio ( vlc_object_t * );
110
111 static void Play       ( aout_instance_t * );
112
113 /* local functions */
114 static int  DirectxCreateSecondaryBuffer ( aout_instance_t * );
115 static void DirectxDestroySecondaryBuffer( aout_instance_t * );
116 static int  DirectxInitDSound            ( aout_instance_t * );
117 static void DirectSoundThread            ( notification_thread_t * );
118 static int  DirectxFillBuffer            ( aout_instance_t *, int,
119                                            aout_buffer_t * );
120
121 /*****************************************************************************
122  * Module descriptor
123  *****************************************************************************/
124 vlc_module_begin();
125     set_description( _("DirectX audio module") );
126     set_capability( "audio output", 100 );
127     add_shortcut( "directx" );
128     set_callbacks( OpenAudio, CloseAudio );
129 vlc_module_end();
130
131 /*****************************************************************************
132  * OpenAudio: open the audio device
133  *****************************************************************************
134  * This function opens and setups Direct Sound.
135  *****************************************************************************/
136 static int OpenAudio( vlc_object_t *p_this )
137 {
138     aout_instance_t * p_aout = (aout_instance_t *)p_this;
139     HRESULT dsresult;
140     DSBUFFERDESC dsbuffer_desc;
141     int i;
142
143     msg_Dbg( p_aout, "Open" );
144
145    /* Allocate structure */
146     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
147     if( p_aout->output.p_sys == NULL )
148     {
149         msg_Err( p_aout, "out of memory" );
150         return VLC_EGENERIC;
151     }
152
153     /* Initialize some variables */
154     p_aout->output.p_sys->p_dsobject = NULL;
155     p_aout->output.p_sys->p_dsbuffer_primary = NULL;
156     p_aout->output.p_sys->p_dsbuffer = NULL;
157     p_aout->output.p_sys->p_dsnotify = NULL;
158     p_aout->output.p_sys->p_notif = NULL;
159     p_aout->output.p_sys->b_playing = 0;
160
161     p_aout->output.pf_play = Play;
162     aout_VolumeSoftInit( p_aout );
163
164     /* Initialise DirectSound */
165     if( DirectxInitDSound( p_aout ) )
166     {
167         msg_Err( p_aout, "cannot initialize DirectSound" );
168         goto error;
169     }
170
171     /* Obtain (not create) Direct Sound primary buffer */
172     memset( &dsbuffer_desc, 0, sizeof(DSBUFFERDESC) );
173     dsbuffer_desc.dwSize = sizeof(DSBUFFERDESC);
174     dsbuffer_desc.dwFlags = DSBCAPS_PRIMARYBUFFER;
175     msg_Warn( p_aout, "create direct sound primary buffer" );
176     dsresult = IDirectSound_CreateSoundBuffer(p_aout->output.p_sys->p_dsobject,
177                                      &dsbuffer_desc,
178                                      &p_aout->output.p_sys->p_dsbuffer_primary,
179                                      NULL);
180     if( dsresult != DS_OK )
181     {
182         msg_Err( p_aout, "cannot create direct sound primary buffer" );
183         goto error;
184     }
185
186     /* Now we need to setup DirectSound play notification */
187     p_aout->output.p_sys->p_notif =
188         vlc_object_create( p_aout, sizeof(notification_thread_t) );
189     p_aout->output.p_sys->p_notif->p_aout = p_aout;
190
191     /* first we need to create the notification events */
192     for( i = 0; i < FRAMES_NUM; i++ )
193         p_aout->output.p_sys->p_notif->p_events[i].hEventNotify =
194             CreateEvent( NULL, FALSE, FALSE, NULL );
195
196     /* then create a new secondary buffer */
197     p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
198     if( DirectxCreateSecondaryBuffer( p_aout ) )
199     {
200         msg_Err( p_aout, "cannot create WAVE_FORMAT_IEEE_FLOAT buffer" );
201
202         p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
203         if( DirectxCreateSecondaryBuffer( p_aout ) )
204         {
205             msg_Err( p_aout, "cannot create WAVE_FORMAT_PCM buffer" );
206             return 1;
207         }
208     }
209
210     /* then launch the notification thread */
211     msg_Dbg( p_aout, "creating DirectSoundThread" );
212     if( vlc_thread_create( p_aout->output.p_sys->p_notif,
213                            "DirectSound Notification Thread",
214                            DirectSoundThread,
215                            THREAD_PRIORITY_TIME_CRITICAL, 1 ) )
216     {
217         msg_Err( p_aout, "cannot create DirectSoundThread" );
218         goto error;
219     }
220
221     vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
222
223     return 0;
224
225  error:
226     CloseAudio( VLC_OBJECT(p_aout) );
227     return VLC_EGENERIC;
228 }
229
230 /*****************************************************************************
231  * Play: we'll start playing the directsound buffer here because at least here
232  *       we know the first buffer has been put in the aout fifo and we also
233  *       know its date.
234  *****************************************************************************/
235 static void Play( aout_instance_t *p_aout )
236 {
237     if( !p_aout->output.p_sys->b_playing )
238     {
239         aout_buffer_t *p_buffer;
240
241         p_aout->output.p_sys->b_playing = 1;
242
243         /* get the playing date of the first aout buffer */
244         p_aout->output.p_sys->p_notif->start_date =
245             aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
246
247         /* fill in the first samples */
248         p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
249         DirectxFillBuffer( p_aout, 0, p_buffer );
250
251         /* wake up the audio output thread */
252         SetEvent( p_aout->output.p_sys->p_notif->p_events[0].hEventNotify );
253     }
254 }
255
256 /*****************************************************************************
257  * CloseAudio: close the audio device
258  *****************************************************************************/
259 static void CloseAudio( vlc_object_t *p_this )
260 {
261     aout_instance_t * p_aout = (aout_instance_t *)p_this;
262
263     msg_Dbg( p_aout, "Close" );
264
265     /* kill the position notification thread, if any */
266     if( p_aout->output.p_sys->p_notif )
267     {
268         vlc_object_detach( p_aout->output.p_sys->p_notif );
269         if( p_aout->output.p_sys->p_notif->b_thread )
270         {
271             p_aout->output.p_sys->p_notif->b_die = 1;
272
273             if( !p_aout->output.p_sys->b_playing )
274                 /* wake up the audio thread */
275                 SetEvent(
276                     p_aout->output.p_sys->p_notif->p_events[0].hEventNotify );
277
278             vlc_thread_join( p_aout->output.p_sys->p_notif );
279         }
280         vlc_object_destroy( p_aout->output.p_sys->p_notif );
281     }
282
283     /* release the secondary buffer */
284     DirectxDestroySecondaryBuffer( p_aout );
285
286     /* then release the primary buffer */
287     if( p_aout->output.p_sys->p_dsbuffer_primary )
288         IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer_primary );
289
290     /* finally release the DirectSound object */
291     if( p_aout->output.p_sys->p_dsobject )
292         IDirectSound_Release( p_aout->output.p_sys->p_dsobject );
293     
294     /* free DSOUND.DLL */
295     if( p_aout->output.p_sys->hdsound_dll )
296        FreeLibrary( p_aout->output.p_sys->hdsound_dll );
297
298     free( p_aout->output.p_sys );
299 }
300
301 /*****************************************************************************
302  * DirectxInitDSound: handle all the gory details of DirectSound initialisation
303  *****************************************************************************/
304 static int DirectxInitDSound( aout_instance_t *p_aout )
305 {
306     HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
307
308     p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
309     if( p_aout->output.p_sys->hdsound_dll == NULL )
310     {
311         msg_Warn( p_aout, "cannot open DSOUND.DLL" );
312         goto error;
313     }
314
315     OurDirectSoundCreate = (void *)GetProcAddress(
316                                            p_aout->output.p_sys->hdsound_dll,
317                                            "DirectSoundCreate" );
318     if( OurDirectSoundCreate == NULL )
319     {
320         msg_Warn( p_aout, "GetProcAddress FAILED" );
321         goto error;
322     }
323
324     /* Create the direct sound object */
325     if( OurDirectSoundCreate( NULL, &p_aout->output.p_sys->p_dsobject, NULL )
326         != DS_OK )
327     {
328         msg_Warn( p_aout, "cannot create a direct sound device" );
329         goto error;
330     }
331
332     /* Set DirectSound Cooperative level, ie what control we want over Windows
333      * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
334      * settings of the primary buffer, but also that only the sound of our
335      * application will be hearable when it will have the focus.
336      * !!! (this is not really working as intended yet because to set the
337      * cooperative level you need the window handle of your application, and
338      * I don't know of any easy way to get it. Especially since we might play
339      * sound without any video, and so what window handle should we use ???
340      * The hack for now is to use the Desktop window handle - it seems to be
341      * working */
342     if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
343                                           GetDesktopWindow(),
344                                           DSSCL_EXCLUSIVE) )
345     {
346         msg_Warn( p_aout, "cannot set direct sound cooperative level" );
347     }
348
349     return 0;
350
351  error:
352     p_aout->output.p_sys->p_dsobject = NULL;
353     if( p_aout->output.p_sys->hdsound_dll )
354     {
355         FreeLibrary( p_aout->output.p_sys->hdsound_dll );
356         p_aout->output.p_sys->hdsound_dll = NULL;
357     }
358     return 1;
359
360 }
361
362 /*****************************************************************************
363  * DirectxCreateSecondaryBuffer
364  *****************************************************************************
365  * This function creates the buffer we'll use to play audio.
366  * In DirectSound there are two kinds of buffers:
367  * - the primary buffer: which is the actual buffer that the soundcard plays
368  * - the secondary buffer(s): these buffers are the one actually used by
369  *    applications and DirectSound takes care of mixing them into the primary.
370  *
371  * Once you create a secondary buffer, you cannot change its format anymore so
372  * you have to release the current and create another one.
373  *****************************************************************************/
374 static int DirectxCreateSecondaryBuffer( aout_instance_t *p_aout )
375 {
376     WAVEFORMATEX         waveformat;
377     DSBUFFERDESC         dsbdesc;
378     DSBCAPS              dsbcaps;
379     int                  i_nb_channels, i;
380
381     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
382     if ( i_nb_channels > 2 )
383     {
384         i_nb_channels = 2;
385         p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
386     }
387
388     /* First set the buffer format */
389     memset( &waveformat, 0, sizeof(WAVEFORMATEX) );
390     switch( p_aout->output.output.i_format )
391     {
392     case VLC_FOURCC('s','1','6','l'):
393         waveformat.wFormatTag     = WAVE_FORMAT_PCM;
394         waveformat.wBitsPerSample = 16;
395         break;
396     case VLC_FOURCC('f','l','3','2'):
397         waveformat.wFormatTag     = WAVE_FORMAT_IEEE_FLOAT;
398         waveformat.wBitsPerSample = sizeof(float) * 8;
399         break;
400     }
401     waveformat.nChannels       = i_nb_channels;
402     waveformat.nSamplesPerSec  = p_aout->output.output.i_rate;
403     waveformat.nBlockAlign     = waveformat.wBitsPerSample / 8 *
404                                  waveformat.nChannels;
405     waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec *
406                                      waveformat.nBlockAlign;
407
408     aout_FormatPrepare( &p_aout->output.output );
409
410     /* Then fill in the descriptor */
411     memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
412     dsbdesc.dwSize = sizeof(DSBUFFERDESC);
413     dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
414                     | DSBCAPS_CTRLPOSITIONNOTIFY     /* We need notification */
415                     | DSBCAPS_GLOBALFOCUS;      /* Allows background playing */
416     dsbdesc.dwBufferBytes = FRAME_SIZE * FRAMES_NUM           /* buffer size */
417                             * p_aout->output.output.i_bytes_per_frame;
418     dsbdesc.lpwfxFormat = &waveformat;
419  
420     if( IDirectSound_CreateSoundBuffer( p_aout->output.p_sys->p_dsobject,
421                                         &dsbdesc,
422                                         &p_aout->output.p_sys->p_dsbuffer,
423                                         NULL) != DS_OK )
424     {
425         msg_Warn( p_aout, "cannot create direct sound secondary buffer" );
426         goto error;
427     }
428
429     /* backup the size of a frame */
430     p_aout->output.p_sys->p_notif->i_frame_size = FRAME_SIZE *
431         p_aout->output.output.i_bytes_per_frame;
432
433     memset(&dsbcaps, 0, sizeof(DSBCAPS));
434     dsbcaps.dwSize = sizeof(DSBCAPS);
435     IDirectSoundBuffer_GetCaps( p_aout->output.p_sys->p_dsbuffer, &dsbcaps  );
436     msg_Dbg( p_aout, "requested %li bytes buffer and got %li bytes.",
437              FRAMES_NUM * p_aout->output.p_sys->p_notif->i_frame_size,
438              dsbcaps.dwBufferBytes );
439
440     /* Now the secondary buffer is created, we need to setup its position
441      * notification */
442     for( i = 0; i < FRAMES_NUM; i++ )
443     {
444         p_aout->output.p_sys->p_notif->p_events[i].dwOffset = i *
445             p_aout->output.p_sys->p_notif->i_frame_size;
446
447         p_aout->output.p_sys->p_notif->i_frame_status[i] = FRAME_EMPTY;
448     }
449
450     /* Get the IDirectSoundNotify interface */
451     if FAILED( IDirectSoundBuffer_QueryInterface(
452                                 p_aout->output.p_sys->p_dsbuffer,
453                                 &IID_IDirectSoundNotify,
454                                 (LPVOID *)&p_aout->output.p_sys->p_dsnotify ) )
455     {
456         msg_Err( p_aout, "cannot get Notify interface" );
457         goto error;
458     }
459         
460     if FAILED( IDirectSoundNotify_SetNotificationPositions(
461                                     p_aout->output.p_sys->p_dsnotify,
462                                     FRAMES_NUM,
463                                     p_aout->output.p_sys->p_notif->p_events ) )
464     {
465         msg_Err( p_aout, "cannot set position Notification" );
466         goto error;
467     }
468
469     p_aout->output.i_nb_samples = FRAME_SIZE;
470
471     return 0;
472
473  error:
474     if( p_aout->output.p_sys->p_dsbuffer )
475     {
476         IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
477         p_aout->output.p_sys->p_dsbuffer = NULL;
478     }
479     if( p_aout->output.p_sys->p_dsnotify )
480     {
481         IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
482         p_aout->output.p_sys->p_dsnotify = NULL;
483     }
484     return VLC_EGENERIC;
485 }
486
487 /*****************************************************************************
488  * DirectxCreateSecondaryBuffer
489  *****************************************************************************
490  * This function destroys the secondary buffer.
491  *****************************************************************************/
492 static void DirectxDestroySecondaryBuffer( aout_instance_t *p_aout )
493 {
494     /* make sure the buffer isn't playing */
495     if( p_aout->output.p_sys->p_dsbuffer )
496         IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
497
498     if( p_aout->output.p_sys->p_dsnotify )
499     {
500         IDirectSoundNotify_Release( p_aout->output.p_sys->p_dsnotify );
501         p_aout->output.p_sys->p_dsnotify = NULL;
502     }
503
504     if( p_aout->output.p_sys->p_dsbuffer )
505     {
506         IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
507         p_aout->output.p_sys->p_dsbuffer = NULL;
508     }
509 }
510
511 /*****************************************************************************
512  * DirectxFillBuffer: Fill in one of the direct sound frame buffers.
513  *****************************************************************************
514  * Returns VLC_SUCCESS on success.
515  *****************************************************************************/
516 static int DirectxFillBuffer( aout_instance_t *p_aout, int i_frame,
517                               aout_buffer_t *p_buffer )
518 {
519     notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
520     void *p_write_position, *p_wrap_around;
521     long l_bytes1, l_bytes2;
522     HRESULT dsresult;
523
524     /* Before copying anything, we have to lock the buffer */
525     dsresult = IDirectSoundBuffer_Lock(
526                 p_aout->output.p_sys->p_dsbuffer,               /* DS buffer */
527                 i_frame * p_notif->i_frame_size,             /* Start offset */
528                 p_notif->i_frame_size,                    /* Number of bytes */
529                 &p_write_position,                  /* Address of lock start */
530                 &l_bytes1,       /* Count of bytes locked before wrap around */
531                 &p_wrap_around,            /* Buffer adress (if wrap around) */
532                 &l_bytes2,               /* Count of bytes after wrap around */
533                 0 );                                                /* Flags */
534     if( dsresult == DSERR_BUFFERLOST )
535     {
536         IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
537         dsresult = IDirectSoundBuffer_Lock(
538                                p_aout->output.p_sys->p_dsbuffer,
539                                i_frame * p_notif->i_frame_size,
540                                p_notif->i_frame_size,
541                                &p_write_position,
542                                &l_bytes1,
543                                &p_wrap_around,
544                                &l_bytes2,
545                                0 );
546     }
547     if( dsresult != DS_OK )
548     {
549         msg_Warn( p_notif, "cannot lock buffer" );
550         if( p_buffer ) aout_BufferFree( p_buffer );
551         return VLC_EGENERIC;
552     }
553
554     if ( p_buffer != NULL )
555     {
556         p_aout->p_vlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
557                                   l_bytes1 );
558         aout_BufferFree( p_buffer );
559     }
560     else
561         memset( p_write_position, 0, l_bytes1 );
562
563     /* Now the data has been copied, unlock the buffer */
564     IDirectSoundBuffer_Unlock( p_aout->output.p_sys->p_dsbuffer,
565                                p_write_position, l_bytes1,
566                                p_wrap_around, l_bytes2 );
567
568     return VLC_SUCCESS;
569 }
570
571 /*****************************************************************************
572  * DirectSoundThread: this thread will capture play notification events. 
573  *****************************************************************************
574  * We use this thread to emulate a callback mechanism. The thread probes for
575  * event notification and fills up the DS secondary buffer when needed.
576  *****************************************************************************/
577 static void DirectSoundThread( notification_thread_t *p_notif )
578 {
579     HANDLE  notification_events[FRAMES_NUM];
580     HRESULT dsresult;
581     aout_instance_t *p_aout = p_notif->p_aout;
582     int i, i_which_frame, i_last_frame, i_next_frame;
583     mtime_t mtime;
584
585     for( i = 0; i < FRAMES_NUM; i++ )
586         notification_events[i] = p_notif->p_events[i].hEventNotify;
587
588     /* Tell the main thread that we are ready */
589     vlc_thread_ready( p_notif );
590
591     msg_Dbg( p_notif, "DirectSoundThread ready" );
592
593     /* Wait here until Play() is called */
594     WaitForSingleObject( notification_events[0], INFINITE );
595
596     if( !p_notif->b_die )
597     {
598         mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
599
600         /* start playing the buffer */
601         dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
602                                         0,                         /* Unused */
603                                         0,                         /* Unused */
604                                         DSBPLAY_LOOPING );          /* Flags */
605         if( dsresult == DSERR_BUFFERLOST )
606         {
607             IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
608             dsresult = IDirectSoundBuffer_Play(
609                                             p_aout->output.p_sys->p_dsbuffer,
610                                             0,                     /* Unused */
611                                             0,                     /* Unused */
612                                             DSBPLAY_LOOPING );      /* Flags */
613         }
614         if( dsresult != DS_OK )
615         {
616             msg_Err( p_aout, "cannot start playing buffer" );
617         }
618     }
619
620     while( !p_notif->b_die )
621     {
622         aout_buffer_t *p_buffer;
623         long l_latency;
624
625         /* wait for the position notification */
626         i_which_frame = WaitForMultipleObjects( FRAMES_NUM,
627                                                 notification_events, 0,
628                                                 INFINITE ) - WAIT_OBJECT_0;
629
630         if( p_notif->b_die )
631             break;
632
633         mtime = mdate();
634
635         /* We take into account the current latency */
636         if( IDirectSoundBuffer_GetCurrentPosition(
637                 p_aout->output.p_sys->p_dsbuffer,
638                 &l_latency, NULL ) == DS_OK )
639         {
640             if( l_latency > (i_which_frame * FRAME_SIZE)
641                   && l_latency < ((i_which_frame+1) * FRAME_SIZE) )
642             {
643                 l_latency = - ( l_latency /
644                                 p_aout->output.output.i_bytes_per_frame %
645                                 FRAME_SIZE );
646             }
647             else
648             {
649                 l_latency = FRAME_SIZE - ( l_latency /
650                                       p_aout->output.output.i_bytes_per_frame %
651                                       FRAME_SIZE );
652             }
653         }
654         else
655         {
656             l_latency = 0;
657         }
658
659         /* Mark last frame as empty */
660         i_last_frame = (i_which_frame + FRAMES_NUM -1) % FRAMES_NUM;
661         i_next_frame = (i_which_frame + 1) % FRAMES_NUM;
662         p_notif->i_frame_status[i_last_frame] = FRAME_EMPTY;
663
664         /* Try to fill in as many frame buffers as possible */
665         for( i = i_next_frame; (i % FRAMES_NUM) != i_which_frame; i++ )
666         {
667
668             /* Check if frame buf is already filled */
669             if( p_notif->i_frame_status[i % FRAMES_NUM] == FRAME_QUEUED )
670                 continue;
671
672             p_buffer = aout_OutputNextBuffer( p_aout,
673                 mtime + 1000000 / p_aout->output.output.i_rate *
674                 ((i - i_next_frame + 1) * FRAME_SIZE + l_latency), VLC_FALSE );
675
676             /* If there is no audio data available and we have some buffered
677              * already, then just wait for the next time */
678             if( !p_buffer && (i != i_next_frame) )
679             {
680                 //msg_Err( p_aout, "only %i frame buffers filled!",
681                 //         i - i_next_frame );
682                 break;
683             }
684
685             if( DirectxFillBuffer( p_aout, (i%FRAMES_NUM), p_buffer )
686                 != VLC_SUCCESS )
687                 break;
688
689             /* Mark the frame buffer as QUEUED */
690             p_notif->i_frame_status[i%FRAMES_NUM] = FRAME_QUEUED;
691         }
692
693     }
694
695     /* free the events */
696     for( i = 0; i < FRAMES_NUM; i++ )
697         CloseHandle( notification_events[i] );
698
699     msg_Dbg( p_notif, "DirectSoundThread exiting" );
700 }