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