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