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);
151 gst_object_unref (dsoundsink->system_clock);
152 if (dsoundsink->write_wait_clock_id != NULL) {
153 gst_clock_id_unref (dsoundsink->write_wait_clock_id);
156 G_OBJECT_CLASS (parent_class)->finalize (object);
160 gst_directsound_sink_class_init (GstDirectSoundSinkClass * klass)
162 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
163 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
164 GstAudioSinkClass *gstaudiosink_class = GST_AUDIO_SINK_CLASS (klass);
165 GstAudioBaseSinkClass *gstaudiobasesink_class =
166 GST_AUDIO_BASE_SINK_CLASS (klass);
167 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
169 GST_DEBUG_CATEGORY_INIT (directsoundsink_debug, "directsoundsink", 0,
172 gobject_class->finalize = gst_directsound_sink_finalize;
173 gobject_class->set_property = gst_directsound_sink_set_property;
174 gobject_class->get_property = gst_directsound_sink_get_property;
176 gstbasesink_class->get_caps =
177 GST_DEBUG_FUNCPTR (gst_directsound_sink_getcaps);
179 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_directsound_sink_query);
181 gstaudiobasesink_class->payload =
182 GST_DEBUG_FUNCPTR (gst_directsound_sink_payload);
184 gstaudiosink_class->prepare =
185 GST_DEBUG_FUNCPTR (gst_directsound_sink_prepare);
186 gstaudiosink_class->unprepare =
187 GST_DEBUG_FUNCPTR (gst_directsound_sink_unprepare);
188 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_directsound_sink_open);
189 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_directsound_sink_close);
190 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_directsound_sink_write);
191 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_directsound_sink_delay);
192 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_directsound_sink_reset);
194 g_object_class_install_property (gobject_class,
196 g_param_spec_double ("volume", "Volume",
197 "Volume of this stream", 0.0, 1.0, 1.0,
198 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200 g_object_class_install_property (gobject_class,
202 g_param_spec_boolean ("mute", "Mute",
203 "Mute state of this stream", DEFAULT_MUTE,
204 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206 g_object_class_install_property (gobject_class,
208 g_param_spec_string ("device", "Device",
209 "DirectSound playback device as a GUID string",
210 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 gst_element_class_set_static_metadata (element_class,
213 "Direct Sound Audio Sink", "Sink/Audio",
214 "Output to a sound card via Direct Sound",
215 "Sebastien Moutte <sebastien@moutte.net>");
217 gst_element_class_add_static_pad_template (element_class,
218 &directsoundsink_sink_factory);
222 gst_directsound_sink_init (GstDirectSoundSink * dsoundsink)
224 dsoundsink->volume = 100;
225 dsoundsink->mute = FALSE;
226 dsoundsink->device_id = NULL;
227 dsoundsink->pDS = NULL;
228 dsoundsink->cached_caps = NULL;
229 dsoundsink->pDSBSecondary = NULL;
230 dsoundsink->current_circular_offset = 0;
231 dsoundsink->buffer_size = DSBSIZE_MIN;
232 dsoundsink->volume = 100;
233 g_mutex_init (&dsoundsink->dsound_lock);
234 dsoundsink->system_clock = gst_system_clock_obtain ();
235 dsoundsink->write_wait_clock_id = NULL;
236 dsoundsink->first_buffer_after_reset = FALSE;
240 gst_directsound_sink_set_property (GObject * object,
241 guint prop_id, const GValue * value, GParamSpec * pspec)
243 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
247 gst_directsound_sink_set_volume (sink, g_value_get_double (value), TRUE);
250 gst_directsound_sink_set_mute (sink, g_value_get_boolean (value));
253 gst_directsound_sink_set_device (sink, g_value_get_string (value));
256 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262 gst_directsound_sink_get_property (GObject * object,
263 guint prop_id, GValue * value, GParamSpec * pspec)
265 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
269 g_value_set_double (value, gst_directsound_sink_get_volume (sink));
272 g_value_set_boolean (value, gst_directsound_sink_get_mute (sink));
275 g_value_set_string (value, gst_directsound_sink_get_device (sink));
278 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 gst_directsound_sink_getcaps (GstBaseSink * bsink, GstCaps * filter)
286 GstElementClass *element_class;
287 GstPadTemplate *pad_template;
288 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (bsink);
291 if (dsoundsink->pDS == NULL) {
292 GST_DEBUG_OBJECT (dsoundsink, "device not open, using template caps");
293 return NULL; /* base class will get template caps for us */
296 if (dsoundsink->cached_caps) {
297 caps = gst_caps_ref (dsoundsink->cached_caps);
299 element_class = GST_ELEMENT_GET_CLASS (dsoundsink);
300 pad_template = gst_element_class_get_pad_template (element_class, "sink");
301 g_return_val_if_fail (pad_template != NULL, NULL);
303 caps = gst_directsound_probe_supported_formats (dsoundsink,
304 gst_pad_template_get_caps (pad_template));
306 dsoundsink->cached_caps = gst_caps_ref (caps);
309 if (caps && filter) {
311 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
312 gst_caps_unref (caps);
317 gchar *caps_string = gst_caps_to_string (caps);
318 GST_DEBUG_OBJECT (dsoundsink, "returning caps %s", caps_string);
319 g_free (caps_string);
326 gst_directsound_sink_acceptcaps (GstBaseSink * sink, GstQuery * query)
328 GstDirectSoundSink *dsink = GST_DIRECTSOUND_SINK (sink);
333 gboolean ret = FALSE;
334 GstAudioRingBufferSpec spec = { 0 };
336 if (G_UNLIKELY (dsink == NULL))
341 gst_query_parse_accept_caps (query, &caps);
342 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
344 pad_caps = gst_pad_query_caps (pad, NULL);
346 gboolean cret = gst_caps_is_subset (caps, pad_caps);
347 gst_caps_unref (pad_caps);
349 GST_DEBUG_OBJECT (dsink,
350 "Caps are not a subset of the pad caps, not accepting caps");
355 /* If we've not got fixed caps, creating a stream might fail, so let's just
356 * return from here with default acceptcaps behaviour */
357 if (!gst_caps_is_fixed (caps)) {
358 GST_DEBUG_OBJECT (dsink, "Caps are not fixed, not accepting caps");
362 spec.latency_time = GST_SECOND;
363 if (!gst_audio_ring_buffer_parse_caps (&spec, caps)) {
364 GST_DEBUG_OBJECT (dsink, "Failed to parse caps, not accepting");
368 /* Make sure input is framed (one frame per buffer) and can be payloaded */
370 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
371 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
373 gboolean framed = FALSE, parsed = FALSE;
374 st = gst_caps_get_structure (caps, 0);
376 gst_structure_get_boolean (st, "framed", &framed);
377 gst_structure_get_boolean (st, "parsed", &parsed);
378 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0) {
379 GST_DEBUG_OBJECT (dsink, "Wrong AC3/DTS caps, not accepting");
387 GST_DEBUG_OBJECT (dsink, "Accepting caps");
390 gst_query_set_accept_caps_result (query, ret);
395 gst_directsound_sink_query (GstBaseSink * sink, GstQuery * query)
399 switch (GST_QUERY_TYPE (query)) {
400 case GST_QUERY_ACCEPT_CAPS:
401 res = gst_directsound_sink_acceptcaps (sink, query);
404 res = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
411 string_to_guid (const gchar * str)
417 wstr = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL);
421 out = g_new (GUID, 1);
422 ret = CLSIDFromString ((LPOLESTR) wstr, out);
424 if (ret != NOERROR) {
433 gst_directsound_sink_open (GstAudioSink * asink)
435 GstDirectSoundSink *dsoundsink;
437 LPGUID lpGuid = NULL;
439 dsoundsink = GST_DIRECTSOUND_SINK (asink);
441 if (dsoundsink->device_id) {
442 lpGuid = string_to_guid (dsoundsink->device_id);
443 if (lpGuid == NULL) {
444 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
445 ("gst_directsound_sink_open: device set, but guid not found: %s",
446 dsoundsink->device_id), (NULL));
451 /* create and initialize a DirecSound object */
452 if (FAILED (hRes = DirectSoundCreate (lpGuid, &dsoundsink->pDS, NULL))) {
453 gchar *error_text = gst_hres_to_string (hRes);
454 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
455 ("gst_directsound_sink_open: DirectSoundCreate: %s",
456 error_text), (NULL));
464 if (FAILED (hRes = IDirectSound_SetCooperativeLevel (dsoundsink->pDS,
465 GetDesktopWindow (), DSSCL_PRIORITY))) {
466 gchar *error_text = gst_hres_to_string (hRes);
467 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
468 ("gst_directsound_sink_open: IDirectSound_SetCooperativeLevel: %s",
469 error_text), (NULL));
478 gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec * spec)
480 return spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3 ||
481 spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS;
485 gst_directsound_sink_prepare (GstAudioSink * asink,
486 GstAudioRingBufferSpec * spec)
488 GstDirectSoundSink *dsoundsink;
490 DSBUFFERDESC descSecondary;
493 dsoundsink = GST_DIRECTSOUND_SINK (asink);
495 /*save number of bytes per sample and buffer format */
496 dsoundsink->bytes_per_sample = spec->info.bpf;
497 dsoundsink->type = spec->type;
499 /* fill the WAVEFORMATEX structure with spec params */
500 memset (&wfx, 0, sizeof (wfx));
501 if (!gst_directsound_sink_is_spdif_format (spec)) {
502 wfx.cbSize = sizeof (wfx);
503 wfx.wFormatTag = WAVE_FORMAT_PCM;
504 wfx.nChannels = spec->info.channels;
505 wfx.nSamplesPerSec = spec->info.rate;
506 wfx.wBitsPerSample = (spec->info.bpf * 8) / wfx.nChannels;
507 wfx.nBlockAlign = spec->info.bpf;
508 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
510 /* Create directsound buffer with size based on our configured
511 * buffer_size (which is 200 ms by default) */
512 dsoundsink->buffer_size =
513 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->buffer_time,
515 /* Make sure we make those numbers multiple of our sample size in bytes */
516 dsoundsink->buffer_size -= dsoundsink->buffer_size % spec->info.bpf;
519 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->latency_time,
521 spec->segsize -= spec->segsize % spec->info.bpf;
522 spec->segtotal = dsoundsink->buffer_size / spec->segsize;
524 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
526 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
528 wfx.nSamplesPerSec = 48000;
529 wfx.wBitsPerSample = 16;
530 wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
531 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
533 spec->segsize = 6144;
536 g_assert_not_reached ();
540 // Make the final buffer size be an integer number of segments
541 dsoundsink->buffer_size = spec->segsize * spec->segtotal;
543 GST_INFO_OBJECT (dsoundsink,
544 "GstAudioRingBufferSpec->channels: %d, GstAudioRingBufferSpec->rate: %d, GstAudioRingBufferSpec->bytes_per_sample: %d\n"
545 "WAVEFORMATEX.nSamplesPerSec: %ld, WAVEFORMATEX.wBitsPerSample: %d, WAVEFORMATEX.nBlockAlign: %d, WAVEFORMATEX.nAvgBytesPerSec: %ld\n"
546 "Size of dsound circular buffer=>%d\n", spec->info.channels,
547 spec->info.rate, spec->info.bpf, wfx.nSamplesPerSec, wfx.wBitsPerSample,
548 wfx.nBlockAlign, wfx.nAvgBytesPerSec, dsoundsink->buffer_size);
550 /* create a secondary directsound buffer */
551 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
552 descSecondary.dwSize = sizeof (DSBUFFERDESC);
553 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
554 if (!gst_directsound_sink_is_spdif_format (spec))
555 descSecondary.dwFlags |= DSBCAPS_CTRLVOLUME;
557 descSecondary.dwBufferBytes = dsoundsink->buffer_size;
558 descSecondary.lpwfxFormat = (WAVEFORMATEX *) & wfx;
560 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
561 &dsoundsink->pDSBSecondary, NULL);
563 gchar *error_text = gst_hres_to_string (hRes);
564 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
565 ("gst_directsound_sink_prepare: IDirectSound_CreateSoundBuffer: %s",
566 error_text), (NULL));
571 gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
572 gst_directsound_sink_set_mute (dsoundsink, dsoundsink->mute);
578 gst_directsound_sink_unprepare (GstAudioSink * asink)
580 GstDirectSoundSink *dsoundsink;
582 dsoundsink = GST_DIRECTSOUND_SINK (asink);
584 /* release secondary DirectSound buffer */
585 if (dsoundsink->pDSBSecondary) {
586 IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
587 dsoundsink->pDSBSecondary = NULL;
594 gst_directsound_sink_close (GstAudioSink * asink)
596 GstDirectSoundSink *dsoundsink = NULL;
598 dsoundsink = GST_DIRECTSOUND_SINK (asink);
600 /* release DirectSound object */
601 g_return_val_if_fail (dsoundsink->pDS != NULL, FALSE);
602 IDirectSound_Release (dsoundsink->pDS);
603 dsoundsink->pDS = NULL;
605 gst_caps_replace (&dsoundsink->cached_caps, NULL);
611 gst_directsound_sink_write (GstAudioSink * asink, gpointer data, guint length)
613 GstDirectSoundSink *dsoundsink;
616 LPVOID pLockedBuffer1 = NULL, pLockedBuffer2 = NULL;
617 DWORD dwSizeBuffer1, dwSizeBuffer2;
618 DWORD dwCurrentPlayCursor;
620 dsoundsink = GST_DIRECTSOUND_SINK (asink);
622 GST_DSOUND_LOCK (dsoundsink);
624 /* get current buffer status */
625 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
627 /* get current play cursor position */
628 hRes2 = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
629 &dwCurrentPlayCursor, NULL);
631 if (SUCCEEDED (hRes) && SUCCEEDED (hRes2) && (dwStatus & DSBSTATUS_PLAYING)) {
632 DWORD dwFreeBufferSize = 0;
633 GstClockTime sleep_time_ms = 0, sleep_until;
637 /* Calculate the free space in the circular buffer */
638 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
640 dsoundsink->buffer_size - (dsoundsink->current_circular_offset -
641 dwCurrentPlayCursor);
644 dwCurrentPlayCursor - dsoundsink->current_circular_offset;
646 /* Not enough free space, wait for some samples to be played out. We could
647 * write out partial data, but that will result in a tight loop in the
648 * audioringbuffer write thread, and lead to high CPU usage. */
649 if (length > dwFreeBufferSize) {
650 gint rate = GST_AUDIO_BASE_SINK (asink)->ringbuffer->spec.info.rate;
651 /* Wait for a time proportional to the space needed. In reality, the
652 * directsound sink's position does not update frequently enough, so we
653 * will end up waiting for much longer. Note that Sleep() has millisecond
654 * resolution at best. */
655 sleep_time_ms = gst_util_uint64_scale_int ((length - dwFreeBufferSize),
656 1000, dsoundsink->bytes_per_sample * rate);
657 /* Make sure we don't run in a tight loop unnecessarily */
658 sleep_time_ms = MAX (sleep_time_ms, 10);
659 sleep_until = gst_clock_get_time (dsoundsink->system_clock) +
660 sleep_time_ms * GST_MSECOND;
662 GST_DEBUG_OBJECT (dsoundsink,
663 "length: %u, FreeBufSiz: %ld, sleep_time_ms: %" G_GUINT64_FORMAT
664 ", bps: %i, rate: %i", length, dwFreeBufferSize, sleep_time_ms,
665 dsoundsink->bytes_per_sample, rate);
667 if (G_UNLIKELY (dsoundsink->write_wait_clock_id == NULL ||
668 gst_clock_single_shot_id_reinit (dsoundsink->system_clock,
669 dsoundsink->write_wait_clock_id, sleep_until) == FALSE)) {
671 if (dsoundsink->write_wait_clock_id != NULL) {
672 gst_clock_id_unref (dsoundsink->write_wait_clock_id);
675 dsoundsink->write_wait_clock_id =
676 gst_clock_new_single_shot_id (dsoundsink->system_clock,
680 clock_id = dsoundsink->write_wait_clock_id;
681 dsoundsink->reset_while_sleeping = FALSE;
683 GST_DSOUND_UNLOCK (dsoundsink);
685 /* don't bother with the return value as we'll detect reset separately,
686 as reset could happen between when this returns and we obtain the lock
687 again -- so we can't use UNSCHEDULED here */
688 gst_clock_id_wait (clock_id, NULL);
690 GST_DSOUND_LOCK (dsoundsink);
692 /* if a reset occurs, exit now */
693 if (dsoundsink->reset_while_sleeping == TRUE) {
694 GST_DSOUND_UNLOCK (dsoundsink);
698 /* May we send out? */
699 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
700 &dwCurrentPlayCursor, NULL);
702 IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
703 if (SUCCEEDED (hRes) && SUCCEEDED (hRes2)
704 && (dwStatus & DSBSTATUS_PLAYING))
705 goto calculate_freesize;
709 dsoundsink->first_buffer_after_reset = FALSE;
710 GST_DSOUND_UNLOCK (dsoundsink);
712 err1 = gst_hres_to_string (hRes);
713 err2 = gst_hres_to_string (hRes2);
714 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_WRITE,
715 ("gst_directsound_sink_write: IDirectSoundBuffer_GetStatus %s, "
716 "IDirectSoundBuffer_GetCurrentPosition: %s, dwStatus: %lu",
717 err2, err1, dwStatus), (NULL));
725 if (dwStatus & DSBSTATUS_BUFFERLOST) {
726 hRes = IDirectSoundBuffer_Restore (dsoundsink->pDSBSecondary); /*need a loop waiting the buffer is restored?? */
727 dsoundsink->current_circular_offset = 0;
730 /* Lock a buffer of length @length for writing */
731 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
732 dsoundsink->current_circular_offset, length, &pLockedBuffer1,
733 &dwSizeBuffer1, &pLockedBuffer2, &dwSizeBuffer2, 0L);
735 if (SUCCEEDED (hRes)) {
736 // Write to pointers without reordering.
737 memcpy (pLockedBuffer1, data, dwSizeBuffer1);
738 if (pLockedBuffer2 != NULL)
739 memcpy (pLockedBuffer2, (LPBYTE) data + dwSizeBuffer1, dwSizeBuffer2);
741 hRes = IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer1,
742 dwSizeBuffer1, pLockedBuffer2, dwSizeBuffer2);
744 // Update where the buffer will lock (for next time)
745 dsoundsink->current_circular_offset += dwSizeBuffer1 + dwSizeBuffer2;
746 dsoundsink->current_circular_offset %= dsoundsink->buffer_size; /* Circular buffer */
749 /* if the buffer was not in playing state yet, call play on the buffer
750 except if this buffer is the fist after a reset (base class call reset and write a buffer when setting the sink to pause) */
751 if (!(dwStatus & DSBSTATUS_PLAYING) &&
752 dsoundsink->first_buffer_after_reset == FALSE) {
753 hRes = IDirectSoundBuffer_Play (dsoundsink->pDSBSecondary, 0, 0,
757 dsoundsink->first_buffer_after_reset = FALSE;
759 GST_DSOUND_UNLOCK (dsoundsink);
765 gst_directsound_sink_delay (GstAudioSink * asink)
767 GstDirectSoundSink *dsoundsink;
769 DWORD dwCurrentPlayCursor;
770 DWORD dwBytesInQueue = 0;
771 gint nNbSamplesInQueue = 0;
774 dsoundsink = GST_DIRECTSOUND_SINK (asink);
776 /* get current buffer status */
777 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
779 if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) {
780 /*evaluate the number of samples in queue in the circular buffer */
781 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
782 &dwCurrentPlayCursor, NULL);
785 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
787 dsoundsink->current_circular_offset - dwCurrentPlayCursor;
790 dsoundsink->current_circular_offset + (dsoundsink->buffer_size -
791 dwCurrentPlayCursor);
793 nNbSamplesInQueue = dwBytesInQueue / dsoundsink->bytes_per_sample;
797 return nNbSamplesInQueue;
801 gst_directsound_sink_reset (GstAudioSink * asink)
803 GstDirectSoundSink *dsoundsink;
804 LPVOID pLockedBuffer = NULL;
805 DWORD dwSizeBuffer = 0;
807 dsoundsink = GST_DIRECTSOUND_SINK (asink);
809 GST_DSOUND_LOCK (dsoundsink);
811 if (dsoundsink->pDSBSecondary) {
813 HRESULT hRes = IDirectSoundBuffer_Stop (dsoundsink->pDSBSecondary);
816 hRes = IDirectSoundBuffer_SetCurrentPosition (dsoundsink->pDSBSecondary, 0);
817 dsoundsink->current_circular_offset = 0;
819 /*reset the buffer */
820 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
821 dsoundsink->current_circular_offset, dsoundsink->buffer_size,
822 &pLockedBuffer, &dwSizeBuffer, NULL, NULL, 0L);
824 if (SUCCEEDED (hRes)) {
825 memset (pLockedBuffer, 0, dwSizeBuffer);
828 IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer,
829 dwSizeBuffer, NULL, 0);
833 dsoundsink->reset_while_sleeping = TRUE;
834 dsoundsink->first_buffer_after_reset = TRUE;
835 if (dsoundsink->write_wait_clock_id != NULL) {
836 gst_clock_id_unschedule (dsoundsink->write_wait_clock_id);
839 GST_DSOUND_UNLOCK (dsoundsink);
843 * gst_directsound_probe_supported_formats:
845 * Takes the template caps and returns the subset which is actually
846 * supported by this device.
851 gst_directsound_probe_supported_formats (GstDirectSoundSink * dsoundsink,
852 const GstCaps * template_caps)
855 DSBUFFERDESC descSecondary;
859 LPDIRECTSOUNDBUFFER tmpBuffer;
861 caps = gst_caps_copy (template_caps);
864 * Check availability of digital output by trying to create an SPDIF buffer
867 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
868 /* fill the WAVEFORMATEX structure with some standard AC3 over SPDIF params */
869 memset (&wfx, 0, sizeof (wfx));
871 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
873 wfx.nSamplesPerSec = 48000;
874 wfx.wBitsPerSample = 16;
876 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
878 // create a secondary directsound buffer
879 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
880 descSecondary.dwSize = sizeof (DSBUFFERDESC);
881 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
882 descSecondary.dwBufferBytes = 6144;
883 descSecondary.lpwfxFormat = &wfx;
885 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
888 gchar *error_text = gst_hres_to_string (hRes);
889 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough not supported "
890 "(IDirectSound_CreateSoundBuffer returned: %s)\n", error_text);
892 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
893 tmp2 = gst_caps_subtract (caps, tmp);
894 gst_caps_unref (tmp);
895 gst_caps_unref (caps);
897 tmp = gst_caps_new_empty_simple ("audio/x-dts");
898 tmp2 = gst_caps_subtract (caps, tmp);
899 gst_caps_unref (tmp);
900 gst_caps_unref (caps);
903 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
904 hRes = IDirectSoundBuffer_Release (tmpBuffer);
906 gchar *error_text = gst_hres_to_string (hRes);
907 GST_DEBUG_OBJECT (dsoundsink,
908 "(IDirectSoundBuffer_Release returned: %s)\n", error_text);
913 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
914 tmp2 = gst_caps_subtract (caps, tmp);
915 gst_caps_unref (tmp);
916 gst_caps_unref (caps);
918 tmp = gst_caps_new_empty_simple ("audio/x-dts");
919 tmp2 = gst_caps_subtract (caps, tmp);
920 gst_caps_unref (tmp);
921 gst_caps_unref (caps);
929 gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
931 if (gst_directsound_sink_is_spdif_format (&sink->ringbuffer->spec)) {
932 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
934 GstMapInfo infobuf, infoout;
940 out = gst_buffer_new_and_alloc (framesize);
942 if (!gst_buffer_map (buf, &infobuf, GST_MAP_READWRITE)) {
943 gst_buffer_unref (out);
946 if (!gst_buffer_map (out, &infoout, GST_MAP_READWRITE)) {
947 gst_buffer_unmap (buf, &infobuf);
948 gst_buffer_unref (out);
951 success = gst_audio_iec61937_payload (infobuf.data, infobuf.size,
952 infoout.data, infoout.size, &sink->ringbuffer->spec, G_BYTE_ORDER);
954 gst_buffer_unmap (out, &infoout);
955 gst_buffer_unmap (buf, &infobuf);
956 gst_buffer_unref (out);
960 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_ALL, 0, -1);
962 _swab ((gchar *) infoout.data, (gchar *) infoout.data, infobuf.size);
963 gst_buffer_unmap (out, &infoout);
964 gst_buffer_unmap (buf, &infobuf);
967 return gst_buffer_ref (buf);
971 gst_directsound_sink_set_volume (GstDirectSoundSink * dsoundsink,
972 gdouble dvolume, gboolean store)
976 volume = dvolume * 100;
978 dsoundsink->volume = volume;
980 if (dsoundsink->pDSBSecondary) {
981 /* DirectSound controls volume using units of 100th of a decibel,
982 * ranging from -10000 to 0. We use a linear scale of 0 - 100
986 if (volume == 0 || dsoundsink->mute)
989 dsVolume = 100 * (long) (20 * log10 ((double) volume / 100.));
990 dsVolume = CLAMP (dsVolume, -10000, 0);
992 GST_DEBUG_OBJECT (dsoundsink,
993 "Setting volume on secondary buffer to %d from %d", (int) dsVolume,
995 IDirectSoundBuffer_SetVolume (dsoundsink->pDSBSecondary, dsVolume);
1000 gst_directsound_sink_get_volume (GstDirectSoundSink * dsoundsink)
1002 return (gdouble) dsoundsink->volume / 100;
1006 gst_directsound_sink_set_mute (GstDirectSoundSink * dsoundsink, gboolean mute)
1009 gst_directsound_sink_set_volume (dsoundsink, 0, FALSE);
1010 dsoundsink->mute = TRUE;
1012 gst_directsound_sink_set_volume (dsoundsink,
1013 gst_directsound_sink_get_volume (dsoundsink), FALSE);
1014 dsoundsink->mute = FALSE;
1020 gst_directsound_sink_get_mute (GstDirectSoundSink * dsoundsink)
1022 return dsoundsink->mute;
1025 static const gchar *
1026 gst_directsound_sink_get_device (GstDirectSoundSink * dsoundsink)
1028 return dsoundsink->device_id;
1032 gst_directsound_sink_set_device (GstDirectSoundSink * dsoundsink,
1033 const gchar * device_id)
1035 g_free (dsoundsink->device_id);
1036 dsoundsink->device_id = g_strdup (device_id);
1039 /* Converts a HRESULT error to a text string
1040 * LPTSTR is either a */
1042 gst_hres_to_string (HRESULT hRes)
1046 LPTSTR error_text = NULL;
1048 flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER
1049 | FORMAT_MESSAGE_IGNORE_INSERTS;
1050 FormatMessage (flags, NULL, hRes, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1051 (LPTSTR) & error_text, 0, NULL);
1054 /* If UNICODE is defined, LPTSTR is LPWSTR which is UTF-16 */
1055 ret_text = g_utf16_to_utf8 (error_text, 0, NULL, NULL, NULL);
1057 ret_text = g_strdup (error_text);
1060 LocalFree (error_text);