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);
435 if (lpGuid == NULL) {
436 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
437 ("gst_directsound_sink_open: device set, but guid not found: %s",
438 dsoundsink->device_id), (NULL));
443 /* create and initialize a DirecSound object */
444 if (FAILED (hRes = DirectSoundCreate (lpGuid, &dsoundsink->pDS, NULL))) {
445 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
446 ("gst_directsound_sink_open: DirectSoundCreate: %s",
447 DXGetErrorString9 (hRes)), (NULL));
454 if (FAILED (hRes = IDirectSound_SetCooperativeLevel (dsoundsink->pDS,
455 GetDesktopWindow (), DSSCL_PRIORITY))) {
456 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
457 ("gst_directsound_sink_open: IDirectSound_SetCooperativeLevel: %s",
458 DXGetErrorString9 (hRes)), (NULL));
466 gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec * spec)
468 return spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3 ||
469 spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS;
473 gst_directsound_sink_prepare (GstAudioSink * asink,
474 GstAudioRingBufferSpec * spec)
476 GstDirectSoundSink *dsoundsink;
478 DSBUFFERDESC descSecondary;
481 dsoundsink = GST_DIRECTSOUND_SINK (asink);
483 /*save number of bytes per sample and buffer format */
484 dsoundsink->bytes_per_sample = spec->info.bpf;
485 dsoundsink->type = spec->type;
487 /* fill the WAVEFORMATEX structure with spec params */
488 memset (&wfx, 0, sizeof (wfx));
489 if (!gst_directsound_sink_is_spdif_format (spec)) {
490 wfx.cbSize = sizeof (wfx);
491 wfx.wFormatTag = WAVE_FORMAT_PCM;
492 wfx.nChannels = spec->info.channels;
493 wfx.nSamplesPerSec = spec->info.rate;
494 wfx.wBitsPerSample = (spec->info.bpf * 8) / wfx.nChannels;
495 wfx.nBlockAlign = spec->info.bpf;
496 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
498 /* Create directsound buffer with size based on our configured
499 * buffer_size (which is 200 ms by default) */
500 dsoundsink->buffer_size =
501 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->buffer_time,
503 /* Make sure we make those numbers multiple of our sample size in bytes */
504 dsoundsink->buffer_size -= dsoundsink->buffer_size % spec->info.bpf;
507 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->latency_time,
509 spec->segsize -= spec->segsize % spec->info.bpf;
510 spec->segtotal = dsoundsink->buffer_size / spec->segsize;
512 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
514 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
516 wfx.nSamplesPerSec = 48000;
517 wfx.wBitsPerSample = 16;
518 wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
519 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
521 spec->segsize = 6144;
524 g_assert_not_reached ();
528 // Make the final buffer size be an integer number of segments
529 dsoundsink->buffer_size = spec->segsize * spec->segtotal;
531 GST_INFO_OBJECT (dsoundsink,
532 "GstAudioRingBufferSpec->channels: %d, GstAudioRingBufferSpec->rate: %d, GstAudioRingBufferSpec->bytes_per_sample: %d\n"
533 "WAVEFORMATEX.nSamplesPerSec: %ld, WAVEFORMATEX.wBitsPerSample: %d, WAVEFORMATEX.nBlockAlign: %d, WAVEFORMATEX.nAvgBytesPerSec: %ld\n"
534 "Size of dsound circular buffer=>%d\n", spec->info.channels,
535 spec->info.rate, spec->info.bpf, wfx.nSamplesPerSec, wfx.wBitsPerSample,
536 wfx.nBlockAlign, wfx.nAvgBytesPerSec, dsoundsink->buffer_size);
538 /* create a secondary directsound buffer */
539 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
540 descSecondary.dwSize = sizeof (DSBUFFERDESC);
541 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
542 if (!gst_directsound_sink_is_spdif_format (spec))
543 descSecondary.dwFlags |= DSBCAPS_CTRLVOLUME;
545 descSecondary.dwBufferBytes = dsoundsink->buffer_size;
546 descSecondary.lpwfxFormat = (WAVEFORMATEX *) & wfx;
548 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
549 &dsoundsink->pDSBSecondary, NULL);
551 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
552 ("gst_directsound_sink_prepare: IDirectSound_CreateSoundBuffer: %s",
553 DXGetErrorString9 (hRes)), (NULL));
557 gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
558 gst_directsound_sink_set_mute (dsoundsink, dsoundsink->mute);
564 gst_directsound_sink_unprepare (GstAudioSink * asink)
566 GstDirectSoundSink *dsoundsink;
568 dsoundsink = GST_DIRECTSOUND_SINK (asink);
570 /* release secondary DirectSound buffer */
571 if (dsoundsink->pDSBSecondary) {
572 IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
573 dsoundsink->pDSBSecondary = NULL;
580 gst_directsound_sink_close (GstAudioSink * asink)
582 GstDirectSoundSink *dsoundsink = NULL;
584 dsoundsink = GST_DIRECTSOUND_SINK (asink);
586 /* release DirectSound object */
587 g_return_val_if_fail (dsoundsink->pDS != NULL, FALSE);
588 IDirectSound_Release (dsoundsink->pDS);
589 dsoundsink->pDS = NULL;
591 gst_caps_replace (&dsoundsink->cached_caps, NULL);
597 gst_directsound_sink_write (GstAudioSink * asink, gpointer data, guint length)
599 GstDirectSoundSink *dsoundsink;
602 LPVOID pLockedBuffer1 = NULL, pLockedBuffer2 = NULL;
603 DWORD dwSizeBuffer1, dwSizeBuffer2;
604 DWORD dwCurrentPlayCursor;
606 dsoundsink = GST_DIRECTSOUND_SINK (asink);
608 GST_DSOUND_LOCK (dsoundsink);
610 /* get current buffer status */
611 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
613 /* get current play cursor position */
614 hRes2 = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
615 &dwCurrentPlayCursor, NULL);
617 if (SUCCEEDED (hRes) && SUCCEEDED (hRes2) && (dwStatus & DSBSTATUS_PLAYING)) {
618 DWORD dwFreeBufferSize = 0;
622 /* calculate the free size of the circular buffer */
623 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
625 dsoundsink->buffer_size - (dsoundsink->current_circular_offset -
626 dwCurrentPlayCursor);
629 dwCurrentPlayCursor - dsoundsink->current_circular_offset;
631 if (length >= dwFreeBufferSize) {
634 dwFreeBufferSize) * 1000) / (dsoundsink->bytes_per_sample *
635 GST_AUDIO_BASE_SINK (asink)->ringbuffer->spec.info.rate);
637 GST_DEBUG_OBJECT (dsoundsink,
638 "gst_directsound_sink_write: length:%i, FreeBufSiz: %ld, sleepTime: %ld, bps: %i, rate: %i",
639 length, dwFreeBufferSize, sleepTime, dsoundsink->bytes_per_sample,
640 GST_AUDIO_BASE_SINK (asink)->ringbuffer->spec.info.rate);
643 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
644 &dwCurrentPlayCursor, NULL);
647 IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
648 if (SUCCEEDED (hRes) && SUCCEEDED (hRes2)
649 && (dwStatus & DSBSTATUS_PLAYING))
650 goto calculate_freesize;
652 dsoundsink->first_buffer_after_reset = FALSE;
653 GST_DSOUND_UNLOCK (dsoundsink);
654 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_WRITE,
655 ("gst_directsound_sink_write: IDirectSoundBuffer_GetStatus %s, IDirectSoundBuffer_GetCurrentPosition: %s, dwStatus: %lu",
656 DXGetErrorString9 (hRes2), DXGetErrorString9 (hRes), dwStatus),
663 if (dwStatus & DSBSTATUS_BUFFERLOST) {
664 hRes = IDirectSoundBuffer_Restore (dsoundsink->pDSBSecondary); /*need a loop waiting the buffer is restored?? */
666 dsoundsink->current_circular_offset = 0;
669 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
670 dsoundsink->current_circular_offset, length, &pLockedBuffer1,
671 &dwSizeBuffer1, &pLockedBuffer2, &dwSizeBuffer2, 0L);
673 if (SUCCEEDED (hRes)) {
674 // Write to pointers without reordering.
675 memcpy (pLockedBuffer1, data, dwSizeBuffer1);
676 if (pLockedBuffer2 != NULL)
677 memcpy (pLockedBuffer2, (LPBYTE) data + dwSizeBuffer1, dwSizeBuffer2);
679 // Update where the buffer will lock (for next time)
680 dsoundsink->current_circular_offset += dwSizeBuffer1 + dwSizeBuffer2;
681 dsoundsink->current_circular_offset %= dsoundsink->buffer_size; /* Circular buffer */
683 hRes = IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer1,
684 dwSizeBuffer1, pLockedBuffer2, dwSizeBuffer2);
687 /* if the buffer was not in playing state yet, call play on the buffer
688 except if this buffer is the fist after a reset (base class call reset and write a buffer when setting the sink to pause) */
689 if (!(dwStatus & DSBSTATUS_PLAYING) &&
690 dsoundsink->first_buffer_after_reset == FALSE) {
691 hRes = IDirectSoundBuffer_Play (dsoundsink->pDSBSecondary, 0, 0,
695 dsoundsink->first_buffer_after_reset = FALSE;
697 GST_DSOUND_UNLOCK (dsoundsink);
703 gst_directsound_sink_delay (GstAudioSink * asink)
705 GstDirectSoundSink *dsoundsink;
707 DWORD dwCurrentPlayCursor;
708 DWORD dwBytesInQueue = 0;
709 gint nNbSamplesInQueue = 0;
712 dsoundsink = GST_DIRECTSOUND_SINK (asink);
714 /* get current buffer status */
715 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
717 if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) {
718 /*evaluate the number of samples in queue in the circular buffer */
719 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
720 &dwCurrentPlayCursor, NULL);
723 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
725 dsoundsink->current_circular_offset - dwCurrentPlayCursor;
728 dsoundsink->current_circular_offset + (dsoundsink->buffer_size -
729 dwCurrentPlayCursor);
731 nNbSamplesInQueue = dwBytesInQueue / dsoundsink->bytes_per_sample;
735 return nNbSamplesInQueue;
739 gst_directsound_sink_reset (GstAudioSink * asink)
741 GstDirectSoundSink *dsoundsink;
742 LPVOID pLockedBuffer = NULL;
743 DWORD dwSizeBuffer = 0;
745 dsoundsink = GST_DIRECTSOUND_SINK (asink);
747 GST_DSOUND_LOCK (dsoundsink);
749 if (dsoundsink->pDSBSecondary) {
751 HRESULT hRes = IDirectSoundBuffer_Stop (dsoundsink->pDSBSecondary);
754 hRes = IDirectSoundBuffer_SetCurrentPosition (dsoundsink->pDSBSecondary, 0);
755 dsoundsink->current_circular_offset = 0;
757 /*reset the buffer */
758 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
759 dsoundsink->current_circular_offset, dsoundsink->buffer_size,
760 &pLockedBuffer, &dwSizeBuffer, NULL, NULL, 0L);
762 if (SUCCEEDED (hRes)) {
763 memset (pLockedBuffer, 0, dwSizeBuffer);
766 IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer,
767 dwSizeBuffer, NULL, 0);
771 dsoundsink->first_buffer_after_reset = TRUE;
773 GST_DSOUND_UNLOCK (dsoundsink);
777 * gst_directsound_probe_supported_formats:
779 * Takes the template caps and returns the subset which is actually
780 * supported by this device.
785 gst_directsound_probe_supported_formats (GstDirectSoundSink * dsoundsink,
786 const GstCaps * template_caps)
789 DSBUFFERDESC descSecondary;
793 LPDIRECTSOUNDBUFFER tmpBuffer;
795 caps = gst_caps_copy (template_caps);
798 * Check availability of digital output by trying to create an SPDIF buffer
801 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
802 /* fill the WAVEFORMATEX structure with some standard AC3 over SPDIF params */
803 memset (&wfx, 0, sizeof (wfx));
805 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
807 wfx.nSamplesPerSec = 48000;
808 wfx.wBitsPerSample = 16;
810 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
812 // create a secondary directsound buffer
813 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
814 descSecondary.dwSize = sizeof (DSBUFFERDESC);
815 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
816 descSecondary.dwBufferBytes = 6144;
817 descSecondary.lpwfxFormat = &wfx;
819 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
822 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough not supported "
823 "(IDirectSound_CreateSoundBuffer returned: %s)\n",
824 DXGetErrorString9 (hRes));
825 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
826 tmp2 = gst_caps_subtract (caps, tmp);
827 gst_caps_unref (tmp);
828 gst_caps_unref (caps);
830 tmp = gst_caps_new_empty_simple ("audio/x-dts");
831 tmp2 = gst_caps_subtract (caps, tmp);
832 gst_caps_unref (tmp);
833 gst_caps_unref (caps);
836 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
837 hRes = IDirectSoundBuffer_Release (tmpBuffer);
839 GST_DEBUG_OBJECT (dsoundsink,
840 "(IDirectSoundBuffer_Release returned: %s)\n",
841 DXGetErrorString9 (hRes));
845 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
846 tmp2 = gst_caps_subtract (caps, tmp);
847 gst_caps_unref (tmp);
848 gst_caps_unref (caps);
850 tmp = gst_caps_new_empty_simple ("audio/x-dts");
851 tmp2 = gst_caps_subtract (caps, tmp);
852 gst_caps_unref (tmp);
853 gst_caps_unref (caps);
861 gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
863 if (gst_directsound_sink_is_spdif_format (&sink->ringbuffer->spec)) {
864 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
866 GstMapInfo infobuf, infoout;
872 out = gst_buffer_new_and_alloc (framesize);
874 if (!gst_buffer_map (buf, &infobuf, GST_MAP_READWRITE)) {
875 gst_buffer_unref (out);
878 if (!gst_buffer_map (out, &infoout, GST_MAP_READWRITE)) {
879 gst_buffer_unmap (buf, &infobuf);
880 gst_buffer_unref (out);
883 success = gst_audio_iec61937_payload (infobuf.data, infobuf.size,
884 infoout.data, infoout.size, &sink->ringbuffer->spec, G_BYTE_ORDER);
886 gst_buffer_unmap (out, &infoout);
887 gst_buffer_unmap (buf, &infobuf);
888 gst_buffer_unref (out);
892 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_ALL, 0, -1);
894 _swab ((gchar *) infoout.data, (gchar *) infoout.data, infobuf.size);
895 gst_buffer_unmap (out, &infoout);
896 gst_buffer_unmap (buf, &infobuf);
899 return gst_buffer_ref (buf);
903 gst_directsound_sink_set_volume (GstDirectSoundSink * dsoundsink,
904 gdouble dvolume, gboolean store)
908 volume = dvolume * 100;
910 dsoundsink->volume = volume;
912 if (dsoundsink->pDSBSecondary) {
913 /* DirectSound controls volume using units of 100th of a decibel,
914 * ranging from -10000 to 0. We use a linear scale of 0 - 100
921 dsVolume = 100 * (long) (20 * log10 ((double) volume / 100.));
922 dsVolume = CLAMP (dsVolume, -10000, 0);
924 GST_DEBUG_OBJECT (dsoundsink,
925 "Setting volume on secondary buffer to %d from %d", (int) dsVolume,
926 (int) dsoundsink->volume);
927 IDirectSoundBuffer_SetVolume (dsoundsink->pDSBSecondary, dsVolume);
932 gst_directsound_sink_get_volume (GstDirectSoundSink * dsoundsink)
934 return (gdouble) dsoundsink->volume / 100;
938 gst_directsound_sink_set_mute (GstDirectSoundSink * dsoundsink, gboolean mute)
941 gst_directsound_sink_set_volume (dsoundsink, 0, FALSE);
942 dsoundsink->mute = TRUE;
944 gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
945 dsoundsink->mute = FALSE;
951 gst_directsound_sink_get_mute (GstDirectSoundSink * dsoundsink)
953 return dsoundsink->mute;
957 gst_directsound_sink_get_device (GstDirectSoundSink * dsoundsink)
959 return dsoundsink->device_id;
963 gst_directsound_sink_set_device (GstDirectSoundSink * dsoundsink,
964 const gchar * device_id)
966 g_free (dsoundsink->device_id);
967 dsoundsink->device_id = g_strdup (device_id);