decklink: Add property for configuring the input connection of the video sources
[platform/upstream/gstreamer.git] / sys / decklink / gstdecklink.cpp
1 /* GStreamer
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/gst.h>
27 #include "gstdecklink.h"
28 #include "gstdecklinkaudiosink.h"
29 #include "gstdecklinkvideosink.h"
30 #include "gstdecklinkaudiosrc.h"
31 #include "gstdecklinkvideosrc.h"
32
33 GST_DEBUG_CATEGORY_STATIC (gst_decklink_debug);
34 #define GST_CAT_DEFAULT gst_decklink_debug
35
36 GType
37 gst_decklink_mode_get_type (void)
38 {
39   static gsize id = 0;
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"},
46
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"},
52
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"},
56
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"},
60
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"},
64
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"},
68
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"},
77
78     {0, NULL, NULL}
79   };
80
81   if (g_once_init_enter (&id)) {
82     GType tmp = g_enum_register_static ("GstDecklinkModes", modes);
83     g_once_init_leave (&id, tmp);
84   }
85
86   return (GType) id;
87 }
88
89 GType
90 gst_decklink_connection_get_type (void)
91 {
92   static gsize id = 0;
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"},
100     {0, NULL, NULL}
101   };
102
103   if (g_once_init_enter (&id)) {
104     GType tmp = g_enum_register_static ("GstDecklinkConnection", connections);
105     g_once_init_leave (&id, tmp);
106   }
107
108   return (GType) id;
109 }
110
111 GType
112 gst_decklink_audio_connection_get_type (void)
113 {
114   static gsize id = 0;
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"},
121     {0, NULL, NULL}
122   };
123
124   if (g_once_init_enter (&id)) {
125     GType tmp =
126         g_enum_register_static ("GstDecklinkAudioConnection", connections);
127     g_once_init_leave (&id, tmp);
128   }
129
130   return (GType) id;
131 }
132
133 #define NTSC 10, 11, false, false
134 #define PAL 12, 11, true, false
135 #define HD 1, 1, false, true
136
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},
143
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},
149
150   {bmdModeHD1080i50, 1920, 1080, 50, 1, true, HD},
151   {bmdModeHD1080i5994, 1920, 1080, 60000, 1001, true, HD},
152   {bmdModeHD1080i6000, 1920, 1080, 60, 1, true, HD},
153
154   {bmdModeHD1080p50, 1920, 1080, 50, 1, false, HD},
155   {bmdModeHD1080p5994, 1920, 1080, 60000, 1001, false, HD},
156   {bmdModeHD1080p6000, 1920, 1080, 60, 1, false, HD},
157
158   {bmdModeHD720p50, 1280, 720, 50, 1, false, HD},
159   {bmdModeHD720p5994, 1280, 720, 60000, 1001, false, HD},
160   {bmdModeHD720p60, 1280, 720, 60, 1, false, HD},
161
162   {bmdMode2k2398, 2048, 1556, 24000, 1001, false, HD},
163   {bmdMode2k24, 2048, 1556, 24, 1, false, HD},
164   {bmdMode2k25, 2048, 1556, 25, 1, false, HD},
165
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}
174 };
175
176 const GstDecklinkMode *
177 gst_decklink_get_mode (GstDecklinkModeEnum e)
178 {
179   if (e < GST_DECKLINK_MODE_NTSC || e > GST_DECKLINK_MODE_3184p60)
180     return NULL;
181   return &modes[e];
182 }
183
184 static const BMDVideoConnection connections[] = {
185   bmdVideoConnectionSDI,
186   bmdVideoConnectionHDMI,
187   bmdVideoConnectionOpticalSDI,
188   bmdVideoConnectionComponent,
189   bmdVideoConnectionComposite,
190   bmdVideoConnectionSVideo
191 };
192
193 const BMDVideoConnection
194 gst_decklink_get_connection (GstDecklinkConnectionEnum e)
195 {
196   if (e < GST_DECKLINK_CONNECTION_SDI || e > GST_DECKLINK_CONNECTION_SVIDEO)
197     e = GST_DECKLINK_CONNECTION_SDI;
198   return connections[e];
199 }
200
201 static GstStructure *
202 gst_decklink_mode_get_structure (GstDecklinkModeEnum e)
203 {
204   const GstDecklinkMode *mode = &modes[e];
205
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",
215       NULL);
216 }
217
218 GstCaps *
219 gst_decklink_mode_get_caps (GstDecklinkModeEnum e)
220 {
221   GstCaps *caps;
222
223   caps = gst_caps_new_empty ();
224   gst_caps_append_structure (caps, gst_decklink_mode_get_structure (e));
225
226   return caps;
227 }
228
229 GstCaps *
230 gst_decklink_mode_get_template_caps (void)
231 {
232   int i;
233   GstCaps *caps;
234   GstStructure *s;
235
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);
240   }
241
242   return caps;
243 }
244
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))
257
258 typedef struct _GstDecklinkClock GstDecklinkClock;
259 typedef struct _GstDecklinkClockClass GstDecklinkClockClass;
260
261 struct _GstDecklinkClock
262 {
263   GstSystemClock clock;
264
265   IDeckLinkInput *input;
266   IDeckLinkOutput *output;
267 };
268
269 struct _GstDecklinkClockClass
270 {
271   GstSystemClockClass parent_class;
272 };
273
274 GType gst_decklink_clock_get_type (void);
275 static GstClock *gst_decklink_clock_new (const gchar * name);
276
277 typedef struct _Device Device;
278 struct _Device
279 {
280   GstDecklinkOutput output;
281   GstDecklinkInput input;
282 };
283
284 class GStreamerDecklinkInputCallback:public IDeckLinkInputCallback
285 {
286 private:
287   GstDecklinkInput * m_input;
288   GMutex m_mutex;
289   gint m_refcount;
290 public:
291     GStreamerDecklinkInputCallback (GstDecklinkInput * input)
292   : IDeckLinkInputCallback ()
293   {
294     m_input = input;
295     g_mutex_init (&m_mutex);
296   }
297
298   virtual ~ GStreamerDecklinkInputCallback ()
299   {
300     g_mutex_clear (&m_mutex);
301   }
302
303   virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID, LPVOID *)
304   {
305     return E_NOINTERFACE;
306   }
307
308   virtual ULONG STDMETHODCALLTYPE AddRef (void)
309   {
310     ULONG ret;
311
312     g_mutex_lock (&m_mutex);
313     m_refcount++;
314     ret = m_refcount;
315     g_mutex_unlock (&m_mutex);
316
317     return ret;
318   }
319
320   virtual ULONG STDMETHODCALLTYPE Release (void)
321   {
322     ULONG ret;
323
324     g_mutex_lock (&m_mutex);
325     m_refcount--;
326     ret = m_refcount;
327     g_mutex_unlock (&m_mutex);
328
329
330     if (ret == 0) {
331       delete this;
332     }
333
334     return ret;
335   }
336
337   virtual HRESULT STDMETHODCALLTYPE
338       VideoInputFormatChanged (BMDVideoInputFormatChangedEvents,
339       IDeckLinkDisplayMode *, BMDDetectedVideoInputFormatFlags)
340   {
341     GST_FIXME ("Video input format change not supported yet");
342     return S_OK;
343   }
344
345   virtual HRESULT STDMETHODCALLTYPE
346       VideoInputFrameArrived (IDeckLinkVideoInputFrame * video_frame,
347       IDeckLinkAudioInputPacket * audio_packet)
348   {
349     GstClockTime clock_time = gst_clock_get_time (m_input->clock);
350
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);
356     }
357
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);
362     }
363
364     g_mutex_unlock (&m_input->lock);
365     return S_OK;
366   }
367 };
368
369 #ifdef _MSC_VER
370 /* FIXME: We currently never deinit this */
371
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;
378
379 /* COM initialization/uninitialization thread */
380 static gpointer
381 gst_decklink_com_thread (gpointer data)
382 {
383   HRESULT res;
384
385   g_mutex_lock (&com_init_lock);
386
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 */
390
391   res = CoInitializeEx (0, COINIT_MULTITHREADED);
392   if (res == S_FALSE)
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.");
396   else
397     GST_INFO ("COM intialized succesfully");
398
399   com_initialized = TRUE;
400
401   /* Signal other threads waiting on this condition that COM was initialized */
402   g_cond_signal (&com_init_cond);
403
404   g_mutex_unlock (&com_init_lock);
405
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);
409
410   CoUninitialize ();
411   GST_INFO ("COM unintialized succesfully");
412   com_initialized = FALSE;
413   g_cond_signal (&com_deinited_cond);
414   g_mutex_unlock (&com_deinit_lock);
415
416   return NULL;
417 }
418 #endif /* _MSC_VER */
419
420 static GOnce devices_once = G_ONCE_INIT;
421 static int n_devices;
422 static Device devices[10];
423
424 static gpointer
425 init_devices (gpointer data)
426 {
427   IDeckLinkIterator *iterator;
428   IDeckLink *decklink = NULL;
429   HRESULT ret;
430   int i;
431
432 #ifdef _MSC_VER
433   // Start COM thread for Windows
434
435   g_mutex_lock (&com_init_lock);
436
437   /* create the COM initialization thread */
438   g_thread_create ((GThreadFunc) gst_decklink_com_thread, NULL, FALSE, NULL);
439
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 */
444
445   iterator = CreateDeckLinkIteratorInstance ();
446   if (iterator == NULL) {
447     GST_ERROR ("no driver");
448     return NULL;
449   }
450
451   i = 0;
452   ret = iterator->Next (&decklink);
453   while (ret == S_OK) {
454     ret = decklink->QueryInterface (IID_IDeckLinkInput,
455         (void **) &devices[i].input.input);
456     if (ret != S_OK) {
457       GST_WARNING ("selected device does not have input interface");
458     } else {
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;
463       devices[i].input.
464           input->SetCallback (new GStreamerDecklinkInputCallback (&devices[i].
465               input));
466     }
467
468     ret = decklink->QueryInterface (IID_IDeckLinkOutput,
469         (void **) &devices[i].output.output);
470     if (ret != S_OK) {
471       GST_WARNING ("selected device does not have output interface");
472     } else {
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;
478     }
479
480     ret = decklink->QueryInterface (IID_IDeckLinkConfiguration,
481         (void **) &devices[i].input.config);
482     if (ret != S_OK) {
483       GST_WARNING ("selected device does not have config interface");
484     }
485
486     ret = iterator->Next (&decklink);
487     i++;
488
489     if (i == 10) {
490       GST_WARNING ("this hardware has more then 10 devices");
491       break;
492     }
493   }
494
495   n_devices = i;
496
497   iterator->Release ();
498
499   return NULL;
500 }
501
502 GstDecklinkOutput *
503 gst_decklink_acquire_nth_output (gint n, GstElement * sink, gboolean is_audio)
504 {
505   GstDecklinkOutput *output;
506
507   g_once (&devices_once, init_devices, NULL);
508
509   if (n >= n_devices)
510     return NULL;
511
512   output = &devices[n].output;
513   if (!output->output) {
514     GST_ERROR ("Device %d has no output", n);
515     return NULL;
516   }
517
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);
522     return output;
523   } else if (!output->videosink) {
524     output->videosink = GST_ELEMENT_CAST (gst_object_ref (sink));
525     g_mutex_unlock (&output->lock);
526     return output;
527   }
528   g_mutex_unlock (&output->lock);
529
530   GST_ERROR ("Output device %d (audio: %d) in use already", n, is_audio);
531   return NULL;
532 }
533
534 void
535 gst_decklink_release_nth_output (gint n, GstElement * sink, gboolean is_audio)
536 {
537   GstDecklinkOutput *output;
538
539   if (n >= n_devices)
540     return;
541
542   output = &devices[n].output;
543   g_assert (output->output);
544
545   g_mutex_lock (&output->lock);
546   if (is_audio) {
547     g_assert (output->audiosink == sink);
548     gst_object_unref (sink);
549     output->audiosink = NULL;
550   } else {
551     g_assert (output->videosink == sink);
552     gst_object_unref (sink);
553     output->videosink = NULL;
554   }
555   g_mutex_unlock (&output->lock);
556 }
557
558 void
559 gst_decklink_output_set_audio_clock (GstDecklinkOutput * output,
560     GstClock * clock)
561 {
562   g_mutex_lock (&output->lock);
563   if (output->audio_clock)
564     gst_object_unref (output->audio_clock);
565   output->audio_clock = clock;
566   if (clock)
567     gst_object_ref (clock);
568   g_mutex_unlock (&output->lock);
569 }
570
571
572 GstClock *
573 gst_decklink_output_get_audio_clock (GstDecklinkOutput * output)
574 {
575   GstClock *ret = NULL;
576
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);
581
582   return ret;
583 }
584
585 GstDecklinkInput *
586 gst_decklink_acquire_nth_input (gint n, GstElement * src, gboolean is_audio)
587 {
588   GstDecklinkInput *input;
589
590   g_once (&devices_once, init_devices, NULL);
591
592   if (n >= n_devices)
593     return NULL;
594
595   input = &devices[n].input;
596   if (!input->input) {
597     GST_ERROR ("Device %d has no input", n);
598     return NULL;
599   }
600
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);
605     return input;
606   } else if (!input->videosrc) {
607     input->videosrc = GST_ELEMENT_CAST (gst_object_ref (src));
608     g_mutex_unlock (&input->lock);
609     return input;
610   }
611   g_mutex_unlock (&input->lock);
612
613   GST_ERROR ("Input device %d (audio: %d) in use already", n, is_audio);
614   return NULL;
615 }
616
617 void
618 gst_decklink_release_nth_input (gint n, GstElement * src, gboolean is_audio)
619 {
620   GstDecklinkInput *input;
621
622   if (n >= n_devices)
623     return;
624
625   input = &devices[n].input;
626   g_assert (input->input);
627
628   g_mutex_lock (&input->lock);
629   if (is_audio) {
630     g_assert (input->audiosrc == src);
631     gst_object_unref (src);
632     input->audiosrc = NULL;
633   } else {
634     g_assert (input->videosrc == src);
635     gst_object_unref (src);
636     input->videosrc = NULL;
637   }
638   g_mutex_unlock (&input->lock);
639 }
640
641 G_DEFINE_TYPE (GstDecklinkClock, gst_decklink_clock, GST_TYPE_SYSTEM_CLOCK);
642
643 static GstClockTime gst_decklink_clock_get_internal_time (GstClock * clock);
644
645 static void
646 gst_decklink_clock_class_init (GstDecklinkClockClass * klass)
647 {
648   GstClockClass *clock_class = (GstClockClass *) klass;
649
650   clock_class->get_internal_time = gst_decklink_clock_get_internal_time;
651 }
652
653 static void
654 gst_decklink_clock_init (GstDecklinkClock * clock)
655 {
656   GST_OBJECT_FLAG_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER);
657 }
658
659 static GstClock *
660 gst_decklink_clock_new (const gchar * name)
661 {
662   GstDecklinkClock *self =
663       GST_DECKLINK_CLOCK (g_object_new (GST_TYPE_DECKLINK_CLOCK, "name", name,
664           "clock-type", GST_CLOCK_TYPE_OTHER, NULL));
665
666   return GST_CLOCK_CAST (self);
667 }
668
669 static GstClockTime
670 gst_decklink_clock_get_internal_time (GstClock * clock)
671 {
672   GstDecklinkClock *self = GST_DECKLINK_CLOCK (clock);
673   GstClockTime result;
674   BMDTimeValue time;
675   HRESULT ret;
676
677   GST_OBJECT_LOCK (clock);
678   if (self->input != NULL) {
679     ret =
680         self->input->GetHardwareReferenceClock (GST_SECOND, &time, NULL, NULL);
681     if (ret == S_OK && time >= 0)
682       result = time;
683     else
684       result = GST_CLOCK_TIME_NONE;
685   } else if (self->output != NULL) {
686     ret =
687         self->output->GetHardwareReferenceClock (GST_SECOND, &time, NULL, NULL);
688     if (ret == S_OK && time >= 0)
689       result = time;
690     else
691       result = GST_CLOCK_TIME_NONE;
692   } else {
693     result = GST_CLOCK_TIME_NONE;
694   }
695   GST_OBJECT_UNLOCK (clock);
696   GST_LOG_OBJECT (clock, "result %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
697
698   return result;
699 }
700
701 static gboolean
702 plugin_init (GstPlugin * plugin)
703 {
704   GST_DEBUG_CATEGORY_INIT (gst_decklink_debug, "decklink", 0,
705       "debug category for decklink plugin");
706
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);
715   return TRUE;
716 }
717
718 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
719     GST_VERSION_MINOR,
720     decklink,
721     "Blackmagic Decklink plugin",
722     plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)