directx.c 23.5 KB
Newer Older
1
/*****************************************************************************
2
 * directx.c: Windows DirectX audio output method
3
4
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
gbazin's avatar
   
gbazin committed
5
 * $Id: directx.c,v 1.5 2002/10/28 22:31:49 gbazin Exp $
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <fcntl.h>                                       /* open(), O_WRONLY */
#include <string.h>                                            /* strerror() */

#include <stdlib.h>                            /* calloc(), malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/aout.h>
gbazin's avatar
   
gbazin committed
35
#include "aout_internal.h"
36

gbazin's avatar
   
gbazin committed
37
#include <windows.h>
38
39
40
#include <mmsystem.h>
#include <dsound.h>

gbazin's avatar
   
gbazin committed
41
42
#define FRAME_SIZE 2048              /* The size is in samples, not in bytes */

43
44
45
46
47
48
49
50
/*****************************************************************************
 * DirectSound GUIDs.
 * Defining them here allows us to get rid of the dxguid library during
 * the linking stage.
 *****************************************************************************/
#include <initguid.h>
DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf, 0x8, 0x0, 0xa0, 0xc9, 0x25, 0xcd, 0x16);

gbazin's avatar
   
gbazin committed
51
52
53
54
55
56
57
/*****************************************************************************
 * Useful macros
 *****************************************************************************/
#ifndef WAVE_FORMAT_IEEE_FLOAT
#   define WAVE_FORMAT_IEEE_FLOAT 0x0003
#endif

58
59
60
61
62
63
64
/*****************************************************************************
 * notification_thread_t: DirectX event thread
 *****************************************************************************/
typedef struct notification_thread_t
{
    VLC_COMMON_MEMBERS

gbazin's avatar
   
gbazin committed
65
    aout_instance_t * p_aout;
66
    DSBPOSITIONNOTIFY p_events[2];               /* play notification events */
gbazin's avatar
   
gbazin committed
67
    int i_buffer_size;                         /* Size in bytes of one frame */
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

} notification_thread_t;

/*****************************************************************************
 * aout_sys_t: directx audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes the direct sound specific properties of an audio device.
 *****************************************************************************/
struct aout_sys_t
{
    LPDIRECTSOUND       p_dsobject;              /* main Direct Sound object */

    LPDIRECTSOUNDBUFFER p_dsbuffer_primary;     /* the actual sound card buffer
                                                   (not used directly) */

    LPDIRECTSOUNDBUFFER p_dsbuffer;   /* the sound buffer we use (direct sound
                                       * takes care of mixing all the
                                       * secondary buffers into the primary) */

    LPDIRECTSOUNDNOTIFY p_dsnotify;         /* the position notify interface */

    HINSTANCE           hdsound_dll;      /* handle of the opened dsound dll */

    notification_thread_t * p_notif;                 /* DirectSoundThread id */
gbazin's avatar
   
gbazin committed
93

94
95
96
97
98
};

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
99
100
static int  OpenAudio  ( vlc_object_t * );
static void CloseAudio ( vlc_object_t * );
101

gbazin's avatar
   
gbazin committed
102
static void Play       ( aout_instance_t * );
103
104

/* local functions */
gbazin's avatar
   
gbazin committed
105
106
107
static int  DirectxCreateSecondaryBuffer ( aout_instance_t * );
static void DirectxDestroySecondaryBuffer( aout_instance_t * );
static int  DirectxInitDSound            ( aout_instance_t * );
108
109
static void DirectSoundThread            ( notification_thread_t * );

gbazin's avatar
   
gbazin committed
110
111
112
113
114
115
116
117
118
119
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_description( _("DirectX audio module") );
    set_capability( "audio output", 100 );
    add_shortcut( "directx" );
    set_callbacks( OpenAudio, CloseAudio );
vlc_module_end();

120
121
122
123
124
/*****************************************************************************
 * OpenAudio: open the audio device
 *****************************************************************************
 * This function opens and setups Direct Sound.
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
125
static int OpenAudio( vlc_object_t *p_this )
126
{
gbazin's avatar
   
gbazin committed
127
    aout_instance_t * p_aout = (aout_instance_t *)p_this;
128
129
130
131
132
133
    HRESULT dsresult;
    DSBUFFERDESC dsbuffer_desc;

    msg_Dbg( p_aout, "Open" );

   /* Allocate structure */
gbazin's avatar
   
gbazin committed
134
135
    p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
    if( p_aout->output.p_sys == NULL )
136
137
    {
        msg_Err( p_aout, "out of memory" );
138
        return VLC_EGENERIC;
139
140
141
    }

    /* Initialize some variables */
gbazin's avatar
   
gbazin committed
142
143
144
145
146
147
148
    p_aout->output.p_sys->p_dsobject = NULL;
    p_aout->output.p_sys->p_dsbuffer_primary = NULL;
    p_aout->output.p_sys->p_dsbuffer = NULL;
    p_aout->output.p_sys->p_dsnotify = NULL;
    p_aout->output.p_sys->p_notif = NULL;

    p_aout->output.pf_play = Play;
149
    aout_VolumeSoftInit( p_aout );
150
151
152
153

    /* Initialise DirectSound */
    if( DirectxInitDSound( p_aout ) )
    {
gbazin's avatar
   
gbazin committed
154
155
        msg_Err( p_aout, "cannot initialize DirectSound" );
        goto error;
156
157
158
159
160
161
162
    }

    /* Obtain (not create) Direct Sound primary buffer */
    memset( &dsbuffer_desc, 0, sizeof(DSBUFFERDESC) );
    dsbuffer_desc.dwSize = sizeof(DSBUFFERDESC);
    dsbuffer_desc.dwFlags = DSBCAPS_PRIMARYBUFFER;
    msg_Warn( p_aout, "create direct sound primary buffer" );
gbazin's avatar
   
gbazin committed
163
164
165
166
    dsresult = IDirectSound_CreateSoundBuffer(p_aout->output.p_sys->p_dsobject,
                                     &dsbuffer_desc,
                                     &p_aout->output.p_sys->p_dsbuffer_primary,
                                     NULL);
167
168
    if( dsresult != DS_OK )
    {
gbazin's avatar
   
gbazin committed
169
170
        msg_Err( p_aout, "cannot create direct sound primary buffer" );
        goto error;
171
172
173
    }

    /* Now we need to setup DirectSound play notification */
gbazin's avatar
   
gbazin committed
174
175
176
    p_aout->output.p_sys->p_notif =
        vlc_object_create( p_aout, sizeof(notification_thread_t) );
    p_aout->output.p_sys->p_notif->p_aout = p_aout;
177
178

    /* first we need to create the notification events */
gbazin's avatar
   
gbazin committed
179
    p_aout->output.p_sys->p_notif->p_events[0].hEventNotify =
180
        CreateEvent( NULL, FALSE, FALSE, NULL );
gbazin's avatar
   
gbazin committed
181
    p_aout->output.p_sys->p_notif->p_events[1].hEventNotify =
182
183
184
        CreateEvent( NULL, FALSE, FALSE, NULL );

    /* then create a new secondary buffer */
gbazin's avatar
   
gbazin committed
185
    p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
186
187
    if( DirectxCreateSecondaryBuffer( p_aout ) )
    {
gbazin's avatar
   
gbazin committed
188
        msg_Err( p_aout, "cannot create WAVE_FORMAT_IEEE_FLOAT buffer" );
189

gbazin's avatar
   
gbazin committed
190
191
192
193
194
195
196
        p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
        if( DirectxCreateSecondaryBuffer( p_aout ) )
        {
            msg_Err( p_aout, "cannot create WAVE_FORMAT_PCM buffer" );
            return 1;
        }
    }
197

gbazin's avatar
   
gbazin committed
198
199
    /* start playing the buffer */
    dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
200
201
202
203
204
                                        0,                         /* Unused */
                                        0,                         /* Unused */
                                        DSBPLAY_LOOPING );          /* Flags */
    if( dsresult == DSERR_BUFFERLOST )
    {
gbazin's avatar
   
gbazin committed
205
206
        IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
        dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
207
208
209
210
211
212
                                            0,                     /* Unused */
                                            0,                     /* Unused */
                                            DSBPLAY_LOOPING );      /* Flags */
    }
    if( dsresult != DS_OK )
    {
gbazin's avatar
   
gbazin committed
213
        msg_Warn( p_aout, "cannot play buffer" );
214
215
    }

Christophe Massiot's avatar
Christophe Massiot committed
216
217
218
219
    /* then launch the notification thread */
    msg_Dbg( p_aout, "creating DirectSoundThread" );
    if( vlc_thread_create( p_aout->output.p_sys->p_notif,
                           "DirectSound Notification Thread",
gbazin's avatar
   
gbazin committed
220
221
                           DirectSoundThread,
                           THREAD_PRIORITY_TIME_CRITICAL, 1 ) )
Christophe Massiot's avatar
Christophe Massiot committed
222
223
224
225
226
227
228
    {
        msg_Err( p_aout, "cannot create DirectSoundThread" );
        goto error;
    }

    vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );

gbazin's avatar
   
gbazin committed
229
    return 0;
Christophe Massiot's avatar
Christophe Massiot committed
230
231

 error:
gbazin's avatar
   
gbazin committed
232
    CloseAudio( VLC_OBJECT(p_aout) );
233
    return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
234
235
236
}

/*****************************************************************************
Christophe Massiot's avatar
Christophe Massiot committed
237
 * Play: nothing to do
gbazin's avatar
   
gbazin committed
238
 *****************************************************************************/
239
static void Play( aout_instance_t *p_aout )
gbazin's avatar
   
gbazin committed
240
{
241
242
243
244
245
}

/*****************************************************************************
 * CloseAudio: close the audio device
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
246
static void CloseAudio( vlc_object_t *p_this )
247
{
gbazin's avatar
   
gbazin committed
248
    aout_instance_t * p_aout = (aout_instance_t *)p_this;
249
250
251
252

    msg_Dbg( p_aout, "Close" );

    /* kill the position notification thread, if any */
gbazin's avatar
   
gbazin committed
253
    if( p_aout->output.p_sys->p_notif )
254
    {
255
        vlc_object_detach( p_aout->output.p_sys->p_notif );
gbazin's avatar
   
gbazin committed
256
257
258
259
260
261
        if( p_aout->output.p_sys->p_notif->b_thread )
        {
            p_aout->output.p_sys->p_notif->b_die = 1;
            vlc_thread_join( p_aout->output.p_sys->p_notif );
        }
        vlc_object_destroy( p_aout->output.p_sys->p_notif );
262
263
264
265
266
267
    }

    /* release the secondary buffer */
    DirectxDestroySecondaryBuffer( p_aout );

    /* then release the primary buffer */
gbazin's avatar
   
gbazin committed
268
269
    if( p_aout->output.p_sys->p_dsbuffer_primary )
        IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer_primary );
270
271

    /* finally release the DirectSound object */
gbazin's avatar
   
gbazin committed
272
273
    if( p_aout->output.p_sys->p_dsobject )
        IDirectSound_Release( p_aout->output.p_sys->p_dsobject );
274
275
    
    /* free DSOUND.DLL */
gbazin's avatar
   
gbazin committed
276
277
    if( p_aout->output.p_sys->hdsound_dll )
       FreeLibrary( p_aout->output.p_sys->hdsound_dll );
278

279
    free( p_aout->output.p_sys );
280
281
282
}

/*****************************************************************************
gbazin's avatar
   
gbazin committed
283
 * DirectxInitDSound: handle all the gory details of DirectSound initialisation
284
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
285
static int DirectxInitDSound( aout_instance_t *p_aout )
286
287
288
{
    HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);

gbazin's avatar
   
gbazin committed
289
290
    p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
    if( p_aout->output.p_sys->hdsound_dll == NULL )
291
    {
gbazin's avatar
   
gbazin committed
292
293
        msg_Warn( p_aout, "cannot open DSOUND.DLL" );
        goto error;
294
295
    }

gbazin's avatar
   
gbazin committed
296
297
298
    OurDirectSoundCreate = (void *)GetProcAddress(
                                           p_aout->output.p_sys->hdsound_dll,
                                           "DirectSoundCreate" );
299
300
    if( OurDirectSoundCreate == NULL )
    {
gbazin's avatar
   
gbazin committed
301
302
        msg_Warn( p_aout, "GetProcAddress FAILED" );
        goto error;
303
304
305
    }

    /* Create the direct sound object */
gbazin's avatar
   
gbazin committed
306
307
    if( OurDirectSoundCreate( NULL, &p_aout->output.p_sys->p_dsobject, NULL )
        != DS_OK )
308
309
    {
        msg_Warn( p_aout, "cannot create a direct sound device" );
gbazin's avatar
   
gbazin committed
310
        goto error;
311
312
313
314
315
316
317
318
319
320
321
322
    }

    /* Set DirectSound Cooperative level, ie what control we want over Windows
     * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
     * settings of the primary buffer, but also that only the sound of our
     * application will be hearable when it will have the focus.
     * !!! (this is not really working as intended yet because to set the
     * cooperative level you need the window handle of your application, and
     * I don't know of any easy way to get it. Especially since we might play
     * sound without any video, and so what window handle should we use ???
     * The hack for now is to use the Desktop window handle - it seems to be
     * working */
gbazin's avatar
   
gbazin committed
323
324
325
    if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
                                          GetDesktopWindow(),
                                          DSSCL_EXCLUSIVE) )
326
327
328
329
    {
        msg_Warn( p_aout, "cannot set direct sound cooperative level" );
    }

gbazin's avatar
   
gbazin committed
330
331
332
333
334
335
336
337
338
339
340
    return 0;

 error:
    p_aout->output.p_sys->p_dsobject = NULL;
    if( p_aout->output.p_sys->hdsound_dll )
    {
        FreeLibrary( p_aout->output.p_sys->hdsound_dll );
        p_aout->output.p_sys->hdsound_dll = NULL;
    }
    return 1;

341
342
343
344
345
346
347
348
349
350
351
352
353
354
}

/*****************************************************************************
 * DirectxCreateSecondaryBuffer
 *****************************************************************************
 * This function creates the buffer we'll use to play audio.
 * In DirectSound there are two kinds of buffers:
 * - the primary buffer: which is the actual buffer that the soundcard plays
 * - the secondary buffer(s): these buffers are the one actually used by
 *    applications and DirectSound takes care of mixing them into the primary.
 *
 * Once you create a secondary buffer, you cannot change its format anymore so
 * you have to release the current and create another one.
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
355
static int DirectxCreateSecondaryBuffer( aout_instance_t *p_aout )
356
357
358
359
{
    WAVEFORMATEX         waveformat;
    DSBUFFERDESC         dsbdesc;
    DSBCAPS              dsbcaps;
360
    int                  i_nb_channels;
361

362
363
364
365
366
367
    i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
    if ( i_nb_channels > 2 )
    {
        i_nb_channels = 2;
        p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
    }
gbazin's avatar
   
gbazin committed
368

369
    /* First set the buffer format */
gbazin's avatar
   
gbazin committed
370
    memset(&waveformat, 0, sizeof(WAVEFORMATEX));
gbazin's avatar
   
gbazin committed
371
372
373
374
375
376
377
378
379
380
381
    switch( p_aout->output.output.i_format )
    {
    case VLC_FOURCC('s','1','6','l'):
        waveformat.wFormatTag     = WAVE_FORMAT_PCM;
        waveformat.wBitsPerSample = 16;
        break;
    case VLC_FOURCC('f','l','3','2'):
        waveformat.wFormatTag     = WAVE_FORMAT_IEEE_FLOAT;
        waveformat.wBitsPerSample = sizeof(float) * 8;
        break;
    }
382
    waveformat.nChannels       = i_nb_channels;
gbazin's avatar
   
gbazin committed
383
    waveformat.nSamplesPerSec  = p_aout->output.output.i_rate;
384
385
386
387
388
    waveformat.nBlockAlign     = waveformat.wBitsPerSample / 8 *
                                 waveformat.nChannels;
    waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec *
                                     waveformat.nBlockAlign;

gbazin's avatar
   
gbazin committed
389
390
    aout_FormatPrepare( &p_aout->output.output );

391
    /* Then fill in the descriptor */
gbazin's avatar
   
gbazin committed
392
393
    memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
    dsbdesc.dwSize = sizeof(DSBUFFERDESC);
394
395
396
    dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
                    | DSBCAPS_CTRLPOSITIONNOTIFY     /* We need notification */
                    | DSBCAPS_GLOBALFOCUS;      /* Allows background playing */
gbazin's avatar
   
gbazin committed
397
398
    dsbdesc.dwBufferBytes = FRAME_SIZE * 2 /* frames*/        /* buffer size */
                            * p_aout->output.output.i_bytes_per_frame;
gbazin's avatar
   
gbazin committed
399
    dsbdesc.lpwfxFormat = &waveformat;
400
 
gbazin's avatar
   
gbazin committed
401
    if( IDirectSound_CreateSoundBuffer( p_aout->output.p_sys->p_dsobject,
402
                                        &dsbdesc,
gbazin's avatar
   
gbazin committed
403
                                        &p_aout->output.p_sys->p_dsbuffer,
404
405
406
                                        NULL) != DS_OK )
    {
        msg_Warn( p_aout, "cannot create direct sound secondary buffer" );
gbazin's avatar
   
gbazin committed
407
        goto error;
408
409
    }

gbazin's avatar
   
gbazin committed
410
    /* backup the size of a frame */
gbazin's avatar
   
gbazin committed
411
412
    p_aout->output.p_sys->p_notif->i_buffer_size = FRAME_SIZE *
        p_aout->output.output.i_bytes_per_frame;
gbazin's avatar
   
gbazin committed
413
414

    memset(&dsbcaps, 0, sizeof(DSBCAPS));
415
    dsbcaps.dwSize = sizeof(DSBCAPS);
gbazin's avatar
   
gbazin committed
416
    IDirectSoundBuffer_GetCaps( p_aout->output.p_sys->p_dsbuffer, &dsbcaps  );
gbazin's avatar
   
gbazin committed
417
418
    msg_Dbg( p_aout, "requested %li bytes buffer and got %li bytes.",
             2 * p_aout->output.p_sys->p_notif->i_buffer_size,
gbazin's avatar
   
gbazin committed
419
             dsbcaps.dwBufferBytes );
420
421
422

    /* Now the secondary buffer is created, we need to setup its position
     * notification */
gbazin's avatar
   
gbazin committed
423
424
425
    p_aout->output.p_sys->p_notif->p_events[0].dwOffset = 0;
    p_aout->output.p_sys->p_notif->p_events[1].dwOffset =
        p_aout->output.p_sys->p_notif->i_buffer_size;
426
427

    /* Get the IDirectSoundNotify interface */
gbazin's avatar
   
gbazin committed
428
429
430
431
    if FAILED( IDirectSoundBuffer_QueryInterface(
                                p_aout->output.p_sys->p_dsbuffer,
                                &IID_IDirectSoundNotify,
                                (LPVOID *)&p_aout->output.p_sys->p_dsnotify ) )
432
    {
gbazin's avatar
   
gbazin committed
433
        msg_Err( p_aout, "cannot get Notify interface" );
gbazin's avatar
   
gbazin committed
434
        goto error;
435
436
437
    }
        
    if FAILED( IDirectSoundNotify_SetNotificationPositions(
gbazin's avatar
   
gbazin committed
438
439
                                    p_aout->output.p_sys->p_dsnotify, 2,
                                    p_aout->output.p_sys->p_notif->p_events ) )
440
    {
gbazin's avatar
   
gbazin committed
441
        msg_Err( p_aout, "cannot set position Notification" );
gbazin's avatar
   
gbazin committed
442
        goto error;
443
    }
gbazin's avatar
   
gbazin committed
444

gbazin's avatar
   
gbazin committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
    p_aout->output.i_nb_samples = FRAME_SIZE;

    return 0;

 error:
    if( p_aout->output.p_sys->p_dsbuffer )
    {
        IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
        p_aout->output.p_sys->p_dsbuffer = NULL;
    }
    if( p_aout->output.p_sys->p_dsnotify )
    {
        IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
        p_aout->output.p_sys->p_dsnotify = NULL;
    }
460
    return VLC_EGENERIC;
461
462
463
464
465
}

/*****************************************************************************
 * DirectxCreateSecondaryBuffer
 *****************************************************************************
gbazin's avatar
   
gbazin committed
466
 * This function destroys the secondary buffer.
467
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
468
static void DirectxDestroySecondaryBuffer( aout_instance_t *p_aout )
469
470
{
    /* make sure the buffer isn't playing */
gbazin's avatar
   
gbazin committed
471
472
    if( p_aout->output.p_sys->p_dsbuffer )
        IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
473

gbazin's avatar
   
gbazin committed
474
    if( p_aout->output.p_sys->p_dsnotify )
475
    {
gbazin's avatar
   
gbazin committed
476
477
        IDirectSoundNotify_Release( p_aout->output.p_sys->p_dsnotify );
        p_aout->output.p_sys->p_dsnotify = NULL;
478
479
    }

gbazin's avatar
   
gbazin committed
480
    if( p_aout->output.p_sys->p_dsbuffer )
481
    {
gbazin's avatar
   
gbazin committed
482
483
        IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
        p_aout->output.p_sys->p_dsbuffer = NULL;
484
485
486
487
488
489
    }
}

/*****************************************************************************
 * DirectSoundThread: this thread will capture play notification events. 
 *****************************************************************************
gbazin's avatar
   
gbazin committed
490
491
 * We use this thread to emulate a callback mechanism. The thread probes for
 * event notification and fills up the DS secondary buffer when needed.
492
493
494
495
496
 *****************************************************************************/
static void DirectSoundThread( notification_thread_t *p_notif )
{
    HANDLE  notification_events[2];
    HRESULT dsresult;
gbazin's avatar
   
gbazin committed
497
    aout_instance_t *p_aout = p_notif->p_aout;
498

gbazin's avatar
   
gbazin committed
499
500
    notification_events[0] = p_notif->p_events[0].hEventNotify;
    notification_events[1] = p_notif->p_events[1].hEventNotify;
501
502
503
504
505
506
507
508

    /* Tell the main thread that we are ready */
    vlc_thread_ready( p_notif );

    msg_Dbg( p_notif, "DirectSoundThread ready" );

    while( !p_notif->b_die )
    {
gbazin's avatar
   
gbazin committed
509
510
511
512
        int i_which_event;
        void *p_write_position, *p_wrap_around;
        long l_bytes1, l_bytes2;
        aout_buffer_t * p_buffer;
gbazin's avatar
   
gbazin committed
513
        long l_play_position;
gbazin's avatar
   
gbazin committed
514

515
        /* wait for the position notification */
gbazin's avatar
   
gbazin committed
516
517
518
        i_which_event = WaitForMultipleObjects( 2, notification_events, 0,
                                                INFINITE ) - WAIT_OBJECT_0;

519
520
521
        if( p_notif->b_die )
            break;

gbazin's avatar
   
gbazin committed
522
523
524
525
526
527
528
529
530
531
532
533
534
        /* Before copying anything, we have to lock the buffer */
        dsresult = IDirectSoundBuffer_Lock(
                                                                /* DS buffer */
            p_aout->output.p_sys->p_dsbuffer,
                                                     /* Offset of lock start */
            i_which_event ? 0 : p_notif->i_buffer_size,
            p_notif->i_buffer_size,                 /* Number of bytes */
            &p_write_position,                      /* Address of lock start */
            &l_bytes1,           /* Count of bytes locked before wrap around */
            &p_wrap_around,                /* Buffer adress (if wrap around) */
            &l_bytes2,                   /* Count of bytes after wrap around */
            0 );                                                    /* Flags */
        if( dsresult == DSERR_BUFFERLOST )
535
        {
gbazin's avatar
   
gbazin committed
536
537
538
539
540
541
542
543
544
545
            IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
            dsresult = IDirectSoundBuffer_Lock(
                           p_aout->output.p_sys->p_dsbuffer,
                           i_which_event ? 0 : p_notif->i_buffer_size,
                           p_notif->i_buffer_size,
                           &p_write_position,
                           &l_bytes1,
                           &p_wrap_around,
                           &l_bytes2,
                           0 );
546
        }
gbazin's avatar
   
gbazin committed
547
        if( dsresult != DS_OK )
548
        {
gbazin's avatar
   
gbazin committed
549
550
            msg_Warn( p_notif, "cannot lock buffer" );
            continue;
551
552
        }

gbazin's avatar
   
gbazin committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
        /* We take into account the current latency */
        if( IDirectSoundBuffer_GetCurrentPosition(
                p_aout->output.p_sys->p_dsbuffer,
                &l_play_position, NULL ) == DS_OK )
        {
            if( l_play_position > (i_which_event * FRAME_SIZE)
                  && l_play_position < ((i_which_event+1) * FRAME_SIZE) )
            {
                l_play_position = FRAME_SIZE - ( l_play_position /
                                      p_aout->output.output.i_bytes_per_frame %
                                      FRAME_SIZE );
            }
            else
            {
                l_play_position = 2 * FRAME_SIZE - ( l_play_position /
                                      p_aout->output.output.i_bytes_per_frame %
                                      FRAME_SIZE );
            }
        }
        else
        {
            l_play_position = FRAME_SIZE;
        }

gbazin's avatar
   
gbazin committed
577
        p_buffer = aout_OutputNextBuffer( p_aout,
gbazin's avatar
   
gbazin committed
578
            mdate() + 1000000 / p_aout->output.output.i_rate * l_play_position,
gbazin's avatar
   
gbazin committed
579
            VLC_FALSE );
580

gbazin's avatar
   
gbazin committed
581
582
583
584
        /* Now do the actual memcpy into the circular buffer */
        if ( l_bytes1 != p_notif->i_buffer_size )
            msg_Err( p_aout, "Wrong buffer size: %d, %d", l_bytes1,
                     p_notif->i_buffer_size );
585

gbazin's avatar
   
gbazin committed
586
        if ( p_buffer != NULL )
587
        {
gbazin's avatar
   
gbazin committed
588
589
590
            p_aout->p_vlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
                                      l_bytes1 );
            aout_BufferFree( p_buffer );
591
        }
gbazin's avatar
   
gbazin committed
592
        else
593
        {
gbazin's avatar
   
gbazin committed
594
            memset( p_write_position, 0, l_bytes1 );
595
596
597
        }

        /* Now the data has been copied, unlock the buffer */
gbazin's avatar
   
gbazin committed
598
599
        IDirectSoundBuffer_Unlock( p_aout->output.p_sys->p_dsbuffer,
                        p_write_position, l_bytes1, p_wrap_around, l_bytes2 );
600
601
602
603
604
605
606
607
608

    }

    /* free the events */
    CloseHandle( notification_events[0] );
    CloseHandle( notification_events[1] );

    msg_Dbg( p_notif, "DirectSoundThread exiting" );
}