validate:pad-monitor: Accept any return value when we aggregated FLUSHING while teari...
[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       && g_strcmp0 (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   GstState state, pending;
1342   GstValidatePadMonitor *othermonitor;
1343   GstFlowReturn aggregated = GST_FLOW_NOT_LINKED;
1344   gboolean found_a_pad = FALSE;
1345   GstPad *pad =
1346       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1347           (monitor)));
1348
1349   iter = gst_pad_iterate_internal_links (pad);
1350   done = FALSE;
1351   while (!done) {
1352     GValue value = { 0, };
1353     switch (gst_iterator_next (iter, &value)) {
1354       case GST_ITERATOR_OK:
1355         otherpad = g_value_get_object (&value);
1356         peerpad = gst_pad_get_peer (otherpad);
1357         if (peerpad) {
1358           othermonitor = _GET_PAD_MONITOR (peerpad);
1359           if (othermonitor) {
1360             found_a_pad = TRUE;
1361             GST_VALIDATE_MONITOR_LOCK (othermonitor);
1362             aggregated =
1363                 _combine_flows (aggregated, othermonitor->last_flow_return);
1364             GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1365           }
1366
1367           gst_object_unref (peerpad);
1368         }
1369         g_value_reset (&value);
1370         break;
1371       case GST_ITERATOR_RESYNC:
1372         gst_iterator_resync (iter);
1373         break;
1374       case GST_ITERATOR_ERROR:
1375         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1376         done = TRUE;
1377         break;
1378       case GST_ITERATOR_DONE:
1379         done = TRUE;
1380         break;
1381     }
1382   }
1383   gst_iterator_free (iter);
1384   if (!found_a_pad) {
1385     /* no peer pad found, nothing to do */
1386     goto done;
1387   }
1388
1389   if (aggregated == GST_FLOW_FLUSHING) {
1390     gst_element_get_state (GST_ELEMENT (parent), &state, &pending, 0);
1391     if (state < GST_STATE_PAUSED || pending < GST_STATE_PAUSED) {
1392       /* Aggregated is flushing, we might have been aggregating a combination
1393        * of pads that are not what was present on the element during the actual
1394        * data flow combination (pads might have been removed meanwhile) */
1395
1396       goto done;
1397     }
1398   } else if (aggregated == GST_FLOW_OK || aggregated == GST_FLOW_EOS) {
1399
1400     /* those are acceptable situations */
1401     if (GST_PAD_IS_FLUSHING (pad) && ret == GST_FLOW_FLUSHING) {
1402       /* pad is flushing, always acceptable to return flushing */
1403       goto done;
1404     }
1405
1406     gst_element_get_state (GST_ELEMENT (parent), &state, &pending, 0);
1407     if (ret == GST_FLOW_FLUSHING && (state < GST_STATE_PAUSED
1408             || pending < GST_STATE_PAUSED)) {
1409       /* Element is being teared down, accept FLOW_FLUSHING */
1410
1411       goto done;
1412     }
1413
1414     if (monitor->is_eos && ret == GST_FLOW_EOS) {
1415       /* this element received eos and returned eos */
1416       goto done;
1417     }
1418
1419     if (PAD_PARENT_IS_DEMUXER (monitor) && ret == GST_FLOW_EOS) {
1420       /* a demuxer can return EOS when the samples end */
1421       goto done;
1422     }
1423   }
1424
1425   if (aggregated != ret) {
1426     GST_VALIDATE_REPORT (monitor, WRONG_FLOW_RETURN,
1427         "Wrong combined flow return %s(%d). Expected: %s(%d)",
1428         gst_flow_get_name (ret), ret, gst_flow_get_name (aggregated),
1429         aggregated);
1430   }
1431
1432 done:
1433   gst_object_unref (pad);
1434 }
1435
1436 static void
1437     gst_validate_pad_monitor_otherpad_add_pending_serialized_event
1438     (GstValidatePadMonitor * monitor, GstEvent * event, GstClockTime last_ts)
1439 {
1440   GstIterator *iter;
1441   gboolean done;
1442   GstPad *otherpad;
1443   GstValidatePadMonitor *othermonitor;
1444   GstPad *pad;
1445
1446
1447   if (!GST_EVENT_IS_SERIALIZED (event))
1448     return;
1449
1450   pad =
1451       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1452           (monitor)));
1453   iter = gst_pad_iterate_internal_links (pad);
1454   if (iter == NULL) {
1455     /* inputselector will return NULL if the sinkpad is not the active one .... */
1456     GST_FIXME_OBJECT (pad, "No iterator");
1457     gst_object_unref (pad);
1458     return;
1459   }
1460
1461   done = FALSE;
1462   while (!done) {
1463     GValue value = { 0, };
1464     switch (gst_iterator_next (iter, &value)) {
1465       case GST_ITERATOR_OK:
1466         otherpad = g_value_get_object (&value);
1467         othermonitor = _GET_PAD_MONITOR (otherpad);
1468         if (othermonitor) {
1469           SerializedEventData *data = g_slice_new0 (SerializedEventData);
1470           data->timestamp = last_ts;
1471           data->event = gst_event_ref (event);
1472           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1473           GST_DEBUG_OBJECT (pad, "Storing for pad %s:%s event %p %s",
1474               GST_DEBUG_PAD_NAME (otherpad), event,
1475               GST_EVENT_TYPE_NAME (event));
1476           g_ptr_array_add (othermonitor->serialized_events, data);
1477           debug_pending_event (otherpad, othermonitor->serialized_events);
1478           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1479         }
1480         g_value_reset (&value);
1481         break;
1482       case GST_ITERATOR_RESYNC:
1483         gst_iterator_resync (iter);
1484         break;
1485       case GST_ITERATOR_ERROR:
1486         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1487         done = TRUE;
1488         break;
1489       case GST_ITERATOR_DONE:
1490         done = TRUE;
1491         break;
1492     }
1493   }
1494   gst_iterator_free (iter);
1495   gst_object_unref (pad);
1496 }
1497
1498 static void
1499 gst_validate_pad_monitor_otherpad_add_pending_field (GstValidatePadMonitor *
1500     monitor, GstStructure * structure, const gchar * field)
1501 {
1502   GstIterator *iter;
1503   gboolean done;
1504   GstPad *otherpad;
1505   GstValidatePadMonitor *othermonitor;
1506   const GValue *v;
1507   GstPad *pad;
1508
1509   v = gst_structure_get_value (structure, field);
1510   pad =
1511       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1512           (monitor)));
1513
1514   if (v == NULL) {
1515     GST_DEBUG_OBJECT (pad, "Not adding pending field %s as it isn't "
1516         "present on structure %" GST_PTR_FORMAT, field, structure);
1517     gst_object_unref (pad);
1518     return;
1519   }
1520
1521   iter = gst_pad_iterate_internal_links (pad);
1522   done = FALSE;
1523   while (!done) {
1524     GValue value = { 0, };
1525     switch (gst_iterator_next (iter, &value)) {
1526       case GST_ITERATOR_OK:
1527         otherpad = g_value_get_object (&value);
1528         othermonitor = _GET_PAD_MONITOR (otherpad);
1529         if (othermonitor) {
1530           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1531           g_assert (othermonitor->pending_setcaps_fields != NULL);
1532           gst_structure_set_value (othermonitor->pending_setcaps_fields,
1533               field, v);
1534           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1535         }
1536         g_value_reset (&value);
1537         break;
1538       case GST_ITERATOR_RESYNC:
1539         gst_iterator_resync (iter);
1540         break;
1541       case GST_ITERATOR_ERROR:
1542         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1543         done = TRUE;
1544         break;
1545       case GST_ITERATOR_DONE:
1546         done = TRUE;
1547         break;
1548     }
1549   }
1550   gst_iterator_free (iter);
1551   gst_object_unref (pad);
1552 }
1553
1554 static void
1555 gst_validate_pad_monitor_otherpad_clear_pending_fields (GstValidatePadMonitor *
1556     monitor)
1557 {
1558   GstIterator *iter;
1559   gboolean done;
1560   GstPad *otherpad;
1561   GstValidatePadMonitor *othermonitor;
1562
1563   GstPad *pad =
1564       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1565           (monitor)));
1566
1567   iter = gst_pad_iterate_internal_links (pad);
1568   if (iter == NULL) {
1569     gst_object_unref (pad);
1570     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1571
1572     return;
1573   }
1574
1575   done = FALSE;
1576   while (!done) {
1577     GValue value = { 0, };
1578     switch (gst_iterator_next (iter, &value)) {
1579       case GST_ITERATOR_OK:
1580         otherpad = g_value_get_object (&value);
1581         othermonitor = _GET_PAD_MONITOR (otherpad);
1582         if (othermonitor) {
1583           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1584           g_assert (othermonitor->pending_setcaps_fields != NULL);
1585           gst_structure_free (othermonitor->pending_setcaps_fields);
1586           othermonitor->pending_setcaps_fields =
1587               gst_structure_new_empty (PENDING_FIELDS);
1588           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1589         }
1590         g_value_reset (&value);
1591         break;
1592       case GST_ITERATOR_RESYNC:
1593         gst_iterator_resync (iter);
1594         break;
1595       case GST_ITERATOR_ERROR:
1596         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1597         done = TRUE;
1598         break;
1599       case GST_ITERATOR_DONE:
1600         done = TRUE;
1601         break;
1602     }
1603   }
1604   gst_object_unref (pad);
1605   gst_iterator_free (iter);
1606 }
1607
1608 static void
1609 gst_validate_pad_monitor_add_expected_newsegment (GstValidatePadMonitor *
1610     monitor, GstEvent * event)
1611 {
1612   GstIterator *iter;
1613   gboolean done;
1614   GstPad *otherpad;
1615   GstValidatePadMonitor *othermonitor;
1616   GstPad *pad =
1617       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1618           (monitor)));
1619
1620   iter = gst_pad_iterate_internal_links (pad);
1621   if (iter == NULL) {
1622     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1623     gst_object_unref (pad);
1624     return;
1625   }
1626
1627   done = FALSE;
1628   while (!done) {
1629     GValue value = { 0, };
1630     switch (gst_iterator_next (iter, &value)) {
1631       case GST_ITERATOR_OK:
1632         otherpad = g_value_get_object (&value);
1633         if (!otherpad)
1634           continue;
1635         othermonitor = _GET_PAD_MONITOR (otherpad);
1636         GST_VALIDATE_MONITOR_LOCK (othermonitor);
1637         gst_event_replace (&othermonitor->expected_segment, event);
1638         GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1639         g_value_reset (&value);
1640         break;
1641       case GST_ITERATOR_RESYNC:
1642         gst_iterator_resync (iter);
1643         break;
1644       case GST_ITERATOR_ERROR:
1645         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1646         done = TRUE;
1647         break;
1648       case GST_ITERATOR_DONE:
1649         done = TRUE;
1650         break;
1651     }
1652   }
1653   gst_iterator_free (iter);
1654   gst_object_unref (pad);
1655 }
1656
1657 /* common checks for both sink and src event functions */
1658 static void
1659 gst_validate_pad_monitor_common_event_check (GstValidatePadMonitor *
1660     pad_monitor, GstEvent * event)
1661 {
1662   guint32 seqnum = gst_event_get_seqnum (event);
1663
1664   if (seqnum == GST_SEQNUM_INVALID)
1665     GST_VALIDATE_REPORT (pad_monitor, EVENT_INVALID_SEQNUM,
1666         "Event %p (%s) has an invalid SEQNUM", event,
1667         GST_EVENT_TYPE_NAME (event));
1668
1669   switch (GST_EVENT_TYPE (event)) {
1670     case GST_EVENT_FLUSH_START:
1671     {
1672       if (pad_monitor->pending_flush_start_seqnum != GST_SEQNUM_INVALID) {
1673         if (seqnum == pad_monitor->pending_flush_start_seqnum) {
1674           pad_monitor->pending_flush_start_seqnum = GST_SEQNUM_INVALID;
1675         } else {
1676           GST_VALIDATE_REPORT (pad_monitor, FLUSH_START_HAS_WRONG_SEQNUM,
1677               "Got: %u Expected: %u", seqnum,
1678               pad_monitor->pending_flush_start_seqnum);
1679         }
1680       }
1681
1682       if (pad_monitor->pending_flush_stop) {
1683         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_START_UNEXPECTED,
1684             "Received flush-start from when flush-stop was expected");
1685       }
1686       pad_monitor->pending_flush_stop = TRUE;
1687     }
1688       break;
1689     case GST_EVENT_FLUSH_STOP:
1690     {
1691       if (pad_monitor->pending_flush_stop_seqnum != GST_SEQNUM_INVALID) {
1692         if (seqnum == pad_monitor->pending_flush_stop_seqnum) {
1693           pad_monitor->pending_flush_stop_seqnum = GST_SEQNUM_INVALID;
1694         } else {
1695           GST_VALIDATE_REPORT (pad_monitor, FLUSH_STOP_HAS_WRONG_SEQNUM,
1696               "Got: %u Expected: %u", seqnum,
1697               pad_monitor->pending_flush_stop_seqnum);
1698         }
1699       }
1700
1701       pad_monitor->pending_newsegment_seqnum = seqnum;
1702       pad_monitor->pending_eos_seqnum = seqnum;
1703
1704       if (!pad_monitor->pending_flush_stop) {
1705         gchar *event_str = _get_event_string (event);
1706
1707         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_STOP_UNEXPECTED,
1708             "Unexpected flush-stop %s", event_str);
1709         g_free (event_str);
1710       }
1711       pad_monitor->pending_flush_stop = FALSE;
1712
1713       /* Buffers following a FLUSH should have the DISCONT flag set */
1714       pad_monitor->pending_buffer_discont = TRUE;
1715
1716       /* cleanup our data */
1717       gst_validate_pad_monitor_flush (pad_monitor);
1718     }
1719       break;
1720     default:
1721       break;
1722   }
1723 }
1724
1725 static void
1726 mark_pads_eos (GstValidatePadMonitor * pad_monitor)
1727 {
1728   GstValidatePadMonitor *peer_monitor;
1729   GstPad *real_peer;
1730   GstPad *pad =
1731       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1732           (pad_monitor)));
1733   GstPad *peer = gst_pad_get_peer (pad);
1734
1735   gst_object_unref (pad);
1736   pad_monitor->is_eos = TRUE;
1737   if (peer) {
1738     real_peer = _get_actual_pad (peer);
1739     peer_monitor = _GET_PAD_MONITOR (real_peer);
1740     if (peer_monitor)
1741       peer_monitor->is_eos = TRUE;
1742     gst_object_unref (peer);
1743     gst_object_unref (real_peer);
1744   }
1745 }
1746
1747 static inline gboolean
1748 _should_check_buffers (GstValidatePadMonitor * pad_monitor,
1749     gboolean force_checks)
1750 {
1751   GstPad *pad =
1752       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1753           (pad_monitor)));
1754   GstValidateMonitor *monitor = GST_VALIDATE_MONITOR (pad_monitor);
1755
1756   if (pad_monitor->first_buffer || force_checks) {
1757     if (pad_monitor->segment.rate != 1.0) {
1758       GST_INFO_OBJECT (pad_monitor, "We do not support buffer checking"
1759           " for trick modes");
1760
1761       pad_monitor->check_buffers = FALSE;
1762     } else if (!PAD_PARENT_IS_DECODER (pad_monitor)) {
1763       GST_DEBUG_OBJECT (pad, "Not on a decoder => no buffer checking");
1764
1765       pad_monitor->check_buffers = FALSE;
1766     } else if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK) {
1767       GST_DEBUG_OBJECT (pad, "Not a sinkpad => no buffer checking");
1768
1769       pad_monitor->check_buffers = FALSE;
1770     } else if (!pad_monitor->caps_is_video) {
1771       GST_DEBUG_OBJECT (pad, "Not working with video => no buffer checking");
1772
1773       pad_monitor->check_buffers = FALSE;
1774     } else if (monitor->media_descriptor == NULL) {
1775       GST_DEBUG_OBJECT (pad, "No media_descriptor set => no buffer checking");
1776
1777       pad_monitor->check_buffers = FALSE;
1778     } else if (!gst_validate_media_descriptor_detects_frames
1779         (monitor->media_descriptor)) {
1780       GST_DEBUG_OBJECT (pad,
1781           "No frame detection media descriptor => no buffer checking");
1782       pad_monitor->check_buffers = FALSE;
1783     } else if (pad_monitor->all_bufs == NULL &&
1784         !gst_validate_media_descriptor_get_buffers (monitor->media_descriptor,
1785             pad, NULL, &pad_monitor->all_bufs)) {
1786
1787       GST_INFO_OBJECT (monitor,
1788           "The MediaInfo is marked as detecting frame, but getting frames"
1789           " from pad %" GST_PTR_FORMAT " did not work (some format conversion"
1790           " might be happening)", pad);
1791
1792       pad_monitor->check_buffers = FALSE;
1793     } else {
1794       if (!pad_monitor->current_buf)
1795         pad_monitor->current_buf = pad_monitor->all_bufs;
1796       pad_monitor->check_buffers = TRUE;
1797     }
1798   }
1799   gst_object_unref (pad);
1800
1801   return pad_monitor->check_buffers;
1802 }
1803
1804 static void
1805 gst_validate_monitor_find_next_buffer (GstValidatePadMonitor * pad_monitor)
1806 {
1807   GList *tmp;
1808   gboolean passed_start = FALSE;
1809
1810   if (!_should_check_buffers (pad_monitor, TRUE))
1811     return;
1812
1813   for (tmp = g_list_last (pad_monitor->all_bufs); tmp; tmp = tmp->prev) {
1814     GstBuffer *cbuf = (GstBuffer *) tmp->data;
1815     GstClockTime ts =
1816         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cbuf)) ? GST_BUFFER_DTS (cbuf)
1817         : GST_BUFFER_PTS (cbuf);
1818
1819     if (!GST_CLOCK_TIME_IS_VALID (ts))
1820       continue;
1821
1822     if (ts <= pad_monitor->segment.start)
1823       passed_start = TRUE;
1824
1825     if (!passed_start)
1826       continue;
1827
1828     if (!GST_BUFFER_FLAG_IS_SET (cbuf, GST_BUFFER_FLAG_DELTA_UNIT)) {
1829       break;
1830     }
1831   }
1832
1833   if (tmp == NULL)
1834     pad_monitor->current_buf = pad_monitor->all_bufs;
1835   else
1836     pad_monitor->current_buf = tmp;
1837 }
1838
1839 /* Checks whether a segment is just an update of another,
1840  * That is to say that only the base and offset field differ and all
1841  * other fields are identical */
1842 static gboolean
1843 is_segment_update (GstSegment * a, const GstSegment * b)
1844 {
1845   /* Note : We never care about the position field, it is only
1846    * used for internal usage by elements */
1847   if (a->rate == b->rate &&
1848       a->applied_rate == b->applied_rate &&
1849       a->format == b->format && a->time == b->time) {
1850     /* Changes in base/offset are considered updates */
1851     /* Updating the end position of a segment is an update */
1852     /* Updating the duration of a segment is an update */
1853     if (a->rate > 0.0) {
1854       if (a->start == b->start)
1855         return TRUE;
1856     } else {
1857       if (a->stop == b->stop)
1858         return TRUE;
1859     }
1860   }
1861   return FALSE;
1862 }
1863
1864 static GstFlowReturn
1865 gst_validate_pad_monitor_downstream_event_check (GstValidatePadMonitor *
1866     pad_monitor, GstObject * parent, GstEvent * event,
1867     GstPadEventFunction handler)
1868 {
1869   GstFlowReturn ret = GST_FLOW_OK;
1870   const GstSegment *segment;
1871   guint32 seqnum = gst_event_get_seqnum (event);
1872   GstPad *pad =
1873       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1874           (pad_monitor)));
1875
1876   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
1877
1878   /* pre checks */
1879   switch (GST_EVENT_TYPE (event)) {
1880     case GST_EVENT_STREAM_START:
1881       /* Buffers following a STREAM_START should have the DISCONT flag set */
1882       pad_monitor->pending_buffer_discont = TRUE;
1883       break;
1884     case GST_EVENT_SEGMENT:
1885       /* parse segment data to be used if event is handled */
1886       gst_event_parse_segment (event, &segment);
1887
1888       GST_DEBUG_OBJECT (pad, "Got segment %" GST_SEGMENT_FORMAT, segment);
1889
1890       /* Reset expected flush start/stop values, we have a segment */
1891       pad_monitor->pending_flush_start_seqnum = GST_SEQNUM_INVALID;
1892       pad_monitor->pending_flush_stop_seqnum = GST_SEQNUM_INVALID;
1893
1894       if (pad_monitor->pending_newsegment_seqnum != GST_SEQNUM_INVALID) {
1895         if (pad_monitor->pending_newsegment_seqnum == seqnum) {
1896           pad_monitor->pending_newsegment_seqnum = GST_SEQNUM_INVALID;
1897           if (GST_CLOCK_TIME_IS_VALID (pad_monitor->pending_seek_accurate_time)) {
1898             if (segment->time == pad_monitor->pending_seek_accurate_time) {
1899               pad_monitor->pending_seek_accurate_time = GST_CLOCK_TIME_NONE;
1900             } else {
1901               GST_VALIDATE_REPORT (pad_monitor, SEGMENT_HAS_WRONG_START,
1902                   "After an accurate seek, got: %" GST_TIME_FORMAT
1903                   " Expected: %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->time),
1904                   GST_TIME_ARGS (pad_monitor->pending_seek_accurate_time));
1905             }
1906           }
1907         } else {
1908           GST_VALIDATE_REPORT (pad_monitor, SEGMENT_HAS_WRONG_SEQNUM,
1909               "Got: %u Expected: %u", seqnum, pad_monitor->pending_eos_seqnum);
1910         }
1911       }
1912
1913       pad_monitor->pending_eos_seqnum = seqnum;
1914
1915       if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
1916         gst_validate_pad_monitor_add_expected_newsegment (pad_monitor, event);
1917       } else {
1918         /* check if this segment is the expected one */
1919         if (pad_monitor->expected_segment) {
1920           const GstSegment *exp_segment;
1921
1922           if (pad_monitor->expected_segment != event) {
1923             gst_event_parse_segment (pad_monitor->expected_segment,
1924                 &exp_segment);
1925             if (segment->format == exp_segment->format) {
1926               if ((exp_segment->rate * exp_segment->applied_rate !=
1927                       segment->rate * segment->applied_rate))
1928                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1929                     "Rate * applied_rate %f != expected %f",
1930                     segment->rate * segment->applied_rate,
1931                     exp_segment->rate * exp_segment->applied_rate);
1932               if (exp_segment->start != segment->start)
1933                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1934                     "Start %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
1935                     GST_TIME_ARGS (segment->start),
1936                     GST_TIME_ARGS (exp_segment->start));
1937               if (exp_segment->stop != segment->stop)
1938                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1939                     "Stop %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
1940                     GST_TIME_ARGS (segment->stop),
1941                     GST_TIME_ARGS (exp_segment->stop));
1942               if (exp_segment->position != segment->position)
1943                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1944                     "Position %" GST_TIME_FORMAT " != expected %"
1945                     GST_TIME_FORMAT, GST_TIME_ARGS (segment->position),
1946                     GST_TIME_ARGS (exp_segment->position));
1947             }
1948           }
1949           gst_event_replace (&pad_monitor->expected_segment, NULL);
1950         }
1951       }
1952       break;
1953     case GST_EVENT_CAPS:{
1954       GstCaps *caps;
1955
1956       gst_event_parse_caps (event, &caps);
1957       gst_validate_pad_monitor_setcaps_pre (pad_monitor, caps);
1958       break;
1959     }
1960     case GST_EVENT_EOS:
1961       pad_monitor->is_eos = TRUE;
1962       /* FIXME : This feels and looks wrong ... */
1963       if (pad_monitor->pending_eos_seqnum == GST_SEQNUM_INVALID) {
1964         GST_VALIDATE_REPORT (pad_monitor, EVENT_EOS_WITHOUT_SEGMENT,
1965             "EOS %" GST_PTR_FORMAT " received before a segment was received",
1966             event);
1967       } else if (pad_monitor->pending_eos_seqnum != seqnum) {
1968         GST_VALIDATE_REPORT (pad_monitor, EOS_HAS_WRONG_SEQNUM,
1969             "Got: %u. Expected: %u", seqnum, pad_monitor->pending_eos_seqnum);
1970       }
1971
1972       /*
1973        * TODO add end of stream checks for
1974        *  - events not pushed
1975        *  - buffer data not pushed
1976        *  - pending events not received
1977        */
1978       break;
1979
1980       /* both flushes are handled by the common event function */
1981     case GST_EVENT_FLUSH_START:
1982     case GST_EVENT_FLUSH_STOP:
1983     case GST_EVENT_TAG:
1984     case GST_EVENT_SINK_MESSAGE:
1985     default:
1986       break;
1987   }
1988
1989   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1990   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1991   gst_validate_pad_monitor_event_overrides (pad_monitor, event);
1992   if (handler) {
1993     gst_event_ref (event);
1994     if (pad_monitor->event_full_func)
1995       ret = pad_monitor->event_full_func (pad, parent, event);
1996     else if (pad_monitor->event_func (pad, parent, event))
1997       ret = GST_FLOW_OK;
1998     else
1999       ret = GST_FLOW_ERROR;
2000   }
2001   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2002   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2003
2004   /* post checks */
2005   switch (GST_EVENT_TYPE (event)) {
2006     case GST_EVENT_SEGMENT:
2007       if (ret == GST_FLOW_OK) {
2008         /* If the new segment is not an update of the previous one, then
2009          * the following buffer should have the DISCONT flag set */
2010         if (!is_segment_update (&pad_monitor->segment, segment))
2011           pad_monitor->pending_buffer_discont = TRUE;
2012         if (!pad_monitor->has_segment
2013             && pad_monitor->segment.format != segment->format) {
2014           gst_segment_init (&pad_monitor->segment, segment->format);
2015         }
2016         gst_segment_copy_into (segment, &pad_monitor->segment);
2017         pad_monitor->has_segment = TRUE;
2018         gst_validate_monitor_find_next_buffer (pad_monitor);
2019       }
2020       break;
2021     case GST_EVENT_CAPS:{
2022       GstCaps *caps;
2023
2024       gst_event_parse_caps (event, &caps);
2025       gst_validate_pad_monitor_setcaps_post (pad_monitor, caps,
2026           ret == GST_FLOW_OK);
2027       break;
2028     }
2029     case GST_EVENT_FLUSH_START:
2030     case GST_EVENT_FLUSH_STOP:
2031     case GST_EVENT_EOS:
2032     case GST_EVENT_TAG:
2033     case GST_EVENT_SINK_MESSAGE:
2034     default:
2035       break;
2036   }
2037
2038   if (handler)
2039     gst_event_unref (event);
2040   gst_object_unref (pad);
2041   return ret;
2042 }
2043
2044 static gboolean
2045 gst_validate_pad_monitor_src_event_check (GstValidatePadMonitor * pad_monitor,
2046     GstObject * parent, GstEvent * event, GstPadEventFunction handler)
2047 {
2048   gboolean ret = TRUE;
2049   gdouble rate;
2050   GstFormat format;
2051   gint64 start, stop;
2052   GstSeekFlags seek_flags;
2053   GstSeekType start_type, stop_type;
2054   guint32 seqnum = gst_event_get_seqnum (event);
2055   GstPad *pad =
2056       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2057           (pad_monitor)));
2058
2059   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
2060
2061   /* pre checks */
2062   switch (GST_EVENT_TYPE (event)) {
2063     case GST_EVENT_SEEK:
2064     {
2065       gst_event_parse_seek (event, &rate, &format, &seek_flags, &start_type,
2066           &start, &stop_type, &stop);
2067       /* upstream seek - store the seek event seqnum to check
2068        * flushes and newsegments share the same */
2069     }
2070       break;
2071       /* both flushes are handled by the common event handling function */
2072     case GST_EVENT_FLUSH_START:
2073     case GST_EVENT_FLUSH_STOP:
2074     case GST_EVENT_NAVIGATION:
2075     case GST_EVENT_LATENCY:
2076     case GST_EVENT_STEP:
2077     case GST_EVENT_QOS:
2078     default:
2079       break;
2080   }
2081
2082   if (handler) {
2083     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2084     /* Safely store pending accurate seek values */
2085     if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
2086       if (seek_flags & GST_SEEK_FLAG_ACCURATE) {
2087         GST_DEBUG_OBJECT (pad,
2088             "Storing expected accurate seek time %" GST_TIME_FORMAT,
2089             GST_TIME_ARGS (start));
2090         pad_monitor->pending_seek_accurate_time = start;
2091       }
2092       /* TODO we might need to use a list as multiple seeks can be sent
2093        * before the flushes arrive here */
2094       if (seek_flags & GST_SEEK_FLAG_FLUSH) {
2095         pad_monitor->pending_flush_start_seqnum = seqnum;
2096         pad_monitor->pending_flush_stop_seqnum = seqnum;
2097       }
2098     }
2099
2100     gst_event_ref (event);
2101     ret = pad_monitor->event_func (pad, parent, event);
2102
2103     if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
2104       /* If the seek was already handled (same current seqnum), reset the
2105        * expected accurate seek value */
2106       if (ret && pad_monitor->has_segment
2107           && seqnum == pad_monitor->pending_eos_seqnum) {
2108         GST_DEBUG_OBJECT (pad,
2109             "Resetting expected accurate seek value, was already handled");
2110         pad_monitor->pending_seek_accurate_time = GST_CLOCK_TIME_NONE;
2111       } else if (!ret) {
2112         /* do not expect any of these events anymore */
2113         pad_monitor->pending_flush_start_seqnum = GST_SEQNUM_INVALID;
2114         pad_monitor->pending_flush_stop_seqnum = GST_SEQNUM_INVALID;
2115         pad_monitor->pending_newsegment_seqnum = GST_SEQNUM_INVALID;
2116         pad_monitor->pending_eos_seqnum = GST_SEQNUM_INVALID;
2117         pad_monitor->pending_seek_accurate_time = GST_CLOCK_TIME_NONE;
2118       }
2119     }
2120     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2121   }
2122
2123   /* post checks */
2124   switch (GST_EVENT_TYPE (event)) {
2125     case GST_EVENT_FLUSH_START:
2126     case GST_EVENT_FLUSH_STOP:
2127     case GST_EVENT_QOS:
2128     case GST_EVENT_SEEK:
2129     case GST_EVENT_NAVIGATION:
2130     case GST_EVENT_LATENCY:
2131     case GST_EVENT_STEP:
2132     default:
2133       break;
2134   }
2135
2136   if (handler)
2137     gst_event_unref (event);
2138   gst_object_unref (pad);
2139   return ret;
2140 }
2141
2142 static gboolean
2143 gst_validate_pad_monitor_check_right_buffer (GstValidatePadMonitor *
2144     pad_monitor, GstBuffer * buffer)
2145 {
2146   gchar *checksum;
2147   GstBuffer *wanted_buf;
2148   GstMapInfo map, wanted_map;
2149
2150   gboolean ret = TRUE;
2151   GstPad *pad;
2152
2153
2154   if (_should_check_buffers (pad_monitor, FALSE) == FALSE)
2155     return FALSE;
2156
2157   pad =
2158       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2159           (pad_monitor)));
2160   if (pad_monitor->current_buf == NULL) {
2161     GST_INFO_OBJECT (pad, "No current buffer one pad, Why?");
2162     gst_object_unref (pad);
2163     return FALSE;
2164   }
2165
2166   wanted_buf = pad_monitor->current_buf->data;
2167
2168   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (wanted_buf)) &&
2169       GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buffer)) &&
2170       GST_BUFFER_PTS (wanted_buf) != GST_BUFFER_PTS (buffer)) {
2171
2172     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2173         "buffer %" GST_PTR_FORMAT " PTS %" GST_TIME_FORMAT
2174         " different than expected: %" GST_TIME_FORMAT, buffer,
2175         GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2176         GST_TIME_ARGS (GST_BUFFER_PTS (wanted_buf)));
2177
2178     ret = FALSE;
2179   }
2180
2181   if (GST_BUFFER_DTS (wanted_buf) != GST_BUFFER_DTS (buffer)) {
2182     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2183         "buffer %" GST_PTR_FORMAT " DTS %" GST_TIME_FORMAT
2184         " different than expected: %" GST_TIME_FORMAT, buffer,
2185         GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
2186         GST_TIME_ARGS (GST_BUFFER_DTS (wanted_buf)));
2187     ret = FALSE;
2188   }
2189
2190   if (GST_BUFFER_DURATION (wanted_buf) != GST_BUFFER_DURATION (buffer)) {
2191     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2192         "buffer %" GST_PTR_FORMAT " DURATION %" GST_TIME_FORMAT
2193         " different than expected: %" GST_TIME_FORMAT, buffer,
2194         GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
2195         GST_TIME_ARGS (GST_BUFFER_DURATION (wanted_buf)));
2196     ret = FALSE;
2197   }
2198
2199   if (GST_BUFFER_FLAG_IS_SET (wanted_buf, GST_BUFFER_FLAG_DELTA_UNIT) !=
2200       GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
2201     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2202         "buffer %" GST_PTR_FORMAT "  Delta unit is set to %s but expected %s",
2203         buffer, GST_BUFFER_FLAG_IS_SET (buffer,
2204             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False",
2205         GST_BUFFER_FLAG_IS_SET (wanted_buf,
2206             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False");
2207     ret = FALSE;
2208   }
2209
2210   g_assert (gst_buffer_map (wanted_buf, &wanted_map, GST_MAP_READ));
2211   g_assert (gst_buffer_map (buffer, &map, GST_MAP_READ));
2212
2213   checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5,
2214       (const guchar *) map.data, map.size);
2215
2216   if (g_strcmp0 ((gchar *) wanted_map.data, checksum)) {
2217     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2218         "buffer %" GST_PTR_FORMAT " checksum %s different from expected: %s",
2219         buffer, checksum, wanted_map.data);
2220     ret = FALSE;
2221   }
2222
2223   gst_buffer_unmap (wanted_buf, &wanted_map);
2224   gst_buffer_unmap (buffer, &map);
2225   g_free (checksum);
2226   gst_object_unref (pad);
2227
2228   pad_monitor->current_buf = pad_monitor->current_buf->next;
2229
2230   return ret;
2231 }
2232
2233 static void
2234 gst_validate_pad_monitor_check_return (GstValidatePadMonitor * pad_monitor,
2235     GstFlowReturn ret)
2236 {
2237   GstValidateMonitor *parent = GST_VALIDATE_MONITOR (pad_monitor);
2238
2239   if (ret != GST_FLOW_ERROR)
2240     return;
2241
2242   while (GST_VALIDATE_MONITOR_GET_PARENT (parent))
2243     parent = GST_VALIDATE_MONITOR_GET_PARENT (parent);
2244
2245   if (GST_IS_VALIDATE_PIPELINE_MONITOR (parent)) {
2246     GstValidatePipelineMonitor *m = GST_VALIDATE_PIPELINE_MONITOR (parent);
2247
2248     GST_VALIDATE_MONITOR_LOCK (m);
2249     if (m->got_error == FALSE) {
2250       GST_VALIDATE_REPORT (pad_monitor, FLOW_ERROR_WITHOUT_ERROR_MESSAGE,
2251           "Pad return GST_FLOW_ERROR but no GST_MESSAGE_ERROR was received on"
2252           " the bus");
2253
2254       /* Only report it the first time */
2255       m->got_error = TRUE;
2256     }
2257     GST_VALIDATE_MONITOR_UNLOCK (m);
2258   }
2259 }
2260
2261 static GstFlowReturn
2262 gst_validate_pad_monitor_chain_func (GstPad * pad, GstObject * parent,
2263     GstBuffer * buffer)
2264 {
2265   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2266   GstFlowReturn ret;
2267
2268   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2269   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2270
2271   gst_validate_pad_monitor_check_discont (pad_monitor, buffer);
2272   gst_validate_pad_monitor_check_right_buffer (pad_monitor, buffer);
2273   gst_validate_pad_monitor_check_first_buffer (pad_monitor, buffer);
2274   gst_validate_pad_monitor_update_buffer_data (pad_monitor, buffer);
2275   gst_validate_pad_monitor_check_eos (pad_monitor, buffer);
2276
2277   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2278   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2279
2280   gst_validate_pad_monitor_buffer_overrides (pad_monitor, buffer);
2281
2282   ret = pad_monitor->chain_func (pad, parent, buffer);
2283
2284   gst_validate_pad_monitor_check_return (pad_monitor, ret);
2285
2286   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2287   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2288
2289   pad_monitor->last_flow_return = ret;
2290   if (ret == GST_FLOW_EOS) {
2291     mark_pads_eos (pad_monitor);
2292   }
2293   if (PAD_PARENT_IS_DEMUXER (pad_monitor))
2294     gst_validate_pad_monitor_check_aggregated_return (pad_monitor, parent, ret);
2295
2296   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2297   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2298
2299   return ret;
2300 }
2301
2302 static gboolean
2303 gst_validate_pad_monitor_event_is_tracked (GstValidatePadMonitor * monitor,
2304     GstEvent * event)
2305 {
2306   if (!GST_EVENT_IS_SERIALIZED (event)) {
2307     return FALSE;
2308   }
2309
2310   /* we don't track Tag events because they mutate too much and it is hard
2311    * to match a tag event pushed on a source pad with the one that was received
2312    * on a sink pad.
2313    * One idea would be to use seqnum, but it seems that it is undefined whether
2314    * seqnums should be maintained in tag events that are created from others
2315    * up to today. (2013-08-29)
2316    */
2317   if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
2318     return FALSE;
2319
2320   return TRUE;
2321 }
2322
2323 static GstFlowReturn
2324 gst_validate_pad_monitor_sink_event_full_func (GstPad * pad, GstObject * parent,
2325     GstEvent * event)
2326 {
2327   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2328   GstFlowReturn ret;
2329
2330   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2331   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2332
2333   GST_DEBUG_OBJECT (pad, "event %p %s", event, GST_EVENT_TYPE_NAME (event));
2334
2335   if (gst_validate_pad_monitor_event_is_tracked (pad_monitor, event)) {
2336     GstClockTime last_ts = GST_CLOCK_TIME_NONE;
2337     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_timestamp)) {
2338       last_ts = pad_monitor->current_timestamp;
2339       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_duration)) {
2340         last_ts += pad_monitor->current_duration;
2341       }
2342     }
2343     gst_validate_pad_monitor_otherpad_add_pending_serialized_event (pad_monitor,
2344         event, last_ts);
2345   }
2346
2347   ret =
2348       gst_validate_pad_monitor_downstream_event_check (pad_monitor, parent,
2349       event, pad_monitor->event_func);
2350
2351   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2352   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2353   return ret;
2354 }
2355
2356 static gboolean
2357 gst_validate_pad_monitor_sink_event_func (GstPad * pad, GstObject * parent,
2358     GstEvent * event)
2359 {
2360   if (gst_validate_pad_monitor_sink_event_full_func (pad, parent,
2361           event) == GST_FLOW_OK)
2362     return TRUE;
2363   return FALSE;
2364 }
2365
2366 static gboolean
2367 gst_validate_pad_monitor_src_event_func (GstPad * pad, GstObject * parent,
2368     GstEvent * event)
2369 {
2370   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2371   gboolean ret;
2372
2373   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2374   ret = gst_validate_pad_monitor_src_event_check (pad_monitor, parent, event,
2375       pad_monitor->event_func);
2376   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2377   return ret;
2378 }
2379
2380 static gboolean
2381 gst_validate_pad_monitor_query_func (GstPad * pad, GstObject * parent,
2382     GstQuery * query)
2383 {
2384   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2385   gboolean ret;
2386
2387   gst_validate_pad_monitor_query_overrides (pad_monitor, query);
2388   ret = pad_monitor->query_func (pad, parent, query);
2389
2390   if (ret) {
2391     switch (GST_QUERY_TYPE (query)) {
2392       case GST_QUERY_ACCEPT_CAPS:
2393       {
2394         gboolean result;
2395
2396         gst_caps_replace (&pad_monitor->last_refused_caps, NULL);
2397         gst_query_parse_accept_caps_result (query, &result);
2398         if (!result) {
2399           GstCaps *refused_caps;
2400
2401           gst_query_parse_accept_caps (query, &refused_caps);
2402           pad_monitor->last_refused_caps = gst_caps_copy (refused_caps);
2403
2404         }
2405
2406         break;
2407       }
2408       case GST_QUERY_CAPS:{
2409         GstCaps *res;
2410         GstCaps *filter;
2411
2412         /* We shouldn't need to lock the parent as this doesn't modify
2413          * other monitors, just does some peer_pad_caps */
2414         GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2415
2416         gst_query_parse_caps (query, &filter);
2417         gst_query_parse_caps_result (query, &res);
2418
2419         gst_caps_replace (&pad_monitor->last_query_res, NULL);
2420         gst_caps_replace (&pad_monitor->last_query_filter, NULL);
2421         pad_monitor->last_query_res =
2422             res ? gst_caps_copy (res) : gst_caps_ref (GST_CAPS_NONE);
2423         pad_monitor->last_query_filter =
2424             filter ? gst_caps_copy (filter) : gst_caps_ref (GST_CAPS_NONE);
2425
2426         if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2427           gst_validate_pad_monitor_check_caps_fields_proxied (pad_monitor, res,
2428               filter);
2429         }
2430         GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2431         break;
2432       }
2433       default:
2434         break;
2435     }
2436   }
2437
2438   return ret;
2439 }
2440
2441 static gboolean
2442 gst_validate_pad_monitor_activatemode_func (GstPad * pad, GstObject * parent,
2443     GstPadMode mode, gboolean active)
2444 {
2445   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2446   gboolean ret = TRUE;
2447
2448   /* TODO add overrides for activate func */
2449   GST_DEBUG_OBJECT (pad, "active:%d", active);
2450
2451   if (pad_monitor->activatemode_func)
2452     ret = pad_monitor->activatemode_func (pad, parent, mode, active);
2453   if (ret && active == FALSE) {
2454     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2455     gst_validate_pad_monitor_reset (pad_monitor);
2456     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2457   }
2458
2459   return ret;
2460 }
2461
2462 /* The interval between two buffer frequency checks */
2463 #define BUF_FREQ_CHECK_INTERVAL (GST_SECOND)
2464
2465 static void
2466 gst_validate_pad_monitor_check_buffer_freq (GstValidatePadMonitor * monitor,
2467     GstPad * pad)
2468 {
2469   GstClockTime ts;
2470
2471   if (!GST_PAD_IS_SRC (pad))
2472     return;
2473
2474   if (!monitor->min_buf_freq)
2475     return;
2476
2477   ts = gst_util_get_timestamp ();
2478   monitor->buffers_pushed++;
2479
2480   /* Same logic as in fpsdisplaysink to compute the buffer frequency */
2481   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID
2482           (monitor->min_buf_freq_first_buffer_ts))) {
2483     monitor->min_buf_freq_first_buffer_ts = ts;
2484     monitor->min_buf_freq_interval_ts = ts;
2485     return;
2486   }
2487
2488   if (GST_CLOCK_DIFF (monitor->min_buf_freq_interval_ts,
2489           ts) > BUF_FREQ_CHECK_INTERVAL) {
2490     guint time_diff;
2491     gdouble fps;
2492
2493     time_diff = (gdouble) (ts - monitor->min_buf_freq_interval_ts) / GST_SECOND;
2494     fps =
2495         (gdouble) (monitor->buffers_pushed -
2496         monitor->last_buffers_pushed) / time_diff;
2497
2498     if (fps < monitor->min_buf_freq) {
2499       if (GST_CLOCK_TIME_IS_VALID (monitor->min_buf_freq_start) &&
2500           GST_CLOCK_DIFF (monitor->min_buf_freq_first_buffer_ts,
2501               ts) < monitor->min_buf_freq_start) {
2502         GST_DEBUG_OBJECT (pad,
2503             "buffer frequency is too low (%.2f) but ignore for now (buffer-frequency-start =%"
2504             GST_TIME_FORMAT ")", fps,
2505             GST_TIME_ARGS (monitor->min_buf_freq_start));
2506       } else {
2507         GST_VALIDATE_REPORT (monitor, CONFIG_BUFFER_FREQUENCY_TOO_LOW,
2508             "Buffers are not pushed fast enough on this pad: %.2f/sec (minimum: %.2f)",
2509             fps, monitor->min_buf_freq);
2510       }
2511     }
2512
2513     monitor->last_buffers_pushed = monitor->buffers_pushed;
2514     monitor->min_buf_freq_interval_ts = ts;
2515   }
2516 }
2517
2518 static gboolean
2519 gst_validate_pad_monitor_buffer_probe (GstPad * pad, GstBuffer * buffer,
2520     gpointer udata, gboolean pull_mode)
2521 {
2522   GstValidatePadMonitor *monitor = udata;
2523
2524   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2525   GST_VALIDATE_MONITOR_LOCK (monitor);
2526
2527   if (!pull_mode)
2528     gst_validate_pad_monitor_check_discont (monitor, buffer);
2529   gst_validate_pad_monitor_check_first_buffer (monitor, buffer);
2530   gst_validate_pad_monitor_update_buffer_data (monitor, buffer);
2531   gst_validate_pad_monitor_check_eos (monitor, buffer);
2532
2533   if (PAD_PARENT_IS_DECODER (monitor) || PAD_PARENT_IS_ENCODER (monitor)) {
2534     GstClockTime tolerance = 0;
2535
2536     if (monitor->caps_is_audio)
2537       tolerance = AUDIO_TIMESTAMP_TOLERANCE;
2538
2539     gst_validate_pad_monitor_check_buffer_timestamp_in_received_range (monitor,
2540         buffer, tolerance);
2541   }
2542
2543   gst_validate_pad_monitor_check_late_serialized_events (monitor,
2544       GST_BUFFER_TIMESTAMP (buffer));
2545
2546   /* a GstValidatePadMonitor parent must be a GstValidateElementMonitor */
2547   if (PAD_PARENT_IS_DECODER (monitor)) {
2548
2549     /* should not push out of segment data */
2550     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer)) &&
2551         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer)) &&
2552         ((!gst_segment_clip (&monitor->segment, monitor->segment.format,
2553                     GST_BUFFER_TIMESTAMP (buffer),
2554                     GST_BUFFER_TIMESTAMP (buffer) +
2555                     GST_BUFFER_DURATION (buffer), NULL, NULL)) ||
2556             /* In the case of raw data, buffers should be strictly contained inside the
2557              * segment */
2558             (monitor->caps_is_raw &&
2559                 GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer) <
2560                 monitor->segment.start))
2561         ) {
2562       /* TODO is this a timestamp issue? */
2563       GST_VALIDATE_REPORT (monitor, BUFFER_IS_OUT_OF_SEGMENT,
2564           "buffer is out of segment and shouldn't be pushed. Timestamp: %"
2565           GST_TIME_FORMAT " - Duration: %" GST_TIME_FORMAT ". Range: %"
2566           GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
2567           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2568           GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
2569           GST_TIME_ARGS (monitor->segment.start),
2570           GST_TIME_ARGS (monitor->segment.stop));
2571     }
2572   }
2573
2574   gst_validate_pad_monitor_check_buffer_freq (monitor, pad);
2575
2576   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2577   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2578   gst_validate_pad_monitor_buffer_probe_overrides (monitor, buffer);
2579   return TRUE;
2580 }
2581
2582 static void
2583 gst_validate_pad_monitor_event_probe (GstPad * pad, GstEvent * event,
2584     gpointer udata)
2585 {
2586   GstValidatePadMonitor *monitor = GST_VALIDATE_PAD_MONITOR_CAST (udata);
2587
2588   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2589   GST_VALIDATE_MONITOR_LOCK (monitor);
2590
2591   GST_DEBUG_OBJECT (pad, "event %p %s", event, GST_EVENT_TYPE_NAME (event));
2592
2593   if (GST_EVENT_IS_SERIALIZED (event)) {
2594     gint i;
2595
2596     /* Detect if events the element received are being forwarded in the same order
2597      *
2598      * Several scenarios:
2599      * 1) The element pushes the event as-is
2600      * 2) The element consumes the event and does not forward it
2601      * 3) The element consumes the event and creates another one instead
2602      * 4) The element pushes other serialized event before pushing out the
2603      *    one it received
2604      *
2605      * For each pad we have two lists to track serialized events:
2606      *  1) We received on input and expect to see (serialized_events)
2607      *  2) We received on input but don't expect to see (expired_events)
2608      *
2609      * To detect events that are pushed in a different order from the one they were
2610      * received in we check that:
2611      *
2612      * For each event being outputted:
2613      *   If it is in the expired_events list:
2614      *     RAISE WARNING
2615      *   If it is in the serialized_events list:
2616      *     If there are other events that were received before:
2617      *        Put those events on the expired_events list
2618      *     Remove that event and any previous ones from the serialized_events list
2619      *
2620      * Clear expired events list when flushing or on pad deactivation
2621      *
2622      */
2623
2624     if (g_list_find (monitor->expired_events, event)) {
2625       gchar *event_str = _get_event_string (event);
2626       /* If it's the expired events, we've failed */
2627       GST_WARNING_OBJECT (pad, "Did not expect event %p %s", event,
2628           GST_EVENT_TYPE_NAME (event));
2629       GST_VALIDATE_REPORT (monitor, EVENT_SERIALIZED_OUT_OF_ORDER,
2630           "Serialized event was pushed out of order: %s", event_str);
2631
2632       g_free (event_str);
2633       monitor->expired_events = g_list_remove (monitor->expired_events, event);
2634       gst_event_unref (event);  /* remove the ref that was on the list */
2635     } else if (monitor->serialized_events->len) {
2636       for (i = 0; i < monitor->serialized_events->len; i++) {
2637         SerializedEventData *next_event =
2638             g_ptr_array_index (monitor->serialized_events, i);
2639         GST_DEBUG_OBJECT (pad, "Checking against stored event #%d: %p %s", i,
2640             next_event->event, GST_EVENT_TYPE_NAME (next_event->event));
2641
2642         if (event == next_event->event
2643             || GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2644           /* We have found our event */
2645           GST_DEBUG_OBJECT (pad, "Found matching event");
2646
2647           while (monitor->serialized_events->len > i
2648               && GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2649             /* Swallow all expected events of the same type */
2650             g_ptr_array_remove_index (monitor->serialized_events, i);
2651             next_event = g_ptr_array_index (monitor->serialized_events, i);
2652           }
2653
2654           /* Move all previous events to expired events */
2655           if (G_UNLIKELY (i > 0)) {
2656             GST_DEBUG_OBJECT (pad,
2657                 "Moving previous expected events to expired list");
2658             while (i--) {
2659               next_event = g_ptr_array_index (monitor->serialized_events, 0);
2660               monitor->expired_events =
2661                   g_list_append (monitor->expired_events,
2662                   gst_event_ref (next_event->event));
2663               g_ptr_array_remove_index (monitor->serialized_events, 0);
2664             }
2665           }
2666           debug_pending_event (pad, monitor->serialized_events);
2667           break;
2668         }
2669       }
2670     }
2671   }
2672
2673   /* This so far is just like an event that is flowing downstream,
2674    * so we do the same checks as a sinkpad event handler */
2675   gst_validate_pad_monitor_downstream_event_check (monitor, NULL, event, NULL);
2676   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2677   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2678 }
2679
2680 static GstPadProbeReturn
2681 gst_validate_pad_monitor_pad_probe (GstPad * pad, GstPadProbeInfo * info,
2682     gpointer udata)
2683 {
2684   if (info->type & GST_PAD_PROBE_TYPE_BUFFER)
2685     gst_validate_pad_monitor_buffer_probe (pad, info->data, udata,
2686         GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_PULL);
2687   else if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM)
2688     gst_validate_pad_monitor_event_probe (pad, info->data, udata);
2689
2690   return GST_PAD_PROBE_OK;
2691 }
2692
2693 static void
2694 gst_validate_pad_monitor_update_caps_info (GstValidatePadMonitor * pad_monitor,
2695     GstCaps * caps)
2696 {
2697   GstStructure *structure;
2698
2699   g_return_if_fail (gst_caps_is_fixed (caps));
2700
2701   pad_monitor->caps_is_audio = FALSE;
2702   pad_monitor->caps_is_video = FALSE;
2703
2704   structure = gst_caps_get_structure (caps, 0);
2705   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/")) {
2706     pad_monitor->caps_is_audio = TRUE;
2707   } else if (g_str_has_prefix (gst_structure_get_name (structure), "video/")) {
2708     pad_monitor->caps_is_video = TRUE;
2709   }
2710
2711   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/x-raw") ||
2712       g_str_has_prefix (gst_structure_get_name (structure), "video/x-raw")) {
2713     pad_monitor->caps_is_raw = TRUE;
2714   } else {
2715     pad_monitor->caps_is_raw = FALSE;
2716   }
2717 }
2718
2719 static void
2720 gst_validate_pad_monitor_setcaps_pre (GstValidatePadMonitor * pad_monitor,
2721     GstCaps * caps)
2722 {
2723   GstStructure *structure;
2724   GstPad *pad =
2725       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2726           (pad_monitor)));
2727
2728   /* Check if caps are identical to last caps and complain if so
2729    * Only checked for sink pads as src pads might push the same caps
2730    * multiple times during unlinked/autoplugging scenarios */
2731   if (GST_PAD_IS_SINK (pad) && pad_monitor->last_caps
2732       && gst_caps_is_equal (caps, pad_monitor->last_caps)) {
2733     gchar *caps_str = gst_caps_to_string (caps);
2734
2735     GST_VALIDATE_REPORT (pad_monitor, EVENT_CAPS_DUPLICATE, "%s", caps_str);
2736     g_free (caps_str);
2737
2738   }
2739
2740   gst_validate_pad_monitor_check_caps_complete (pad_monitor, caps);
2741
2742   if (caps) {
2743     structure = gst_caps_get_structure (caps, 0);
2744     if (gst_structure_n_fields (pad_monitor->pending_setcaps_fields)) {
2745       gint i;
2746       for (i = 0;
2747           i < gst_structure_n_fields (pad_monitor->pending_setcaps_fields);
2748           i++) {
2749         const gchar *name =
2750             gst_structure_nth_field_name (pad_monitor->pending_setcaps_fields,
2751             i);
2752         const GValue *v = gst_structure_get_value (structure, name);
2753         const GValue *otherv =
2754             gst_structure_get_value (pad_monitor->pending_setcaps_fields, name);
2755
2756         if (v == NULL) {
2757           gchar *caps_str = gst_caps_to_string (caps);
2758
2759           GST_VALIDATE_REPORT (pad_monitor, CAPS_EXPECTED_FIELD_NOT_FOUND,
2760               "Field %s is missing from setcaps caps '%s'", name, caps_str);
2761           g_free (caps_str);
2762         } else if (gst_value_compare (v, otherv) != GST_VALUE_EQUAL) {
2763           gchar *caps_str = gst_caps_to_string (caps),
2764               *pending_setcaps_fields_str =
2765               gst_structure_to_string (pad_monitor->pending_setcaps_fields);
2766
2767
2768           GST_VALIDATE_REPORT (pad_monitor, CAPS_FIELD_UNEXPECTED_VALUE,
2769               "Field %s from setcaps caps '%s' is different "
2770               "from expected value in caps '%s'", name, caps_str,
2771               pending_setcaps_fields_str);
2772
2773           g_free (pending_setcaps_fields_str);
2774           g_free (caps_str);
2775         }
2776       }
2777     }
2778
2779     if (GST_PAD_IS_SINK (pad) &&
2780         gst_validate_pad_monitor_pad_should_proxy_othercaps (pad_monitor)) {
2781       if (_structure_is_video (structure)) {
2782         GST_DEBUG_OBJECT (pad,
2783             "Adding video common pending fields to other pad: %" GST_PTR_FORMAT,
2784             structure);
2785         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2786             structure, "width");
2787         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2788             structure, "height");
2789         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2790             structure, "framerate");
2791         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2792             structure, "pixel-aspect-ratio");
2793       } else if (_structure_is_audio (structure)) {
2794         GST_DEBUG_OBJECT (pad,
2795             "Adding audio common pending fields to other pad: %" GST_PTR_FORMAT,
2796             structure);
2797         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2798             structure, "rate");
2799         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2800             structure, "channels");
2801       }
2802     }
2803   }
2804
2805   gst_structure_free (pad_monitor->pending_setcaps_fields);
2806   pad_monitor->pending_setcaps_fields =
2807       gst_structure_new_empty (PENDING_FIELDS);
2808   gst_object_unref (pad);
2809
2810   gst_validate_pad_monitor_setcaps_overrides (pad_monitor, caps);
2811 }
2812
2813 static void
2814 gst_validate_pad_monitor_setcaps_post (GstValidatePadMonitor * pad_monitor,
2815     GstCaps * caps, gboolean ret)
2816 {
2817   if (!ret)
2818     gst_validate_pad_monitor_otherpad_clear_pending_fields (pad_monitor);
2819   else {
2820     if (pad_monitor->last_caps) {
2821       gst_caps_unref (pad_monitor->last_caps);
2822     }
2823     pad_monitor->last_caps = gst_caps_ref (caps);
2824     gst_validate_pad_monitor_update_caps_info (pad_monitor, caps);
2825   }
2826 }
2827
2828 static void
2829 gst_validate_pad_monitor_get_min_buffer_frequency (GstValidatePadMonitor *
2830     monitor, GstPad * pad)
2831 {
2832   GList *config, *l;
2833
2834   if (!GST_PAD_IS_SRC (pad))
2835     return;
2836
2837   config = gst_validate_plugin_get_config (NULL);
2838   for (l = config; l != NULL; l = g_list_next (l)) {
2839     GstStructure *s = l->data;
2840     gdouble min_buf_freq;
2841     const gchar *pad_name;
2842     GstElement *element = NULL;
2843
2844     if (!gst_structure_get_double (s, "min-buffer-frequency", &min_buf_freq)) {
2845       gint max_int;
2846
2847       if (!gst_structure_get_int (s, "min-buffer-frequency", &max_int))
2848         goto next;
2849
2850       min_buf_freq = max_int;
2851     }
2852
2853     pad_name = gst_structure_get_string (s, "name");
2854     if (!pad_name)
2855       pad_name = "src";
2856
2857     if (g_strcmp0 (GST_PAD_NAME (pad), pad_name))
2858       goto next;
2859
2860     element = gst_pad_get_parent_element (pad);
2861
2862     if (!gst_validate_element_matches_target (element, s))
2863       goto next;
2864
2865     monitor->min_buf_freq = min_buf_freq;
2866
2867     gst_validate_utils_get_clocktime (s, "buffer-frequency-start",
2868         &monitor->min_buf_freq_start);
2869
2870     GST_DEBUG_OBJECT (pad, "pad has a minimum buffer frequency of %f",
2871         min_buf_freq);
2872   next:
2873     g_clear_object (&element);
2874   }
2875 }
2876
2877 static gboolean
2878 gst_validate_pad_monitor_do_setup (GstValidateMonitor * monitor)
2879 {
2880   GstValidatePadMonitor *pad_monitor = GST_VALIDATE_PAD_MONITOR_CAST (monitor);
2881   GstPad *pad = (gpointer) gst_validate_monitor_get_target (monitor);
2882
2883   if (!GST_IS_PAD (pad)) {
2884     GST_WARNING_OBJECT (monitor, "Trying to create pad monitor with other "
2885         "type of object");
2886     gst_object_unref (pad);
2887     return FALSE;
2888   }
2889
2890   if (_GET_PAD_MONITOR (pad)) {
2891     GST_WARNING_OBJECT (pad_monitor,
2892         "Pad already has a validate-monitor associated");
2893     gst_object_unref (pad);
2894     return FALSE;
2895   }
2896
2897   _SET_PAD_MONITOR (pad, pad_monitor);
2898
2899   pad_monitor->event_func = GST_PAD_EVENTFUNC (pad);
2900   pad_monitor->event_full_func = GST_PAD_EVENTFULLFUNC (pad);
2901   pad_monitor->query_func = GST_PAD_QUERYFUNC (pad);
2902   pad_monitor->activatemode_func = GST_PAD_ACTIVATEMODEFUNC (pad);
2903   if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2904
2905     pad_monitor->chain_func = GST_PAD_CHAINFUNC (pad);
2906     if (pad_monitor->chain_func)
2907       gst_pad_set_chain_function (pad, gst_validate_pad_monitor_chain_func);
2908
2909     if (pad_monitor->event_full_func)
2910       gst_pad_set_event_full_function (pad,
2911           gst_validate_pad_monitor_sink_event_full_func);
2912     else
2913       gst_pad_set_event_function (pad,
2914           gst_validate_pad_monitor_sink_event_func);
2915   } else {
2916     gst_pad_set_event_function (pad, gst_validate_pad_monitor_src_event_func);
2917
2918     /* add buffer/event probes */
2919     pad_monitor->pad_probe_id =
2920         gst_pad_add_probe (pad,
2921         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
2922         GST_PAD_PROBE_TYPE_EVENT_FLUSH,
2923         (GstPadProbeCallback) gst_validate_pad_monitor_pad_probe, pad_monitor,
2924         NULL);
2925   }
2926   gst_pad_set_query_function (pad, gst_validate_pad_monitor_query_func);
2927   gst_pad_set_activatemode_function (pad,
2928       gst_validate_pad_monitor_activatemode_func);
2929
2930   gst_validate_reporter_set_name (GST_VALIDATE_REPORTER (monitor),
2931       g_strdup_printf ("%s:%s", GST_DEBUG_PAD_NAME (pad)));
2932
2933   if (G_UNLIKELY (GST_PAD_PARENT (pad) == NULL))
2934     GST_FIXME ("Saw a pad not belonging to any object");
2935
2936   gst_validate_pad_monitor_get_min_buffer_frequency (pad_monitor, pad);
2937
2938   gst_object_unref (pad);
2939   return TRUE;
2940 }