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"},
71 if (g_once_init_enter (&id)) {
72 GType tmp = g_enum_register_static ("GstDecklinkModes", modes);
73 g_once_init_leave (&id, tmp);
80 gst_decklink_connection_get_type (void)
83 static const GEnumValue connections[] = {
84 {GST_DECKLINK_CONNECTION_SDI, "sdi", "SDI"},
85 {GST_DECKLINK_CONNECTION_HDMI, "hdmi", "HDMI"},
86 {GST_DECKLINK_CONNECTION_OPTICAL_SDI, "optical-sdi", "Optical SDI"},
87 {GST_DECKLINK_CONNECTION_COMPONENT, "component", "Component"},
88 {GST_DECKLINK_CONNECTION_COMPOSITE, "composite", "Composite"},
89 {GST_DECKLINK_CONNECTION_SVIDEO, "svideo", "S-Video"},
93 if (g_once_init_enter (&id)) {
94 GType tmp = g_enum_register_static ("GstDecklinkConnection", connections);
95 g_once_init_leave (&id, tmp);
102 gst_decklink_audio_connection_get_type (void)
105 static const GEnumValue connections[] = {
106 {GST_DECKLINK_AUDIO_CONNECTION_AUTO, "auto", "Automatic"},
107 {GST_DECKLINK_AUDIO_CONNECTION_EMBEDDED, "embedded",
108 "SDI/HDMI embedded audio"},
109 {GST_DECKLINK_AUDIO_CONNECTION_AES_EBU, "aes", "AES/EBU input"},
110 {GST_DECKLINK_AUDIO_CONNECTION_ANALOG, "analog", "Analog input"},
114 if (g_once_init_enter (&id)) {
116 g_enum_register_static ("GstDecklinkAudioConnection", connections);
117 g_once_init_leave (&id, tmp);
123 #define NTSC 10, 11, false, false
124 #define PAL 12, 11, true, false
125 #define HD 1, 1, false, true
127 static const GstDecklinkMode modes[] = {
128 {bmdModeNTSC, 720, 486, 30000, 1001, true, NTSC},
129 {bmdModeNTSC2398, 720, 486, 24000, 1001, true, NTSC},
130 {bmdModePAL, 720, 576, 25, 1, true, PAL},
131 {bmdModeNTSCp, 720, 486, 30000, 1001, false, NTSC},
132 {bmdModePALp, 720, 576, 25, 1, false, PAL},
134 {bmdModeHD1080p2398, 1920, 1080, 24000, 1001, false, HD},
135 {bmdModeHD1080p24, 1920, 1080, 24, 1, false, HD},
136 {bmdModeHD1080p25, 1920, 1080, 25, 1, false, HD},
137 {bmdModeHD1080p2997, 1920, 1080, 30000, 1001, false, HD},
138 {bmdModeHD1080p30, 1920, 1080, 30, 1, false, HD},
140 {bmdModeHD1080i50, 1920, 1080, 50, 1, true, HD},
141 {bmdModeHD1080i5994, 1920, 1080, 60000, 1001, true, HD},
142 {bmdModeHD1080i6000, 1920, 1080, 60, 1, true, HD},
144 {bmdModeHD1080p50, 1920, 1080, 50, 1, false, HD},
145 {bmdModeHD1080p5994, 1920, 1080, 60000, 1001, false, HD},
146 {bmdModeHD1080p6000, 1920, 1080, 60, 1, false, HD},
148 {bmdModeHD720p50, 1280, 720, 50, 1, false, HD},
149 {bmdModeHD720p5994, 1280, 720, 60000, 1001, false, HD},
150 {bmdModeHD720p60, 1280, 720, 60, 1, false, HD},
152 {bmdMode2k2398, 2048, 1556, 24000, 1001, false, HD},
153 {bmdMode2k24, 2048, 1556, 24, 1, false, HD},
154 {bmdMode2k25, 2048, 1556, 25, 1, false, HD}
158 const GstDecklinkMode *
159 gst_decklink_get_mode (GstDecklinkModeEnum e)
164 static GstStructure *
165 gst_decklink_mode_get_structure (GstDecklinkModeEnum e)
167 const GstDecklinkMode *mode = &modes[e];
169 return gst_structure_new ("video/x-raw",
170 "format", G_TYPE_STRING, "UYVY",
171 "width", G_TYPE_INT, mode->width,
172 "height", G_TYPE_INT, mode->height,
173 "framerate", GST_TYPE_FRACTION, mode->fps_n, mode->fps_d,
174 "interlace-mode", G_TYPE_STRING,
175 mode->interlaced ? "interleaved" : "progressive", "pixel-aspect-ratio",
176 GST_TYPE_FRACTION, mode->par_n, mode->par_d, "colorimetry", G_TYPE_STRING,
177 mode->is_hdtv ? "bt709" : "bt601", "chroma-site", G_TYPE_STRING, "mpeg2",
182 gst_decklink_mode_get_caps (GstDecklinkModeEnum e)
186 caps = gst_caps_new_empty ();
187 gst_caps_append_structure (caps, gst_decklink_mode_get_structure (e));
193 gst_decklink_mode_get_template_caps (void)
199 caps = gst_caps_new_empty ();
200 for (i = 0; i < (int) G_N_ELEMENTS (modes); i++) {
201 s = gst_decklink_mode_get_structure ((GstDecklinkModeEnum) i);
202 gst_caps_append_structure (caps, s);
208 #define GST_TYPE_DECKLINK_CLOCK \
209 (gst_decklink_clock_get_type())
210 #define GST_DECKLINK_CLOCK(obj) \
211 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECKLINK_CLOCK,GstDecklinkClock))
212 #define GST_DECKLINK_CLOCK_CLASS(klass) \
213 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECKLINK_CLOCK,GstDecklinkClockClass))
214 #define GST_IS_Decklink_CLOCK(obj) \
215 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECKLINK_CLOCK))
216 #define GST_IS_Decklink_CLOCK_CLASS(klass) \
217 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECKLINK_CLOCK))
218 #define GST_DECKLINK_CLOCK_CAST(obj) \
219 ((GstDecklinkClock*)(obj))
221 typedef struct _GstDecklinkClock GstDecklinkClock;
222 typedef struct _GstDecklinkClockClass GstDecklinkClockClass;
224 struct _GstDecklinkClock
226 GstSystemClock clock;
228 IDeckLinkInput *input;
229 IDeckLinkOutput *output;
232 struct _GstDecklinkClockClass
234 GstSystemClockClass parent_class;
237 GType gst_decklink_clock_get_type (void);
238 static GstClock *gst_decklink_clock_new (const gchar * name);
240 typedef struct _Device Device;
243 GstDecklinkOutput output;
244 GstDecklinkInput input;
247 class GStreamerDecklinkInputCallback:public IDeckLinkInputCallback
250 GstDecklinkInput * m_input;
254 GStreamerDecklinkInputCallback (GstDecklinkInput * input)
255 : IDeckLinkInputCallback ()
258 g_mutex_init (&m_mutex);
261 virtual ~ GStreamerDecklinkInputCallback ()
263 g_mutex_clear (&m_mutex);
266 virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID, LPVOID *)
268 return E_NOINTERFACE;
271 virtual ULONG STDMETHODCALLTYPE AddRef (void)
275 g_mutex_lock (&m_mutex);
278 g_mutex_unlock (&m_mutex);
283 virtual ULONG STDMETHODCALLTYPE Release (void)
287 g_mutex_lock (&m_mutex);
290 g_mutex_unlock (&m_mutex);
300 virtual HRESULT STDMETHODCALLTYPE
301 VideoInputFormatChanged (BMDVideoInputFormatChangedEvents,
302 IDeckLinkDisplayMode *, BMDDetectedVideoInputFormatFlags)
304 GST_FIXME ("Video input format change not supported yet");
308 virtual HRESULT STDMETHODCALLTYPE
309 VideoInputFrameArrived (IDeckLinkVideoInputFrame * video_frame,
310 IDeckLinkAudioInputPacket * audio_packet)
312 GstClockTime clock_time = gst_clock_get_time (m_input->clock);
314 g_mutex_lock (&m_input->lock);
315 if (m_input->got_video_frame) {
316 GstClockTime capture_time = clock_time -
317 gst_element_get_base_time (m_input->videosrc);
318 m_input->got_video_frame (m_input->videosrc, video_frame, capture_time);
321 if (m_input->got_audio_packet) {
322 GstClockTime capture_time = clock_time -
323 gst_element_get_base_time (m_input->audiosrc);
324 m_input->got_audio_packet (m_input->audiosrc, audio_packet, capture_time);
327 g_mutex_unlock (&m_input->lock);
333 /* FIXME: We currently never deinit this */
335 static GMutex com_init_lock;
336 static GMutex com_deinit_lock;
337 static GCond com_init_cond;
338 static GCond com_deinit_cond;
339 static GCond com_deinited_cond;
340 static gboolean com_initialized = FALSE;
342 /* COM initialization/uninitialization thread */
344 gst_decklink_com_thread (gpointer data)
348 g_mutex_lock (&com_init_lock);
350 /* Initialize COM with a MTA for this process. This thread will
351 * be the first one to enter the apartement and the last one to leave
352 * it, unitializing COM properly */
354 res = CoInitializeEx (0, COINIT_MULTITHREADED);
356 GST_WARNING ("COM has been already initialized in the same process");
357 else if (res == RPC_E_CHANGED_MODE)
358 GST_WARNING ("The concurrency model of COM has changed.");
360 GST_INFO ("COM intialized succesfully");
362 com_initialized = TRUE;
364 /* Signal other threads waiting on this condition that COM was initialized */
365 g_cond_signal (&com_init_cond);
367 g_mutex_unlock (&com_init_lock);
369 /* Wait until the unitialize condition is met to leave the COM apartement */
370 g_mutex_lock (&com_deinit_lock);
371 g_cond_wait (&com_deinit_cond, &com_deinit_lock);
374 GST_INFO ("COM unintialized succesfully");
375 com_initialized = FALSE;
376 g_cond_signal (&com_deinited_cond);
377 g_mutex_unlock (&com_deinit_lock);
381 #endif /* _MSC_VER */
383 static GOnce devices_once = G_ONCE_INIT;
384 static int n_devices;
385 static Device devices[10];
388 init_devices (gpointer data)
390 IDeckLinkIterator *iterator;
391 IDeckLink *decklink = NULL;
396 // Start COM thread for Windows
398 g_mutex_lock (&com_init_lock);
400 /* create the COM initialization thread */
401 g_thread_create ((GThreadFunc) gst_decklink_com_thread, NULL, FALSE, NULL);
403 /* wait until the COM thread signals that COM has been initialized */
404 g_cond_wait (&com_init_cond, &com_init_lock);
405 g_mutex_unlock (&com_init_lock);
406 #endif /* _MSC_VER */
408 iterator = CreateDeckLinkIteratorInstance ();
409 if (iterator == NULL) {
410 GST_ERROR ("no driver");
415 ret = iterator->Next (&decklink);
416 while (ret == S_OK) {
417 ret = decklink->QueryInterface (IID_IDeckLinkInput,
418 (void **) &devices[i].input.input);
420 GST_WARNING ("selected device does not have input interface");
422 devices[i].input.device = decklink;
423 devices[i].input.clock = gst_decklink_clock_new ("GstDecklinkInputClock");
424 GST_DECKLINK_CLOCK_CAST (devices[i].input.clock)->input =
425 devices[i].input.input;
427 input->SetCallback (new GStreamerDecklinkInputCallback (&devices[i].
431 ret = decklink->QueryInterface (IID_IDeckLinkOutput,
432 (void **) &devices[i].output.output);
434 GST_WARNING ("selected device does not have output interface");
436 devices[i].output.device = decklink;
437 devices[i].output.clock =
438 gst_decklink_clock_new ("GstDecklinkOutputClock");
439 GST_DECKLINK_CLOCK_CAST (devices[i].output.clock)->output =
440 devices[i].output.output;
443 ret = decklink->QueryInterface (IID_IDeckLinkConfiguration,
444 (void **) &devices[i].input.config);
446 GST_WARNING ("selected device does not have config interface");
449 ret = iterator->Next (&decklink);
453 GST_WARNING ("this hardware has more then 10 devices");
460 iterator->Release ();
466 gst_decklink_acquire_nth_output (gint n, GstElement * sink, gboolean is_audio)
468 GstDecklinkOutput *output;
470 g_once (&devices_once, init_devices, NULL);
475 output = &devices[n].output;
476 if (!output->output) {
477 GST_ERROR ("Device %d has no output", n);
481 g_mutex_lock (&output->lock);
482 if (is_audio && !output->audiosink) {
483 output->audiosink = GST_ELEMENT_CAST (gst_object_ref (sink));
484 g_mutex_unlock (&output->lock);
486 } else if (!output->videosink) {
487 output->videosink = GST_ELEMENT_CAST (gst_object_ref (sink));
488 g_mutex_unlock (&output->lock);
491 g_mutex_unlock (&output->lock);
493 GST_ERROR ("Output device %d (audio: %d) in use already", n, is_audio);
498 gst_decklink_release_nth_output (gint n, GstElement * sink, gboolean is_audio)
500 GstDecklinkOutput *output;
505 output = &devices[n].output;
506 g_assert (output->output);
508 g_mutex_lock (&output->lock);
510 g_assert (output->audiosink == sink);
511 gst_object_unref (sink);
512 output->audiosink = NULL;
514 g_assert (output->videosink == sink);
515 gst_object_unref (sink);
516 output->videosink = NULL;
518 g_mutex_unlock (&output->lock);
522 gst_decklink_output_set_audio_clock (GstDecklinkOutput * output,
525 g_mutex_lock (&output->lock);
526 if (output->audio_clock)
527 gst_object_unref (output->audio_clock);
528 output->audio_clock = clock;
530 gst_object_ref (clock);
531 g_mutex_unlock (&output->lock);
536 gst_decklink_output_get_audio_clock (GstDecklinkOutput * output)
538 GstClock *ret = NULL;
540 g_mutex_lock (&output->lock);
541 if (output->audio_clock)
542 ret = GST_CLOCK_CAST (gst_object_ref (output->audio_clock));
543 g_mutex_unlock (&output->lock);
549 gst_decklink_acquire_nth_input (gint n, GstElement * src, gboolean is_audio)
551 GstDecklinkInput *input;
553 g_once (&devices_once, init_devices, NULL);
558 input = &devices[n].input;
560 GST_ERROR ("Device %d has no input", n);
564 g_mutex_lock (&input->lock);
565 if (is_audio && !input->audiosrc) {
566 input->audiosrc = GST_ELEMENT_CAST (gst_object_ref (src));
567 g_mutex_unlock (&input->lock);
569 } else if (!input->videosrc) {
570 input->videosrc = GST_ELEMENT_CAST (gst_object_ref (src));
571 g_mutex_unlock (&input->lock);
574 g_mutex_unlock (&input->lock);
576 GST_ERROR ("Input device %d (audio: %d) in use already", n, is_audio);
581 gst_decklink_release_nth_input (gint n, GstElement * src, gboolean is_audio)
583 GstDecklinkInput *input;
588 input = &devices[n].input;
589 g_assert (input->input);
591 g_mutex_lock (&input->lock);
593 g_assert (input->audiosrc == src);
594 gst_object_unref (src);
595 input->audiosrc = NULL;
597 g_assert (input->videosrc == src);
598 gst_object_unref (src);
599 input->videosrc = NULL;
601 g_mutex_unlock (&input->lock);
604 G_DEFINE_TYPE (GstDecklinkClock, gst_decklink_clock, GST_TYPE_SYSTEM_CLOCK);
606 static GstClockTime gst_decklink_clock_get_internal_time (GstClock * clock);
609 gst_decklink_clock_class_init (GstDecklinkClockClass * klass)
611 GstClockClass *clock_class = (GstClockClass *) klass;
613 clock_class->get_internal_time = gst_decklink_clock_get_internal_time;
617 gst_decklink_clock_init (GstDecklinkClock * clock)
619 GST_OBJECT_FLAG_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER);
623 gst_decklink_clock_new (const gchar * name)
625 GstDecklinkClock *self =
626 GST_DECKLINK_CLOCK (g_object_new (GST_TYPE_DECKLINK_CLOCK, "name", name,
627 "clock-type", GST_CLOCK_TYPE_OTHER, NULL));
629 return GST_CLOCK_CAST (self);
633 gst_decklink_clock_get_internal_time (GstClock * clock)
635 GstDecklinkClock *self = GST_DECKLINK_CLOCK (clock);
640 GST_OBJECT_LOCK (clock);
641 if (self->input != NULL) {
643 self->input->GetHardwareReferenceClock (GST_SECOND, &time, NULL, NULL);
644 if (ret == S_OK && time >= 0)
647 result = GST_CLOCK_TIME_NONE;
648 } else if (self->output != NULL) {
650 self->output->GetHardwareReferenceClock (GST_SECOND, &time, NULL, NULL);
651 if (ret == S_OK && time >= 0)
654 result = GST_CLOCK_TIME_NONE;
656 result = GST_CLOCK_TIME_NONE;
658 GST_OBJECT_UNLOCK (clock);
659 GST_LOG_OBJECT (clock, "result %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
665 plugin_init (GstPlugin * plugin)
667 GST_DEBUG_CATEGORY_INIT (gst_decklink_debug, "decklink", 0,
668 "debug category for decklink plugin");
670 gst_element_register (plugin, "decklinkaudiosink", GST_RANK_NONE,
671 GST_TYPE_DECKLINK_AUDIO_SINK);
672 gst_element_register (plugin, "decklinkvideosink", GST_RANK_NONE,
673 GST_TYPE_DECKLINK_VIDEO_SINK);
674 gst_element_register (plugin, "decklinkaudiosrc", GST_RANK_NONE,
675 GST_TYPE_DECKLINK_AUDIO_SRC);
676 gst_element_register (plugin, "decklinkvideosrc", GST_RANK_NONE,
677 GST_TYPE_DECKLINK_VIDEO_SRC);
681 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
684 "Blackmagic Decklink plugin",
685 plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)