mirrored from https://gitlab.freedesktop.org/gstreamer/gstreamer.git
/
gstpad.c
6556 lines (5683 loc) · 182 KB
/
gstpad.c
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) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
* 2000 Wim Taymans <wtay@chello.be>
*
* gstpad.c: Pads for linking elements together
*
* 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 St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:gstpad
* @title: GstPad
* @short_description: Object contained by elements that allows links to
* other elements
* @see_also: #GstPadTemplate, #GstElement, #GstEvent, #GstQuery, #GstBuffer
*
* A #GstElement is linked to other elements via "pads", which are extremely
* light-weight generic link points.
*
* Pads have a #GstPadDirection, source pads produce data, sink pads consume
* data.
*
* Pads are typically created from a #GstPadTemplate with
* gst_pad_new_from_template() and are then added to a #GstElement. This usually
* happens when the element is created but it can also happen dynamically based
* on the data that the element is processing or based on the pads that the
* application requests.
*
* Pads without pad templates can be created with gst_pad_new(),
* which takes a direction and a name as an argument. If the name is %NULL,
* then a guaranteed unique name will be assigned to it.
*
* A #GstElement creating a pad will typically use the various
* gst_pad_set_*_function\() calls to register callbacks for events, queries or
* dataflow on the pads.
*
* gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
*
* After two pads are retrieved from an element by gst_element_get_static_pad(),
* the pads can be linked with gst_pad_link(). (For quick links,
* you can also use gst_element_link(), which will make the obvious
* link for you if it's straightforward.). Pads can be unlinked again with
* gst_pad_unlink(). gst_pad_get_peer() can be used to check what the pad is
* linked to.
*
* Before dataflow is possible on the pads, they need to be activated with
* gst_pad_set_active().
*
* gst_pad_query() and gst_pad_peer_query() can be used to query various
* properties of the pad and the stream.
*
* To send a #GstEvent on a pad, use gst_pad_send_event() and
* gst_pad_push_event(). Some events will be sticky on the pad, meaning that
* after they pass on the pad they can be queried later with
* gst_pad_get_sticky_event() and gst_pad_sticky_events_foreach().
* gst_pad_get_current_caps() and gst_pad_has_current_caps() are convenience
* functions to query the current sticky CAPS event on a pad.
*
* GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
* or pull in a buffer.
*
* The dataflow, events and queries that happen on a pad can be monitored with
* probes that can be installed with gst_pad_add_probe(). gst_pad_is_blocked()
* can be used to check if a block probe is installed on the pad.
* gst_pad_is_blocking() checks if the blocking probe is currently blocking the
* pad. gst_pad_remove_probe() is used to remove a previously installed probe
* and unblock blocking probes if any.
*
* Pad have an offset that can be retrieved with gst_pad_get_offset(). This
* offset will be applied to the running_time of all data passing over the pad.
* gst_pad_set_offset() can be used to change the offset.
*
* Convenience functions exist to start, pause and stop the task on a pad with
* gst_pad_start_task(), gst_pad_pause_task() and gst_pad_stop_task()
* respectively.
*/
#include "gst_private.h"
#include "gstpad.h"
#include "gstpadtemplate.h"
#include "gstenumtypes.h"
#include "gstutils.h"
#include "gstinfo.h"
#include "gsterror.h"
#include "gsttracerutils.h"
#include "gstvalue.h"
#include "glib-compat-private.h"
GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
#define GST_CAT_DEFAULT GST_CAT_PADS
/* Pad signals and args */
enum
{
PAD_LINKED,
PAD_UNLINKED,
/* FILL ME */
LAST_SIGNAL
};
enum
{
PAD_PROP_0,
PAD_PROP_CAPS,
PAD_PROP_DIRECTION,
PAD_PROP_TEMPLATE,
PAD_PROP_OFFSET
/* FILL ME */
};
#define _PAD_PROBE_TYPE_ALL_BOTH_AND_FLUSH (GST_PAD_PROBE_TYPE_ALL_BOTH | GST_PAD_PROBE_TYPE_EVENT_FLUSH)
/* we have a pending and an active event on the pad. On source pads only the
* active event is used. On sinkpads, events are copied to the pending entry and
* moved to the active event when the eventfunc returned %TRUE. */
typedef struct
{
gboolean received;
GstEvent *event;
} PadEvent;
struct _GstPadPrivate
{
guint events_cookie;
GArray *events;
guint last_cookie;
gint using;
guint probe_list_cookie;
/* counter of how many idle probes are running directly from the add_probe
* call. Used to block any data flowing in the pad while the idle callback
* Doesn't finish its work */
gint idle_running;
/* conditional and variable used to ensure pads only get (de)activated
* by a single thread at a time. Protected by the object lock */
GCond activation_cond;
gboolean in_activation;
};
typedef struct
{
GHook hook;
} GstProbe;
#define GST_PAD_IS_RUNNING_IDLE_PROBE(p) \
(((GstPad *)(p))->priv->idle_running > 0)
typedef struct
{
GstPad *pad;
GstPadProbeInfo *info;
gboolean dropped;
gboolean pass;
gboolean handled;
gboolean marshalled;
gulong *called_probes;
guint n_called_probes;
guint called_probes_size;
gboolean retry;
} ProbeMarshall;
static void gst_pad_dispose (GObject * object);
static void gst_pad_finalize (GObject * object);
static void gst_pad_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_pad_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
GstObject * parent, GstBufferList * list);
static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
GstEvent * event, GstPadProbeType type);
static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
GstEvent * event, GstPadProbeType type);
static gboolean activate_mode_internal (GstPad * pad, GstObject * parent,
GstPadMode mode, gboolean active);
static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
static GParamSpec *pspec_caps = NULL;
/* quarks for probe signals */
static GQuark buffer_quark;
static GQuark buffer_list_quark;
static GQuark event_quark;
typedef struct
{
const gint ret;
const gchar *name;
GQuark quark;
} GstFlowQuarks;
static GstFlowQuarks flow_quarks[] = {
{GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
{GST_FLOW_OK, "ok", 0},
{GST_FLOW_NOT_LINKED, "not-linked", 0},
{GST_FLOW_FLUSHING, "flushing", 0},
{GST_FLOW_EOS, "eos", 0},
{GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
{GST_FLOW_ERROR, "error", 0},
{GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
{GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
};
/**
* gst_flow_get_name:
* @ret: a #GstFlowReturn to get the name of.
*
* Gets a string representing the given flow return.
*
* Returns: a static string with the name of the flow return.
*/
const gchar *
gst_flow_get_name (GstFlowReturn ret)
{
gint i;
ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
if (ret == flow_quarks[i].ret)
return flow_quarks[i].name;
}
return "unknown";
}
/**
* gst_flow_to_quark:
* @ret: a #GstFlowReturn to get the quark of.
*
* Get the unique quark for the given GstFlowReturn.
*
* Returns: the quark associated with the flow return or 0 if an
* invalid return was specified.
*/
GQuark
gst_flow_to_quark (GstFlowReturn ret)
{
gint i;
ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
if (ret == flow_quarks[i].ret)
return flow_quarks[i].quark;
}
return 0;
}
/**
* gst_pad_link_get_name:
* @ret: a #GstPadLinkReturn to get the name of.
*
* Gets a string representing the given pad-link return.
*
* Returns: a static string with the name of the pad-link return.
*
* Since: 1.4
*/
const gchar *
gst_pad_link_get_name (GstPadLinkReturn ret)
{
switch (ret) {
case GST_PAD_LINK_OK:
return "ok";
case GST_PAD_LINK_WRONG_HIERARCHY:
return "wrong hierarchy";
case GST_PAD_LINK_WAS_LINKED:
return "was linked";
case GST_PAD_LINK_WRONG_DIRECTION:
return "wrong direction";
case GST_PAD_LINK_NOFORMAT:
return "no common format";
case GST_PAD_LINK_NOSCHED:
return "incompatible scheduling";
case GST_PAD_LINK_REFUSED:
return "refused";
}
g_return_val_if_reached ("unknown");
}
#define _do_init \
{ \
gint i; \
\
buffer_quark = g_quark_from_static_string ("buffer"); \
buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
event_quark = g_quark_from_static_string ("event"); \
\
for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
} \
\
GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
}
#define gst_pad_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT,
G_ADD_PRIVATE (GstPad) _do_init);
static void
gst_pad_class_init (GstPadClass * klass)
{
GObjectClass *gobject_class;
GstObjectClass *gstobject_class;
gobject_class = G_OBJECT_CLASS (klass);
gstobject_class = GST_OBJECT_CLASS (klass);
gobject_class->dispose = gst_pad_dispose;
gobject_class->finalize = gst_pad_finalize;
gobject_class->set_property = gst_pad_set_property;
gobject_class->get_property = gst_pad_get_property;
/**
* GstPad::linked:
* @pad: the pad that emitted the signal
* @peer: the peer pad that has been connected
*
* Signals that a pad has been linked to the peer pad.
*/
gst_pad_signals[PAD_LINKED] =
g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
NULL, G_TYPE_NONE, 1, GST_TYPE_PAD);
/**
* GstPad::unlinked:
* @pad: the pad that emitted the signal
* @peer: the peer pad that has been disconnected
*
* Signals that a pad has been unlinked from the peer pad.
*/
gst_pad_signals[PAD_UNLINKED] =
g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
NULL, G_TYPE_NONE, 1, GST_TYPE_PAD);
pspec_caps = g_param_spec_boxed ("caps", "Caps",
"The capabilities of the pad", GST_TYPE_CAPS,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
g_param_spec_enum ("direction", "Direction", "The direction of the pad",
GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
/* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
g_param_spec_object ("template", "Template",
"The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/**
* GstPad:offset:
*
* The offset that will be applied to the running time of the pad.
*
* Since: 1.6
*/
g_object_class_install_property (gobject_class, PAD_PROP_OFFSET,
g_param_spec_int64 ("offset", "Offset",
"The running time offset of the pad", 0, G_MAXINT64, 0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gstobject_class->path_string_separator = ".";
/* Register common function pointer descriptions */
GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
}
static void
gst_pad_init (GstPad * pad)
{
pad->priv = gst_pad_get_instance_private (pad);
GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
GST_PAD_SET_FLUSHING (pad);
g_rec_mutex_init (&pad->stream_rec_lock);
g_cond_init (&pad->block_cond);
g_hook_list_init (&pad->probes, sizeof (GstProbe));
pad->priv->events = g_array_sized_new (FALSE, TRUE, sizeof (PadEvent), 16);
pad->priv->events_cookie = 0;
pad->priv->last_cookie = -1;
g_cond_init (&pad->priv->activation_cond);
pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
}
/* called when setting the pad inactive. It removes all sticky events from
* the pad. must be called with object lock */
static void
remove_events (GstPad * pad)
{
guint i, len;
GArray *events;
gboolean notify = FALSE;
events = pad->priv->events;
len = events->len;
for (i = 0; i < len; i++) {
PadEvent *ev = &g_array_index (events, PadEvent, i);
GstEvent *event = ev->event;
ev->event = NULL;
if (event && GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
notify = TRUE;
gst_event_unref (event);
}
GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
g_array_set_size (events, 0);
pad->priv->events_cookie++;
if (notify) {
GST_OBJECT_UNLOCK (pad);
GST_DEBUG_OBJECT (pad, "notify caps");
g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
GST_OBJECT_LOCK (pad);
}
}
/* should be called with object lock */
static PadEvent *
find_event_by_type (GstPad * pad, GstEventType type, guint idx)
{
guint i, len;
GArray *events;
PadEvent *ev;
events = pad->priv->events;
len = events->len;
for (i = 0; i < len; i++) {
ev = &g_array_index (events, PadEvent, i);
if (ev->event == NULL)
continue;
if (GST_EVENT_TYPE (ev->event) == type) {
if (idx == 0)
goto found;
idx--;
} else if (GST_EVENT_TYPE (ev->event) > type) {
break;
}
}
ev = NULL;
found:
return ev;
}
/* should be called with OBJECT lock */
static PadEvent *
find_event (GstPad * pad, GstEvent * event)
{
guint i, len;
GArray *events;
PadEvent *ev;
events = pad->priv->events;
len = events->len;
for (i = 0; i < len; i++) {
ev = &g_array_index (events, PadEvent, i);
if (event == ev->event)
goto found;
else if (GST_EVENT_TYPE (ev->event) > GST_EVENT_TYPE (event))
break;
}
ev = NULL;
found:
return ev;
}
/* should be called with OBJECT lock */
static void
remove_event_by_type (GstPad * pad, GstEventType type)
{
guint i, len;
GArray *events;
PadEvent *ev;
events = pad->priv->events;
len = events->len;
i = 0;
while (i < len) {
ev = &g_array_index (events, PadEvent, i);
if (ev->event == NULL)
goto next;
if (GST_EVENT_TYPE (ev->event) > type)
break;
else if (GST_EVENT_TYPE (ev->event) != type)
goto next;
gst_event_unref (ev->event);
g_array_remove_index (events, i);
len--;
pad->priv->events_cookie++;
continue;
next:
i++;
}
}
/* check all events on srcpad against those on sinkpad. All events that are not
* on sinkpad are marked as received=%FALSE and the PENDING_EVENTS is set on the
* srcpad so that the events will be sent next time */
/* should be called with srcpad and sinkpad LOCKS */
static void
schedule_events (GstPad * srcpad, GstPad * sinkpad)
{
gint i, len;
GArray *events;
PadEvent *ev;
gboolean pending = FALSE;
events = srcpad->priv->events;
len = events->len;
for (i = 0; i < len; i++) {
ev = &g_array_index (events, PadEvent, i);
if (ev->event == NULL)
continue;
if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
ev->received = FALSE;
pending = TRUE;
}
}
if (pending)
GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
}
typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
gpointer user_data);
/* should be called with pad LOCK */
static void
events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
{
guint i, len;
GArray *events;
gboolean ret;
guint cookie;
events = pad->priv->events;
restart:
cookie = pad->priv->events_cookie;
i = 0;
len = events->len;
while (i < len) {
PadEvent *ev, ev_ret;
ev = &g_array_index (events, PadEvent, i);
if (G_UNLIKELY (ev->event == NULL))
goto next;
/* take additional ref, func might release the lock */
ev_ret.event = gst_event_ref (ev->event);
ev_ret.received = ev->received;
ret = func (pad, &ev_ret, user_data);
/* recheck the cookie, lock might have been released and the list could have
* changed */
if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
if (G_LIKELY (ev_ret.event))
gst_event_unref (ev_ret.event);
goto restart;
}
/* store the received state */
ev->received = ev_ret.received;
/* if the event changed, we need to do something */
if (G_UNLIKELY (ev->event != ev_ret.event)) {
if (G_UNLIKELY (ev_ret.event == NULL)) {
/* function unreffed and set the event to NULL, remove it */
gst_event_unref (ev->event);
g_array_remove_index (events, i);
len--;
cookie = ++pad->priv->events_cookie;
continue;
} else {
/* function gave a new event for us */
gst_event_take (&ev->event, ev_ret.event);
}
} else {
/* just unref, nothing changed */
gst_event_unref (ev_ret.event);
}
if (!ret)
break;
next:
i++;
}
}
/* should be called with LOCK */
static GstEvent *
_apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream,
gint64 pad_offset)
{
gint64 offset;
GST_DEBUG_OBJECT (pad, "apply pad offset %" GST_STIME_FORMAT,
GST_STIME_ARGS (pad_offset));
if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
GstSegment segment;
g_assert (!upstream);
/* copy segment values */
gst_event_copy_segment (event, &segment);
gst_event_unref (event);
gst_segment_offset_running_time (&segment, segment.format, pad_offset);
event = gst_event_new_segment (&segment);
}
event = gst_event_make_writable (event);
offset = gst_event_get_running_time_offset (event);
if (upstream)
offset -= pad_offset;
else
offset += pad_offset;
gst_event_set_running_time_offset (event, offset);
return event;
}
static inline GstEvent *
apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream)
{
if (G_UNLIKELY (pad->offset != 0))
return _apply_pad_offset (pad, event, upstream, pad->offset);
return event;
}
/* should be called with the OBJECT_LOCK */
static GstCaps *
get_pad_caps (GstPad * pad)
{
GstCaps *caps = NULL;
PadEvent *ev;
ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
if (ev && ev->event)
gst_event_parse_caps (ev->event, &caps);
return caps;
}
static void
gst_pad_dispose (GObject * object)
{
GstPad *pad = GST_PAD_CAST (object);
GstPad *peer;
GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "%p dispose", pad);
/* unlink the peer pad */
if ((peer = gst_pad_get_peer (pad))) {
/* window for MT unsafeness, someone else could unlink here
* and then we call unlink with wrong pads. The unlink
* function would catch this and safely return failed. */
if (GST_PAD_IS_SRC (pad))
gst_pad_unlink (pad, peer);
else
gst_pad_unlink (peer, pad);
gst_object_unref (peer);
}
gst_pad_set_pad_template (pad, NULL);
GST_OBJECT_LOCK (pad);
remove_events (pad);
g_hook_list_clear (&pad->probes);
GST_OBJECT_UNLOCK (pad);
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
gst_pad_finalize (GObject * object)
{
GstPad *pad = GST_PAD_CAST (object);
GstTask *task;
/* in case the task is still around, clean it up */
if ((task = GST_PAD_TASK (pad))) {
gst_task_join (task);
GST_PAD_TASK (pad) = NULL;
gst_object_unref (task);
}
if (pad->activatenotify)
pad->activatenotify (pad->activatedata);
if (pad->activatemodenotify)
pad->activatemodenotify (pad->activatemodedata);
if (pad->linknotify)
pad->linknotify (pad->linkdata);
if (pad->unlinknotify)
pad->unlinknotify (pad->unlinkdata);
if (pad->chainnotify)
pad->chainnotify (pad->chaindata);
if (pad->chainlistnotify)
pad->chainlistnotify (pad->chainlistdata);
if (pad->getrangenotify)
pad->getrangenotify (pad->getrangedata);
if (pad->eventnotify)
pad->eventnotify (pad->eventdata);
if (pad->querynotify)
pad->querynotify (pad->querydata);
if (pad->iterintlinknotify)
pad->iterintlinknotify (pad->iterintlinkdata);
g_rec_mutex_clear (&pad->stream_rec_lock);
g_cond_clear (&pad->block_cond);
g_cond_clear (&pad->priv->activation_cond);
g_array_free (pad->priv->events, TRUE);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_pad_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
g_return_if_fail (GST_IS_PAD (object));
switch (prop_id) {
case PAD_PROP_DIRECTION:
GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
break;
case PAD_PROP_TEMPLATE:
gst_pad_set_pad_template (GST_PAD_CAST (object),
(GstPadTemplate *) g_value_get_object (value));
break;
case PAD_PROP_OFFSET:
gst_pad_set_offset (GST_PAD_CAST (object), g_value_get_int64 (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_pad_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
g_return_if_fail (GST_IS_PAD (object));
switch (prop_id) {
case PAD_PROP_CAPS:
GST_OBJECT_LOCK (object);
g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
GST_OBJECT_UNLOCK (object);
break;
case PAD_PROP_DIRECTION:
g_value_set_enum (value, GST_PAD_DIRECTION (object));
break;
case PAD_PROP_TEMPLATE:
g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
break;
case PAD_PROP_OFFSET:
g_value_set_int64 (value, gst_pad_get_offset (GST_PAD_CAST (object)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* gst_pad_new:
* @name: (allow-none): the name of the new pad.
* @direction: the #GstPadDirection of the pad.
*
* Creates a new pad with the given name in the given direction.
* If name is %NULL, a guaranteed unique name (across all pads)
* will be assigned.
* This function makes a copy of the name so you can safely free the name.
*
* Returns: (transfer floating): a new #GstPad.
*
* MT safe.
*/
GstPad *
gst_pad_new (const gchar * name, GstPadDirection direction)
{
return g_object_new (GST_TYPE_PAD,
"name", name, "direction", direction, NULL);
}
/**
* gst_pad_new_from_template:
* @templ: the pad template to use
* @name: (allow-none): the name of the pad
*
* Creates a new pad with the given name from the given template.
* If name is %NULL, a guaranteed unique name (across all pads)
* will be assigned.
* This function makes a copy of the name so you can safely free the name.
*
* Returns: (transfer floating): a new #GstPad.
*/
GstPad *
gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
{
GType pad_type =
GST_PAD_TEMPLATE_GTYPE (templ) ==
G_TYPE_NONE ? GST_TYPE_PAD : GST_PAD_TEMPLATE_GTYPE (templ);
g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
return g_object_new (pad_type,
"name", name, "direction", templ->direction, "template", templ, NULL);
}
/**
* gst_pad_new_from_static_template:
* @templ: the #GstStaticPadTemplate to use
* @name: the name of the pad
*
* Creates a new pad with the given name from the given static template.
* If name is %NULL, a guaranteed unique name (across all pads)
* will be assigned.
* This function makes a copy of the name so you can safely free the name.
*
* Returns: (transfer floating): a new #GstPad.
*/
GstPad *
gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
const gchar * name)
{
GstPad *pad;
GstPadTemplate *template;
template = gst_static_pad_template_get (templ);
pad = gst_pad_new_from_template (template, name);
gst_object_unref (template);
return pad;
}
#define ACQUIRE_PARENT(pad, parent, label) \
G_STMT_START { \
if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
gst_object_ref (parent); \
else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
goto label; \
} G_STMT_END
#define RELEASE_PARENT(parent) \
G_STMT_START { \
if (G_LIKELY (parent)) \
gst_object_unref (parent); \
} G_STMT_END
/**
* gst_pad_get_direction:
* @pad: a #GstPad to get the direction of.
*
* Gets the direction of the pad. The direction of the pad is
* decided at construction time so this function does not take
* the LOCK.
*
* Returns: the #GstPadDirection of the pad.
*
* MT safe.
*/
GstPadDirection
gst_pad_get_direction (GstPad * pad)
{
GstPadDirection result;
/* PAD_UNKNOWN is a little silly but we need some sort of
* error return value */
g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
result = GST_PAD_DIRECTION (pad);
return result;
}
static gboolean
gst_pad_activate_default (GstPad * pad, GstObject * parent)
{
g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
return activate_mode_internal (pad, parent, GST_PAD_MODE_PUSH, TRUE);
}
/**
* gst_pad_mode_get_name:
* @mode: the pad mode
*
* Return the name of a pad mode, for use in debug messages mostly.
*
* Returns: short mnemonic for pad mode @mode
*/
const gchar *
gst_pad_mode_get_name (GstPadMode mode)
{
switch (mode) {
case GST_PAD_MODE_NONE:
return "none";
case GST_PAD_MODE_PUSH:
return "push";
case GST_PAD_MODE_PULL:
return "pull";
default:
break;
}
return "unknown";
}
/* Returns TRUE if pad wasn't already in the new_mode */
static gboolean
pre_activate (GstPad * pad, GstPadMode new_mode)
{
switch (new_mode) {
case GST_PAD_MODE_NONE:
GST_OBJECT_LOCK (pad);
while (G_UNLIKELY (pad->priv->in_activation))
g_cond_wait (&pad->priv->activation_cond, GST_OBJECT_GET_LOCK (pad));
if (new_mode == GST_PAD_MODE (pad)) {
GST_WARNING_OBJECT (pad,
"Pad is already in the process of being deactivated");
GST_OBJECT_UNLOCK (pad);
return FALSE;
}
pad->priv->in_activation = TRUE;
GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
GST_PAD_SET_FLUSHING (pad);
pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
GST_PAD_MODE (pad) = new_mode;
/* unlock blocked pads so element can resume and stop */
GST_PAD_BLOCK_BROADCAST (pad);
GST_OBJECT_UNLOCK (pad);
break;
case GST_PAD_MODE_PUSH:
case GST_PAD_MODE_PULL:
GST_OBJECT_LOCK (pad);
while (G_UNLIKELY (pad->priv->in_activation))
g_cond_wait (&pad->priv->activation_cond, GST_OBJECT_GET_LOCK (pad));
if (new_mode == GST_PAD_MODE (pad)) {
GST_WARNING_OBJECT (pad,
"Pad is already in the process of being activated");
GST_OBJECT_UNLOCK (pad);
return FALSE;