Debian Codegraph @

gst/directsound/gstdirectsoundsink.c

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) 2005 Sebastien Moutte <[email protected]>
3 * Copyright (C) 2007 Pioneers of the Inevitable <[email protected]>
4 * Copyright (C) 2010 Fluendo S.A. <[email protected]>
5 * Copyright (C) 2014 Mario Stephan <[email protected]>
6 *
7 * gstdirectsoundsink.c:
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 *
24 *
25 * The development of this code was made possible due to the involvement
26 * of Pioneers of the Inevitable, the creators of the Songbird Music player
27 *
28 * Patched version of GStreamer sink plugin for Windows to enable selection of audio output device
29 */
30
31 /**
32 * SECTION:element-directsoundsink
33 *
34 * This element lets you output sound using the DirectSound API.
35 *
36 * Note that you should almost always use generic audio conversion elements
37 * like audioconvert and audioresample in front of an audiosink to make sure
38 * your pipeline works under all circumstances (those conversion elements will
39 * act in passthrough-mode if no conversion is necessary).
40 *
41 * <refsect2>
42 * <title>Example pipelines</title>
43 * |[
44 * gst-launch-1.0 -v audiotestsrc ! audioconvert ! volume volume=0.1 ! directsoundsink
45 * ]| will output a sine wave (continuous beep sound) to your sound card (with
46 * a very low volume as precaution).
47 * |[
48 * gst-launch-1.0 -v filesrc location=music.ogg ! decodebin ! audioconvert ! audioresample ! directsoundsink
49 * ]| will play an Ogg/Vorbis audio file and output it.
50 * </refsect2>
51 */
52
53 #ifdef HAVE_CONFIG_H
54 #include "config.h"
55 #endif
56
57 #include <gst/base/gstbasesink.h>
58 #include "gstdirectsoundsink.h"
59 #include <gst/audio/gstaudioiec61937.h>
60
61 #include <math.h>
62
63 #ifdef __CYGWIN__
64 #include <unistd.h>
65 #ifndef _swab
66 #define _swab swab
67 #endif
68 #endif
69
70 #define DEFAULT_MUTE FALSE
71
72 GST_DEBUG_CATEGORY_STATIC (directsoundsink_debug);
73 #define GST_CAT_DEFAULT directsoundsink_debug
74
75 static void gst_directsound_sink_finalize (GObject * object);
76
77 static void gst_directsound_sink_set_property (GObject * object, guint prop_id,
78 const GValue * value, GParamSpec * pspec);
79 static void gst_directsound_sink_get_property (GObject * object, guint prop_id,
80 GValue * value, GParamSpec * pspec);
81
82 static GstCaps *gst_directsound_sink_getcaps (GstBaseSink * bsink,
83 GstCaps * filter);
84 static GstBuffer *gst_directsound_sink_payload (GstAudioBaseSink * sink,
85 GstBuffer * buf);
86 static gboolean gst_directsound_sink_prepare (GstAudioSink * asink,
87 GstAudioRingBufferSpec * spec);
88 static gboolean gst_directsound_sink_unprepare (GstAudioSink * asink);
89 static gboolean gst_directsound_sink_open (GstAudioSink * asink);
90 static gboolean gst_directsound_sink_close (GstAudioSink * asink);
91 static gint gst_directsound_sink_write (GstAudioSink * asink,
92 gpointer data, guint length);
93 static guint gst_directsound_sink_delay (GstAudioSink * asink);
94 static void gst_directsound_sink_reset (GstAudioSink * asink);
95 static GstCaps *gst_directsound_probe_supported_formats (GstDirectSoundSink *
96 dsoundsink, const GstCaps * template_caps);
97 static gboolean gst_directsound_sink_query (GstBaseSink * pad,
98 GstQuery * query);
99
100 static void gst_directsound_sink_set_volume (GstDirectSoundSink * sink,
101 gdouble volume, gboolean store);
102 static gdouble gst_directsound_sink_get_volume (GstDirectSoundSink * sink);
103 static void gst_directsound_sink_set_mute (GstDirectSoundSink * sink,
104 gboolean mute);
105 static gboolean gst_directsound_sink_get_mute (GstDirectSoundSink * sink);
106
107 static gboolean gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec *
108 spec);
109
110 static GstStaticPadTemplate directsoundsink_sink_factory =
111 GST_STATIC_PAD_TEMPLATE ("sink",
112 GST_PAD_SINK,
113 GST_PAD_ALWAYS,
114 GST_STATIC_CAPS ("audio/x-raw, "
115 "format = (string) S16LE, "
116 "layout = (string) interleaved, "
117 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
118 "audio/x-raw, "
119 "format = (string) U8, "
120 "layout = (string) interleaved, "
121 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ];"
122 "audio/x-ac3, framed = (boolean) true;"
123 "audio/x-dts, framed = (boolean) true;"));
124
125 enum
126 {
127 PROP_0,
128 PROP_VOLUME,
129 PROP_MUTE,
130 PROP_DEVICE,
131 PROP_DEVICE_NAME
132 };
133
134 #define gst_directsound_sink_parent_class parent_class
135 G_DEFINE_TYPE_WITH_CODE (GstDirectSoundSink, gst_directsound_sink,
136 GST_TYPE_AUDIO_SINK, G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
137 );
138
139 static gchar *
140 guid_to_string (LPGUID in)
141 {
142 WCHAR buffer[256];
143 if (StringFromGUID2 (in, buffer, sizeof buffer / sizeof buffer[0]) == 0)
144 return NULL;
145 return g_utf16_to_utf8 ((const gunichar2 *) buffer, -1, NULL, NULL, NULL);
146 }
147
148 static LPGUID
149 string_to_guid (const gchar * str)
150 {
151 HRESULT ret;
152 gunichar2 * wstr;
153 LPGUID out;
154
155 wstr = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL);
156 if (!wstr)
157 return NULL;
158
159 out = g_malloc (sizeof (GUID));
160 ret = CLSIDFromString ((LPOLESTR) wstr, out);
161 g_free (wstr);
162 if (ret != NOERROR) {
163 g_free (out);
164 return NULL;
165 }
166
167 return out;
168 }
169
170
171 static void
172 gst_directsound_sink_finalize (GObject * object)
173 {
174 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (object);
175
176 g_mutex_clear (&dsoundsink->dsound_lock);
177
178 G_OBJECT_CLASS (parent_class)->finalize (object);
179 }
180
181 static void
182 gst_directsound_sink_class_init (GstDirectSoundSinkClass * klass)
183 {
184 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
185 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
186 GstAudioSinkClass *gstaudiosink_class = GST_AUDIO_SINK_CLASS (klass);
187 GstAudioBaseSinkClass *gstaudiobasesink_class =
188 GST_AUDIO_BASE_SINK_CLASS (klass);
189 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
190
191 GST_DEBUG_CATEGORY_INIT (directsoundsink_debug, "directsoundsink", 0,
192 "DirectSound sink");
193
194 gobject_class->finalize = gst_directsound_sink_finalize;
195 gobject_class->set_property = gst_directsound_sink_set_property;
196 gobject_class->get_property = gst_directsound_sink_get_property;
197
198 gstbasesink_class->get_caps =
199 GST_DEBUG_FUNCPTR (gst_directsound_sink_getcaps);
200
201 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_directsound_sink_query);
202
203 gstaudiobasesink_class->payload =
204 GST_DEBUG_FUNCPTR (gst_directsound_sink_payload);
205
206 gstaudiosink_class->prepare =
207 GST_DEBUG_FUNCPTR (gst_directsound_sink_prepare);
208 gstaudiosink_class->unprepare =
209 GST_DEBUG_FUNCPTR (gst_directsound_sink_unprepare);
210 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_directsound_sink_open);
211 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_directsound_sink_close);
212 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_directsound_sink_write);
213 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_directsound_sink_delay);
214 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_directsound_sink_reset);
215
216 g_object_class_install_property (gobject_class,
217 PROP_VOLUME,
218 g_param_spec_double ("volume", "Volume",
219 "Volume of this stream", 0.0, 1.0, 1.0,
220 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221
222 g_object_class_install_property (gobject_class,
223 PROP_MUTE,
224 g_param_spec_boolean ("mute", "Mute",
225 "Mute state of this stream", DEFAULT_MUTE,
226 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227
228 g_object_class_install_property (gobject_class, PROP_DEVICE,
229 g_param_spec_string ("device", "Device",
230 "DirectSound playback device as a GUID string",
231 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232
233 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
234 g_param_spec_string ("device-name", "Device name",
235 "Human-readable name of the audio device",
236 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
237
238
239 gst_element_class_set_static_metadata (element_class,
240 "Direct Sound Audio Sink", "Sink/Audio",
241 "Output to a sound card via Direct Sound",
242 "Sebastien Moutte <[email protected]>");
243
244 gst_element_class_add_pad_template (element_class,
245 gst_static_pad_template_get (&directsoundsink_sink_factory));
246 }
247
248 static void
249 gst_directsound_sink_init (GstDirectSoundSink * dsoundsink)
250 {
251 dsoundsink->volume = 100;
252 dsoundsink->mute = FALSE;
253 dsoundsink->pDS = NULL;
254 dsoundsink->cached_caps = NULL;
255 dsoundsink->pDSBSecondary = NULL;
256 dsoundsink->current_circular_offset = 0;
257 dsoundsink->buffer_size = DSBSIZE_MIN;
258 dsoundsink->volume = 100;
259 g_mutex_init (&dsoundsink->dsound_lock);
260 dsoundsink->first_buffer_after_reset = FALSE;
261 dsoundsink->device_id = NULL;
262 dsoundsink->device_name = NULL;
263 }
264
265 static void
266 gst_directsound_sink_set_property (GObject * object,
267 guint prop_id, const GValue * value, GParamSpec * pspec)
268 {
269 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
270
271 switch (prop_id) {
272 case PROP_VOLUME:
273 gst_directsound_sink_set_volume (sink, g_value_get_double (value), TRUE);
274 break;
275 case PROP_MUTE:
276 gst_directsound_sink_set_mute (sink, g_value_get_boolean (value));
277 break;
278 case PROP_DEVICE:
279 if (sink->device_id) {
280 g_free (sink->device_id);
281 sink->device_id = NULL;
282 }
283 if (sink->device_name) {
284 g_free (sink->device_name);
285 sink->device_name = NULL;
286 }
287 sink->device_id = g_strdup (g_value_get_string (value));
288 break;
289 default:
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 break;
292 }
293 }
294
295 static void
296 gst_directsound_sink_get_property (GObject * object,
297 guint prop_id, GValue * value, GParamSpec * pspec)
298 {
299 GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
300
301 switch (prop_id) {
302 case PROP_VOLUME:
303 g_value_set_double (value, gst_directsound_sink_get_volume (sink));
304 break;
305 case PROP_MUTE:
306 g_value_set_boolean (value, gst_directsound_sink_get_mute (sink));
307 break;
308 default:
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310 break;
311 }
312 }
313
314 static GstCaps *
315 gst_directsound_sink_getcaps (GstBaseSink * bsink, GstCaps * filter)
316 {
317 GstElementClass *element_class;
318 GstPadTemplate *pad_template;
319 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (bsink);
320 GstCaps *caps;
321
322 if (dsoundsink->pDS == NULL) {
323 GST_DEBUG_OBJECT (dsoundsink, "device not open, using template caps");
324 return NULL; /* base class will get template caps for us */
325 }
326
327 if (dsoundsink->cached_caps) {
328 caps = gst_caps_ref (dsoundsink->cached_caps);
329 } else {
330 element_class = GST_ELEMENT_GET_CLASS (dsoundsink);
331 pad_template = gst_element_class_get_pad_template (element_class, "sink");
332 g_return_val_if_fail (pad_template != NULL, NULL);
333
334 caps = gst_directsound_probe_supported_formats (dsoundsink,
335 gst_pad_template_get_caps (pad_template));
336 if (caps)
337 dsoundsink->cached_caps = gst_caps_ref (caps);
338 }
339
340 if (caps && filter) {
341 GstCaps *tmp =
342 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
343 gst_caps_unref (caps);
344 caps = tmp;
345 }
346
347 if (caps) {
348 gchar *caps_string = gst_caps_to_string (caps);
349 GST_DEBUG_OBJECT (dsoundsink, "returning caps %s", caps_string);
350 g_free (caps_string);
351 }
352
353 return caps;
354 }
355
356 static gboolean
357 gst_directsound_sink_acceptcaps (GstBaseSink * sink, GstQuery * query)
358 {
359 GstDirectSoundSink *dsink = GST_DIRECTSOUND_SINK (sink);
360 GstPad *pad;
361 GstCaps *caps;
362 GstCaps *pad_caps;
363 GstStructure *st;
364 gboolean ret = FALSE;
365 GstAudioRingBufferSpec spec = { 0 };
366
367 if (G_UNLIKELY (dsink == NULL))
368 return FALSE;
369
370 pad = sink->sinkpad;
371
372 gst_query_parse_accept_caps (query, &caps);
373 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
374
375 pad_caps = gst_pad_query_caps (pad, NULL);
376 if (pad_caps) {
377 gboolean cret = gst_caps_is_subset (caps, pad_caps);
378 gst_caps_unref (pad_caps);
379 if (!cret) {
380 GST_DEBUG_OBJECT (dsink,
381 "Caps are not a subset of the pad caps, not accepting caps");
382 goto done;
383 }
384 }
385
386 /* If we've not got fixed caps, creating a stream might fail, so let's just
387 * return from here with default acceptcaps behaviour */
388 if (!gst_caps_is_fixed (caps)) {
389 GST_DEBUG_OBJECT (dsink, "Caps are not fixed, not accepting caps");
390 goto done;
391 }
392
393 spec.latency_time = GST_SECOND;
394 if (!gst_audio_ring_buffer_parse_caps (&spec, caps)) {
395 GST_DEBUG_OBJECT (dsink, "Failed to parse caps, not accepting");
396 goto done;
397 }
398
399 /* Make sure input is framed (one frame per buffer) and can be payloaded */
400 switch (spec.type) {
401 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
402 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
403 {
404 gboolean framed = FALSE, parsed = FALSE;
405 st = gst_caps_get_structure (caps, 0);
406
407 gst_structure_get_boolean (st, "framed", &framed);
408 gst_structure_get_boolean (st, "parsed", &parsed);
409 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0) {
410 GST_DEBUG_OBJECT (dsink, "Wrong AC3/DTS caps, not accepting");
411 goto done;
412 }
413 }
414 default:
415 break;
416 }
417 ret = TRUE;
418 GST_DEBUG_OBJECT (dsink, "Accepting caps");
419
420 done:
421 gst_query_set_accept_caps_result (query, ret);
422 return TRUE;
423 }
424
425 static gboolean
426 gst_directsound_sink_query (GstBaseSink * sink, GstQuery * query)
427 {
428 gboolean res = TRUE;
429
430 switch (GST_QUERY_TYPE (query)) {
431 case GST_QUERY_ACCEPT_CAPS:
432 res = gst_directsound_sink_acceptcaps (sink, query);
433 break;
434 default:
435 res = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
436 }
437
438 return res;
439 }
440
441 static gboolean
442 gst_directsound_sink_open (GstAudioSink * asink)
443 {
444 GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (asink);
445 HRESULT hRes;
446 LPGUID id=NULL;
447
448 if (dsoundsink->device_id)
449 id = string_to_guid( dsoundsink->device_id);
450
451 /* create and initialize a DirecSound object */
452 if (FAILED (hRes = DirectSoundCreate (id, &dsoundsink->pDS, NULL))) {
453 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
454 ("gst_directsound_sink_open: DirectSoundCreate: %s",
455 DXGetErrorString9 (hRes)), (NULL));
456 return FALSE;
457 }
458
459 if (FAILED (hRes = IDirectSound_SetCooperativeLevel (dsoundsink->pDS,
460 GetDesktopWindow (), DSSCL_PRIORITY))) {
461 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
462 ("gst_directsound_sink_open: IDirectSound_SetCooperativeLevel: %s",
463 DXGetErrorString9 (hRes)), (NULL));
464 return FALSE;
465 }
466
467 return TRUE;
468 }
469
470 static gboolean
471 gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec * spec)
472 {
473 return spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3 ||
474 spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS;
475 }
476
477 static gboolean
478 gst_directsound_sink_prepare (GstAudioSink * asink,
479 GstAudioRingBufferSpec * spec)
480 {
481 GstDirectSoundSink *dsoundsink;
482 HRESULT hRes;
483 DSBUFFERDESC descSecondary;
484 WAVEFORMATEX wfx;
485
486 dsoundsink = GST_DIRECTSOUND_SINK (asink);
487
488 /*save number of bytes per sample and buffer format */
489 dsoundsink->bytes_per_sample = spec->info.bpf;
490 dsoundsink->type = spec->type;
491
492 /* fill the WAVEFORMATEX structure with spec params */
493 memset (&wfx, 0, sizeof (wfx));
494 if (!gst_directsound_sink_is_spdif_format (spec)) {
495 wfx.cbSize = sizeof (wfx);
496 wfx.wFormatTag = WAVE_FORMAT_PCM;
497 wfx.nChannels = spec->info.channels;
498 wfx.nSamplesPerSec = spec->info.rate;
499 wfx.wBitsPerSample = (spec->info.bpf * 8) / wfx.nChannels;
500 wfx.nBlockAlign = spec->info.bpf;
501 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
502
503 /* Create directsound buffer with size based on our configured
504 * buffer_size (which is 200 ms by default) */
505 dsoundsink->buffer_size =
506 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->buffer_time,
507 GST_MSECOND);
508 /* Make sure we make those numbers multiple of our sample size in bytes */
509 dsoundsink->buffer_size += dsoundsink->buffer_size % spec->info.bpf;
510
511 spec->segsize =
512 gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->latency_time,
513 GST_MSECOND);
514 spec->segsize += spec->segsize % spec->info.bpf;
515 spec->segtotal = dsoundsink->buffer_size / spec->segsize;
516 } else {
517 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
518 wfx.cbSize = 0;
519 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
520 wfx.nChannels = 2;
521 wfx.nSamplesPerSec = 48000;
522 wfx.wBitsPerSample = 16;
523 wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
524 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
525
526 spec->segsize = 6144;
527 spec->segtotal = 10;
528 #else
529 g_assert_not_reached ();
530 #endif
531 }
532
533 // Make the final buffer size be an integer number of segments
534 dsoundsink->buffer_size = spec->segsize * spec->segtotal;
535
536 GST_INFO_OBJECT (dsoundsink,
537 "GstAudioRingBufferSpec->channels: %d, GstAudioRingBufferSpec->rate: %d, GstAudioRingBufferSpec->bytes_per_sample: %d\n"
538 "WAVEFORMATEX.nSamplesPerSec: %ld, WAVEFORMATEX.wBitsPerSample: %d, WAVEFORMATEX.nBlockAlign: %d, WAVEFORMATEX.nAvgBytesPerSec: %ld\n"
539 "Size of dsound circular buffer=>%d\n", spec->info.channels,
540 spec->info.rate, spec->info.bpf, wfx.nSamplesPerSec, wfx.wBitsPerSample,
541 wfx.nBlockAlign, wfx.nAvgBytesPerSec, dsoundsink->buffer_size);
542
543 /* create a secondary directsound buffer */
544 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
545 descSecondary.dwSize = sizeof (DSBUFFERDESC);
546 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
547 if (!gst_directsound_sink_is_spdif_format (spec))
548 descSecondary.dwFlags |= DSBCAPS_CTRLVOLUME;
549
550 descSecondary.dwBufferBytes = dsoundsink->buffer_size;
551 descSecondary.lpwfxFormat = (WAVEFORMATEX *) & wfx;
552
553 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
554 &dsoundsink->pDSBSecondary, NULL);
555 if (FAILED (hRes)) {
556 GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
557 ("gst_directsound_sink_prepare: IDirectSound_CreateSoundBuffer: %s",
558 DXGetErrorString9 (hRes)), (NULL));
559 return FALSE;
560 }
561
562 gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
563
564 return TRUE;
565 }
566
567 static gboolean
568 gst_directsound_sink_unprepare (GstAudioSink * asink)
569 {
570 GstDirectSoundSink *dsoundsink;
571
572 dsoundsink = GST_DIRECTSOUND_SINK (asink);
573
574 /* release secondary DirectSound buffer */
575 if (dsoundsink->pDSBSecondary) {
576 IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
577 dsoundsink->pDSBSecondary = NULL;
578 }
579
580 return TRUE;
581 }
582
583 static gboolean
584 gst_directsound_sink_close (GstAudioSink * asink)
585 {
586 GstDirectSoundSink *dsoundsink = NULL;
587
588 dsoundsink = GST_DIRECTSOUND_SINK (asink);
589
590 /* release DirectSound object */
591 g_return_val_if_fail (dsoundsink->pDS != NULL, FALSE);
592 IDirectSound_Release (dsoundsink->pDS);
593 dsoundsink->pDS = NULL;
594
595 gst_caps_replace (&dsoundsink->cached_caps, NULL);
596
597 return TRUE;
598 }
599
600 static gint
601 gst_directsound_sink_write (GstAudioSink * asink, gpointer data, guint length)
602 {
603 GstDirectSoundSink *dsoundsink;
604 DWORD dwStatus;
605 HRESULT hRes;
606 LPVOID pLockedBuffer1 = NULL, pLockedBuffer2 = NULL;
607 DWORD dwSizeBuffer1, dwSizeBuffer2;
608 DWORD dwCurrentPlayCursor;
609
610 dsoundsink = GST_DIRECTSOUND_SINK (asink);
611
612 GST_DSOUND_LOCK (dsoundsink);
613
614 /* get current buffer status */
615 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
616
617 /* get current play cursor position */
618 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
619 &dwCurrentPlayCursor, NULL);
620
621 if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) {
622 DWORD dwFreeBufferSize;
623
624 calculate_freesize:
625 /* calculate the free size of the circular buffer */
626 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
627 dwFreeBufferSize =
628 dsoundsink->buffer_size - (dsoundsink->current_circular_offset -
629 dwCurrentPlayCursor);
630 else
631 dwFreeBufferSize =
632 dwCurrentPlayCursor - dsoundsink->current_circular_offset;
633
634 if (length >= dwFreeBufferSize) {
635 Sleep (100);
636 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
637 &dwCurrentPlayCursor, NULL);
638
639 hRes =
640 IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
641 if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING))
642 goto calculate_freesize;
643 else {
644 dsoundsink->first_buffer_after_reset = FALSE;
645 GST_DSOUND_UNLOCK (dsoundsink);
646 return 0;
647 }
648 }
649 }
650
651 if (dwStatus & DSBSTATUS_BUFFERLOST) {
652 hRes = IDirectSoundBuffer_Restore (dsoundsink->pDSBSecondary); /*need a loop waiting the buffer is restored?? */
653
654 dsoundsink->current_circular_offset = 0;
655 }
656
657 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
658 dsoundsink->current_circular_offset, length, &pLockedBuffer1,
659 &dwSizeBuffer1, &pLockedBuffer2, &dwSizeBuffer2, 0L);
660
661 if (SUCCEEDED (hRes)) {
662 // Write to pointers without reordering.
663 memcpy (pLockedBuffer1, data, dwSizeBuffer1);
664 if (pLockedBuffer2 != NULL)
665 memcpy (pLockedBuffer2, (LPBYTE) data + dwSizeBuffer1, dwSizeBuffer2);
666
667 // Update where the buffer will lock (for next time)
668 dsoundsink->current_circular_offset += dwSizeBuffer1 + dwSizeBuffer2;
669 dsoundsink->current_circular_offset %= dsoundsink->buffer_size; /* Circular buffer */
670
671 hRes = IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer1,
672 dwSizeBuffer1, pLockedBuffer2, dwSizeBuffer2);
673 }
674
675 /* if the buffer was not in playing state yet, call play on the buffer
676 except if this buffer is the fist after a reset (base class call reset and write a buffer when setting the sink to pause) */
677 if (!(dwStatus & DSBSTATUS_PLAYING) &&
678 dsoundsink->first_buffer_after_reset == FALSE) {
679 hRes = IDirectSoundBuffer_Play (dsoundsink->pDSBSecondary, 0, 0,
680 DSBPLAY_LOOPING);
681 }
682
683 dsoundsink->first_buffer_after_reset = FALSE;
684
685 GST_DSOUND_UNLOCK (dsoundsink);
686
687 return length;
688 }
689
690 static guint
691 gst_directsound_sink_delay (GstAudioSink * asink)
692 {
693 GstDirectSoundSink *dsoundsink;
694 HRESULT hRes;
695 DWORD dwCurrentPlayCursor;
696 DWORD dwBytesInQueue = 0;
697 gint nNbSamplesInQueue = 0;
698 DWORD dwStatus;
699
700 dsoundsink = GST_DIRECTSOUND_SINK (asink);
701
702 /* get current buffer status */
703 hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
704
705 if (dwStatus & DSBSTATUS_PLAYING) {
706 /*evaluate the number of samples in queue in the circular buffer */
707 hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
708 &dwCurrentPlayCursor, NULL);
709
710 if (hRes == S_OK) {
711 if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
712 dwBytesInQueue =
713 dsoundsink->current_circular_offset - dwCurrentPlayCursor;
714 else
715 dwBytesInQueue =
716 dsoundsink->current_circular_offset + (dsoundsink->buffer_size -
717 dwCurrentPlayCursor);
718
719 nNbSamplesInQueue = dwBytesInQueue / dsoundsink->bytes_per_sample;
720 }
721 }
722
723 return nNbSamplesInQueue;
724 }
725
726 static void
727 gst_directsound_sink_reset (GstAudioSink * asink)
728 {
729 GstDirectSoundSink *dsoundsink;
730 LPVOID pLockedBuffer = NULL;
731 DWORD dwSizeBuffer = 0;
732
733 dsoundsink = GST_DIRECTSOUND_SINK (asink);
734
735 GST_DSOUND_LOCK (dsoundsink);
736
737 if (dsoundsink->pDSBSecondary) {
738 /*stop playing */
739 HRESULT hRes = IDirectSoundBuffer_Stop (dsoundsink->pDSBSecondary);
740
741 /*reset position */
742 hRes = IDirectSoundBuffer_SetCurrentPosition (dsoundsink->pDSBSecondary, 0);
743 dsoundsink->current_circular_offset = 0;
744
745 /*reset the buffer */
746 hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
747 dsoundsink->current_circular_offset, dsoundsink->buffer_size,
748 &pLockedBuffer, &dwSizeBuffer, NULL, NULL, 0L);
749
750 if (SUCCEEDED (hRes)) {
751 memset (pLockedBuffer, 0, dwSizeBuffer);
752
753 hRes =
754 IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer,
755 dwSizeBuffer, NULL, 0);
756 }
757 }
758
759 dsoundsink->first_buffer_after_reset = TRUE;
760
761 GST_DSOUND_UNLOCK (dsoundsink);
762 }
763
764 /*
765 * gst_directsound_probe_supported_formats:
766 *
767 * Takes the template caps and returns the subset which is actually
768 * supported by this device.
769 *
770 */
771
772 static GstCaps *
773 gst_directsound_probe_supported_formats (GstDirectSoundSink * dsoundsink,
774 const GstCaps * template_caps)
775 {
776 HRESULT hRes;
777 DSBUFFERDESC descSecondary;
778 WAVEFORMATEX wfx;
779 GstCaps *caps;
780 GstCaps *tmp, *tmp2;
781 LPDIRECTSOUNDBUFFER tmpBuffer;
782
783 caps = gst_caps_copy (template_caps);
784
785 /*
786 * Check availability of digital output by trying to create an SPDIF buffer
787 */
788
789 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
790 /* fill the WAVEFORMATEX structure with some standard AC3 over SPDIF params */
791 memset (&wfx, 0, sizeof (wfx));
792 wfx.cbSize = 0;
793 wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
794 wfx.nChannels = 2;
795 wfx.nSamplesPerSec = 48000;
796 wfx.wBitsPerSample = 16;
797 wfx.nBlockAlign = 4;
798 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
799
800 // create a secondary directsound buffer
801 memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
802 descSecondary.dwSize = sizeof (DSBUFFERDESC);
803 descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
804 descSecondary.dwBufferBytes = 6144;
805 descSecondary.lpwfxFormat = &wfx;
806
807 hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
808 &tmpBuffer, NULL);
809 if (FAILED (hRes)) {
810 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough not supported "
811 "(IDirectSound_CreateSoundBuffer returned: %s)\n",
812 DXGetErrorString9 (hRes));
813 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
814 tmp2 = gst_caps_subtract (caps, tmp);
815 gst_caps_unref (tmp);
816 gst_caps_unref (caps);
817 caps = tmp2;
818 tmp = gst_caps_new_empty_simple ("audio/x-dts");
819 tmp2 = gst_caps_subtract (caps, tmp);
820 gst_caps_unref (tmp);
821 gst_caps_unref (caps);
822 caps = tmp2;
823 } else {
824 GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
825 hRes = IDirectSoundBuffer_Release (tmpBuffer);
826 if (FAILED (hRes)) {
827 GST_DEBUG_OBJECT (dsoundsink,
828 "(IDirectSoundBuffer_Release returned: %s)\n",
829 DXGetErrorString9 (hRes));
830 }
831 }
832 #else
833 tmp = gst_caps_new_empty_simple ("audio/x-ac3");
834 tmp2 = gst_caps_subtract (caps, tmp);
835 gst_caps_unref (tmp);
836 gst_caps_unref (caps);
837 caps = tmp2;
838 tmp = gst_caps_new_empty_simple ("audio/x-dts");
839 tmp2 = gst_caps_subtract (caps, tmp);
840 gst_caps_unref (tmp);
841 gst_caps_unref (caps);
842 caps = tmp2;
843 #endif
844
845 return caps;
846 }
847
848 static GstBuffer *
849 gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
850 {
851 if (gst_directsound_sink_is_spdif_format (&sink->ringbuffer->spec)) {
852 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
853 GstBuffer *out;
854 GstMapInfo infobuf, infoout;
855 gboolean success;
856
857 if (framesize <= 0)
858 return NULL;
859
860 out = gst_buffer_new_and_alloc (framesize);
861
862 if (!gst_buffer_map (buf, &infobuf, GST_MAP_READWRITE)) {
863 gst_buffer_unref (out);
864 return NULL;
865 }
866 if (!gst_buffer_map (out, &infoout, GST_MAP_READWRITE)) {
867 gst_buffer_unmap (buf, &infobuf);
868 gst_buffer_unref (out);
869 return NULL;
870 }
871 success = gst_audio_iec61937_payload (infobuf.data, infobuf.size,
872 infoout.data, infoout.size, &sink->ringbuffer->spec, G_BYTE_ORDER);
873 if (!success) {
874 gst_buffer_unmap (out, &infoout);
875 gst_buffer_unmap (buf, &infobuf);
876 gst_buffer_unref (out);
877 return NULL;
878 }
879
880 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_ALL, 0, -1);
881 /* Fix endianness */
882 _swab ((gchar *) infoout.data, (gchar *) infoout.data, infobuf.size);
883 gst_buffer_unmap (out, &infoout);
884 gst_buffer_unmap (buf, &infobuf);
885 return out;
886 } else
887 return gst_buffer_ref (buf);
888 }
889
890 static void
891 gst_directsound_sink_set_volume (GstDirectSoundSink * dsoundsink,
892 gdouble dvolume, gboolean store)
893 {
894 glong volume;
895
896 volume = dvolume * 100;
897 if (store)
898 dsoundsink->volume = volume;
899
900 if (dsoundsink->pDSBSecondary) {
901 /* DirectSound controls volume using units of 100th of a decibel,
902 * ranging from -10000 to 0. We use a linear scale of 0 - 100
903 * here, so remap.
904 */
905 long dsVolume;
906 if (dsoundsink->volume == 0)
907 dsVolume = -10000;
908 else
909 dsVolume = 100 * (long) (20 * log10 ((double) dsoundsink->volume / 100.));
910 dsVolume = CLAMP (dsVolume, -10000, 0);
911
912 GST_DEBUG_OBJECT (dsoundsink,
913 "Setting volume on secondary buffer to %d from %d", (int) dsVolume,
914 (int) dsoundsink->volume);
915 IDirectSoundBuffer_SetVolume (dsoundsink->pDSBSecondary, dsVolume);
916 }
917 }
918
919 gdouble
920 gst_directsound_sink_get_volume (GstDirectSoundSink * dsoundsink)
921 {
922 return (gdouble) dsoundsink->volume / 100;
923 }
924
925 static void
926 gst_directsound_sink_set_mute (GstDirectSoundSink * dsoundsink, gboolean mute)
927 {
928 if (mute)
929 gst_directsound_sink_set_volume (dsoundsink, 0, FALSE);
930 else
931 gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
932 }
933
934 static gboolean
935 gst_directsound_sink_get_mute (GstDirectSoundSink * dsoundsink)
936 {
937 return FALSE;
938 }