-
Notifications
You must be signed in to change notification settings - Fork 0
/
gstajasink.cpp
2201 lines (1923 loc) · 81.1 KB
/
gstajasink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GStreamer
* Copyright (C) 2021 Sebastian Dröge <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
* Boston, MA 02110-1335, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ajaanc/includes/ancillarydata_cea608_vanc.h>
#include <ajaanc/includes/ancillarydata_cea708.h>
#include <ajaanc/includes/ancillarylist.h>
#include <ajantv2/includes/ntv2rp188.h>
#include "gstajacommon.h"
#include "gstajasink.h"
GST_DEBUG_CATEGORY_STATIC(gst_aja_sink_debug);
#define GST_CAT_DEFAULT gst_aja_sink_debug
#define DEFAULT_DEVICE_IDENTIFIER ("0")
#define DEFAULT_CHANNEL (::NTV2_CHANNEL1)
#define DEFAULT_AUDIO_SYSTEM (GST_AJA_AUDIO_SYSTEM_AUTO)
#define DEFAULT_OUTPUT_DESTINATION (GST_AJA_OUTPUT_DESTINATION_AUTO)
#define DEFAULT_SDI_MODE (GST_AJA_SDI_MODE_SINGLE_LINK)
#define DEFAULT_TIMECODE_INDEX (GST_AJA_TIMECODE_INDEX_VITC)
#define DEFAULT_RP188 (TRUE)
#define DEFAULT_REFERENCE_SOURCE (GST_AJA_REFERENCE_SOURCE_AUTO)
#define DEFAULT_CEA608_LINE_NUMBER (12)
#define DEFAULT_CEA708_LINE_NUMBER (12)
#define DEFAULT_QUEUE_SIZE (16)
#define DEFAULT_START_FRAME (0)
#define DEFAULT_END_FRAME (0)
#define DEFAULT_OUTPUT_CPU_CORE (G_MAXUINT)
enum {
PROP_0,
PROP_DEVICE_IDENTIFIER,
PROP_CHANNEL,
PROP_AUDIO_SYSTEM,
PROP_OUTPUT_DESTINATION,
PROP_SDI_MODE,
PROP_TIMECODE_INDEX,
PROP_RP188,
PROP_REFERENCE_SOURCE,
PROP_CEA608_LINE_NUMBER,
PROP_CEA708_LINE_NUMBER,
PROP_QUEUE_SIZE,
PROP_START_FRAME,
PROP_END_FRAME,
PROP_OUTPUT_CPU_CORE,
};
typedef enum {
QUEUE_ITEM_TYPE_FRAME,
} QueueItemType;
typedef struct {
QueueItemType type;
// For FRAME
GstBuffer *video_buffer;
GstMapInfo video_map;
GstBuffer *audio_buffer;
GstMapInfo audio_map;
NTV2_RP188 tc;
GstBuffer *anc_buffer;
GstMapInfo anc_map;
GstBuffer *anc_buffer2;
GstMapInfo anc_map2;
} QueueItem;
static void gst_aja_sink_set_property(GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec);
static void gst_aja_sink_get_property(GObject *object, guint property_id,
GValue *value, GParamSpec *pspec);
static void gst_aja_sink_finalize(GObject *object);
static gboolean gst_aja_sink_set_caps(GstBaseSink *bsink, GstCaps *caps);
static GstCaps *gst_aja_sink_get_caps(GstBaseSink *bsink, GstCaps *filter);
static gboolean gst_aja_sink_event(GstBaseSink *bsink, GstEvent *event);
static gboolean gst_aja_sink_propose_allocation(GstBaseSink *bsink,
GstQuery *query);
static GstFlowReturn gst_aja_sink_render(GstBaseSink *bsink, GstBuffer *buffer);
static gboolean gst_aja_sink_open(GstAjaSink *sink);
static gboolean gst_aja_sink_close(GstAjaSink *sink);
static gboolean gst_aja_sink_start(GstAjaSink *sink);
static gboolean gst_aja_sink_stop(GstAjaSink *sink);
static GstStateChangeReturn gst_aja_sink_change_state(
GstElement *element, GstStateChange transition);
static void output_thread_func(AJAThread *thread, void *data);
#define parent_class gst_aja_sink_parent_class
G_DEFINE_TYPE(GstAjaSink, gst_aja_sink, GST_TYPE_BASE_SINK);
static void gst_aja_sink_class_init(GstAjaSinkClass *klass) {
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS(klass);
GstCaps *templ_caps;
gobject_class->set_property = gst_aja_sink_set_property;
gobject_class->get_property = gst_aja_sink_get_property;
gobject_class->finalize = gst_aja_sink_finalize;
g_object_class_install_property(
gobject_class, PROP_DEVICE_IDENTIFIER,
g_param_spec_string(
"device-identifier", "Device identifier",
"Input device instance to use", DEFAULT_DEVICE_IDENTIFIER,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT)));
g_object_class_install_property(
gobject_class, PROP_CHANNEL,
g_param_spec_uint(
"channel", "Channel", "Channel to use", 0, NTV2_MAX_NUM_CHANNELS - 1,
DEFAULT_CHANNEL,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT)));
g_object_class_install_property(
gobject_class, PROP_QUEUE_SIZE,
g_param_spec_uint(
"queue-size", "Queue Size",
"Size of internal queue in number of video frames. "
"Half of this is allocated as device buffers and equal to the "
"latency.",
1, G_MAXINT, DEFAULT_QUEUE_SIZE,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property(
gobject_class, PROP_START_FRAME,
g_param_spec_uint(
"start-frame", "Start Frame",
"Start frame buffer to be used for output (auto if same number as "
"end-frame).",
0, G_MAXINT, DEFAULT_START_FRAME,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property(
gobject_class, PROP_END_FRAME,
g_param_spec_uint(
"end-frame", "End Frame",
"End frame buffer to be used for output (auto if same number as "
"start-frame).",
0, G_MAXINT, DEFAULT_END_FRAME,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property(
gobject_class, PROP_AUDIO_SYSTEM,
g_param_spec_enum(
"audio-system", "Audio System", "Audio system to use",
GST_TYPE_AJA_AUDIO_SYSTEM, DEFAULT_AUDIO_SYSTEM,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT)));
g_object_class_install_property(
gobject_class, PROP_OUTPUT_DESTINATION,
g_param_spec_enum(
"output-destination", "Output Destination",
"Output destination to use", GST_TYPE_AJA_OUTPUT_DESTINATION,
DEFAULT_OUTPUT_DESTINATION,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT)));
g_object_class_install_property(
gobject_class, PROP_SDI_MODE,
g_param_spec_enum(
"sdi-mode", "SDI Mode", "SDI mode to use", GST_TYPE_AJA_SDI_MODE,
DEFAULT_SDI_MODE,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT)));
g_object_class_install_property(
gobject_class, PROP_TIMECODE_INDEX,
g_param_spec_enum(
"timecode-index", "Timecode Index", "Timecode index to use",
GST_TYPE_AJA_TIMECODE_INDEX, DEFAULT_TIMECODE_INDEX,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT)));
g_object_class_install_property(
gobject_class, PROP_RP188,
g_param_spec_boolean(
"rp188", "RP188", "Enable RP188 timecode transmission", DEFAULT_RP188,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT)));
g_object_class_install_property(
gobject_class, PROP_REFERENCE_SOURCE,
g_param_spec_enum(
"reference-source", "Reference Source", "Reference source to use",
GST_TYPE_AJA_REFERENCE_SOURCE, DEFAULT_REFERENCE_SOURCE,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT)));
g_object_class_install_property(
gobject_class, PROP_CEA608_LINE_NUMBER,
g_param_spec_uint(
"cea608-line-number", "CEA608 Line Number",
"Sets the line number to use for CEA608 S334-1 Annex A Closed "
"Captions "
"(-1=disabled)",
0, G_MAXUINT, DEFAULT_CEA608_LINE_NUMBER,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT)));
g_object_class_install_property(
gobject_class, PROP_CEA708_LINE_NUMBER,
g_param_spec_uint(
"cea708-line-number", "CEA708 Line Number",
"Sets the line number to use for CEA708 S334-2 Closed Captions "
"(-1=disabled)",
0, G_MAXUINT, DEFAULT_CEA608_LINE_NUMBER,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT)));
g_object_class_install_property(
gobject_class, PROP_OUTPUT_CPU_CORE,
g_param_spec_uint(
"output-cpu-core", "Output CPU Core",
"Sets the affinity of the output thread to this CPU core "
"(-1=disabled)",
0, G_MAXUINT, DEFAULT_OUTPUT_CPU_CORE,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT)));
element_class->change_state = GST_DEBUG_FUNCPTR(gst_aja_sink_change_state);
basesink_class->set_caps = GST_DEBUG_FUNCPTR(gst_aja_sink_set_caps);
basesink_class->get_caps = GST_DEBUG_FUNCPTR(gst_aja_sink_get_caps);
basesink_class->event = GST_DEBUG_FUNCPTR(gst_aja_sink_event);
basesink_class->propose_allocation =
GST_DEBUG_FUNCPTR(gst_aja_sink_propose_allocation);
basesink_class->render = GST_DEBUG_FUNCPTR(gst_aja_sink_render);
templ_caps = gst_ntv2_supported_caps(DEVICE_ID_INVALID);
gst_element_class_add_pad_template(
element_class,
gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS, templ_caps));
gst_caps_unref(templ_caps);
gst_element_class_set_static_metadata(
element_class, "AJA audio/video sink", "Audio/Video/Sink",
"Outputs audio/video frames with AJA devices",
"Sebastian Dröge <[email protected]>");
GST_DEBUG_CATEGORY_INIT(gst_aja_sink_debug, "ajasink", 0, "AJA sink");
}
static void gst_aja_sink_init(GstAjaSink *self) {
g_mutex_init(&self->queue_lock);
g_cond_init(&self->queue_cond);
g_cond_init(&self->drain_cond);
self->device_identifier = g_strdup(DEFAULT_DEVICE_IDENTIFIER);
self->channel = DEFAULT_CHANNEL;
self->queue_size = DEFAULT_QUEUE_SIZE;
self->start_frame = DEFAULT_START_FRAME;
self->end_frame = DEFAULT_END_FRAME;
self->audio_system_setting = DEFAULT_AUDIO_SYSTEM;
self->output_destination = DEFAULT_OUTPUT_DESTINATION;
self->timecode_index = DEFAULT_TIMECODE_INDEX;
self->reference_source = DEFAULT_REFERENCE_SOURCE;
self->output_cpu_core = DEFAULT_OUTPUT_CPU_CORE;
self->queue =
gst_queue_array_new_for_struct(sizeof(QueueItem), self->queue_size);
}
void gst_aja_sink_set_property(GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec) {
GstAjaSink *self = GST_AJA_SINK(object);
switch (property_id) {
case PROP_DEVICE_IDENTIFIER:
g_free(self->device_identifier);
self->device_identifier = g_value_dup_string(value);
break;
case PROP_CHANNEL:
self->channel = (NTV2Channel)g_value_get_uint(value);
break;
case PROP_QUEUE_SIZE:
self->queue_size = g_value_get_uint(value);
break;
case PROP_START_FRAME:
self->start_frame = g_value_get_uint(value);
break;
case PROP_END_FRAME:
self->end_frame = g_value_get_uint(value);
break;
case PROP_AUDIO_SYSTEM:
self->audio_system_setting = (GstAjaAudioSystem)g_value_get_enum(value);
break;
case PROP_OUTPUT_DESTINATION:
self->output_destination =
(GstAjaOutputDestination)g_value_get_enum(value);
break;
case PROP_SDI_MODE:
self->sdi_mode = (GstAjaSdiMode)g_value_get_enum(value);
break;
case PROP_TIMECODE_INDEX:
self->timecode_index = (GstAjaTimecodeIndex)g_value_get_enum(value);
break;
case PROP_RP188:
self->rp188 = g_value_get_boolean(value);
break;
case PROP_REFERENCE_SOURCE:
self->reference_source = (GstAjaReferenceSource)g_value_get_enum(value);
break;
case PROP_CEA608_LINE_NUMBER:
self->cea608_line_number = g_value_get_uint(value);
break;
case PROP_CEA708_LINE_NUMBER:
self->cea708_line_number = g_value_get_uint(value);
break;
case PROP_OUTPUT_CPU_CORE:
self->output_cpu_core = g_value_get_uint(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
break;
}
}
void gst_aja_sink_get_property(GObject *object, guint property_id,
GValue *value, GParamSpec *pspec) {
GstAjaSink *self = GST_AJA_SINK(object);
switch (property_id) {
case PROP_DEVICE_IDENTIFIER:
g_value_set_string(value, self->device_identifier);
break;
case PROP_CHANNEL:
g_value_set_uint(value, self->channel);
break;
case PROP_QUEUE_SIZE:
g_value_set_uint(value, self->queue_size);
break;
case PROP_START_FRAME:
g_value_set_uint(value, self->start_frame);
break;
case PROP_END_FRAME:
g_value_set_uint(value, self->end_frame);
break;
case PROP_AUDIO_SYSTEM:
g_value_set_enum(value, self->audio_system_setting);
break;
case PROP_OUTPUT_DESTINATION:
g_value_set_enum(value, self->output_destination);
break;
case PROP_SDI_MODE:
g_value_set_enum(value, self->sdi_mode);
break;
case PROP_TIMECODE_INDEX:
g_value_set_enum(value, self->timecode_index);
break;
case PROP_RP188:
g_value_set_boolean(value, self->rp188);
break;
case PROP_REFERENCE_SOURCE:
g_value_set_enum(value, self->reference_source);
break;
case PROP_CEA608_LINE_NUMBER:
g_value_set_uint(value, self->cea608_line_number);
break;
case PROP_CEA708_LINE_NUMBER:
g_value_set_uint(value, self->cea708_line_number);
break;
case PROP_OUTPUT_CPU_CORE:
g_value_set_uint(value, self->output_cpu_core);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
break;
}
}
void gst_aja_sink_finalize(GObject *object) {
GstAjaSink *self = GST_AJA_SINK(object);
g_assert(self->device == NULL);
g_assert(gst_queue_array_get_length(self->queue) == 0);
g_clear_pointer(&self->queue, gst_queue_array_free);
g_mutex_clear(&self->queue_lock);
g_cond_clear(&self->queue_cond);
g_cond_clear(&self->drain_cond);
G_OBJECT_CLASS(parent_class)->finalize(object);
}
static gboolean gst_aja_sink_open(GstAjaSink *self) {
GST_DEBUG_OBJECT(self, "Opening device");
g_assert(self->device == NULL);
self->device = gst_aja_ntv2_device_obtain(self->device_identifier);
if (!self->device) {
GST_ERROR_OBJECT(self, "Failed to open device");
return FALSE;
}
if (!self->device->device->IsDeviceReady(false)) {
g_clear_pointer(&self->device, gst_aja_ntv2_device_unref);
return FALSE;
}
self->device->device->SetEveryFrameServices(::NTV2_OEM_TASKS);
self->device_id = self->device->device->GetDeviceID();
std::string serial_number;
if (!self->device->device->GetSerialNumberString(serial_number))
serial_number = "none";
GST_DEBUG_OBJECT(self,
"Opened device with ID %d at index %d (%s, version %s, "
"serial number %s, can do VANC %d)",
self->device_id, self->device->device->GetIndexNumber(),
self->device->device->GetDisplayName().c_str(),
self->device->device->GetDeviceVersionString().c_str(),
serial_number.c_str(),
::NTV2DeviceCanDoCustomAnc(self->device_id));
GST_DEBUG_OBJECT(self,
"Using SDK version %d.%d.%d.%d (%s) and driver version %s",
AJA_NTV2_SDK_VERSION_MAJOR, AJA_NTV2_SDK_VERSION_MINOR,
AJA_NTV2_SDK_VERSION_POINT, AJA_NTV2_SDK_BUILD_NUMBER,
AJA_NTV2_SDK_BUILD_DATETIME,
self->device->device->GetDriverVersionString().c_str());
self->device->device->SetMultiFormatMode(true);
self->allocator = gst_aja_allocator_new(self->device);
GST_DEBUG_OBJECT(self, "Opened device");
return TRUE;
}
static gboolean gst_aja_sink_close(GstAjaSink *self) {
gst_clear_object(&self->allocator);
g_clear_pointer(&self->device, gst_aja_ntv2_device_unref);
self->device_id = DEVICE_ID_INVALID;
GST_DEBUG_OBJECT(self, "Closed device");
return TRUE;
}
static gboolean gst_aja_sink_start(GstAjaSink *self) {
GST_DEBUG_OBJECT(self, "Starting");
self->output_thread = new AJAThread();
self->output_thread->Attach(output_thread_func, self);
self->output_thread->SetPriority(AJA_ThreadPriority_High);
self->output_thread->Start();
g_mutex_lock(&self->queue_lock);
self->shutdown = FALSE;
self->playing = FALSE;
self->eos = FALSE;
g_cond_signal(&self->queue_cond);
g_mutex_unlock(&self->queue_lock);
return TRUE;
}
static gboolean gst_aja_sink_stop(GstAjaSink *self) {
QueueItem *item;
GST_DEBUG_OBJECT(self, "Stopping");
g_mutex_lock(&self->queue_lock);
self->shutdown = TRUE;
self->playing = FALSE;
g_cond_signal(&self->queue_cond);
g_mutex_unlock(&self->queue_lock);
if (self->output_thread) {
self->output_thread->Stop();
delete self->output_thread;
self->output_thread = NULL;
}
GST_OBJECT_LOCK(self);
gst_clear_caps(&self->configured_caps);
self->configured_audio_channels = 0;
GST_OBJECT_UNLOCK(self);
while ((item = (QueueItem *)gst_queue_array_pop_head_struct(self->queue))) {
if (item->type == QUEUE_ITEM_TYPE_FRAME) {
gst_buffer_unmap(item->video_buffer, &item->video_map);
gst_buffer_unref(item->video_buffer);
if (item->audio_buffer) {
gst_buffer_unmap(item->audio_buffer, &item->audio_map);
gst_buffer_unref(item->audio_buffer);
}
if (item->anc_buffer) {
gst_buffer_unmap(item->anc_buffer, &item->anc_map);
gst_buffer_unref(item->anc_buffer);
}
if (item->anc_buffer2) {
gst_buffer_unmap(item->anc_buffer2, &item->anc_map2);
gst_buffer_unref(item->anc_buffer2);
}
}
}
if (self->buffer_pool) {
gst_buffer_pool_set_active(self->buffer_pool, FALSE);
gst_clear_object(&self->buffer_pool);
}
if (self->audio_buffer_pool) {
gst_buffer_pool_set_active(self->audio_buffer_pool, FALSE);
gst_clear_object(&self->audio_buffer_pool);
}
if (self->anc_buffer_pool) {
gst_buffer_pool_set_active(self->anc_buffer_pool, FALSE);
gst_clear_object(&self->anc_buffer_pool);
}
if (self->tc_indexes) {
delete self->tc_indexes;
self->tc_indexes = NULL;
}
GST_DEBUG_OBJECT(self, "Stopped");
return TRUE;
}
static GstStateChangeReturn gst_aja_sink_change_state(
GstElement *element, GstStateChange transition) {
GstAjaSink *self = GST_AJA_SINK(element);
GstStateChangeReturn ret;
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
if (!gst_aja_sink_open(self)) return GST_STATE_CHANGE_FAILURE;
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
if (!gst_aja_sink_start(self)) return GST_STATE_CHANGE_FAILURE;
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
break;
default:
break;
}
ret = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
if (ret == GST_STATE_CHANGE_FAILURE) return ret;
switch (transition) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
g_mutex_lock(&self->queue_lock);
self->playing = FALSE;
g_cond_signal(&self->queue_cond);
g_mutex_unlock(&self->queue_lock);
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
g_mutex_lock(&self->queue_lock);
self->playing = TRUE;
g_cond_signal(&self->queue_cond);
g_mutex_unlock(&self->queue_lock);
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
if (!gst_aja_sink_stop(self)) return GST_STATE_CHANGE_FAILURE;
break;
case GST_STATE_CHANGE_READY_TO_NULL:
if (!gst_aja_sink_close(self)) return GST_STATE_CHANGE_FAILURE;
break;
default:
break;
}
return ret;
}
static gboolean gst_aja_sink_set_caps(GstBaseSink *bsink, GstCaps *caps) {
GstAjaSink *self = GST_AJA_SINK(bsink);
const GstStructure *s;
NTV2VideoFormat video_format = ::NTV2_FORMAT_UNKNOWN;
GST_DEBUG_OBJECT(self, "Configuring caps %" GST_PTR_FORMAT, caps);
GST_OBJECT_LOCK(self);
if (self->configured_caps) {
if (!gst_caps_can_intersect(self->configured_caps, caps)) {
GST_DEBUG_OBJECT(self, "Need to reconfigure, waiting for draining");
GST_OBJECT_UNLOCK(self);
g_mutex_lock(&self->queue_lock);
self->draining = TRUE;
g_cond_signal(&self->queue_cond);
while (self->draining && !self->flushing && !self->shutdown) {
g_cond_wait(&self->drain_cond, &self->queue_lock);
}
if (self->flushing || self->shutdown) {
g_mutex_unlock(&self->queue_lock);
GST_DEBUG_OBJECT(self, "Flushing");
return FALSE;
}
g_mutex_unlock(&self->queue_lock);
GST_OBJECT_LOCK(self);
} else {
GST_OBJECT_UNLOCK(self);
GST_DEBUG_OBJECT(self,
"Compatible caps with previous caps, not reconfiguring");
return TRUE;
}
}
if (!gst_video_info_from_caps(&self->configured_info, caps)) {
GST_OBJECT_UNLOCK(self);
GST_FIXME_OBJECT(self, "Failed to parse caps");
return FALSE;
}
self->configured_audio_channels = 0;
s = gst_caps_get_structure(caps, 0);
gst_structure_get_int(s, "audio-channels", &self->configured_audio_channels);
gst_caps_replace(&self->configured_caps, caps);
GST_OBJECT_UNLOCK(self);
bool quad_mode = (self->sdi_mode != GST_AJA_SDI_MODE_SINGLE_LINK);
video_format = gst_ntv2_video_format_from_caps(caps, quad_mode);
if (video_format == NTV2_FORMAT_UNKNOWN) {
GST_ERROR_OBJECT(self, "Unsupported caps %" GST_PTR_FORMAT, caps);
return FALSE;
}
if (quad_mode) {
if (self->output_destination != GST_AJA_OUTPUT_DESTINATION_AUTO) {
GST_ERROR_OBJECT(self,
"Quad modes require usage of the channel's default "
"output destination");
return FALSE;
}
if (self->channel != ::NTV2_CHANNEL1 && self->channel != ::NTV2_CHANNEL5) {
GST_ERROR_OBJECT(self, "Quad modes require channels 1 or 5");
return FALSE;
}
}
self->quad_mode = quad_mode;
self->video_format = video_format;
g_assert(self->device != NULL);
// Make sure to globally lock here as the routing settings and others are
// global shared state
ShmMutexLocker locker;
if (!::NTV2DeviceCanDoVideoFormat(self->device_id, video_format)) {
GST_ERROR_OBJECT(self, "Device does not support mode %d",
(int)video_format);
return FALSE;
}
self->device->device->SetMode(self->channel, NTV2_MODE_DISPLAY, false);
if (self->quad_mode) {
for (int i = 1; i < 4; i++)
self->device->device->SetMode((NTV2Channel)(self->channel + i),
NTV2_MODE_DISPLAY, false);
}
GST_DEBUG_OBJECT(self, "Configuring video format %d on channel %d",
(int)video_format, (int)self->channel);
self->device->device->SetVideoFormat(video_format, false, false,
self->channel);
if (self->quad_mode) {
for (int i = 1; i < 4; i++)
self->device->device->SetVideoFormat(video_format, false, false,
(NTV2Channel)(self->channel + i));
}
if (!::NTV2DeviceCanDoFrameBufferFormat(self->device_id,
::NTV2_FBF_10BIT_YCBCR)) {
GST_ERROR_OBJECT(self, "Device does not support frame buffer format %d",
(int)::NTV2_FBF_10BIT_YCBCR);
return FALSE;
}
self->device->device->SetFrameBufferFormat(self->channel,
::NTV2_FBF_10BIT_YCBCR);
if (self->quad_mode) {
for (int i = 1; i < 4; i++)
self->device->device->SetFrameBufferFormat(
(NTV2Channel)(self->channel + i), ::NTV2_FBF_10BIT_YCBCR);
}
NTV2ReferenceSource reference_source;
switch (self->reference_source) {
case GST_AJA_REFERENCE_SOURCE_EXTERNAL:
reference_source = ::NTV2_REFERENCE_EXTERNAL;
break;
case GST_AJA_REFERENCE_SOURCE_FREERUN:
case GST_AJA_REFERENCE_SOURCE_AUTO:
reference_source = ::NTV2_REFERENCE_FREERUN;
break;
case GST_AJA_REFERENCE_SOURCE_INPUT_1:
reference_source = ::NTV2_REFERENCE_INPUT1;
break;
case GST_AJA_REFERENCE_SOURCE_INPUT_2:
reference_source = ::NTV2_REFERENCE_INPUT2;
break;
case GST_AJA_REFERENCE_SOURCE_INPUT_3:
reference_source = ::NTV2_REFERENCE_INPUT3;
break;
case GST_AJA_REFERENCE_SOURCE_INPUT_4:
reference_source = ::NTV2_REFERENCE_INPUT4;
break;
case GST_AJA_REFERENCE_SOURCE_INPUT_5:
reference_source = ::NTV2_REFERENCE_INPUT5;
break;
case GST_AJA_REFERENCE_SOURCE_INPUT_6:
reference_source = ::NTV2_REFERENCE_INPUT6;
break;
case GST_AJA_REFERENCE_SOURCE_INPUT_7:
reference_source = ::NTV2_REFERENCE_INPUT7;
break;
case GST_AJA_REFERENCE_SOURCE_INPUT_8:
reference_source = ::NTV2_REFERENCE_INPUT8;
break;
default:
g_assert_not_reached();
break;
}
GST_DEBUG_OBJECT(self, "Configuring reference source %d",
(int)reference_source);
self->device->device->SetFramePulseReference(reference_source);
self->device->device->DMABufferAutoLock(false, true, 0);
if (::NTV2DeviceHasBiDirectionalSDI(self->device_id))
self->device->device->SetSDITransmitEnable(self->channel, true);
if (self->quad_mode) {
for (int i = 1; i < 4; i++)
self->device->device->SetSDITransmitEnable(
(NTV2Channel)(self->channel + i), true);
}
if (self->configured_audio_channels) {
switch (self->audio_system_setting) {
case GST_AJA_AUDIO_SYSTEM_1:
self->audio_system = ::NTV2_AUDIOSYSTEM_1;
break;
case GST_AJA_AUDIO_SYSTEM_2:
self->audio_system = ::NTV2_AUDIOSYSTEM_2;
break;
case GST_AJA_AUDIO_SYSTEM_3:
self->audio_system = ::NTV2_AUDIOSYSTEM_3;
break;
case GST_AJA_AUDIO_SYSTEM_4:
self->audio_system = ::NTV2_AUDIOSYSTEM_4;
break;
case GST_AJA_AUDIO_SYSTEM_5:
self->audio_system = ::NTV2_AUDIOSYSTEM_5;
break;
case GST_AJA_AUDIO_SYSTEM_6:
self->audio_system = ::NTV2_AUDIOSYSTEM_6;
break;
case GST_AJA_AUDIO_SYSTEM_7:
self->audio_system = ::NTV2_AUDIOSYSTEM_7;
break;
case GST_AJA_AUDIO_SYSTEM_8:
self->audio_system = ::NTV2_AUDIOSYSTEM_8;
break;
case GST_AJA_AUDIO_SYSTEM_AUTO:
self->audio_system = ::NTV2_AUDIOSYSTEM_1;
if (::NTV2DeviceGetNumAudioSystems(self->device_id) > 1)
self->audio_system = ::NTV2ChannelToAudioSystem(self->channel);
if (!::NTV2DeviceCanDoFrameStore1Display(self->device_id))
self->audio_system = ::NTV2_AUDIOSYSTEM_1;
break;
default:
g_assert_not_reached();
break;
}
GST_DEBUG_OBJECT(self, "Using audio system %d", self->audio_system);
self->device->device->SetNumberAudioChannels(
self->configured_audio_channels, self->audio_system);
self->device->device->SetAudioRate(::NTV2_AUDIO_48K, self->audio_system);
self->device->device->SetAudioBufferSize(::NTV2_AUDIO_BUFFER_BIG,
self->audio_system);
self->device->device->SetSDIOutputAudioSystem(self->channel,
self->audio_system);
self->device->device->SetSDIOutputDS2AudioSystem(self->channel,
self->audio_system);
if (self->quad_mode) {
for (int i = 1; i < 4; i++) {
self->device->device->SetSDIOutputAudioSystem(
(NTV2Channel)(self->channel + i), self->audio_system);
self->device->device->SetSDIOutputDS2AudioSystem(
(NTV2Channel)(self->channel + i), self->audio_system);
}
}
self->device->device->SetAudioLoopBack(::NTV2_AUDIO_LOOPBACK_OFF,
self->audio_system);
} else {
self->audio_system = ::NTV2_AUDIOSYSTEM_INVALID;
}
// Always use the framebuffer associated with the channel
NTV2OutputCrosspointID framebuffer_id =
::GetFrameBufferOutputXptFromChannel(self->channel, false, false);
NTV2VANCMode vanc_mode;
NTV2TCIndex tc_indexes_vitc[2] = {::NTV2_TCINDEX_INVALID,
::NTV2_TCINDEX_INVALID};
NTV2TCIndex tc_index_atc_ltc = ::NTV2_TCINDEX_INVALID;
NTV2InputCrosspointID output_destination_id;
switch (self->output_destination) {
case GST_AJA_OUTPUT_DESTINATION_AUTO:
tc_indexes_vitc[0] =
::NTV2ChannelToTimecodeIndex(self->channel, false, false);
tc_indexes_vitc[1] =
::NTV2ChannelToTimecodeIndex(self->channel, false, true);
tc_index_atc_ltc =
::NTV2ChannelToTimecodeIndex(self->channel, false, true);
output_destination_id = ::GetSDIOutputInputXpt(self->channel, false);
vanc_mode = ::NTV2DeviceCanDoCustomAnc(self->device_id)
? ::NTV2_VANCMODE_OFF
: ::NTV2_VANCMODE_TALL;
break;
case GST_AJA_OUTPUT_DESTINATION_SDI1:
tc_indexes_vitc[0] = ::NTV2_TCINDEX_SDI1;
tc_indexes_vitc[1] = ::NTV2_TCINDEX_SDI1_2;
tc_index_atc_ltc = ::NTV2_TCINDEX_SDI1_LTC;
output_destination_id = ::NTV2_XptSDIOut1Input;
vanc_mode = ::NTV2_VANCMODE_TALL;
break;
case GST_AJA_OUTPUT_DESTINATION_SDI2:
tc_indexes_vitc[0] = ::NTV2_TCINDEX_SDI2;
tc_indexes_vitc[1] = ::NTV2_TCINDEX_SDI2_2;
tc_index_atc_ltc = ::NTV2_TCINDEX_SDI2_LTC;
output_destination_id = ::NTV2_XptSDIOut2Input;
vanc_mode = ::NTV2_VANCMODE_TALL;
break;
case GST_AJA_OUTPUT_DESTINATION_SDI3:
tc_indexes_vitc[0] = ::NTV2_TCINDEX_SDI3;
tc_indexes_vitc[1] = ::NTV2_TCINDEX_SDI3_2;
tc_index_atc_ltc = ::NTV2_TCINDEX_SDI3_LTC;
output_destination_id = ::NTV2_XptSDIOut3Input;
vanc_mode = ::NTV2_VANCMODE_TALL;
break;
case GST_AJA_OUTPUT_DESTINATION_SDI4:
tc_indexes_vitc[0] = ::NTV2_TCINDEX_SDI4;
tc_indexes_vitc[1] = ::NTV2_TCINDEX_SDI4_2;
tc_index_atc_ltc = ::NTV2_TCINDEX_SDI4_LTC;
output_destination_id = ::NTV2_XptSDIOut4Input;
vanc_mode = ::NTV2_VANCMODE_TALL;
break;
case GST_AJA_OUTPUT_DESTINATION_SDI5:
tc_indexes_vitc[0] = ::NTV2_TCINDEX_SDI5;
tc_indexes_vitc[1] = ::NTV2_TCINDEX_SDI5_2;
tc_index_atc_ltc = ::NTV2_TCINDEX_SDI5_LTC;
output_destination_id = ::NTV2_XptSDIOut5Input;
vanc_mode = ::NTV2_VANCMODE_TALL;
break;
case GST_AJA_OUTPUT_DESTINATION_SDI6:
tc_indexes_vitc[0] = ::NTV2_TCINDEX_SDI6;
tc_indexes_vitc[1] = ::NTV2_TCINDEX_SDI6_2;
tc_index_atc_ltc = ::NTV2_TCINDEX_SDI6_LTC;
output_destination_id = ::NTV2_XptSDIOut6Input;
vanc_mode = ::NTV2_VANCMODE_TALL;
break;
case GST_AJA_OUTPUT_DESTINATION_SDI7:
tc_indexes_vitc[0] = ::NTV2_TCINDEX_SDI7;
tc_indexes_vitc[1] = ::NTV2_TCINDEX_SDI7_2;
tc_index_atc_ltc = ::NTV2_TCINDEX_SDI7_LTC;
output_destination_id = ::NTV2_XptSDIOut7Input;
vanc_mode = ::NTV2_VANCMODE_TALL;
break;
case GST_AJA_OUTPUT_DESTINATION_SDI8:
tc_indexes_vitc[0] = ::NTV2_TCINDEX_SDI8;
tc_indexes_vitc[1] = ::NTV2_TCINDEX_SDI8_2;
tc_index_atc_ltc = ::NTV2_TCINDEX_SDI8_LTC;
output_destination_id = ::NTV2_XptSDIOut8Input;
vanc_mode = ::NTV2_VANCMODE_TALL;
break;
case GST_AJA_OUTPUT_DESTINATION_ANALOG:
output_destination_id = ::NTV2_XptAnalogOutInput;
vanc_mode = ::NTV2_VANCMODE_TALL;
break;
case GST_AJA_OUTPUT_DESTINATION_HDMI:
output_destination_id = ::NTV2_XptHDMIOutInput;
vanc_mode = ::NTV2_VANCMODE_OFF;
break;
default:
g_assert_not_reached();
break;
}
if (!self->tc_indexes) self->tc_indexes = new NTV2TCIndexes;
switch (self->timecode_index) {
case GST_AJA_TIMECODE_INDEX_VITC:
self->tc_indexes->insert(tc_indexes_vitc[0]);
if (self->configured_info.interlace_mode !=
GST_VIDEO_INTERLACE_MODE_PROGRESSIVE)
self->tc_indexes->insert(tc_indexes_vitc[1]);
break;
case GST_AJA_TIMECODE_INDEX_ATC_LTC:
self->tc_indexes->insert(tc_index_atc_ltc);
break;
case GST_AJA_TIMECODE_INDEX_LTC1:
self->tc_indexes->insert(::NTV2_TCINDEX_LTC1);
break;
case GST_AJA_TIMECODE_INDEX_LTC2:
self->tc_indexes->insert(::NTV2_TCINDEX_LTC2);
break;
default:
g_assert_not_reached();
break;
}
const NTV2Standard standard(::GetNTV2StandardFromVideoFormat(video_format));
self->device->device->SetSDIOutputStandard(self->channel, standard);
if (self->quad_mode) {
for (int i = 1; i < 4; i++)
self->device->device->SetSDIOutputStandard(
(NTV2Channel)(self->channel + i), standard);
}
const NTV2FrameGeometry geometry =
::GetNTV2FrameGeometryFromVideoFormat(video_format);
self->vanc_mode =
::HasVANCGeometries(geometry) ? vanc_mode : ::NTV2_VANCMODE_OFF;
if (self->vanc_mode == ::NTV2_VANCMODE_OFF) {
self->device->device->SetFrameGeometry(geometry, false, self->channel);
self->device->device->SetVANCMode(self->vanc_mode, self->channel);
if (self->quad_mode) {
for (int i = 1; i < 4; i++) {
self->device->device->SetFrameGeometry(
geometry, false, (NTV2Channel)(self->channel + i));
self->device->device->SetVANCMode(self->vanc_mode,
(NTV2Channel)(self->channel + i));
}
}
} else {
const NTV2FrameGeometry vanc_geometry =
::GetVANCFrameGeometry(geometry, self->vanc_mode);
self->device->device->SetFrameGeometry(vanc_geometry, false, self->channel);
self->device->device->SetVANCMode(self->vanc_mode, self->channel);
if (self->quad_mode) {
for (int i = 1; i < 4; i++) {
self->device->device->SetFrameGeometry(
vanc_geometry, false, (NTV2Channel)(self->channel + i));
self->device->device->SetVANCMode(self->vanc_mode,
(NTV2Channel)(self->channel + i));
}
}
}
bool had_quad_enabled = false, had_quad_quad_enabled = false;
if (self->channel < ::NTV2_CHANNEL5) {
self->device->device->GetQuadFrameEnable(had_quad_enabled, ::NTV2_CHANNEL1);
self->device->device->GetQuadQuadFrameEnable(had_quad_quad_enabled,
::NTV2_CHANNEL1);
} else {
self->device->device->GetQuadFrameEnable(had_quad_enabled, ::NTV2_CHANNEL5);
self->device->device->GetQuadQuadFrameEnable(had_quad_quad_enabled,
::NTV2_CHANNEL5);
}
if (self->quad_mode) {
switch (self->sdi_mode) {
case GST_AJA_SDI_MODE_SINGLE_LINK:
g_assert_not_reached();
break;
case GST_AJA_SDI_MODE_QUAD_LINK_SQD:
if (self->configured_info.height > 2160) {
self->device->device->Set4kSquaresEnable(false, self->channel);
self->device->device->SetTsiFrameEnable(false, self->channel);