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