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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, 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-1.0 -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-1.0 -v filesrc location=music.ogg ! decodebin ! audioconvert ! audioresample ! directsoundsink
47 * ]| will play an Ogg/Vorbis audio file and output it.
55 #include <gst/base/gstbasesink.h>
56 #include "gstdirectsoundsink.h"
57 #include <gst/audio/gstaudioiec61937.h>
68 #define DEFAULT_MUTE FALSE
70 GST_DEBUG_CATEGORY_STATIC (directsoundsink_debug);
71 #define GST_CAT_DEFAULT directsoundsink_debug
73 static void gst_directsound_sink_finalize (GObject * object);
75 static void gst_directsound_sink_set_property (GObject * object, guint prop_id,
76 const GValue * value, GParamSpec * pspec);
77 static void gst_directsound_sink_get_property (GObject * object, guint prop_id,
78 GValue * value, GParamSpec * pspec);
80 static GstCaps *gst_directsound_sink_getcaps (GstBaseSink * bsink,
82 static GstBuffer *gst_directsound_sink_payload (GstAudioBaseSink * sink,
84 static gboolean gst_directsound_sink_prepare (GstAudioSink * asink,
85 GstAudioRingBufferSpec * spec);
86 static gboolean gst_directsound_sink_unprepare (GstAudioSink * asink);
87 static gboolean gst_directsound_sink_open (GstAudioSink * asink);
88 static gboolean gst_directsound_sink_close (GstAudioSink * asink);
89 static gint gst_directsound_sink_write (GstAudioSink * asink,
90 gpointer data, guint length);
91 static guint gst_directsound_sink_delay (GstAudioSink * asink);
92 static void gst_directsound_sink_reset (GstAudioSink * asink);
93 static GstCaps *gst_directsound_probe_supported_formats (GstDirectSoundSink *
94 dsoundsink, const GstCaps * template_caps);
95 static gboolean gst_directsound_sink_query (GstBaseSink * pad,
98 static void gst_directsound_sink_set_volume (GstDirectSoundSink * sink,
99 gdouble volume, gboolean store);
100 static gdouble gst_directsound_sink_get_volume (GstDirectSoundSink * sink);
101 static void gst_directsound_sink_set_mute (GstDirectSoundSink * sink,
103 static gboolean gst_directsound_sink_get_mute (GstDirectSoundSink * sink);
104 static const gchar *gst_directsound_sink_get_device (GstDirectSoundSink *
106 static void gst_directsound_sink_set_device (GstDirectSoundSink * dsoundsink,
107 const gchar * device_id);
109 static gboolean gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec *
112 static GstStaticPadTemplate directsoundsink_sink_factory =
113 GST_STATIC_PAD_TEMPLATE ("sink",
116 GST_STATIC_CAPS ("audio/x-raw, "
117 "format = (string) S16LE, "
118 "layout = (string) interleaved, "
119 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
121 "format = (string) U8, "
122 "layout = (string) interleaved, "
123 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ];"
124 "audio/x-ac3, framed = (boolean) true;"
125 "audio/x-dts, framed = (boolean) true;"));
135 #define gst_directsound_sink_parent_class parent_class
136 G_DEFINE_TYPE_WITH_CODE (GstDirectSoundSink, gst_directsound_sink,
137 GST_TYPE_AUDIO_SINK, G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
141 gst_directsound_sink_finalize (GObject * object)
143 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (object);
145 g_free (dsoundsink->device_id);
146 dsoundsink->device_id = NULL;
148 g_mutex_clear (&dsoundsink->dsound_lock);
150 G_OBJECT_CLASS (parent_class)->finalize (object);
154 gst_directsound_sink_class_init (GstDirectSoundSinkClass * klass)
156 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
157 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
158 GstAudioSinkClass *gstaudiosink_class = GST_AUDIO_SINK_CLASS (klass);
159 GstAudioBaseSinkClass *gstaudiobasesink_class =
160 GST_AUDIO_BASE_SINK_CLASS (klass);
161 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
163 GST_DEBUG_CATEGORY_INIT (directsoundsink_debug, "directsoundsink", 0,
166 gobject_class->finalize = gst_directsound_sink_finalize;
167 gobject_class->set_property = gst_directsound_sink_set_property;
168 gobject_class->get_property = gst_directsound_sink_get_property;
170 gstbasesink_class->get_caps =
171 GST_DEBUG_FUNCPTR (gst_directsound_sink_getcaps);
173 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_directsound_sink_query);
175 gstaudiobasesink_class->payload =
176 GST_DEBUG_FUNCPTR (gst_directsound_sink_payload);
178 gstaudiosink_class->prepare =
179 GST_DEBUG_FUNCPTR (gst_directsound_sink_prepare);
180 gstaudiosink_class->unprepare =
181 GST_DEBUG_FUNCPTR (gst_directsound_sink_unprepare);
182 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_directsound_sink_open);
183 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_directsound_sink_close);
184 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_directsound_sink_write);
185 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_directsound_sink_delay);
186 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_directsound_sink_reset);
188 g_object_class_install_property (gobject_class,
190 g_param_spec_double ("volume", "Volume",
191 "Volume of this stream", 0.0, 1.0, 1.0,
192 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194 g_object_class_install_property (gobject_class,
196 g_param_spec_boolean ("mute", "Mute",
197 "Mute state of this stream", DEFAULT_MUTE,
198 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200 g_object_class_install_property (gobject_class,
202 g_param_spec_string ("device", "Device",
203 "DirectSound playback device as a GUID string",
204 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206 gst_element_class_set_static_metadata (element_class,
207 "Direct Sound Audio Sink", "Sink/Audio",
208 "Output to a sound card via Direct Sound",
209 "Sebastien Moutte <sebastien@moutte.net>");
211 gst_element_class_add_pad_template (element_class,
212 gst_static_pad_template_get (&directsoundsink_sink_factory));
216 gst_directsound_sink_init (GstDirectSoundSink * dsoundsink)
218 dsoundsink->volume = 100;
219 dsoundsink->mute = FALSE;
220 dsoundsink->device_id = NULL;
221 dsoundsink->pDS = NULL;
222 dsoundsink->cached_caps = NULL;
223 dsoundsink->pDSBSecondary = NULL;
224 dsoundsink->current_circular_offset = 0;
225 dsoundsink->buffer_size = DSBSIZE_MIN;
226 dsoundsink->volume = 100;
227 g_mutex_init (&dsoundsink->dsound_lock);
228 dsoundsink->first_buffer_after_reset = FALSE;
232 gst_directsound_sink_set_property (GObject * object,
233 guint prop_id, const GValue * value, GParamSpec * pspec)
235 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
239 gst_directsound_sink_set_volume (sink, g_value_get_double (value), TRUE);
242 gst_directsound_sink_set_mute (sink, g_value_get_boolean (value));
245 gst_directsound_sink_set_device (sink, g_value_get_string (value));
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 gst_directsound_sink_get_property (GObject * object,
255 guint prop_id, GValue * value, GParamSpec * pspec)
257 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
261 g_value_set_double (value, gst_directsound_sink_get_volume (sink));
264 g_value_set_boolean (value, gst_directsound_sink_get_mute (sink));
267 g_value_set_string (value, gst_directsound_sink_get_device (sink));
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276 gst_directsound_sink_getcaps (GstBaseSink * bsink, GstCaps * filter)
278 GstElementClass *element_class;
279 GstPadTemplate *pad_template;
280 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (bsink);
283 if (dsoundsink->pDS == NULL) {
284 GST_DEBUG_OBJECT (dsoundsink, "device not open, using template caps");
285 return NULL; /* base class will get template caps for us */
288 if (dsoundsink->cached_caps) {
289 caps = gst_caps_ref (dsoundsink->cached_caps);
291 element_class = GST_ELEMENT_GET_CLASS (dsoundsink);
292 pad_template = gst_element_class_get_pad_template (element_class, "sink");
293 g_return_val_if_fail (pad_template != NULL, NULL);
295 caps = gst_directsound_probe_supported_formats (dsoundsink,
296 gst_pad_template_get_caps (pad_template));
298 dsoundsink->cached_caps = gst_caps_ref (caps);
301 if (caps && filter) {
303 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
304 gst_caps_unref (caps);
309 gchar *caps_string = gst_caps_to_string (caps);
310 GST_DEBUG_OBJECT (dsoundsink, "returning caps %s", caps_string);
311 g_free (caps_string);
318 gst_directsound_sink_acceptcaps (GstBaseSink * sink, GstQuery * query)
320 GstDirectSoundSink *dsink = GST_DIRECTSOUND_SINK (sink);
325 gboolean ret = FALSE;
326 GstAudioRingBufferSpec spec = { 0 };
328 if (G_UNLIKELY (dsink == NULL))
333 gst_query_parse_accept_caps (query, &caps);
334 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
336 pad_caps = gst_pad_query_caps (pad, NULL);
338 gboolean cret = gst_caps_is_subset (caps, pad_caps);
339 gst_caps_unref (pad_caps);
341 GST_DEBUG_OBJECT (dsink,
342 "Caps are not a subset of the pad caps, not accepting caps");
347 /* If we've not got fixed caps, creating a stream might fail, so let's just
348 * return from here with default acceptcaps behaviour */
349 if (!gst_caps_is_fixed (caps)) {
350 GST_DEBUG_OBJECT (dsink, "Caps are not fixed, not accepting caps");
354 spec.latency_time = GST_SECOND;
355 if (!gst_audio_ring_buffer_parse_caps (&spec, caps)) {
356 GST_DEBUG_OBJECT (dsink, "Failed to parse caps, not accepting");
360 /* Make sure input is framed (one frame per buffer) and can be payloaded */
362 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
363 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
365 gboolean framed = FALSE, parsed = FALSE;
366 st = gst_caps_get_structure (caps, 0);
368 gst_structure_get_boolean (st, "framed", &framed);
369 gst_structure_get_boolean (st, "parsed", &parsed);
370 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0) {
371 GST_DEBUG_OBJECT (dsink, "Wrong AC3/DTS caps, not accepting");
379 GST_DEBUG_OBJECT (dsink, "Accepting caps");
382 gst_query_set_accept_caps_result (query, ret);
387 gst_directsound_sink_query (GstBaseSink * sink, GstQuery * query)
391 switch (GST_QUERY_TYPE (query)) {
392 case GST_QUERY_ACCEPT_CAPS:
393 res = gst_directsound_sink_acceptcaps (sink, query);
396 res = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
403 string_to_guid (const gchar * str)
409 wstr = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL);
413 out = g_new (GUID, 1);
414 ret = CLSIDFromString ((LPOLESTR) wstr, out);
416 if (ret != NOERROR) {
425 gst_directsound_sink_open (GstAudioSink * asink)
427 GstDirectSoundSink *dsoundsink;
429 LPGUID lpGuid = NULL;
431 dsoundsink = GST_DIRECTSOUND_SINK (asink);
433 if (dsoundsink->device_id)
434 lpGuid = string_to_guid (dsoundsink->device_id);
436 /* create and initialize a DirecSound object */
437 if (FAILED (hRes = DirectSoundCreate (lpGuid, &dsoundsink->pDS, NULL))) {
438 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
439 ("gst_directsound_sink_open: DirectSoundCreate: %s",
440 DXGetErrorString9 (hRes)), (NULL));
447 if (FAILED (hRes = IDirectSound_SetCooperativeLevel (dsoundsink->pDS,
448 GetDesktopWindow (), DSSCL_PRIORITY))) {
449 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
450 ("gst_directsound_sink_open: IDirectSound_SetCooperativeLevel: %s",
451 DXGetErrorString9 (hRes)), (NULL));
459 gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec * spec)
461 return spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3 ||
462 spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS;
466 gst_directsound_sink_prepare (GstAudioSink * asink,
467 GstAudioRingBufferSpec * spec)
469 GstDirectSoundSink *dsoundsink;
471 DSBUFFERDESC descSecondary;
474 dsoundsink = GST_DIRECTSOUND_SINK (asink);
476 /*save number of bytes per sample and buffer format */
477 dsoundsink->bytes_per_sample = spec->info.bpf;
478 dsoundsink->type = spec->type;
480 /* fill the WAVEFORMATEX structure with spec params */
481 memset (&wfx, 0, sizeof (wfx));
482 if (!gst_directsound_sink_is_spdif_format (spec)) {
483 wfx.cbSize = sizeof (wfx);
484 wfx.wFormatTag = WAVE_FORMAT_PCM;
485 wfx.nChannels = spec->info.channels;
486 wfx.nSamplesPerSec = spec->info.rate;
487 wfx.wBitsPerSample = (spec->info.bpf * 8) / wfx.nChannels;
488 wfx.nBlockAlign = spec->info.bpf;
489 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
491 /* Create directsound buffer with size based on our configured
492 * buffer_size (which is 200 ms by default) */
493 dsoundsink->buffer_size =
494 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->buffer_time,
496 /* Make sure we make those numbers multiple of our sample size in bytes */
497 dsoundsink->buffer_size -= dsoundsink->buffer_size % spec->info.bpf;
500 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->latency_time,
502 spec->segsize -= spec->segsize % spec->info.bpf;
503 spec->segtotal = dsoundsink->buffer_size / spec->segsize;
505 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
507 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
509 wfx.nSamplesPerSec = 48000;
510 wfx.wBitsPerSample = 16;
511 wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
512 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
514 spec->segsize = 6144;
517 g_assert_not_reached ();
521 // Make the final buffer size be an integer number of segments
522 dsoundsink->buffer_size = spec->segsize * spec->segtotal;
524 GST_INFO_OBJECT (dsoundsink,
525 "GstAudioRingBufferSpec->channels: %d, GstAudioRingBufferSpec->rate: %d, GstAudioRingBufferSpec->bytes_per_sample: %d\n"
526 "WAVEFORMATEX.nSamplesPerSec: %ld, WAVEFORMATEX.wBitsPerSample: %d, WAVEFORMATEX.nBlockAlign: %d, WAVEFORMATEX.nAvgBytesPerSec: %ld\n"
527 "Size of dsound circular buffer=>%d\n", spec->info.channels,
528 spec->info.rate, spec->info.bpf, wfx.nSamplesPerSec, wfx.wBitsPerSample,
529 wfx.nBlockAlign, wfx.nAvgBytesPerSec, dsoundsink->buffer_size);
531 /* create a secondary directsound buffer */
532 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
533 descSecondary.dwSize = sizeof (DSBUFFERDESC);
534 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
535 if (!gst_directsound_sink_is_spdif_format (spec))
536 descSecondary.dwFlags |= DSBCAPS_CTRLVOLUME;
538 descSecondary.dwBufferBytes = dsoundsink->buffer_size;
539 descSecondary.lpwfxFormat = (WAVEFORMATEX *) & wfx;
541 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
542 &dsoundsink->pDSBSecondary, NULL);
544 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
545 ("gst_directsound_sink_prepare: IDirectSound_CreateSoundBuffer: %s",
546 DXGetErrorString9 (hRes)), (NULL));
550 gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
556 gst_directsound_sink_unprepare (GstAudioSink * asink)
558 GstDirectSoundSink *dsoundsink;
560 dsoundsink = GST_DIRECTSOUND_SINK (asink);
562 /* release secondary DirectSound buffer */
563 if (dsoundsink->pDSBSecondary) {
564 IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
565 dsoundsink->pDSBSecondary = NULL;
572 gst_directsound_sink_close (GstAudioSink * asink)
574 GstDirectSoundSink *dsoundsink = NULL;
576 dsoundsink = GST_DIRECTSOUND_SINK (asink);
578 /* release DirectSound object */
579 g_return_val_if_fail (dsoundsink->pDS != NULL, FALSE);
580 IDirectSound_Release (dsoundsink->pDS);
581 dsoundsink->pDS = NULL;
583 gst_caps_replace (&dsoundsink->cached_caps, NULL);
589 gst_directsound_sink_write (GstAudioSink * asink, gpointer data, guint length)
591 GstDirectSoundSink *dsoundsink;
594 LPVOID pLockedBuffer1 = NULL, pLockedBuffer2 = NULL;
595 DWORD dwSizeBuffer1, dwSizeBuffer2;
596 DWORD dwCurrentPlayCursor;
598 dsoundsink = GST_DIRECTSOUND_SINK (asink);
600 GST_DSOUND_LOCK (dsoundsink);
602 /* get current buffer status */
603 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
605 /* get current play cursor position */
606 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
607 &dwCurrentPlayCursor, NULL);
609 if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) {
610 DWORD dwFreeBufferSize;
613 /* calculate the free size of the circular buffer */
614 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
616 dsoundsink->buffer_size - (dsoundsink->current_circular_offset -
617 dwCurrentPlayCursor);
620 dwCurrentPlayCursor - dsoundsink->current_circular_offset;
622 if (length >= dwFreeBufferSize) {
624 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
625 &dwCurrentPlayCursor, NULL);
628 IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
629 if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING))
630 goto calculate_freesize;
632 dsoundsink->first_buffer_after_reset = FALSE;
633 GST_DSOUND_UNLOCK (dsoundsink);
639 if (dwStatus & DSBSTATUS_BUFFERLOST) {
640 hRes = IDirectSoundBuffer_Restore (dsoundsink->pDSBSecondary); /*need a loop waiting the buffer is restored?? */
642 dsoundsink->current_circular_offset = 0;
645 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
646 dsoundsink->current_circular_offset, length, &pLockedBuffer1,
647 &dwSizeBuffer1, &pLockedBuffer2, &dwSizeBuffer2, 0L);
649 if (SUCCEEDED (hRes)) {
650 // Write to pointers without reordering.
651 memcpy (pLockedBuffer1, data, dwSizeBuffer1);
652 if (pLockedBuffer2 != NULL)
653 memcpy (pLockedBuffer2, (LPBYTE) data + dwSizeBuffer1, dwSizeBuffer2);
655 // Update where the buffer will lock (for next time)
656 dsoundsink->current_circular_offset += dwSizeBuffer1 + dwSizeBuffer2;
657 dsoundsink->current_circular_offset %= dsoundsink->buffer_size; /* Circular buffer */
659 hRes = IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer1,
660 dwSizeBuffer1, pLockedBuffer2, dwSizeBuffer2);
663 /* if the buffer was not in playing state yet, call play on the buffer
664 except if this buffer is the fist after a reset (base class call reset and write a buffer when setting the sink to pause) */
665 if (!(dwStatus & DSBSTATUS_PLAYING) &&
666 dsoundsink->first_buffer_after_reset == FALSE) {
667 hRes = IDirectSoundBuffer_Play (dsoundsink->pDSBSecondary, 0, 0,
671 dsoundsink->first_buffer_after_reset = FALSE;
673 GST_DSOUND_UNLOCK (dsoundsink);
679 gst_directsound_sink_delay (GstAudioSink * asink)
681 GstDirectSoundSink *dsoundsink;
683 DWORD dwCurrentPlayCursor;
684 DWORD dwBytesInQueue = 0;
685 gint nNbSamplesInQueue = 0;
688 dsoundsink = GST_DIRECTSOUND_SINK (asink);
690 /* get current buffer status */
691 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
693 if (dwStatus & DSBSTATUS_PLAYING) {
694 /*evaluate the number of samples in queue in the circular buffer */
695 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
696 &dwCurrentPlayCursor, NULL);
699 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
701 dsoundsink->current_circular_offset - dwCurrentPlayCursor;
704 dsoundsink->current_circular_offset + (dsoundsink->buffer_size -
705 dwCurrentPlayCursor);
707 nNbSamplesInQueue = dwBytesInQueue / dsoundsink->bytes_per_sample;
711 return nNbSamplesInQueue;
715 gst_directsound_sink_reset (GstAudioSink * asink)
717 GstDirectSoundSink *dsoundsink;
718 LPVOID pLockedBuffer = NULL;
719 DWORD dwSizeBuffer = 0;
721 dsoundsink = GST_DIRECTSOUND_SINK (asink);
723 GST_DSOUND_LOCK (dsoundsink);
725 if (dsoundsink->pDSBSecondary) {
727 HRESULT hRes = IDirectSoundBuffer_Stop (dsoundsink->pDSBSecondary);
730 hRes = IDirectSoundBuffer_SetCurrentPosition (dsoundsink->pDSBSecondary, 0);
731 dsoundsink->current_circular_offset = 0;
733 /*reset the buffer */
734 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
735 dsoundsink->current_circular_offset, dsoundsink->buffer_size,
736 &pLockedBuffer, &dwSizeBuffer, NULL, NULL, 0L);
738 if (SUCCEEDED (hRes)) {
739 memset (pLockedBuffer, 0, dwSizeBuffer);
742 IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer,
743 dwSizeBuffer, NULL, 0);
747 dsoundsink->first_buffer_after_reset = TRUE;
749 GST_DSOUND_UNLOCK (dsoundsink);
753 * gst_directsound_probe_supported_formats:
755 * Takes the template caps and returns the subset which is actually
756 * supported by this device.
761 gst_directsound_probe_supported_formats (GstDirectSoundSink * dsoundsink,
762 const GstCaps * template_caps)
765 DSBUFFERDESC descSecondary;
769 LPDIRECTSOUNDBUFFER tmpBuffer;
771 caps = gst_caps_copy (template_caps);
774 * Check availability of digital output by trying to create an SPDIF buffer
777 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
778 /* fill the WAVEFORMATEX structure with some standard AC3 over SPDIF params */
779 memset (&wfx, 0, sizeof (wfx));
781 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
783 wfx.nSamplesPerSec = 48000;
784 wfx.wBitsPerSample = 16;
786 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
788 // create a secondary directsound buffer
789 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
790 descSecondary.dwSize = sizeof (DSBUFFERDESC);
791 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
792 descSecondary.dwBufferBytes = 6144;
793 descSecondary.lpwfxFormat = &wfx;
795 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
798 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough not supported "
799 "(IDirectSound_CreateSoundBuffer returned: %s)\n",
800 DXGetErrorString9 (hRes));
801 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
802 tmp2 = gst_caps_subtract (caps, tmp);
803 gst_caps_unref (tmp);
804 gst_caps_unref (caps);
806 tmp = gst_caps_new_empty_simple ("audio/x-dts");
807 tmp2 = gst_caps_subtract (caps, tmp);
808 gst_caps_unref (tmp);
809 gst_caps_unref (caps);
812 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
813 hRes = IDirectSoundBuffer_Release (tmpBuffer);
815 GST_DEBUG_OBJECT (dsoundsink,
816 "(IDirectSoundBuffer_Release returned: %s)\n",
817 DXGetErrorString9 (hRes));
821 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
822 tmp2 = gst_caps_subtract (caps, tmp);
823 gst_caps_unref (tmp);
824 gst_caps_unref (caps);
826 tmp = gst_caps_new_empty_simple ("audio/x-dts");
827 tmp2 = gst_caps_subtract (caps, tmp);
828 gst_caps_unref (tmp);
829 gst_caps_unref (caps);
837 gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
839 if (gst_directsound_sink_is_spdif_format (&sink->ringbuffer->spec)) {
840 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
842 GstMapInfo infobuf, infoout;
848 out = gst_buffer_new_and_alloc (framesize);
850 if (!gst_buffer_map (buf, &infobuf, GST_MAP_READWRITE)) {
851 gst_buffer_unref (out);
854 if (!gst_buffer_map (out, &infoout, GST_MAP_READWRITE)) {
855 gst_buffer_unmap (buf, &infobuf);
856 gst_buffer_unref (out);
859 success = gst_audio_iec61937_payload (infobuf.data, infobuf.size,
860 infoout.data, infoout.size, &sink->ringbuffer->spec, G_BYTE_ORDER);
862 gst_buffer_unmap (out, &infoout);
863 gst_buffer_unmap (buf, &infobuf);
864 gst_buffer_unref (out);
868 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_ALL, 0, -1);
870 _swab ((gchar *) infoout.data, (gchar *) infoout.data, infobuf.size);
871 gst_buffer_unmap (out, &infoout);
872 gst_buffer_unmap (buf, &infobuf);
875 return gst_buffer_ref (buf);
879 gst_directsound_sink_set_volume (GstDirectSoundSink * dsoundsink,
880 gdouble dvolume, gboolean store)
884 volume = dvolume * 100;
886 dsoundsink->volume = volume;
888 if (dsoundsink->pDSBSecondary) {
889 /* DirectSound controls volume using units of 100th of a decibel,
890 * ranging from -10000 to 0. We use a linear scale of 0 - 100
894 if (dsoundsink->volume == 0)
897 dsVolume = 100 * (long) (20 * log10 ((double) dsoundsink->volume / 100.));
898 dsVolume = CLAMP (dsVolume, -10000, 0);
900 GST_DEBUG_OBJECT (dsoundsink,
901 "Setting volume on secondary buffer to %d from %d", (int) dsVolume,
902 (int) dsoundsink->volume);
903 IDirectSoundBuffer_SetVolume (dsoundsink->pDSBSecondary, dsVolume);
908 gst_directsound_sink_get_volume (GstDirectSoundSink * dsoundsink)
910 return (gdouble) dsoundsink->volume / 100;
914 gst_directsound_sink_set_mute (GstDirectSoundSink * dsoundsink, gboolean mute)
917 gst_directsound_sink_set_volume (dsoundsink, 0, FALSE);
919 gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
923 gst_directsound_sink_get_mute (GstDirectSoundSink * dsoundsink)
929 gst_directsound_sink_get_device (GstDirectSoundSink * dsoundsink)
931 return dsoundsink->device_id;
935 gst_directsound_sink_set_device (GstDirectSoundSink * dsoundsink,
936 const gchar * device_id)
938 g_free (dsoundsink->device_id);
939 dsoundsink->device_id = g_strdup (device_id);