validate: allow config to check for minimum buffer frequency on pads
[platform/upstream/gstreamer.git] / validate / gst / validate / gst-validate-pad-monitor.c
1 /* GStreamer
2  *
3  * Copyright (C) 2013 Collabora Ltd.
4  *  Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>
5  *
6  * gst-validate-pad-monitor.c - Validate PadMonitor class
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "gst-validate-internal.h"
29 #include "gst-validate-pad-monitor.h"
30 #include "gst-validate-element-monitor.h"
31 #include "gst-validate-pipeline-monitor.h"
32 #include "gst-validate-reporter.h"
33 #include "gst-validate-utils.h"
34 #include "validate.h"
35 #include <string.h>
36 #include <stdarg.h>
37
38 /**
39  * SECTION:gst-validate-pad-monitor
40  * @short_description: Class that wraps a #GstPad for Validate checks
41  *
42  * TODO
43  */
44 #define _GET_PAD_MONITOR(p) g_object_get_qdata ((GObject*) p, _Q_VALIDATE_MONITOR)
45 #define _SET_PAD_MONITOR(p,d) g_object_set_qdata ((GObject*) p, _Q_VALIDATE_MONITOR, d)
46
47 static GstValidateInterceptionReturn
48 gst_validate_pad_monitor_intercept_report (GstValidateReporter * reporter,
49     GstValidateReport * report);
50
51 #define _do_init \
52   G_IMPLEMENT_INTERFACE (GST_TYPE_VALIDATE_REPORTER, _reporter_iface_init)
53
54 static void
55 _reporter_iface_init (GstValidateReporterInterface * iface)
56 {
57   iface->intercept_report = gst_validate_pad_monitor_intercept_report;
58 }
59
60 #define gst_validate_pad_monitor_parent_class parent_class
61 G_DEFINE_TYPE_WITH_CODE (GstValidatePadMonitor, gst_validate_pad_monitor,
62     GST_TYPE_VALIDATE_MONITOR, _do_init);
63
64 #define PENDING_FIELDS "pending-fields"
65 #define AUDIO_TIMESTAMP_TOLERANCE (GST_MSECOND * 100)
66
67 #define PAD_PARENT_IS_DEMUXER(m) \
68     (GST_VALIDATE_MONITOR_GET_PARENT(m) ? \
69         GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_DEMUXER ( \
70             GST_VALIDATE_MONITOR_GET_PARENT(m)) : \
71         FALSE)
72
73 #define PAD_PARENT_IS_DECODER(m) \
74     (GST_VALIDATE_MONITOR_GET_PARENT(m) ? \
75         GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_DECODER ( \
76             GST_VALIDATE_MONITOR_GET_PARENT(m)) : \
77         FALSE)
78
79 #define PAD_PARENT_IS_ENCODER(m) \
80     (GST_VALIDATE_MONITOR_GET_PARENT(m) ? \
81         GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_ENCODER ( \
82             GST_VALIDATE_MONITOR_GET_PARENT(m)) : \
83         FALSE)
84
85
86 /*
87  * Locking the parent should always be done before locking the
88  * pad-monitor to prevent deadlocks in case another monitor from
89  * another pad on the same element starts an operation that also
90  * requires locking itself and some other monitors from internally
91  * linked pads.
92  *
93  * An example:
94  * An element has a sink and a src pad. Some test starts running at sinkpad
95  * and it locks the parent, and then it locks itself. In case it needs to get
96  * some information from the srcpad, it is able to lock the srcpad and get it
97  * because the srcpad should never lock itself before locking the parent (which
98  * it won't be able as sinkpad already locked it).
99  *
100  * As a side one, it is possible that srcpad locks itself without locking the
101  * parent in case it wants to do a check that won't need to use other internally
102  * linked pads (sinkpad). But in this case it might lock and unlock freely without
103  * causing deadlocks.
104  */
105 #define GST_VALIDATE_PAD_MONITOR_PARENT_LOCK(m)                  \
106 G_STMT_START {                                             \
107   if (G_LIKELY (GST_VALIDATE_MONITOR_GET_PARENT (m))) {          \
108     GST_VALIDATE_MONITOR_LOCK (GST_VALIDATE_MONITOR_GET_PARENT (m));   \
109   } else {                                                 \
110     GST_WARNING_OBJECT (m, "No parent found, can't lock"); \
111   }                                                        \
112 } G_STMT_END
113
114 #define GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK(m)                  \
115 G_STMT_START {                                               \
116   if (G_LIKELY (GST_VALIDATE_MONITOR_GET_PARENT (m))) {            \
117     GST_VALIDATE_MONITOR_UNLOCK (GST_VALIDATE_MONITOR_GET_PARENT (m));   \
118   } else {                                                   \
119     GST_WARNING_OBJECT (m, "No parent found, can't unlock"); \
120   }                                                          \
121 } G_STMT_END
122
123 typedef struct
124 {
125   GstClockTime timestamp;
126   GstEvent *event;
127 } SerializedEventData;
128
129 static GstPad *
130 _get_actual_pad (GstPad * pad)
131 {
132   GstPad *tmp_pad;
133
134   gst_object_ref (pad);
135
136   /* We don't monitor ghost pads */
137   while (GST_IS_GHOST_PAD (pad)) {
138     tmp_pad = pad;
139     pad = gst_ghost_pad_get_target ((GstGhostPad *) pad);
140     gst_object_unref (tmp_pad);
141   }
142
143   while (GST_IS_PROXY_PAD (pad)) {
144     tmp_pad = pad;
145     pad = gst_pad_get_peer (pad);
146     gst_object_unref (tmp_pad);
147   }
148
149   return pad;
150 }
151
152 static gboolean
153 _find_master_report_on_pad (GstPad * pad, GstValidateReport * report)
154 {
155   GstValidatePadMonitor *pad_monitor;
156   GstValidateReport *prev_report;
157   gboolean result = FALSE;
158   GstPad *tmppad = pad;
159
160   pad = _get_actual_pad (pad);
161   if (pad == NULL) {
162     GST_ERROR_OBJECT (tmppad, "Does not have a target yet");
163
164     return FALSE;
165   }
166
167   pad_monitor = _GET_PAD_MONITOR (pad);
168
169   /* For some reason this pad isn't monitored */
170   if (pad_monitor == NULL)
171     goto done;
172
173   prev_report = gst_validate_reporter_get_report ((GstValidateReporter *)
174       pad_monitor, report->issue->issue_id);
175
176   if (prev_report) {
177     if (prev_report->master_report)
178       result = gst_validate_report_set_master_report (report,
179           prev_report->master_report);
180     else
181       result = gst_validate_report_set_master_report (report, prev_report);
182   }
183
184 done:
185   gst_object_unref (pad);
186
187   return result;
188 }
189
190 static gboolean
191 _find_master_report_for_sink_pad (GstValidatePadMonitor * pad_monitor,
192     GstValidateReport * report)
193 {
194   GstPad *peerpad;
195   gboolean result = FALSE;
196   GstPad *pad =
197       GST_PAD_CAST (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
198           (pad_monitor)));
199
200   peerpad = gst_pad_get_peer (pad);
201   gst_object_unref (pad);
202
203   /* If the peer src pad already has a similar report no need to look
204    * any further */
205   if (peerpad && _find_master_report_on_pad (peerpad, report))
206     result = TRUE;
207
208   if (peerpad)
209     gst_object_unref (peerpad);
210
211   return result;
212 }
213
214 static gboolean
215 _find_master_report_for_src_pad (GstValidatePadMonitor * pad_monitor,
216     GstValidateReport * report)
217 {
218   GstIterator *iter;
219   gboolean done;
220   gboolean result = FALSE;
221   GstPad *target =
222       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
223           (pad_monitor)));
224
225   iter = gst_pad_iterate_internal_links (target);
226   done = FALSE;
227   while (!done) {
228     GValue value = { 0, };
229     switch (gst_iterator_next (iter, &value)) {
230       case GST_ITERATOR_OK:
231       {
232         GstPad *pad = g_value_get_object (&value);
233
234         if (_find_master_report_on_pad (pad, report)) {
235           result = TRUE;
236           done = TRUE;
237         }
238
239         g_value_reset (&value);
240         break;
241       }
242       case GST_ITERATOR_RESYNC:
243         gst_iterator_resync (iter);
244         break;
245       case GST_ITERATOR_ERROR:
246         GST_WARNING_OBJECT (target, "Internal links pad iteration error");
247         done = TRUE;
248         break;
249       case GST_ITERATOR_DONE:
250         done = TRUE;
251         break;
252     }
253   }
254   gst_object_unref (target);
255   gst_iterator_free (iter);
256
257   return result;
258 }
259
260 static GstValidateInterceptionReturn
261 _concatenate_issues (GstValidatePadMonitor * pad_monitor,
262     GstValidateReport * report)
263 {
264   GstPad *pad =
265       GST_PAD_CAST (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
266           (pad_monitor)));
267
268   if (GST_PAD_IS_SINK (pad)
269       && _find_master_report_for_sink_pad (pad_monitor, report)) {
270     gst_object_unref (pad);
271     return GST_VALIDATE_REPORTER_KEEP;
272   } else if (GST_PAD_IS_SRC (pad)
273       && _find_master_report_for_src_pad (pad_monitor, report)) {
274     gst_object_unref (pad);
275     return GST_VALIDATE_REPORTER_KEEP;
276   }
277
278   gst_object_unref (pad);
279   return GST_VALIDATE_REPORTER_REPORT;
280 }
281
282 static GstValidateInterceptionReturn
283 gst_validate_pad_monitor_intercept_report (GstValidateReporter *
284     reporter, GstValidateReport * report)
285 {
286   GstValidateReporterInterface *iface_class, *old_iface_class;
287   GstValidatePadMonitor *pad_monitor = GST_VALIDATE_PAD_MONITOR (reporter);
288   GstValidateReportingDetails monitor_reporting_level;
289   GstValidateInterceptionReturn ret;
290
291   monitor_reporting_level =
292       gst_validate_reporter_get_reporting_level (reporter);
293
294   iface_class =
295       G_TYPE_INSTANCE_GET_INTERFACE (reporter, GST_TYPE_VALIDATE_REPORTER,
296       GstValidateReporterInterface);
297   old_iface_class = g_type_interface_peek_parent (iface_class);
298
299   old_iface_class->intercept_report (reporter, report);
300
301   switch (monitor_reporting_level) {
302     case GST_VALIDATE_SHOW_NONE:
303       ret = GST_VALIDATE_REPORTER_DROP;
304       break;
305     case GST_VALIDATE_SHOW_UNKNOWN:
306       ret = _concatenate_issues (pad_monitor, report);
307       break;
308     default:
309       ret = GST_VALIDATE_REPORTER_REPORT;
310       break;
311   }
312
313   gst_validate_report_set_reporting_level (report, monitor_reporting_level);
314   return ret;
315 }
316
317 static void
318 debug_pending_event (GstPad * pad, GPtrArray * array)
319 {
320   guint i, len;
321
322   len = array->len;
323   for (i = 0; i < len; i++) {
324     SerializedEventData *data = g_ptr_array_index (array, i);
325     GST_DEBUG_OBJECT (pad, "event #%d %" GST_TIME_FORMAT " %s %p",
326         i, GST_TIME_ARGS (data->timestamp),
327         GST_EVENT_TYPE_NAME (data->event), data->event);
328   }
329 }
330
331 static void
332 _serialized_event_data_free (SerializedEventData * serialized_event)
333 {
334   gst_event_unref (serialized_event->event);
335   g_slice_free (SerializedEventData, serialized_event);
336 }
337
338 static gboolean gst_validate_pad_monitor_do_setup (GstValidateMonitor *
339     monitor);
340 static GstElement *gst_validate_pad_monitor_get_element (GstValidateMonitor *
341     monitor);
342 static void
343 gst_validate_pad_monitor_setcaps_pre (GstValidatePadMonitor * pad_monitor,
344     GstCaps * caps);
345 static void gst_validate_pad_monitor_setcaps_post (GstValidatePadMonitor *
346     pad_monitor, GstCaps * caps, gboolean ret);
347
348 #define PAD_IS_IN_PUSH_MODE(p) ((p)->mode == GST_PAD_MODE_PUSH)
349
350 static gboolean
351 _structure_is_raw_video (GstStructure * structure)
352 {
353   return gst_structure_has_name (structure, "video/x-raw");
354 }
355
356 static gboolean
357 _structure_is_raw_audio (GstStructure * structure)
358 {
359   return gst_structure_has_name (structure, "audio/x-raw");
360 }
361
362 static gchar *
363 _get_event_string (GstEvent * event)
364 {
365   const GstStructure *st;
366
367   if ((st = gst_event_get_structure (event)))
368     return gst_structure_to_string (st);
369   else
370     return g_strdup_printf ("%s", GST_EVENT_TYPE_NAME (event));
371 }
372
373 static void
374 _check_field_type (GstValidatePadMonitor * monitor,
375     GstStructure * structure, gboolean mandatory, const gchar * field, ...)
376 {
377   va_list var_args;
378   GType type;
379   gchar *joined_types = NULL;
380   const gchar *rejected_types[5];
381   gint rejected_types_index = 0;
382   gchar *struct_str;
383
384   if (!gst_structure_has_field (structure, field)) {
385     if (mandatory) {
386       gchar *str = gst_structure_to_string (structure);
387
388       GST_VALIDATE_REPORT (monitor, CAPS_IS_MISSING_FIELD,
389           "Field '%s' is missing from structure: %s", field, str);
390       g_free (str);
391     } else {
392       GST_DEBUG_OBJECT (monitor, "Field %s is missing but is not mandatory",
393           field);
394     }
395     return;
396   }
397
398   memset ((gchar **) rejected_types, 0, sizeof (rejected_types));
399   va_start (var_args, field);
400   while ((type = va_arg (var_args, GType)) != 0) {
401     if (gst_structure_has_field_typed (structure, field, type)) {
402       va_end (var_args);
403       return;
404     }
405     rejected_types[rejected_types_index++] = g_type_name (type);
406   }
407   va_end (var_args);
408
409   joined_types = g_strjoinv (" / ", (gchar **) rejected_types);
410   struct_str = gst_structure_to_string (structure);
411   GST_VALIDATE_REPORT (monitor, CAPS_FIELD_HAS_BAD_TYPE,
412       "Field '%s' has wrong type %s in structure '%s'. Expected: %s", field,
413       g_type_name (gst_structure_get_field_type (structure, field)), struct_str,
414       joined_types);
415   g_free (joined_types);
416   g_free (struct_str);
417 }
418
419 static void
420 gst_validate_pad_monitor_check_raw_video_caps_complete (GstValidatePadMonitor *
421     monitor, GstStructure * structure)
422 {
423   _check_field_type (monitor, structure, TRUE, "width", G_TYPE_INT,
424       GST_TYPE_INT_RANGE, 0);
425   _check_field_type (monitor, structure, TRUE, "height", G_TYPE_INT,
426       GST_TYPE_INT_RANGE, 0);
427   _check_field_type (monitor, structure, TRUE, "framerate", GST_TYPE_FRACTION,
428       GST_TYPE_FRACTION_RANGE, 0);
429   _check_field_type (monitor, structure, FALSE, "pixel-aspect-ratio",
430       GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE, 0);
431   _check_field_type (monitor, structure, TRUE, "format", G_TYPE_STRING,
432       GST_TYPE_LIST);
433 }
434
435 static void
436 gst_validate_pad_monitor_check_raw_audio_caps_complete (GstValidatePadMonitor *
437     monitor, GstStructure * structure)
438 {
439   gint channels;
440   _check_field_type (monitor, structure, TRUE, "format", G_TYPE_STRING,
441       GST_TYPE_LIST, 0);
442   _check_field_type (monitor, structure, TRUE, "layout", G_TYPE_STRING,
443       GST_TYPE_LIST, 0);
444   _check_field_type (monitor, structure, TRUE, "rate", G_TYPE_INT,
445       GST_TYPE_LIST, GST_TYPE_INT_RANGE, 0);
446   _check_field_type (monitor, structure, TRUE, "channels", G_TYPE_INT,
447       GST_TYPE_LIST, GST_TYPE_INT_RANGE, 0);
448   if (gst_structure_get_int (structure, "channels", &channels)) {
449     if (channels > 2)
450       _check_field_type (monitor, structure, TRUE, "channel-mask",
451           GST_TYPE_BITMASK, GST_TYPE_LIST, 0);
452   }
453 }
454
455 static void
456 gst_validate_pad_monitor_check_caps_complete (GstValidatePadMonitor * monitor,
457     GstCaps * caps)
458 {
459   GstStructure *structure;
460   gint i;
461
462   GST_DEBUG_OBJECT (monitor, "Checking caps %" GST_PTR_FORMAT, caps);
463
464   for (i = 0; i < gst_caps_get_size (caps); i++) {
465     structure = gst_caps_get_structure (caps, i);
466
467     if (_structure_is_raw_video (structure)) {
468       gst_validate_pad_monitor_check_raw_video_caps_complete (monitor,
469           structure);
470
471     } else if (_structure_is_raw_audio (structure)) {
472       gst_validate_pad_monitor_check_raw_audio_caps_complete (monitor,
473           structure);
474     }
475   }
476 }
477
478 static GstCaps *
479 gst_validate_pad_monitor_get_othercaps (GstValidatePadMonitor * monitor,
480     GstCaps * filter)
481 {
482   GstCaps *caps = gst_caps_new_empty ();
483   GstIterator *iter;
484   gboolean done;
485   GstPad *otherpad;
486   GstCaps *peercaps;
487   GstPad *pad =
488       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
489           (monitor)));
490
491   iter = gst_pad_iterate_internal_links (pad);
492   done = FALSE;
493   while (!done) {
494     GValue value = { 0, };
495     switch (gst_iterator_next (iter, &value)) {
496       case GST_ITERATOR_OK:
497         otherpad = g_value_get_object (&value);
498
499         /* TODO What would be the correct caps operation to merge the caps in
500          * case one sink is internally linked to multiple srcs? */
501         peercaps = gst_pad_peer_query_caps (otherpad, filter);
502         if (peercaps)
503           caps = gst_caps_merge (caps, peercaps);
504
505         g_value_reset (&value);
506         break;
507       case GST_ITERATOR_RESYNC:
508         gst_iterator_resync (iter);
509         gst_caps_unref (caps);
510         caps = gst_caps_new_empty ();
511         break;
512       case GST_ITERATOR_ERROR:
513         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
514         done = TRUE;
515         break;
516       case GST_ITERATOR_DONE:
517         done = TRUE;
518         break;
519     }
520   }
521   GST_DEBUG_OBJECT (pad, "Otherpad caps: %" GST_PTR_FORMAT, caps);
522
523   gst_iterator_free (iter);
524   gst_object_unref (pad);
525
526   return caps;
527 }
528
529 static gboolean
530 _structure_is_video (GstStructure * structure)
531 {
532   const gchar *name = gst_structure_get_name (structure);
533
534   return g_strstr_len (name, 6, "video/")
535       && strcmp (name, "video/quicktime") != 0;
536 }
537
538 static gboolean
539 _structure_is_audio (GstStructure * structure)
540 {
541   const gchar *name = gst_structure_get_name (structure);
542
543   return g_strstr_len (name, 6, "audio/") != NULL;
544 }
545
546 static gboolean
547 gst_validate_pad_monitor_pad_should_proxy_othercaps (GstValidatePadMonitor *
548     monitor)
549 {
550   GstValidateMonitor *parent = GST_VALIDATE_MONITOR_GET_PARENT (monitor);
551
552   if (!parent)
553     return FALSE;
554
555   /* We only know how to handle othercaps checks for codecs so far */
556   return (GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_DECODER (parent) ||
557       GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_ENCODER (parent)) &&
558       !GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_CONVERTER (parent);
559 }
560
561
562 /* Check if the field @f from @s2 (if present) is represented in @s1
563  * Represented here means either equal or @s1's value is in a list/range
564  * from @s2
565  */
566 static gboolean
567 _structures_field_is_contained (GstStructure * s1, GstStructure * s2,
568     gboolean mandatory, const gchar * f)
569 {
570   const GValue *v1;
571   const GValue *v2;
572
573   v2 = gst_structure_get_value (s2, f);
574   if (!v2)
575     return TRUE;                /* nothing to compare to */
576
577   v1 = gst_structure_get_value (s1, f);
578   if (!v1)
579     return !mandatory;
580
581   if (!gst_value_is_fixed (v1))
582     return TRUE;
583
584   if (gst_value_compare (v1, v2) == GST_VALUE_EQUAL)
585     return TRUE;
586
587   if (GST_VALUE_HOLDS_LIST (v2)) {
588     gint i;
589     for (i = 0; i < gst_value_list_get_size (v2); i++) {
590       const GValue *v2_subvalue = gst_value_list_get_value (v2, i);
591       if (gst_value_compare (v1, v2_subvalue) == GST_VALUE_EQUAL)
592         return TRUE;
593     }
594   }
595
596   if (GST_VALUE_HOLDS_ARRAY (v2)) {
597     gint i;
598     for (i = 0; i < gst_value_array_get_size (v2); i++) {
599       const GValue *v2_subvalue = gst_value_array_get_value (v2, i);
600       if (gst_value_compare (v1, v2_subvalue) == GST_VALUE_EQUAL)
601         return TRUE;
602     }
603   }
604
605   if (GST_VALUE_HOLDS_INT_RANGE (v2)) {
606     gint min, max;
607
608     min = gst_value_get_int_range_min (v2);
609     max = gst_value_get_int_range_max (v2);
610
611     if (G_VALUE_HOLDS_INT (v1)) {
612       gint v = g_value_get_int (v1);
613
614       return v >= min && v <= max;
615     } else {
616       /* TODO compare int ranges with int ranges
617        * or with lists if useful */
618     }
619   }
620
621   if (GST_VALUE_HOLDS_FRACTION_RANGE (v2)) {
622     const GValue *min, *max;
623
624     min = gst_value_get_fraction_range_min (v2);
625     max = gst_value_get_fraction_range_max (v2);
626
627     if (GST_VALUE_HOLDS_FRACTION (v1)) {
628       gint v_min = gst_value_compare (v1, min);
629       gint v_max = gst_value_compare (v1, max);
630
631       return (v_min == GST_VALUE_EQUAL || v_min == GST_VALUE_GREATER_THAN) &&
632           (v_max == GST_VALUE_EQUAL || v_max == GST_VALUE_LESS_THAN);
633     } else {
634       /* TODO compare fraction ranges with fraction ranges
635        * or with lists if useful */
636     }
637   }
638
639   return FALSE;
640 }
641
642 static void
643 _check_and_copy_structure_field (GstStructure * from, GstStructure * to,
644     const gchar * name)
645 {
646   if (gst_structure_has_field (from, name)) {
647     gst_structure_set_value (to, name, gst_structure_get_value (from, name));
648   }
649 }
650
651 static GstCaps *
652 gst_validate_pad_monitor_copy_caps_fields_into_caps (GstValidatePadMonitor *
653     monitor, GstCaps * from_caps, GstCaps * into_caps)
654 {
655   gint i, j, into_size, from_size;
656   GstStructure *structure;
657   GstCaps *res = gst_caps_new_empty ();
658
659   into_size = gst_caps_get_size (into_caps);
660   from_size = gst_caps_get_size (from_caps);
661
662   for (i = 0; i < into_size; i++) {
663     GstStructure *s = gst_caps_get_structure (into_caps, i);
664
665     for (j = 0; j < from_size; j++) {
666       GstStructure *new_structure = gst_structure_copy (s);
667
668       structure = gst_caps_get_structure (from_caps, j);
669       if (_structure_is_video (structure)) {
670         _check_and_copy_structure_field (structure, new_structure, "width");
671         _check_and_copy_structure_field (structure, new_structure, "height");
672         _check_and_copy_structure_field (structure, new_structure, "framerate");
673         _check_and_copy_structure_field (structure, new_structure,
674             "pixel-aspect-ratio");
675       } else if (_structure_is_audio (s)) {
676         _check_and_copy_structure_field (structure, new_structure, "rate");
677         _check_and_copy_structure_field (structure, new_structure, "channels");
678       }
679
680       gst_caps_append_structure (res, new_structure);
681     }
682   }
683   return res;
684 }
685
686 static GstCaps *
687 gst_validate_pad_monitor_transform_caps (GstValidatePadMonitor * monitor,
688     GstCaps * caps)
689 {
690   GstCaps *othercaps;
691   GstCaps *new_caps;
692   GstIterator *iter;
693   gboolean done;
694   GstPad *otherpad;
695   GstCaps *template_caps;
696   GstPad *pad;
697
698
699   GST_DEBUG_OBJECT (monitor, "Transform caps %" GST_PTR_FORMAT, caps);
700
701   if (caps == NULL)
702     return NULL;
703
704   othercaps = gst_caps_new_empty ();
705
706   pad =
707       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
708           (monitor)));
709   iter = gst_pad_iterate_internal_links (pad);
710   done = FALSE;
711   while (!done) {
712     GValue value = { 0, };
713     switch (gst_iterator_next (iter, &value)) {
714       case GST_ITERATOR_OK:
715         otherpad = g_value_get_object (&value);
716         template_caps = gst_pad_get_pad_template_caps (otherpad);
717
718         new_caps =
719             gst_validate_pad_monitor_copy_caps_fields_into_caps (monitor, caps,
720             template_caps);
721         if (!gst_caps_is_empty (new_caps))
722           gst_caps_append (othercaps, new_caps);
723         else
724           gst_caps_unref (new_caps);
725
726         gst_caps_unref (template_caps);
727         g_value_reset (&value);
728         break;
729       case GST_ITERATOR_RESYNC:
730         gst_iterator_resync (iter);
731         gst_caps_unref (othercaps);
732         othercaps = gst_caps_new_empty ();
733         break;
734       case GST_ITERATOR_ERROR:
735         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
736         done = TRUE;
737         break;
738       case GST_ITERATOR_DONE:
739         done = TRUE;
740         break;
741     }
742   }
743   gst_iterator_free (iter);
744
745   GST_DEBUG_OBJECT (pad, "Transformed caps: %" GST_PTR_FORMAT, othercaps);
746   gst_object_unref (pad);
747
748   return othercaps;
749 }
750
751 static void
752 gst_validate_pad_monitor_check_caps_fields_proxied (GstValidatePadMonitor *
753     monitor, GstCaps * caps, GstCaps * filter)
754 {
755   GstStructure *structure;
756   GstStructure *otherstructure;
757   GstCaps *othercaps;
758   GstCaps *otherfilter;
759   gint i, j;
760
761   if (!gst_validate_pad_monitor_pad_should_proxy_othercaps (monitor))
762     return;
763
764   otherfilter = gst_validate_pad_monitor_transform_caps (monitor, filter);
765   othercaps = gst_validate_pad_monitor_get_othercaps (monitor, otherfilter);
766   if (otherfilter)
767     gst_caps_unref (otherfilter);
768
769   for (i = 0; i < gst_caps_get_size (othercaps); i++) {
770     gboolean found = FALSE;
771     gboolean type_match = FALSE;
772
773     otherstructure = gst_caps_get_structure (othercaps, i);
774
775     /* look for a proxied version of 'otherstructure' */
776     if (_structure_is_video (otherstructure)) {
777       for (j = 0; j < gst_caps_get_size (caps); j++) {
778         structure = gst_caps_get_structure (caps, j);
779         if (_structure_is_video (structure)) {
780           type_match = TRUE;
781           if (_structures_field_is_contained (structure, otherstructure, TRUE,
782                   "width")
783               && _structures_field_is_contained (structure, otherstructure,
784                   TRUE, "height")
785               && _structures_field_is_contained (structure, otherstructure,
786                   TRUE, "framerate")
787               && _structures_field_is_contained (structure, otherstructure,
788                   FALSE, "pixel-aspect-ratio")) {
789             found = TRUE;
790             break;
791           }
792         }
793       }
794     } else if (_structure_is_audio (otherstructure)) {
795       for (j = 0; j < gst_caps_get_size (caps); j++) {
796         structure = gst_caps_get_structure (caps, j);
797         if (_structure_is_audio (structure)) {
798           type_match = TRUE;
799           if (_structures_field_is_contained (structure, otherstructure, TRUE,
800                   "rate")
801               && _structures_field_is_contained (structure, otherstructure,
802                   TRUE, "channels")) {
803             found = TRUE;
804             break;
805           }
806         }
807       }
808     }
809
810     if (type_match && !found) {
811       gchar *otherstruct_str = gst_structure_to_string (otherstructure),
812           *caps_str = gst_caps_to_string (caps);
813
814       GST_VALIDATE_REPORT (monitor, GET_CAPS_NOT_PROXYING_FIELDS,
815           "Peer pad structure '%s' has no similar version "
816           "on pad's caps '%s'", otherstruct_str, caps_str);
817
818       g_free (otherstruct_str);
819       g_free (caps_str);
820     }
821   }
822
823   gst_caps_unref (othercaps);
824 }
825
826 static void
827 gst_validate_pad_monitor_check_late_serialized_events (GstValidatePadMonitor *
828     monitor, GstClockTime ts)
829 {
830   gint i;
831   GstPad *pad;
832
833   if (!GST_CLOCK_TIME_IS_VALID (ts))
834     return;
835
836   pad =
837       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
838           (monitor)));
839
840   GST_DEBUG_OBJECT (pad, "Timestamp to check %" GST_TIME_FORMAT,
841       GST_TIME_ARGS (ts));
842
843   for (i = 0; i < monitor->serialized_events->len; i++) {
844     SerializedEventData *data =
845         g_ptr_array_index (monitor->serialized_events, i);
846
847     GST_DEBUG_OBJECT (pad, "Event #%d (%s) ts: %" GST_TIME_FORMAT,
848         i, GST_EVENT_TYPE_NAME (data->event), GST_TIME_ARGS (data->timestamp));
849
850     if (GST_CLOCK_TIME_IS_VALID (data->timestamp) && data->timestamp < ts) {
851       gchar *event_str = _get_event_string (data->event);
852
853       GST_VALIDATE_REPORT (monitor, SERIALIZED_EVENT_WASNT_PUSHED_IN_TIME,
854           "Serialized event %s wasn't pushed before expected timestamp %"
855           GST_TIME_FORMAT " on pad %s:%s", event_str,
856           GST_TIME_ARGS (data->timestamp), GST_DEBUG_PAD_NAME (pad));
857
858       g_free (event_str);
859     } else {
860       /* events should be ordered by ts */
861       break;
862     }
863   }
864
865   if (i) {
866     debug_pending_event (pad, monitor->serialized_events);
867     g_ptr_array_remove_range (monitor->serialized_events, 0, i);
868   }
869
870   gst_object_unref (pad);
871 }
872
873 static void
874 gst_validate_pad_monitor_dispose (GObject * object)
875 {
876   GstValidatePadMonitor *monitor = GST_VALIDATE_PAD_MONITOR_CAST (object);
877   GstPad *pad =
878       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
879           (monitor)));
880
881   if (pad) {
882     if (monitor->pad_probe_id)
883       gst_pad_remove_probe (pad, monitor->pad_probe_id);
884     gst_object_unref (pad);
885   }
886
887   if (monitor->expected_segment)
888     gst_event_unref (monitor->expected_segment);
889
890   gst_structure_free (monitor->pending_setcaps_fields);
891   g_ptr_array_unref (monitor->serialized_events);
892   g_list_free_full (monitor->expired_events, (GDestroyNotify) gst_event_unref);
893   g_list_free_full (monitor->all_bufs, (GDestroyNotify) gst_buffer_unref);
894   gst_caps_replace (&monitor->last_caps, NULL);
895   gst_caps_replace (&monitor->last_query_res, NULL);
896   gst_caps_replace (&monitor->last_query_filter, NULL);
897
898   G_OBJECT_CLASS (parent_class)->dispose (object);
899 }
900
901 static void
902 gst_validate_pad_monitor_class_init (GstValidatePadMonitorClass * klass)
903 {
904   GObjectClass *gobject_class;
905   GstValidateMonitorClass *monitor_klass;
906
907   gobject_class = G_OBJECT_CLASS (klass);
908   monitor_klass = GST_VALIDATE_MONITOR_CLASS (klass);
909
910   gobject_class->dispose = gst_validate_pad_monitor_dispose;
911
912   monitor_klass->setup = gst_validate_pad_monitor_do_setup;
913   monitor_klass->get_element = gst_validate_pad_monitor_get_element;
914 }
915
916 /* Called when a pad is being flushed */
917 static void
918 gst_validate_pad_monitor_flush (GstValidatePadMonitor * pad_monitor)
919 {
920   /* Note: Keep in the same order as in the GstValidatePadMonitor structure */
921
922   gst_caps_replace (&pad_monitor->last_caps, NULL);
923   pad_monitor->caps_is_audio = pad_monitor->caps_is_video =
924       pad_monitor->caps_is_raw = FALSE;
925
926   pad_monitor->first_buffer = TRUE;
927
928   pad_monitor->has_segment = FALSE;
929   pad_monitor->is_eos = FALSE;
930
931   pad_monitor->pending_buffer_discont = TRUE;
932
933   gst_event_replace (&pad_monitor->expected_segment, NULL);
934   if (pad_monitor->serialized_events->len)
935     g_ptr_array_remove_range (pad_monitor->serialized_events, 0,
936         pad_monitor->serialized_events->len);
937   g_list_free_full (pad_monitor->expired_events,
938       (GDestroyNotify) gst_event_unref);
939   pad_monitor->expired_events = NULL;
940
941   gst_segment_init (&pad_monitor->segment, GST_FORMAT_BYTES);
942   pad_monitor->current_timestamp = GST_CLOCK_TIME_NONE;
943   pad_monitor->current_duration = GST_CLOCK_TIME_NONE;
944
945   pad_monitor->last_flow_return = GST_FLOW_OK;
946
947   pad_monitor->timestamp_range_start = GST_CLOCK_TIME_NONE;
948   pad_monitor->timestamp_range_end = GST_CLOCK_TIME_NONE;
949 }
950
951 /* Called when the pad monitor is initialized or when
952  * the pad is deactivated */
953 static void
954 gst_validate_pad_monitor_reset (GstValidatePadMonitor * pad_monitor)
955 {
956   gst_validate_pad_monitor_flush (pad_monitor);
957
958   /* Note : For the entries that haven't been resetted in _flush(), do
959    * it here and keep in the same order as the GstValidatePadMonitor
960    * structure */
961
962   pad_monitor->pending_flush_stop = FALSE;
963   pad_monitor->pending_newsegment_seqnum = GST_SEQNUM_INVALID;
964   pad_monitor->pending_eos_seqnum = GST_SEQNUM_INVALID;
965
966   pad_monitor->pending_seek_accurate_time = GST_CLOCK_TIME_NONE;
967
968   if (pad_monitor->pending_setcaps_fields)
969     gst_structure_free (pad_monitor->pending_setcaps_fields);
970   pad_monitor->pending_setcaps_fields =
971       gst_structure_new_empty (PENDING_FIELDS);
972
973   /* FIXME : Why BYTES and not UNDEFINED ? */
974   gst_segment_init (&pad_monitor->segment, GST_FORMAT_BYTES);
975
976   pad_monitor->min_buf_freq = 0;
977   pad_monitor->buffers_pushed = 0;
978   pad_monitor->last_buffers_pushed = 0;
979   pad_monitor->min_buf_freq_interval_ts = GST_CLOCK_TIME_NONE;
980   pad_monitor->min_buf_freq_first_buffer_ts = GST_CLOCK_TIME_NONE;
981   pad_monitor->min_buf_freq_start = GST_CLOCK_TIME_NONE;
982 }
983
984 static void
985 gst_validate_pad_monitor_init (GstValidatePadMonitor * pad_monitor)
986 {
987   pad_monitor->serialized_events =
988       g_ptr_array_new_with_free_func ((GDestroyNotify)
989       _serialized_event_data_free);
990
991   gst_validate_pad_monitor_reset (pad_monitor);
992 }
993
994 /**
995  * gst_validate_pad_monitor_new:
996  * @pad: (transfer none): a #GstPad to run Validate on
997  */
998 GstValidatePadMonitor *
999 gst_validate_pad_monitor_new (GstPad * pad, GstValidateRunner * runner,
1000     GstValidateElementMonitor * parent)
1001 {
1002   GstValidatePadMonitor *monitor = g_object_new (GST_TYPE_VALIDATE_PAD_MONITOR,
1003       "object", pad, "validate-runner", runner, "validate-parent",
1004       parent, NULL);
1005   GstObject *target =
1006       gst_validate_monitor_get_target (GST_VALIDATE_MONITOR (monitor));
1007
1008   if (target == NULL) {
1009     g_object_unref (monitor);
1010     return NULL;
1011   }
1012
1013   gst_object_unref (target);
1014   return monitor;
1015 }
1016
1017 static GstElement *
1018 gst_validate_pad_monitor_get_element (GstValidateMonitor * monitor)
1019 {
1020   GstPad *pad =
1021       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1022           (monitor)));
1023   GstElement *parent = GST_ELEMENT (gst_pad_get_parent (pad));
1024
1025   gst_object_unref (pad);
1026
1027   return parent;
1028 }
1029
1030 static void
1031 gst_validate_pad_monitor_event_overrides (GstValidatePadMonitor * pad_monitor,
1032     GstEvent * event)
1033 {
1034   GList *iter;
1035
1036   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1037   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1038       iter = g_list_next (iter)) {
1039     GstValidateOverride *override = iter->data;
1040
1041     gst_validate_override_event_handler (override,
1042         GST_VALIDATE_MONITOR_CAST (pad_monitor), event);
1043   }
1044   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1045 }
1046
1047 static void
1048 gst_validate_pad_monitor_buffer_overrides (GstValidatePadMonitor * pad_monitor,
1049     GstBuffer * buffer)
1050 {
1051   GList *iter;
1052
1053   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1054   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1055       iter = g_list_next (iter)) {
1056     GstValidateOverride *override = iter->data;
1057
1058     gst_validate_override_buffer_handler (override,
1059         GST_VALIDATE_MONITOR_CAST (pad_monitor), buffer);
1060   }
1061   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1062 }
1063
1064 static void
1065 gst_validate_pad_monitor_buffer_probe_overrides (GstValidatePadMonitor *
1066     pad_monitor, GstBuffer * buffer)
1067 {
1068   GList *iter;
1069
1070   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1071   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1072       iter = g_list_next (iter)) {
1073     GstValidateOverride *override = iter->data;
1074
1075     gst_validate_override_buffer_probe_handler (override,
1076         GST_VALIDATE_MONITOR_CAST (pad_monitor), buffer);
1077   }
1078   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1079 }
1080
1081 static void
1082 gst_validate_pad_monitor_query_overrides (GstValidatePadMonitor * pad_monitor,
1083     GstQuery * query)
1084 {
1085   GList *iter;
1086
1087   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1088   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1089       iter = g_list_next (iter)) {
1090     GstValidateOverride *override = iter->data;
1091
1092     gst_validate_override_query_handler (override,
1093         GST_VALIDATE_MONITOR_CAST (pad_monitor), query);
1094   }
1095   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1096 }
1097
1098 static void
1099 gst_validate_pad_monitor_setcaps_overrides (GstValidatePadMonitor * pad_monitor,
1100     GstCaps * caps)
1101 {
1102   GList *iter;
1103
1104   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1105   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1106       iter = g_list_next (iter)) {
1107     GstValidateOverride *override = iter->data;
1108
1109     gst_validate_override_setcaps_handler (override,
1110         GST_VALIDATE_MONITOR_CAST (pad_monitor), caps);
1111   }
1112   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1113 }
1114
1115 /* FIXME : This is a bit dubious, what's the point of this check ? */
1116 static gboolean
1117 gst_validate_pad_monitor_timestamp_is_in_received_range (GstValidatePadMonitor *
1118     monitor, GstClockTime ts, GstClockTime tolerance)
1119 {
1120   GstPad *pad =
1121       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1122           (monitor)));
1123
1124   GST_DEBUG_OBJECT (pad,
1125       "Checking if timestamp %" GST_TIME_FORMAT " is in range: %"
1126       GST_TIME_FORMAT " - %" GST_TIME_FORMAT " for pad "
1127       "%s:%s with tolerance: %" GST_TIME_FORMAT, GST_TIME_ARGS (ts),
1128       GST_TIME_ARGS (monitor->timestamp_range_start),
1129       GST_TIME_ARGS (monitor->timestamp_range_end), GST_DEBUG_PAD_NAME (pad),
1130       GST_TIME_ARGS (tolerance));
1131   gst_object_unref (pad);
1132
1133   return !GST_CLOCK_TIME_IS_VALID (monitor->timestamp_range_start) ||
1134       !GST_CLOCK_TIME_IS_VALID (monitor->timestamp_range_end) ||
1135       ((monitor->timestamp_range_start >= tolerance ?
1136           monitor->timestamp_range_start - tolerance : 0) <= ts
1137       && (ts >= tolerance ? ts - tolerance : 0) <=
1138       monitor->timestamp_range_end);
1139 }
1140
1141 /* Iterates over internal links (sinkpads) to check that this buffer has
1142  * a timestamp that is in the range of the lastly received buffers */
1143 static void
1144     gst_validate_pad_monitor_check_buffer_timestamp_in_received_range
1145     (GstValidatePadMonitor * monitor, GstBuffer * buffer,
1146     GstClockTime tolerance)
1147 {
1148   GstClockTime ts;
1149   GstClockTime ts_end;
1150   GstIterator *iter;
1151   gboolean has_one = FALSE;
1152   gboolean found = FALSE;
1153   gboolean done;
1154   GstPad *otherpad;
1155   GstValidatePadMonitor *othermonitor;
1156   GstPad *pad =
1157       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1158           (monitor)));
1159
1160   if (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))
1161       || !GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) {
1162     GST_DEBUG_OBJECT (pad,
1163         "Can't check buffer timestamps range as "
1164         "buffer has no valid timestamp/duration");
1165     goto done;
1166   }
1167
1168   ts = GST_BUFFER_TIMESTAMP (buffer);
1169   ts_end = ts + GST_BUFFER_DURATION (buffer);
1170
1171   iter = gst_pad_iterate_internal_links (pad);
1172
1173   if (iter == NULL) {
1174     GST_WARNING_OBJECT (pad, "No iterator available");
1175     goto done;
1176   }
1177
1178   done = FALSE;
1179   while (!done) {
1180     GValue value = { 0, };
1181     switch (gst_iterator_next (iter, &value)) {
1182       case GST_ITERATOR_OK:
1183         otherpad = g_value_get_object (&value);
1184         GST_DEBUG_OBJECT (pad, "Checking pad %s:%s input timestamps",
1185             GST_DEBUG_PAD_NAME (otherpad));
1186         othermonitor = _GET_PAD_MONITOR (otherpad);
1187         GST_VALIDATE_MONITOR_LOCK (othermonitor);
1188         if (gst_validate_pad_monitor_timestamp_is_in_received_range
1189             (othermonitor, ts, tolerance)
1190             &&
1191             gst_validate_pad_monitor_timestamp_is_in_received_range
1192             (othermonitor, ts_end, tolerance)) {
1193           done = TRUE;
1194           found = TRUE;
1195         }
1196         GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1197         g_value_reset (&value);
1198         has_one = TRUE;
1199         break;
1200       case GST_ITERATOR_RESYNC:
1201         gst_iterator_resync (iter);
1202         has_one = FALSE;
1203         found = FALSE;
1204         break;
1205       case GST_ITERATOR_ERROR:
1206         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1207         done = TRUE;
1208         break;
1209       case GST_ITERATOR_DONE:
1210         done = TRUE;
1211         break;
1212     }
1213   }
1214   gst_iterator_free (iter);
1215
1216   if (!has_one) {
1217     GST_DEBUG_OBJECT (pad, "Skipping timestamp in range check as no "
1218         "internal linked pad was found");
1219     goto done;
1220   }
1221   if (!found) {
1222     GST_VALIDATE_REPORT (monitor, BUFFER_TIMESTAMP_OUT_OF_RECEIVED_RANGE,
1223         "Timestamp %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT
1224         " is out of range of received input", GST_TIME_ARGS (ts),
1225         GST_TIME_ARGS (ts_end));
1226   }
1227 done:
1228   if (pad)
1229     gst_object_unref (pad);
1230 }
1231
1232 static void
1233 gst_validate_pad_monitor_check_discont (GstValidatePadMonitor * pad_monitor,
1234     GstBuffer * buffer)
1235 {
1236   if (pad_monitor->pending_buffer_discont) {
1237     if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
1238       GST_VALIDATE_REPORT (pad_monitor, BUFFER_MISSING_DISCONT,
1239           "Buffer is missing a DISCONT flag");
1240     pad_monitor->pending_buffer_discont = FALSE;
1241   }
1242 }
1243
1244 static void
1245 gst_validate_pad_monitor_check_first_buffer (GstValidatePadMonitor *
1246     pad_monitor, GstBuffer * buffer)
1247 {
1248   GstPad *pad =
1249       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1250           (pad_monitor)));
1251
1252   if (G_UNLIKELY (pad_monitor->first_buffer)) {
1253     pad_monitor->first_buffer = FALSE;
1254
1255     if (!pad_monitor->has_segment && PAD_IS_IN_PUSH_MODE (pad)) {
1256       GST_VALIDATE_REPORT (pad_monitor, BUFFER_BEFORE_SEGMENT,
1257           "Received buffer before Segment event");
1258     }
1259
1260     GST_DEBUG_OBJECT (pad,
1261         "Checking first buffer (pts:%" GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT
1262         ")", GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1263         GST_TIME_ARGS (GST_BUFFER_DTS (buffer)));
1264
1265   }
1266
1267   gst_object_unref (pad);
1268 }
1269
1270 static void
1271 gst_validate_pad_monitor_check_eos (GstValidatePadMonitor *
1272     pad_monitor, GstBuffer * buffer)
1273 {
1274   if (G_UNLIKELY (pad_monitor->is_eos)) {
1275     GST_VALIDATE_REPORT (pad_monitor, BUFFER_AFTER_EOS,
1276         "Received buffer %" GST_PTR_FORMAT " after EOS", buffer);
1277   }
1278 }
1279
1280 static void
1281 gst_validate_pad_monitor_update_buffer_data (GstValidatePadMonitor *
1282     pad_monitor, GstBuffer * buffer)
1283 {
1284   GstPad *pad =
1285       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1286           (pad_monitor)));
1287   pad_monitor->current_timestamp = GST_BUFFER_TIMESTAMP (buffer);
1288   pad_monitor->current_duration = GST_BUFFER_DURATION (buffer);
1289   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) {
1290     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->timestamp_range_start)) {
1291       pad_monitor->timestamp_range_start =
1292           MIN (pad_monitor->timestamp_range_start,
1293           GST_BUFFER_TIMESTAMP (buffer));
1294     } else {
1295       pad_monitor->timestamp_range_start = GST_BUFFER_TIMESTAMP (buffer);
1296     }
1297
1298     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) {
1299       GstClockTime endts =
1300           GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1301       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->timestamp_range_end)) {
1302         pad_monitor->timestamp_range_end =
1303             MAX (pad_monitor->timestamp_range_end, endts);
1304       } else {
1305         pad_monitor->timestamp_range_end = endts;
1306       }
1307     }
1308   }
1309   GST_DEBUG_OBJECT (pad, "Current stored range: %" GST_TIME_FORMAT
1310       " - %" GST_TIME_FORMAT,
1311       GST_TIME_ARGS (pad_monitor->timestamp_range_start),
1312       GST_TIME_ARGS (pad_monitor->timestamp_range_end));
1313
1314   gst_object_unref (pad);
1315 }
1316
1317 static GstFlowReturn
1318 _combine_flows (GstFlowReturn ret1, GstFlowReturn ret2)
1319 {
1320   if (ret1 == ret2)
1321     return ret1;
1322   if (ret1 <= GST_FLOW_NOT_NEGOTIATED)
1323     return ret1;
1324   if (ret2 <= GST_FLOW_NOT_NEGOTIATED)
1325     return ret2;
1326   if (ret1 == GST_FLOW_FLUSHING || ret2 == GST_FLOW_FLUSHING)
1327     return GST_FLOW_FLUSHING;
1328   if (ret1 == GST_FLOW_OK || ret2 == GST_FLOW_OK)
1329     return GST_FLOW_OK;
1330   return ret2;
1331 }
1332
1333 static void
1334 gst_validate_pad_monitor_check_aggregated_return (GstValidatePadMonitor *
1335     monitor, GstObject * parent, GstFlowReturn ret)
1336 {
1337   GstIterator *iter;
1338   gboolean done;
1339   GstPad *otherpad;
1340   GstPad *peerpad;
1341   GstValidatePadMonitor *othermonitor;
1342   GstFlowReturn aggregated = GST_FLOW_NOT_LINKED;
1343   gboolean found_a_pad = FALSE;
1344   GstPad *pad =
1345       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1346           (monitor)));
1347
1348   iter = gst_pad_iterate_internal_links (pad);
1349   done = FALSE;
1350   while (!done) {
1351     GValue value = { 0, };
1352     switch (gst_iterator_next (iter, &value)) {
1353       case GST_ITERATOR_OK:
1354         otherpad = g_value_get_object (&value);
1355         peerpad = gst_pad_get_peer (otherpad);
1356         if (peerpad) {
1357           othermonitor = _GET_PAD_MONITOR (peerpad);
1358           if (othermonitor) {
1359             found_a_pad = TRUE;
1360             GST_VALIDATE_MONITOR_LOCK (othermonitor);
1361             aggregated =
1362                 _combine_flows (aggregated, othermonitor->last_flow_return);
1363             GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1364           }
1365
1366           gst_object_unref (peerpad);
1367         }
1368         g_value_reset (&value);
1369         break;
1370       case GST_ITERATOR_RESYNC:
1371         gst_iterator_resync (iter);
1372         break;
1373       case GST_ITERATOR_ERROR:
1374         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1375         done = TRUE;
1376         break;
1377       case GST_ITERATOR_DONE:
1378         done = TRUE;
1379         break;
1380     }
1381   }
1382   gst_iterator_free (iter);
1383   if (!found_a_pad) {
1384     /* no peer pad found, nothing to do */
1385     goto done;
1386   }
1387   if (aggregated == GST_FLOW_OK || aggregated == GST_FLOW_EOS) {
1388     GstState state, pending;
1389
1390     /* those are acceptable situations */
1391     if (GST_PAD_IS_FLUSHING (pad) && ret == GST_FLOW_FLUSHING) {
1392       /* pad is flushing, always acceptable to return flushing */
1393       goto done;
1394     }
1395
1396     gst_element_get_state (GST_ELEMENT (parent), &state, &pending, 0);
1397     if (ret == GST_FLOW_FLUSHING && (state < GST_STATE_PAUSED
1398             || pending < GST_STATE_PAUSED)) {
1399       /* Element is being teared down, accept FLOW_FLUSHING */
1400
1401       goto done;
1402     }
1403
1404     if (monitor->is_eos && ret == GST_FLOW_EOS) {
1405       /* this element received eos and returned eos */
1406       goto done;
1407     }
1408
1409     if (PAD_PARENT_IS_DEMUXER (monitor) && ret == GST_FLOW_EOS) {
1410       /* a demuxer can return EOS when the samples end */
1411       goto done;
1412     }
1413   }
1414
1415   if (aggregated != ret) {
1416     GST_VALIDATE_REPORT (monitor, WRONG_FLOW_RETURN,
1417         "Wrong combined flow return %s(%d). Expected: %s(%d)",
1418         gst_flow_get_name (ret), ret, gst_flow_get_name (aggregated),
1419         aggregated);
1420   }
1421
1422 done:
1423   gst_object_unref (pad);
1424 }
1425
1426 static void
1427     gst_validate_pad_monitor_otherpad_add_pending_serialized_event
1428     (GstValidatePadMonitor * monitor, GstEvent * event, GstClockTime last_ts)
1429 {
1430   GstIterator *iter;
1431   gboolean done;
1432   GstPad *otherpad;
1433   GstValidatePadMonitor *othermonitor;
1434   GstPad *pad;
1435
1436
1437   if (!GST_EVENT_IS_SERIALIZED (event))
1438     return;
1439
1440   pad =
1441       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1442           (monitor)));
1443   iter = gst_pad_iterate_internal_links (pad);
1444   if (iter == NULL) {
1445     /* inputselector will return NULL if the sinkpad is not the active one .... */
1446     GST_FIXME_OBJECT (pad, "No iterator");
1447     gst_object_unref (pad);
1448     return;
1449   }
1450
1451   done = FALSE;
1452   while (!done) {
1453     GValue value = { 0, };
1454     switch (gst_iterator_next (iter, &value)) {
1455       case GST_ITERATOR_OK:
1456         otherpad = g_value_get_object (&value);
1457         othermonitor = _GET_PAD_MONITOR (otherpad);
1458         if (othermonitor) {
1459           SerializedEventData *data = g_slice_new0 (SerializedEventData);
1460           data->timestamp = last_ts;
1461           data->event = gst_event_ref (event);
1462           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1463           GST_DEBUG_OBJECT (pad, "Storing for pad %s:%s event %p %s",
1464               GST_DEBUG_PAD_NAME (otherpad), event,
1465               GST_EVENT_TYPE_NAME (event));
1466           g_ptr_array_add (othermonitor->serialized_events, data);
1467           debug_pending_event (otherpad, othermonitor->serialized_events);
1468           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1469         }
1470         g_value_reset (&value);
1471         break;
1472       case GST_ITERATOR_RESYNC:
1473         gst_iterator_resync (iter);
1474         break;
1475       case GST_ITERATOR_ERROR:
1476         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1477         done = TRUE;
1478         break;
1479       case GST_ITERATOR_DONE:
1480         done = TRUE;
1481         break;
1482     }
1483   }
1484   gst_iterator_free (iter);
1485   gst_object_unref (pad);
1486 }
1487
1488 static void
1489 gst_validate_pad_monitor_otherpad_add_pending_field (GstValidatePadMonitor *
1490     monitor, GstStructure * structure, const gchar * field)
1491 {
1492   GstIterator *iter;
1493   gboolean done;
1494   GstPad *otherpad;
1495   GstValidatePadMonitor *othermonitor;
1496   const GValue *v;
1497   GstPad *pad;
1498
1499   v = gst_structure_get_value (structure, field);
1500   pad =
1501       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1502           (monitor)));
1503
1504   if (v == NULL) {
1505     GST_DEBUG_OBJECT (pad, "Not adding pending field %s as it isn't "
1506         "present on structure %" GST_PTR_FORMAT, field, structure);
1507     gst_object_unref (pad);
1508     return;
1509   }
1510
1511   iter = gst_pad_iterate_internal_links (pad);
1512   done = FALSE;
1513   while (!done) {
1514     GValue value = { 0, };
1515     switch (gst_iterator_next (iter, &value)) {
1516       case GST_ITERATOR_OK:
1517         otherpad = g_value_get_object (&value);
1518         othermonitor = _GET_PAD_MONITOR (otherpad);
1519         if (othermonitor) {
1520           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1521           g_assert (othermonitor->pending_setcaps_fields != NULL);
1522           gst_structure_set_value (othermonitor->pending_setcaps_fields,
1523               field, v);
1524           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1525         }
1526         g_value_reset (&value);
1527         break;
1528       case GST_ITERATOR_RESYNC:
1529         gst_iterator_resync (iter);
1530         break;
1531       case GST_ITERATOR_ERROR:
1532         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1533         done = TRUE;
1534         break;
1535       case GST_ITERATOR_DONE:
1536         done = TRUE;
1537         break;
1538     }
1539   }
1540   gst_iterator_free (iter);
1541   gst_object_unref (pad);
1542 }
1543
1544 static void
1545 gst_validate_pad_monitor_otherpad_clear_pending_fields (GstValidatePadMonitor *
1546     monitor)
1547 {
1548   GstIterator *iter;
1549   gboolean done;
1550   GstPad *otherpad;
1551   GstValidatePadMonitor *othermonitor;
1552
1553   GstPad *pad =
1554       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1555           (monitor)));
1556
1557   iter = gst_pad_iterate_internal_links (pad);
1558   if (iter == NULL) {
1559     gst_object_unref (pad);
1560     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1561
1562     return;
1563   }
1564
1565   done = FALSE;
1566   while (!done) {
1567     GValue value = { 0, };
1568     switch (gst_iterator_next (iter, &value)) {
1569       case GST_ITERATOR_OK:
1570         otherpad = g_value_get_object (&value);
1571         othermonitor = _GET_PAD_MONITOR (otherpad);
1572         if (othermonitor) {
1573           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1574           g_assert (othermonitor->pending_setcaps_fields != NULL);
1575           gst_structure_free (othermonitor->pending_setcaps_fields);
1576           othermonitor->pending_setcaps_fields =
1577               gst_structure_new_empty (PENDING_FIELDS);
1578           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1579         }
1580         g_value_reset (&value);
1581         break;
1582       case GST_ITERATOR_RESYNC:
1583         gst_iterator_resync (iter);
1584         break;
1585       case GST_ITERATOR_ERROR:
1586         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1587         done = TRUE;
1588         break;
1589       case GST_ITERATOR_DONE:
1590         done = TRUE;
1591         break;
1592     }
1593   }
1594   gst_object_unref (pad);
1595   gst_iterator_free (iter);
1596 }
1597
1598 static void
1599 gst_validate_pad_monitor_add_expected_newsegment (GstValidatePadMonitor *
1600     monitor, GstEvent * event)
1601 {
1602   GstIterator *iter;
1603   gboolean done;
1604   GstPad *otherpad;
1605   GstValidatePadMonitor *othermonitor;
1606   GstPad *pad =
1607       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1608           (monitor)));
1609
1610   iter = gst_pad_iterate_internal_links (pad);
1611   if (iter == NULL) {
1612     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1613     gst_object_unref (pad);
1614     return;
1615   }
1616
1617   done = FALSE;
1618   while (!done) {
1619     GValue value = { 0, };
1620     switch (gst_iterator_next (iter, &value)) {
1621       case GST_ITERATOR_OK:
1622         otherpad = g_value_get_object (&value);
1623         if (!otherpad)
1624           continue;
1625         othermonitor = _GET_PAD_MONITOR (otherpad);
1626         GST_VALIDATE_MONITOR_LOCK (othermonitor);
1627         gst_event_replace (&othermonitor->expected_segment, event);
1628         GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1629         g_value_reset (&value);
1630         break;
1631       case GST_ITERATOR_RESYNC:
1632         gst_iterator_resync (iter);
1633         break;
1634       case GST_ITERATOR_ERROR:
1635         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1636         done = TRUE;
1637         break;
1638       case GST_ITERATOR_DONE:
1639         done = TRUE;
1640         break;
1641     }
1642   }
1643   gst_iterator_free (iter);
1644   gst_object_unref (pad);
1645 }
1646
1647 /* common checks for both sink and src event functions */
1648 static void
1649 gst_validate_pad_monitor_common_event_check (GstValidatePadMonitor *
1650     pad_monitor, GstEvent * event)
1651 {
1652   guint32 seqnum = gst_event_get_seqnum (event);
1653
1654   if (seqnum == GST_SEQNUM_INVALID)
1655     GST_VALIDATE_REPORT (pad_monitor, EVENT_INVALID_SEQNUM,
1656         "Event %p (%s) has an invalid SEQNUM", event,
1657         GST_EVENT_TYPE_NAME (event));
1658
1659   switch (GST_EVENT_TYPE (event)) {
1660     case GST_EVENT_FLUSH_START:
1661     {
1662       if (pad_monitor->pending_flush_start_seqnum != GST_SEQNUM_INVALID) {
1663         if (seqnum == pad_monitor->pending_flush_start_seqnum) {
1664           pad_monitor->pending_flush_start_seqnum = GST_SEQNUM_INVALID;
1665         } else {
1666           GST_VALIDATE_REPORT (pad_monitor, FLUSH_START_HAS_WRONG_SEQNUM,
1667               "Got: %u Expected: %u", seqnum,
1668               pad_monitor->pending_flush_start_seqnum);
1669         }
1670       }
1671
1672       if (pad_monitor->pending_flush_stop) {
1673         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_START_UNEXPECTED,
1674             "Received flush-start from when flush-stop was expected");
1675       }
1676       pad_monitor->pending_flush_stop = TRUE;
1677     }
1678       break;
1679     case GST_EVENT_FLUSH_STOP:
1680     {
1681       if (pad_monitor->pending_flush_stop_seqnum != GST_SEQNUM_INVALID) {
1682         if (seqnum == pad_monitor->pending_flush_stop_seqnum) {
1683           pad_monitor->pending_flush_stop_seqnum = GST_SEQNUM_INVALID;
1684         } else {
1685           GST_VALIDATE_REPORT (pad_monitor, FLUSH_STOP_HAS_WRONG_SEQNUM,
1686               "Got: %u Expected: %u", seqnum,
1687               pad_monitor->pending_flush_stop_seqnum);
1688         }
1689       }
1690
1691       pad_monitor->pending_newsegment_seqnum = seqnum;
1692       pad_monitor->pending_eos_seqnum = seqnum;
1693
1694       if (!pad_monitor->pending_flush_stop) {
1695         gchar *event_str = _get_event_string (event);
1696
1697         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_STOP_UNEXPECTED,
1698             "Unexpected flush-stop %s", event_str);
1699         g_free (event_str);
1700       }
1701       pad_monitor->pending_flush_stop = FALSE;
1702
1703       /* Buffers following a FLUSH should have the DISCONT flag set */
1704       pad_monitor->pending_buffer_discont = TRUE;
1705
1706       /* cleanup our data */
1707       gst_validate_pad_monitor_flush (pad_monitor);
1708     }
1709       break;
1710     default:
1711       break;
1712   }
1713 }
1714
1715 static void
1716 mark_pads_eos (GstValidatePadMonitor * pad_monitor)
1717 {
1718   GstValidatePadMonitor *peer_monitor;
1719   GstPad *real_peer;
1720   GstPad *pad =
1721       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1722           (pad_monitor)));
1723   GstPad *peer = gst_pad_get_peer (pad);
1724
1725   gst_object_unref (pad);
1726   pad_monitor->is_eos = TRUE;
1727   if (peer) {
1728     real_peer = _get_actual_pad (peer);
1729     peer_monitor = _GET_PAD_MONITOR (real_peer);
1730     if (peer_monitor)
1731       peer_monitor->is_eos = TRUE;
1732     gst_object_unref (peer);
1733     gst_object_unref (real_peer);
1734   }
1735 }
1736
1737 static inline gboolean
1738 _should_check_buffers (GstValidatePadMonitor * pad_monitor,
1739     gboolean force_checks)
1740 {
1741   GstPad *pad =
1742       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1743           (pad_monitor)));
1744   GstValidateMonitor *monitor = GST_VALIDATE_MONITOR (pad_monitor);
1745
1746   if (pad_monitor->first_buffer || force_checks) {
1747     if (pad_monitor->segment.rate != 1.0) {
1748       GST_INFO_OBJECT (pad_monitor, "We do not support buffer checking"
1749           " for trick modes");
1750
1751       pad_monitor->check_buffers = FALSE;
1752     } else if (!PAD_PARENT_IS_DECODER (pad_monitor)) {
1753       GST_DEBUG_OBJECT (pad, "Not on a decoder => no buffer checking");
1754
1755       pad_monitor->check_buffers = FALSE;
1756     } else if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK) {
1757       GST_DEBUG_OBJECT (pad, "Not a sinkpad => no buffer checking");
1758
1759       pad_monitor->check_buffers = FALSE;
1760     } else if (!pad_monitor->caps_is_video) {
1761       GST_DEBUG_OBJECT (pad, "Not working with video => no buffer checking");
1762
1763       pad_monitor->check_buffers = FALSE;
1764     } else if (monitor->media_descriptor == NULL) {
1765       GST_DEBUG_OBJECT (pad, "No media_descriptor set => no buffer checking");
1766
1767       pad_monitor->check_buffers = FALSE;
1768     } else if (!gst_validate_media_descriptor_detects_frames
1769         (monitor->media_descriptor)) {
1770       GST_DEBUG_OBJECT (pad,
1771           "No frame detection media descriptor => no buffer checking");
1772       pad_monitor->check_buffers = FALSE;
1773     } else if (pad_monitor->all_bufs == NULL &&
1774         !gst_validate_media_descriptor_get_buffers (monitor->media_descriptor,
1775             pad, NULL, &pad_monitor->all_bufs)) {
1776
1777       GST_INFO_OBJECT (monitor,
1778           "The MediaInfo is marked as detecting frame, but getting frames"
1779           " from pad %" GST_PTR_FORMAT " did not work (some format conversion"
1780           " might be happening)", pad);
1781
1782       pad_monitor->check_buffers = FALSE;
1783     } else {
1784       if (!pad_monitor->current_buf)
1785         pad_monitor->current_buf = pad_monitor->all_bufs;
1786       pad_monitor->check_buffers = TRUE;
1787     }
1788   }
1789   gst_object_unref (pad);
1790
1791   return pad_monitor->check_buffers;
1792 }
1793
1794 static void
1795 gst_validate_monitor_find_next_buffer (GstValidatePadMonitor * pad_monitor)
1796 {
1797   GList *tmp;
1798   gboolean passed_start = FALSE;
1799
1800   if (!_should_check_buffers (pad_monitor, TRUE))
1801     return;
1802
1803   for (tmp = g_list_last (pad_monitor->all_bufs); tmp; tmp = tmp->prev) {
1804     GstBuffer *cbuf = (GstBuffer *) tmp->data;
1805     GstClockTime ts =
1806         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cbuf)) ? GST_BUFFER_DTS (cbuf)
1807         : GST_BUFFER_PTS (cbuf);
1808
1809     if (!GST_CLOCK_TIME_IS_VALID (ts))
1810       continue;
1811
1812     if (ts <= pad_monitor->segment.start)
1813       passed_start = TRUE;
1814
1815     if (!passed_start)
1816       continue;
1817
1818     if (!GST_BUFFER_FLAG_IS_SET (cbuf, GST_BUFFER_FLAG_DELTA_UNIT)) {
1819       break;
1820     }
1821   }
1822
1823   if (tmp == NULL)
1824     pad_monitor->current_buf = pad_monitor->all_bufs;
1825   else
1826     pad_monitor->current_buf = tmp;
1827 }
1828
1829 /* Checks whether a segment is just an update of another,
1830  * That is to say that only the base and offset field differ and all
1831  * other fields are identical */
1832 static gboolean
1833 is_segment_update (GstSegment * a, const GstSegment * b)
1834 {
1835   /* Note : We never care about the position field, it is only
1836    * used for internal usage by elements */
1837   if (a->rate == b->rate &&
1838       a->applied_rate == b->applied_rate &&
1839       a->format == b->format && a->time == b->time) {
1840     /* Changes in base/offset are considered updates */
1841     /* Updating the end position of a segment is an update */
1842     /* Updating the duration of a segment is an update */
1843     if (a->rate > 0.0) {
1844       if (a->start == b->start)
1845         return TRUE;
1846     } else {
1847       if (a->stop == b->stop)
1848         return TRUE;
1849     }
1850   }
1851   return FALSE;
1852 }
1853
1854 static GstFlowReturn
1855 gst_validate_pad_monitor_downstream_event_check (GstValidatePadMonitor *
1856     pad_monitor, GstObject * parent, GstEvent * event,
1857     GstPadEventFunction handler)
1858 {
1859   GstFlowReturn ret = GST_FLOW_OK;
1860   const GstSegment *segment;
1861   guint32 seqnum = gst_event_get_seqnum (event);
1862   GstPad *pad =
1863       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1864           (pad_monitor)));
1865
1866   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
1867
1868   /* pre checks */
1869   switch (GST_EVENT_TYPE (event)) {
1870     case GST_EVENT_STREAM_START:
1871       /* Buffers following a STREAM_START should have the DISCONT flag set */
1872       pad_monitor->pending_buffer_discont = TRUE;
1873       break;
1874     case GST_EVENT_SEGMENT:
1875       /* parse segment data to be used if event is handled */
1876       gst_event_parse_segment (event, &segment);
1877
1878       GST_DEBUG_OBJECT (pad, "Got segment %" GST_SEGMENT_FORMAT, segment);
1879
1880       /* Reset expected flush start/stop values, we have a segment */
1881       pad_monitor->pending_flush_start_seqnum = GST_SEQNUM_INVALID;
1882       pad_monitor->pending_flush_stop_seqnum = GST_SEQNUM_INVALID;
1883
1884       if (pad_monitor->pending_newsegment_seqnum != GST_SEQNUM_INVALID) {
1885         if (pad_monitor->pending_newsegment_seqnum == seqnum) {
1886           pad_monitor->pending_newsegment_seqnum = GST_SEQNUM_INVALID;
1887           if (GST_CLOCK_TIME_IS_VALID (pad_monitor->pending_seek_accurate_time)) {
1888             if (segment->time == pad_monitor->pending_seek_accurate_time) {
1889               pad_monitor->pending_seek_accurate_time = GST_CLOCK_TIME_NONE;
1890             } else {
1891               GST_VALIDATE_REPORT (pad_monitor, SEGMENT_HAS_WRONG_START,
1892                   "After an accurate seek, got: %" GST_TIME_FORMAT
1893                   " Expected: %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->time),
1894                   GST_TIME_ARGS (pad_monitor->pending_seek_accurate_time));
1895             }
1896           }
1897         } else {
1898           GST_VALIDATE_REPORT (pad_monitor, SEGMENT_HAS_WRONG_SEQNUM,
1899               "Got: %u Expected: %u", seqnum, pad_monitor->pending_eos_seqnum);
1900         }
1901       }
1902
1903       pad_monitor->pending_eos_seqnum = seqnum;
1904
1905       if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
1906         gst_validate_pad_monitor_add_expected_newsegment (pad_monitor, event);
1907       } else {
1908         /* check if this segment is the expected one */
1909         if (pad_monitor->expected_segment) {
1910           const GstSegment *exp_segment;
1911
1912           if (pad_monitor->expected_segment != event) {
1913             gst_event_parse_segment (pad_monitor->expected_segment,
1914                 &exp_segment);
1915             if (segment->format == exp_segment->format) {
1916               if ((exp_segment->rate * exp_segment->applied_rate !=
1917                       segment->rate * segment->applied_rate))
1918                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1919                     "Rate * applied_rate %f != expected %f",
1920                     segment->rate * segment->applied_rate,
1921                     exp_segment->rate * exp_segment->applied_rate);
1922               if (exp_segment->start != segment->start)
1923                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1924                     "Start %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
1925                     GST_TIME_ARGS (segment->start),
1926                     GST_TIME_ARGS (exp_segment->start));
1927               if (exp_segment->stop != segment->stop)
1928                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1929                     "Stop %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
1930                     GST_TIME_ARGS (segment->stop),
1931                     GST_TIME_ARGS (exp_segment->stop));
1932               if (exp_segment->position != segment->position)
1933                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1934                     "Position %" GST_TIME_FORMAT " != expected %"
1935                     GST_TIME_FORMAT, GST_TIME_ARGS (segment->position),
1936                     GST_TIME_ARGS (exp_segment->position));
1937             }
1938           }
1939           gst_event_replace (&pad_monitor->expected_segment, NULL);
1940         }
1941       }
1942       break;
1943     case GST_EVENT_CAPS:{
1944       GstCaps *caps;
1945
1946       gst_event_parse_caps (event, &caps);
1947       gst_validate_pad_monitor_setcaps_pre (pad_monitor, caps);
1948       break;
1949     }
1950     case GST_EVENT_EOS:
1951       pad_monitor->is_eos = TRUE;
1952       /* FIXME : This feels and looks wrong ... */
1953       if (pad_monitor->pending_eos_seqnum == GST_SEQNUM_INVALID) {
1954         GST_VALIDATE_REPORT (pad_monitor, EVENT_EOS_WITHOUT_SEGMENT,
1955             "EOS %" GST_PTR_FORMAT " received before a segment was received",
1956             event);
1957       } else if (pad_monitor->pending_eos_seqnum != seqnum) {
1958         GST_VALIDATE_REPORT (pad_monitor, EOS_HAS_WRONG_SEQNUM,
1959             "Got: %u. Expected: %u", seqnum, pad_monitor->pending_eos_seqnum);
1960       }
1961
1962       /*
1963        * TODO add end of stream checks for
1964        *  - events not pushed
1965        *  - buffer data not pushed
1966        *  - pending events not received
1967        */
1968       break;
1969
1970       /* both flushes are handled by the common event function */
1971     case GST_EVENT_FLUSH_START:
1972     case GST_EVENT_FLUSH_STOP:
1973     case GST_EVENT_TAG:
1974     case GST_EVENT_SINK_MESSAGE:
1975     default:
1976       break;
1977   }
1978
1979   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1980   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1981   gst_validate_pad_monitor_event_overrides (pad_monitor, event);
1982   if (handler) {
1983     gst_event_ref (event);
1984     if (pad_monitor->event_full_func)
1985       ret = pad_monitor->event_full_func (pad, parent, event);
1986     else if (pad_monitor->event_func (pad, parent, event))
1987       ret = GST_FLOW_OK;
1988     else
1989       ret = GST_FLOW_ERROR;
1990   }
1991   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1992   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1993
1994   /* post checks */
1995   switch (GST_EVENT_TYPE (event)) {
1996     case GST_EVENT_SEGMENT:
1997       if (ret == GST_FLOW_OK) {
1998         /* If the new segment is not an update of the previous one, then
1999          * the following buffer should have the DISCONT flag set */
2000         if (!is_segment_update (&pad_monitor->segment, segment))
2001           pad_monitor->pending_buffer_discont = TRUE;
2002         if (!pad_monitor->has_segment
2003             && pad_monitor->segment.format != segment->format) {
2004           gst_segment_init (&pad_monitor->segment, segment->format);
2005         }
2006         gst_segment_copy_into (segment, &pad_monitor->segment);
2007         pad_monitor->has_segment = TRUE;
2008         gst_validate_monitor_find_next_buffer (pad_monitor);
2009       }
2010       break;
2011     case GST_EVENT_CAPS:{
2012       GstCaps *caps;
2013
2014       gst_event_parse_caps (event, &caps);
2015       gst_validate_pad_monitor_setcaps_post (pad_monitor, caps,
2016           ret == GST_FLOW_OK);
2017       break;
2018     }
2019     case GST_EVENT_FLUSH_START:
2020     case GST_EVENT_FLUSH_STOP:
2021     case GST_EVENT_EOS:
2022     case GST_EVENT_TAG:
2023     case GST_EVENT_SINK_MESSAGE:
2024     default:
2025       break;
2026   }
2027
2028   if (handler)
2029     gst_event_unref (event);
2030   gst_object_unref (pad);
2031   return ret;
2032 }
2033
2034 static gboolean
2035 gst_validate_pad_monitor_src_event_check (GstValidatePadMonitor * pad_monitor,
2036     GstObject * parent, GstEvent * event, GstPadEventFunction handler)
2037 {
2038   gboolean ret = TRUE;
2039   gdouble rate;
2040   GstFormat format;
2041   gint64 start, stop;
2042   GstSeekFlags seek_flags;
2043   GstSeekType start_type, stop_type;
2044   guint32 seqnum = gst_event_get_seqnum (event);
2045   GstPad *pad =
2046       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2047           (pad_monitor)));
2048
2049   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
2050
2051   /* pre checks */
2052   switch (GST_EVENT_TYPE (event)) {
2053     case GST_EVENT_SEEK:
2054     {
2055       gst_event_parse_seek (event, &rate, &format, &seek_flags, &start_type,
2056           &start, &stop_type, &stop);
2057       /* upstream seek - store the seek event seqnum to check
2058        * flushes and newsegments share the same */
2059     }
2060       break;
2061       /* both flushes are handled by the common event handling function */
2062     case GST_EVENT_FLUSH_START:
2063     case GST_EVENT_FLUSH_STOP:
2064     case GST_EVENT_NAVIGATION:
2065     case GST_EVENT_LATENCY:
2066     case GST_EVENT_STEP:
2067     case GST_EVENT_QOS:
2068     default:
2069       break;
2070   }
2071
2072   if (handler) {
2073     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2074     /* Safely store pending accurate seek values */
2075     if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
2076       if (seek_flags & GST_SEEK_FLAG_ACCURATE) {
2077         GST_DEBUG_OBJECT (pad,
2078             "Storing expected accurate seek time %" GST_TIME_FORMAT,
2079             GST_TIME_ARGS (start));
2080         pad_monitor->pending_seek_accurate_time = start;
2081       }
2082       /* TODO we might need to use a list as multiple seeks can be sent
2083        * before the flushes arrive here */
2084       if (seek_flags & GST_SEEK_FLAG_FLUSH) {
2085         pad_monitor->pending_flush_start_seqnum = seqnum;
2086         pad_monitor->pending_flush_stop_seqnum = seqnum;
2087       }
2088     }
2089
2090     gst_event_ref (event);
2091     ret = pad_monitor->event_func (pad, parent, event);
2092
2093     if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
2094       /* If the seek was already handled (same current seqnum), reset the
2095        * expected accurate seek value */
2096       if (ret && pad_monitor->has_segment
2097           && seqnum == pad_monitor->pending_eos_seqnum) {
2098         GST_DEBUG_OBJECT (pad,
2099             "Resetting expected accurate seek value, was already handled");
2100         pad_monitor->pending_seek_accurate_time = GST_CLOCK_TIME_NONE;
2101       } else if (!ret) {
2102         /* do not expect any of these events anymore */
2103         pad_monitor->pending_flush_start_seqnum = GST_SEQNUM_INVALID;
2104         pad_monitor->pending_flush_stop_seqnum = GST_SEQNUM_INVALID;
2105         pad_monitor->pending_newsegment_seqnum = GST_SEQNUM_INVALID;
2106         pad_monitor->pending_eos_seqnum = GST_SEQNUM_INVALID;
2107         pad_monitor->pending_seek_accurate_time = GST_CLOCK_TIME_NONE;
2108       }
2109     }
2110     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2111   }
2112
2113   /* post checks */
2114   switch (GST_EVENT_TYPE (event)) {
2115     case GST_EVENT_FLUSH_START:
2116     case GST_EVENT_FLUSH_STOP:
2117     case GST_EVENT_QOS:
2118     case GST_EVENT_SEEK:
2119     case GST_EVENT_NAVIGATION:
2120     case GST_EVENT_LATENCY:
2121     case GST_EVENT_STEP:
2122     default:
2123       break;
2124   }
2125
2126   if (handler)
2127     gst_event_unref (event);
2128   gst_object_unref (pad);
2129   return ret;
2130 }
2131
2132 static gboolean
2133 gst_validate_pad_monitor_check_right_buffer (GstValidatePadMonitor *
2134     pad_monitor, GstBuffer * buffer)
2135 {
2136   gchar *checksum;
2137   GstBuffer *wanted_buf;
2138   GstMapInfo map, wanted_map;
2139
2140   gboolean ret = TRUE;
2141   GstPad *pad;
2142
2143
2144   if (_should_check_buffers (pad_monitor, FALSE) == FALSE)
2145     return FALSE;
2146
2147   pad =
2148       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2149           (pad_monitor)));
2150   if (pad_monitor->current_buf == NULL) {
2151     GST_INFO_OBJECT (pad, "No current buffer one pad, Why?");
2152     gst_object_unref (pad);
2153     return FALSE;
2154   }
2155
2156   wanted_buf = pad_monitor->current_buf->data;
2157
2158   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (wanted_buf)) &&
2159       GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buffer)) &&
2160       GST_BUFFER_PTS (wanted_buf) != GST_BUFFER_PTS (buffer)) {
2161
2162     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2163         "buffer %" GST_PTR_FORMAT " PTS %" GST_TIME_FORMAT
2164         " different than expected: %" GST_TIME_FORMAT, buffer,
2165         GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2166         GST_TIME_ARGS (GST_BUFFER_PTS (wanted_buf)));
2167
2168     ret = FALSE;
2169   }
2170
2171   if (GST_BUFFER_DTS (wanted_buf) != GST_BUFFER_DTS (buffer)) {
2172     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2173         "buffer %" GST_PTR_FORMAT " DTS %" GST_TIME_FORMAT
2174         " different than expected: %" GST_TIME_FORMAT, buffer,
2175         GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
2176         GST_TIME_ARGS (GST_BUFFER_DTS (wanted_buf)));
2177     ret = FALSE;
2178   }
2179
2180   if (GST_BUFFER_DURATION (wanted_buf) != GST_BUFFER_DURATION (buffer)) {
2181     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2182         "buffer %" GST_PTR_FORMAT " DURATION %" GST_TIME_FORMAT
2183         " different than expected: %" GST_TIME_FORMAT, buffer,
2184         GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
2185         GST_TIME_ARGS (GST_BUFFER_DURATION (wanted_buf)));
2186     ret = FALSE;
2187   }
2188
2189   if (GST_BUFFER_FLAG_IS_SET (wanted_buf, GST_BUFFER_FLAG_DELTA_UNIT) !=
2190       GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
2191     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2192         "buffer %" GST_PTR_FORMAT "  Delta unit is set to %s but expected %s",
2193         buffer, GST_BUFFER_FLAG_IS_SET (buffer,
2194             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False",
2195         GST_BUFFER_FLAG_IS_SET (wanted_buf,
2196             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False");
2197     ret = FALSE;
2198   }
2199
2200   g_assert (gst_buffer_map (wanted_buf, &wanted_map, GST_MAP_READ));
2201   g_assert (gst_buffer_map (buffer, &map, GST_MAP_READ));
2202
2203   checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5,
2204       (const guchar *) map.data, map.size);
2205
2206   if (g_strcmp0 ((gchar *) wanted_map.data, checksum)) {
2207     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2208         "buffer %" GST_PTR_FORMAT " checksum %s different from expected: %s",
2209         buffer, checksum, wanted_map.data);
2210     ret = FALSE;
2211   }
2212
2213   gst_buffer_unmap (wanted_buf, &wanted_map);
2214   gst_buffer_unmap (buffer, &map);
2215   g_free (checksum);
2216   gst_object_unref (pad);
2217
2218   pad_monitor->current_buf = pad_monitor->current_buf->next;
2219
2220   return ret;
2221 }
2222
2223 static void
2224 gst_validate_pad_monitor_check_return (GstValidatePadMonitor * pad_monitor,
2225     GstFlowReturn ret)
2226 {
2227   GstValidateMonitor *parent = GST_VALIDATE_MONITOR (pad_monitor);
2228
2229   if (ret != GST_FLOW_ERROR)
2230     return;
2231
2232   while (GST_VALIDATE_MONITOR_GET_PARENT (parent))
2233     parent = GST_VALIDATE_MONITOR_GET_PARENT (parent);
2234
2235   if (GST_IS_VALIDATE_PIPELINE_MONITOR (parent)) {
2236     GstValidatePipelineMonitor *m = GST_VALIDATE_PIPELINE_MONITOR (parent);
2237
2238     GST_VALIDATE_MONITOR_LOCK (m);
2239     if (m->got_error == FALSE) {
2240       GST_VALIDATE_REPORT (pad_monitor, FLOW_ERROR_WITHOUT_ERROR_MESSAGE,
2241           "Pad return GST_FLOW_ERROR but no GST_MESSAGE_ERROR was received on"
2242           " the bus");
2243
2244       /* Only report it the first time */
2245       m->got_error = TRUE;
2246     }
2247     GST_VALIDATE_MONITOR_UNLOCK (m);
2248   }
2249 }
2250
2251 static GstFlowReturn
2252 gst_validate_pad_monitor_chain_func (GstPad * pad, GstObject * parent,
2253     GstBuffer * buffer)
2254 {
2255   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2256   GstFlowReturn ret;
2257
2258   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2259   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2260
2261   gst_validate_pad_monitor_check_discont (pad_monitor, buffer);
2262   gst_validate_pad_monitor_check_right_buffer (pad_monitor, buffer);
2263   gst_validate_pad_monitor_check_first_buffer (pad_monitor, buffer);
2264   gst_validate_pad_monitor_update_buffer_data (pad_monitor, buffer);
2265   gst_validate_pad_monitor_check_eos (pad_monitor, buffer);
2266
2267   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2268   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2269
2270   gst_validate_pad_monitor_buffer_overrides (pad_monitor, buffer);
2271
2272   ret = pad_monitor->chain_func (pad, parent, buffer);
2273
2274   gst_validate_pad_monitor_check_return (pad_monitor, ret);
2275
2276   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2277   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2278
2279   pad_monitor->last_flow_return = ret;
2280   if (ret == GST_FLOW_EOS) {
2281     mark_pads_eos (pad_monitor);
2282   }
2283   if (PAD_PARENT_IS_DEMUXER (pad_monitor))
2284     gst_validate_pad_monitor_check_aggregated_return (pad_monitor, parent, ret);
2285
2286   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2287   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2288
2289   return ret;
2290 }
2291
2292 static gboolean
2293 gst_validate_pad_monitor_event_is_tracked (GstValidatePadMonitor * monitor,
2294     GstEvent * event)
2295 {
2296   if (!GST_EVENT_IS_SERIALIZED (event)) {
2297     return FALSE;
2298   }
2299
2300   /* we don't track Tag events because they mutate too much and it is hard
2301    * to match a tag event pushed on a source pad with the one that was received
2302    * on a sink pad.
2303    * One idea would be to use seqnum, but it seems that it is undefined whether
2304    * seqnums should be maintained in tag events that are created from others
2305    * up to today. (2013-08-29)
2306    */
2307   if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
2308     return FALSE;
2309
2310   return TRUE;
2311 }
2312
2313 static GstFlowReturn
2314 gst_validate_pad_monitor_sink_event_full_func (GstPad * pad, GstObject * parent,
2315     GstEvent * event)
2316 {
2317   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2318   GstFlowReturn ret;
2319
2320   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2321   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2322
2323   GST_DEBUG_OBJECT (pad, "event %p %s", event, GST_EVENT_TYPE_NAME (event));
2324
2325   if (gst_validate_pad_monitor_event_is_tracked (pad_monitor, event)) {
2326     GstClockTime last_ts = GST_CLOCK_TIME_NONE;
2327     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_timestamp)) {
2328       last_ts = pad_monitor->current_timestamp;
2329       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_duration)) {
2330         last_ts += pad_monitor->current_duration;
2331       }
2332     }
2333     gst_validate_pad_monitor_otherpad_add_pending_serialized_event (pad_monitor,
2334         event, last_ts);
2335   }
2336
2337   ret =
2338       gst_validate_pad_monitor_downstream_event_check (pad_monitor, parent,
2339       event, pad_monitor->event_func);
2340
2341   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2342   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2343   return ret;
2344 }
2345
2346 static gboolean
2347 gst_validate_pad_monitor_sink_event_func (GstPad * pad, GstObject * parent,
2348     GstEvent * event)
2349 {
2350   if (gst_validate_pad_monitor_sink_event_full_func (pad, parent,
2351           event) == GST_FLOW_OK)
2352     return TRUE;
2353   return FALSE;
2354 }
2355
2356 static gboolean
2357 gst_validate_pad_monitor_src_event_func (GstPad * pad, GstObject * parent,
2358     GstEvent * event)
2359 {
2360   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2361   gboolean ret;
2362
2363   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2364   ret = gst_validate_pad_monitor_src_event_check (pad_monitor, parent, event,
2365       pad_monitor->event_func);
2366   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2367   return ret;
2368 }
2369
2370 static gboolean
2371 gst_validate_pad_monitor_query_func (GstPad * pad, GstObject * parent,
2372     GstQuery * query)
2373 {
2374   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2375   gboolean ret;
2376
2377   gst_validate_pad_monitor_query_overrides (pad_monitor, query);
2378   ret = pad_monitor->query_func (pad, parent, query);
2379
2380   if (ret) {
2381     switch (GST_QUERY_TYPE (query)) {
2382       case GST_QUERY_ACCEPT_CAPS:
2383       {
2384         gboolean result;
2385
2386         gst_caps_replace (&pad_monitor->last_refused_caps, NULL);
2387         gst_query_parse_accept_caps_result (query, &result);
2388         if (!result) {
2389           GstCaps *refused_caps;
2390
2391           gst_query_parse_accept_caps (query, &refused_caps);
2392           pad_monitor->last_refused_caps = gst_caps_copy (refused_caps);
2393
2394         }
2395
2396         break;
2397       }
2398       case GST_QUERY_CAPS:{
2399         GstCaps *res;
2400         GstCaps *filter;
2401
2402         /* We shouldn't need to lock the parent as this doesn't modify
2403          * other monitors, just does some peer_pad_caps */
2404         GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2405
2406         gst_query_parse_caps (query, &filter);
2407         gst_query_parse_caps_result (query, &res);
2408
2409         gst_caps_replace (&pad_monitor->last_query_res, NULL);
2410         gst_caps_replace (&pad_monitor->last_query_filter, NULL);
2411         pad_monitor->last_query_res =
2412             res ? gst_caps_copy (res) : gst_caps_ref (GST_CAPS_NONE);
2413         pad_monitor->last_query_filter =
2414             filter ? gst_caps_copy (filter) : gst_caps_ref (GST_CAPS_NONE);
2415
2416         if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2417           gst_validate_pad_monitor_check_caps_fields_proxied (pad_monitor, res,
2418               filter);
2419         }
2420         GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2421         break;
2422       }
2423       default:
2424         break;
2425     }
2426   }
2427
2428   return ret;
2429 }
2430
2431 static gboolean
2432 gst_validate_pad_monitor_activatemode_func (GstPad * pad, GstObject * parent,
2433     GstPadMode mode, gboolean active)
2434 {
2435   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2436   gboolean ret = TRUE;
2437
2438   /* TODO add overrides for activate func */
2439   GST_DEBUG_OBJECT (pad, "active:%d", active);
2440
2441   if (pad_monitor->activatemode_func)
2442     ret = pad_monitor->activatemode_func (pad, parent, mode, active);
2443   if (ret && active == FALSE) {
2444     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2445     gst_validate_pad_monitor_reset (pad_monitor);
2446     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2447   }
2448
2449   return ret;
2450 }
2451
2452 /* The interval between two buffer frequency checks */
2453 #define BUF_FREQ_CHECK_INTERVAL (GST_SECOND)
2454
2455 static void
2456 gst_validate_pad_monitor_check_buffer_freq (GstValidatePadMonitor * monitor,
2457     GstPad * pad)
2458 {
2459   GstClockTime ts;
2460
2461   if (!GST_PAD_IS_SRC (pad))
2462     return;
2463
2464   if (!monitor->min_buf_freq)
2465     return;
2466
2467   ts = gst_util_get_timestamp ();
2468   monitor->buffers_pushed++;
2469
2470   /* Same logic as in fpsdisplaysink to compute the buffer frequency */
2471   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID
2472           (monitor->min_buf_freq_first_buffer_ts))) {
2473     monitor->min_buf_freq_first_buffer_ts = ts;
2474     monitor->min_buf_freq_interval_ts = ts;
2475     return;
2476   }
2477
2478   if (GST_CLOCK_DIFF (monitor->min_buf_freq_interval_ts,
2479           ts) > BUF_FREQ_CHECK_INTERVAL) {
2480     guint time_diff;
2481     gdouble fps;
2482
2483     time_diff = (gdouble) (ts - monitor->min_buf_freq_interval_ts) / GST_SECOND;
2484     fps =
2485         (gdouble) (monitor->buffers_pushed -
2486         monitor->last_buffers_pushed) / time_diff;
2487
2488     if (fps < monitor->min_buf_freq) {
2489       if (GST_CLOCK_TIME_IS_VALID (monitor->min_buf_freq_start) &&
2490           GST_CLOCK_DIFF (monitor->min_buf_freq_first_buffer_ts,
2491               ts) < monitor->min_buf_freq_start) {
2492         GST_DEBUG_OBJECT (pad,
2493             "buffer frequency is too low (%.2f) but ignore for now (buffer-frequency-start =%"
2494             GST_TIME_FORMAT ")", fps,
2495             GST_TIME_ARGS (monitor->min_buf_freq_start));
2496       } else {
2497         GST_VALIDATE_REPORT (monitor, CONFIG_BUFFER_FREQUENCY_TOO_LOW,
2498             "Buffers are not pushed fast enough on this pad: %.2f/sec (minimum: %.2f)",
2499             fps, monitor->min_buf_freq);
2500       }
2501     }
2502
2503     monitor->last_buffers_pushed = monitor->buffers_pushed;
2504     monitor->min_buf_freq_interval_ts = ts;
2505   }
2506 }
2507
2508 static gboolean
2509 gst_validate_pad_monitor_buffer_probe (GstPad * pad, GstBuffer * buffer,
2510     gpointer udata, gboolean pull_mode)
2511 {
2512   GstValidatePadMonitor *monitor = udata;
2513
2514   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2515   GST_VALIDATE_MONITOR_LOCK (monitor);
2516
2517   if (!pull_mode)
2518     gst_validate_pad_monitor_check_discont (monitor, buffer);
2519   gst_validate_pad_monitor_check_first_buffer (monitor, buffer);
2520   gst_validate_pad_monitor_update_buffer_data (monitor, buffer);
2521   gst_validate_pad_monitor_check_eos (monitor, buffer);
2522
2523   if (PAD_PARENT_IS_DECODER (monitor) || PAD_PARENT_IS_ENCODER (monitor)) {
2524     GstClockTime tolerance = 0;
2525
2526     if (monitor->caps_is_audio)
2527       tolerance = AUDIO_TIMESTAMP_TOLERANCE;
2528
2529     gst_validate_pad_monitor_check_buffer_timestamp_in_received_range (monitor,
2530         buffer, tolerance);
2531   }
2532
2533   gst_validate_pad_monitor_check_late_serialized_events (monitor,
2534       GST_BUFFER_TIMESTAMP (buffer));
2535
2536   /* a GstValidatePadMonitor parent must be a GstValidateElementMonitor */
2537   if (PAD_PARENT_IS_DECODER (monitor)) {
2538
2539     /* should not push out of segment data */
2540     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer)) &&
2541         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer)) &&
2542         ((!gst_segment_clip (&monitor->segment, monitor->segment.format,
2543                     GST_BUFFER_TIMESTAMP (buffer),
2544                     GST_BUFFER_TIMESTAMP (buffer) +
2545                     GST_BUFFER_DURATION (buffer), NULL, NULL)) ||
2546             /* In the case of raw data, buffers should be strictly contained inside the
2547              * segment */
2548             (monitor->caps_is_raw &&
2549                 GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer) <
2550                 monitor->segment.start))
2551         ) {
2552       /* TODO is this a timestamp issue? */
2553       GST_VALIDATE_REPORT (monitor, BUFFER_IS_OUT_OF_SEGMENT,
2554           "buffer is out of segment and shouldn't be pushed. Timestamp: %"
2555           GST_TIME_FORMAT " - Duration: %" GST_TIME_FORMAT ". Range: %"
2556           GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
2557           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2558           GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
2559           GST_TIME_ARGS (monitor->segment.start),
2560           GST_TIME_ARGS (monitor->segment.stop));
2561     }
2562   }
2563
2564   gst_validate_pad_monitor_check_buffer_freq (monitor, pad);
2565
2566   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2567   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2568   gst_validate_pad_monitor_buffer_probe_overrides (monitor, buffer);
2569   return TRUE;
2570 }
2571
2572 static void
2573 gst_validate_pad_monitor_event_probe (GstPad * pad, GstEvent * event,
2574     gpointer udata)
2575 {
2576   GstValidatePadMonitor *monitor = GST_VALIDATE_PAD_MONITOR_CAST (udata);
2577
2578   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2579   GST_VALIDATE_MONITOR_LOCK (monitor);
2580
2581   GST_DEBUG_OBJECT (pad, "event %p %s", event, GST_EVENT_TYPE_NAME (event));
2582
2583   if (GST_EVENT_IS_SERIALIZED (event)) {
2584     gint i;
2585
2586     /* Detect if events the element received are being forwarded in the same order
2587      *
2588      * Several scenarios:
2589      * 1) The element pushes the event as-is
2590      * 2) The element consumes the event and does not forward it
2591      * 3) The element consumes the event and creates another one instead
2592      * 4) The element pushes other serialized event before pushing out the
2593      *    one it received
2594      *
2595      * For each pad we have two lists to track serialized events:
2596      *  1) We received on input and expect to see (serialized_events)
2597      *  2) We received on input but don't expect to see (expired_events)
2598      *
2599      * To detect events that are pushed in a different order from the one they were
2600      * received in we check that:
2601      *
2602      * For each event being outputted:
2603      *   If it is in the expired_events list:
2604      *     RAISE WARNING
2605      *   If it is in the serialized_events list:
2606      *     If there are other events that were received before:
2607      *        Put those events on the expired_events list
2608      *     Remove that event and any previous ones from the serialized_events list
2609      *
2610      * Clear expired events list when flushing or on pad deactivation
2611      *
2612      */
2613
2614     if (g_list_find (monitor->expired_events, event)) {
2615       gchar *event_str = _get_event_string (event);
2616       /* If it's the expired events, we've failed */
2617       GST_WARNING_OBJECT (pad, "Did not expect event %p %s", event,
2618           GST_EVENT_TYPE_NAME (event));
2619       GST_VALIDATE_REPORT (monitor, EVENT_SERIALIZED_OUT_OF_ORDER,
2620           "Serialized event was pushed out of order: %s", event_str);
2621
2622       g_free (event_str);
2623       monitor->expired_events = g_list_remove (monitor->expired_events, event);
2624       gst_event_unref (event);  /* remove the ref that was on the list */
2625     } else if (monitor->serialized_events->len) {
2626       for (i = 0; i < monitor->serialized_events->len; i++) {
2627         SerializedEventData *next_event =
2628             g_ptr_array_index (monitor->serialized_events, i);
2629         GST_DEBUG_OBJECT (pad, "Checking against stored event #%d: %p %s", i,
2630             next_event->event, GST_EVENT_TYPE_NAME (next_event->event));
2631
2632         if (event == next_event->event
2633             || GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2634           /* We have found our event */
2635           GST_DEBUG_OBJECT (pad, "Found matching event");
2636
2637           while (monitor->serialized_events->len > i
2638               && GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2639             /* Swallow all expected events of the same type */
2640             g_ptr_array_remove_index (monitor->serialized_events, i);
2641             next_event = g_ptr_array_index (monitor->serialized_events, i);
2642           }
2643
2644           /* Move all previous events to expired events */
2645           if (G_UNLIKELY (i > 0)) {
2646             GST_DEBUG_OBJECT (pad,
2647                 "Moving previous expected events to expired list");
2648             while (i--) {
2649               next_event = g_ptr_array_index (monitor->serialized_events, 0);
2650               monitor->expired_events =
2651                   g_list_append (monitor->expired_events,
2652                   gst_event_ref (next_event->event));
2653               g_ptr_array_remove_index (monitor->serialized_events, 0);
2654             }
2655           }
2656           debug_pending_event (pad, monitor->serialized_events);
2657           break;
2658         }
2659       }
2660     }
2661   }
2662
2663   /* This so far is just like an event that is flowing downstream,
2664    * so we do the same checks as a sinkpad event handler */
2665   gst_validate_pad_monitor_downstream_event_check (monitor, NULL, event, NULL);
2666   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2667   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2668 }
2669
2670 static GstPadProbeReturn
2671 gst_validate_pad_monitor_pad_probe (GstPad * pad, GstPadProbeInfo * info,
2672     gpointer udata)
2673 {
2674   if (info->type & GST_PAD_PROBE_TYPE_BUFFER)
2675     gst_validate_pad_monitor_buffer_probe (pad, info->data, udata,
2676         GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_PULL);
2677   else if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM)
2678     gst_validate_pad_monitor_event_probe (pad, info->data, udata);
2679
2680   return GST_PAD_PROBE_OK;
2681 }
2682
2683 static void
2684 gst_validate_pad_monitor_update_caps_info (GstValidatePadMonitor * pad_monitor,
2685     GstCaps * caps)
2686 {
2687   GstStructure *structure;
2688
2689   g_return_if_fail (gst_caps_is_fixed (caps));
2690
2691   pad_monitor->caps_is_audio = FALSE;
2692   pad_monitor->caps_is_video = FALSE;
2693
2694   structure = gst_caps_get_structure (caps, 0);
2695   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/")) {
2696     pad_monitor->caps_is_audio = TRUE;
2697   } else if (g_str_has_prefix (gst_structure_get_name (structure), "video/")) {
2698     pad_monitor->caps_is_video = TRUE;
2699   }
2700
2701   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/x-raw") ||
2702       g_str_has_prefix (gst_structure_get_name (structure), "video/x-raw")) {
2703     pad_monitor->caps_is_raw = TRUE;
2704   } else {
2705     pad_monitor->caps_is_raw = FALSE;
2706   }
2707 }
2708
2709 static void
2710 gst_validate_pad_monitor_setcaps_pre (GstValidatePadMonitor * pad_monitor,
2711     GstCaps * caps)
2712 {
2713   GstStructure *structure;
2714   GstPad *pad =
2715       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2716           (pad_monitor)));
2717
2718   /* Check if caps are identical to last caps and complain if so
2719    * Only checked for sink pads as src pads might push the same caps
2720    * multiple times during unlinked/autoplugging scenarios */
2721   if (GST_PAD_IS_SINK (pad) && pad_monitor->last_caps
2722       && gst_caps_is_equal (caps, pad_monitor->last_caps)) {
2723     gchar *caps_str = gst_caps_to_string (caps);
2724
2725     GST_VALIDATE_REPORT (pad_monitor, EVENT_CAPS_DUPLICATE, "%s", caps_str);
2726     g_free (caps_str);
2727
2728   }
2729
2730   gst_validate_pad_monitor_check_caps_complete (pad_monitor, caps);
2731
2732   if (caps) {
2733     structure = gst_caps_get_structure (caps, 0);
2734     if (gst_structure_n_fields (pad_monitor->pending_setcaps_fields)) {
2735       gint i;
2736       for (i = 0;
2737           i < gst_structure_n_fields (pad_monitor->pending_setcaps_fields);
2738           i++) {
2739         const gchar *name =
2740             gst_structure_nth_field_name (pad_monitor->pending_setcaps_fields,
2741             i);
2742         const GValue *v = gst_structure_get_value (structure, name);
2743         const GValue *otherv =
2744             gst_structure_get_value (pad_monitor->pending_setcaps_fields, name);
2745
2746         if (v == NULL) {
2747           gchar *caps_str = gst_caps_to_string (caps);
2748
2749           GST_VALIDATE_REPORT (pad_monitor, CAPS_EXPECTED_FIELD_NOT_FOUND,
2750               "Field %s is missing from setcaps caps '%s'", name, caps_str);
2751           g_free (caps_str);
2752         } else if (gst_value_compare (v, otherv) != GST_VALUE_EQUAL) {
2753           gchar *caps_str = gst_caps_to_string (caps),
2754               *pending_setcaps_fields_str =
2755               gst_structure_to_string (pad_monitor->pending_setcaps_fields);
2756
2757
2758           GST_VALIDATE_REPORT (pad_monitor, CAPS_FIELD_UNEXPECTED_VALUE,
2759               "Field %s from setcaps caps '%s' is different "
2760               "from expected value in caps '%s'", name, caps_str,
2761               pending_setcaps_fields_str);
2762
2763           g_free (pending_setcaps_fields_str);
2764           g_free (caps_str);
2765         }
2766       }
2767     }
2768
2769     if (GST_PAD_IS_SINK (pad) &&
2770         gst_validate_pad_monitor_pad_should_proxy_othercaps (pad_monitor)) {
2771       if (_structure_is_video (structure)) {
2772         GST_DEBUG_OBJECT (pad,
2773             "Adding video common pending fields to other pad: %" GST_PTR_FORMAT,
2774             structure);
2775         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2776             structure, "width");
2777         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2778             structure, "height");
2779         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2780             structure, "framerate");
2781         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2782             structure, "pixel-aspect-ratio");
2783       } else if (_structure_is_audio (structure)) {
2784         GST_DEBUG_OBJECT (pad,
2785             "Adding audio common pending fields to other pad: %" GST_PTR_FORMAT,
2786             structure);
2787         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2788             structure, "rate");
2789         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2790             structure, "channels");
2791       }
2792     }
2793   }
2794
2795   gst_structure_free (pad_monitor->pending_setcaps_fields);
2796   pad_monitor->pending_setcaps_fields =
2797       gst_structure_new_empty (PENDING_FIELDS);
2798   gst_object_unref (pad);
2799
2800   gst_validate_pad_monitor_setcaps_overrides (pad_monitor, caps);
2801 }
2802
2803 static void
2804 gst_validate_pad_monitor_setcaps_post (GstValidatePadMonitor * pad_monitor,
2805     GstCaps * caps, gboolean ret)
2806 {
2807   if (!ret)
2808     gst_validate_pad_monitor_otherpad_clear_pending_fields (pad_monitor);
2809   else {
2810     if (pad_monitor->last_caps) {
2811       gst_caps_unref (pad_monitor->last_caps);
2812     }
2813     pad_monitor->last_caps = gst_caps_ref (caps);
2814     gst_validate_pad_monitor_update_caps_info (pad_monitor, caps);
2815   }
2816 }
2817
2818 static void
2819 gst_validate_pad_monitor_get_min_buffer_frequency (GstValidatePadMonitor *
2820     monitor, GstPad * pad)
2821 {
2822   GList *config, *l;
2823
2824   if (!GST_PAD_IS_SRC (pad))
2825     return;
2826
2827   config = gst_validate_plugin_get_config (NULL);
2828   for (l = config; l != NULL; l = g_list_next (l)) {
2829     GstStructure *s = l->data;
2830     gdouble min_buf_freq;
2831     const gchar *pad_name;
2832     GstElement *element = NULL;
2833
2834     if (!gst_structure_get_double (s, "min-buffer-frequency", &min_buf_freq)) {
2835       gint max_int;
2836
2837       if (!gst_structure_get_int (s, "min-buffer-frequency", &max_int))
2838         goto next;
2839
2840       min_buf_freq = max_int;
2841     }
2842
2843     pad_name = gst_structure_get_string (s, "name");
2844     if (!pad_name)
2845       pad_name = "src";
2846
2847     if (g_strcmp0 (GST_PAD_NAME (pad), pad_name))
2848       goto next;
2849
2850     element = gst_pad_get_parent_element (pad);
2851
2852     if (!gst_validate_element_matches_target (element, s))
2853       goto next;
2854
2855     monitor->min_buf_freq = min_buf_freq;
2856
2857     gst_validate_utils_get_clocktime (s, "buffer-frequency-start",
2858         &monitor->min_buf_freq_start);
2859
2860     GST_DEBUG_OBJECT (pad, "pad has a minimum buffer frequency of %f",
2861         min_buf_freq);
2862   next:
2863     g_clear_object (&element);
2864   }
2865 }
2866
2867 static gboolean
2868 gst_validate_pad_monitor_do_setup (GstValidateMonitor * monitor)
2869 {
2870   GstValidatePadMonitor *pad_monitor = GST_VALIDATE_PAD_MONITOR_CAST (monitor);
2871   GstPad *pad = (gpointer) gst_validate_monitor_get_target (monitor);
2872
2873   if (!GST_IS_PAD (pad)) {
2874     GST_WARNING_OBJECT (monitor, "Trying to create pad monitor with other "
2875         "type of object");
2876     gst_object_unref (pad);
2877     return FALSE;
2878   }
2879
2880   if (_GET_PAD_MONITOR (pad)) {
2881     GST_WARNING_OBJECT (pad_monitor,
2882         "Pad already has a validate-monitor associated");
2883     gst_object_unref (pad);
2884     return FALSE;
2885   }
2886
2887   _SET_PAD_MONITOR (pad, pad_monitor);
2888
2889   pad_monitor->event_func = GST_PAD_EVENTFUNC (pad);
2890   pad_monitor->event_full_func = GST_PAD_EVENTFULLFUNC (pad);
2891   pad_monitor->query_func = GST_PAD_QUERYFUNC (pad);
2892   pad_monitor->activatemode_func = GST_PAD_ACTIVATEMODEFUNC (pad);
2893   if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2894
2895     pad_monitor->chain_func = GST_PAD_CHAINFUNC (pad);
2896     if (pad_monitor->chain_func)
2897       gst_pad_set_chain_function (pad, gst_validate_pad_monitor_chain_func);
2898
2899     if (pad_monitor->event_full_func)
2900       gst_pad_set_event_full_function (pad,
2901           gst_validate_pad_monitor_sink_event_full_func);
2902     else
2903       gst_pad_set_event_function (pad,
2904           gst_validate_pad_monitor_sink_event_func);
2905   } else {
2906     gst_pad_set_event_function (pad, gst_validate_pad_monitor_src_event_func);
2907
2908     /* add buffer/event probes */
2909     pad_monitor->pad_probe_id =
2910         gst_pad_add_probe (pad,
2911         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
2912         GST_PAD_PROBE_TYPE_EVENT_FLUSH,
2913         (GstPadProbeCallback) gst_validate_pad_monitor_pad_probe, pad_monitor,
2914         NULL);
2915   }
2916   gst_pad_set_query_function (pad, gst_validate_pad_monitor_query_func);
2917   gst_pad_set_activatemode_function (pad,
2918       gst_validate_pad_monitor_activatemode_func);
2919
2920   gst_validate_reporter_set_name (GST_VALIDATE_REPORTER (monitor),
2921       g_strdup_printf ("%s:%s", GST_DEBUG_PAD_NAME (pad)));
2922
2923   if (G_UNLIKELY (GST_PAD_PARENT (pad) == NULL))
2924     GST_FIXME ("Saw a pad not belonging to any object");
2925
2926   gst_validate_pad_monitor_get_min_buffer_frequency (pad_monitor, pad);
2927
2928   gst_object_unref (pad);
2929   return TRUE;
2930 }