1 /*****************************************************************************
2 * aout.c: Windows DirectX audio output method
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: aout.c,v 1.9 2002/08/30 23:27:06 massiot Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <fcntl.h> /* open(), O_WRONLY */
29 #include <string.h> /* strerror() */
31 #include <stdlib.h> /* calloc(), malloc(), free() */
35 #include "aout_internal.h"
40 #define FRAME_SIZE 2048 /* The size is in samples, not in bytes */
42 /*****************************************************************************
44 * Defining them here allows us to get rid of the dxguid library during
46 *****************************************************************************/
48 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf, 0x8, 0x0, 0xa0, 0xc9, 0x25, 0xcd, 0x16);
50 /*****************************************************************************
51 * notification_thread_t: DirectX event thread
52 *****************************************************************************/
53 typedef struct notification_thread_t
57 aout_instance_t * p_aout;
58 DSBPOSITIONNOTIFY p_events[2]; /* play notification events */
59 int i_buffer_size; /* Size in bytes of one frame */
61 } notification_thread_t;
63 /*****************************************************************************
64 * aout_sys_t: directx audio output method descriptor
65 *****************************************************************************
66 * This structure is part of the audio output thread descriptor.
67 * It describes the direct sound specific properties of an audio device.
68 *****************************************************************************/
72 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
74 LPDIRECTSOUNDBUFFER p_dsbuffer_primary; /* the actual sound card buffer
75 (not used directly) */
77 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
78 * takes care of mixing all the
79 * secondary buffers into the primary) */
81 LPDIRECTSOUNDNOTIFY p_dsnotify; /* the position notify interface */
83 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
85 vlc_mutex_t buffer_lock; /* audio buffer lock */
87 notification_thread_t * p_notif; /* DirectSoundThread id */
91 /*****************************************************************************
93 *****************************************************************************/
94 static void Play ( aout_instance_t * );
97 static int DirectxCreateSecondaryBuffer ( aout_instance_t * );
98 static void DirectxDestroySecondaryBuffer( aout_instance_t * );
99 static int DirectxInitDSound ( aout_instance_t * );
100 static void DirectSoundThread ( notification_thread_t * );
102 /*****************************************************************************
103 * OpenAudio: open the audio device
104 *****************************************************************************
105 * This function opens and setups Direct Sound.
106 *****************************************************************************/
107 int E_(OpenAudio) ( vlc_object_t *p_this )
109 aout_instance_t * p_aout = (aout_instance_t *)p_this;
111 DSBUFFERDESC dsbuffer_desc;
113 msg_Dbg( p_aout, "Open" );
115 /* Allocate structure */
116 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
117 if( p_aout->output.p_sys == NULL )
119 msg_Err( p_aout, "out of memory" );
123 /* Initialize some variables */
124 p_aout->output.p_sys->p_dsobject = NULL;
125 p_aout->output.p_sys->p_dsbuffer_primary = NULL;
126 p_aout->output.p_sys->p_dsbuffer = NULL;
127 p_aout->output.p_sys->p_dsnotify = NULL;
128 p_aout->output.p_sys->p_notif = NULL;
129 vlc_mutex_init( p_aout, &p_aout->output.p_sys->buffer_lock );
131 p_aout->output.pf_play = Play;
133 /* Initialise DirectSound */
134 if( DirectxInitDSound( p_aout ) )
136 msg_Err( p_aout, "cannot initialize DirectSound" );
140 /* Obtain (not create) Direct Sound primary buffer */
141 memset( &dsbuffer_desc, 0, sizeof(DSBUFFERDESC) );
142 dsbuffer_desc.dwSize = sizeof(DSBUFFERDESC);
143 dsbuffer_desc.dwFlags = DSBCAPS_PRIMARYBUFFER;
144 msg_Warn( p_aout, "create direct sound primary buffer" );
145 dsresult = IDirectSound_CreateSoundBuffer(p_aout->output.p_sys->p_dsobject,
147 &p_aout->output.p_sys->p_dsbuffer_primary,
149 if( dsresult != DS_OK )
151 msg_Err( p_aout, "cannot create direct sound primary buffer" );
155 /* Now we need to setup DirectSound play notification */
156 p_aout->output.p_sys->p_notif =
157 vlc_object_create( p_aout, sizeof(notification_thread_t) );
158 p_aout->output.p_sys->p_notif->p_aout = p_aout;
160 /* first we need to create the notification events */
161 p_aout->output.p_sys->p_notif->p_events[0].hEventNotify =
162 CreateEvent( NULL, FALSE, FALSE, NULL );
163 p_aout->output.p_sys->p_notif->p_events[1].hEventNotify =
164 CreateEvent( NULL, FALSE, FALSE, NULL );
166 vlc_mutex_lock( &p_aout->output.p_sys->buffer_lock );
168 /* first release the current secondary buffer */
169 DirectxDestroySecondaryBuffer( p_aout );
171 /* calculate the frame size in bytes */
172 p_aout->output.p_sys->p_notif->i_buffer_size = FRAME_SIZE * sizeof(s16)
173 * p_aout->output.output.i_channels;
175 /* then create a new secondary buffer */
176 if( DirectxCreateSecondaryBuffer( p_aout ) )
178 msg_Err( p_aout, "cannot create buffer" );
179 vlc_mutex_unlock( &p_aout->output.p_sys->buffer_lock );
183 vlc_mutex_unlock( &p_aout->output.p_sys->buffer_lock );
185 /* start playing the buffer */
186 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
189 DSBPLAY_LOOPING ); /* Flags */
190 if( dsresult == DSERR_BUFFERLOST )
192 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
193 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
196 DSBPLAY_LOOPING ); /* Flags */
198 if( dsresult != DS_OK )
200 msg_Warn( p_aout, "cannot play buffer" );
203 /* then launch the notification thread */
204 msg_Dbg( p_aout, "creating DirectSoundThread" );
205 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
206 "DirectSound Notification Thread",
207 DirectSoundThread, VLC_THREAD_PRIORITY_OUTPUT, 1 ) )
209 msg_Err( p_aout, "cannot create DirectSoundThread" );
213 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
218 E_(CloseAudio)( VLC_OBJECT(p_aout) );
222 /*****************************************************************************
223 * Play: nothing to do
224 *****************************************************************************/
225 static void Play( aout_instance_t *p_aout )
229 /*****************************************************************************
230 * CloseAudio: close the audio device
231 *****************************************************************************/
232 void E_(CloseAudio) ( vlc_object_t *p_this )
234 aout_instance_t * p_aout = (aout_instance_t *)p_this;
236 msg_Dbg( p_aout, "Close" );
238 /* kill the position notification thread, if any */
239 if( p_aout->output.p_sys->p_notif )
241 vlc_object_detach( p_aout->output.p_sys->p_notif );
242 if( p_aout->output.p_sys->p_notif->b_thread )
244 p_aout->output.p_sys->p_notif->b_die = 1;
245 vlc_thread_join( p_aout->output.p_sys->p_notif );
247 vlc_object_destroy( p_aout->output.p_sys->p_notif );
250 /* release the secondary buffer */
251 DirectxDestroySecondaryBuffer( p_aout );
253 /* then release the primary buffer */
254 if( p_aout->output.p_sys->p_dsbuffer_primary )
255 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer_primary );
257 /* finally release the DirectSound object */
258 if( p_aout->output.p_sys->p_dsobject )
259 IDirectSound_Release( p_aout->output.p_sys->p_dsobject );
261 /* free DSOUND.DLL */
262 if( p_aout->output.p_sys->hdsound_dll )
263 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
265 /* Close the Output. */
266 if ( p_aout->output.p_sys )
268 free( p_aout->output.p_sys );
269 p_aout->output.p_sys = NULL;
273 /*****************************************************************************
274 * DirectxInitDSound: handle all the gory details of DirectSound initialisation
275 *****************************************************************************/
276 static int DirectxInitDSound( aout_instance_t *p_aout )
278 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
280 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
281 if( p_aout->output.p_sys->hdsound_dll == NULL )
283 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
287 OurDirectSoundCreate = (void *)GetProcAddress(
288 p_aout->output.p_sys->hdsound_dll,
289 "DirectSoundCreate" );
290 if( OurDirectSoundCreate == NULL )
292 msg_Warn( p_aout, "GetProcAddress FAILED" );
296 /* Create the direct sound object */
297 if( OurDirectSoundCreate( NULL, &p_aout->output.p_sys->p_dsobject, NULL )
300 msg_Warn( p_aout, "cannot create a direct sound device" );
304 /* Set DirectSound Cooperative level, ie what control we want over Windows
305 * sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
306 * settings of the primary buffer, but also that only the sound of our
307 * application will be hearable when it will have the focus.
308 * !!! (this is not really working as intended yet because to set the
309 * cooperative level you need the window handle of your application, and
310 * I don't know of any easy way to get it. Especially since we might play
311 * sound without any video, and so what window handle should we use ???
312 * The hack for now is to use the Desktop window handle - it seems to be
314 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
318 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
324 p_aout->output.p_sys->p_dsobject = NULL;
325 if( p_aout->output.p_sys->hdsound_dll )
327 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
328 p_aout->output.p_sys->hdsound_dll = NULL;
334 /*****************************************************************************
335 * DirectxCreateSecondaryBuffer
336 *****************************************************************************
337 * This function creates the buffer we'll use to play audio.
338 * In DirectSound there are two kinds of buffers:
339 * - the primary buffer: which is the actual buffer that the soundcard plays
340 * - the secondary buffer(s): these buffers are the one actually used by
341 * applications and DirectSound takes care of mixing them into the primary.
343 * Once you create a secondary buffer, you cannot change its format anymore so
344 * you have to release the current and create another one.
345 *****************************************************************************/
346 static int DirectxCreateSecondaryBuffer( aout_instance_t *p_aout )
348 WAVEFORMATEX waveformat;
349 DSBUFFERDESC dsbdesc;
352 /* First set the buffer format */
353 memset(&waveformat, 0, sizeof(WAVEFORMATEX));
354 waveformat.wFormatTag = WAVE_FORMAT_PCM;
355 waveformat.nChannels = p_aout->output.output.i_channels;
356 waveformat.nSamplesPerSec = p_aout->output.output.i_rate;
357 waveformat.wBitsPerSample = 16;
358 waveformat.nBlockAlign = waveformat.wBitsPerSample / 8 *
359 waveformat.nChannels;
360 waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec *
361 waveformat.nBlockAlign;
363 /* Then fill in the descriptor */
364 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
365 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
366 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2/* Better position accuracy */
367 | DSBCAPS_CTRLPOSITIONNOTIFY /* We need notification */
368 | DSBCAPS_GLOBALFOCUS; /* Allows background playing */
369 dsbdesc.dwBufferBytes = FRAME_SIZE * 2 /* frames*/ * /* buffer size */
370 sizeof(s16) * p_aout->output.output.i_channels;
371 dsbdesc.lpwfxFormat = &waveformat;
373 if( IDirectSound_CreateSoundBuffer( p_aout->output.p_sys->p_dsobject,
375 &p_aout->output.p_sys->p_dsbuffer,
378 msg_Warn( p_aout, "cannot create direct sound secondary buffer" );
382 /* backup the size of the secondary sound buffer */
383 memset(&dsbcaps, 0, sizeof(DSBCAPS));
384 dsbcaps.dwSize = sizeof(DSBCAPS);
385 IDirectSoundBuffer_GetCaps( p_aout->output.p_sys->p_dsbuffer, &dsbcaps );
387 msg_Dbg( p_aout, "DirectxCreateSecondaryBuffer: %li",
388 dsbcaps.dwBufferBytes );
390 /* Now the secondary buffer is created, we need to setup its position
392 p_aout->output.p_sys->p_notif->p_events[0].dwOffset = 0;
393 p_aout->output.p_sys->p_notif->p_events[1].dwOffset =
394 p_aout->output.p_sys->p_notif->i_buffer_size;
396 /* Get the IDirectSoundNotify interface */
397 if FAILED( IDirectSoundBuffer_QueryInterface(
398 p_aout->output.p_sys->p_dsbuffer,
399 &IID_IDirectSoundNotify,
400 (LPVOID *)&p_aout->output.p_sys->p_dsnotify ) )
402 msg_Warn( p_aout, "cannot get Notify interface" );
406 if FAILED( IDirectSoundNotify_SetNotificationPositions(
407 p_aout->output.p_sys->p_dsnotify, 2,
408 p_aout->output.p_sys->p_notif->p_events ) )
410 msg_Warn( p_aout, "cannot set position Notification" );
414 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
415 p_aout->output.i_nb_samples = FRAME_SIZE;
420 if( p_aout->output.p_sys->p_dsbuffer )
422 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
423 p_aout->output.p_sys->p_dsbuffer = NULL;
425 if( p_aout->output.p_sys->p_dsnotify )
427 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
428 p_aout->output.p_sys->p_dsnotify = NULL;
433 /*****************************************************************************
434 * DirectxCreateSecondaryBuffer
435 *****************************************************************************
436 * This function destroys the secondary buffer.
437 *****************************************************************************/
438 static void DirectxDestroySecondaryBuffer( aout_instance_t *p_aout )
440 /* make sure the buffer isn't playing */
441 if( p_aout->output.p_sys->p_dsbuffer )
442 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
444 if( p_aout->output.p_sys->p_dsnotify )
446 IDirectSoundNotify_Release( p_aout->output.p_sys->p_dsnotify );
447 p_aout->output.p_sys->p_dsnotify = NULL;
450 if( p_aout->output.p_sys->p_dsbuffer )
452 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
453 p_aout->output.p_sys->p_dsbuffer = NULL;
457 /*****************************************************************************
458 * DirectSoundThread: this thread will capture play notification events.
459 *****************************************************************************
460 * We use this thread to emulate a callback mechanism. The thread probes for
461 * event notification and fills up the DS secondary buffer when needed.
462 *****************************************************************************/
463 static void DirectSoundThread( notification_thread_t *p_notif )
465 HANDLE notification_events[2];
467 aout_instance_t *p_aout = p_notif->p_aout;
469 notification_events[0] = p_notif->p_events[0].hEventNotify;
470 notification_events[1] = p_notif->p_events[1].hEventNotify;
472 /* Tell the main thread that we are ready */
473 vlc_thread_ready( p_notif );
475 /* this thread must be high-priority */
476 if( !SetThreadPriority( GetCurrentThread(),
477 THREAD_PRIORITY_ABOVE_NORMAL ) )
479 msg_Warn( p_notif, "DirectSoundThread could not raise its priority" );
482 msg_Dbg( p_notif, "DirectSoundThread ready" );
484 while( !p_notif->b_die )
487 void *p_write_position, *p_wrap_around;
488 long l_bytes1, l_bytes2;
489 aout_buffer_t * p_buffer;
491 /* wait for the position notification */
492 i_which_event = WaitForMultipleObjects( 2, notification_events, 0,
493 INFINITE ) - WAIT_OBJECT_0;
495 vlc_mutex_lock( &p_aout->output.p_sys->buffer_lock );
499 vlc_mutex_unlock( &p_aout->output.p_sys->buffer_lock );
503 /* Before copying anything, we have to lock the buffer */
504 dsresult = IDirectSoundBuffer_Lock(
506 p_aout->output.p_sys->p_dsbuffer,
507 /* Offset of lock start */
508 i_which_event ? 0 : p_notif->i_buffer_size,
509 p_notif->i_buffer_size, /* Number of bytes */
510 &p_write_position, /* Address of lock start */
511 &l_bytes1, /* Count of bytes locked before wrap around */
512 &p_wrap_around, /* Buffer adress (if wrap around) */
513 &l_bytes2, /* Count of bytes after wrap around */
515 if( dsresult == DSERR_BUFFERLOST )
517 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
518 dsresult = IDirectSoundBuffer_Lock(
519 p_aout->output.p_sys->p_dsbuffer,
520 i_which_event ? 0 : p_notif->i_buffer_size,
521 p_notif->i_buffer_size,
528 if( dsresult != DS_OK )
530 msg_Warn( p_notif, "cannot lock buffer" );
531 vlc_mutex_unlock( &p_aout->output.p_sys->buffer_lock );
535 /* FIXME : take into account DirectSound latency instead of mdate() */
536 p_buffer = aout_OutputNextBuffer( p_aout, mdate(), VLC_FALSE );
538 /* Now do the actual memcpy into the circular buffer */
539 if ( l_bytes1 != p_notif->i_buffer_size )
540 msg_Err( p_aout, "Wrong buffer size: %d, %d", l_bytes1,
541 p_notif->i_buffer_size );
543 if ( p_buffer != NULL )
545 p_aout->p_vlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
547 aout_BufferFree( p_buffer );
551 memset( p_write_position, 0, l_bytes1 );
554 /* Now the data has been copied, unlock the buffer */
555 IDirectSoundBuffer_Unlock( p_aout->output.p_sys->p_dsbuffer,
556 p_write_position, l_bytes1, p_wrap_around, l_bytes2 );
558 vlc_mutex_unlock( &p_aout->output.p_sys->buffer_lock );
562 /* free the events */
563 CloseHandle( notification_events[0] );
564 CloseHandle( notification_events[1] );
566 msg_Dbg( p_notif, "DirectSoundThread exiting" );