2 * Copyright (C) 2011 David Schleef <ds@schleef.org>
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.
26 #include "gstdecklink.h"
27 #include "gstdecklinkaudiosink.h"
28 #include "gstdecklinkvideosink.h"
29 #include "gstdecklinkaudiosrc.h"
30 #include "gstdecklinkvideosrc.h"
32 GST_DEBUG_CATEGORY_STATIC (gst_decklink_debug);
33 #define GST_CAT_DEFAULT gst_decklink_debug
36 gst_decklink_mode_get_type (void)
39 static const GEnumValue modes[] = {
40 {GST_DECKLINK_MODE_NTSC, "ntsc", "NTSC SD 60i"},
41 {GST_DECKLINK_MODE_NTSC2398, "ntsc2398", "NTSC SD 60i (24 fps)"},
42 {GST_DECKLINK_MODE_PAL, "pal", "PAL SD 50i"},
43 {GST_DECKLINK_MODE_NTSC_P, "ntsc-p", "NTSC SD 60p"},
44 {GST_DECKLINK_MODE_PAL_P, "pal-p", "PAL SD 50p"},
46 {GST_DECKLINK_MODE_1080p2398, "1080p2398", "HD1080 23.98p"},
47 {GST_DECKLINK_MODE_1080p24, "1080p24", "HD1080 24p"},
48 {GST_DECKLINK_MODE_1080p25, "1080p25", "HD1080 25p"},
49 {GST_DECKLINK_MODE_1080p2997, "1080p2997", "HD1080 29.97p"},
50 {GST_DECKLINK_MODE_1080p30, "1080p30", "HD1080 30p"},
52 {GST_DECKLINK_MODE_1080i50, "1080i50", "HD1080 50i"},
53 {GST_DECKLINK_MODE_1080i5994, "1080i5994", "HD1080 59.94i"},
54 {GST_DECKLINK_MODE_1080i60, "1080i60", "HD1080 60i"},
56 {GST_DECKLINK_MODE_1080p50, "1080p50", "HD1080 50p"},
57 {GST_DECKLINK_MODE_1080p5994, "1080p5994", "HD1080 59.94p"},
58 {GST_DECKLINK_MODE_1080p60, "1080p60", "HD1080 60p"},
60 {GST_DECKLINK_MODE_720p50, "720p50", "HD720 50p"},
61 {GST_DECKLINK_MODE_720p5994, "720p5994", "HD720 59.94p"},
62 {GST_DECKLINK_MODE_720p60, "720p60", "HD720 60p"},
64 {GST_DECKLINK_MODE_2048p2398, "2048p2398", "2k 23.98p"},
65 {GST_DECKLINK_MODE_2048p24, "2048p24", "2k 24p"},
66 {GST_DECKLINK_MODE_2048p25, "2048p25", "2k 25p"},
68 {GST_DECKLINK_MODE_3184p2398, "3184p2398", "4k 23.98p"},
69 {GST_DECKLINK_MODE_3184p24, "3184p24", "4k 24p"},
70 {GST_DECKLINK_MODE_3184p25, "3184p25", "4k 25p"},
71 {GST_DECKLINK_MODE_3184p2997, "3184p2997", "4k 29.97p"},
72 {GST_DECKLINK_MODE_3184p30, "3184p30", "4k 30p"},
73 {GST_DECKLINK_MODE_3184p50, "3184p50", "4k 50p"},
74 {GST_DECKLINK_MODE_3184p5994, "3184p5994", "4k 59.94p"},
75 {GST_DECKLINK_MODE_3184p60, "3184p60", "4k 60p"},
80 if (g_once_init_enter (&id)) {
81 GType tmp = g_enum_register_static ("GstDecklinkModes", modes);
82 g_once_init_leave (&id, tmp);
89 gst_decklink_connection_get_type (void)
92 static const GEnumValue connections[] = {
93 {GST_DECKLINK_CONNECTION_SDI, "sdi", "SDI"},
94 {GST_DECKLINK_CONNECTION_HDMI, "hdmi", "HDMI"},
95 {GST_DECKLINK_CONNECTION_OPTICAL_SDI, "optical-sdi", "Optical SDI"},
96 {GST_DECKLINK_CONNECTION_COMPONENT, "component", "Component"},
97 {GST_DECKLINK_CONNECTION_COMPOSITE, "composite", "Composite"},
98 {GST_DECKLINK_CONNECTION_SVIDEO, "svideo", "S-Video"},
102 if (g_once_init_enter (&id)) {
103 GType tmp = g_enum_register_static ("GstDecklinkConnection", connections);
104 g_once_init_leave (&id, tmp);
111 gst_decklink_audio_connection_get_type (void)
114 static const GEnumValue connections[] = {
115 {GST_DECKLINK_AUDIO_CONNECTION_AUTO, "auto", "Automatic"},
116 {GST_DECKLINK_AUDIO_CONNECTION_EMBEDDED, "embedded",
117 "SDI/HDMI embedded audio"},
118 {GST_DECKLINK_AUDIO_CONNECTION_AES_EBU, "aes", "AES/EBU input"},
119 {GST_DECKLINK_AUDIO_CONNECTION_ANALOG, "analog", "Analog input"},
123 if (g_once_init_enter (&id)) {
125 g_enum_register_static ("GstDecklinkAudioConnection", connections);
126 g_once_init_leave (&id, tmp);
132 #define NTSC 10, 11, false, false
133 #define PAL 12, 11, true, false
134 #define HD 1, 1, false, true
136 static const GstDecklinkMode modes[] = {
137 {bmdModeNTSC, 720, 486, 30000, 1001, true, NTSC},
138 {bmdModeNTSC2398, 720, 486, 24000, 1001, true, NTSC},
139 {bmdModePAL, 720, 576, 25, 1, true, PAL},
140 {bmdModeNTSCp, 720, 486, 30000, 1001, false, NTSC},
141 {bmdModePALp, 720, 576, 25, 1, false, PAL},
143 {bmdModeHD1080p2398, 1920, 1080, 24000, 1001, false, HD},
144 {bmdModeHD1080p24, 1920, 1080, 24, 1, false, HD},
145 {bmdModeHD1080p25, 1920, 1080, 25, 1, false, HD},
146 {bmdModeHD1080p2997, 1920, 1080, 30000, 1001, false, HD},
147 {bmdModeHD1080p30, 1920, 1080, 30, 1, false, HD},
149 {bmdModeHD1080i50, 1920, 1080, 50, 1, true, HD},
150 {bmdModeHD1080i5994, 1920, 1080, 60000, 1001, true, HD},
151 {bmdModeHD1080i6000, 1920, 1080, 60, 1, true, HD},
153 {bmdModeHD1080p50, 1920, 1080, 50, 1, false, HD},
154 {bmdModeHD1080p5994, 1920, 1080, 60000, 1001, false, HD},
155 {bmdModeHD1080p6000, 1920, 1080, 60, 1, false, HD},
157 {bmdModeHD720p50, 1280, 720, 50, 1, false, HD},
158 {bmdModeHD720p5994, 1280, 720, 60000, 1001, false, HD},
159 {bmdModeHD720p60, 1280, 720, 60, 1, false, HD},
161 {bmdMode2k2398, 2048, 1556, 24000, 1001, false, HD},
162 {bmdMode2k24, 2048, 1556, 24, 1, false, HD},
163 {bmdMode2k25, 2048, 1556, 25, 1, false, HD},
165 {bmdMode4K2160p2398, 3840, 2160, 24000, 1001, false, HD},
166 {bmdMode4K2160p24, 3840, 2160, 24, 1, false, HD},
167 {bmdMode4K2160p25, 3840, 2160, 25, 1, false, HD},
168 {bmdMode4K2160p2997, 3840, 2160, 30000, 1001, false, HD},
169 {bmdMode4K2160p30, 3840, 2160, 30, 1, false, HD},
170 {bmdMode4K2160p50, 3840, 2160, 55, 1, false, HD},
171 {bmdMode4K2160p5994, 3840, 2160, 60000, 1001, false, HD},
172 {bmdMode4K2160p60, 3840, 2160, 60, 1, false, HD}
175 const GstDecklinkMode *
176 gst_decklink_get_mode (GstDecklinkModeEnum e)
181 static GstStructure *
182 gst_decklink_mode_get_structure (GstDecklinkModeEnum e)
184 const GstDecklinkMode *mode = &modes[e];
186 return gst_structure_new ("video/x-raw",
187 "format", G_TYPE_STRING, "UYVY",
188 "width", G_TYPE_INT, mode->width,
189 "height", G_TYPE_INT, mode->height,
190 "framerate", GST_TYPE_FRACTION, mode->fps_n, mode->fps_d,
191 "interlace-mode", G_TYPE_STRING,
192 mode->interlaced ? "interleaved" : "progressive", "pixel-aspect-ratio",
193 GST_TYPE_FRACTION, mode->par_n, mode->par_d, "colorimetry", G_TYPE_STRING,
194 mode->is_hdtv ? "bt709" : "bt601", "chroma-site", G_TYPE_STRING, "mpeg2",
199 gst_decklink_mode_get_caps (GstDecklinkModeEnum e)
203 caps = gst_caps_new_empty ();
204 gst_caps_append_structure (caps, gst_decklink_mode_get_structure (e));
210 gst_decklink_mode_get_template_caps (void)
216 caps = gst_caps_new_empty ();
217 for (i = 0; i < (int) G_N_ELEMENTS (modes); i++) {
218 s = gst_decklink_mode_get_structure ((GstDecklinkModeEnum) i);
219 gst_caps_append_structure (caps, s);
225 #define GST_TYPE_DECKLINK_CLOCK \
226 (gst_decklink_clock_get_type())
227 #define GST_DECKLINK_CLOCK(obj) \
228 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECKLINK_CLOCK,GstDecklinkClock))
229 #define GST_DECKLINK_CLOCK_CLASS(klass) \
230 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECKLINK_CLOCK,GstDecklinkClockClass))
231 #define GST_IS_Decklink_CLOCK(obj) \
232 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECKLINK_CLOCK))
233 #define GST_IS_Decklink_CLOCK_CLASS(klass) \
234 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECKLINK_CLOCK))
235 #define GST_DECKLINK_CLOCK_CAST(obj) \
236 ((GstDecklinkClock*)(obj))
238 typedef struct _GstDecklinkClock GstDecklinkClock;
239 typedef struct _GstDecklinkClockClass GstDecklinkClockClass;
241 struct _GstDecklinkClock
243 GstSystemClock clock;
245 IDeckLinkInput *input;
246 IDeckLinkOutput *output;
249 struct _GstDecklinkClockClass
251 GstSystemClockClass parent_class;
254 GType gst_decklink_clock_get_type (void);
255 static GstClock *gst_decklink_clock_new (const gchar * name);
257 typedef struct _Device Device;
260 GstDecklinkOutput output;
261 GstDecklinkInput input;
264 class GStreamerDecklinkInputCallback:public IDeckLinkInputCallback
267 GstDecklinkInput * m_input;
271 GStreamerDecklinkInputCallback (GstDecklinkInput * input)
272 : IDeckLinkInputCallback ()
275 g_mutex_init (&m_mutex);
278 virtual ~ GStreamerDecklinkInputCallback ()
280 g_mutex_clear (&m_mutex);
283 virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID, LPVOID *)
285 return E_NOINTERFACE;
288 virtual ULONG STDMETHODCALLTYPE AddRef (void)
292 g_mutex_lock (&m_mutex);
295 g_mutex_unlock (&m_mutex);
300 virtual ULONG STDMETHODCALLTYPE Release (void)
304 g_mutex_lock (&m_mutex);
307 g_mutex_unlock (&m_mutex);
317 virtual HRESULT STDMETHODCALLTYPE
318 VideoInputFormatChanged (BMDVideoInputFormatChangedEvents,
319 IDeckLinkDisplayMode *, BMDDetectedVideoInputFormatFlags)
321 GST_FIXME ("Video input format change not supported yet");
325 virtual HRESULT STDMETHODCALLTYPE
326 VideoInputFrameArrived (IDeckLinkVideoInputFrame * video_frame,
327 IDeckLinkAudioInputPacket * audio_packet)
329 GstClockTime clock_time = gst_clock_get_time (m_input->clock);
331 g_mutex_lock (&m_input->lock);
332 if (m_input->got_video_frame) {
333 GstClockTime capture_time = clock_time -
334 gst_element_get_base_time (m_input->videosrc);
335 m_input->got_video_frame (m_input->videosrc, video_frame, capture_time);
338 if (m_input->got_audio_packet) {
339 GstClockTime capture_time = clock_time -
340 gst_element_get_base_time (m_input->audiosrc);
341 m_input->got_audio_packet (m_input->audiosrc, audio_packet, capture_time);
344 g_mutex_unlock (&m_input->lock);
350 /* FIXME: We currently never deinit this */
352 static GMutex com_init_lock;
353 static GMutex com_deinit_lock;
354 static GCond com_init_cond;
355 static GCond com_deinit_cond;
356 static GCond com_deinited_cond;
357 static gboolean com_initialized = FALSE;
359 /* COM initialization/uninitialization thread */
361 gst_decklink_com_thread (gpointer data)
365 g_mutex_lock (&com_init_lock);
367 /* Initialize COM with a MTA for this process. This thread will
368 * be the first one to enter the apartement and the last one to leave
369 * it, unitializing COM properly */
371 res = CoInitializeEx (0, COINIT_MULTITHREADED);
373 GST_WARNING ("COM has been already initialized in the same process");
374 else if (res == RPC_E_CHANGED_MODE)
375 GST_WARNING ("The concurrency model of COM has changed.");
377 GST_INFO ("COM intialized succesfully");
379 com_initialized = TRUE;
381 /* Signal other threads waiting on this condition that COM was initialized */
382 g_cond_signal (&com_init_cond);
384 g_mutex_unlock (&com_init_lock);
386 /* Wait until the unitialize condition is met to leave the COM apartement */
387 g_mutex_lock (&com_deinit_lock);
388 g_cond_wait (&com_deinit_cond, &com_deinit_lock);
391 GST_INFO ("COM unintialized succesfully");
392 com_initialized = FALSE;
393 g_cond_signal (&com_deinited_cond);
394 g_mutex_unlock (&com_deinit_lock);
398 #endif /* _MSC_VER */
400 static GOnce devices_once = G_ONCE_INIT;
401 static int n_devices;
402 static Device devices[10];
405 init_devices (gpointer data)
407 IDeckLinkIterator *iterator;
408 IDeckLink *decklink = NULL;
413 // Start COM thread for Windows
415 g_mutex_lock (&com_init_lock);
417 /* create the COM initialization thread */
418 g_thread_create ((GThreadFunc) gst_decklink_com_thread, NULL, FALSE, NULL);
420 /* wait until the COM thread signals that COM has been initialized */
421 g_cond_wait (&com_init_cond, &com_init_lock);
422 g_mutex_unlock (&com_init_lock);
423 #endif /* _MSC_VER */
425 iterator = CreateDeckLinkIteratorInstance ();
426 if (iterator == NULL) {
427 GST_ERROR ("no driver");
432 ret = iterator->Next (&decklink);
433 while (ret == S_OK) {
434 ret = decklink->QueryInterface (IID_IDeckLinkInput,
435 (void **) &devices[i].input.input);
437 GST_WARNING ("selected device does not have input interface");
439 devices[i].input.device = decklink;
440 devices[i].input.clock = gst_decklink_clock_new ("GstDecklinkInputClock");
441 GST_DECKLINK_CLOCK_CAST (devices[i].input.clock)->input =
442 devices[i].input.input;
444 input->SetCallback (new GStreamerDecklinkInputCallback (&devices[i].
448 ret = decklink->QueryInterface (IID_IDeckLinkOutput,
449 (void **) &devices[i].output.output);
451 GST_WARNING ("selected device does not have output interface");
453 devices[i].output.device = decklink;
454 devices[i].output.clock =
455 gst_decklink_clock_new ("GstDecklinkOutputClock");
456 GST_DECKLINK_CLOCK_CAST (devices[i].output.clock)->output =
457 devices[i].output.output;
460 ret = decklink->QueryInterface (IID_IDeckLinkConfiguration,
461 (void **) &devices[i].input.config);
463 GST_WARNING ("selected device does not have config interface");
466 ret = iterator->Next (&decklink);
470 GST_WARNING ("this hardware has more then 10 devices");
477 iterator->Release ();
483 gst_decklink_acquire_nth_output (gint n, GstElement * sink, gboolean is_audio)
485 GstDecklinkOutput *output;
487 g_once (&devices_once, init_devices, NULL);
492 output = &devices[n].output;
493 if (!output->output) {
494 GST_ERROR ("Device %d has no output", n);
498 g_mutex_lock (&output->lock);
499 if (is_audio && !output->audiosink) {
500 output->audiosink = GST_ELEMENT_CAST (gst_object_ref (sink));
501 g_mutex_unlock (&output->lock);
503 } else if (!output->videosink) {
504 output->videosink = GST_ELEMENT_CAST (gst_object_ref (sink));
505 g_mutex_unlock (&output->lock);
508 g_mutex_unlock (&output->lock);
510 GST_ERROR ("Output device %d (audio: %d) in use already", n, is_audio);
515 gst_decklink_release_nth_output (gint n, GstElement * sink, gboolean is_audio)
517 GstDecklinkOutput *output;
522 output = &devices[n].output;
523 g_assert (output->output);
525 g_mutex_lock (&output->lock);
527 g_assert (output->audiosink == sink);
528 gst_object_unref (sink);
529 output->audiosink = NULL;
531 g_assert (output->videosink == sink);
532 gst_object_unref (sink);
533 output->videosink = NULL;
535 g_mutex_unlock (&output->lock);
539 gst_decklink_output_set_audio_clock (GstDecklinkOutput * output,
542 g_mutex_lock (&output->lock);
543 if (output->audio_clock)
544 gst_object_unref (output->audio_clock);
545 output->audio_clock = clock;
547 gst_object_ref (clock);
548 g_mutex_unlock (&output->lock);
553 gst_decklink_output_get_audio_clock (GstDecklinkOutput * output)
555 GstClock *ret = NULL;
557 g_mutex_lock (&output->lock);
558 if (output->audio_clock)
559 ret = GST_CLOCK_CAST (gst_object_ref (output->audio_clock));
560 g_mutex_unlock (&output->lock);
566 gst_decklink_acquire_nth_input (gint n, GstElement * src, gboolean is_audio)
568 GstDecklinkInput *input;
570 g_once (&devices_once, init_devices, NULL);
575 input = &devices[n].input;
577 GST_ERROR ("Device %d has no input", n);
581 g_mutex_lock (&input->lock);
582 if (is_audio && !input->audiosrc) {
583 input->audiosrc = GST_ELEMENT_CAST (gst_object_ref (src));
584 g_mutex_unlock (&input->lock);
586 } else if (!input->videosrc) {
587 input->videosrc = GST_ELEMENT_CAST (gst_object_ref (src));
588 g_mutex_unlock (&input->lock);
591 g_mutex_unlock (&input->lock);
593 GST_ERROR ("Input device %d (audio: %d) in use already", n, is_audio);
598 gst_decklink_release_nth_input (gint n, GstElement * src, gboolean is_audio)
600 GstDecklinkInput *input;
605 input = &devices[n].input;
606 g_assert (input->input);
608 g_mutex_lock (&input->lock);
610 g_assert (input->audiosrc == src);
611 gst_object_unref (src);
612 input->audiosrc = NULL;
614 g_assert (input->videosrc == src);
615 gst_object_unref (src);
616 input->videosrc = NULL;
618 g_mutex_unlock (&input->lock);
621 G_DEFINE_TYPE (GstDecklinkClock, gst_decklink_clock, GST_TYPE_SYSTEM_CLOCK);
623 static GstClockTime gst_decklink_clock_get_internal_time (GstClock * clock);
626 gst_decklink_clock_class_init (GstDecklinkClockClass * klass)
628 GstClockClass *clock_class = (GstClockClass *) klass;
630 clock_class->get_internal_time = gst_decklink_clock_get_internal_time;
634 gst_decklink_clock_init (GstDecklinkClock * clock)
636 GST_OBJECT_FLAG_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER);
640 gst_decklink_clock_new (const gchar * name)
642 GstDecklinkClock *self =
643 GST_DECKLINK_CLOCK (g_object_new (GST_TYPE_DECKLINK_CLOCK, "name", name,
644 "clock-type", GST_CLOCK_TYPE_OTHER, NULL));
646 return GST_CLOCK_CAST (self);
650 gst_decklink_clock_get_internal_time (GstClock * clock)
652 GstDecklinkClock *self = GST_DECKLINK_CLOCK (clock);
657 GST_OBJECT_LOCK (clock);
658 if (self->input != NULL) {
660 self->input->GetHardwareReferenceClock (GST_SECOND, &time, NULL, NULL);
661 if (ret == S_OK && time >= 0)
664 result = GST_CLOCK_TIME_NONE;
665 } else if (self->output != NULL) {
667 self->output->GetHardwareReferenceClock (GST_SECOND, &time, NULL, NULL);
668 if (ret == S_OK && time >= 0)
671 result = GST_CLOCK_TIME_NONE;
673 result = GST_CLOCK_TIME_NONE;
675 GST_OBJECT_UNLOCK (clock);
676 GST_LOG_OBJECT (clock, "result %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
682 plugin_init (GstPlugin * plugin)
684 GST_DEBUG_CATEGORY_INIT (gst_decklink_debug, "decklink", 0,
685 "debug category for decklink plugin");
687 gst_element_register (plugin, "decklinkaudiosink", GST_RANK_NONE,
688 GST_TYPE_DECKLINK_AUDIO_SINK);
689 gst_element_register (plugin, "decklinkvideosink", GST_RANK_NONE,
690 GST_TYPE_DECKLINK_VIDEO_SINK);
691 gst_element_register (plugin, "decklinkaudiosrc", GST_RANK_NONE,
692 GST_TYPE_DECKLINK_AUDIO_SRC);
693 gst_element_register (plugin, "decklinkvideosrc", GST_RANK_NONE,
694 GST_TYPE_DECKLINK_VIDEO_SRC);
698 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
701 "Blackmagic Decklink plugin",
702 plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)