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 $
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"
41 #define FRAME_SIZE 2048 /* The size is in samples, not in bytes */
44 /* frame buffer status */
45 #define FRAME_QUEUED 0
48 /*****************************************************************************
50 * Defining them here allows us to get rid of the dxguid library during
52 *****************************************************************************/
54 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf, 0x8, 0x0, 0xa0, 0xc9, 0x25, 0xcd, 0x16);
56 /*****************************************************************************
58 *****************************************************************************/
59 #ifndef WAVE_FORMAT_IEEE_FLOAT
60 # define WAVE_FORMAT_IEEE_FLOAT 0x0003
63 /*****************************************************************************
64 * notification_thread_t: DirectX event thread
65 *****************************************************************************/
66 typedef struct notification_thread_t
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 */
77 } notification_thread_t;
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 *****************************************************************************/
87 LPDIRECTSOUND p_dsobject; /* main Direct Sound object */
89 LPDIRECTSOUNDBUFFER p_dsbuffer_primary; /* the actual sound card buffer
90 (not used directly) */
92 LPDIRECTSOUNDBUFFER p_dsbuffer; /* the sound buffer we use (direct sound
93 * takes care of mixing all the
94 * secondary buffers into the primary) */
96 LPDIRECTSOUNDNOTIFY p_dsnotify; /* the position notify interface */
98 HINSTANCE hdsound_dll; /* handle of the opened dsound dll */
100 notification_thread_t * p_notif; /* DirectSoundThread id */
102 int b_playing; /* playing status */
105 /*****************************************************************************
107 *****************************************************************************/
108 static int OpenAudio ( vlc_object_t * );
109 static void CloseAudio ( vlc_object_t * );
111 static void Play ( aout_instance_t * );
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,
121 /*****************************************************************************
123 *****************************************************************************/
125 set_description( _("DirectX audio module") );
126 set_capability( "audio output", 100 );
127 add_shortcut( "directx" );
128 set_callbacks( OpenAudio, CloseAudio );
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 )
138 aout_instance_t * p_aout = (aout_instance_t *)p_this;
140 DSBUFFERDESC dsbuffer_desc;
143 msg_Dbg( p_aout, "Open" );
145 /* Allocate structure */
146 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
147 if( p_aout->output.p_sys == NULL )
149 msg_Err( p_aout, "out of memory" );
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;
161 p_aout->output.pf_play = Play;
162 aout_VolumeSoftInit( p_aout );
164 /* Initialise DirectSound */
165 if( DirectxInitDSound( p_aout ) )
167 msg_Err( p_aout, "cannot initialize DirectSound" );
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,
178 &p_aout->output.p_sys->p_dsbuffer_primary,
180 if( dsresult != DS_OK )
182 msg_Err( p_aout, "cannot create direct sound primary buffer" );
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;
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 );
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 ) )
200 msg_Err( p_aout, "cannot create WAVE_FORMAT_IEEE_FLOAT buffer" );
202 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
203 if( DirectxCreateSecondaryBuffer( p_aout ) )
205 msg_Err( p_aout, "cannot create WAVE_FORMAT_PCM buffer" );
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",
215 THREAD_PRIORITY_TIME_CRITICAL, 1 ) )
217 msg_Err( p_aout, "cannot create DirectSoundThread" );
221 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
226 CloseAudio( VLC_OBJECT(p_aout) );
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
234 *****************************************************************************/
235 static void Play( aout_instance_t *p_aout )
237 if( !p_aout->output.p_sys->b_playing )
239 aout_buffer_t *p_buffer;
241 p_aout->output.p_sys->b_playing = 1;
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 );
247 /* fill in the first samples */
248 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
249 DirectxFillBuffer( p_aout, 0, p_buffer );
251 /* wake up the audio output thread */
252 SetEvent( p_aout->output.p_sys->p_notif->p_events[0].hEventNotify );
256 /*****************************************************************************
257 * CloseAudio: close the audio device
258 *****************************************************************************/
259 static void CloseAudio( vlc_object_t *p_this )
261 aout_instance_t * p_aout = (aout_instance_t *)p_this;
263 msg_Dbg( p_aout, "Close" );
265 /* kill the position notification thread, if any */
266 if( p_aout->output.p_sys->p_notif )
268 vlc_object_detach( p_aout->output.p_sys->p_notif );
269 if( p_aout->output.p_sys->p_notif->b_thread )
271 p_aout->output.p_sys->p_notif->b_die = 1;
273 if( !p_aout->output.p_sys->b_playing )
274 /* wake up the audio thread */
276 p_aout->output.p_sys->p_notif->p_events[0].hEventNotify );
278 vlc_thread_join( p_aout->output.p_sys->p_notif );
280 vlc_object_destroy( p_aout->output.p_sys->p_notif );
283 /* release the secondary buffer */
284 DirectxDestroySecondaryBuffer( p_aout );
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 );
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 );
294 /* free DSOUND.DLL */
295 if( p_aout->output.p_sys->hdsound_dll )
296 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
298 free( p_aout->output.p_sys );
301 /*****************************************************************************
302 * DirectxInitDSound: handle all the gory details of DirectSound initialisation
303 *****************************************************************************/
304 static int DirectxInitDSound( aout_instance_t *p_aout )
306 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
308 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
309 if( p_aout->output.p_sys->hdsound_dll == NULL )
311 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
315 OurDirectSoundCreate = (void *)GetProcAddress(
316 p_aout->output.p_sys->hdsound_dll,
317 "DirectSoundCreate" );
318 if( OurDirectSoundCreate == NULL )
320 msg_Warn( p_aout, "GetProcAddress FAILED" );
324 /* Create the direct sound object */
325 if( OurDirectSoundCreate( NULL, &p_aout->output.p_sys->p_dsobject, NULL )
328 msg_Warn( p_aout, "cannot create a direct sound device" );
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
342 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
346 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
352 p_aout->output.p_sys->p_dsobject = NULL;
353 if( p_aout->output.p_sys->hdsound_dll )
355 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
356 p_aout->output.p_sys->hdsound_dll = NULL;
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.
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 )
376 WAVEFORMATEX waveformat;
377 DSBUFFERDESC dsbdesc;
379 int i_nb_channels, i;
381 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
382 if ( i_nb_channels > 2 )
385 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
388 /* First set the buffer format */
389 memset( &waveformat, 0, sizeof(WAVEFORMATEX) );
390 switch( p_aout->output.output.i_format )
392 case VLC_FOURCC('s','1','6','l'):
393 waveformat.wFormatTag = WAVE_FORMAT_PCM;
394 waveformat.wBitsPerSample = 16;
396 case VLC_FOURCC('f','l','3','2'):
397 waveformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
398 waveformat.wBitsPerSample = sizeof(float) * 8;
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;
408 aout_FormatPrepare( &p_aout->output.output );
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;
420 if( IDirectSound_CreateSoundBuffer( p_aout->output.p_sys->p_dsobject,
422 &p_aout->output.p_sys->p_dsbuffer,
425 msg_Warn( p_aout, "cannot create direct sound secondary buffer" );
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;
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 );
440 /* Now the secondary buffer is created, we need to setup its position
442 for( i = 0; i < FRAMES_NUM; i++ )
444 p_aout->output.p_sys->p_notif->p_events[i].dwOffset = i *
445 p_aout->output.p_sys->p_notif->i_frame_size;
447 p_aout->output.p_sys->p_notif->i_frame_status[i] = FRAME_EMPTY;
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 ) )
456 msg_Err( p_aout, "cannot get Notify interface" );
460 if FAILED( IDirectSoundNotify_SetNotificationPositions(
461 p_aout->output.p_sys->p_dsnotify,
463 p_aout->output.p_sys->p_notif->p_events ) )
465 msg_Err( p_aout, "cannot set position Notification" );
469 p_aout->output.i_nb_samples = FRAME_SIZE;
474 if( p_aout->output.p_sys->p_dsbuffer )
476 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
477 p_aout->output.p_sys->p_dsbuffer = NULL;
479 if( p_aout->output.p_sys->p_dsnotify )
481 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
482 p_aout->output.p_sys->p_dsnotify = NULL;
487 /*****************************************************************************
488 * DirectxCreateSecondaryBuffer
489 *****************************************************************************
490 * This function destroys the secondary buffer.
491 *****************************************************************************/
492 static void DirectxDestroySecondaryBuffer( aout_instance_t *p_aout )
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 );
498 if( p_aout->output.p_sys->p_dsnotify )
500 IDirectSoundNotify_Release( p_aout->output.p_sys->p_dsnotify );
501 p_aout->output.p_sys->p_dsnotify = NULL;
504 if( p_aout->output.p_sys->p_dsbuffer )
506 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
507 p_aout->output.p_sys->p_dsbuffer = NULL;
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 )
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;
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 */
534 if( dsresult == DSERR_BUFFERLOST )
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,
547 if( dsresult != DS_OK )
549 msg_Warn( p_notif, "cannot lock buffer" );
550 if( p_buffer ) aout_BufferFree( p_buffer );
554 if ( p_buffer != NULL )
556 p_aout->p_vlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
558 aout_BufferFree( p_buffer );
561 memset( p_write_position, 0, l_bytes1 );
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 );
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 )
579 HANDLE notification_events[FRAMES_NUM];
581 aout_instance_t *p_aout = p_notif->p_aout;
582 int i, i_which_frame, i_last_frame, i_next_frame;
585 for( i = 0; i < FRAMES_NUM; i++ )
586 notification_events[i] = p_notif->p_events[i].hEventNotify;
588 /* Tell the main thread that we are ready */
589 vlc_thread_ready( p_notif );
591 msg_Dbg( p_notif, "DirectSoundThread ready" );
593 /* Wait here until Play() is called */
594 WaitForSingleObject( notification_events[0], INFINITE );
596 if( !p_notif->b_die )
598 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
600 /* start playing the buffer */
601 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
604 DSBPLAY_LOOPING ); /* Flags */
605 if( dsresult == DSERR_BUFFERLOST )
607 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
608 dsresult = IDirectSoundBuffer_Play(
609 p_aout->output.p_sys->p_dsbuffer,
612 DSBPLAY_LOOPING ); /* Flags */
614 if( dsresult != DS_OK )
616 msg_Err( p_aout, "cannot start playing buffer" );
620 while( !p_notif->b_die )
622 aout_buffer_t *p_buffer;
625 /* wait for the position notification */
626 i_which_frame = WaitForMultipleObjects( FRAMES_NUM,
627 notification_events, 0,
628 INFINITE ) - WAIT_OBJECT_0;
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 )
640 if( l_latency > (i_which_frame * FRAME_SIZE)
641 && l_latency < ((i_which_frame+1) * FRAME_SIZE) )
643 l_latency = - ( l_latency /
644 p_aout->output.output.i_bytes_per_frame %
649 l_latency = FRAME_SIZE - ( l_latency /
650 p_aout->output.output.i_bytes_per_frame %
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;
664 /* Try to fill in as many frame buffers as possible */
665 for( i = i_next_frame; (i % FRAMES_NUM) != i_which_frame; i++ )
668 /* Check if frame buf is already filled */
669 if( p_notif->i_frame_status[i % FRAMES_NUM] == FRAME_QUEUED )
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 );
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) )
680 //msg_Err( p_aout, "only %i frame buffers filled!",
681 // i - i_next_frame );
685 if( DirectxFillBuffer( p_aout, (i%FRAMES_NUM), p_buffer )
689 /* Mark the frame buffer as QUEUED */
690 p_notif->i_frame_status[i%FRAMES_NUM] = FRAME_QUEUED;
695 /* free the events */
696 for( i = 0; i < FRAMES_NUM; i++ )
697 CloseHandle( notification_events[i] );
699 msg_Dbg( p_notif, "DirectSoundThread exiting" );