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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
|
/* GStreamer
* Copyright (C) 2003 Julien Moutte <julien@moutte.net>
* Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
* Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
* Copyright (C) 2007 Wim Taymans <wim.taymans@gmail.com>
* Copyright (C) 2007 Andy Wingo <wingo@pobox.com>
* Copyright (C) 2008 Nokia Corporation. (contact <stefan.kost@nokia.com>)
*
* 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/**
* SECTION:element-input-selector
* @short_description: N-to-1 stream selectoring
* @see_also: #GstOutputSelector
*
* Direct one out of N input streams to the output pad.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include "gstinputselector.h"
#include "gstselector-marshal.h"
GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
#define GST_CAT_DEFAULT input_selector_debug
static const GstElementDetails gst_input_selector_details =
GST_ELEMENT_DETAILS ("Input selector",
"Generic",
"N-to-1 input stream selectoring",
"Julien Moutte <julien@moutte.net>\n"
"Ronald S. Bultje <rbultje@ronald.bitfreak.net>\n"
"Jan Schmidt <thaytan@mad.scientist.com>\n"
"Wim Taymans <wim.taymans@gmail.com>");
static GstStaticPadTemplate gst_input_selector_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink%d",
GST_PAD_SINK,
GST_PAD_REQUEST,
GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate gst_input_selector_src_factory =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
enum
{
PROP_0,
PROP_N_PADS,
PROP_ACTIVE_PAD,
PROP_SELECT_ALL,
PROP_LAST
};
enum
{
PROP_PAD_0,
PROP_PAD_RUNNING_TIME,
PROP_PAD_TAGS,
PROP_PAD_ACTIVE,
PROP_PAD_LAST
};
enum
{
/* methods */
SIGNAL_BLOCK,
SIGNAL_SWITCH,
LAST_SIGNAL
};
static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
static gboolean gst_input_selector_is_active_sinkpad (GstInputSelector * sel,
GstPad * pad);
static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
GstPad * pad);
static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
gboolean strict);
static void gst_input_selector_push_pending_stop (GstInputSelector * self);
static gboolean gst_input_selector_check_eos (GstElement * selector);
#define GST_TYPE_SELECTOR_PAD \
(gst_selector_pad_get_type())
#define GST_SELECTOR_PAD(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
#define GST_SELECTOR_PAD_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
#define GST_IS_SELECTOR_PAD(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
#define GST_IS_SELECTOR_PAD_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
#define GST_SELECTOR_PAD_CAST(obj) \
((GstSelectorPad *)(obj))
typedef struct _GstSelectorPad GstSelectorPad;
typedef struct _GstSelectorPadClass GstSelectorPadClass;
struct _GstSelectorPad
{
GstPad parent;
gboolean active; /* when buffer have passed the pad */
gboolean eos; /* when EOS has been received */
GstSegment segment; /* the current segment on the pad */
GstTagList *tags; /* last tags received on the pad */
gboolean segment_pending;
};
struct _GstSelectorPadClass
{
GstPadClass parent;
};
static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
static void gst_selector_pad_init (GstSelectorPad * pad);
static void gst_selector_pad_finalize (GObject * object);
static void gst_selector_pad_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec);
static GstPadClass *selector_pad_parent_class = NULL;
static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
static void gst_selector_pad_reset (GstSelectorPad * pad);
static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
static GList *gst_selector_pad_get_linked_pads (GstPad * pad);
static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
static GType
gst_selector_pad_get_type (void)
{
static GType selector_pad_type = 0;
if (!selector_pad_type) {
static const GTypeInfo selector_pad_info = {
sizeof (GstSelectorPadClass),
NULL,
NULL,
(GClassInitFunc) gst_selector_pad_class_init,
NULL,
NULL,
sizeof (GstSelectorPad),
0,
(GInstanceInitFunc) gst_selector_pad_init,
};
selector_pad_type =
g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
&selector_pad_info, 0);
}
return selector_pad_type;
}
static void
gst_selector_pad_class_init (GstSelectorPadClass * klass)
{
GObjectClass *gobject_class;
gobject_class = (GObjectClass *) klass;
selector_pad_parent_class = g_type_class_peek_parent (klass);
gobject_class->finalize = gst_selector_pad_finalize;
gobject_class->get_property =
GST_DEBUG_FUNCPTR (gst_selector_pad_get_property);
g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
g_param_spec_int64 ("running-time", "Running time",
"Running time of stream on pad", 0, G_MAXINT64, 0, G_PARAM_READABLE));
g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
g_param_spec_boxed ("tags", "Tags",
"The currently active tags on the pad", GST_TYPE_TAG_LIST,
G_PARAM_READABLE));
g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
g_param_spec_boolean ("active", "Active",
"If the pad is currently active", FALSE, G_PARAM_READABLE));
}
static void
gst_selector_pad_init (GstSelectorPad * pad)
{
gst_selector_pad_reset (pad);
}
static void
gst_selector_pad_finalize (GObject * object)
{
GstSelectorPad *pad;
pad = GST_SELECTOR_PAD_CAST (object);
if (pad->tags)
gst_tag_list_free (pad->tags);
G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
}
static void
gst_selector_pad_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
switch (prop_id) {
case PROP_PAD_RUNNING_TIME:
g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
break;
case PROP_PAD_TAGS:
GST_OBJECT_LOCK (object);
g_value_set_boxed (value, spad->tags);
GST_OBJECT_UNLOCK (object);
break;
case PROP_PAD_ACTIVE:
{
GstInputSelector *sel;
sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
GST_PAD_CAST (spad)));
gst_object_unref (sel);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gint64
gst_selector_pad_get_running_time (GstSelectorPad * pad)
{
gint64 ret = 0;
GST_OBJECT_LOCK (pad);
if (pad->active) {
gint64 last_stop = pad->segment.last_stop;
if (last_stop >= 0)
ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
last_stop);
}
GST_OBJECT_UNLOCK (pad);
GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
GST_TIME_ARGS (ret));
return ret;
}
static void
gst_selector_pad_reset (GstSelectorPad * pad)
{
pad->active = FALSE;
pad->eos = FALSE;
gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
}
/* strictly get the linked pad from the sinkpad. If the pad is active we return
* the srcpad else we return NULL */
static GList *
gst_selector_pad_get_linked_pads (GstPad * pad)
{
GstPad *otherpad;
otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
if (!otherpad)
return NULL;
/* need to drop the ref, internal linked pads is not MT safe */
gst_object_unref (otherpad);
return g_list_append (NULL, otherpad);
}
static gboolean
gst_selector_pad_event (GstPad * pad, GstEvent * event)
{
gboolean res = TRUE;
gboolean forward = TRUE;
GstInputSelector *sel;
GstSelectorPad *selpad;
sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
selpad = GST_SELECTOR_PAD_CAST (pad);
/* only forward if we are dealing with the active sinkpad */
forward = gst_input_selector_is_active_sinkpad (sel, pad);
/* forward all events in select_all mode by default */
if (sel->select_all) {
forward = TRUE;
}
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_FLUSH_STOP:
gst_selector_pad_reset (selpad);
break;
case GST_EVENT_NEWSEGMENT:
{
gboolean update;
GstFormat format;
gdouble rate, arate;
gint64 start, stop, time;
gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
&start, &stop, &time);
GST_DEBUG_OBJECT (sel,
"configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
"format %d, "
"%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
gst_segment_set_newsegment_full (&selpad->segment, update,
rate, arate, format, start, stop, time);
/* if we are not going to forward the segment, mark the segment as
* pending */
if (!forward)
selpad->segment_pending = TRUE;
break;
}
case GST_EVENT_TAG:
{
GstTagList *tags;
GST_OBJECT_LOCK (selpad);
if (selpad->tags)
gst_tag_list_free (selpad->tags);
gst_event_parse_tag (event, &tags);
if (tags)
tags = gst_tag_list_copy (tags);
selpad->tags = tags;
GST_DEBUG_OBJECT (sel, "received tags %" GST_PTR_FORMAT, selpad->tags);
GST_OBJECT_UNLOCK (selpad);
break;
}
case GST_EVENT_EOS:
selpad->eos = TRUE;
/* don't forward eos in select_all mode until all sink pads have eos */
if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
forward = FALSE;
}
break;
default:
break;
}
if (forward)
res = gst_pad_push_event (sel->srcpad, event);
else
gst_event_unref (event);
gst_object_unref (sel);
return res;
}
static GstCaps *
gst_selector_pad_getcaps (GstPad * pad)
{
GstInputSelector *sel;
GstCaps *caps;
sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
caps = gst_pad_peer_get_caps (sel->srcpad);
if (caps == NULL)
caps = gst_caps_new_any ();
gst_object_unref (sel);
return caps;
}
static GstFlowReturn
gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
guint size, GstCaps * caps, GstBuffer ** buf)
{
GstInputSelector *sel;
GstFlowReturn result;
GstPad *active_sinkpad;
sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
/* Fallback allocation for buffers from pads except the selected one */
if (pad != active_sinkpad && !sel->select_all) {
GST_DEBUG_OBJECT (sel,
"Pad %s:%s is not selected. Performing fallback allocation",
GST_DEBUG_PAD_NAME (pad));
*buf = NULL;
result = GST_FLOW_OK;
} else {
result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
/* FIXME: HACK. If buffer alloc returns not-linked, perform a fallback
* allocation. This should NOT be necessary, because playbin should
* properly block the source pad from running until it's finished hooking
* everything up, but playbin needs refactoring first. */
if (result == GST_FLOW_NOT_LINKED) {
GST_DEBUG_OBJECT (sel,
"No peer pad yet - performing fallback allocation for pad %s:%s",
GST_DEBUG_PAD_NAME (pad));
*buf = NULL;
result = GST_FLOW_OK;
}
}
gst_object_unref (sel);
return result;
}
static gboolean
gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
{
gboolean flushing;
GST_OBJECT_LOCK (self);
while (self->blocked)
g_cond_wait (self->blocked_cond, GST_OBJECT_GET_LOCK (self));
GST_OBJECT_UNLOCK (self);
GST_OBJECT_LOCK (pad);
flushing = GST_PAD_IS_FLUSHING (pad);
GST_OBJECT_UNLOCK (pad);
return flushing;
}
static GstFlowReturn
gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
{
GstInputSelector *sel;
GstFlowReturn res;
GstPad *active_sinkpad;
GstSelectorPad *selpad;
GstClockTime end_time, duration;
GstSegment *seg;
sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
selpad = GST_SELECTOR_PAD_CAST (pad);
seg = &selpad->segment;
if (gst_input_selector_wait (sel, pad))
goto ignore;
active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
end_time = GST_BUFFER_TIMESTAMP (buf);
if (GST_CLOCK_TIME_IS_VALID (end_time)) {
duration = GST_BUFFER_DURATION (buf);
if (GST_CLOCK_TIME_IS_VALID (duration))
end_time += duration;
GST_DEBUG_OBJECT (sel, "received end time %" GST_TIME_FORMAT,
GST_TIME_ARGS (end_time));
gst_segment_set_last_stop (seg, seg->format, end_time);
}
/* Ignore buffers from pads except the selected one */
if (pad != active_sinkpad && !sel->select_all)
goto ignore;
gst_input_selector_push_pending_stop (sel);
/* if we have a pending segment, push it out now */
if (selpad->segment_pending) {
gst_pad_push_event (sel->srcpad, gst_event_new_new_segment_full (FALSE,
seg->rate, seg->applied_rate, seg->format, seg->start, seg->stop,
seg->time));
selpad->segment_pending = FALSE;
}
/* forward */
GST_DEBUG_OBJECT (sel, "Forwarding buffer %p from pad %s:%s", buf,
GST_DEBUG_PAD_NAME (pad));
res = gst_pad_push (sel->srcpad, buf);
done:
gst_object_unref (sel);
return res;
/* dropped buffers */
ignore:
{
GST_DEBUG_OBJECT (sel, "Ignoring buffer %p from pad %s:%s",
buf, GST_DEBUG_PAD_NAME (pad));
gst_buffer_unref (buf);
res = GST_FLOW_OK;
goto done;
}
}
static void gst_input_selector_dispose (GObject * object);
static void gst_input_selector_init (GstInputSelector * sel);
static void gst_input_selector_base_init (GstInputSelectorClass * klass);
static void gst_input_selector_class_init (GstInputSelectorClass * klass);
static void gst_input_selector_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_input_selector_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec);
static GstPad *gst_input_selector_request_new_pad (GstElement * element,
GstPadTemplate * templ, const gchar * unused);
static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
static GstStateChangeReturn gst_input_selector_change_state (GstElement *
element, GstStateChange transition);
static GList *gst_input_selector_get_linked_pads (GstPad * pad);
static GstCaps *gst_input_selector_getcaps (GstPad * pad);
static gint64 gst_input_selector_block (GstInputSelector * self);
static void gst_input_selector_switch (GstInputSelector * self,
GstPad * pad, gint64 stop_time, gint64 start_time);
static GstElementClass *parent_class = NULL;
GType
gst_input_selector_get_type (void)
{
static GType input_selector_type = 0;
if (!input_selector_type) {
static const GTypeInfo input_selector_info = {
sizeof (GstInputSelectorClass),
(GBaseInitFunc) gst_input_selector_base_init,
NULL,
(GClassInitFunc) gst_input_selector_class_init,
NULL,
NULL,
sizeof (GstInputSelector),
0,
(GInstanceInitFunc) gst_input_selector_init,
};
input_selector_type =
g_type_register_static (GST_TYPE_ELEMENT,
"GstInputSelector", &input_selector_info, 0);
GST_DEBUG_CATEGORY_INIT (input_selector_debug,
"input-selector", 0, "An input stream selector element");
}
return input_selector_type;
}
static void
gst_input_selector_base_init (GstInputSelectorClass * klass)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
gst_element_class_set_details (element_class, &gst_input_selector_details);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_input_selector_sink_factory));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_input_selector_src_factory));
}
static void
gst_input_selector_class_init (GstInputSelectorClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
gobject_class->dispose = gst_input_selector_dispose;
gobject_class->set_property =
GST_DEBUG_FUNCPTR (gst_input_selector_set_property);
gobject_class->get_property =
GST_DEBUG_FUNCPTR (gst_input_selector_get_property);
g_object_class_install_property (gobject_class, PROP_N_PADS,
g_param_spec_uint ("n-pads", "Number of Pads",
"The number of sink pads", 0, G_MAXUINT, 0, G_PARAM_READABLE));
g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
g_param_spec_object ("active-pad", "Active pad",
"The currently active sink pad", GST_TYPE_PAD, G_PARAM_READWRITE));
g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
g_param_spec_boolean ("select-all", "Select all mode",
"Forwards data from all input pads", FALSE, G_PARAM_READWRITE));
/**
* GstInputSelector::block:
* @inputselector: the #GstInputSelector
*
* Block all sink pads in preparation for a switch. Returns the stop time of
* the current switch segment, as a running time, or 0 if there is no current
* active pad or the current active pad never received data.
*/
gst_input_selector_signals[SIGNAL_BLOCK] =
g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
/**
* GstInputSelector::switch:
* @inputselector: the #GstInputSelector
* @pad: the pad to switch to
* @stop_time: running time at which to close the previous segment, or -1
* to use the running time of the previously active sink pad
* @start_time: running time at which to start the new segment, or -1 to
* use the running time of the newly active sink pad
*
* Switch to a new feed. The segment opened by the previously active pad, if
* any, will be closed, and a new segment opened before data flows again.
*
* This signal must be emitted when the element has been blocked via the <link
* linkend="GstInputSelector-block">block</link> signal.
*
* If you have a stream with only one switch element, such as an audio-only
* stream, a stream switch should be performed by first emitting the block
* signal, and then emitting the switch signal with -1 for the stop and start
* time values.
*
* The intention of the @stop_time and @start_time arguments is to allow
* multiple switch elements to switch and maintain stream synchronization.
* When switching a stream with multiple feeds, you will need as many switch
* elements as you have feeds. For example, a feed with audio and video will
* have one switch element between the audio feeds and one for video.
*
* A switch over multiple switch elements should be performed as follows:
* First, emit the <link linkend="GstInputSelector-block">block</link>
* signal, collecting the returned values. The maximum running time returned
* by block should then be used as the time at which to close the previous
* segment.
*
* Then, query the running times of the new audio and video pads that you will
* switch to. Naturally, these pads are on separate switch elements. Take the
* minimum running time for those streams and use it for the time at which to
* open the new segment.
*
* If @pad is the same as the current active pad, the element will cancel any
* previous block without adjusting segments.
*/
gst_input_selector_signals[SIGNAL_SWITCH] =
g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
NULL, NULL, gst_selector_marshal_VOID__OBJECT_INT64_INT64,
G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
gstelement_class->release_pad = gst_input_selector_release_pad;
gstelement_class->change_state = gst_input_selector_change_state;
klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
}
static void
gst_input_selector_init (GstInputSelector * sel)
{
sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
gst_pad_set_internal_link_function (sel->srcpad,
GST_DEBUG_FUNCPTR (gst_input_selector_get_linked_pads));
gst_pad_set_getcaps_function (sel->srcpad,
GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
/* sinkpad management */
sel->active_sinkpad = NULL;
sel->padcount = 0;
gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
sel->blocked_cond = g_cond_new ();
sel->blocked = FALSE;
sel->select_all = FALSE;
}
static void
gst_input_selector_dispose (GObject * object)
{
GstInputSelector *sel = GST_INPUT_SELECTOR (object);
if (sel->active_sinkpad) {
gst_object_unref (sel->active_sinkpad);
sel->active_sinkpad = NULL;
}
if (sel->blocked_cond) {
g_cond_free (sel->blocked_cond);
sel->blocked_cond = NULL;
}
G_OBJECT_CLASS (parent_class)->dispose (object);
}
/* Solve the following equation for B.timestamp, and set that as the segment
* stop:
* B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
*/
static gint64
gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
{
return (running_time - segment->accum) * segment->abs_rate + segment->start;
}
static void
gst_segment_set_stop (GstSegment * segment, gint64 running_time)
{
segment->stop = gst_segment_get_timestamp (segment, running_time);
segment->last_stop = -1;
}
static void
gst_segment_set_start (GstSegment * segment, gint64 running_time)
{
segment->start = gst_segment_get_timestamp (segment, running_time);
}
static void
gst_input_selector_set_active_pad (GstInputSelector * self,
GstPad * pad, gint64 stop_time, gint64 start_time)
{
GstSelectorPad *old, *new;
GstPad **active_pad_p;
GST_OBJECT_LOCK (self);
if (pad == self->active_sinkpad)
goto done;
old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
new = GST_SELECTOR_PAD_CAST (pad);
if (old && old->active && !self->pending_stop && stop_time >= 0) {
/* schedule a last_stop update if one isn't already scheduled, and a
segment has been pushed before. */
memcpy (&self->pending_stop_segment, &old->segment,
sizeof (self->pending_stop_segment));
gst_segment_set_stop (&self->pending_stop_segment, stop_time);
self->pending_stop = TRUE;
}
if (new && new->active && start_time >= 0) {
/* schedule a new segment push */
gst_segment_set_start (&new->segment, start_time);
new->segment_pending = TRUE;
}
active_pad_p = &self->active_sinkpad;
gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
self->active_sinkpad);
done:
GST_OBJECT_UNLOCK (self);
}
static void
gst_input_selector_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstInputSelector *sel = GST_INPUT_SELECTOR (object);
switch (prop_id) {
case PROP_ACTIVE_PAD:
gst_input_selector_set_active_pad (sel,
g_value_get_object (value), GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
break;
case PROP_SELECT_ALL:
sel->select_all = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_input_selector_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstInputSelector *sel = GST_INPUT_SELECTOR (object);
switch (prop_id) {
case PROP_N_PADS:
GST_OBJECT_LOCK (object);
g_value_set_uint (value, sel->n_pads);
GST_OBJECT_UNLOCK (object);
break;
case PROP_ACTIVE_PAD:
GST_OBJECT_LOCK (object);
g_value_set_object (value, sel->active_sinkpad);
GST_OBJECT_UNLOCK (object);
break;
case PROP_SELECT_ALL:
g_value_set_boolean (value, sel->select_all);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GstPad *
gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
{
GstInputSelector *sel;
GstPad *otherpad = NULL;
sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
GST_OBJECT_LOCK (sel);
if (pad == sel->srcpad)
otherpad = sel->active_sinkpad;
else if (pad == sel->active_sinkpad || !strict)
otherpad = sel->srcpad;
if (otherpad)
gst_object_ref (otherpad);
GST_OBJECT_UNLOCK (sel);
gst_object_unref (sel);
return otherpad;
}
static GstCaps *
gst_input_selector_getcaps (GstPad * pad)
{
GstPad *otherpad;
GstObject *parent;
GstCaps *caps;
parent = gst_object_get_parent (GST_OBJECT (pad));
if (GST_INPUT_SELECTOR (parent)->select_all) {
caps = gst_pad_proxy_getcaps (pad);
goto done;
}
otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
if (!otherpad) {
GST_DEBUG_OBJECT (parent,
"Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
caps = gst_caps_new_any ();
} else {
GST_DEBUG_OBJECT (parent,
"Pad %s:%s is linked (to %s:%s), returning peer caps",
GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
/* if the peer has caps, use those. If the pad is not linked, this function
* returns NULL and we return ANY */
if (!(caps = gst_pad_peer_get_caps (otherpad)))
caps = gst_caps_new_any ();
gst_object_unref (otherpad);
}
done:
gst_object_unref (parent);
return caps;
}
/* check if the pad is the active sinkpad */
static gboolean
gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
{
GstSelectorPad *selpad;
gboolean res;
selpad = GST_SELECTOR_PAD_CAST (pad);
GST_OBJECT_LOCK (sel);
res = (pad == sel->active_sinkpad);
GST_OBJECT_UNLOCK (sel);
return res;
}
/* Get or create the active sinkpad */
static GstPad *
gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
{
GstPad *active_sinkpad;
GstSelectorPad *selpad;
selpad = GST_SELECTOR_PAD_CAST (pad);
GST_OBJECT_LOCK (sel);
selpad->active = TRUE;
active_sinkpad = sel->active_sinkpad;
if (active_sinkpad == NULL) {
/* first pad we get an alloc on becomes the activated pad by default */
active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
}
GST_OBJECT_UNLOCK (sel);
return active_sinkpad;
}
static GList *
gst_input_selector_get_linked_pads (GstPad * pad)
{
GstPad *otherpad;
otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
if (!otherpad)
return NULL;
/* need to drop the ref, internal linked pads is not MT safe */
gst_object_unref (otherpad);
return g_list_append (NULL, otherpad);
}
static GstPad *
gst_input_selector_request_new_pad (GstElement * element,
GstPadTemplate * templ, const gchar * unused)
{
GstInputSelector *sel;
gchar *name = NULL;
GstPad *sinkpad = NULL;
sel = GST_INPUT_SELECTOR (element);
g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
GST_OBJECT_LOCK (sel);
name = g_strdup_printf ("sink%d", sel->padcount++);
sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
"name", name, "direction", templ->direction, "template", templ, NULL);
g_free (name);
sel->n_pads++;
GST_OBJECT_UNLOCK (sel);
gst_pad_set_event_function (sinkpad,
GST_DEBUG_FUNCPTR (gst_selector_pad_event));
gst_pad_set_getcaps_function (sinkpad,
GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
gst_pad_set_chain_function (sinkpad,
GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
gst_pad_set_internal_link_function (sinkpad,
GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
gst_pad_set_bufferalloc_function (sinkpad,
GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
gst_pad_set_active (sinkpad, TRUE);
gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
return sinkpad;
}
static void
gst_input_selector_release_pad (GstElement * element, GstPad * pad)
{
GstInputSelector *sel;
sel = GST_INPUT_SELECTOR (element);
GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
GST_OBJECT_LOCK (sel);
/* if the pad was the active pad, makes us select a new one */
if (sel->active_sinkpad == pad) {
GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
sel->active_sinkpad = NULL;
}
sel->n_pads--;
GST_OBJECT_UNLOCK (sel);
gst_pad_set_active (pad, FALSE);
gst_element_remove_pad (GST_ELEMENT (sel), pad);
}
static GstStateChangeReturn
gst_input_selector_change_state (GstElement * element,
GstStateChange transition)
{
GstInputSelector *self = GST_INPUT_SELECTOR (element);
GstStateChangeReturn result;
result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
GST_OBJECT_LOCK (self);
self->blocked = FALSE;
g_cond_broadcast (self->blocked_cond);
GST_OBJECT_UNLOCK (self);
}
return result;
}
static gint64
gst_input_selector_block (GstInputSelector * self)
{
gint64 ret = 0;
GstSelectorPad *spad;
GST_OBJECT_LOCK (self);
if (self->blocked)
GST_WARNING_OBJECT (self, "switch already blocked");
self->blocked = TRUE;
spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
if (self->active_sinkpad)
ret = gst_selector_pad_get_running_time (spad);
else
GST_DEBUG_OBJECT (self, "no active pad while blocking");
GST_OBJECT_UNLOCK (self);
return ret;
}
static void
gst_input_selector_push_pending_stop (GstInputSelector * self)
{
GstEvent *event = NULL;
GST_OBJECT_LOCK (self);
if (G_UNLIKELY (self->pending_stop)) {
GstSegment *seg = &self->pending_stop_segment;
GST_DEBUG_OBJECT (self, "pushing pending stop");
event = gst_event_new_new_segment_full (TRUE, seg->rate,
seg->applied_rate, seg->format, seg->start, seg->stop, seg->stop);
self->pending_stop = FALSE;
}
GST_OBJECT_UNLOCK (self);
if (event)
gst_pad_push_event (self->srcpad, event);
}
/* stop_time and start_time are running times */
static void
gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
gint64 stop_time, gint64 start_time)
{
g_return_if_fail (self->blocked == TRUE);
gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
GST_OBJECT_LOCK (self);
self->blocked = FALSE;
g_cond_broadcast (self->blocked_cond);
GST_OBJECT_UNLOCK (self);
}
static gboolean
gst_input_selector_check_eos (GstElement * selector)
{
GstIterator *it = gst_element_iterate_sink_pads (selector);
GstIteratorResult ires;
gpointer item;
gboolean done = FALSE, is_eos = FALSE;
GstSelectorPad *pad;
while (!done) {
ires = gst_iterator_next (it, &item);
switch (ires) {
case GST_ITERATOR_DONE:
GST_INFO_OBJECT (selector, "all sink pads have eos");
done = TRUE;
is_eos = TRUE;
break;
case GST_ITERATOR_OK:
pad = GST_SELECTOR_PAD_CAST (item);
if (!pad->eos) {
done = TRUE;
}
break;
case GST_ITERATOR_RESYNC:
gst_iterator_resync (it);
break;
default:
done = TRUE;
break;
}
}
gst_iterator_free (it);
return is_eos;
}
|