2 * Copyright (C) 2011 David Schleef <ds@schleef.org>
3 * Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
4 * Copyright (C) 2015 Florian Langlois <florian.langlois@fr.thalesgroup.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
19 * Boston, MA 02110-1335, USA.
27 #include "gstdecklink.h"
28 #include "gstdecklinkaudiosink.h"
29 #include "gstdecklinkvideosink.h"
30 #include "gstdecklinkaudiosrc.h"
31 #include "gstdecklinkvideosrc.h"
33 GST_DEBUG_CATEGORY_STATIC (gst_decklink_debug);
34 #define GST_CAT_DEFAULT gst_decklink_debug
37 gst_decklink_mode_get_type (void)
40 static const GEnumValue modes[] = {
41 {GST_DECKLINK_MODE_NTSC, "ntsc", "NTSC SD 60i"},
42 {GST_DECKLINK_MODE_NTSC2398, "ntsc2398", "NTSC SD 60i (24 fps)"},
43 {GST_DECKLINK_MODE_PAL, "pal", "PAL SD 50i"},
44 {GST_DECKLINK_MODE_NTSC_P, "ntsc-p", "NTSC SD 60p"},
45 {GST_DECKLINK_MODE_PAL_P, "pal-p", "PAL SD 50p"},
47 {GST_DECKLINK_MODE_1080p2398, "1080p2398", "HD1080 23.98p"},
48 {GST_DECKLINK_MODE_1080p24, "1080p24", "HD1080 24p"},
49 {GST_DECKLINK_MODE_1080p25, "1080p25", "HD1080 25p"},
50 {GST_DECKLINK_MODE_1080p2997, "1080p2997", "HD1080 29.97p"},
51 {GST_DECKLINK_MODE_1080p30, "1080p30", "HD1080 30p"},
53 {GST_DECKLINK_MODE_1080i50, "1080i50", "HD1080 50i"},
54 {GST_DECKLINK_MODE_1080i5994, "1080i5994", "HD1080 59.94i"},
55 {GST_DECKLINK_MODE_1080i60, "1080i60", "HD1080 60i"},
57 {GST_DECKLINK_MODE_1080p50, "1080p50", "HD1080 50p"},
58 {GST_DECKLINK_MODE_1080p5994, "1080p5994", "HD1080 59.94p"},
59 {GST_DECKLINK_MODE_1080p60, "1080p60", "HD1080 60p"},
61 {GST_DECKLINK_MODE_720p50, "720p50", "HD720 50p"},
62 {GST_DECKLINK_MODE_720p5994, "720p5994", "HD720 59.94p"},
63 {GST_DECKLINK_MODE_720p60, "720p60", "HD720 60p"},
65 {GST_DECKLINK_MODE_2048p2398, "2048p2398", "2k 23.98p"},
66 {GST_DECKLINK_MODE_2048p24, "2048p24", "2k 24p"},
67 {GST_DECKLINK_MODE_2048p25, "2048p25", "2k 25p"},
69 {GST_DECKLINK_MODE_3184p2398, "3184p2398", "4k 23.98p"},
70 {GST_DECKLINK_MODE_3184p24, "3184p24", "4k 24p"},
71 {GST_DECKLINK_MODE_3184p25, "3184p25", "4k 25p"},
72 {GST_DECKLINK_MODE_3184p2997, "3184p2997", "4k 29.97p"},
73 {GST_DECKLINK_MODE_3184p30, "3184p30", "4k 30p"},
74 {GST_DECKLINK_MODE_3184p50, "3184p50", "4k 50p"},
75 {GST_DECKLINK_MODE_3184p5994, "3184p5994", "4k 59.94p"},
76 {GST_DECKLINK_MODE_3184p60, "3184p60", "4k 60p"},
81 if (g_once_init_enter (&id)) {
82 GType tmp = g_enum_register_static ("GstDecklinkModes", modes);
83 g_once_init_leave (&id, tmp);
90 gst_decklink_connection_get_type (void)
93 static const GEnumValue connections[] = {
94 {GST_DECKLINK_CONNECTION_SDI, "sdi", "SDI"},
95 {GST_DECKLINK_CONNECTION_HDMI, "hdmi", "HDMI"},
96 {GST_DECKLINK_CONNECTION_OPTICAL_SDI, "optical-sdi", "Optical SDI"},
97 {GST_DECKLINK_CONNECTION_COMPONENT, "component", "Component"},
98 {GST_DECKLINK_CONNECTION_COMPOSITE, "composite", "Composite"},
99 {GST_DECKLINK_CONNECTION_SVIDEO, "svideo", "S-Video"},
103 if (g_once_init_enter (&id)) {
104 GType tmp = g_enum_register_static ("GstDecklinkConnection", connections);
105 g_once_init_leave (&id, tmp);
112 gst_decklink_audio_connection_get_type (void)
115 static const GEnumValue connections[] = {
116 {GST_DECKLINK_AUDIO_CONNECTION_AUTO, "auto", "Automatic"},
117 {GST_DECKLINK_AUDIO_CONNECTION_EMBEDDED, "embedded",
118 "SDI/HDMI embedded audio"},
119 {GST_DECKLINK_AUDIO_CONNECTION_AES_EBU, "aes", "AES/EBU input"},
120 {GST_DECKLINK_AUDIO_CONNECTION_ANALOG, "analog", "Analog input"},
124 if (g_once_init_enter (&id)) {
126 g_enum_register_static ("GstDecklinkAudioConnection", connections);
127 g_once_init_leave (&id, tmp);
133 #define NTSC 10, 11, false, false
134 #define PAL 12, 11, true, false
135 #define HD 1, 1, false, true
137 static const GstDecklinkMode modes[] = {
138 {bmdModeNTSC, 720, 486, 30000, 1001, true, NTSC},
139 {bmdModeNTSC2398, 720, 486, 24000, 1001, true, NTSC},
140 {bmdModePAL, 720, 576, 25, 1, true, PAL},
141 {bmdModeNTSCp, 720, 486, 30000, 1001, false, NTSC},
142 {bmdModePALp, 720, 576, 25, 1, false, PAL},
144 {bmdModeHD1080p2398, 1920, 1080, 24000, 1001, false, HD},
145 {bmdModeHD1080p24, 1920, 1080, 24, 1, false, HD},
146 {bmdModeHD1080p25, 1920, 1080, 25, 1, false, HD},
147 {bmdModeHD1080p2997, 1920, 1080, 30000, 1001, false, HD},
148 {bmdModeHD1080p30, 1920, 1080, 30, 1, false, HD},
150 {bmdModeHD1080i50, 1920, 1080, 50, 1, true, HD},
151 {bmdModeHD1080i5994, 1920, 1080, 60000, 1001, true, HD},
152 {bmdModeHD1080i6000, 1920, 1080, 60, 1, true, HD},
154 {bmdModeHD1080p50, 1920, 1080, 50, 1, false, HD},
155 {bmdModeHD1080p5994, 1920, 1080, 60000, 1001, false, HD},
156 {bmdModeHD1080p6000, 1920, 1080, 60, 1, false, HD},
158 {bmdModeHD720p50, 1280, 720, 50, 1, false, HD},
159 {bmdModeHD720p5994, 1280, 720, 60000, 1001, false, HD},
160 {bmdModeHD720p60, 1280, 720, 60, 1, false, HD},
162 {bmdMode2k2398, 2048, 1556, 24000, 1001, false, HD},
163 {bmdMode2k24, 2048, 1556, 24, 1, false, HD},
164 {bmdMode2k25, 2048, 1556, 25, 1, false, HD},
166 {bmdMode4K2160p2398, 3840, 2160, 24000, 1001, false, HD},
167 {bmdMode4K2160p24, 3840, 2160, 24, 1, false, HD},
168 {bmdMode4K2160p25, 3840, 2160, 25, 1, false, HD},
169 {bmdMode4K2160p2997, 3840, 2160, 30000, 1001, false, HD},
170 {bmdMode4K2160p30, 3840, 2160, 30, 1, false, HD},
171 {bmdMode4K2160p50, 3840, 2160, 55, 1, false, HD},
172 {bmdMode4K2160p5994, 3840, 2160, 60000, 1001, false, HD},
173 {bmdMode4K2160p60, 3840, 2160, 60, 1, false, HD}
176 const GstDecklinkMode *
177 gst_decklink_get_mode (GstDecklinkModeEnum e)
179 if (e < GST_DECKLINK_MODE_NTSC || e > GST_DECKLINK_MODE_3184p60)
184 static const BMDVideoConnection connections[] = {
185 bmdVideoConnectionSDI,
186 bmdVideoConnectionHDMI,
187 bmdVideoConnectionOpticalSDI,
188 bmdVideoConnectionComponent,
189 bmdVideoConnectionComposite,
190 bmdVideoConnectionSVideo
193 const BMDVideoConnection
194 gst_decklink_get_connection (GstDecklinkConnectionEnum e)
196 if (e < GST_DECKLINK_CONNECTION_SDI || e > GST_DECKLINK_CONNECTION_SVIDEO)
197 e = GST_DECKLINK_CONNECTION_SDI;
198 return connections[e];
201 static GstStructure *
202 gst_decklink_mode_get_structure (GstDecklinkModeEnum e)
204 const GstDecklinkMode *mode = &modes[e];
206 return gst_structure_new ("video/x-raw",
207 "format", G_TYPE_STRING, "UYVY",
208 "width", G_TYPE_INT, mode->width,
209 "height", G_TYPE_INT, mode->height,
210 "framerate", GST_TYPE_FRACTION, mode->fps_n, mode->fps_d,
211 "interlace-mode", G_TYPE_STRING,
212 mode->interlaced ? "interleaved" : "progressive", "pixel-aspect-ratio",
213 GST_TYPE_FRACTION, mode->par_n, mode->par_d, "colorimetry", G_TYPE_STRING,
214 mode->is_hdtv ? "bt709" : "bt601", "chroma-site", G_TYPE_STRING, "mpeg2",
219 gst_decklink_mode_get_caps (GstDecklinkModeEnum e)
223 caps = gst_caps_new_empty ();
224 gst_caps_append_structure (caps, gst_decklink_mode_get_structure (e));
230 gst_decklink_mode_get_template_caps (void)
236 caps = gst_caps_new_empty ();
237 for (i = 0; i < (int) G_N_ELEMENTS (modes); i++) {
238 s = gst_decklink_mode_get_structure ((GstDecklinkModeEnum) i);
239 gst_caps_append_structure (caps, s);
245 #define GST_TYPE_DECKLINK_CLOCK \
246 (gst_decklink_clock_get_type())
247 #define GST_DECKLINK_CLOCK(obj) \
248 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECKLINK_CLOCK,GstDecklinkClock))
249 #define GST_DECKLINK_CLOCK_CLASS(klass) \
250 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECKLINK_CLOCK,GstDecklinkClockClass))
251 #define GST_IS_Decklink_CLOCK(obj) \
252 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECKLINK_CLOCK))
253 #define GST_IS_Decklink_CLOCK_CLASS(klass) \
254 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECKLINK_CLOCK))
255 #define GST_DECKLINK_CLOCK_CAST(obj) \
256 ((GstDecklinkClock*)(obj))
258 typedef struct _GstDecklinkClock GstDecklinkClock;
259 typedef struct _GstDecklinkClockClass GstDecklinkClockClass;
261 struct _GstDecklinkClock
263 GstSystemClock clock;
265 IDeckLinkInput *input;
266 IDeckLinkOutput *output;
269 struct _GstDecklinkClockClass
271 GstSystemClockClass parent_class;
274 GType gst_decklink_clock_get_type (void);
275 static GstClock *gst_decklink_clock_new (const gchar * name);
277 typedef struct _Device Device;
280 GstDecklinkOutput output;
281 GstDecklinkInput input;
284 class GStreamerDecklinkInputCallback:public IDeckLinkInputCallback
287 GstDecklinkInput * m_input;
291 GStreamerDecklinkInputCallback (GstDecklinkInput * input)
292 : IDeckLinkInputCallback ()
295 g_mutex_init (&m_mutex);
298 virtual ~ GStreamerDecklinkInputCallback ()
300 g_mutex_clear (&m_mutex);
303 virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID, LPVOID *)
305 return E_NOINTERFACE;
308 virtual ULONG STDMETHODCALLTYPE AddRef (void)
312 g_mutex_lock (&m_mutex);
315 g_mutex_unlock (&m_mutex);
320 virtual ULONG STDMETHODCALLTYPE Release (void)
324 g_mutex_lock (&m_mutex);
327 g_mutex_unlock (&m_mutex);
337 virtual HRESULT STDMETHODCALLTYPE
338 VideoInputFormatChanged (BMDVideoInputFormatChangedEvents,
339 IDeckLinkDisplayMode *, BMDDetectedVideoInputFormatFlags)
341 GST_FIXME ("Video input format change not supported yet");
345 virtual HRESULT STDMETHODCALLTYPE
346 VideoInputFrameArrived (IDeckLinkVideoInputFrame * video_frame,
347 IDeckLinkAudioInputPacket * audio_packet)
349 GstClockTime clock_time = gst_clock_get_time (m_input->clock);
351 g_mutex_lock (&m_input->lock);
352 if (m_input->got_video_frame) {
353 GstClockTime capture_time = clock_time -
354 gst_element_get_base_time (m_input->videosrc);
355 m_input->got_video_frame (m_input->videosrc, video_frame, capture_time);
358 if (m_input->got_audio_packet) {
359 GstClockTime capture_time = clock_time -
360 gst_element_get_base_time (m_input->audiosrc);
361 m_input->got_audio_packet (m_input->audiosrc, audio_packet, capture_time);
364 g_mutex_unlock (&m_input->lock);
370 /* FIXME: We currently never deinit this */
372 static GMutex com_init_lock;
373 static GMutex com_deinit_lock;
374 static GCond com_init_cond;
375 static GCond com_deinit_cond;
376 static GCond com_deinited_cond;
377 static gboolean com_initialized = FALSE;
379 /* COM initialization/uninitialization thread */
381 gst_decklink_com_thread (gpointer data)
385 g_mutex_lock (&com_init_lock);
387 /* Initialize COM with a MTA for this process. This thread will
388 * be the first one to enter the apartement and the last one to leave
389 * it, unitializing COM properly */
391 res = CoInitializeEx (0, COINIT_MULTITHREADED);
393 GST_WARNING ("COM has been already initialized in the same process");
394 else if (res == RPC_E_CHANGED_MODE)
395 GST_WARNING ("The concurrency model of COM has changed.");
397 GST_INFO ("COM intialized succesfully");
399 com_initialized = TRUE;
401 /* Signal other threads waiting on this condition that COM was initialized */
402 g_cond_signal (&com_init_cond);
404 g_mutex_unlock (&com_init_lock);
406 /* Wait until the unitialize condition is met to leave the COM apartement */
407 g_mutex_lock (&com_deinit_lock);
408 g_cond_wait (&com_deinit_cond, &com_deinit_lock);
411 GST_INFO ("COM unintialized succesfully");
412 com_initialized = FALSE;
413 g_cond_signal (&com_deinited_cond);
414 g_mutex_unlock (&com_deinit_lock);
418 #endif /* _MSC_VER */
420 static GOnce devices_once = G_ONCE_INIT;
421 static int n_devices;
422 static Device devices[10];
425 init_devices (gpointer data)
427 IDeckLinkIterator *iterator;
428 IDeckLink *decklink = NULL;
433 // Start COM thread for Windows
435 g_mutex_lock (&com_init_lock);
437 /* create the COM initialization thread */
438 g_thread_create ((GThreadFunc) gst_decklink_com_thread, NULL, FALSE, NULL);
440 /* wait until the COM thread signals that COM has been initialized */
441 g_cond_wait (&com_init_cond, &com_init_lock);
442 g_mutex_unlock (&com_init_lock);
443 #endif /* _MSC_VER */
445 iterator = CreateDeckLinkIteratorInstance ();
446 if (iterator == NULL) {
447 GST_ERROR ("no driver");
452 ret = iterator->Next (&decklink);
453 while (ret == S_OK) {
454 ret = decklink->QueryInterface (IID_IDeckLinkInput,
455 (void **) &devices[i].input.input);
457 GST_WARNING ("selected device does not have input interface");
459 devices[i].input.device = decklink;
460 devices[i].input.clock = gst_decklink_clock_new ("GstDecklinkInputClock");
461 GST_DECKLINK_CLOCK_CAST (devices[i].input.clock)->input =
462 devices[i].input.input;
464 input->SetCallback (new GStreamerDecklinkInputCallback (&devices[i].
468 ret = decklink->QueryInterface (IID_IDeckLinkOutput,
469 (void **) &devices[i].output.output);
471 GST_WARNING ("selected device does not have output interface");
473 devices[i].output.device = decklink;
474 devices[i].output.clock =
475 gst_decklink_clock_new ("GstDecklinkOutputClock");
476 GST_DECKLINK_CLOCK_CAST (devices[i].output.clock)->output =
477 devices[i].output.output;
480 ret = decklink->QueryInterface (IID_IDeckLinkConfiguration,
481 (void **) &devices[i].input.config);
483 GST_WARNING ("selected device does not have config interface");
486 ret = iterator->Next (&decklink);
490 GST_WARNING ("this hardware has more then 10 devices");
497 iterator->Release ();
503 gst_decklink_acquire_nth_output (gint n, GstElement * sink, gboolean is_audio)
505 GstDecklinkOutput *output;
507 g_once (&devices_once, init_devices, NULL);
512 output = &devices[n].output;
513 if (!output->output) {
514 GST_ERROR ("Device %d has no output", n);
518 g_mutex_lock (&output->lock);
519 if (is_audio && !output->audiosink) {
520 output->audiosink = GST_ELEMENT_CAST (gst_object_ref (sink));
521 g_mutex_unlock (&output->lock);
523 } else if (!output->videosink) {
524 output->videosink = GST_ELEMENT_CAST (gst_object_ref (sink));
525 g_mutex_unlock (&output->lock);
528 g_mutex_unlock (&output->lock);
530 GST_ERROR ("Output device %d (audio: %d) in use already", n, is_audio);
535 gst_decklink_release_nth_output (gint n, GstElement * sink, gboolean is_audio)
537 GstDecklinkOutput *output;
542 output = &devices[n].output;
543 g_assert (output->output);
545 g_mutex_lock (&output->lock);
547 g_assert (output->audiosink == sink);
548 gst_object_unref (sink);
549 output->audiosink = NULL;
551 g_assert (output->videosink == sink);
552 gst_object_unref (sink);
553 output->videosink = NULL;
555 g_mutex_unlock (&output->lock);
559 gst_decklink_output_set_audio_clock (GstDecklinkOutput * output,
562 g_mutex_lock (&output->lock);
563 if (output->audio_clock)
564 gst_object_unref (output->audio_clock);
565 output->audio_clock = clock;
567 gst_object_ref (clock);
568 g_mutex_unlock (&output->lock);
573 gst_decklink_output_get_audio_clock (GstDecklinkOutput * output)
575 GstClock *ret = NULL;
577 g_mutex_lock (&output->lock);
578 if (output->audio_clock)
579 ret = GST_CLOCK_CAST (gst_object_ref (output->audio_clock));
580 g_mutex_unlock (&output->lock);
586 gst_decklink_acquire_nth_input (gint n, GstElement * src, gboolean is_audio)
588 GstDecklinkInput *input;
590 g_once (&devices_once, init_devices, NULL);
595 input = &devices[n].input;
597 GST_ERROR ("Device %d has no input", n);
601 g_mutex_lock (&input->lock);
602 if (is_audio && !input->audiosrc) {
603 input->audiosrc = GST_ELEMENT_CAST (gst_object_ref (src));
604 g_mutex_unlock (&input->lock);
606 } else if (!input->videosrc) {
607 input->videosrc = GST_ELEMENT_CAST (gst_object_ref (src));
608 g_mutex_unlock (&input->lock);
611 g_mutex_unlock (&input->lock);
613 GST_ERROR ("Input device %d (audio: %d) in use already", n, is_audio);
618 gst_decklink_release_nth_input (gint n, GstElement * src, gboolean is_audio)
620 GstDecklinkInput *input;
625 input = &devices[n].input;
626 g_assert (input->input);
628 g_mutex_lock (&input->lock);
630 g_assert (input->audiosrc == src);
631 gst_object_unref (src);
632 input->audiosrc = NULL;
634 g_assert (input->videosrc == src);
635 gst_object_unref (src);
636 input->videosrc = NULL;
638 g_mutex_unlock (&input->lock);
641 G_DEFINE_TYPE (GstDecklinkClock, gst_decklink_clock, GST_TYPE_SYSTEM_CLOCK);
643 static GstClockTime gst_decklink_clock_get_internal_time (GstClock * clock);
646 gst_decklink_clock_class_init (GstDecklinkClockClass * klass)
648 GstClockClass *clock_class = (GstClockClass *) klass;
650 clock_class->get_internal_time = gst_decklink_clock_get_internal_time;
654 gst_decklink_clock_init (GstDecklinkClock * clock)
656 GST_OBJECT_FLAG_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER);
660 gst_decklink_clock_new (const gchar * name)
662 GstDecklinkClock *self =
663 GST_DECKLINK_CLOCK (g_object_new (GST_TYPE_DECKLINK_CLOCK, "name", name,
664 "clock-type", GST_CLOCK_TYPE_OTHER, NULL));
666 return GST_CLOCK_CAST (self);
670 gst_decklink_clock_get_internal_time (GstClock * clock)
672 GstDecklinkClock *self = GST_DECKLINK_CLOCK (clock);
677 GST_OBJECT_LOCK (clock);
678 if (self->input != NULL) {
680 self->input->GetHardwareReferenceClock (GST_SECOND, &time, NULL, NULL);
681 if (ret == S_OK && time >= 0)
684 result = GST_CLOCK_TIME_NONE;
685 } else if (self->output != NULL) {
687 self->output->GetHardwareReferenceClock (GST_SECOND, &time, NULL, NULL);
688 if (ret == S_OK && time >= 0)
691 result = GST_CLOCK_TIME_NONE;
693 result = GST_CLOCK_TIME_NONE;
695 GST_OBJECT_UNLOCK (clock);
696 GST_LOG_OBJECT (clock, "result %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
702 plugin_init (GstPlugin * plugin)
704 GST_DEBUG_CATEGORY_INIT (gst_decklink_debug, "decklink", 0,
705 "debug category for decklink plugin");
707 gst_element_register (plugin, "decklinkaudiosink", GST_RANK_NONE,
708 GST_TYPE_DECKLINK_AUDIO_SINK);
709 gst_element_register (plugin, "decklinkvideosink", GST_RANK_NONE,
710 GST_TYPE_DECKLINK_VIDEO_SINK);
711 gst_element_register (plugin, "decklinkaudiosrc", GST_RANK_NONE,
712 GST_TYPE_DECKLINK_AUDIO_SRC);
713 gst_element_register (plugin, "decklinkvideosrc", GST_RANK_NONE,
714 GST_TYPE_DECKLINK_VIDEO_SRC);
718 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
721 "Blackmagic Decklink plugin",
722 plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)