2 * Copyright (C) 2005 Sebastien Moutte <sebastien@moutte.net>
3 * Copyright (C) 2007 Pioneers of the Inevitable <songbird@songbirdnest.com>
4 * Copyright (C) 2010 Fluendo S.A. <support@fluendo.com>
6 * gstdirectsoundsink.c:
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 * The development of this code was made possible due to the involvement
25 * of Pioneers of the Inevitable, the creators of the Songbird Music player
30 * SECTION:element-directsoundsink
32 * This element lets you output sound using the DirectSound API.
34 * Note that you should almost always use generic audio conversion elements
35 * like audioconvert and audioresample in front of an audiosink to make sure
36 * your pipeline works under all circumstances (those conversion elements will
37 * act in passthrough-mode if no conversion is necessary).
40 * <title>Example pipelines</title>
42 * gst-launch -v audiotestsrc ! audioconvert ! volume volume=0.1 ! directsoundsink
43 * ]| will output a sine wave (continuous beep sound) to your sound card (with
44 * a very low volume as precaution).
46 * gst-launch -v filesrc location=music.ogg ! decodebin ! audioconvert ! audioresample ! directsoundsink
47 * ]| will play an Ogg/Vorbis audio file and output it.
55 #include "gstdirectsoundsink.h"
66 GST_DEBUG_CATEGORY_STATIC (directsoundsink_debug);
67 #define GST_CAT_DEFAULT directsoundsink_debug
69 static void gst_directsound_sink_finalise (GObject * object);
71 static void gst_directsound_sink_set_property (GObject * object, guint prop_id,
72 const GValue * value, GParamSpec * pspec);
73 static void gst_directsound_sink_get_property (GObject * object, guint prop_id,
74 GValue * value, GParamSpec * pspec);
76 static GstCaps *gst_directsound_sink_getcaps (GstBaseSink * bsink);
77 static gboolean gst_directsound_sink_prepare (GstAudioSink * asink,
78 GstRingBufferSpec * spec);
79 static gboolean gst_directsound_sink_unprepare (GstAudioSink * asink);
81 static gboolean gst_directsound_sink_open (GstAudioSink * asink);
82 static gboolean gst_directsound_sink_close (GstAudioSink * asink);
83 static guint gst_directsound_sink_write (GstAudioSink * asink, gpointer data,
85 static guint gst_directsound_sink_delay (GstAudioSink * asink);
86 static void gst_directsound_sink_reset (GstAudioSink * asink);
87 static GstCaps *gst_directsound_probe_supported_formats (GstDirectSoundSink *
88 dsoundsink, const GstCaps * template_caps);
91 static void gst_directsound_sink_interfaces_init (GType type);
93 gst_directsound_sink_implements_interface_init (GstImplementsInterfaceClass *
95 static void gst_directsound_sink_mixer_interface_init (GstMixerClass * iface);
97 static GstStaticPadTemplate directsoundsink_sink_factory =
98 GST_STATIC_PAD_TEMPLATE ("sink",
101 GST_STATIC_CAPS ("audio/x-raw-int, "
102 "signed = (boolean) TRUE, "
105 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
107 "signed = (boolean) FALSE, "
110 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ];"
119 GST_BOILERPLATE_FULL (GstDirectSoundSink, gst_directsound_sink, GstAudioSink,
120 GST_TYPE_AUDIO_SINK, gst_directsound_sink_interfaces_init);
122 /* interfaces stuff */
124 gst_directsound_sink_interfaces_init (GType type)
126 static const GInterfaceInfo implements_interface_info = {
127 (GInterfaceInitFunc) gst_directsound_sink_implements_interface_init,
132 static const GInterfaceInfo mixer_interface_info = {
133 (GInterfaceInitFunc) gst_directsound_sink_mixer_interface_init,
138 g_type_add_interface_static (type,
139 GST_TYPE_IMPLEMENTS_INTERFACE, &implements_interface_info);
140 g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_interface_info);
144 gst_directsound_sink_interface_supported (GstImplementsInterface * iface,
147 g_return_val_if_fail (iface_type == GST_TYPE_MIXER, FALSE);
149 /* for the sake of this example, we'll always support it. However, normally,
150 * you would check whether the device you've opened supports mixers. */
155 gst_directsound_sink_implements_interface_init (GstImplementsInterfaceClass *
158 iface->supported = gst_directsound_sink_interface_supported;
162 * This function returns the list of support tracks (inputs, outputs)
163 * on this element instance. Elements usually build this list during
164 * _init () or when going from NULL to READY.
168 gst_directsound_sink_mixer_list_tracks (GstMixer * mixer)
170 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (mixer);
172 return dsoundsink->tracks;
176 gst_directsound_sink_set_volume (GstDirectSoundSink * dsoundsink)
178 if (dsoundsink->pDSBSecondary) {
179 /* DirectSound controls volume using units of 100th of a decibel,
180 * ranging from -10000 to 0. We use a linear scale of 0 - 100
184 if (dsoundsink->volume == 0)
187 dsVolume = 100 * (long) (20 * log10 ((double) dsoundsink->volume / 100.));
188 dsVolume = CLAMP (dsVolume, -10000, 0);
190 GST_DEBUG_OBJECT (dsoundsink,
191 "Setting volume on secondary buffer to %d from %d", (int) dsVolume,
192 (int) dsoundsink->volume);
193 IDirectSoundBuffer_SetVolume (dsoundsink->pDSBSecondary, dsVolume);
198 * Set volume. volumes is an array of size track->num_channels, and
199 * each value in the array gives the wanted volume for one channel
204 gst_directsound_sink_mixer_set_volume (GstMixer * mixer,
205 GstMixerTrack * track, gint * volumes)
207 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (mixer);
209 if (volumes[0] != dsoundsink->volume) {
210 dsoundsink->volume = volumes[0];
212 gst_directsound_sink_set_volume (dsoundsink);
217 gst_directsound_sink_mixer_get_volume (GstMixer * mixer,
218 GstMixerTrack * track, gint * volumes)
220 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (mixer);
222 volumes[0] = dsoundsink->volume;
226 gst_directsound_sink_mixer_interface_init (GstMixerClass * iface)
228 /* the mixer interface requires a definition of the mixer type:
229 * hardware or software? */
230 GST_MIXER_TYPE (iface) = GST_MIXER_SOFTWARE;
232 /* virtual function pointers */
233 iface->list_tracks = gst_directsound_sink_mixer_list_tracks;
234 iface->set_volume = gst_directsound_sink_mixer_set_volume;
235 iface->get_volume = gst_directsound_sink_mixer_get_volume;
239 gst_directsound_sink_finalise (GObject * object)
241 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (object);
243 g_mutex_free (dsoundsink->dsound_lock);
245 if (dsoundsink->tracks) {
246 g_list_foreach (dsoundsink->tracks, (GFunc) g_object_unref, NULL);
247 g_list_free (dsoundsink->tracks);
248 dsoundsink->tracks = NULL;
251 G_OBJECT_CLASS (parent_class)->finalize (object);
255 gst_directsound_sink_base_init (gpointer g_class)
257 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
259 gst_element_class_set_details_simple (element_class,
260 "Direct Sound Audio Sink", "Sink/Audio",
261 "Output to a sound card via Direct Sound",
262 "Sebastien Moutte <sebastien@moutte.net>");
263 gst_element_class_add_static_pad_template (element_class,
264 &directsoundsink_sink_factory);
268 gst_directsound_sink_class_init (GstDirectSoundSinkClass * klass)
270 GObjectClass *gobject_class;
271 GstElementClass *gstelement_class;
272 GstBaseSinkClass *gstbasesink_class;
273 GstBaseAudioSinkClass *gstbaseaudiosink_class;
274 GstAudioSinkClass *gstaudiosink_class;
276 gobject_class = (GObjectClass *) klass;
277 gstelement_class = (GstElementClass *) klass;
278 gstbasesink_class = (GstBaseSinkClass *) klass;
279 gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
280 gstaudiosink_class = (GstAudioSinkClass *) klass;
282 GST_DEBUG_CATEGORY_INIT (directsoundsink_debug, "directsoundsink", 0,
285 parent_class = g_type_class_peek_parent (klass);
287 gobject_class->finalize = gst_directsound_sink_finalise;
288 gobject_class->set_property = gst_directsound_sink_set_property;
289 gobject_class->get_property = gst_directsound_sink_get_property;
291 gstbasesink_class->get_caps =
292 GST_DEBUG_FUNCPTR (gst_directsound_sink_getcaps);
294 gstaudiosink_class->prepare =
295 GST_DEBUG_FUNCPTR (gst_directsound_sink_prepare);
296 gstaudiosink_class->unprepare =
297 GST_DEBUG_FUNCPTR (gst_directsound_sink_unprepare);
298 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_directsound_sink_open);
299 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_directsound_sink_close);
300 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_directsound_sink_write);
301 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_directsound_sink_delay);
302 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_directsound_sink_reset);
304 g_object_class_install_property (gobject_class,
306 g_param_spec_double ("volume", "Volume",
307 "Volume of this stream", 0.0, 1.0, 1.0,
308 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312 gst_directsound_sink_init (GstDirectSoundSink * dsoundsink,
313 GstDirectSoundSinkClass * g_class)
315 GstMixerTrack *track = NULL;
317 dsoundsink->tracks = NULL;
318 track = g_object_new (GST_TYPE_MIXER_TRACK, NULL);
319 track->label = g_strdup ("DSoundTrack");
320 track->num_channels = 2;
321 track->min_volume = 0;
322 track->max_volume = 100;
323 track->flags = GST_MIXER_TRACK_OUTPUT;
324 dsoundsink->tracks = g_list_append (dsoundsink->tracks, track);
326 dsoundsink->pDS = NULL;
327 dsoundsink->cached_caps = NULL;
328 dsoundsink->pDSBSecondary = NULL;
329 dsoundsink->current_circular_offset = 0;
330 dsoundsink->buffer_size = DSBSIZE_MIN;
331 dsoundsink->volume = 100;
332 dsoundsink->dsound_lock = g_mutex_new ();
333 dsoundsink->first_buffer_after_reset = FALSE;
337 gst_directsound_sink_set_property (GObject * object,
338 guint prop_id, const GValue * value, GParamSpec * pspec)
340 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
344 sink->volume = (int) (g_value_get_double (value) * 100);
345 gst_directsound_sink_set_volume (sink);
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354 gst_directsound_sink_get_property (GObject * object,
355 guint prop_id, GValue * value, GParamSpec * pspec)
357 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
361 g_value_set_double (value, (double) sink->volume / 100.);
364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
370 gst_directsound_sink_getcaps (GstBaseSink * bsink)
372 GstElementClass *element_class;
373 GstPadTemplate *pad_template;
374 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (bsink);
376 gchar *caps_string = NULL;
378 if (dsoundsink->pDS == NULL) {
379 GST_DEBUG_OBJECT (dsoundsink, "device not open, using template caps");
380 return NULL; /* base class will get template caps for us */
383 if (dsoundsink->cached_caps) {
384 caps_string = gst_caps_to_string (dsoundsink->cached_caps);
385 GST_DEBUG_OBJECT (dsoundsink, "Returning cached caps: %s", caps_string);
386 g_free (caps_string);
387 return gst_caps_ref (dsoundsink->cached_caps);
390 element_class = GST_ELEMENT_GET_CLASS (dsoundsink);
391 pad_template = gst_element_class_get_pad_template (element_class, "sink");
392 g_return_val_if_fail (pad_template != NULL, NULL);
394 caps = gst_directsound_probe_supported_formats (dsoundsink,
395 gst_pad_template_get_caps (pad_template));
397 dsoundsink->cached_caps = gst_caps_ref (caps);
401 gchar *caps_string = gst_caps_to_string (caps);
402 GST_DEBUG_OBJECT (dsoundsink, "returning caps %s", caps_string);
403 g_free (caps_string);
410 gst_directsound_sink_open (GstAudioSink * asink)
412 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (asink);
415 /* create and initialize a DirecSound object */
416 if (FAILED (hRes = DirectSoundCreate (NULL, &dsoundsink->pDS, NULL))) {
417 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
418 ("gst_directsound_sink_open: DirectSoundCreate: %s",
419 DXGetErrorString9 (hRes)), (NULL));
423 if (FAILED (hRes = IDirectSound_SetCooperativeLevel (dsoundsink->pDS,
424 GetDesktopWindow (), DSSCL_PRIORITY))) {
425 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
426 ("gst_directsound_sink_open: IDirectSound_SetCooperativeLevel: %s",
427 DXGetErrorString9 (hRes)), (NULL));
435 gst_directsound_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
437 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (asink);
439 DSBUFFERDESC descSecondary;
442 /*save number of bytes per sample and buffer format */
443 dsoundsink->bytes_per_sample = spec->bytes_per_sample;
444 dsoundsink->buffer_format = spec->format;
446 /* fill the WAVEFORMATEX structure with spec params */
447 memset (&wfx, 0, sizeof (wfx));
448 if (spec->format != GST_IEC958) {
449 wfx.cbSize = sizeof (wfx);
450 wfx.wFormatTag = WAVE_FORMAT_PCM;
451 wfx.nChannels = spec->channels;
452 wfx.nSamplesPerSec = spec->rate;
453 wfx.wBitsPerSample = (spec->bytes_per_sample * 8) / wfx.nChannels;
454 wfx.nBlockAlign = spec->bytes_per_sample;
455 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
457 /* Create directsound buffer with size based on our configured
458 * buffer_size (which is 200 ms by default) */
459 dsoundsink->buffer_size =
460 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->buffer_time,
462 /* Make sure we make those numbers multiple of our sample size in bytes */
463 dsoundsink->buffer_size += dsoundsink->buffer_size % spec->bytes_per_sample;
466 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->latency_time,
468 spec->segsize += spec->segsize % spec->bytes_per_sample;
469 spec->segtotal = dsoundsink->buffer_size / spec->segsize;
471 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
473 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
475 wfx.nSamplesPerSec = spec->rate;
476 wfx.wBitsPerSample = 16;
477 wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
478 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
480 spec->segsize = 6144;
483 g_assert_not_reached ();
487 // Make the final buffer size be an integer number of segments
488 dsoundsink->buffer_size = spec->segsize * spec->segtotal;
490 GST_INFO_OBJECT (dsoundsink,
491 "GstRingBufferSpec->channels: %d, GstRingBufferSpec->rate: %d, GstRingBufferSpec->bytes_per_sample: %d\n"
492 "WAVEFORMATEX.nSamplesPerSec: %ld, WAVEFORMATEX.wBitsPerSample: %d, WAVEFORMATEX.nBlockAlign: %d, WAVEFORMATEX.nAvgBytesPerSec: %ld\n"
493 "Size of dsound circular buffer=>%d\n", spec->channels, spec->rate,
494 spec->bytes_per_sample, wfx.nSamplesPerSec, wfx.wBitsPerSample,
495 wfx.nBlockAlign, wfx.nAvgBytesPerSec, dsoundsink->buffer_size);
497 /* create a secondary directsound buffer */
498 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
499 descSecondary.dwSize = sizeof (DSBUFFERDESC);
500 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
501 if (spec->format != GST_IEC958)
502 descSecondary.dwFlags |= DSBCAPS_CTRLVOLUME;
504 descSecondary.dwBufferBytes = dsoundsink->buffer_size;
505 descSecondary.lpwfxFormat = (WAVEFORMATEX *) & wfx;
507 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
508 &dsoundsink->pDSBSecondary, NULL);
510 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
511 ("gst_directsound_sink_prepare: IDirectSound_CreateSoundBuffer: %s",
512 DXGetErrorString9 (hRes)), (NULL));
516 gst_directsound_sink_set_volume (dsoundsink);
522 gst_directsound_sink_unprepare (GstAudioSink * asink)
524 GstDirectSoundSink *dsoundsink;
526 dsoundsink = GST_DIRECTSOUND_SINK (asink);
528 /* release secondary DirectSound buffer */
529 if (dsoundsink->pDSBSecondary) {
530 IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
531 dsoundsink->pDSBSecondary = NULL;
538 gst_directsound_sink_close (GstAudioSink * asink)
540 GstDirectSoundSink *dsoundsink = NULL;
542 dsoundsink = GST_DIRECTSOUND_SINK (asink);
544 /* release DirectSound object */
545 g_return_val_if_fail (dsoundsink->pDS != NULL, FALSE);
546 IDirectSound_Release (dsoundsink->pDS);
547 dsoundsink->pDS = NULL;
549 gst_caps_replace (&dsoundsink->cached_caps, NULL);
555 gst_directsound_sink_write (GstAudioSink * asink, gpointer data, guint length)
557 GstDirectSoundSink *dsoundsink;
560 LPVOID pLockedBuffer1 = NULL, pLockedBuffer2 = NULL;
561 DWORD dwSizeBuffer1, dwSizeBuffer2;
562 DWORD dwCurrentPlayCursor;
564 dsoundsink = GST_DIRECTSOUND_SINK (asink);
567 if (dsoundsink->buffer_format == GST_IEC958)
568 _swab (data, data, length);
570 GST_DSOUND_LOCK (dsoundsink);
572 /* get current buffer status */
573 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
575 /* get current play cursor position */
576 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
577 &dwCurrentPlayCursor, NULL);
579 if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) {
580 DWORD dwFreeBufferSize;
583 /* calculate the free size of the circular buffer */
584 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
586 dsoundsink->buffer_size - (dsoundsink->current_circular_offset -
587 dwCurrentPlayCursor);
590 dwCurrentPlayCursor - dsoundsink->current_circular_offset;
592 if (length >= dwFreeBufferSize) {
594 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
595 &dwCurrentPlayCursor, NULL);
598 IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
599 if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING))
600 goto calculate_freesize;
602 dsoundsink->first_buffer_after_reset = FALSE;
603 GST_DSOUND_UNLOCK (dsoundsink);
609 if (dwStatus & DSBSTATUS_BUFFERLOST) {
610 hRes = IDirectSoundBuffer_Restore (dsoundsink->pDSBSecondary); /*need a loop waiting the buffer is restored?? */
612 dsoundsink->current_circular_offset = 0;
615 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
616 dsoundsink->current_circular_offset, length, &pLockedBuffer1,
617 &dwSizeBuffer1, &pLockedBuffer2, &dwSizeBuffer2, 0L);
619 if (SUCCEEDED (hRes)) {
620 // Write to pointers without reordering.
621 memcpy (pLockedBuffer1, data, dwSizeBuffer1);
622 if (pLockedBuffer2 != NULL)
623 memcpy (pLockedBuffer2, (LPBYTE) data + dwSizeBuffer1, dwSizeBuffer2);
625 // Update where the buffer will lock (for next time)
626 dsoundsink->current_circular_offset += dwSizeBuffer1 + dwSizeBuffer2;
627 dsoundsink->current_circular_offset %= dsoundsink->buffer_size; /* Circular buffer */
629 hRes = IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer1,
630 dwSizeBuffer1, pLockedBuffer2, dwSizeBuffer2);
633 /* if the buffer was not in playing state yet, call play on the buffer
634 except if this buffer is the fist after a reset (base class call reset and write a buffer when setting the sink to pause) */
635 if (!(dwStatus & DSBSTATUS_PLAYING) &&
636 dsoundsink->first_buffer_after_reset == FALSE) {
637 hRes = IDirectSoundBuffer_Play (dsoundsink->pDSBSecondary, 0, 0,
641 dsoundsink->first_buffer_after_reset = FALSE;
643 GST_DSOUND_UNLOCK (dsoundsink);
649 gst_directsound_sink_delay (GstAudioSink * asink)
651 GstDirectSoundSink *dsoundsink;
653 DWORD dwCurrentPlayCursor;
654 DWORD dwBytesInQueue = 0;
655 gint nNbSamplesInQueue = 0;
658 dsoundsink = GST_DIRECTSOUND_SINK (asink);
660 /* get current buffer status */
661 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
663 if (dwStatus & DSBSTATUS_PLAYING) {
664 /*evaluate the number of samples in queue in the circular buffer */
665 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
666 &dwCurrentPlayCursor, NULL);
669 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
671 dsoundsink->current_circular_offset - dwCurrentPlayCursor;
674 dsoundsink->current_circular_offset + (dsoundsink->buffer_size -
675 dwCurrentPlayCursor);
677 nNbSamplesInQueue = dwBytesInQueue / dsoundsink->bytes_per_sample;
681 return nNbSamplesInQueue;
685 gst_directsound_sink_reset (GstAudioSink * asink)
687 GstDirectSoundSink *dsoundsink;
688 LPVOID pLockedBuffer = NULL;
689 DWORD dwSizeBuffer = 0;
691 dsoundsink = GST_DIRECTSOUND_SINK (asink);
693 GST_DSOUND_LOCK (dsoundsink);
695 if (dsoundsink->pDSBSecondary) {
697 HRESULT hRes = IDirectSoundBuffer_Stop (dsoundsink->pDSBSecondary);
700 hRes = IDirectSoundBuffer_SetCurrentPosition (dsoundsink->pDSBSecondary, 0);
701 dsoundsink->current_circular_offset = 0;
703 /*reset the buffer */
704 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
705 dsoundsink->current_circular_offset, dsoundsink->buffer_size,
706 &pLockedBuffer, &dwSizeBuffer, NULL, NULL, 0L);
708 if (SUCCEEDED (hRes)) {
709 memset (pLockedBuffer, 0, dwSizeBuffer);
712 IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer,
713 dwSizeBuffer, NULL, 0);
717 dsoundsink->first_buffer_after_reset = TRUE;
719 GST_DSOUND_UNLOCK (dsoundsink);
723 * gst_directsound_probe_supported_formats:
725 * Takes the template caps and returns the subset which is actually
726 * supported by this device.
731 gst_directsound_probe_supported_formats (GstDirectSoundSink * dsoundsink,
732 const GstCaps * template_caps)
735 DSBUFFERDESC descSecondary;
739 caps = gst_caps_copy (template_caps);
742 * Check availability of digital output by trying to create an SPDIF buffer
745 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
746 /* fill the WAVEFORMATEX structure with some standard AC3 over SPDIF params */
747 memset (&wfx, 0, sizeof (wfx));
749 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
751 wfx.nSamplesPerSec = 48000;
752 wfx.wBitsPerSample = 16;
754 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
756 // create a secondary directsound buffer
757 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
758 descSecondary.dwSize = sizeof (DSBUFFERDESC);
759 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
760 descSecondary.dwBufferBytes = 6144;
761 descSecondary.lpwfxFormat = &wfx;
763 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
764 &dsoundsink->pDSBSecondary, NULL);
766 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough not supported "
767 "(IDirectSound_CreateSoundBuffer returned: %s)\n",
768 DXGetErrorString9 (hRes));
770 gst_caps_subtract (caps, gst_caps_new_simple ("audio/x-iec958", NULL));
772 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
773 hRes = IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
775 GST_DEBUG_OBJECT (dsoundsink,
776 "(IDirectSoundBuffer_Release returned: %s)\n",
777 DXGetErrorString9 (hRes));
781 caps = gst_caps_subtract (caps, gst_caps_new_simple ("audio/x-iec958", NULL));