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);
105 static gboolean gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec *
108 static GstStaticPadTemplate directsoundsink_sink_factory =
109 GST_STATIC_PAD_TEMPLATE ("sink",
112 GST_STATIC_CAPS ("audio/x-raw, "
113 "format = (string) S16LE, "
114 "layout = (string) interleaved, "
115 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
117 "format = (string) U8, "
118 "layout = (string) interleaved, "
119 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ];"
120 "audio/x-ac3, framed = (boolean) true;"
121 "audio/x-dts, framed = (boolean) true;"));
130 #define gst_directsound_sink_parent_class parent_class
131 G_DEFINE_TYPE_WITH_CODE (GstDirectSoundSink, gst_directsound_sink,
132 GST_TYPE_AUDIO_SINK, G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
136 gst_directsound_sink_finalize (GObject * object)
138 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (object);
140 g_mutex_clear (&dsoundsink->dsound_lock);
142 G_OBJECT_CLASS (parent_class)->finalize (object);
146 gst_directsound_sink_class_init (GstDirectSoundSinkClass * klass)
148 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
149 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
150 GstAudioSinkClass *gstaudiosink_class = GST_AUDIO_SINK_CLASS (klass);
151 GstAudioBaseSinkClass *gstaudiobasesink_class =
152 GST_AUDIO_BASE_SINK_CLASS (klass);
153 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
155 GST_DEBUG_CATEGORY_INIT (directsoundsink_debug, "directsoundsink", 0,
158 gobject_class->finalize = gst_directsound_sink_finalize;
159 gobject_class->set_property = gst_directsound_sink_set_property;
160 gobject_class->get_property = gst_directsound_sink_get_property;
162 gstbasesink_class->get_caps =
163 GST_DEBUG_FUNCPTR (gst_directsound_sink_getcaps);
165 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_directsound_sink_query);
167 gstaudiobasesink_class->payload =
168 GST_DEBUG_FUNCPTR (gst_directsound_sink_payload);
170 gstaudiosink_class->prepare =
171 GST_DEBUG_FUNCPTR (gst_directsound_sink_prepare);
172 gstaudiosink_class->unprepare =
173 GST_DEBUG_FUNCPTR (gst_directsound_sink_unprepare);
174 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_directsound_sink_open);
175 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_directsound_sink_close);
176 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_directsound_sink_write);
177 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_directsound_sink_delay);
178 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_directsound_sink_reset);
180 g_object_class_install_property (gobject_class,
182 g_param_spec_double ("volume", "Volume",
183 "Volume of this stream", 0.0, 1.0, 1.0,
184 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 g_object_class_install_property (gobject_class,
188 g_param_spec_boolean ("mute", "Mute",
189 "Mute state of this stream", DEFAULT_MUTE,
190 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 gst_element_class_set_static_metadata (element_class,
193 "Direct Sound Audio Sink", "Sink/Audio",
194 "Output to a sound card via Direct Sound",
195 "Sebastien Moutte <sebastien@moutte.net>");
197 gst_element_class_add_pad_template (element_class,
198 gst_static_pad_template_get (&directsoundsink_sink_factory));
202 gst_directsound_sink_init (GstDirectSoundSink * dsoundsink)
204 dsoundsink->volume = 100;
205 dsoundsink->mute = FALSE;
206 dsoundsink->pDS = NULL;
207 dsoundsink->cached_caps = NULL;
208 dsoundsink->pDSBSecondary = NULL;
209 dsoundsink->current_circular_offset = 0;
210 dsoundsink->buffer_size = DSBSIZE_MIN;
211 dsoundsink->volume = 100;
212 g_mutex_init (&dsoundsink->dsound_lock);
213 dsoundsink->first_buffer_after_reset = FALSE;
217 gst_directsound_sink_set_property (GObject * object,
218 guint prop_id, const GValue * value, GParamSpec * pspec)
220 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
224 gst_directsound_sink_set_volume (sink, g_value_get_double (value), TRUE);
227 gst_directsound_sink_set_mute (sink, g_value_get_boolean (value));
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 gst_directsound_sink_get_property (GObject * object,
237 guint prop_id, GValue * value, GParamSpec * pspec)
239 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
243 g_value_set_double (value, gst_directsound_sink_get_volume (sink));
246 g_value_set_boolean (value, gst_directsound_sink_get_mute (sink));
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255 gst_directsound_sink_getcaps (GstBaseSink * bsink, GstCaps * filter)
257 GstElementClass *element_class;
258 GstPadTemplate *pad_template;
259 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (bsink);
262 if (dsoundsink->pDS == NULL) {
263 GST_DEBUG_OBJECT (dsoundsink, "device not open, using template caps");
264 return NULL; /* base class will get template caps for us */
267 if (dsoundsink->cached_caps) {
268 caps = gst_caps_ref (dsoundsink->cached_caps);
270 element_class = GST_ELEMENT_GET_CLASS (dsoundsink);
271 pad_template = gst_element_class_get_pad_template (element_class, "sink");
272 g_return_val_if_fail (pad_template != NULL, NULL);
274 caps = gst_directsound_probe_supported_formats (dsoundsink,
275 gst_pad_template_get_caps (pad_template));
277 dsoundsink->cached_caps = gst_caps_ref (caps);
280 if (caps && filter) {
282 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
283 gst_caps_unref (caps);
288 gchar *caps_string = gst_caps_to_string (caps);
289 GST_DEBUG_OBJECT (dsoundsink, "returning caps %s", caps_string);
290 g_free (caps_string);
297 gst_directsound_sink_acceptcaps (GstBaseSink * sink, GstQuery * query)
299 GstDirectSoundSink *dsink = GST_DIRECTSOUND_SINK (sink);
304 gboolean ret = FALSE;
305 GstAudioRingBufferSpec spec = { 0 };
307 if (G_UNLIKELY (dsink == NULL))
312 gst_query_parse_accept_caps (query, &caps);
313 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
315 pad_caps = gst_pad_query_caps (pad, NULL);
317 gboolean cret = gst_caps_is_subset (caps, pad_caps);
318 gst_caps_unref (pad_caps);
320 GST_DEBUG_OBJECT (dsink,
321 "Caps are not a subset of the pad caps, not accepting caps");
326 /* If we've not got fixed caps, creating a stream might fail, so let's just
327 * return from here with default acceptcaps behaviour */
328 if (!gst_caps_is_fixed (caps)) {
329 GST_DEBUG_OBJECT (dsink, "Caps are not fixed, not accepting caps");
333 spec.latency_time = GST_SECOND;
334 if (!gst_audio_ring_buffer_parse_caps (&spec, caps)) {
335 GST_DEBUG_OBJECT (dsink, "Failed to parse caps, not accepting");
339 /* Make sure input is framed (one frame per buffer) and can be payloaded */
341 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
342 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
344 gboolean framed = FALSE, parsed = FALSE;
345 st = gst_caps_get_structure (caps, 0);
347 gst_structure_get_boolean (st, "framed", &framed);
348 gst_structure_get_boolean (st, "parsed", &parsed);
349 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0) {
350 GST_DEBUG_OBJECT (dsink, "Wrong AC3/DTS caps, not accepting");
358 GST_DEBUG_OBJECT (dsink, "Accepting caps");
361 gst_query_set_accept_caps_result (query, ret);
366 gst_directsound_sink_query (GstBaseSink * sink, GstQuery * query)
370 switch (GST_QUERY_TYPE (query)) {
371 case GST_QUERY_ACCEPT_CAPS:
372 res = gst_directsound_sink_acceptcaps (sink, query);
375 res = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
382 gst_directsound_sink_open (GstAudioSink * asink)
384 GstDirectSoundSink *dsoundsink;
387 dsoundsink = GST_DIRECTSOUND_SINK (asink);
389 /* create and initialize a DirecSound object */
390 if (FAILED (hRes = DirectSoundCreate (NULL, &dsoundsink->pDS, NULL))) {
391 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
392 ("gst_directsound_sink_open: DirectSoundCreate: %s",
393 DXGetErrorString9 (hRes)), (NULL));
397 if (FAILED (hRes = IDirectSound_SetCooperativeLevel (dsoundsink->pDS,
398 GetDesktopWindow (), DSSCL_PRIORITY))) {
399 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
400 ("gst_directsound_sink_open: IDirectSound_SetCooperativeLevel: %s",
401 DXGetErrorString9 (hRes)), (NULL));
409 gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec * spec)
411 return spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3 ||
412 spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS;
416 gst_directsound_sink_prepare (GstAudioSink * asink,
417 GstAudioRingBufferSpec * spec)
419 GstDirectSoundSink *dsoundsink;
421 DSBUFFERDESC descSecondary;
424 dsoundsink = GST_DIRECTSOUND_SINK (asink);
426 /*save number of bytes per sample and buffer format */
427 dsoundsink->bytes_per_sample = spec->info.bpf;
428 dsoundsink->type = spec->type;
430 /* fill the WAVEFORMATEX structure with spec params */
431 memset (&wfx, 0, sizeof (wfx));
432 if (!gst_directsound_sink_is_spdif_format (spec)) {
433 wfx.cbSize = sizeof (wfx);
434 wfx.wFormatTag = WAVE_FORMAT_PCM;
435 wfx.nChannels = spec->info.channels;
436 wfx.nSamplesPerSec = spec->info.rate;
437 wfx.wBitsPerSample = (spec->info.bpf * 8) / wfx.nChannels;
438 wfx.nBlockAlign = spec->info.bpf;
439 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
441 /* Create directsound buffer with size based on our configured
442 * buffer_size (which is 200 ms by default) */
443 dsoundsink->buffer_size =
444 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->buffer_time,
446 /* Make sure we make those numbers multiple of our sample size in bytes */
447 dsoundsink->buffer_size += dsoundsink->buffer_size % spec->info.bpf;
450 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->latency_time,
452 spec->segsize += spec->segsize % spec->info.bpf;
453 spec->segtotal = dsoundsink->buffer_size / spec->segsize;
455 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
457 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
459 wfx.nSamplesPerSec = 48000;
460 wfx.wBitsPerSample = 16;
461 wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
462 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
464 spec->segsize = 6144;
467 g_assert_not_reached ();
471 // Make the final buffer size be an integer number of segments
472 dsoundsink->buffer_size = spec->segsize * spec->segtotal;
474 GST_INFO_OBJECT (dsoundsink,
475 "GstAudioRingBufferSpec->channels: %d, GstAudioRingBufferSpec->rate: %d, GstAudioRingBufferSpec->bytes_per_sample: %d\n"
476 "WAVEFORMATEX.nSamplesPerSec: %ld, WAVEFORMATEX.wBitsPerSample: %d, WAVEFORMATEX.nBlockAlign: %d, WAVEFORMATEX.nAvgBytesPerSec: %ld\n"
477 "Size of dsound circular buffer=>%d\n", spec->info.channels,
478 spec->info.rate, spec->info.bpf, wfx.nSamplesPerSec, wfx.wBitsPerSample,
479 wfx.nBlockAlign, wfx.nAvgBytesPerSec, dsoundsink->buffer_size);
481 /* create a secondary directsound buffer */
482 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
483 descSecondary.dwSize = sizeof (DSBUFFERDESC);
484 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
485 if (!gst_directsound_sink_is_spdif_format (spec))
486 descSecondary.dwFlags |= DSBCAPS_CTRLVOLUME;
488 descSecondary.dwBufferBytes = dsoundsink->buffer_size;
489 descSecondary.lpwfxFormat = (WAVEFORMATEX *) & wfx;
491 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
492 &dsoundsink->pDSBSecondary, NULL);
494 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
495 ("gst_directsound_sink_prepare: IDirectSound_CreateSoundBuffer: %s",
496 DXGetErrorString9 (hRes)), (NULL));
500 gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
506 gst_directsound_sink_unprepare (GstAudioSink * asink)
508 GstDirectSoundSink *dsoundsink;
510 dsoundsink = GST_DIRECTSOUND_SINK (asink);
512 /* release secondary DirectSound buffer */
513 if (dsoundsink->pDSBSecondary) {
514 IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
515 dsoundsink->pDSBSecondary = NULL;
522 gst_directsound_sink_close (GstAudioSink * asink)
524 GstDirectSoundSink *dsoundsink = NULL;
526 dsoundsink = GST_DIRECTSOUND_SINK (asink);
528 /* release DirectSound object */
529 g_return_val_if_fail (dsoundsink->pDS != NULL, FALSE);
530 IDirectSound_Release (dsoundsink->pDS);
531 dsoundsink->pDS = NULL;
533 gst_caps_replace (&dsoundsink->cached_caps, NULL);
539 gst_directsound_sink_write (GstAudioSink * asink, gpointer data, guint length)
541 GstDirectSoundSink *dsoundsink;
544 LPVOID pLockedBuffer1 = NULL, pLockedBuffer2 = NULL;
545 DWORD dwSizeBuffer1, dwSizeBuffer2;
546 DWORD dwCurrentPlayCursor;
548 dsoundsink = GST_DIRECTSOUND_SINK (asink);
550 GST_DSOUND_LOCK (dsoundsink);
552 /* get current buffer status */
553 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
555 /* get current play cursor position */
556 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
557 &dwCurrentPlayCursor, NULL);
559 if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) {
560 DWORD dwFreeBufferSize;
563 /* calculate the free size of the circular buffer */
564 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
566 dsoundsink->buffer_size - (dsoundsink->current_circular_offset -
567 dwCurrentPlayCursor);
570 dwCurrentPlayCursor - dsoundsink->current_circular_offset;
572 if (length >= dwFreeBufferSize) {
574 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
575 &dwCurrentPlayCursor, NULL);
578 IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
579 if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING))
580 goto calculate_freesize;
582 dsoundsink->first_buffer_after_reset = FALSE;
583 GST_DSOUND_UNLOCK (dsoundsink);
589 if (dwStatus & DSBSTATUS_BUFFERLOST) {
590 hRes = IDirectSoundBuffer_Restore (dsoundsink->pDSBSecondary); /*need a loop waiting the buffer is restored?? */
592 dsoundsink->current_circular_offset = 0;
595 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
596 dsoundsink->current_circular_offset, length, &pLockedBuffer1,
597 &dwSizeBuffer1, &pLockedBuffer2, &dwSizeBuffer2, 0L);
599 if (SUCCEEDED (hRes)) {
600 // Write to pointers without reordering.
601 memcpy (pLockedBuffer1, data, dwSizeBuffer1);
602 if (pLockedBuffer2 != NULL)
603 memcpy (pLockedBuffer2, (LPBYTE) data + dwSizeBuffer1, dwSizeBuffer2);
605 // Update where the buffer will lock (for next time)
606 dsoundsink->current_circular_offset += dwSizeBuffer1 + dwSizeBuffer2;
607 dsoundsink->current_circular_offset %= dsoundsink->buffer_size; /* Circular buffer */
609 hRes = IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer1,
610 dwSizeBuffer1, pLockedBuffer2, dwSizeBuffer2);
613 /* if the buffer was not in playing state yet, call play on the buffer
614 except if this buffer is the fist after a reset (base class call reset and write a buffer when setting the sink to pause) */
615 if (!(dwStatus & DSBSTATUS_PLAYING) &&
616 dsoundsink->first_buffer_after_reset == FALSE) {
617 hRes = IDirectSoundBuffer_Play (dsoundsink->pDSBSecondary, 0, 0,
621 dsoundsink->first_buffer_after_reset = FALSE;
623 GST_DSOUND_UNLOCK (dsoundsink);
629 gst_directsound_sink_delay (GstAudioSink * asink)
631 GstDirectSoundSink *dsoundsink;
633 DWORD dwCurrentPlayCursor;
634 DWORD dwBytesInQueue = 0;
635 gint nNbSamplesInQueue = 0;
638 dsoundsink = GST_DIRECTSOUND_SINK (asink);
640 /* get current buffer status */
641 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
643 if (dwStatus & DSBSTATUS_PLAYING) {
644 /*evaluate the number of samples in queue in the circular buffer */
645 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
646 &dwCurrentPlayCursor, NULL);
649 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
651 dsoundsink->current_circular_offset - dwCurrentPlayCursor;
654 dsoundsink->current_circular_offset + (dsoundsink->buffer_size -
655 dwCurrentPlayCursor);
657 nNbSamplesInQueue = dwBytesInQueue / dsoundsink->bytes_per_sample;
661 return nNbSamplesInQueue;
665 gst_directsound_sink_reset (GstAudioSink * asink)
667 GstDirectSoundSink *dsoundsink;
668 LPVOID pLockedBuffer = NULL;
669 DWORD dwSizeBuffer = 0;
671 dsoundsink = GST_DIRECTSOUND_SINK (asink);
673 GST_DSOUND_LOCK (dsoundsink);
675 if (dsoundsink->pDSBSecondary) {
677 HRESULT hRes = IDirectSoundBuffer_Stop (dsoundsink->pDSBSecondary);
680 hRes = IDirectSoundBuffer_SetCurrentPosition (dsoundsink->pDSBSecondary, 0);
681 dsoundsink->current_circular_offset = 0;
683 /*reset the buffer */
684 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
685 dsoundsink->current_circular_offset, dsoundsink->buffer_size,
686 &pLockedBuffer, &dwSizeBuffer, NULL, NULL, 0L);
688 if (SUCCEEDED (hRes)) {
689 memset (pLockedBuffer, 0, dwSizeBuffer);
692 IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer,
693 dwSizeBuffer, NULL, 0);
697 dsoundsink->first_buffer_after_reset = TRUE;
699 GST_DSOUND_UNLOCK (dsoundsink);
703 * gst_directsound_probe_supported_formats:
705 * Takes the template caps and returns the subset which is actually
706 * supported by this device.
711 gst_directsound_probe_supported_formats (GstDirectSoundSink * dsoundsink,
712 const GstCaps * template_caps)
715 DSBUFFERDESC descSecondary;
719 LPDIRECTSOUNDBUFFER tmpBuffer;
721 caps = gst_caps_copy (template_caps);
724 * Check availability of digital output by trying to create an SPDIF buffer
727 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
728 /* fill the WAVEFORMATEX structure with some standard AC3 over SPDIF params */
729 memset (&wfx, 0, sizeof (wfx));
731 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
733 wfx.nSamplesPerSec = 48000;
734 wfx.wBitsPerSample = 16;
736 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
738 // create a secondary directsound buffer
739 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
740 descSecondary.dwSize = sizeof (DSBUFFERDESC);
741 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
742 descSecondary.dwBufferBytes = 6144;
743 descSecondary.lpwfxFormat = &wfx;
745 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
748 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough not supported "
749 "(IDirectSound_CreateSoundBuffer returned: %s)\n",
750 DXGetErrorString9 (hRes));
751 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
752 tmp2 = gst_caps_subtract (caps, tmp);
753 gst_caps_unref (tmp);
754 gst_caps_unref (caps);
756 tmp = gst_caps_new_empty_simple ("audio/x-dts");
757 tmp2 = gst_caps_subtract (caps, tmp);
758 gst_caps_unref (tmp);
759 gst_caps_unref (caps);
762 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
763 hRes = IDirectSoundBuffer_Release (tmpBuffer);
765 GST_DEBUG_OBJECT (dsoundsink,
766 "(IDirectSoundBuffer_Release returned: %s)\n",
767 DXGetErrorString9 (hRes));
771 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
772 tmp2 = gst_caps_subtract (caps, tmp);
773 gst_caps_unref (tmp);
774 gst_caps_unref (caps);
776 tmp = gst_caps_new_empty_simple ("audio/x-dts");
777 tmp2 = gst_caps_subtract (caps, tmp);
778 gst_caps_unref (tmp);
779 gst_caps_unref (caps);
787 gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
789 if (gst_directsound_sink_is_spdif_format (&sink->ringbuffer->spec)) {
790 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
792 GstMapInfo infobuf, infoout;
798 out = gst_buffer_new_and_alloc (framesize);
800 if (!gst_buffer_map (buf, &infobuf, GST_MAP_READWRITE)) {
801 gst_buffer_unref (out);
804 if (!gst_buffer_map (out, &infoout, GST_MAP_READWRITE)) {
805 gst_buffer_unmap (buf, &infobuf);
806 gst_buffer_unref (out);
809 success = gst_audio_iec61937_payload (infobuf.data, infobuf.size,
810 infoout.data, infoout.size, &sink->ringbuffer->spec, G_BYTE_ORDER);
812 gst_buffer_unmap (out, &infoout);
813 gst_buffer_unmap (buf, &infobuf);
814 gst_buffer_unref (out);
818 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_ALL, 0, -1);
820 _swab ((gchar *) infoout.data, (gchar *) infoout.data, infobuf.size);
821 gst_buffer_unmap (out, &infoout);
822 gst_buffer_unmap (buf, &infobuf);
825 return gst_buffer_ref (buf);
829 gst_directsound_sink_set_volume (GstDirectSoundSink * dsoundsink,
830 gdouble dvolume, gboolean store)
834 volume = dvolume * 100;
836 dsoundsink->volume = volume;
838 if (dsoundsink->pDSBSecondary) {
839 /* DirectSound controls volume using units of 100th of a decibel,
840 * ranging from -10000 to 0. We use a linear scale of 0 - 100
844 if (dsoundsink->volume == 0)
847 dsVolume = 100 * (long) (20 * log10 ((double) dsoundsink->volume / 100.));
848 dsVolume = CLAMP (dsVolume, -10000, 0);
850 GST_DEBUG_OBJECT (dsoundsink,
851 "Setting volume on secondary buffer to %d from %d", (int) dsVolume,
852 (int) dsoundsink->volume);
853 IDirectSoundBuffer_SetVolume (dsoundsink->pDSBSecondary, dsVolume);
858 gst_directsound_sink_get_volume (GstDirectSoundSink * dsoundsink)
860 return (gdouble) dsoundsink->volume / 100;
864 gst_directsound_sink_set_mute (GstDirectSoundSink * dsoundsink, gboolean mute)
867 gst_directsound_sink_set_volume (dsoundsink, 0, FALSE);
869 gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
873 gst_directsound_sink_get_mute (GstDirectSoundSink * dsoundsink)