2 * Copyright (C) 2011 David Schleef <ds@entropywave.com>
3 * Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
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.
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.
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.
21 * SECTION:element-decklinkaudiosrc
22 * @short_description: Inputs Audio from a BlackMagic DeckLink Device
23 * @see_also: decklinkvideosrc
25 * Capture Video and Audio from a BlackMagic DeckLink Device. Can only be used
26 * in conjunction with decklinkvideosink.
31 * decklinkvideosrc device-number=0 mode=1080p25 ! autovideosink \
32 * decklinkaudiosrc device-number=0 ! autoaudiosink
34 * Capturing 1080p25 video and audio from the SDI-In of Card 0. Devices are numbered
42 #include "gstdecklinkaudiosrc.h"
43 #include "gstdecklinkvideosrc.h"
46 GST_DEBUG_CATEGORY_STATIC (gst_decklink_audio_src_debug);
47 #define GST_CAT_DEFAULT gst_decklink_audio_src_debug
49 #define DEFAULT_CONNECTION (GST_DECKLINK_AUDIO_CONNECTION_AUTO)
50 #define DEFAULT_BUFFER_SIZE (5)
52 #define DEFAULT_ALIGNMENT_THRESHOLD (40 * GST_MSECOND)
53 #define DEFAULT_DISCONT_WAIT (1 * GST_SECOND)
54 #define DEFAULT_CHANNELS (GST_DECKLINK_AUDIO_CHANNELS_2)
57 #define ABSDIFF(x, y) ( (x) > (y) ? ((x) - (y)) : ((y) - (x)) )
65 PROP_ALIGNMENT_THRESHOLD,
72 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("src",
76 ("audio/x-raw, format={S16LE,S32LE}, channels=2, rate=48000, "
78 "audio/x-raw, format={S16LE,S32LE}, channels={8,16}, channel-mask=(bitmask)0, rate=48000, "
84 IDeckLinkAudioInputPacket *packet;
85 GstClockTime timestamp;
86 GstClockTime stream_timestamp;
87 GstClockTime stream_duration;
88 GstClockTime hardware_timestamp;
89 GstClockTime hardware_duration;
94 capture_packet_clear (CapturePacket * packet)
96 packet->packet->Release ();
97 memset (packet, 0, sizeof (*packet));
102 IDeckLinkAudioInputPacket *packet;
103 IDeckLinkInput *input;
107 audio_packet_free (void *data)
109 AudioPacket *packet = (AudioPacket *) data;
111 packet->packet->Release ();
112 packet->input->Release ();
116 static void gst_decklink_audio_src_set_property (GObject * object,
117 guint property_id, const GValue * value, GParamSpec * pspec);
118 static void gst_decklink_audio_src_get_property (GObject * object,
119 guint property_id, GValue * value, GParamSpec * pspec);
120 static void gst_decklink_audio_src_finalize (GObject * object);
122 static GstStateChangeReturn
123 gst_decklink_audio_src_change_state (GstElement * element,
124 GstStateChange transition);
126 static gboolean gst_decklink_audio_src_unlock (GstBaseSrc * bsrc);
127 static gboolean gst_decklink_audio_src_unlock_stop (GstBaseSrc * bsrc);
128 static GstCaps *gst_decklink_audio_src_get_caps (GstBaseSrc * bsrc,
130 static gboolean gst_decklink_audio_src_query (GstBaseSrc * bsrc,
133 static GstFlowReturn gst_decklink_audio_src_create (GstPushSrc * psrc,
134 GstBuffer ** buffer);
136 static gboolean gst_decklink_audio_src_open (GstDecklinkAudioSrc * self);
137 static gboolean gst_decklink_audio_src_close (GstDecklinkAudioSrc * self);
139 static gboolean gst_decklink_audio_src_stop (GstDecklinkAudioSrc * self);
141 #define parent_class gst_decklink_audio_src_parent_class
142 G_DEFINE_TYPE (GstDecklinkAudioSrc, gst_decklink_audio_src, GST_TYPE_PUSH_SRC);
145 gst_decklink_audio_src_class_init (GstDecklinkAudioSrcClass * klass)
147 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
148 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
149 GstBaseSrcClass *basesrc_class = GST_BASE_SRC_CLASS (klass);
150 GstPushSrcClass *pushsrc_class = GST_PUSH_SRC_CLASS (klass);
152 gobject_class->set_property = gst_decklink_audio_src_set_property;
153 gobject_class->get_property = gst_decklink_audio_src_get_property;
154 gobject_class->finalize = gst_decklink_audio_src_finalize;
156 element_class->change_state =
157 GST_DEBUG_FUNCPTR (gst_decklink_audio_src_change_state);
159 basesrc_class->query = GST_DEBUG_FUNCPTR (gst_decklink_audio_src_query);
160 basesrc_class->negotiate = NULL;
161 basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_decklink_audio_src_get_caps);
162 basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_decklink_audio_src_unlock);
163 basesrc_class->unlock_stop =
164 GST_DEBUG_FUNCPTR (gst_decklink_audio_src_unlock_stop);
166 pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_decklink_audio_src_create);
168 g_object_class_install_property (gobject_class, PROP_CONNECTION,
169 g_param_spec_enum ("connection", "Connection",
170 "Audio input connection to use",
171 GST_TYPE_DECKLINK_AUDIO_CONNECTION, DEFAULT_CONNECTION,
172 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
173 G_PARAM_CONSTRUCT)));
175 g_object_class_install_property (gobject_class, PROP_DEVICE_NUMBER,
176 g_param_spec_int ("device-number", "Device number",
177 "Output device instance to use", 0, G_MAXINT, 0,
178 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
179 G_PARAM_CONSTRUCT)));
181 g_object_class_install_property (gobject_class, PROP_ALIGNMENT_THRESHOLD,
182 g_param_spec_uint64 ("alignment-threshold", "Alignment Threshold",
183 "Timestamp alignment threshold in nanoseconds", 0,
184 G_MAXUINT64 - 1, DEFAULT_ALIGNMENT_THRESHOLD,
185 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
187 g_object_class_install_property (gobject_class, PROP_DISCONT_WAIT,
188 g_param_spec_uint64 ("discont-wait", "Discont Wait",
189 "Window of time in nanoseconds to wait before "
190 "creating a discontinuity", 0,
191 G_MAXUINT64 - 1, DEFAULT_DISCONT_WAIT,
192 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
194 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
195 g_param_spec_uint ("buffer-size", "Buffer Size",
196 "Size of internal buffer in number of video frames", 1,
197 G_MAXINT, DEFAULT_BUFFER_SIZE,
198 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
200 g_object_class_install_property (gobject_class, PROP_CHANNELS,
201 g_param_spec_enum ("channels", "Channels",
203 GST_TYPE_DECKLINK_AUDIO_CHANNELS, DEFAULT_CHANNELS,
204 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
205 G_PARAM_CONSTRUCT)));
207 g_object_class_install_property (gobject_class, PROP_HW_SERIAL_NUMBER,
208 g_param_spec_string ("hw-serial-number", "Hardware serial number",
209 "The serial number (hardware ID) of the Decklink card",
210 NULL, (GParamFlags) (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
212 gst_element_class_add_static_pad_template (element_class, &sink_template);
214 gst_element_class_set_static_metadata (element_class, "Decklink Audio Source",
215 "Audio/Source/Hardware", "Decklink Source",
216 "David Schleef <ds@entropywave.com>, "
217 "Sebastian Dröge <sebastian@centricular.com>");
219 GST_DEBUG_CATEGORY_INIT (gst_decklink_audio_src_debug, "decklinkaudiosrc",
220 0, "debug category for decklinkaudiosrc element");
224 gst_decklink_audio_src_init (GstDecklinkAudioSrc * self)
226 self->device_number = 0;
227 self->alignment_threshold = DEFAULT_ALIGNMENT_THRESHOLD;
228 self->discont_wait = DEFAULT_DISCONT_WAIT;
229 self->buffer_size = DEFAULT_BUFFER_SIZE;
230 self->channels = DEFAULT_CHANNELS;
232 gst_base_src_set_live (GST_BASE_SRC (self), TRUE);
233 gst_base_src_set_format (GST_BASE_SRC (self), GST_FORMAT_TIME);
235 gst_pad_use_fixed_caps (GST_BASE_SRC_PAD (self));
237 g_mutex_init (&self->lock);
238 g_cond_init (&self->cond);
240 self->current_packets =
241 gst_queue_array_new_for_struct (sizeof (CapturePacket),
242 DEFAULT_BUFFER_SIZE);
244 self->skipped_last = 0;
245 self->skip_from_timestamp = GST_CLOCK_TIME_NONE;
246 self->skip_to_timestamp = GST_CLOCK_TIME_NONE;
250 gst_decklink_audio_src_set_property (GObject * object, guint property_id,
251 const GValue * value, GParamSpec * pspec)
253 GstDecklinkAudioSrc *self = GST_DECKLINK_AUDIO_SRC_CAST (object);
255 switch (property_id) {
256 case PROP_CONNECTION:
258 (GstDecklinkAudioConnectionEnum) g_value_get_enum (value);
260 case PROP_DEVICE_NUMBER:
261 self->device_number = g_value_get_int (value);
263 case PROP_ALIGNMENT_THRESHOLD:
264 self->alignment_threshold = g_value_get_uint64 (value);
266 case PROP_DISCONT_WAIT:
267 self->discont_wait = g_value_get_uint64 (value);
269 case PROP_BUFFER_SIZE:
270 self->buffer_size = g_value_get_uint (value);
273 self->channels = (GstDecklinkAudioChannelsEnum) g_value_get_enum (value);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
282 gst_decklink_audio_src_get_property (GObject * object, guint property_id,
283 GValue * value, GParamSpec * pspec)
285 GstDecklinkAudioSrc *self = GST_DECKLINK_AUDIO_SRC_CAST (object);
287 switch (property_id) {
288 case PROP_CONNECTION:
289 g_value_set_enum (value, self->connection);
291 case PROP_DEVICE_NUMBER:
292 g_value_set_int (value, self->device_number);
294 case PROP_ALIGNMENT_THRESHOLD:
295 g_value_set_uint64 (value, self->alignment_threshold);
297 case PROP_DISCONT_WAIT:
298 g_value_set_uint64 (value, self->discont_wait);
300 case PROP_BUFFER_SIZE:
301 g_value_set_uint (value, self->buffer_size);
304 g_value_set_enum (value, self->channels);
306 case PROP_HW_SERIAL_NUMBER:
308 g_value_set_string (value, self->input->hw_serial_number);
310 g_value_set_string (value, NULL);
313 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
319 gst_decklink_audio_src_finalize (GObject * object)
321 GstDecklinkAudioSrc *self = GST_DECKLINK_AUDIO_SRC_CAST (object);
323 g_mutex_clear (&self->lock);
324 g_cond_clear (&self->cond);
325 if (self->current_packets) {
326 while (gst_queue_array_get_length (self->current_packets) > 0) {
327 CapturePacket *tmp = (CapturePacket *)
328 gst_queue_array_pop_head_struct (self->current_packets);
329 capture_packet_clear (tmp);
331 gst_queue_array_free (self->current_packets);
332 self->current_packets = NULL;
335 G_OBJECT_CLASS (parent_class)->finalize (object);
339 gst_decklink_audio_src_start (GstDecklinkAudioSrc * self)
341 BMDAudioSampleType sample_depth;
343 BMDAudioConnection conn = (BMDAudioConnection) - 1;
344 GstCaps *allowed_caps, *caps;
346 g_mutex_lock (&self->input->lock);
347 if (self->input->audio_enabled) {
348 g_mutex_unlock (&self->input->lock);
351 g_mutex_unlock (&self->input->lock);
353 /* Negotiate the format / sample depth with downstream */
354 allowed_caps = gst_pad_get_allowed_caps (GST_BASE_SRC_PAD (self));
356 allowed_caps = gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD (self));
358 sample_depth = bmdAudioSampleType32bitInteger;
359 if (!gst_caps_is_empty (allowed_caps)) {
362 allowed_caps = gst_caps_simplify (allowed_caps);
364 s = gst_caps_get_structure (allowed_caps, 0);
366 /* If it's not a string then both formats are supported */
367 if (gst_structure_has_field_typed (s, "format", G_TYPE_STRING)) {
368 const gchar *format = gst_structure_get_string (s, "format");
369 if (g_str_equal (format, "S16LE")) {
370 sample_depth = bmdAudioSampleType16bitInteger;
374 gst_caps_unref (allowed_caps);
376 switch (self->connection) {
377 case GST_DECKLINK_AUDIO_CONNECTION_AUTO:{
378 GstElement *videosrc = NULL;
379 GstDecklinkConnectionEnum vconn;
381 // Try to get the connection from the videosrc and try
382 // to select a sensible audio connection based on that
383 g_mutex_lock (&self->input->lock);
384 if (self->input->videosrc)
385 videosrc = GST_ELEMENT_CAST (gst_object_ref (self->input->videosrc));
386 g_mutex_unlock (&self->input->lock);
389 g_object_get (videosrc, "connection", &vconn, NULL);
390 gst_object_unref (videosrc);
393 case GST_DECKLINK_CONNECTION_SDI:
394 conn = bmdAudioConnectionEmbedded;
396 case GST_DECKLINK_CONNECTION_HDMI:
397 conn = bmdAudioConnectionEmbedded;
399 case GST_DECKLINK_CONNECTION_OPTICAL_SDI:
400 conn = bmdAudioConnectionEmbedded;
402 case GST_DECKLINK_CONNECTION_COMPONENT:
403 conn = bmdAudioConnectionAnalog;
405 case GST_DECKLINK_CONNECTION_COMPOSITE:
406 conn = bmdAudioConnectionAnalog;
408 case GST_DECKLINK_CONNECTION_SVIDEO:
409 conn = bmdAudioConnectionAnalog;
419 case GST_DECKLINK_AUDIO_CONNECTION_EMBEDDED:
420 conn = bmdAudioConnectionEmbedded;
422 case GST_DECKLINK_AUDIO_CONNECTION_AES_EBU:
423 conn = bmdAudioConnectionAESEBU;
425 case GST_DECKLINK_AUDIO_CONNECTION_ANALOG:
426 conn = bmdAudioConnectionAnalog;
428 case GST_DECKLINK_AUDIO_CONNECTION_ANALOG_XLR:
429 conn = bmdAudioConnectionAnalogXLR;
431 case GST_DECKLINK_AUDIO_CONNECTION_ANALOG_RCA:
432 conn = bmdAudioConnectionAnalogRCA;
435 g_assert_not_reached ();
439 if (conn != (BMDAudioConnection) - 1) {
441 self->input->config->SetInt (bmdDeckLinkConfigAudioInputConnection,
444 GST_ERROR ("set configuration (audio input connection): 0x%08lx",
445 (unsigned long) ret);
450 ret = self->input->input->EnableAudioInput (bmdAudioSampleRate48kHz,
451 sample_depth, self->channels_found);
453 GST_WARNING_OBJECT (self, "Failed to enable audio input: 0x%08lx",
454 (unsigned long) ret);
457 gst_audio_info_set_format (&self->info,
459 bmdAudioSampleType16bitInteger ? GST_AUDIO_FORMAT_S16LE :
460 GST_AUDIO_FORMAT_S32LE, 48000, self->channels_found, NULL);
462 g_mutex_lock (&self->input->lock);
463 self->input->audio_enabled = TRUE;
464 if (self->input->start_streams && self->input->videosrc)
465 self->input->start_streams (self->input->videosrc);
466 g_mutex_unlock (&self->input->lock);
468 caps = gst_audio_info_to_caps (&self->info);
469 if (!gst_base_src_set_caps (GST_BASE_SRC (self), caps)) {
470 gst_caps_unref (caps);
471 GST_WARNING_OBJECT (self, "Failed to set caps");
474 gst_caps_unref (caps);
476 self->skipped_last = 0;
477 self->skip_from_timestamp = GST_CLOCK_TIME_NONE;
478 self->skip_to_timestamp = GST_CLOCK_TIME_NONE;
484 gst_decklink_audio_src_got_packet (GstElement * element,
485 IDeckLinkAudioInputPacket * packet, GstClockTime capture_time,
486 GstClockTime stream_time, GstClockTime stream_duration,
487 GstClockTime hardware_time, GstClockTime hardware_duration,
490 GstDecklinkAudioSrc *self = GST_DECKLINK_AUDIO_SRC_CAST (element);
491 GstClockTime timestamp;
493 GST_LOG_OBJECT (self,
494 "Got audio packet at %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT
495 ", no signal %d", GST_TIME_ARGS (capture_time),
496 GST_TIME_ARGS (stream_time), no_signal);
498 g_mutex_lock (&self->input->lock);
499 if (self->input->videosrc) {
500 GstDecklinkVideoSrc *videosrc =
501 GST_DECKLINK_VIDEO_SRC_CAST (gst_object_ref (self->input->videosrc));
503 if (videosrc->drop_no_signal_frames && no_signal) {
504 g_mutex_unlock (&self->input->lock);
508 if (videosrc->first_time == GST_CLOCK_TIME_NONE)
509 videosrc->first_time = stream_time;
511 if (videosrc->skip_first_time > 0
512 && stream_time - videosrc->first_time < videosrc->skip_first_time) {
513 GST_DEBUG_OBJECT (self,
514 "Skipping frame as requested: %" GST_TIME_FORMAT " < %"
515 GST_TIME_FORMAT, GST_TIME_ARGS (stream_time),
516 GST_TIME_ARGS (videosrc->skip_first_time + videosrc->first_time));
517 g_mutex_unlock (&self->input->lock);
521 if (videosrc->output_stream_time)
522 timestamp = stream_time;
524 timestamp = gst_clock_adjust_with_calibration (NULL, stream_time,
525 videosrc->current_time_mapping.xbase,
526 videosrc->current_time_mapping.b, videosrc->current_time_mapping.num,
527 videosrc->current_time_mapping.den);
529 timestamp = capture_time;
531 g_mutex_unlock (&self->input->lock);
533 GST_LOG_OBJECT (self, "Converted times to %" GST_TIME_FORMAT,
534 GST_TIME_ARGS (timestamp));
536 g_mutex_lock (&self->lock);
537 if (!self->flushing) {
539 guint skipped_packets = 0;
541 while (gst_queue_array_get_length (self->current_packets) >=
543 CapturePacket *tmp = (CapturePacket *)
544 gst_queue_array_pop_head_struct (self->current_packets);
545 if (skipped_packets == 0 && self->skipped_last == 0)
546 self->skip_from_timestamp = tmp->timestamp;
548 self->skip_to_timestamp = tmp->timestamp;
549 capture_packet_clear (tmp);
552 if (self->skipped_last == 0 && skipped_packets > 0) {
553 GST_WARNING_OBJECT (self, "Starting to drop audio packets");
556 if (skipped_packets == 0 && self->skipped_last > 0) {
557 GST_ELEMENT_WARNING_WITH_DETAILS (self,
559 ("Dropped %u old packets from %" GST_TIME_FORMAT " to %"
560 GST_TIME_FORMAT, self->skipped_last,
561 GST_TIME_ARGS (self->skip_from_timestamp),
562 GST_TIME_ARGS (self->skip_to_timestamp)),
564 ("dropped", G_TYPE_UINT, self->skipped_last,
565 "from", G_TYPE_UINT64, self->skip_from_timestamp,
566 "to", G_TYPE_UINT64, self->skip_to_timestamp, NULL));
567 self->skipped_last = 0;
569 self->skipped_last += skipped_packets;
571 memset (&p, 0, sizeof (p));
573 p.timestamp = timestamp;
574 p.stream_timestamp = stream_time;
575 p.stream_duration = stream_duration;
576 p.hardware_timestamp = hardware_time;
577 p.hardware_duration = hardware_duration;
578 p.no_signal = no_signal;
580 gst_queue_array_push_tail_struct (self->current_packets, &p);
581 g_cond_signal (&self->cond);
583 g_mutex_unlock (&self->lock);
587 gst_decklink_audio_src_create (GstPushSrc * bsrc, GstBuffer ** buffer)
589 GstDecklinkAudioSrc *self = GST_DECKLINK_AUDIO_SRC_CAST (bsrc);
590 GstFlowReturn flow_ret = GST_FLOW_OK;
596 GstClockTime timestamp, duration;
597 GstClockTime start_time, end_time;
598 guint64 start_offset, end_offset;
599 gboolean discont = FALSE;
600 static GstStaticCaps stream_reference =
601 GST_STATIC_CAPS ("timestamp/x-decklink-stream");
602 static GstStaticCaps hardware_reference =
603 GST_STATIC_CAPS ("timestamp/x-decklink-hardware");
605 if (!gst_decklink_audio_src_start (self)) {
606 return GST_FLOW_NOT_NEGOTIATED;
610 g_mutex_lock (&self->lock);
611 while (gst_queue_array_is_empty (self->current_packets) && !self->flushing) {
612 g_cond_wait (&self->cond, &self->lock);
615 if (self->flushing) {
616 GST_DEBUG_OBJECT (self, "Flushing");
617 g_mutex_unlock (&self->lock);
618 return GST_FLOW_FLUSHING;
621 p = *(CapturePacket *)
622 gst_queue_array_pop_head_struct (self->current_packets);
623 g_mutex_unlock (&self->lock);
625 p.packet->GetBytes ((gpointer *) & data);
626 sample_count = p.packet->GetSampleFrameCount ();
627 data_size = self->info.bpf * sample_count;
629 if (p.timestamp == GST_CLOCK_TIME_NONE && self->next_offset == (guint64) - 1) {
630 GST_DEBUG_OBJECT (self,
631 "Got packet without timestamp before initial "
632 "timestamp after discont - dropping");
633 capture_packet_clear (&p);
637 ap = (AudioPacket *) g_malloc0 (sizeof (AudioPacket));
640 gst_buffer_new_wrapped_full ((GstMemoryFlags) GST_MEMORY_FLAG_READONLY,
641 (gpointer) data, data_size, 0, data_size, ap,
642 (GDestroyNotify) audio_packet_free);
644 ap->packet = p.packet;
646 ap->input = self->input->input;
647 ap->input->AddRef ();
649 timestamp = p.timestamp;
651 // Jitter and discontinuity handling, based on audiobasesrc
652 start_time = timestamp;
654 // Convert to the sample numbers
656 gst_util_uint64_scale (start_time, self->info.rate, GST_SECOND);
657 // Convert back to round down to a sample multiple and get rid of rounding errors
658 start_time = gst_util_uint64_scale (start_offset, GST_SECOND, self->info.rate);
660 end_offset = start_offset + sample_count;
661 end_time = gst_util_uint64_scale_int (end_offset, GST_SECOND,
664 duration = end_time - start_time;
666 if (self->next_offset == (guint64) - 1) {
669 guint64 diff, max_sample_diff;
672 if (start_offset <= self->next_offset)
673 diff = self->next_offset - start_offset;
675 diff = start_offset - self->next_offset;
678 gst_util_uint64_scale_int (self->alignment_threshold, self->info.rate,
682 if (self->alignment_threshold > 0
683 && self->alignment_threshold != GST_CLOCK_TIME_NONE
684 && G_UNLIKELY (diff >= max_sample_diff)) {
685 if (self->discont_wait > 0) {
686 if (self->discont_time == GST_CLOCK_TIME_NONE) {
687 self->discont_time = start_time;
688 } else if (start_time - self->discont_time >= self->discont_wait) {
690 self->discont_time = GST_CLOCK_TIME_NONE;
695 } else if (G_UNLIKELY (self->discont_time != GST_CLOCK_TIME_NONE)) {
696 // we have had a discont, but are now back on track!
697 self->discont_time = GST_CLOCK_TIME_NONE;
702 // Have discont, need resync and use the capture timestamps
703 if (self->next_offset != (guint64) - 1)
704 GST_INFO_OBJECT (self, "Have discont. Expected %"
705 G_GUINT64_FORMAT ", got %" G_GUINT64_FORMAT,
706 self->next_offset, start_offset);
707 GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_DISCONT);
708 self->next_offset = end_offset;
709 // Got a discont and adjusted, reset the discont_time marker.
710 self->discont_time = GST_CLOCK_TIME_NONE;
711 } else if (self->alignment_threshold == 0) {
712 // Don't align, just pass through timestamps
714 // No discont, just keep counting
716 gst_util_uint64_scale (self->next_offset, GST_SECOND, self->info.rate);
717 self->next_offset += sample_count;
719 gst_util_uint64_scale (self->next_offset, GST_SECOND,
720 self->info.rate) - timestamp;
723 // Detect gaps in stream time
724 self->processed += sample_count;
725 if (self->expected_stream_time != GST_CLOCK_TIME_NONE
726 && p.stream_timestamp == GST_CLOCK_TIME_NONE) {
727 /* We missed a frame. Extrapolate the timestamps */
728 p.stream_timestamp = self->expected_stream_time;
730 gst_util_uint64_scale_int (sample_count, GST_SECOND, self->info.rate);
732 if (self->last_hardware_time != GST_CLOCK_TIME_NONE
733 && p.hardware_timestamp == GST_CLOCK_TIME_NONE) {
734 /* This should always happen when the previous one also does, but let's
735 * have two separate checks just in case */
736 GstClockTime start_hw_offset, end_hw_offset;
738 gst_util_uint64_scale (self->last_hardware_time, self->info.rate,
740 end_hw_offset = start_hw_offset + sample_count;
741 p.hardware_timestamp =
742 gst_util_uint64_scale_int (end_hw_offset, GST_SECOND, self->info.rate);
743 /* Will be the same as the stream duration - reuse it */
744 p.hardware_duration = p.stream_duration;
747 if (p.stream_timestamp != GST_CLOCK_TIME_NONE) {
748 GstClockTime start_stream_time, end_stream_time;
750 start_stream_time = p.stream_timestamp;
753 gst_util_uint64_scale (start_stream_time, self->info.rate, GST_SECOND);
755 end_offset = start_offset + sample_count;
756 end_stream_time = gst_util_uint64_scale_int (end_offset, GST_SECOND,
759 /* With drop-frame we have gaps of 1 sample every now and then (rounding
760 * errors because of the samples-per-frame pattern which is not 100%
761 * accurate), and due to rounding errors in the calculations these can be
763 if (self->expected_stream_time != GST_CLOCK_TIME_NONE &&
764 ABSDIFF (self->expected_stream_time, p.stream_timestamp) >
765 gst_util_uint64_scale (2, GST_SECOND, self->info.rate)) {
767 GstClockTime running_time;
770 gst_util_uint64_scale (ABSDIFF (self->expected_stream_time,
771 p.stream_timestamp), self->info.rate, GST_SECOND);
773 gst_segment_to_running_time (&GST_BASE_SRC (self)->segment,
774 GST_FORMAT_TIME, timestamp);
777 gst_message_new_qos (GST_OBJECT (self), TRUE, running_time,
778 p.stream_timestamp, timestamp, duration);
779 gst_message_set_qos_stats (msg, GST_FORMAT_DEFAULT, self->processed,
781 gst_element_post_message (GST_ELEMENT (self), msg);
783 self->expected_stream_time = end_stream_time;
785 self->last_hardware_time = p.hardware_timestamp;
788 GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_GAP);
789 GST_BUFFER_TIMESTAMP (*buffer) = timestamp;
790 GST_BUFFER_DURATION (*buffer) = duration;
792 gst_buffer_add_reference_timestamp_meta (*buffer,
793 gst_static_caps_get (&stream_reference), p.stream_timestamp,
795 gst_buffer_add_reference_timestamp_meta (*buffer,
796 gst_static_caps_get (&hardware_reference), p.hardware_timestamp,
797 p.hardware_duration);
799 GST_DEBUG_OBJECT (self,
800 "Outputting buffer %p with timestamp %" GST_TIME_FORMAT " and duration %"
801 GST_TIME_FORMAT, *buffer, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buffer)),
802 GST_TIME_ARGS (GST_BUFFER_DURATION (*buffer)));
804 capture_packet_clear (&p);
810 gst_decklink_audio_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
812 GstDecklinkAudioSrc *self = GST_DECKLINK_AUDIO_SRC_CAST (bsrc);
813 GstCaps *caps, *template_caps;
814 const GstStructure *s;
817 channels = self->channels;
819 channels = self->channels_found;
821 template_caps = gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD (bsrc));
823 caps = template_caps;
826 s = gst_caps_get_structure (template_caps, 1);
828 s = gst_caps_get_structure (template_caps, 0);
830 caps = gst_caps_new_full (gst_structure_copy (s), NULL);
831 gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
832 gst_caps_unref (template_caps);
837 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
838 gst_caps_unref (caps);
846 gst_decklink_audio_src_query (GstBaseSrc * bsrc, GstQuery * query)
848 GstDecklinkAudioSrc *self = GST_DECKLINK_AUDIO_SRC_CAST (bsrc);
851 switch (GST_QUERY_TYPE (query)) {
852 case GST_QUERY_LATENCY:{
854 g_mutex_lock (&self->input->lock);
855 if (self->input->mode) {
856 GstClockTime min, max;
859 gst_util_uint64_scale_ceil (GST_SECOND, self->input->mode->fps_d,
860 self->input->mode->fps_n);
861 max = self->buffer_size * min;
863 gst_query_set_latency (query, TRUE, min, max);
868 g_mutex_unlock (&self->input->lock);
876 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
884 gst_decklink_audio_src_unlock (GstBaseSrc * bsrc)
886 GstDecklinkAudioSrc *self = GST_DECKLINK_AUDIO_SRC_CAST (bsrc);
888 g_mutex_lock (&self->lock);
889 self->flushing = TRUE;
890 g_cond_signal (&self->cond);
891 g_mutex_unlock (&self->lock);
897 gst_decklink_audio_src_unlock_stop (GstBaseSrc * bsrc)
899 GstDecklinkAudioSrc *self = GST_DECKLINK_AUDIO_SRC_CAST (bsrc);
901 g_mutex_lock (&self->lock);
902 self->flushing = FALSE;
903 while (gst_queue_array_get_length (self->current_packets) > 0) {
904 CapturePacket *tmp = (CapturePacket *)
905 gst_queue_array_pop_head_struct (self->current_packets);
906 capture_packet_clear (tmp);
908 g_mutex_unlock (&self->lock);
914 gst_decklink_audio_src_open (GstDecklinkAudioSrc * self)
916 GST_DEBUG_OBJECT (self, "Opening");
919 gst_decklink_acquire_nth_input (self->device_number,
920 GST_ELEMENT_CAST (self), TRUE);
922 GST_ERROR_OBJECT (self, "Failed to acquire input");
926 g_object_notify (G_OBJECT (self), "hw-serial-number");
928 g_mutex_lock (&self->input->lock);
929 if (self->channels > 0) {
930 self->channels_found = self->channels;
932 if (self->input->attributes) {
933 int64_t channels_found;
935 HRESULT ret = self->input->attributes->GetInt
936 (BMDDeckLinkMaximumAudioChannels, &channels_found);
937 self->channels_found = channels_found;
939 /* Sometimes the card may report an invalid number of channels. In
940 * that case, we should (empirically) use 8. */
942 self->channels_found == 0 || g_enum_get_value ((GEnumClass *)
943 g_type_class_peek (GST_TYPE_DECKLINK_AUDIO_CHANNELS),
944 self->channels_found)
946 self->channels_found = GST_DECKLINK_AUDIO_CHANNELS_8;
950 self->input->got_audio_packet = gst_decklink_audio_src_got_packet;
951 g_mutex_unlock (&self->input->lock);
957 gst_decklink_audio_src_close (GstDecklinkAudioSrc * self)
959 GST_DEBUG_OBJECT (self, "Closing");
962 g_mutex_lock (&self->input->lock);
963 self->input->got_audio_packet = NULL;
964 g_mutex_unlock (&self->input->lock);
966 gst_decklink_release_nth_input (self->device_number,
967 GST_ELEMENT_CAST (self), TRUE);
975 gst_decklink_audio_src_stop (GstDecklinkAudioSrc * self)
977 GST_DEBUG_OBJECT (self, "Stopping");
979 while (gst_queue_array_get_length (self->current_packets) > 0) {
980 CapturePacket *tmp = (CapturePacket *)
981 gst_queue_array_pop_head_struct (self->current_packets);
982 capture_packet_clear (tmp);
985 if (self->input && self->input->audio_enabled) {
986 g_mutex_lock (&self->input->lock);
987 self->input->audio_enabled = FALSE;
988 g_mutex_unlock (&self->input->lock);
990 self->input->input->DisableAudioInput ();
998 in_same_pipeline (GstElement * a, GstElement * b)
1000 GstObject *root = NULL, *tmp;
1001 gboolean ret = FALSE;
1003 tmp = gst_object_get_parent (GST_OBJECT_CAST (a));
1004 while (tmp != NULL) {
1006 gst_object_unref (root);
1008 tmp = gst_object_get_parent (root);
1011 ret = root && gst_object_has_ancestor (GST_OBJECT_CAST (b), root);
1014 gst_object_unref (root);
1020 static GstStateChangeReturn
1021 gst_decklink_audio_src_change_state (GstElement * element,
1022 GstStateChange transition)
1024 GstDecklinkAudioSrc *self = GST_DECKLINK_AUDIO_SRC_CAST (element);
1025 GstStateChangeReturn ret;
1027 switch (transition) {
1028 case GST_STATE_CHANGE_NULL_TO_READY:
1029 self->processed = 0;
1031 self->expected_stream_time = GST_CLOCK_TIME_NONE;
1032 if (!gst_decklink_audio_src_open (self)) {
1033 ret = GST_STATE_CHANGE_FAILURE;
1037 case GST_STATE_CHANGE_READY_TO_PAUSED:{
1038 GstElement *videosrc = NULL;
1040 // Check if there is a video src for this input too and if it
1041 // is actually in the same pipeline
1042 g_mutex_lock (&self->input->lock);
1043 if (self->input->videosrc)
1044 videosrc = GST_ELEMENT_CAST (gst_object_ref (self->input->videosrc));
1045 g_mutex_unlock (&self->input->lock);
1048 GST_ELEMENT_ERROR (self, STREAM, FAILED,
1049 (NULL), ("Audio src needs a video src for its operation"));
1050 ret = GST_STATE_CHANGE_FAILURE;
1053 // FIXME: This causes deadlocks sometimes
1055 else if (!in_same_pipeline (GST_ELEMENT_CAST (self), videosrc)) {
1056 GST_ELEMENT_ERROR (self, STREAM, FAILED,
1058 ("Audio src and video src need to be in the same pipeline"));
1059 ret = GST_STATE_CHANGE_FAILURE;
1060 gst_object_unref (videosrc);
1066 gst_object_unref (videosrc);
1068 self->flushing = FALSE;
1069 self->next_offset = -1;
1076 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1077 if (ret == GST_STATE_CHANGE_FAILURE)
1080 switch (transition) {
1081 case GST_STATE_CHANGE_PAUSED_TO_READY:
1082 gst_decklink_audio_src_stop (self);
1084 case GST_STATE_CHANGE_READY_TO_NULL:
1085 gst_decklink_audio_src_close (self);