v4l2: better handle quirks activation
[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
105 static gboolean gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec *
106     spec);
107
108 static GstStaticPadTemplate directsoundsink_sink_factory =
109     GST_STATIC_PAD_TEMPLATE ("sink",
110     GST_PAD_SINK,
111     GST_PAD_ALWAYS,
112     GST_STATIC_CAPS ("audio/x-raw, "
113         "format = (string) S16LE, "
114         "layout = (string) interleaved, "
115         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
116         "audio/x-raw, "
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;"));
122
123 enum
124 {
125   PROP_0,
126   PROP_VOLUME,
127   PROP_MUTE
128 };
129
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)
133     );
134
135 static void
136 gst_directsound_sink_finalize (GObject * object)
137 {
138   GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (object);
139
140   g_mutex_clear (&dsoundsink->dsound_lock);
141
142   G_OBJECT_CLASS (parent_class)->finalize (object);
143 }
144
145 static void
146 gst_directsound_sink_class_init (GstDirectSoundSinkClass * klass)
147 {
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);
154
155   GST_DEBUG_CATEGORY_INIT (directsoundsink_debug, "directsoundsink", 0,
156       "DirectSound sink");
157
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;
161
162   gstbasesink_class->get_caps =
163       GST_DEBUG_FUNCPTR (gst_directsound_sink_getcaps);
164
165   gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_directsound_sink_query);
166
167   gstaudiobasesink_class->payload =
168       GST_DEBUG_FUNCPTR (gst_directsound_sink_payload);
169
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);
179
180   g_object_class_install_property (gobject_class,
181       PROP_VOLUME,
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));
185
186   g_object_class_install_property (gobject_class,
187       PROP_MUTE,
188       g_param_spec_boolean ("mute", "Mute",
189           "Mute state of this stream", DEFAULT_MUTE,
190           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191
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>");
196
197   gst_element_class_add_pad_template (element_class,
198       gst_static_pad_template_get (&directsoundsink_sink_factory));
199 }
200
201 static void
202 gst_directsound_sink_init (GstDirectSoundSink * dsoundsink)
203 {
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;
214 }
215
216 static void
217 gst_directsound_sink_set_property (GObject * object,
218     guint prop_id, const GValue * value, GParamSpec * pspec)
219 {
220   GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
221
222   switch (prop_id) {
223     case PROP_VOLUME:
224       gst_directsound_sink_set_volume (sink, g_value_get_double (value), TRUE);
225       break;
226     case PROP_MUTE:
227       gst_directsound_sink_set_mute (sink, g_value_get_boolean (value));
228       break;
229     default:
230       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231       break;
232   }
233 }
234
235 static void
236 gst_directsound_sink_get_property (GObject * object,
237     guint prop_id, GValue * value, GParamSpec * pspec)
238 {
239   GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
240
241   switch (prop_id) {
242     case PROP_VOLUME:
243       g_value_set_double (value, gst_directsound_sink_get_volume (sink));
244       break;
245     case PROP_MUTE:
246       g_value_set_boolean (value, gst_directsound_sink_get_mute (sink));
247       break;
248     default:
249       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250       break;
251   }
252 }
253
254 static GstCaps *
255 gst_directsound_sink_getcaps (GstBaseSink * bsink, GstCaps * filter)
256 {
257   GstElementClass *element_class;
258   GstPadTemplate *pad_template;
259   GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (bsink);
260   GstCaps *caps;
261
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 */
265   }
266
267   if (dsoundsink->cached_caps) {
268     caps = gst_caps_ref (dsoundsink->cached_caps);
269   } else {
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);
273
274     caps = gst_directsound_probe_supported_formats (dsoundsink,
275         gst_pad_template_get_caps (pad_template));
276     if (caps)
277       dsoundsink->cached_caps = gst_caps_ref (caps);
278   }
279
280   if (caps && filter) {
281     GstCaps *tmp =
282         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
283     gst_caps_unref (caps);
284     caps = tmp;
285   }
286
287   if (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);
291   }
292
293   return caps;
294 }
295
296 static gboolean
297 gst_directsound_sink_acceptcaps (GstBaseSink * sink, GstQuery * query)
298 {
299   GstDirectSoundSink *dsink = GST_DIRECTSOUND_SINK (sink);
300   GstPad *pad;
301   GstCaps *caps;
302   GstCaps *pad_caps;
303   GstStructure *st;
304   gboolean ret = FALSE;
305   GstAudioRingBufferSpec spec = { 0 };
306
307   if (G_UNLIKELY (dsink == NULL))
308     return FALSE;
309
310   pad = sink->sinkpad;
311
312   gst_query_parse_accept_caps (query, &caps);
313   GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
314
315   pad_caps = gst_pad_query_caps (pad, NULL);
316   if (pad_caps) {
317     gboolean cret = gst_caps_is_subset (caps, pad_caps);
318     gst_caps_unref (pad_caps);
319     if (!cret) {
320       GST_DEBUG_OBJECT (dsink,
321           "Caps are not a subset of the pad caps, not accepting caps");
322       goto done;
323     }
324   }
325
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");
330     goto done;
331   }
332
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");
336     goto done;
337   }
338
339   /* Make sure input is framed (one frame per buffer) and can be payloaded */
340   switch (spec.type) {
341     case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
342     case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
343     {
344       gboolean framed = FALSE, parsed = FALSE;
345       st = gst_caps_get_structure (caps, 0);
346
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");
351         goto done;
352       }
353     }
354     default:
355       break;
356   }
357   ret = TRUE;
358   GST_DEBUG_OBJECT (dsink, "Accepting caps");
359
360 done:
361   gst_query_set_accept_caps_result (query, ret);
362   return TRUE;
363 }
364
365 static gboolean
366 gst_directsound_sink_query (GstBaseSink * sink, GstQuery * query)
367 {
368   gboolean res = TRUE;
369
370   switch (GST_QUERY_TYPE (query)) {
371     case GST_QUERY_ACCEPT_CAPS:
372       res = gst_directsound_sink_acceptcaps (sink, query);
373       break;
374     default:
375       res = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
376   }
377
378   return res;
379 }
380
381 static gboolean
382 gst_directsound_sink_open (GstAudioSink * asink)
383 {
384   GstDirectSoundSink *dsoundsink;
385   HRESULT hRes;
386
387   dsoundsink = GST_DIRECTSOUND_SINK (asink);
388
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));
394     return FALSE;
395   }
396
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));
402     return FALSE;
403   }
404
405   return TRUE;
406 }
407
408 static gboolean
409 gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec * spec)
410 {
411   return spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3 ||
412       spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS;
413 }
414
415 static gboolean
416 gst_directsound_sink_prepare (GstAudioSink * asink,
417     GstAudioRingBufferSpec * spec)
418 {
419   GstDirectSoundSink *dsoundsink;
420   HRESULT hRes;
421   DSBUFFERDESC descSecondary;
422   WAVEFORMATEX wfx;
423
424   dsoundsink = GST_DIRECTSOUND_SINK (asink);
425
426   /*save number of bytes per sample and buffer format */
427   dsoundsink->bytes_per_sample = spec->info.bpf;
428   dsoundsink->type = spec->type;
429
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;
440
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,
445         GST_MSECOND);
446     /* Make sure we make those numbers multiple of our sample size in bytes */
447     dsoundsink->buffer_size += dsoundsink->buffer_size % spec->info.bpf;
448
449     spec->segsize =
450         gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->latency_time,
451         GST_MSECOND);
452     spec->segsize += spec->segsize % spec->info.bpf;
453     spec->segtotal = dsoundsink->buffer_size / spec->segsize;
454   } else {
455 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
456     wfx.cbSize = 0;
457     wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
458     wfx.nChannels = 2;
459     wfx.nSamplesPerSec = 48000;
460     wfx.wBitsPerSample = 16;
461     wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
462     wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
463
464     spec->segsize = 6144;
465     spec->segtotal = 10;
466 #else
467     g_assert_not_reached ();
468 #endif
469   }
470
471   // Make the final buffer size be an integer number of segments
472   dsoundsink->buffer_size = spec->segsize * spec->segtotal;
473
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);
480
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;
487
488   descSecondary.dwBufferBytes = dsoundsink->buffer_size;
489   descSecondary.lpwfxFormat = (WAVEFORMATEX *) & wfx;
490
491   hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
492       &dsoundsink->pDSBSecondary, NULL);
493   if (FAILED (hRes)) {
494     GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
495         ("gst_directsound_sink_prepare: IDirectSound_CreateSoundBuffer: %s",
496             DXGetErrorString9 (hRes)), (NULL));
497     return FALSE;
498   }
499
500   gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
501
502   return TRUE;
503 }
504
505 static gboolean
506 gst_directsound_sink_unprepare (GstAudioSink * asink)
507 {
508   GstDirectSoundSink *dsoundsink;
509
510   dsoundsink = GST_DIRECTSOUND_SINK (asink);
511
512   /* release secondary DirectSound buffer */
513   if (dsoundsink->pDSBSecondary) {
514     IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
515     dsoundsink->pDSBSecondary = NULL;
516   }
517
518   return TRUE;
519 }
520
521 static gboolean
522 gst_directsound_sink_close (GstAudioSink * asink)
523 {
524   GstDirectSoundSink *dsoundsink = NULL;
525
526   dsoundsink = GST_DIRECTSOUND_SINK (asink);
527
528   /* release DirectSound object */
529   g_return_val_if_fail (dsoundsink->pDS != NULL, FALSE);
530   IDirectSound_Release (dsoundsink->pDS);
531   dsoundsink->pDS = NULL;
532
533   gst_caps_replace (&dsoundsink->cached_caps, NULL);
534
535   return TRUE;
536 }
537
538 static gint
539 gst_directsound_sink_write (GstAudioSink * asink, gpointer data, guint length)
540 {
541   GstDirectSoundSink *dsoundsink;
542   DWORD dwStatus;
543   HRESULT hRes;
544   LPVOID pLockedBuffer1 = NULL, pLockedBuffer2 = NULL;
545   DWORD dwSizeBuffer1, dwSizeBuffer2;
546   DWORD dwCurrentPlayCursor;
547
548   dsoundsink = GST_DIRECTSOUND_SINK (asink);
549
550   GST_DSOUND_LOCK (dsoundsink);
551
552   /* get current buffer status */
553   hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
554
555   /* get current play cursor position */
556   hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
557       &dwCurrentPlayCursor, NULL);
558
559   if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) {
560     DWORD dwFreeBufferSize;
561
562   calculate_freesize:
563     /* calculate the free size of the circular buffer */
564     if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
565       dwFreeBufferSize =
566           dsoundsink->buffer_size - (dsoundsink->current_circular_offset -
567           dwCurrentPlayCursor);
568     else
569       dwFreeBufferSize =
570           dwCurrentPlayCursor - dsoundsink->current_circular_offset;
571
572     if (length >= dwFreeBufferSize) {
573       Sleep (100);
574       hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
575           &dwCurrentPlayCursor, NULL);
576
577       hRes =
578           IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
579       if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING))
580         goto calculate_freesize;
581       else {
582         dsoundsink->first_buffer_after_reset = FALSE;
583         GST_DSOUND_UNLOCK (dsoundsink);
584         return 0;
585       }
586     }
587   }
588
589   if (dwStatus & DSBSTATUS_BUFFERLOST) {
590     hRes = IDirectSoundBuffer_Restore (dsoundsink->pDSBSecondary);      /*need a loop waiting the buffer is restored?? */
591
592     dsoundsink->current_circular_offset = 0;
593   }
594
595   hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
596       dsoundsink->current_circular_offset, length, &pLockedBuffer1,
597       &dwSizeBuffer1, &pLockedBuffer2, &dwSizeBuffer2, 0L);
598
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);
604
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 */
608
609     hRes = IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer1,
610         dwSizeBuffer1, pLockedBuffer2, dwSizeBuffer2);
611   }
612
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,
618         DSBPLAY_LOOPING);
619   }
620
621   dsoundsink->first_buffer_after_reset = FALSE;
622
623   GST_DSOUND_UNLOCK (dsoundsink);
624
625   return length;
626 }
627
628 static guint
629 gst_directsound_sink_delay (GstAudioSink * asink)
630 {
631   GstDirectSoundSink *dsoundsink;
632   HRESULT hRes;
633   DWORD dwCurrentPlayCursor;
634   DWORD dwBytesInQueue = 0;
635   gint nNbSamplesInQueue = 0;
636   DWORD dwStatus;
637
638   dsoundsink = GST_DIRECTSOUND_SINK (asink);
639
640   /* get current buffer status */
641   hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
642
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);
647
648     if (hRes == S_OK) {
649       if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
650         dwBytesInQueue =
651             dsoundsink->current_circular_offset - dwCurrentPlayCursor;
652       else
653         dwBytesInQueue =
654             dsoundsink->current_circular_offset + (dsoundsink->buffer_size -
655             dwCurrentPlayCursor);
656
657       nNbSamplesInQueue = dwBytesInQueue / dsoundsink->bytes_per_sample;
658     }
659   }
660
661   return nNbSamplesInQueue;
662 }
663
664 static void
665 gst_directsound_sink_reset (GstAudioSink * asink)
666 {
667   GstDirectSoundSink *dsoundsink;
668   LPVOID pLockedBuffer = NULL;
669   DWORD dwSizeBuffer = 0;
670
671   dsoundsink = GST_DIRECTSOUND_SINK (asink);
672
673   GST_DSOUND_LOCK (dsoundsink);
674
675   if (dsoundsink->pDSBSecondary) {
676     /*stop playing */
677     HRESULT hRes = IDirectSoundBuffer_Stop (dsoundsink->pDSBSecondary);
678
679     /*reset position */
680     hRes = IDirectSoundBuffer_SetCurrentPosition (dsoundsink->pDSBSecondary, 0);
681     dsoundsink->current_circular_offset = 0;
682
683     /*reset the buffer */
684     hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
685         dsoundsink->current_circular_offset, dsoundsink->buffer_size,
686         &pLockedBuffer, &dwSizeBuffer, NULL, NULL, 0L);
687
688     if (SUCCEEDED (hRes)) {
689       memset (pLockedBuffer, 0, dwSizeBuffer);
690
691       hRes =
692           IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer,
693           dwSizeBuffer, NULL, 0);
694     }
695   }
696
697   dsoundsink->first_buffer_after_reset = TRUE;
698
699   GST_DSOUND_UNLOCK (dsoundsink);
700 }
701
702 /*
703  * gst_directsound_probe_supported_formats:
704  *
705  * Takes the template caps and returns the subset which is actually
706  * supported by this device.
707  *
708  */
709
710 static GstCaps *
711 gst_directsound_probe_supported_formats (GstDirectSoundSink * dsoundsink,
712     const GstCaps * template_caps)
713 {
714   HRESULT hRes;
715   DSBUFFERDESC descSecondary;
716   WAVEFORMATEX wfx;
717   GstCaps *caps;
718   GstCaps *tmp, *tmp2;
719   LPDIRECTSOUNDBUFFER tmpBuffer;
720
721   caps = gst_caps_copy (template_caps);
722
723   /*
724    * Check availability of digital output by trying to create an SPDIF buffer
725    */
726
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));
730   wfx.cbSize = 0;
731   wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
732   wfx.nChannels = 2;
733   wfx.nSamplesPerSec = 48000;
734   wfx.wBitsPerSample = 16;
735   wfx.nBlockAlign = 4;
736   wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
737
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;
744
745   hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
746       &tmpBuffer, NULL);
747   if (FAILED (hRes)) {
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);
755     caps = tmp2;
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);
760     caps = tmp2;
761   } else {
762     GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
763     hRes = IDirectSoundBuffer_Release (tmpBuffer);
764     if (FAILED (hRes)) {
765       GST_DEBUG_OBJECT (dsoundsink,
766           "(IDirectSoundBuffer_Release returned: %s)\n",
767           DXGetErrorString9 (hRes));
768     }
769   }
770 #else
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);
775   caps = tmp2;
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);
780   caps = tmp2;
781 #endif
782
783   return caps;
784 }
785
786 static GstBuffer *
787 gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
788 {
789   if (gst_directsound_sink_is_spdif_format (&sink->ringbuffer->spec)) {
790     gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
791     GstBuffer *out;
792     GstMapInfo infobuf, infoout;
793     gboolean success;
794
795     if (framesize <= 0)
796       return NULL;
797
798     out = gst_buffer_new_and_alloc (framesize);
799
800     if (!gst_buffer_map (buf, &infobuf, GST_MAP_READWRITE)) {
801       gst_buffer_unref (out);
802       return NULL;
803     }
804     if (!gst_buffer_map (out, &infoout, GST_MAP_READWRITE)) {
805       gst_buffer_unmap (buf, &infobuf);
806       gst_buffer_unref (out);
807       return NULL;
808     }
809     success = gst_audio_iec61937_payload (infobuf.data, infobuf.size,
810         infoout.data, infoout.size, &sink->ringbuffer->spec, G_BYTE_ORDER);
811     if (!success) {
812       gst_buffer_unmap (out, &infoout);
813       gst_buffer_unmap (buf, &infobuf);
814       gst_buffer_unref (out);
815       return NULL;
816     }
817
818     gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_ALL, 0, -1);
819     /* Fix endianness */
820     _swab ((gchar *) infoout.data, (gchar *) infoout.data, infobuf.size);
821     gst_buffer_unmap (out, &infoout);
822     gst_buffer_unmap (buf, &infobuf);
823     return out;
824   } else
825     return gst_buffer_ref (buf);
826 }
827
828 static void
829 gst_directsound_sink_set_volume (GstDirectSoundSink * dsoundsink,
830     gdouble dvolume, gboolean store)
831 {
832   glong volume;
833
834   volume = dvolume * 100;
835   if (store)
836     dsoundsink->volume = volume;
837
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
841      * here, so remap.
842      */
843     long dsVolume;
844     if (dsoundsink->volume == 0)
845       dsVolume = -10000;
846     else
847       dsVolume = 100 * (long) (20 * log10 ((double) dsoundsink->volume / 100.));
848     dsVolume = CLAMP (dsVolume, -10000, 0);
849
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);
854   }
855 }
856
857 gdouble
858 gst_directsound_sink_get_volume (GstDirectSoundSink * dsoundsink)
859 {
860   return (gdouble) dsoundsink->volume / 100;
861 }
862
863 static void
864 gst_directsound_sink_set_mute (GstDirectSoundSink * dsoundsink, gboolean mute)
865 {
866   if (mute)
867     gst_directsound_sink_set_volume (dsoundsink, 0, FALSE);
868   else
869     gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
870 }
871
872 static gboolean
873 gst_directsound_sink_get_mute (GstDirectSoundSink * dsoundsink)
874 {
875   return FALSE;
876 }