directx.c 22.4 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * aout.c: Windows DirectX audio output method
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
Gildas Bazin's avatar
 
Gildas Bazin committed
5
 * $Id: directx.c,v 1.3 2002/10/11 10:08:06 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>
Gildas Bazin's avatar
 
Gildas Bazin committed
35
#include "aout_internal.h"
36

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

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

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/*****************************************************************************
 * 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);

/*****************************************************************************
 * notification_thread_t: DirectX event thread
 *****************************************************************************/
typedef struct notification_thread_t
{
    VLC_COMMON_MEMBERS

Gildas Bazin's avatar
 
Gildas Bazin committed
58
    aout_instance_t * p_aout;
59
    DSBPOSITIONNOTIFY p_events[2];               /* play notification events */
Gildas Bazin's avatar
 
Gildas Bazin committed
60
    int i_buffer_size;                         /* Size in bytes of one frame */
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

} 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 */

    vlc_mutex_t buffer_lock;                            /* audio buffer lock */

    notification_thread_t * p_notif;                 /* DirectSoundThread id */
Gildas Bazin's avatar
 
Gildas Bazin committed
88

89 90 91 92 93
};

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
94 95
static int  OpenAudio  ( vlc_object_t * );
static void CloseAudio ( vlc_object_t * );
96

Gildas Bazin's avatar
 
Gildas Bazin committed
97
static void Play       ( aout_instance_t * );
98 99

/* local functions */
Gildas Bazin's avatar
 
Gildas Bazin committed
100 101 102
static int  DirectxCreateSecondaryBuffer ( aout_instance_t * );
static void DirectxDestroySecondaryBuffer( aout_instance_t * );
static int  DirectxInitDSound            ( aout_instance_t * );
103 104
static void DirectSoundThread            ( notification_thread_t * );

Gildas Bazin's avatar
 
Gildas Bazin committed
105 106 107 108 109 110 111 112 113 114
/*****************************************************************************
 * 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();

115 116 117 118 119
/*****************************************************************************
 * OpenAudio: open the audio device
 *****************************************************************************
 * This function opens and setups Direct Sound.
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
120
static int OpenAudio( vlc_object_t *p_this )
121
{
Gildas Bazin's avatar
 
Gildas Bazin committed
122
    aout_instance_t * p_aout = (aout_instance_t *)p_this;
123 124 125 126 127 128
    HRESULT dsresult;
    DSBUFFERDESC dsbuffer_desc;

    msg_Dbg( p_aout, "Open" );

   /* Allocate structure */
Gildas Bazin's avatar
 
Gildas Bazin committed
129 130
    p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
    if( p_aout->output.p_sys == NULL )
131 132
    {
        msg_Err( p_aout, "out of memory" );
Gildas Bazin's avatar
 
Gildas Bazin committed
133
        return 1;
134 135 136
    }

    /* Initialize some variables */
Gildas Bazin's avatar
 
Gildas Bazin committed
137 138 139 140 141 142 143 144
    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;
    vlc_mutex_init( p_aout, &p_aout->output.p_sys->buffer_lock );

    p_aout->output.pf_play = Play;
145
    aout_VolumeSoftInit( p_aout );
146 147 148 149

    /* Initialise DirectSound */
    if( DirectxInitDSound( p_aout ) )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
150 151
        msg_Err( p_aout, "cannot initialize DirectSound" );
        goto error;
152 153 154 155 156 157 158
    }

    /* 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" );
Gildas Bazin's avatar
 
Gildas Bazin committed
159 160 161 162
    dsresult = IDirectSound_CreateSoundBuffer(p_aout->output.p_sys->p_dsobject,
                                     &dsbuffer_desc,
                                     &p_aout->output.p_sys->p_dsbuffer_primary,
                                     NULL);
163 164
    if( dsresult != DS_OK )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
165 166
        msg_Err( p_aout, "cannot create direct sound primary buffer" );
        goto error;
167 168 169
    }

    /* Now we need to setup DirectSound play notification */
Gildas Bazin's avatar
 
Gildas Bazin committed
170 171 172
    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;
173 174

    /* first we need to create the notification events */
Gildas Bazin's avatar
 
Gildas Bazin committed
175
    p_aout->output.p_sys->p_notif->p_events[0].hEventNotify =
176
        CreateEvent( NULL, FALSE, FALSE, NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
177
    p_aout->output.p_sys->p_notif->p_events[1].hEventNotify =
178 179
        CreateEvent( NULL, FALSE, FALSE, NULL );

Gildas Bazin's avatar
 
Gildas Bazin committed
180
    vlc_mutex_lock( &p_aout->output.p_sys->buffer_lock );
181 182 183 184

    /* then create a new secondary buffer */
    if( DirectxCreateSecondaryBuffer( p_aout ) )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
185 186 187
        msg_Err( p_aout, "cannot create buffer" );
        vlc_mutex_unlock( &p_aout->output.p_sys->buffer_lock );
        return 1;
188 189
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
190
    vlc_mutex_unlock( &p_aout->output.p_sys->buffer_lock );
191

Gildas Bazin's avatar
 
Gildas Bazin committed
192 193
    /* start playing the buffer */
    dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
194 195 196 197 198
                                        0,                         /* Unused */
                                        0,                         /* Unused */
                                        DSBPLAY_LOOPING );          /* Flags */
    if( dsresult == DSERR_BUFFERLOST )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
199 200
        IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
        dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
201 202 203 204 205 206
                                            0,                     /* Unused */
                                            0,                     /* Unused */
                                            DSBPLAY_LOOPING );      /* Flags */
    }
    if( dsresult != DS_OK )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
207
        msg_Warn( p_aout, "cannot play buffer" );
208 209
    }

Christophe Massiot's avatar
Christophe Massiot committed
210 211 212 213 214 215 216 217 218 219 220 221
    /* 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",
                           DirectSoundThread, VLC_THREAD_PRIORITY_OUTPUT, 1 ) )
    {
        msg_Err( p_aout, "cannot create DirectSoundThread" );
        goto error;
    }

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

Gildas Bazin's avatar
 
Gildas Bazin committed
222
    return 0;
Christophe Massiot's avatar
Christophe Massiot committed
223 224

 error:
Gildas Bazin's avatar
 
Gildas Bazin committed
225
    CloseAudio( VLC_OBJECT(p_aout) );
Christophe Massiot's avatar
Christophe Massiot committed
226
    return 1;
Gildas Bazin's avatar
 
Gildas Bazin committed
227 228 229
}

/*****************************************************************************
Christophe Massiot's avatar
Christophe Massiot committed
230
 * Play: nothing to do
Gildas Bazin's avatar
 
Gildas Bazin committed
231
 *****************************************************************************/
232
static void Play( aout_instance_t *p_aout )
Gildas Bazin's avatar
 
Gildas Bazin committed
233
{
234 235 236 237 238
}

/*****************************************************************************
 * CloseAudio: close the audio device
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
239
static void CloseAudio( vlc_object_t *p_this )
240
{
Gildas Bazin's avatar
 
Gildas Bazin committed
241
    aout_instance_t * p_aout = (aout_instance_t *)p_this;
242 243 244 245

    msg_Dbg( p_aout, "Close" );

    /* kill the position notification thread, if any */
Gildas Bazin's avatar
 
Gildas Bazin committed
246
    if( p_aout->output.p_sys->p_notif )
247
    {
248
        vlc_object_detach( p_aout->output.p_sys->p_notif );
Gildas Bazin's avatar
 
Gildas Bazin committed
249 250 251 252 253 254
        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 );
255 256 257 258 259 260
    }

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

    /* then release the primary buffer */
Gildas Bazin's avatar
 
Gildas Bazin committed
261 262
    if( p_aout->output.p_sys->p_dsbuffer_primary )
        IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer_primary );
263 264

    /* finally release the DirectSound object */
Gildas Bazin's avatar
 
Gildas Bazin committed
265 266
    if( p_aout->output.p_sys->p_dsobject )
        IDirectSound_Release( p_aout->output.p_sys->p_dsobject );
267 268
    
    /* free DSOUND.DLL */
Gildas Bazin's avatar
 
Gildas Bazin committed
269 270
    if( p_aout->output.p_sys->hdsound_dll )
       FreeLibrary( p_aout->output.p_sys->hdsound_dll );
271 272

    /* Close the Output. */
Gildas Bazin's avatar
 
Gildas Bazin committed
273
    if ( p_aout->output.p_sys  )
274
    { 
Gildas Bazin's avatar
 
Gildas Bazin committed
275 276
        free( p_aout->output.p_sys );
        p_aout->output.p_sys = NULL;
277 278 279 280
    }
}

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

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

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

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

    /* 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 */
Gildas Bazin's avatar
 
Gildas Bazin committed
321 322 323
    if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
                                          GetDesktopWindow(),
                                          DSSCL_EXCLUSIVE) )
324 325 326 327
    {
        msg_Warn( p_aout, "cannot set direct sound cooperative level" );
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
328 329 330 331 332 333 334 335 336 337 338
    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;

339 340 341 342 343 344 345 346 347 348 349 350 351 352
}

/*****************************************************************************
 * 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.
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
353
static int DirectxCreateSecondaryBuffer( aout_instance_t *p_aout )
354 355 356 357 358
{
    WAVEFORMATEX         waveformat;
    DSBUFFERDESC         dsbdesc;
    DSBCAPS              dsbcaps;

Gildas Bazin's avatar
 
Gildas Bazin committed
359 360 361
    if( p_aout->output.output.i_channels > 2 )
        p_aout->output.output.i_channels = 2;

362
    /* First set the buffer format */
Gildas Bazin's avatar
 
Gildas Bazin committed
363 364
    memset(&waveformat, 0, sizeof(WAVEFORMATEX));
    waveformat.wFormatTag      = WAVE_FORMAT_PCM;
Gildas Bazin's avatar
 
Gildas Bazin committed
365
    waveformat.nChannels       = p_aout->output.output.i_channels;
Gildas Bazin's avatar
 
Gildas Bazin committed
366 367
    waveformat.nSamplesPerSec  = p_aout->output.output.i_rate;
    waveformat.wBitsPerSample  = 16;
368 369 370 371 372 373
    waveformat.nBlockAlign     = waveformat.wBitsPerSample / 8 *
                                 waveformat.nChannels;
    waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec *
                                     waveformat.nBlockAlign;

    /* Then fill in the descriptor */
Gildas Bazin's avatar
 
Gildas Bazin committed
374 375
    memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
    dsbdesc.dwSize = sizeof(DSBUFFERDESC);
376 377 378
    dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
                    | DSBCAPS_CTRLPOSITIONNOTIFY     /* We need notification */
                    | DSBCAPS_GLOBALFOCUS;      /* Allows background playing */
Gildas Bazin's avatar
 
Gildas Bazin committed
379 380
    dsbdesc.dwBufferBytes = FRAME_SIZE * 2 /* frames*/ *      /* buffer size */
                            sizeof(s16) * p_aout->output.output.i_channels;
Gildas Bazin's avatar
 
Gildas Bazin committed
381
    dsbdesc.lpwfxFormat = &waveformat;
382
 
Gildas Bazin's avatar
 
Gildas Bazin committed
383
    if( IDirectSound_CreateSoundBuffer( p_aout->output.p_sys->p_dsobject,
384
                                        &dsbdesc,
Gildas Bazin's avatar
 
Gildas Bazin committed
385
                                        &p_aout->output.p_sys->p_dsbuffer,
386 387 388
                                        NULL) != DS_OK )
    {
        msg_Warn( p_aout, "cannot create direct sound secondary buffer" );
Gildas Bazin's avatar
 
Gildas Bazin committed
389
        goto error;
390 391
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
392 393 394 395 396
    /* backup the size of a frame */
    p_aout->output.p_sys->p_notif->i_buffer_size = FRAME_SIZE * sizeof(s16)
                                            * p_aout->output.output.i_channels;

    memset(&dsbcaps, 0, sizeof(DSBCAPS));
397
    dsbcaps.dwSize = sizeof(DSBCAPS);
Gildas Bazin's avatar
 
Gildas Bazin committed
398
    IDirectSoundBuffer_GetCaps( p_aout->output.p_sys->p_dsbuffer, &dsbcaps  );
Gildas Bazin's avatar
 
Gildas Bazin committed
399 400
    msg_Dbg( p_aout, "requested %li bytes buffer and got %li bytes.",
             2 * p_aout->output.p_sys->p_notif->i_buffer_size,
Gildas Bazin's avatar
 
Gildas Bazin committed
401
             dsbcaps.dwBufferBytes );
402 403 404

    /* Now the secondary buffer is created, we need to setup its position
     * notification */
Gildas Bazin's avatar
 
Gildas Bazin committed
405 406 407
    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;
408 409

    /* Get the IDirectSoundNotify interface */
Gildas Bazin's avatar
 
Gildas Bazin committed
410 411 412 413
    if FAILED( IDirectSoundBuffer_QueryInterface(
                                p_aout->output.p_sys->p_dsbuffer,
                                &IID_IDirectSoundNotify,
                                (LPVOID *)&p_aout->output.p_sys->p_dsnotify ) )
414
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
415
        msg_Err( p_aout, "cannot get Notify interface" );
Gildas Bazin's avatar
 
Gildas Bazin committed
416
        goto error;
417 418 419
    }
        
    if FAILED( IDirectSoundNotify_SetNotificationPositions(
Gildas Bazin's avatar
 
Gildas Bazin committed
420 421
                                    p_aout->output.p_sys->p_dsnotify, 2,
                                    p_aout->output.p_sys->p_notif->p_events ) )
422
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
423
        msg_Err( p_aout, "cannot set position Notification" );
Gildas Bazin's avatar
 
Gildas Bazin committed
424
        goto error;
425
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    p_aout->output.output.i_format = AOUT_FMT_S16_NE;
    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;
    }
    return 1;
443 444 445 446 447
}

/*****************************************************************************
 * DirectxCreateSecondaryBuffer
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
448
 * This function destroys the secondary buffer.
449
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
450
static void DirectxDestroySecondaryBuffer( aout_instance_t *p_aout )
451 452
{
    /* make sure the buffer isn't playing */
Gildas Bazin's avatar
 
Gildas Bazin committed
453 454
    if( p_aout->output.p_sys->p_dsbuffer )
        IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
455

Gildas Bazin's avatar
 
Gildas Bazin committed
456
    if( p_aout->output.p_sys->p_dsnotify )
457
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
458 459
        IDirectSoundNotify_Release( p_aout->output.p_sys->p_dsnotify );
        p_aout->output.p_sys->p_dsnotify = NULL;
460 461
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
462
    if( p_aout->output.p_sys->p_dsbuffer )
463
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
464 465
        IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
        p_aout->output.p_sys->p_dsbuffer = NULL;
466 467 468 469 470 471
    }
}

/*****************************************************************************
 * DirectSoundThread: this thread will capture play notification events. 
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
472 473
 * We use this thread to emulate a callback mechanism. The thread probes for
 * event notification and fills up the DS secondary buffer when needed.
474 475 476 477 478
 *****************************************************************************/
static void DirectSoundThread( notification_thread_t *p_notif )
{
    HANDLE  notification_events[2];
    HRESULT dsresult;
Gildas Bazin's avatar
 
Gildas Bazin committed
479
    aout_instance_t *p_aout = p_notif->p_aout;
480

Gildas Bazin's avatar
 
Gildas Bazin committed
481 482
    notification_events[0] = p_notif->p_events[0].hEventNotify;
    notification_events[1] = p_notif->p_events[1].hEventNotify;
483 484 485 486 487 488 489 490

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

    msg_Dbg( p_notif, "DirectSoundThread ready" );

    while( !p_notif->b_die )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
491 492 493 494 495
        int i_which_event;
        void *p_write_position, *p_wrap_around;
        long l_bytes1, l_bytes2;
        aout_buffer_t * p_buffer;

496
        /* wait for the position notification */
Gildas Bazin's avatar
 
Gildas Bazin committed
497 498 499 500
        i_which_event = WaitForMultipleObjects( 2, notification_events, 0,
                                                INFINITE ) - WAIT_OBJECT_0;

        vlc_mutex_lock( &p_aout->output.p_sys->buffer_lock );
501 502 503

        if( p_notif->b_die )
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
504
            vlc_mutex_unlock( &p_aout->output.p_sys->buffer_lock );
505 506 507
            break;
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
508 509 510 511 512 513 514 515 516 517 518 519 520
        /* 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 )
521
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
522 523 524 525 526 527 528 529 530 531
            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 );
532
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
533
        if( dsresult != DS_OK )
534
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
535 536 537
            msg_Warn( p_notif, "cannot lock buffer" );
            vlc_mutex_unlock( &p_aout->output.p_sys->buffer_lock );
            continue;
538 539
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
540 541 542 543
        /* We also take into account the latency instead of just mdate() */
        p_buffer = aout_OutputNextBuffer( p_aout,
            mdate() + 1000000 / p_aout->output.output.i_rate * FRAME_SIZE,
            VLC_FALSE );
544

Gildas Bazin's avatar
 
Gildas Bazin committed
545 546 547 548
        /* 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 );
549

Gildas Bazin's avatar
 
Gildas Bazin committed
550
        if ( p_buffer != NULL )
551
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
552 553 554
            p_aout->p_vlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
                                      l_bytes1 );
            aout_BufferFree( p_buffer );
555
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
556
        else
557
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
558
            memset( p_write_position, 0, l_bytes1 );
559 560 561
        }

        /* Now the data has been copied, unlock the buffer */
Gildas Bazin's avatar
 
Gildas Bazin committed
562 563
        IDirectSoundBuffer_Unlock( p_aout->output.p_sys->p_dsbuffer,
                        p_write_position, l_bytes1, p_wrap_around, l_bytes2 );
564

Gildas Bazin's avatar
 
Gildas Bazin committed
565
        vlc_mutex_unlock( &p_aout->output.p_sys->buffer_lock );
566 567 568 569 570 571 572 573 574 575

    }

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

    msg_Dbg( p_notif, "DirectSoundThread exiting" );

}