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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, 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 -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 -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 <gst/audio/streamvolume.h>
57 #include "gstdirectsoundsink.h"
58 #include <gst/audio/gstaudioiec61937.h>
69 #define DEFAULT_MUTE FALSE
71 GST_DEBUG_CATEGORY_STATIC (directsoundsink_debug);
72 #define GST_CAT_DEFAULT directsoundsink_debug
74 static void gst_directsound_sink_finalize (GObject * object);
76 static void gst_directsound_sink_set_property (GObject * object, guint prop_id,
77 const GValue * value, GParamSpec * pspec);
78 static void gst_directsound_sink_get_property (GObject * object, guint prop_id,
79 GValue * value, GParamSpec * pspec);
81 static GstCaps *gst_directsound_sink_getcaps (GstBaseSink * bsink,
83 static GstBuffer *gst_directsound_sink_payload (GstAudioBaseSink * sink,
85 static gboolean gst_directsound_sink_prepare (GstAudioSink * asink,
86 GstAudioRingBufferSpec * spec);
87 static gboolean gst_directsound_sink_unprepare (GstAudioSink * asink);
88 static gboolean gst_directsound_sink_open (GstAudioSink * asink);
89 static gboolean gst_directsound_sink_close (GstAudioSink * asink);
90 static gint gst_directsound_sink_write (GstAudioSink * asink,
91 gpointer data, guint length);
92 static guint gst_directsound_sink_delay (GstAudioSink * asink);
93 static void gst_directsound_sink_reset (GstAudioSink * asink);
94 static GstCaps *gst_directsound_probe_supported_formats (GstDirectSoundSink *
95 dsoundsink, const GstCaps * template_caps);
96 static gboolean gst_directsound_sink_query (GstBaseSink * pad, GstQuery * query);
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) S8, "
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 G_OBJECT_CLASS (parent_class)->finalize (object);
142 gst_directsound_sink_class_init (GstDirectSoundSinkClass * klass)
144 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
145 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
146 GstAudioSinkClass *gstaudiosink_class = GST_AUDIO_SINK_CLASS (klass);
147 GstAudioBaseSinkClass *gstaudiobasesink_class = GST_AUDIO_BASE_SINK_CLASS (klass);
148 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
150 GST_DEBUG_CATEGORY_INIT (directsoundsink_debug, "directsoundsink", 0,
153 parent_class = g_type_class_peek_parent (klass);
155 gobject_class->finalize = gst_directsound_sink_finalize;
156 gobject_class->set_property = gst_directsound_sink_set_property;
157 gobject_class->get_property = gst_directsound_sink_get_property;
159 gstbasesink_class->get_caps =
160 GST_DEBUG_FUNCPTR (gst_directsound_sink_getcaps);
162 gstbasesink_class->query =
163 GST_DEBUG_FUNCPTR (gst_directsound_sink_query);
165 gstaudiobasesink_class->payload =
166 GST_DEBUG_FUNCPTR (gst_directsound_sink_payload);
168 gstaudiosink_class->prepare =
169 GST_DEBUG_FUNCPTR (gst_directsound_sink_prepare);
170 gstaudiosink_class->unprepare =
171 GST_DEBUG_FUNCPTR (gst_directsound_sink_unprepare);
172 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_directsound_sink_open);
173 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_directsound_sink_close);
174 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_directsound_sink_write);
175 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_directsound_sink_delay);
176 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_directsound_sink_reset);
178 g_object_class_install_property (gobject_class,
180 g_param_spec_double ("volume", "Volume",
181 "Volume of this stream", 0.0, 1.0, 1.0,
182 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184 g_object_class_install_property (gobject_class,
186 g_param_spec_boolean ("mute", "Mute",
187 "Mute state of this stream", DEFAULT_MUTE,
188 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
190 gst_element_class_set_static_metadata (element_class,
191 "Direct Sound Audio Sink", "Sink/Audio",
192 "Output to a sound card via Direct Sound",
193 "Sebastien Moutte <sebastien@moutte.net>");
195 gst_element_class_add_pad_template (element_class,
196 gst_static_pad_template_get (&directsoundsink_sink_factory));
200 gst_directsound_sink_init (GstDirectSoundSink * dsoundsink)
202 dsoundsink->volume = 100;
203 dsoundsink->mute = FALSE;
204 dsoundsink->pDS = NULL;
205 dsoundsink->cached_caps = NULL;
206 dsoundsink->pDSBSecondary = NULL;
207 dsoundsink->current_circular_offset = 0;
208 dsoundsink->buffer_size = DSBSIZE_MIN;
209 dsoundsink->volume = 100;
210 dsoundsink->dsound_lock = g_mutex_new ();
211 dsoundsink->first_buffer_after_reset = FALSE;
215 gst_directsound_sink_set_property (GObject * object,
216 guint prop_id, const GValue * value, GParamSpec * pspec)
218 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
222 gst_directsound_sink_set_volume (sink, g_value_get_double (value), TRUE);
225 gst_directsound_sink_set_mute (sink, g_value_get_boolean (value));
228 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 gst_directsound_sink_get_property (GObject * object,
235 guint prop_id, GValue * value, GParamSpec * pspec)
237 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
241 g_value_set_double (value, gst_directsound_sink_get_volume (sink));
244 g_value_set_boolean (value, gst_directsound_sink_get_mute (sink));
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
253 gst_directsound_sink_getcaps (GstBaseSink * bsink, GstCaps * filter)
255 GstElementClass *element_class;
256 GstPadTemplate *pad_template;
257 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (bsink);
259 gchar *caps_string = NULL;
261 if (dsoundsink->pDS == NULL) {
262 GST_DEBUG_OBJECT (dsoundsink, "device not open, using template caps");
263 return NULL; /* base class will get template caps for us */
266 if (dsoundsink->cached_caps) {
267 caps_string = gst_caps_to_string (dsoundsink->cached_caps);
268 GST_DEBUG_OBJECT (dsoundsink, "Returning cached caps: %s", caps_string);
269 g_free (caps_string);
270 return gst_caps_ref (dsoundsink->cached_caps);
273 element_class = GST_ELEMENT_GET_CLASS (dsoundsink);
274 pad_template = gst_element_class_get_pad_template (element_class, "sink");
275 g_return_val_if_fail (pad_template != NULL, NULL);
277 caps = gst_directsound_probe_supported_formats (dsoundsink,
278 gst_pad_template_get_caps (pad_template));
280 dsoundsink->cached_caps = gst_caps_ref (caps);
284 gchar *caps_string = gst_caps_to_string (caps);
285 GST_DEBUG_OBJECT (dsoundsink, "returning caps %s", caps_string);
286 g_free (caps_string);
293 gst_directsound_sink_acceptcaps (GstBaseSink * sink, GstQuery * query)
295 GstDirectSoundSink *dsink = GST_DIRECTSOUND_SINK (sink);
300 gboolean ret = FALSE;
301 GstAudioRingBufferSpec spec = { 0 };
303 if (G_UNLIKELY (dsink == NULL))
308 gst_query_parse_accept_caps (query, &caps);
309 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
311 pad_caps = gst_pad_query_caps (pad, NULL);
313 gboolean cret = gst_caps_can_intersect (pad_caps, caps);
314 gst_caps_unref (pad_caps);
317 GST_DEBUG_OBJECT (dsink, "Can't intersect caps, not accepting caps");
322 /* If we've not got fixed caps, creating a stream might fail, so let's just
323 * return from here with default acceptcaps behaviour */
324 if (!gst_caps_is_fixed (caps))
326 GST_DEBUG_OBJECT (dsink, "Caps are not fixed, not accepting caps");
330 spec.latency_time = GST_SECOND;
331 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
333 GST_DEBUG_OBJECT (dsink, "Failed to parse caps, not accepting");
337 /* Make sure input is framed (one frame per buffer) and can be payloaded */
340 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
341 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
343 gboolean framed = FALSE, parsed = FALSE;
344 st = gst_caps_get_structure (caps, 0);
346 gst_structure_get_boolean (st, "framed", &framed);
347 gst_structure_get_boolean (st, "parsed", &parsed);
348 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;
720 caps = gst_caps_copy (template_caps);
723 * Check availability of digital output by trying to create an SPDIF buffer
726 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
727 /* fill the WAVEFORMATEX structure with some standard AC3 over SPDIF params */
728 memset (&wfx, 0, sizeof (wfx));
730 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
732 wfx.nSamplesPerSec = 48000;
733 wfx.wBitsPerSample = 16;
735 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
737 // create a secondary directsound buffer
738 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
739 descSecondary.dwSize = sizeof (DSBUFFERDESC);
740 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
741 descSecondary.dwBufferBytes = 6144;
742 descSecondary.lpwfxFormat = &wfx;
744 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
745 &dsoundsink->pDSBSecondary, NULL);
747 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough not supported "
748 "(IDirectSound_CreateSoundBuffer returned: %s)\n",
749 DXGetErrorString9 (hRes));
750 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
751 tmp2 = gst_caps_subtract (caps, tmp);
752 gst_caps_unref (tmp);
753 gst_caps_unref (caps);
755 tmp = gst_caps_new_empty_simple ("audio/x-dts");
756 tmp2 = gst_caps_subtract (caps, tmp);
757 gst_caps_unref (tmp);
758 gst_caps_unref (caps);
761 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
762 hRes = IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
764 GST_DEBUG_OBJECT (dsoundsink,
765 "(IDirectSoundBuffer_Release returned: %s)\n",
766 DXGetErrorString9 (hRes));
770 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
771 tmp2 = gst_caps_subtract (caps, tmp);
772 gst_caps_unref (tmp);
773 gst_caps_unref (caps);
775 tmp = gst_caps_new_empty_simple ("audio/x-dts");
776 tmp2 = gst_caps_subtract (caps, tmp);
777 gst_caps_unref (tmp);
778 gst_caps_unref (caps);
786 gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
788 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))
802 gst_buffer_unref (out);
805 if (!gst_buffer_map (out, &infoout, GST_MAP_READWRITE))
807 gst_buffer_unmap (buf, &infobuf);
808 gst_buffer_unref (out);
811 success = gst_audio_iec61937_payload (infobuf.data, infobuf.size,
812 infoout.data, infoout.size, &sink->ringbuffer->spec);
814 gst_buffer_unmap (out, &infoout);
815 gst_buffer_unmap (buf, &infobuf);
816 gst_buffer_unref (out);
820 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_ALL, 0, -1);
822 _swab ((gchar *) infoout.data, (gchar *) infoout.data, infobuf.size);
823 gst_buffer_unmap (out, &infoout);
824 gst_buffer_unmap (buf, &infobuf);
828 return gst_buffer_ref (buf);
832 gst_directsound_sink_set_volume (GstDirectSoundSink * dsoundsink,
833 gdouble dvolume, gboolean store)
837 volume = dvolume * 100;
839 dsoundsink->volume = volume;
841 if (dsoundsink->pDSBSecondary) {
842 /* DirectSound controls volume using units of 100th of a decibel,
843 * ranging from -10000 to 0. We use a linear scale of 0 - 100
847 if (dsoundsink->volume == 0)
850 dsVolume = 100 * (long) (20 * log10 ((double) dsoundsink->volume / 100.));
851 dsVolume = CLAMP (dsVolume, -10000, 0);
853 GST_DEBUG_OBJECT (dsoundsink,
854 "Setting volume on secondary buffer to %d from %d", (int) dsVolume,
855 (int) dsoundsink->volume);
856 IDirectSoundBuffer_SetVolume (dsoundsink->pDSBSecondary, dsVolume);
861 gst_directsound_sink_get_volume (GstDirectSoundSink * dsoundsink)
863 return (gdouble) dsoundsink->volume / 100;
867 gst_directsound_sink_set_mute (GstDirectSoundSink * dsoundsink, gboolean mute)
870 gst_directsound_sink_set_volume (dsoundsink, 0, FALSE);
872 gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
876 gst_directsound_sink_get_mute (GstDirectSoundSink * dsoundsink)