c5755971b0567576ec42a685153ffc69b8792887
[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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, 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 -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 -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 <gst/audio/streamvolume.h>
57 #include "gstdirectsoundsink.h"
58 #include <gst/audio/gstaudioiec61937.h>
59
60 #include <math.h>
61
62 #ifdef __CYGWIN__
63 #include <unistd.h>
64 #ifndef _swab
65 #define _swab swab
66 #endif
67 #endif
68
69 #define DEFAULT_MUTE FALSE
70
71 GST_DEBUG_CATEGORY_STATIC (directsoundsink_debug);
72 #define GST_CAT_DEFAULT directsoundsink_debug
73
74 static void gst_directsound_sink_finalize (GObject * object);
75
76 static void gst_directsound_sink_set_property (GObject * object, guint prop_id,
77     const GValue * value, GParamSpec * pspec);
78 static void gst_directsound_sink_get_property (GObject * object, guint prop_id,
79     GValue * value, GParamSpec * pspec);
80
81 static GstCaps *gst_directsound_sink_getcaps (GstBaseSink * bsink,
82     GstCaps * filter);
83 static GstBuffer *gst_directsound_sink_payload (GstAudioBaseSink * sink,
84     GstBuffer * buf);
85 static gboolean gst_directsound_sink_prepare (GstAudioSink * asink,
86     GstAudioRingBufferSpec * spec);
87 static gboolean gst_directsound_sink_unprepare (GstAudioSink * asink);
88 static gboolean gst_directsound_sink_open (GstAudioSink * asink);
89 static gboolean gst_directsound_sink_close (GstAudioSink * asink);
90 static gint gst_directsound_sink_write (GstAudioSink * asink,
91     gpointer data, guint length);
92 static guint gst_directsound_sink_delay (GstAudioSink * asink);
93 static void gst_directsound_sink_reset (GstAudioSink * asink);
94 static GstCaps *gst_directsound_probe_supported_formats (GstDirectSoundSink *
95     dsoundsink, const GstCaps * template_caps);
96 static gboolean gst_directsound_sink_query (GstBaseSink * pad, GstQuery * query);
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) S8, "
118         "layout = (string) interleaved, "
119         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ];"
120         "audio/x-ac3, framed = (boolean) true;"
121         "audio/x-dts, framed = (boolean) true;"));
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   G_OBJECT_CLASS (parent_class)->finalize (object);
139 }
140
141 static void
142 gst_directsound_sink_class_init (GstDirectSoundSinkClass * klass)
143 {
144   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
145   GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
146   GstAudioSinkClass *gstaudiosink_class = GST_AUDIO_SINK_CLASS (klass);
147   GstAudioBaseSinkClass *gstaudiobasesink_class = GST_AUDIO_BASE_SINK_CLASS (klass);
148   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
149
150   GST_DEBUG_CATEGORY_INIT (directsoundsink_debug, "directsoundsink", 0,
151       "DirectSound sink");
152
153   parent_class = g_type_class_peek_parent (klass);
154
155   gobject_class->finalize = gst_directsound_sink_finalize;
156   gobject_class->set_property = gst_directsound_sink_set_property;
157   gobject_class->get_property = gst_directsound_sink_get_property;
158
159   gstbasesink_class->get_caps =
160       GST_DEBUG_FUNCPTR (gst_directsound_sink_getcaps);
161
162   gstbasesink_class->query =
163       GST_DEBUG_FUNCPTR (gst_directsound_sink_query);
164
165   gstaudiobasesink_class->payload =
166       GST_DEBUG_FUNCPTR (gst_directsound_sink_payload);
167
168   gstaudiosink_class->prepare =
169       GST_DEBUG_FUNCPTR (gst_directsound_sink_prepare);
170   gstaudiosink_class->unprepare =
171       GST_DEBUG_FUNCPTR (gst_directsound_sink_unprepare);
172   gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_directsound_sink_open);
173   gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_directsound_sink_close);
174   gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_directsound_sink_write);
175   gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_directsound_sink_delay);
176   gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_directsound_sink_reset);
177
178   g_object_class_install_property (gobject_class,
179       PROP_VOLUME,
180       g_param_spec_double ("volume", "Volume",
181           "Volume of this stream", 0.0, 1.0, 1.0,
182           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183
184   g_object_class_install_property (gobject_class,
185       PROP_MUTE,
186       g_param_spec_boolean ("mute", "Mute",
187           "Mute state of this stream", DEFAULT_MUTE,
188           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189
190   gst_element_class_set_static_metadata (element_class,
191       "Direct Sound Audio Sink", "Sink/Audio",
192       "Output to a sound card via Direct Sound",
193       "Sebastien Moutte <sebastien@moutte.net>");
194
195   gst_element_class_add_pad_template (element_class,
196       gst_static_pad_template_get (&directsoundsink_sink_factory));
197 }
198
199 static void
200 gst_directsound_sink_init (GstDirectSoundSink * dsoundsink)
201 {
202   dsoundsink->volume = 100;
203   dsoundsink->mute = FALSE;
204   dsoundsink->pDS = NULL;
205   dsoundsink->cached_caps = NULL;
206   dsoundsink->pDSBSecondary = NULL;
207   dsoundsink->current_circular_offset = 0;
208   dsoundsink->buffer_size = DSBSIZE_MIN;
209   dsoundsink->volume = 100;
210   dsoundsink->dsound_lock = g_mutex_new ();
211   dsoundsink->first_buffer_after_reset = FALSE;
212 }
213
214 static void
215 gst_directsound_sink_set_property (GObject * object,
216     guint prop_id, const GValue * value, GParamSpec * pspec)
217 {
218   GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
219
220   switch (prop_id) {
221     case PROP_VOLUME:
222       gst_directsound_sink_set_volume (sink, g_value_get_double (value), TRUE);
223       break;
224     case PROP_MUTE:
225       gst_directsound_sink_set_mute (sink, g_value_get_boolean (value));
226       break;
227     default:
228       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229       break;
230   }
231 }
232
233 static void
234 gst_directsound_sink_get_property (GObject * object,
235     guint prop_id, GValue * value, GParamSpec * pspec)
236 {
237   GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
238
239   switch (prop_id) {
240     case PROP_VOLUME:
241       g_value_set_double (value, gst_directsound_sink_get_volume (sink));
242       break;
243     case PROP_MUTE:
244       g_value_set_boolean (value, gst_directsound_sink_get_mute (sink));
245       break;
246     default:
247       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248       break;
249   }
250 }
251
252 static GstCaps *
253 gst_directsound_sink_getcaps (GstBaseSink * bsink, GstCaps * filter)
254 {
255   GstElementClass *element_class;
256   GstPadTemplate *pad_template;
257   GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (bsink);
258   GstCaps *caps;
259   gchar *caps_string = NULL;
260
261   if (dsoundsink->pDS == NULL) {
262     GST_DEBUG_OBJECT (dsoundsink, "device not open, using template caps");
263     return NULL;                /* base class will get template caps for us */
264   }
265
266   if (dsoundsink->cached_caps) {
267     caps_string = gst_caps_to_string (dsoundsink->cached_caps);
268     GST_DEBUG_OBJECT (dsoundsink, "Returning cached caps: %s", caps_string);
269     g_free (caps_string);
270     return gst_caps_ref (dsoundsink->cached_caps);
271   }
272
273   element_class = GST_ELEMENT_GET_CLASS (dsoundsink);
274   pad_template = gst_element_class_get_pad_template (element_class, "sink");
275   g_return_val_if_fail (pad_template != NULL, NULL);
276
277   caps = gst_directsound_probe_supported_formats (dsoundsink,
278       gst_pad_template_get_caps (pad_template));
279   if (caps) {
280     dsoundsink->cached_caps = gst_caps_ref (caps);
281   }
282
283   if (caps) {
284     gchar *caps_string = gst_caps_to_string (caps);
285     GST_DEBUG_OBJECT (dsoundsink, "returning caps %s", caps_string);
286     g_free (caps_string);
287   }
288
289   return caps;
290 }
291
292 static gboolean
293 gst_directsound_sink_acceptcaps (GstBaseSink * sink, GstQuery * query)
294 {
295   GstDirectSoundSink *dsink = GST_DIRECTSOUND_SINK (sink);
296   GstPad *pad;
297   GstCaps *caps;
298   GstCaps *pad_caps;
299   GstStructure *st;
300   gboolean ret = FALSE;
301   GstAudioRingBufferSpec spec = { 0 };
302
303   if (G_UNLIKELY (dsink == NULL))
304     return FALSE;
305
306   pad = sink->sinkpad;
307
308   gst_query_parse_accept_caps (query, &caps);
309   GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
310
311   pad_caps = gst_pad_query_caps (pad, NULL);
312   if (pad_caps) {
313     gboolean cret = gst_caps_can_intersect (pad_caps, caps);
314     gst_caps_unref (pad_caps);
315     if (!cret)
316     {
317       GST_DEBUG_OBJECT (dsink, "Can't intersect caps, not accepting caps");
318       goto done;
319     }
320   }
321
322   /* If we've not got fixed caps, creating a stream might fail, so let's just
323    * return from here with default acceptcaps behaviour */
324   if (!gst_caps_is_fixed (caps))
325   {
326     GST_DEBUG_OBJECT (dsink, "Caps are not fixed, not accepting caps");
327     goto done;
328   }
329
330   spec.latency_time = GST_SECOND;
331   if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
332   {
333     GST_DEBUG_OBJECT (dsink, "Failed to parse caps, not accepting");
334     goto done;
335   }
336
337   /* Make sure input is framed (one frame per buffer) and can be payloaded */
338   switch (spec.type)
339   {
340     case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
341     case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
342     {
343       gboolean framed = FALSE, parsed = FALSE;
344       st = gst_caps_get_structure (caps, 0);
345
346       gst_structure_get_boolean (st, "framed", &framed);
347       gst_structure_get_boolean (st, "parsed", &parsed);
348       if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
349       {
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
720   caps = gst_caps_copy (template_caps);
721
722   /*
723    * Check availability of digital output by trying to create an SPDIF buffer
724    */
725
726 #ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
727   /* fill the WAVEFORMATEX structure with some standard AC3 over SPDIF params */
728   memset (&wfx, 0, sizeof (wfx));
729   wfx.cbSize = 0;
730   wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
731   wfx.nChannels = 2;
732   wfx.nSamplesPerSec = 48000;
733   wfx.wBitsPerSample = 16;
734   wfx.nBlockAlign = 4;
735   wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
736
737   // create a secondary directsound buffer
738   memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
739   descSecondary.dwSize = sizeof (DSBUFFERDESC);
740   descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
741   descSecondary.dwBufferBytes = 6144;
742   descSecondary.lpwfxFormat = &wfx;
743
744   hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
745       &dsoundsink->pDSBSecondary, NULL);
746   if (FAILED (hRes)) {
747     GST_INFO_OBJECT (dsoundsink, "AC3 passthrough not supported "
748         "(IDirectSound_CreateSoundBuffer returned: %s)\n",
749         DXGetErrorString9 (hRes));
750     tmp = gst_caps_new_empty_simple ("audio/x-ac3");
751     tmp2 = gst_caps_subtract (caps, tmp);
752     gst_caps_unref (tmp);
753     gst_caps_unref (caps);
754     caps = tmp2;
755     tmp = gst_caps_new_empty_simple ("audio/x-dts");
756     tmp2 = gst_caps_subtract (caps, tmp);
757     gst_caps_unref (tmp);
758     gst_caps_unref (caps);
759     caps = tmp2;
760   } else {
761     GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
762     hRes = IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
763     if (FAILED (hRes)) {
764       GST_DEBUG_OBJECT (dsoundsink,
765           "(IDirectSoundBuffer_Release returned: %s)\n",
766           DXGetErrorString9 (hRes));
767     }
768   }
769 #else
770   tmp = gst_caps_new_empty_simple ("audio/x-ac3");
771   tmp2 = gst_caps_subtract (caps, tmp);
772   gst_caps_unref (tmp);
773   gst_caps_unref (caps);
774   caps = tmp2;
775   tmp = gst_caps_new_empty_simple ("audio/x-dts");
776   tmp2 = gst_caps_subtract (caps, tmp);
777   gst_caps_unref (tmp);
778   gst_caps_unref (caps);
779   caps = tmp2;
780 #endif
781
782   return caps;
783 }
784
785 static GstBuffer *
786 gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
787 {
788   if (gst_directsound_sink_is_spdif_format (&sink->ringbuffer->spec))
789     {
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       {
802         gst_buffer_unref (out);
803         return NULL;
804       }
805       if (!gst_buffer_map (out, &infoout, GST_MAP_READWRITE))
806       {
807         gst_buffer_unmap (buf, &infobuf);
808         gst_buffer_unref (out);
809         return NULL;
810       }
811       success = gst_audio_iec61937_payload (infobuf.data, infobuf.size,
812           infoout.data, infoout.size, &sink->ringbuffer->spec);
813       if (!success) {
814         gst_buffer_unmap (out, &infoout);
815         gst_buffer_unmap (buf, &infobuf);
816         gst_buffer_unref (out);
817         return NULL;
818       }
819
820       gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_ALL, 0, -1);
821       /* Fix endianness */
822       _swab ((gchar *) infoout.data, (gchar *) infoout.data, infobuf.size);
823       gst_buffer_unmap (out, &infoout);
824       gst_buffer_unmap (buf, &infobuf);
825       return out;
826     }
827   else
828       return gst_buffer_ref (buf);
829 }
830
831 static void
832 gst_directsound_sink_set_volume (GstDirectSoundSink * dsoundsink,
833     gdouble dvolume, gboolean store)
834 {
835   glong volume;
836
837   volume = dvolume * 100;
838   if (store)
839     dsoundsink->volume = volume;
840
841   if (dsoundsink->pDSBSecondary) {
842     /* DirectSound controls volume using units of 100th of a decibel,
843      * ranging from -10000 to 0. We use a linear scale of 0 - 100
844      * here, so remap.
845      */
846     long dsVolume;
847     if (dsoundsink->volume == 0)
848       dsVolume = -10000;
849     else
850       dsVolume = 100 * (long) (20 * log10 ((double) dsoundsink->volume / 100.));
851     dsVolume = CLAMP (dsVolume, -10000, 0);
852
853     GST_DEBUG_OBJECT (dsoundsink,
854         "Setting volume on secondary buffer to %d from %d", (int) dsVolume,
855         (int) dsoundsink->volume);
856     IDirectSoundBuffer_SetVolume (dsoundsink->pDSBSecondary, dsVolume);
857   }
858 }
859
860 gdouble
861 gst_directsound_sink_get_volume (GstDirectSoundSink * dsoundsink)
862 {
863   return (gdouble) dsoundsink->volume / 100;
864 }
865
866 static void
867 gst_directsound_sink_set_mute (GstDirectSoundSink * dsoundsink, gboolean mute)
868 {
869   if (mute)
870     gst_directsound_sink_set_volume (dsoundsink, 0, FALSE);
871   else
872     gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
873 }
874
875 static gboolean
876 gst_directsound_sink_get_mute (GstDirectSoundSink * dsoundsink)
877 {
878   return FALSE;
879 }