1 /* GStreamer
2 * Copyright (C) 2011 David Schleef <ds@entropywave.com>
3 * Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
18 * Boston, MA 02110-1335, USA.
19 */
20 /**
21 * SECTION:element-decklinkaudiosink
22 * @short_description: Outputs Audio to a BlackMagic DeckLink Device
23 * @see_also: decklinkvideosink
24 *
25 * Playout Video and Audio to a BlackMagic DeckLink Device. Can only be used
26 * in conjunction with decklinkvideosink.
27 *
28 * ## Sample pipeline
29 * |[
30 * gst-launch-1.0 \
31 * videotestsrc ! decklinkvideosink device-number=0 mode=1080p25 \
32 * audiotestsrc ! decklinkaudiosink device-number=0
33 * ]|
34 * Playout a 1080p25 test-video with a test-audio signal to the SDI-Out of Card 0.
35 * Devices are numbered starting with 0.
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include "gstdecklinkaudiosink.h"
43 #include "gstdecklinkvideosink.h"
44 #include <string.h>
45
46 GST_DEBUG_CATEGORY_STATIC (gst_decklink_audio_sink_debug);
47 #define GST_CAT_DEFAULT gst_decklink_audio_sink_debug
48
49 #define DEFAULT_DEVICE_NUMBER (0)
50 #define DEFAULT_ALIGNMENT_THRESHOLD (40 * GST_MSECOND)
51 #define DEFAULT_DISCONT_WAIT (1 * GST_SECOND)
52 // Microseconds for audiobasesink compatibility...
53 #define DEFAULT_BUFFER_TIME (50 * GST_MSECOND / 1000)
54
55 enum
56 {
57 PROP_0,
58 PROP_DEVICE_NUMBER,
59 PROP_HW_SERIAL_NUMBER,
60 PROP_ALIGNMENT_THRESHOLD,
61 PROP_DISCONT_WAIT,
62 PROP_BUFFER_TIME,
63 };
64
65 static void gst_decklink_audio_sink_set_property (GObject * object,
66 guint property_id, const GValue * value, GParamSpec * pspec);
67 static void gst_decklink_audio_sink_get_property (GObject * object,
68 guint property_id, GValue * value, GParamSpec * pspec);
69 static void gst_decklink_audio_sink_finalize (GObject * object);
70
71 static GstStateChangeReturn
72 gst_decklink_audio_sink_change_state (GstElement * element,
73 GstStateChange transition);
74 static GstClock *gst_decklink_audio_sink_provide_clock (GstElement * element);
75
76 static GstCaps *gst_decklink_audio_sink_get_caps (GstBaseSink * bsink,
77 GstCaps * filter);
78 static gboolean gst_decklink_audio_sink_set_caps (GstBaseSink * bsink,
79 GstCaps * caps);
80 static GstFlowReturn gst_decklink_audio_sink_render (GstBaseSink * bsink,
81 GstBuffer * buffer);
82 static gboolean gst_decklink_audio_sink_open (GstBaseSink * bsink);
83 static gboolean gst_decklink_audio_sink_close (GstBaseSink * bsink);
84 static gboolean gst_decklink_audio_sink_stop (GstDecklinkAudioSink * self);
85 static gboolean gst_decklink_audio_sink_unlock_stop (GstBaseSink * bsink);
86 static void gst_decklink_audio_sink_get_times (GstBaseSink * bsink,
87 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
88 static gboolean gst_decklink_audio_sink_query (GstBaseSink * bsink,
89 GstQuery * query);
90 static gboolean gst_decklink_audio_sink_event (GstBaseSink * bsink,
91 GstEvent * event);
92
93 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
94 GST_PAD_SINK,
95 GST_PAD_ALWAYS,
96 GST_STATIC_CAPS
97 ("audio/x-raw, format={S16LE,S32LE}, channels={2, 8, 16}, rate=48000, "
98 "layout=interleaved")
99 );
100
101 #define parent_class gst_decklink_audio_sink_parent_class
102 G_DEFINE_TYPE (GstDecklinkAudioSink, gst_decklink_audio_sink,
103 GST_TYPE_BASE_SINK);
104 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (decklinkaudiosink, "decklinkaudiosink", GST_RANK_NONE,
105 GST_TYPE_DECKLINK_AUDIO_SINK, decklink_element_init (plugin));
106
107 static void
gst_decklink_audio_sink_class_init(GstDecklinkAudioSinkClass * klass)108 gst_decklink_audio_sink_class_init (GstDecklinkAudioSinkClass * klass)
109 {
110 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
111 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
112 GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass);
113
114 gobject_class->set_property = gst_decklink_audio_sink_set_property;
115 gobject_class->get_property = gst_decklink_audio_sink_get_property;
116 gobject_class->finalize = gst_decklink_audio_sink_finalize;
117
118 element_class->change_state =
119 GST_DEBUG_FUNCPTR (gst_decklink_audio_sink_change_state);
120 element_class->provide_clock =
121 GST_DEBUG_FUNCPTR (gst_decklink_audio_sink_provide_clock);
122
123 basesink_class->get_caps =
124 GST_DEBUG_FUNCPTR (gst_decklink_audio_sink_get_caps);
125 basesink_class->set_caps =
126 GST_DEBUG_FUNCPTR (gst_decklink_audio_sink_set_caps);
127 basesink_class->render = GST_DEBUG_FUNCPTR (gst_decklink_audio_sink_render);
128 // FIXME: These are misnamed in basesink!
129 basesink_class->start = GST_DEBUG_FUNCPTR (gst_decklink_audio_sink_open);
130 basesink_class->stop = GST_DEBUG_FUNCPTR (gst_decklink_audio_sink_close);
131 basesink_class->unlock_stop =
132 GST_DEBUG_FUNCPTR (gst_decklink_audio_sink_unlock_stop);
133 basesink_class->get_times =
134 GST_DEBUG_FUNCPTR (gst_decklink_audio_sink_get_times);
135 basesink_class->query = GST_DEBUG_FUNCPTR (gst_decklink_audio_sink_query);
136 basesink_class->event = GST_DEBUG_FUNCPTR (gst_decklink_audio_sink_event);
137
138 g_object_class_install_property (gobject_class, PROP_DEVICE_NUMBER,
139 g_param_spec_int ("device-number", "Device number",
140 "Output device instance to use", 0, G_MAXINT, DEFAULT_DEVICE_NUMBER,
141 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
142 G_PARAM_CONSTRUCT)));
143
144 g_object_class_install_property (gobject_class, PROP_HW_SERIAL_NUMBER,
145 g_param_spec_string ("hw-serial-number", "Hardware serial number",
146 "The serial number (hardware ID) of the Decklink card",
147 NULL, (GParamFlags) (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
148
149 g_object_class_install_property (gobject_class, PROP_ALIGNMENT_THRESHOLD,
150 g_param_spec_uint64 ("alignment-threshold", "Alignment Threshold",
151 "Timestamp alignment threshold in nanoseconds", 0,
152 G_MAXUINT64 - 1, DEFAULT_ALIGNMENT_THRESHOLD,
153 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
154 GST_PARAM_MUTABLE_READY)));
155
156 g_object_class_install_property (gobject_class, PROP_DISCONT_WAIT,
157 g_param_spec_uint64 ("discont-wait", "Discont Wait",
158 "Window of time in nanoseconds to wait before "
159 "creating a discontinuity", 0,
160 G_MAXUINT64 - 1, DEFAULT_DISCONT_WAIT,
161 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
162 GST_PARAM_MUTABLE_READY)));
163
164 g_object_class_install_property (gobject_class, PROP_BUFFER_TIME,
165 g_param_spec_uint64 ("buffer-time", "Buffer Time",
166 "Size of audio buffer in microseconds, this is the minimum latency that the sink reports",
167 0, G_MAXUINT64, DEFAULT_BUFFER_TIME,
168 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
169 GST_PARAM_MUTABLE_READY)));
170
171 gst_element_class_add_static_pad_template (element_class, &sink_template);
172
173 gst_element_class_set_static_metadata (element_class, "Decklink Audio Sink",
174 "Audio/Sink/Hardware", "Decklink Sink",
175 "David Schleef <ds@entropywave.com>, "
176 "Sebastian Dröge <sebastian@centricular.com>");
177
178 GST_DEBUG_CATEGORY_INIT (gst_decklink_audio_sink_debug, "decklinkaudiosink",
179 0, "debug category for decklinkaudiosink element");
180 }
181
182 static void
gst_decklink_audio_sink_init(GstDecklinkAudioSink * self)183 gst_decklink_audio_sink_init (GstDecklinkAudioSink * self)
184 {
185 self->device_number = DEFAULT_DEVICE_NUMBER;
186 self->stream_align =
187 gst_audio_stream_align_new (48000, DEFAULT_ALIGNMENT_THRESHOLD,
188 DEFAULT_DISCONT_WAIT);
189 self->buffer_time = DEFAULT_BUFFER_TIME * 1000;
190
191 gst_base_sink_set_max_lateness (GST_BASE_SINK_CAST (self), 20 * GST_MSECOND);
192 }
193
194 void
gst_decklink_audio_sink_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)195 gst_decklink_audio_sink_set_property (GObject * object, guint property_id,
196 const GValue * value, GParamSpec * pspec)
197 {
198 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK_CAST (object);
199
200 switch (property_id) {
201 case PROP_DEVICE_NUMBER:
202 self->device_number = g_value_get_int (value);
203 break;
204 case PROP_ALIGNMENT_THRESHOLD:
205 GST_OBJECT_LOCK (self);
206 gst_audio_stream_align_set_alignment_threshold (self->stream_align,
207 g_value_get_uint64 (value));
208 GST_OBJECT_UNLOCK (self);
209 break;
210 case PROP_DISCONT_WAIT:
211 GST_OBJECT_LOCK (self);
212 gst_audio_stream_align_set_discont_wait (self->stream_align,
213 g_value_get_uint64 (value));
214 GST_OBJECT_UNLOCK (self);
215 break;
216 case PROP_BUFFER_TIME:
217 GST_OBJECT_LOCK (self);
218 self->buffer_time = g_value_get_uint64 (value) * 1000;
219 GST_OBJECT_UNLOCK (self);
220 break;
221 default:
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
223 break;
224 }
225 }
226
227 void
gst_decklink_audio_sink_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)228 gst_decklink_audio_sink_get_property (GObject * object, guint property_id,
229 GValue * value, GParamSpec * pspec)
230 {
231 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK_CAST (object);
232
233 switch (property_id) {
234 case PROP_DEVICE_NUMBER:
235 g_value_set_int (value, self->device_number);
236 break;
237 case PROP_HW_SERIAL_NUMBER:
238 if (self->output)
239 g_value_set_string (value, self->output->hw_serial_number);
240 else
241 g_value_set_string (value, NULL);
242 break;
243 case PROP_ALIGNMENT_THRESHOLD:
244 GST_OBJECT_LOCK (self);
245 g_value_set_uint64 (value,
246 gst_audio_stream_align_get_alignment_threshold (self->stream_align));
247 GST_OBJECT_UNLOCK (self);
248 break;
249 case PROP_DISCONT_WAIT:
250 GST_OBJECT_LOCK (self);
251 g_value_set_uint64 (value,
252 gst_audio_stream_align_get_discont_wait (self->stream_align));
253 GST_OBJECT_UNLOCK (self);
254 break;
255 case PROP_BUFFER_TIME:
256 GST_OBJECT_LOCK (self);
257 g_value_set_uint64 (value, self->buffer_time / 1000);
258 GST_OBJECT_UNLOCK (self);
259 break;
260 default:
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
262 break;
263 }
264 }
265
266 void
gst_decklink_audio_sink_finalize(GObject * object)267 gst_decklink_audio_sink_finalize (GObject * object)
268 {
269 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK_CAST (object);
270
271 if (self->stream_align) {
272 gst_audio_stream_align_free (self->stream_align);
273 self->stream_align = NULL;
274 }
275
276 G_OBJECT_CLASS (parent_class)->finalize (object);
277 }
278
279 static gboolean
gst_decklink_audio_sink_set_caps(GstBaseSink * bsink,GstCaps * caps)280 gst_decklink_audio_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
281 {
282 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK_CAST (bsink);
283 HRESULT ret;
284 BMDAudioSampleType sample_depth;
285 GstAudioInfo info;
286
287 GST_DEBUG_OBJECT (self, "Setting caps %" GST_PTR_FORMAT, caps);
288
289 if (!gst_audio_info_from_caps (&info, caps))
290 return FALSE;
291
292 if (self->output->audio_enabled
293 && (self->info.finfo->format != info.finfo->format
294 || self->info.channels != info.channels)) {
295 GST_ERROR_OBJECT (self, "Reconfiguration not supported");
296 return FALSE;
297 } else if (self->output->audio_enabled) {
298 return TRUE;
299 }
300
301 if (info.finfo->format == GST_AUDIO_FORMAT_S16LE) {
302 sample_depth = bmdAudioSampleType16bitInteger;
303 } else {
304 sample_depth = bmdAudioSampleType32bitInteger;
305 }
306
307 ret = self->output->output->EnableAudioOutput (bmdAudioSampleRate48kHz,
308 sample_depth, info.channels, bmdAudioOutputStreamContinuous);
309 if (ret != S_OK) {
310 GST_WARNING_OBJECT (self, "Failed to enable audio output 0x%08lx",
311 (unsigned long) ret);
312 return FALSE;
313 }
314
315 self->output->audio_enabled = TRUE;
316 self->info = info;
317
318 // Create a new resampler as needed
319 if (self->resampler)
320 gst_audio_resampler_free (self->resampler);
321 self->resampler = NULL;
322
323 return TRUE;
324 }
325
326 static GstCaps *
gst_decklink_audio_sink_get_caps(GstBaseSink * bsink,GstCaps * filter)327 gst_decklink_audio_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
328 {
329 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK_CAST (bsink);
330 GstCaps *caps;
331
332 if ((caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (bsink))))
333 return caps;
334
335 caps = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (bsink));
336
337 GST_OBJECT_LOCK (self);
338 if (self->output && self->output->attributes) {
339 int64_t max_channels = 0;
340 HRESULT ret;
341 GstStructure *s;
342 GValue arr = G_VALUE_INIT;
343 GValue v = G_VALUE_INIT;
344
345 ret =
346 self->output->attributes->GetInt (BMDDeckLinkMaximumAudioChannels,
347 &max_channels);
348 /* 2 should always be supported */
349 if (ret != S_OK) {
350 max_channels = 2;
351 }
352
353 caps = gst_caps_make_writable (caps);
354 s = gst_caps_get_structure (caps, 0);
355
356 g_value_init (&arr, GST_TYPE_LIST);
357 g_value_init (&v, G_TYPE_INT);
358 if (max_channels >= 16) {
359 g_value_set_int (&v, 16);
360 gst_value_list_append_value (&arr, &v);
361 }
362 if (max_channels >= 8) {
363 g_value_set_int (&v, 8);
364 gst_value_list_append_value (&arr, &v);
365 }
366 g_value_set_int (&v, 2);
367 gst_value_list_append_value (&arr, &v);
368
369 gst_structure_set_value (s, "channels", &arr);
370 g_value_unset (&v);
371 g_value_unset (&arr);
372 }
373 GST_OBJECT_UNLOCK (self);
374
375 if (filter) {
376 GstCaps *intersection =
377 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
378 gst_caps_unref (caps);
379 caps = intersection;
380 }
381
382 return caps;
383 }
384
385 static gboolean
gst_decklink_audio_sink_query(GstBaseSink * bsink,GstQuery * query)386 gst_decklink_audio_sink_query (GstBaseSink * bsink, GstQuery * query)
387 {
388 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK (bsink);
389 gboolean res = FALSE;
390
391 switch (GST_QUERY_TYPE (query)) {
392 case GST_QUERY_LATENCY:
393 {
394 gboolean live, us_live;
395 GstClockTime min_l, max_l;
396
397 GST_DEBUG_OBJECT (self, "latency query");
398
399 /* ask parent first, it will do an upstream query for us. */
400 if ((res =
401 gst_base_sink_query_latency (GST_BASE_SINK_CAST (self), &live,
402 &us_live, &min_l, &max_l))) {
403 GstClockTime base_latency, min_latency, max_latency;
404
405 /* we and upstream are both live, adjust the min_latency */
406 if (live && us_live) {
407 GST_OBJECT_LOCK (self);
408 if (!self->info.rate) {
409 GST_OBJECT_UNLOCK (self);
410
411 GST_DEBUG_OBJECT (self,
412 "we are not negotiated, can't report latency yet");
413 res = FALSE;
414 goto done;
415 }
416
417 base_latency = self->buffer_time * 1000;
418 GST_OBJECT_UNLOCK (self);
419
420 /* we cannot go lower than the buffer size and the min peer latency */
421 min_latency = base_latency + min_l;
422 /* the max latency is the max of the peer, we can delay an infinite
423 * amount of time. */
424 max_latency =
425 (max_l ==
426 GST_CLOCK_TIME_NONE) ? GST_CLOCK_TIME_NONE : (base_latency +
427 max_l);
428
429 GST_DEBUG_OBJECT (self,
430 "peer min %" GST_TIME_FORMAT ", our min latency: %"
431 GST_TIME_FORMAT, GST_TIME_ARGS (min_l),
432 GST_TIME_ARGS (min_latency));
433 GST_DEBUG_OBJECT (self,
434 "peer max %" GST_TIME_FORMAT ", our max latency: %"
435 GST_TIME_FORMAT, GST_TIME_ARGS (max_l),
436 GST_TIME_ARGS (max_latency));
437 } else {
438 GST_DEBUG_OBJECT (self,
439 "peer or we are not live, don't care about latency");
440 min_latency = min_l;
441 max_latency = max_l;
442 }
443 gst_query_set_latency (query, live, min_latency, max_latency);
444 }
445 break;
446 }
447 default:
448 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
449 break;
450 }
451
452 done:
453 return res;
454 }
455
456 static gboolean
gst_decklink_audio_sink_event(GstBaseSink * bsink,GstEvent * event)457 gst_decklink_audio_sink_event (GstBaseSink * bsink, GstEvent * event)
458 {
459 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK_CAST (bsink);
460
461 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
462 const GstSegment *new_segment;
463
464 gst_event_parse_segment (event, &new_segment);
465
466 if (ABS (new_segment->rate) != 1.0) {
467 guint out_rate = self->info.rate / ABS (new_segment->rate);
468
469 if (self->resampler && (self->resampler_out_rate != out_rate
470 || self->resampler_in_rate != (guint) self->info.rate))
471 gst_audio_resampler_update (self->resampler, self->info.rate, out_rate,
472 NULL);
473 else if (!self->resampler)
474 self->resampler =
475 gst_audio_resampler_new (GST_AUDIO_RESAMPLER_METHOD_LINEAR,
476 GST_AUDIO_RESAMPLER_FLAG_NONE, self->info.finfo->format,
477 self->info.channels, self->info.rate, out_rate, NULL);
478
479 self->resampler_in_rate = self->info.rate;
480 self->resampler_out_rate = out_rate;
481 } else if (self->resampler) {
482 gst_audio_resampler_free (self->resampler);
483 self->resampler = NULL;
484 }
485
486 if (new_segment->rate < 0)
487 gst_audio_stream_align_set_rate (self->stream_align, -48000);
488 }
489
490 return GST_BASE_SINK_CLASS (parent_class)->event (bsink, event);
491 }
492
493 static GstFlowReturn
gst_decklink_audio_sink_render(GstBaseSink * bsink,GstBuffer * buffer)494 gst_decklink_audio_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
495 {
496 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK_CAST (bsink);
497 GstDecklinkVideoSink *video_sink;
498 GstFlowReturn flow_ret;
499 HRESULT ret;
500 GstClockTime timestamp, duration;
501 GstClockTime running_time, running_time_duration;
502 GstClockTime schedule_time, schedule_time_duration;
503 GstClockTime latency, render_delay;
504 GstClockTimeDiff ts_offset;
505 GstMapInfo map_info;
506 const guint8 *data;
507 gsize len, written_all;
508 gboolean discont;
509
510 GST_DEBUG_OBJECT (self, "Rendering buffer %p", buffer);
511
512 // FIXME: Handle no timestamps
513 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
514 return GST_FLOW_ERROR;
515 }
516
517 if (GST_BASE_SINK_CAST (self)->flushing) {
518 return GST_FLOW_FLUSHING;
519 }
520 // If we're called before output is actually started, start pre-rolling
521 if (!self->output->started) {
522 self->output->output->BeginAudioPreroll ();
523 }
524
525 video_sink =
526 GST_DECKLINK_VIDEO_SINK (gst_object_ref (self->output->videosink));
527
528 timestamp = GST_BUFFER_TIMESTAMP (buffer);
529 duration = GST_BUFFER_DURATION (buffer);
530 discont = gst_audio_stream_align_process (self->stream_align,
531 GST_BUFFER_IS_DISCONT (buffer), timestamp,
532 gst_buffer_get_size (buffer) / self->info.bpf, ×tamp, &duration,
533 NULL);
534
535 if (discont && self->resampler)
536 gst_audio_resampler_reset (self->resampler);
537
538 if (GST_BASE_SINK_CAST (self)->segment.rate < 0.0) {
539 GstMapInfo out_map;
540 gint out_frames = gst_buffer_get_size (buffer) / self->info.bpf;
541
542 buffer = gst_buffer_make_writable (gst_buffer_ref (buffer));
543
544 gst_buffer_map (buffer, &out_map, GST_MAP_READWRITE);
545 if (self->info.finfo->format == GST_AUDIO_FORMAT_S16) {
546 gint16 *swap_data = (gint16 *) out_map.data;
547 gint16 *swap_data_end =
548 swap_data + (out_frames - 1) * self->info.channels;
549 gint16 swap_tmp[16];
550
551 while (out_frames > 0) {
552 memcpy (&swap_tmp, swap_data, self->info.bpf);
553 memcpy (swap_data, swap_data_end, self->info.bpf);
554 memcpy (swap_data_end, &swap_tmp, self->info.bpf);
555
556 swap_data += self->info.channels;
557 swap_data_end -= self->info.channels;
558
559 out_frames -= 2;
560 }
561 } else {
562 gint32 *swap_data = (gint32 *) out_map.data;
563 gint32 *swap_data_end =
564 swap_data + (out_frames - 1) * self->info.channels;
565 gint32 swap_tmp[16];
566
567 while (out_frames > 0) {
568 memcpy (&swap_tmp, swap_data, self->info.bpf);
569 memcpy (swap_data, swap_data_end, self->info.bpf);
570 memcpy (swap_data_end, &swap_tmp, self->info.bpf);
571
572 swap_data += self->info.channels;
573 swap_data_end -= self->info.channels;
574
575 out_frames -= 2;
576 }
577 }
578 gst_buffer_unmap (buffer, &out_map);
579 } else {
580 gst_buffer_ref (buffer);
581 }
582
583 if (self->resampler) {
584 gint in_frames = gst_buffer_get_size (buffer) / self->info.bpf;
585 gint out_frames =
586 gst_audio_resampler_get_out_frames (self->resampler, in_frames);
587 GstBuffer *out_buf = gst_buffer_new_and_alloc (out_frames * self->info.bpf);
588 GstMapInfo out_map;
589
590 gst_buffer_map (buffer, &map_info, GST_MAP_READ);
591 gst_buffer_map (out_buf, &out_map, GST_MAP_READWRITE);
592
593 gst_audio_resampler_resample (self->resampler, (gpointer *) & map_info.data,
594 in_frames, (gpointer *) & out_map.data, out_frames);
595
596 gst_buffer_unmap (out_buf, &out_map);
597 gst_buffer_unmap (buffer, &map_info);
598 buffer = out_buf;
599 }
600
601 gst_buffer_map (buffer, &map_info, GST_MAP_READ);
602 data = map_info.data;
603 len = map_info.size / self->info.bpf;
604 written_all = 0;
605
606 do {
607 GstClockTime timestamp_now =
608 timestamp + gst_util_uint64_scale (written_all, GST_SECOND,
609 self->info.rate);
610 guint32 buffered_samples;
611 GstClockTime buffered_time;
612 guint32 written = 0;
613 GstClock *clock;
614 GstClockTimeDiff clock_ahead;
615
616 if (GST_BASE_SINK_CAST (self)->flushing) {
617 flow_ret = GST_FLOW_FLUSHING;
618 break;
619 }
620
621 running_time =
622 gst_segment_to_running_time (&GST_BASE_SINK_CAST (self)->segment,
623 GST_FORMAT_TIME, timestamp_now);
624 running_time_duration =
625 gst_segment_to_running_time (&GST_BASE_SINK_CAST (self)->segment,
626 GST_FORMAT_TIME, timestamp_now + duration) - running_time;
627
628 /* See gst_base_sink_adjust_time() */
629 latency = gst_base_sink_get_latency (bsink);
630 render_delay = gst_base_sink_get_render_delay (bsink);
631 ts_offset = gst_base_sink_get_ts_offset (bsink);
632 running_time += latency;
633
634 if (ts_offset < 0) {
635 ts_offset = -ts_offset;
636 if ((GstClockTime) ts_offset < running_time)
637 running_time -= ts_offset;
638 else
639 running_time = 0;
640 } else {
641 running_time += ts_offset;
642 }
643
644 if (running_time > render_delay)
645 running_time -= render_delay;
646 else
647 running_time = 0;
648
649 clock = gst_element_get_clock (GST_ELEMENT_CAST (self));
650 clock_ahead = 0;
651 if (clock) {
652 GstClockTime clock_now = gst_clock_get_time (clock);
653 GstClockTime base_time =
654 gst_element_get_base_time (GST_ELEMENT_CAST (self));
655 gst_object_unref (clock);
656 clock = NULL;
657
658 if (clock_now != GST_CLOCK_TIME_NONE && base_time != GST_CLOCK_TIME_NONE) {
659 GST_DEBUG_OBJECT (self,
660 "Clock time %" GST_TIME_FORMAT ", base time %" GST_TIME_FORMAT
661 ", target running time %" GST_TIME_FORMAT,
662 GST_TIME_ARGS (clock_now), GST_TIME_ARGS (base_time),
663 GST_TIME_ARGS (running_time));
664 if (clock_now > base_time)
665 clock_now -= base_time;
666 else
667 clock_now = 0;
668
669 clock_ahead = running_time - clock_now;
670 }
671 }
672
673 GST_DEBUG_OBJECT (self,
674 "Ahead %" GST_STIME_FORMAT " of the clock running time",
675 GST_STIME_ARGS (clock_ahead));
676
677 if (self->output->
678 output->GetBufferedAudioSampleFrameCount (&buffered_samples) != S_OK)
679 buffered_samples = 0;
680
681 buffered_time =
682 gst_util_uint64_scale (buffered_samples, GST_SECOND, self->info.rate);
683 buffered_time /= ABS (GST_BASE_SINK_CAST (self)->segment.rate);
684 GST_DEBUG_OBJECT (self,
685 "Buffered %" GST_TIME_FORMAT " in the driver (%u samples)",
686 GST_TIME_ARGS (buffered_time), buffered_samples);
687
688 {
689 GstClockTimeDiff buffered_ahead_of_clock_ahead =
690 GST_CLOCK_DIFF (clock_ahead, buffered_time);
691
692 GST_DEBUG_OBJECT (self, "driver is %" GST_STIME_FORMAT " ahead of the "
693 "expected clock", GST_STIME_ARGS (buffered_ahead_of_clock_ahead));
694 /* we don't want to store too much data in the driver as decklink
695 * doesn't seem to actually use our provided timestamps to perform its
696 * own synchronisation. It seems to count samples instead. */
697 /* FIXME: do we need to split buffers? */
698 if (buffered_ahead_of_clock_ahead > 0 &&
699 buffered_ahead_of_clock_ahead >
700 gst_base_sink_get_max_lateness (bsink)) {
701 GST_DEBUG_OBJECT (self,
702 "Dropping buffer that is %" GST_STIME_FORMAT " too late",
703 GST_STIME_ARGS (buffered_ahead_of_clock_ahead));
704 if (self->resampler)
705 gst_audio_resampler_reset (self->resampler);
706 flow_ret = GST_FLOW_OK;
707 break;
708 }
709 }
710
711 // We start waiting once we have more than buffer-time buffered
712 if (((GstClockTime) clock_ahead) > self->buffer_time) {
713 GstClockReturn clock_ret;
714 GstClockTime wait_time = running_time;
715
716 GST_DEBUG_OBJECT (self,
717 "Buffered enough, wait for preroll or the clock or flushing. "
718 "Configured buffer time: %" GST_TIME_FORMAT,
719 GST_TIME_ARGS (self->buffer_time));
720
721 if (wait_time < self->buffer_time)
722 wait_time = 0;
723 else
724 wait_time -= self->buffer_time;
725
726 flow_ret =
727 gst_base_sink_do_preroll (GST_BASE_SINK_CAST (self),
728 GST_MINI_OBJECT_CAST (buffer));
729 if (flow_ret != GST_FLOW_OK)
730 break;
731
732 clock_ret =
733 gst_base_sink_wait_clock (GST_BASE_SINK_CAST (self), wait_time, NULL);
734 if (GST_BASE_SINK_CAST (self)->flushing) {
735 flow_ret = GST_FLOW_FLUSHING;
736 break;
737 }
738 // Rerun the whole loop again
739 if (clock_ret == GST_CLOCK_UNSCHEDULED)
740 continue;
741 }
742
743 schedule_time = running_time;
744 schedule_time_duration = running_time_duration;
745
746 gst_decklink_video_sink_convert_to_internal_clock (video_sink,
747 &schedule_time, &schedule_time_duration);
748
749 GST_LOG_OBJECT (self, "Scheduling audio samples at %" GST_TIME_FORMAT
750 " with duration %" GST_TIME_FORMAT, GST_TIME_ARGS (schedule_time),
751 GST_TIME_ARGS (schedule_time_duration));
752
753 ret = self->output->output->ScheduleAudioSamples ((void *) data, len,
754 schedule_time, GST_SECOND, &written);
755 if (ret != S_OK) {
756 bool is_running = true;
757 self->output->output->IsScheduledPlaybackRunning (&is_running);
758
759 if (is_running && !GST_BASE_SINK_CAST (self)->flushing
760 && self->output->started) {
761 GST_ELEMENT_ERROR (self, STREAM, FAILED, (NULL),
762 ("Failed to schedule frame: 0x%08lx", (unsigned long) ret));
763 flow_ret = GST_FLOW_ERROR;
764 break;
765 } else {
766 // Ignore the error and go out of the loop here, we're shutting down
767 // or are not started yet and there's nothing we can do at this point
768 GST_INFO_OBJECT (self,
769 "Ignoring scheduling error 0x%08x because we're not started yet"
770 " or not anymore", (guint) ret);
771 flow_ret = GST_FLOW_OK;
772 break;
773 }
774 }
775
776 len -= written;
777 data += written * self->info.bpf;
778 if (self->resampler)
779 written_all += written * ABS (GST_BASE_SINK_CAST (self)->segment.rate);
780 else
781 written_all += written;
782
783 flow_ret = GST_FLOW_OK;
784 } while (len > 0);
785
786 gst_buffer_unmap (buffer, &map_info);
787 gst_buffer_unref (buffer);
788
789 GST_DEBUG_OBJECT (self, "Returning %s", gst_flow_get_name (flow_ret));
790
791 return flow_ret;
792 }
793
794 static gboolean
gst_decklink_audio_sink_open(GstBaseSink * bsink)795 gst_decklink_audio_sink_open (GstBaseSink * bsink)
796 {
797 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK_CAST (bsink);
798
799 GST_DEBUG_OBJECT (self, "Starting");
800
801 self->output =
802 gst_decklink_acquire_nth_output (self->device_number,
803 GST_ELEMENT_CAST (self), TRUE);
804 if (!self->output) {
805 GST_ERROR_OBJECT (self, "Failed to acquire output");
806 return FALSE;
807 }
808
809 g_object_notify (G_OBJECT (self), "hw-serial-number");
810
811 return TRUE;
812 }
813
814 static gboolean
gst_decklink_audio_sink_close(GstBaseSink * bsink)815 gst_decklink_audio_sink_close (GstBaseSink * bsink)
816 {
817 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK_CAST (bsink);
818
819 GST_DEBUG_OBJECT (self, "Closing");
820
821 if (self->output) {
822 g_mutex_lock (&self->output->lock);
823 self->output->mode = NULL;
824 self->output->audio_enabled = FALSE;
825 if (self->output->start_scheduled_playback && self->output->videosink)
826 self->output->start_scheduled_playback (self->output->videosink);
827 g_mutex_unlock (&self->output->lock);
828
829 self->output->output->DisableAudioOutput ();
830 gst_decklink_release_nth_output (self->device_number,
831 GST_ELEMENT_CAST (self), TRUE);
832 self->output = NULL;
833 }
834
835 return TRUE;
836 }
837
838 static gboolean
gst_decklink_audio_sink_stop(GstDecklinkAudioSink * self)839 gst_decklink_audio_sink_stop (GstDecklinkAudioSink * self)
840 {
841 GST_DEBUG_OBJECT (self, "Stopping");
842
843 if (self->output && self->output->audio_enabled) {
844 g_mutex_lock (&self->output->lock);
845 self->output->audio_enabled = FALSE;
846 g_mutex_unlock (&self->output->lock);
847
848 self->output->output->DisableAudioOutput ();
849 }
850
851 if (self->resampler) {
852 gst_audio_resampler_free (self->resampler);
853 self->resampler = NULL;
854 }
855
856 return TRUE;
857 }
858
859 static gboolean
gst_decklink_audio_sink_unlock_stop(GstBaseSink * bsink)860 gst_decklink_audio_sink_unlock_stop (GstBaseSink * bsink)
861 {
862 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK (bsink);
863
864 if (self->output) {
865 self->output->output->FlushBufferedAudioSamples ();
866 }
867
868 return TRUE;
869 }
870
871 static void
gst_decklink_audio_sink_get_times(GstBaseSink * bsink,GstBuffer * buffer,GstClockTime * start,GstClockTime * end)872 gst_decklink_audio_sink_get_times (GstBaseSink * bsink, GstBuffer * buffer,
873 GstClockTime * start, GstClockTime * end)
874 {
875 /* our clock sync is a bit too much for the base class to handle so
876 * we implement it ourselves. */
877 *start = GST_CLOCK_TIME_NONE;
878 *end = GST_CLOCK_TIME_NONE;
879 }
880
881 static GstStateChangeReturn
gst_decklink_audio_sink_change_state(GstElement * element,GstStateChange transition)882 gst_decklink_audio_sink_change_state (GstElement * element,
883 GstStateChange transition)
884 {
885 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK_CAST (element);
886 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
887
888 switch (transition) {
889 case GST_STATE_CHANGE_READY_TO_PAUSED:
890 GST_OBJECT_LOCK (self);
891 gst_audio_stream_align_mark_discont (self->stream_align);
892 GST_OBJECT_UNLOCK (self);
893
894 g_mutex_lock (&self->output->lock);
895 if (self->output->start_scheduled_playback)
896 self->output->start_scheduled_playback (self->output->videosink);
897 g_mutex_unlock (&self->output->lock);
898 break;
899 case GST_STATE_CHANGE_PAUSED_TO_READY:
900 gst_decklink_audio_sink_stop (self);
901 break;
902 default:
903 break;
904 }
905
906 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
907 if (ret == GST_STATE_CHANGE_FAILURE)
908 return ret;
909
910 switch (transition) {
911 default:
912 break;
913 }
914
915 return ret;
916 }
917
918 static GstClock *
gst_decklink_audio_sink_provide_clock(GstElement * element)919 gst_decklink_audio_sink_provide_clock (GstElement * element)
920 {
921 GstDecklinkAudioSink *self = GST_DECKLINK_AUDIO_SINK_CAST (element);
922
923 if (!self->output)
924 return NULL;
925
926 return GST_CLOCK_CAST (gst_object_ref (self->output->clock));
927 }
928