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 gchar *gst_hres_to_string (HRESULT hRes);
114 static GstStaticPadTemplate directsoundsink_sink_factory =
115 GST_STATIC_PAD_TEMPLATE ("sink",
118 GST_STATIC_CAPS ("audio/x-raw, "
119 "format = (string) S16LE, "
120 "layout = (string) interleaved, "
121 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
123 "format = (string) U8, "
124 "layout = (string) interleaved, "
125 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ];"
126 "audio/x-ac3, framed = (boolean) true;"
127 "audio/x-dts, framed = (boolean) true;"));
137 #define gst_directsound_sink_parent_class parent_class
138 G_DEFINE_TYPE_WITH_CODE (GstDirectSoundSink, gst_directsound_sink,
139 GST_TYPE_AUDIO_SINK, G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
143 gst_directsound_sink_finalize (GObject * object)
145 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (object);
147 g_free (dsoundsink->device_id);
148 dsoundsink->device_id = NULL;
150 g_mutex_clear (&dsoundsink->dsound_lock);
152 G_OBJECT_CLASS (parent_class)->finalize (object);
156 gst_directsound_sink_class_init (GstDirectSoundSinkClass * klass)
158 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
159 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
160 GstAudioSinkClass *gstaudiosink_class = GST_AUDIO_SINK_CLASS (klass);
161 GstAudioBaseSinkClass *gstaudiobasesink_class =
162 GST_AUDIO_BASE_SINK_CLASS (klass);
163 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
165 GST_DEBUG_CATEGORY_INIT (directsoundsink_debug, "directsoundsink", 0,
168 gobject_class->finalize = gst_directsound_sink_finalize;
169 gobject_class->set_property = gst_directsound_sink_set_property;
170 gobject_class->get_property = gst_directsound_sink_get_property;
172 gstbasesink_class->get_caps =
173 GST_DEBUG_FUNCPTR (gst_directsound_sink_getcaps);
175 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_directsound_sink_query);
177 gstaudiobasesink_class->payload =
178 GST_DEBUG_FUNCPTR (gst_directsound_sink_payload);
180 gstaudiosink_class->prepare =
181 GST_DEBUG_FUNCPTR (gst_directsound_sink_prepare);
182 gstaudiosink_class->unprepare =
183 GST_DEBUG_FUNCPTR (gst_directsound_sink_unprepare);
184 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_directsound_sink_open);
185 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_directsound_sink_close);
186 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_directsound_sink_write);
187 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_directsound_sink_delay);
188 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_directsound_sink_reset);
190 g_object_class_install_property (gobject_class,
192 g_param_spec_double ("volume", "Volume",
193 "Volume of this stream", 0.0, 1.0, 1.0,
194 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196 g_object_class_install_property (gobject_class,
198 g_param_spec_boolean ("mute", "Mute",
199 "Mute state of this stream", DEFAULT_MUTE,
200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202 g_object_class_install_property (gobject_class,
204 g_param_spec_string ("device", "Device",
205 "DirectSound playback device as a GUID string",
206 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208 gst_element_class_set_static_metadata (element_class,
209 "Direct Sound Audio Sink", "Sink/Audio",
210 "Output to a sound card via Direct Sound",
211 "Sebastien Moutte <sebastien@moutte.net>");
213 gst_element_class_add_static_pad_template (element_class,
214 &directsoundsink_sink_factory);
218 gst_directsound_sink_init (GstDirectSoundSink * dsoundsink)
220 dsoundsink->volume = 100;
221 dsoundsink->mute = FALSE;
222 dsoundsink->device_id = NULL;
223 dsoundsink->pDS = NULL;
224 dsoundsink->cached_caps = NULL;
225 dsoundsink->pDSBSecondary = NULL;
226 dsoundsink->current_circular_offset = 0;
227 dsoundsink->buffer_size = DSBSIZE_MIN;
228 dsoundsink->volume = 100;
229 g_mutex_init (&dsoundsink->dsound_lock);
230 dsoundsink->first_buffer_after_reset = FALSE;
234 gst_directsound_sink_set_property (GObject * object,
235 guint prop_id, const GValue * value, GParamSpec * pspec)
237 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
241 gst_directsound_sink_set_volume (sink, g_value_get_double (value), TRUE);
244 gst_directsound_sink_set_mute (sink, g_value_get_boolean (value));
247 gst_directsound_sink_set_device (sink, g_value_get_string (value));
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 gst_directsound_sink_get_property (GObject * object,
257 guint prop_id, GValue * value, GParamSpec * pspec)
259 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
263 g_value_set_double (value, gst_directsound_sink_get_volume (sink));
266 g_value_set_boolean (value, gst_directsound_sink_get_mute (sink));
269 g_value_set_string (value, gst_directsound_sink_get_device (sink));
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 gst_directsound_sink_getcaps (GstBaseSink * bsink, GstCaps * filter)
280 GstElementClass *element_class;
281 GstPadTemplate *pad_template;
282 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (bsink);
285 if (dsoundsink->pDS == NULL) {
286 GST_DEBUG_OBJECT (dsoundsink, "device not open, using template caps");
287 return NULL; /* base class will get template caps for us */
290 if (dsoundsink->cached_caps) {
291 caps = gst_caps_ref (dsoundsink->cached_caps);
293 element_class = GST_ELEMENT_GET_CLASS (dsoundsink);
294 pad_template = gst_element_class_get_pad_template (element_class, "sink");
295 g_return_val_if_fail (pad_template != NULL, NULL);
297 caps = gst_directsound_probe_supported_formats (dsoundsink,
298 gst_pad_template_get_caps (pad_template));
300 dsoundsink->cached_caps = gst_caps_ref (caps);
303 if (caps && filter) {
305 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
306 gst_caps_unref (caps);
311 gchar *caps_string = gst_caps_to_string (caps);
312 GST_DEBUG_OBJECT (dsoundsink, "returning caps %s", caps_string);
313 g_free (caps_string);
320 gst_directsound_sink_acceptcaps (GstBaseSink * sink, GstQuery * query)
322 GstDirectSoundSink *dsink = GST_DIRECTSOUND_SINK (sink);
327 gboolean ret = FALSE;
328 GstAudioRingBufferSpec spec = { 0 };
330 if (G_UNLIKELY (dsink == NULL))
335 gst_query_parse_accept_caps (query, &caps);
336 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
338 pad_caps = gst_pad_query_caps (pad, NULL);
340 gboolean cret = gst_caps_is_subset (caps, pad_caps);
341 gst_caps_unref (pad_caps);
343 GST_DEBUG_OBJECT (dsink,
344 "Caps are not a subset of the pad caps, not accepting caps");
349 /* If we've not got fixed caps, creating a stream might fail, so let's just
350 * return from here with default acceptcaps behaviour */
351 if (!gst_caps_is_fixed (caps)) {
352 GST_DEBUG_OBJECT (dsink, "Caps are not fixed, not accepting caps");
356 spec.latency_time = GST_SECOND;
357 if (!gst_audio_ring_buffer_parse_caps (&spec, caps)) {
358 GST_DEBUG_OBJECT (dsink, "Failed to parse caps, not accepting");
362 /* Make sure input is framed (one frame per buffer) and can be payloaded */
364 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
365 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
367 gboolean framed = FALSE, parsed = FALSE;
368 st = gst_caps_get_structure (caps, 0);
370 gst_structure_get_boolean (st, "framed", &framed);
371 gst_structure_get_boolean (st, "parsed", &parsed);
372 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0) {
373 GST_DEBUG_OBJECT (dsink, "Wrong AC3/DTS caps, not accepting");
381 GST_DEBUG_OBJECT (dsink, "Accepting caps");
384 gst_query_set_accept_caps_result (query, ret);
389 gst_directsound_sink_query (GstBaseSink * sink, GstQuery * query)
393 switch (GST_QUERY_TYPE (query)) {
394 case GST_QUERY_ACCEPT_CAPS:
395 res = gst_directsound_sink_acceptcaps (sink, query);
398 res = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
405 string_to_guid (const gchar * str)
411 wstr = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL);
415 out = g_new (GUID, 1);
416 ret = CLSIDFromString ((LPOLESTR) wstr, out);
418 if (ret != NOERROR) {
427 gst_directsound_sink_open (GstAudioSink * asink)
429 GstDirectSoundSink *dsoundsink;
431 LPGUID lpGuid = NULL;
433 dsoundsink = GST_DIRECTSOUND_SINK (asink);
435 if (dsoundsink->device_id) {
436 lpGuid = string_to_guid (dsoundsink->device_id);
437 if (lpGuid == NULL) {
438 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
439 ("gst_directsound_sink_open: device set, but guid not found: %s",
440 dsoundsink->device_id), (NULL));
445 /* create and initialize a DirecSound object */
446 if (FAILED (hRes = DirectSoundCreate (lpGuid, &dsoundsink->pDS, NULL))) {
447 gchar *error_text = gst_hres_to_string (hRes);
448 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
449 ("gst_directsound_sink_open: DirectSoundCreate: %s",
450 error_text), (NULL));
458 if (FAILED (hRes = IDirectSound_SetCooperativeLevel (dsoundsink->pDS,
459 GetDesktopWindow (), DSSCL_PRIORITY))) {
460 gchar *error_text = gst_hres_to_string (hRes);
461 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
462 ("gst_directsound_sink_open: IDirectSound_SetCooperativeLevel: %s",
463 error_text), (NULL));
472 gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec * spec)
474 return spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3 ||
475 spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS;
479 gst_directsound_sink_prepare (GstAudioSink * asink,
480 GstAudioRingBufferSpec * spec)
482 GstDirectSoundSink *dsoundsink;
484 DSBUFFERDESC descSecondary;
487 dsoundsink = GST_DIRECTSOUND_SINK (asink);
489 /*save number of bytes per sample and buffer format */
490 dsoundsink->bytes_per_sample = spec->info.bpf;
491 dsoundsink->type = spec->type;
493 /* fill the WAVEFORMATEX structure with spec params */
494 memset (&wfx, 0, sizeof (wfx));
495 if (!gst_directsound_sink_is_spdif_format (spec)) {
496 wfx.cbSize = sizeof (wfx);
497 wfx.wFormatTag = WAVE_FORMAT_PCM;
498 wfx.nChannels = spec->info.channels;
499 wfx.nSamplesPerSec = spec->info.rate;
500 wfx.wBitsPerSample = (spec->info.bpf * 8) / wfx.nChannels;
501 wfx.nBlockAlign = spec->info.bpf;
502 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
504 /* Create directsound buffer with size based on our configured
505 * buffer_size (which is 200 ms by default) */
506 dsoundsink->buffer_size =
507 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->buffer_time,
509 /* Make sure we make those numbers multiple of our sample size in bytes */
510 dsoundsink->buffer_size -= dsoundsink->buffer_size % spec->info.bpf;
513 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->latency_time,
515 spec->segsize -= spec->segsize % spec->info.bpf;
516 spec->segtotal = dsoundsink->buffer_size / spec->segsize;
518 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
520 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
522 wfx.nSamplesPerSec = 48000;
523 wfx.wBitsPerSample = 16;
524 wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
525 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
527 spec->segsize = 6144;
530 g_assert_not_reached ();
534 // Make the final buffer size be an integer number of segments
535 dsoundsink->buffer_size = spec->segsize * spec->segtotal;
537 GST_INFO_OBJECT (dsoundsink,
538 "GstAudioRingBufferSpec->channels: %d, GstAudioRingBufferSpec->rate: %d, GstAudioRingBufferSpec->bytes_per_sample: %d\n"
539 "WAVEFORMATEX.nSamplesPerSec: %ld, WAVEFORMATEX.wBitsPerSample: %d, WAVEFORMATEX.nBlockAlign: %d, WAVEFORMATEX.nAvgBytesPerSec: %ld\n"
540 "Size of dsound circular buffer=>%d\n", spec->info.channels,
541 spec->info.rate, spec->info.bpf, wfx.nSamplesPerSec, wfx.wBitsPerSample,
542 wfx.nBlockAlign, wfx.nAvgBytesPerSec, dsoundsink->buffer_size);
544 /* create a secondary directsound buffer */
545 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
546 descSecondary.dwSize = sizeof (DSBUFFERDESC);
547 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
548 if (!gst_directsound_sink_is_spdif_format (spec))
549 descSecondary.dwFlags |= DSBCAPS_CTRLVOLUME;
551 descSecondary.dwBufferBytes = dsoundsink->buffer_size;
552 descSecondary.lpwfxFormat = (WAVEFORMATEX *) & wfx;
554 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
555 &dsoundsink->pDSBSecondary, NULL);
557 gchar *error_text = gst_hres_to_string (hRes);
558 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
559 ("gst_directsound_sink_prepare: IDirectSound_CreateSoundBuffer: %s",
560 error_text), (NULL));
565 gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
566 gst_directsound_sink_set_mute (dsoundsink, dsoundsink->mute);
572 gst_directsound_sink_unprepare (GstAudioSink * asink)
574 GstDirectSoundSink *dsoundsink;
576 dsoundsink = GST_DIRECTSOUND_SINK (asink);
578 /* release secondary DirectSound buffer */
579 if (dsoundsink->pDSBSecondary) {
580 IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
581 dsoundsink->pDSBSecondary = NULL;
588 gst_directsound_sink_close (GstAudioSink * asink)
590 GstDirectSoundSink *dsoundsink = NULL;
592 dsoundsink = GST_DIRECTSOUND_SINK (asink);
594 /* release DirectSound object */
595 g_return_val_if_fail (dsoundsink->pDS != NULL, FALSE);
596 IDirectSound_Release (dsoundsink->pDS);
597 dsoundsink->pDS = NULL;
599 gst_caps_replace (&dsoundsink->cached_caps, NULL);
605 gst_directsound_sink_write (GstAudioSink * asink, gpointer data, guint length)
607 GstDirectSoundSink *dsoundsink;
610 LPVOID pLockedBuffer1 = NULL, pLockedBuffer2 = NULL;
611 DWORD dwSizeBuffer1, dwSizeBuffer2;
612 DWORD dwCurrentPlayCursor;
614 dsoundsink = GST_DIRECTSOUND_SINK (asink);
616 GST_DSOUND_LOCK (dsoundsink);
618 /* get current buffer status */
619 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
621 /* get current play cursor position */
622 hRes2 = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
623 &dwCurrentPlayCursor, NULL);
625 if (SUCCEEDED (hRes) && SUCCEEDED (hRes2) && (dwStatus & DSBSTATUS_PLAYING)) {
626 DWORD dwFreeBufferSize = 0;
627 guint64 sleep_time_ms = 0;
630 /* Calculate the free space in the circular buffer */
631 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
633 dsoundsink->buffer_size - (dsoundsink->current_circular_offset -
634 dwCurrentPlayCursor);
637 dwCurrentPlayCursor - dsoundsink->current_circular_offset;
639 /* Not enough free space, wait for some samples to be played out. We could
640 * write out partial data, but that will result in a tight loop in the
641 * audioringbuffer write thread, and lead to high CPU usage. */
642 if (length > dwFreeBufferSize) {
643 gint rate = GST_AUDIO_BASE_SINK (asink)->ringbuffer->spec.info.rate;
644 /* Wait for a time proportional to the space needed. In reality, the
645 * directsound sink's position does not update frequently enough, so we
646 * will end up waiting for much longer. Note that Sleep() has millisecond
647 * resolution at best. */
648 sleep_time_ms = gst_util_uint64_scale_int ((length - dwFreeBufferSize),
649 1000, dsoundsink->bytes_per_sample * rate);
650 /* Make sure we don't run in a tight loop unnecessarily */
651 sleep_time_ms = MAX (sleep_time_ms, 10);
652 GST_DEBUG_OBJECT (dsoundsink,
653 "length: %u, FreeBufSiz: %ld, sleep_time_ms: %" G_GUINT64_FORMAT
654 ", bps: %i, rate: %i", length, dwFreeBufferSize, sleep_time_ms,
655 dsoundsink->bytes_per_sample, rate);
656 Sleep (sleep_time_ms);
658 /* May we send out? */
659 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
660 &dwCurrentPlayCursor, NULL);
662 IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
663 if (SUCCEEDED (hRes) && SUCCEEDED (hRes2)
664 && (dwStatus & DSBSTATUS_PLAYING))
665 goto calculate_freesize;
669 dsoundsink->first_buffer_after_reset = FALSE;
670 GST_DSOUND_UNLOCK (dsoundsink);
672 err1 = gst_hres_to_string (hRes);
673 err2 = gst_hres_to_string (hRes2);
674 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_WRITE,
675 ("gst_directsound_sink_write: IDirectSoundBuffer_GetStatus %s, "
676 "IDirectSoundBuffer_GetCurrentPosition: %s, dwStatus: %lu",
677 err2, err1, dwStatus), (NULL));
685 if (dwStatus & DSBSTATUS_BUFFERLOST) {
686 hRes = IDirectSoundBuffer_Restore (dsoundsink->pDSBSecondary); /*need a loop waiting the buffer is restored?? */
687 dsoundsink->current_circular_offset = 0;
690 /* Lock a buffer of length @length for writing */
691 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
692 dsoundsink->current_circular_offset, length, &pLockedBuffer1,
693 &dwSizeBuffer1, &pLockedBuffer2, &dwSizeBuffer2, 0L);
695 if (SUCCEEDED (hRes)) {
696 // Write to pointers without reordering.
697 memcpy (pLockedBuffer1, data, dwSizeBuffer1);
698 if (pLockedBuffer2 != NULL)
699 memcpy (pLockedBuffer2, (LPBYTE) data + dwSizeBuffer1, dwSizeBuffer2);
701 // Update where the buffer will lock (for next time)
702 dsoundsink->current_circular_offset += dwSizeBuffer1 + dwSizeBuffer2;
703 dsoundsink->current_circular_offset %= dsoundsink->buffer_size; /* Circular buffer */
705 hRes = IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer1,
706 dwSizeBuffer1, pLockedBuffer2, dwSizeBuffer2);
709 /* if the buffer was not in playing state yet, call play on the buffer
710 except if this buffer is the fist after a reset (base class call reset and write a buffer when setting the sink to pause) */
711 if (!(dwStatus & DSBSTATUS_PLAYING) &&
712 dsoundsink->first_buffer_after_reset == FALSE) {
713 hRes = IDirectSoundBuffer_Play (dsoundsink->pDSBSecondary, 0, 0,
717 dsoundsink->first_buffer_after_reset = FALSE;
719 GST_DSOUND_UNLOCK (dsoundsink);
725 gst_directsound_sink_delay (GstAudioSink * asink)
727 GstDirectSoundSink *dsoundsink;
729 DWORD dwCurrentPlayCursor;
730 DWORD dwBytesInQueue = 0;
731 gint nNbSamplesInQueue = 0;
734 dsoundsink = GST_DIRECTSOUND_SINK (asink);
736 /* get current buffer status */
737 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
739 if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) {
740 /*evaluate the number of samples in queue in the circular buffer */
741 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
742 &dwCurrentPlayCursor, NULL);
745 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
747 dsoundsink->current_circular_offset - dwCurrentPlayCursor;
750 dsoundsink->current_circular_offset + (dsoundsink->buffer_size -
751 dwCurrentPlayCursor);
753 nNbSamplesInQueue = dwBytesInQueue / dsoundsink->bytes_per_sample;
757 return nNbSamplesInQueue;
761 gst_directsound_sink_reset (GstAudioSink * asink)
763 GstDirectSoundSink *dsoundsink;
764 LPVOID pLockedBuffer = NULL;
765 DWORD dwSizeBuffer = 0;
767 dsoundsink = GST_DIRECTSOUND_SINK (asink);
769 GST_DSOUND_LOCK (dsoundsink);
771 if (dsoundsink->pDSBSecondary) {
773 HRESULT hRes = IDirectSoundBuffer_Stop (dsoundsink->pDSBSecondary);
776 hRes = IDirectSoundBuffer_SetCurrentPosition (dsoundsink->pDSBSecondary, 0);
777 dsoundsink->current_circular_offset = 0;
779 /*reset the buffer */
780 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
781 dsoundsink->current_circular_offset, dsoundsink->buffer_size,
782 &pLockedBuffer, &dwSizeBuffer, NULL, NULL, 0L);
784 if (SUCCEEDED (hRes)) {
785 memset (pLockedBuffer, 0, dwSizeBuffer);
788 IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer,
789 dwSizeBuffer, NULL, 0);
793 dsoundsink->first_buffer_after_reset = TRUE;
795 GST_DSOUND_UNLOCK (dsoundsink);
799 * gst_directsound_probe_supported_formats:
801 * Takes the template caps and returns the subset which is actually
802 * supported by this device.
807 gst_directsound_probe_supported_formats (GstDirectSoundSink * dsoundsink,
808 const GstCaps * template_caps)
811 DSBUFFERDESC descSecondary;
815 LPDIRECTSOUNDBUFFER tmpBuffer;
817 caps = gst_caps_copy (template_caps);
820 * Check availability of digital output by trying to create an SPDIF buffer
823 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
824 /* fill the WAVEFORMATEX structure with some standard AC3 over SPDIF params */
825 memset (&wfx, 0, sizeof (wfx));
827 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
829 wfx.nSamplesPerSec = 48000;
830 wfx.wBitsPerSample = 16;
832 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
834 // create a secondary directsound buffer
835 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
836 descSecondary.dwSize = sizeof (DSBUFFERDESC);
837 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
838 descSecondary.dwBufferBytes = 6144;
839 descSecondary.lpwfxFormat = &wfx;
841 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
844 gchar *error_text = gst_hres_to_string (hRes);
845 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough not supported "
846 "(IDirectSound_CreateSoundBuffer returned: %s)\n", error_text);
848 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
849 tmp2 = gst_caps_subtract (caps, tmp);
850 gst_caps_unref (tmp);
851 gst_caps_unref (caps);
853 tmp = gst_caps_new_empty_simple ("audio/x-dts");
854 tmp2 = gst_caps_subtract (caps, tmp);
855 gst_caps_unref (tmp);
856 gst_caps_unref (caps);
859 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
860 hRes = IDirectSoundBuffer_Release (tmpBuffer);
862 gchar *error_text = gst_hres_to_string (hRes);
863 GST_DEBUG_OBJECT (dsoundsink,
864 "(IDirectSoundBuffer_Release returned: %s)\n", error_text);
869 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
870 tmp2 = gst_caps_subtract (caps, tmp);
871 gst_caps_unref (tmp);
872 gst_caps_unref (caps);
874 tmp = gst_caps_new_empty_simple ("audio/x-dts");
875 tmp2 = gst_caps_subtract (caps, tmp);
876 gst_caps_unref (tmp);
877 gst_caps_unref (caps);
885 gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
887 if (gst_directsound_sink_is_spdif_format (&sink->ringbuffer->spec)) {
888 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
890 GstMapInfo infobuf, infoout;
896 out = gst_buffer_new_and_alloc (framesize);
898 if (!gst_buffer_map (buf, &infobuf, GST_MAP_READWRITE)) {
899 gst_buffer_unref (out);
902 if (!gst_buffer_map (out, &infoout, GST_MAP_READWRITE)) {
903 gst_buffer_unmap (buf, &infobuf);
904 gst_buffer_unref (out);
907 success = gst_audio_iec61937_payload (infobuf.data, infobuf.size,
908 infoout.data, infoout.size, &sink->ringbuffer->spec, G_BYTE_ORDER);
910 gst_buffer_unmap (out, &infoout);
911 gst_buffer_unmap (buf, &infobuf);
912 gst_buffer_unref (out);
916 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_ALL, 0, -1);
918 _swab ((gchar *) infoout.data, (gchar *) infoout.data, infobuf.size);
919 gst_buffer_unmap (out, &infoout);
920 gst_buffer_unmap (buf, &infobuf);
923 return gst_buffer_ref (buf);
927 gst_directsound_sink_set_volume (GstDirectSoundSink * dsoundsink,
928 gdouble dvolume, gboolean store)
932 volume = dvolume * 100;
934 dsoundsink->volume = volume;
936 if (dsoundsink->pDSBSecondary) {
937 /* DirectSound controls volume using units of 100th of a decibel,
938 * ranging from -10000 to 0. We use a linear scale of 0 - 100
942 if (volume == 0 || dsoundsink->mute)
945 dsVolume = 100 * (long) (20 * log10 ((double) volume / 100.));
946 dsVolume = CLAMP (dsVolume, -10000, 0);
948 GST_DEBUG_OBJECT (dsoundsink,
949 "Setting volume on secondary buffer to %d from %d", (int) dsVolume,
951 IDirectSoundBuffer_SetVolume (dsoundsink->pDSBSecondary, dsVolume);
956 gst_directsound_sink_get_volume (GstDirectSoundSink * dsoundsink)
958 return (gdouble) dsoundsink->volume / 100;
962 gst_directsound_sink_set_mute (GstDirectSoundSink * dsoundsink, gboolean mute)
965 gst_directsound_sink_set_volume (dsoundsink, 0, FALSE);
966 dsoundsink->mute = TRUE;
968 gst_directsound_sink_set_volume (dsoundsink,
969 gst_directsound_sink_get_volume (dsoundsink), FALSE);
970 dsoundsink->mute = FALSE;
976 gst_directsound_sink_get_mute (GstDirectSoundSink * dsoundsink)
978 return dsoundsink->mute;
982 gst_directsound_sink_get_device (GstDirectSoundSink * dsoundsink)
984 return dsoundsink->device_id;
988 gst_directsound_sink_set_device (GstDirectSoundSink * dsoundsink,
989 const gchar * device_id)
991 g_free (dsoundsink->device_id);
992 dsoundsink->device_id = g_strdup (device_id);
995 /* Converts a HRESULT error to a text string
996 * LPTSTR is either a */
998 gst_hres_to_string (HRESULT hRes)
1002 LPTSTR error_text = NULL;
1004 flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER
1005 | FORMAT_MESSAGE_IGNORE_INSERTS;
1006 FormatMessage (flags, NULL, hRes, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1007 (LPTSTR) & error_text, 0, NULL);
1010 /* If UNICODE is defined, LPTSTR is LPWSTR which is UTF-16 */
1011 ret_text = g_utf16_to_utf8 (error_text, 0, NULL, NULL, NULL);
1013 ret_text = g_strdup (error_text);
1016 LocalFree (error_text);