validate: Check all buffers when we have the info from MediaDescriptor
[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-reporter.h"
32 #include <string.h>
33 #include <stdarg.h>
34
35 /**
36  * SECTION:gst-validate-pad-monitor
37  * @short_description: Class that wraps a #GstPad for Validate checks
38  *
39  * TODO
40  */
41
42 static GstValidateInterceptionReturn
43 gst_validate_pad_monitor_intercept_report (GstValidateReporter * reporter,
44     GstValidateReport * report);
45
46 #define _do_init \
47   G_IMPLEMENT_INTERFACE (GST_TYPE_VALIDATE_REPORTER, _reporter_iface_init)
48
49 static void
50 _reporter_iface_init (GstValidateReporterInterface * iface)
51 {
52   iface->intercept_report = gst_validate_pad_monitor_intercept_report;
53 }
54
55 #define gst_validate_pad_monitor_parent_class parent_class
56 G_DEFINE_TYPE_WITH_CODE (GstValidatePadMonitor, gst_validate_pad_monitor,
57     GST_TYPE_VALIDATE_MONITOR, _do_init);
58
59 #define PENDING_FIELDS "pending-fields"
60 #define AUDIO_TIMESTAMP_TOLERANCE (GST_MSECOND * 100)
61
62 #define PAD_PARENT_IS_DEMUXER(m) \
63     (GST_VALIDATE_MONITOR_GET_PARENT(m) ? \
64         GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_DEMUXER ( \
65             GST_VALIDATE_MONITOR_GET_PARENT(m)) : \
66         FALSE)
67
68 #define PAD_PARENT_IS_DECODER(m) \
69     (GST_VALIDATE_MONITOR_GET_PARENT(m) ? \
70         GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_DECODER ( \
71             GST_VALIDATE_MONITOR_GET_PARENT(m)) : \
72         FALSE)
73
74 #define PAD_PARENT_IS_ENCODER(m) \
75     (GST_VALIDATE_MONITOR_GET_PARENT(m) ? \
76         GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_ENCODER ( \
77             GST_VALIDATE_MONITOR_GET_PARENT(m)) : \
78         FALSE)
79
80
81 /*
82  * Locking the parent should always be done before locking the
83  * pad-monitor to prevent deadlocks in case another monitor from
84  * another pad on the same element starts an operation that also
85  * requires locking itself and some other monitors from internally
86  * linked pads.
87  *
88  * An example:
89  * An element has a sink and a src pad. Some test starts running at sinkpad
90  * and it locks the parent, and then it locks itself. In case it needs to get
91  * some information from the srcpad, it is able to lock the srcpad and get it
92  * because the srcpad should never lock itself before locking the parent (which
93  * it won't be able as sinkpad already locked it).
94  *
95  * As a side one, it is possible that srcpad locks itself without locking the
96  * parent in case it wants to do a check that won't need to use other internally
97  * linked pads (sinkpad). But in this case it might lock and unlock freely without
98  * causing deadlocks.
99  */
100 #define GST_VALIDATE_PAD_MONITOR_PARENT_LOCK(m)                  \
101 G_STMT_START {                                             \
102   if (G_LIKELY (GST_VALIDATE_MONITOR_GET_PARENT (m))) {          \
103     GST_VALIDATE_MONITOR_LOCK (GST_VALIDATE_MONITOR_GET_PARENT (m));   \
104   } else {                                                 \
105     GST_WARNING_OBJECT (m, "No parent found, can't lock"); \
106   }                                                        \
107 } G_STMT_END
108
109 #define GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK(m)                  \
110 G_STMT_START {                                               \
111   if (G_LIKELY (GST_VALIDATE_MONITOR_GET_PARENT (m))) {            \
112     GST_VALIDATE_MONITOR_UNLOCK (GST_VALIDATE_MONITOR_GET_PARENT (m));   \
113   } else {                                                   \
114     GST_WARNING_OBJECT (m, "No parent found, can't unlock"); \
115   }                                                          \
116 } G_STMT_END
117
118 typedef struct
119 {
120   GstClockTime timestamp;
121   GstEvent *event;
122 } SerializedEventData;
123
124 static GstPad *
125 _get_actual_pad (GstPad *pad)
126 {
127   GstPad *tmp_pad;
128
129   gst_object_ref (pad);
130
131   /* We don't monitor ghost pads */
132   while (GST_IS_GHOST_PAD (pad)) {
133     tmp_pad = pad;
134     pad = gst_ghost_pad_get_target ((GstGhostPad *) pad);
135     gst_object_unref (tmp_pad);
136   }
137
138   while (GST_IS_PROXY_PAD (pad)) {
139     tmp_pad = pad;
140     pad = gst_pad_get_peer (pad);
141     gst_object_unref (tmp_pad);
142   }
143
144   return pad;
145 }
146
147 static gboolean
148 _find_master_report_on_pad (GstPad * pad, GstValidateReport * report)
149 {
150   GstValidatePadMonitor *pad_monitor;
151   GstValidateReport *prev_report;
152   gboolean result = FALSE;
153
154   pad = _get_actual_pad (pad);
155
156   pad_monitor = g_object_get_data ((GObject *) pad, "validate-monitor");
157
158   /* For some reason this pad isn't monitored */
159   if (pad_monitor == NULL)
160     goto done;
161
162   prev_report = gst_validate_reporter_get_report ((GstValidateReporter *)
163       pad_monitor, report->issue->issue_id);
164
165   if (prev_report) {
166     if (prev_report->master_report)
167       result = gst_validate_report_set_master_report (report,
168           prev_report->master_report);
169     else
170       result = gst_validate_report_set_master_report (report, prev_report);
171   }
172
173 done:
174   gst_object_unref (pad);
175
176   return result;
177 }
178
179 static gboolean
180 _find_master_report_for_sink_pad (GstValidatePadMonitor * pad_monitor,
181     GstValidateReport * report)
182 {
183   GstPad *peerpad;
184   gboolean result = FALSE;
185
186   peerpad = gst_pad_get_peer (pad_monitor->pad);
187
188   /* If the peer src pad already has a similar report no need to look
189    * any further */
190   if (peerpad && _find_master_report_on_pad (peerpad, report))
191     result = TRUE;
192
193   if (peerpad)
194     gst_object_unref (peerpad);
195
196   return result;
197 }
198
199 static gboolean
200 _find_master_report_for_src_pad (GstValidatePadMonitor * pad_monitor,
201     GstValidateReport * report)
202 {
203   GstIterator *iter;
204   gboolean done;
205   GstPad *pad;
206   gboolean result = FALSE;
207
208   iter =
209       gst_pad_iterate_internal_links (GST_VALIDATE_PAD_MONITOR_GET_PAD
210       (pad_monitor));
211   done = FALSE;
212   while (!done) {
213     GValue value = { 0, };
214     switch (gst_iterator_next (iter, &value)) {
215       case GST_ITERATOR_OK:
216         pad = g_value_get_object (&value);
217
218         if (_find_master_report_on_pad (pad, report)) {
219           result = TRUE;
220           done = TRUE;
221         }
222
223         g_value_reset (&value);
224         break;
225       case GST_ITERATOR_RESYNC:
226         gst_iterator_resync (iter);
227         break;
228       case GST_ITERATOR_ERROR:
229         GST_WARNING_OBJECT (pad_monitor->pad,
230             "Internal links pad iteration error");
231         done = TRUE;
232         break;
233       case GST_ITERATOR_DONE:
234         done = TRUE;
235         break;
236     }
237   }
238   gst_iterator_free (iter);
239
240   return result;
241 }
242
243 static GstValidateInterceptionReturn
244 _concatenate_issues (GstValidatePadMonitor * pad_monitor,
245     GstValidateReport * report)
246 {
247   if (GST_PAD_IS_SINK (pad_monitor->pad)
248       && _find_master_report_for_sink_pad (pad_monitor, report))
249     return GST_VALIDATE_REPORTER_KEEP;
250   else if (GST_PAD_IS_SRC (pad_monitor->pad)
251       && _find_master_report_for_src_pad (pad_monitor, report))
252     return GST_VALIDATE_REPORTER_KEEP;
253
254   return GST_VALIDATE_REPORTER_REPORT;
255 }
256
257 static GstValidateInterceptionReturn
258 gst_validate_pad_monitor_intercept_report (GstValidateReporter *
259     reporter, GstValidateReport * report)
260 {
261   GstValidateReporterInterface *iface_class, *old_iface_class;
262   GstValidatePadMonitor *pad_monitor = GST_VALIDATE_PAD_MONITOR (reporter);
263   GstValidateReportingLevel monitor_reporting_level;
264   GstValidateInterceptionReturn ret;
265
266   monitor_reporting_level =
267       gst_validate_reporter_get_reporting_level (reporter);
268
269   iface_class =
270       G_TYPE_INSTANCE_GET_INTERFACE (reporter, GST_TYPE_VALIDATE_REPORTER,
271       GstValidateReporterInterface);
272   old_iface_class = g_type_interface_peek_parent (iface_class);
273
274   old_iface_class->intercept_report (reporter, report);
275
276   switch (monitor_reporting_level) {
277     case GST_VALIDATE_REPORTING_LEVEL_NONE:
278       ret = GST_VALIDATE_REPORTER_DROP;
279       break;
280     case GST_VALIDATE_REPORTING_LEVEL_UNKNOWN:
281       ret = _concatenate_issues (pad_monitor, report);
282       break;
283     default:
284       ret = GST_VALIDATE_REPORTER_REPORT;
285       break;
286   }
287
288   gst_validate_report_set_reporting_level (report, monitor_reporting_level);
289   return ret;
290 }
291
292 static void
293 debug_pending_event (GstPad * pad, GPtrArray * array)
294 {
295   guint i, len;
296
297   len = array->len;
298   for (i = 0; i < len; i++) {
299     SerializedEventData *data = g_ptr_array_index (array, i);
300     GST_DEBUG_OBJECT (pad, "event #%d %" GST_TIME_FORMAT " %s %p",
301         i, GST_TIME_ARGS (data->timestamp),
302         GST_EVENT_TYPE_NAME (data->event), data->event);
303   }
304 }
305
306 static void
307 _serialized_event_data_free (SerializedEventData * serialized_event)
308 {
309   gst_event_unref (serialized_event->event);
310   g_slice_free (SerializedEventData, serialized_event);
311 }
312
313 static gboolean gst_validate_pad_monitor_do_setup (GstValidateMonitor *
314     monitor);
315 static GstElement *gst_validate_pad_monitor_get_element (GstValidateMonitor *
316     monitor);
317 static void
318 gst_validate_pad_monitor_setcaps_pre (GstValidatePadMonitor * pad_monitor,
319     GstCaps * caps);
320 static void gst_validate_pad_monitor_setcaps_post (GstValidatePadMonitor *
321     pad_monitor, GstCaps * caps, gboolean ret);
322
323 #define PAD_IS_IN_PUSH_MODE(p) ((p)->mode == GST_PAD_MODE_PUSH)
324
325 static gboolean
326 _structure_is_raw_video (GstStructure * structure)
327 {
328   return gst_structure_has_name (structure, "video/x-raw");
329 }
330
331 static gboolean
332 _structure_is_raw_audio (GstStructure * structure)
333 {
334   return gst_structure_has_name (structure, "audio/x-raw");
335 }
336
337 static gchar *
338 _get_event_string (GstEvent * event)
339 {
340   const GstStructure *st;
341
342   if ((st = gst_event_get_structure (event)))
343     return gst_structure_to_string (st);
344   else
345     return g_strdup_printf ("%s", GST_EVENT_TYPE_NAME (event));
346 }
347
348 static void
349 _check_field_type (GstValidatePadMonitor * monitor,
350     GstStructure * structure, gboolean mandatory, const gchar * field, ...)
351 {
352   va_list var_args;
353   GType type;
354   gchar *joined_types = NULL;
355   const gchar *rejected_types[5];
356   gint rejected_types_index = 0;
357   gchar *struct_str;
358
359   if (!gst_structure_has_field (structure, field)) {
360     if (mandatory) {
361       gchar *str = gst_structure_to_string (structure);
362
363       GST_VALIDATE_REPORT (monitor, CAPS_IS_MISSING_FIELD,
364           "Field '%s' is missing from structure: %s", field, str);
365       g_free (str);
366     } else {
367       GST_DEBUG_OBJECT (monitor, "Field %s is missing but is not mandatory",
368           field);
369     }
370     return;
371   }
372
373   memset (rejected_types, 0, sizeof (rejected_types));
374   va_start (var_args, field);
375   while ((type = va_arg (var_args, GType)) != 0) {
376     if (gst_structure_has_field_typed (structure, field, type)) {
377       va_end (var_args);
378       return;
379     }
380     rejected_types[rejected_types_index++] = g_type_name (type);
381   }
382   va_end (var_args);
383
384   joined_types = g_strjoinv (" / ", (gchar **) rejected_types);
385   struct_str = gst_structure_to_string (structure);
386   GST_VALIDATE_REPORT (monitor, CAPS_FIELD_HAS_BAD_TYPE,
387       "Field '%s' has wrong type %s in structure '%s'. Expected: %s", field,
388       g_type_name (gst_structure_get_field_type (structure, field)), struct_str,
389       joined_types);
390   g_free (joined_types);
391   g_free (struct_str);
392 }
393
394 static void
395 gst_validate_pad_monitor_check_raw_video_caps_complete (GstValidatePadMonitor *
396     monitor, GstStructure * structure)
397 {
398   _check_field_type (monitor, structure, TRUE, "width", G_TYPE_INT,
399       GST_TYPE_INT_RANGE, 0);
400   _check_field_type (monitor, structure, TRUE, "height", G_TYPE_INT,
401       GST_TYPE_INT_RANGE, 0);
402   _check_field_type (monitor, structure, TRUE, "framerate", GST_TYPE_FRACTION,
403       GST_TYPE_FRACTION_RANGE, 0);
404   _check_field_type (monitor, structure, FALSE, "pixel-aspect-ratio",
405       GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE, 0);
406   _check_field_type (monitor, structure, TRUE, "format", G_TYPE_STRING,
407       GST_TYPE_LIST);
408 }
409
410 static void
411 gst_validate_pad_monitor_check_raw_audio_caps_complete (GstValidatePadMonitor *
412     monitor, GstStructure * structure)
413 {
414   gint channels;
415   _check_field_type (monitor, structure, TRUE, "format", G_TYPE_STRING,
416       GST_TYPE_LIST, 0);
417   _check_field_type (monitor, structure, TRUE, "layout", G_TYPE_STRING,
418       GST_TYPE_LIST, 0);
419   _check_field_type (monitor, structure, TRUE, "rate", G_TYPE_INT,
420       GST_TYPE_LIST, GST_TYPE_INT_RANGE, 0);
421   _check_field_type (monitor, structure, TRUE, "channels", G_TYPE_INT,
422       GST_TYPE_LIST, GST_TYPE_INT_RANGE, 0);
423   if (gst_structure_get_int (structure, "channels", &channels)) {
424     if (channels > 2)
425       _check_field_type (monitor, structure, TRUE, "channel-mask",
426           GST_TYPE_BITMASK, GST_TYPE_LIST, 0);
427   }
428 }
429
430 static void
431 gst_validate_pad_monitor_check_caps_complete (GstValidatePadMonitor * monitor,
432     GstCaps * caps)
433 {
434   GstStructure *structure;
435   gint i;
436
437   GST_DEBUG_OBJECT (monitor->pad, "Checking caps %" GST_PTR_FORMAT, caps);
438
439   for (i = 0; i < gst_caps_get_size (caps); i++) {
440     structure = gst_caps_get_structure (caps, i);
441
442     if (_structure_is_raw_video (structure)) {
443       gst_validate_pad_monitor_check_raw_video_caps_complete (monitor,
444           structure);
445
446     } else if (_structure_is_raw_audio (structure)) {
447       gst_validate_pad_monitor_check_raw_audio_caps_complete (monitor,
448           structure);
449     }
450   }
451 }
452
453 static GstCaps *
454 gst_validate_pad_monitor_get_othercaps (GstValidatePadMonitor * monitor)
455 {
456   GstCaps *caps = gst_caps_new_empty ();
457   GstIterator *iter;
458   gboolean done;
459   GstPad *otherpad;
460   GstCaps *peercaps;
461
462   iter =
463       gst_pad_iterate_internal_links (GST_VALIDATE_PAD_MONITOR_GET_PAD
464       (monitor));
465   done = FALSE;
466   while (!done) {
467     GValue value = { 0, };
468     switch (gst_iterator_next (iter, &value)) {
469       case GST_ITERATOR_OK:
470         otherpad = g_value_get_object (&value);
471
472         /* TODO What would be the correct caps operation to merge the caps in
473          * case one sink is internally linked to multiple srcs? */
474         peercaps = gst_pad_get_current_caps (otherpad);
475         if (peercaps)
476           caps = gst_caps_merge (caps, peercaps);
477
478         g_value_reset (&value);
479         break;
480       case GST_ITERATOR_RESYNC:
481         gst_iterator_resync (iter);
482         gst_caps_replace (&caps, gst_caps_new_empty ());
483         break;
484       case GST_ITERATOR_ERROR:
485         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
486         done = TRUE;
487         break;
488       case GST_ITERATOR_DONE:
489         done = TRUE;
490         break;
491     }
492   }
493   gst_iterator_free (iter);
494
495   GST_DEBUG_OBJECT (monitor->pad, "Otherpad caps: %" GST_PTR_FORMAT, caps);
496
497   return caps;
498 }
499
500 static gboolean
501 _structure_is_video (GstStructure * structure)
502 {
503   const gchar *name = gst_structure_get_name (structure);
504
505   return g_strstr_len (name, 6, "video/")
506       && strcmp (name, "video/quicktime") != 0;
507 }
508
509 static gboolean
510 _structure_is_audio (GstStructure * structure)
511 {
512   const gchar *name = gst_structure_get_name (structure);
513
514   return g_strstr_len (name, 6, "audio/") != NULL;
515 }
516
517 static gboolean
518 gst_validate_pad_monitor_pad_should_proxy_othercaps (GstValidatePadMonitor *
519     monitor)
520 {
521   GstValidateMonitor *parent = GST_VALIDATE_MONITOR_GET_PARENT (monitor);
522
523   if (!parent)
524     return FALSE;
525
526   /* We only know how to handle othercaps checks for codecs so far */
527   return GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_DECODER (parent) ||
528       GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_ENCODER (parent);
529 }
530
531
532 /* Check if the field @f from @s2 (if present) is represented in @s1
533  * Represented here means either equal or @s1's value is in a list/range
534  * from @s2
535  */
536 static gboolean
537 _structures_field_is_contained (GstStructure * s1, GstStructure * s2,
538     gboolean mandatory, const gchar * f)
539 {
540   const GValue *v1;
541   const GValue *v2;
542
543   v2 = gst_structure_get_value (s2, f);
544   if (!v2)
545     return TRUE;                /* nothing to compare to */
546
547   v1 = gst_structure_get_value (s1, f);
548   if (!v1)
549     return !mandatory;
550
551   if (!gst_value_is_fixed (v1))
552     return TRUE;
553
554   if (gst_value_compare (v1, v2) == GST_VALUE_EQUAL)
555     return TRUE;
556
557   if (GST_VALUE_HOLDS_LIST (v2)) {
558     gint i;
559     for (i = 0; i < gst_value_list_get_size (v2); i++) {
560       const GValue *v2_subvalue = gst_value_list_get_value (v2, i);
561       if (gst_value_compare (v1, v2_subvalue) == GST_VALUE_EQUAL)
562         return TRUE;
563     }
564   }
565
566   if (GST_VALUE_HOLDS_ARRAY (v2)) {
567     gint i;
568     for (i = 0; i < gst_value_array_get_size (v2); i++) {
569       const GValue *v2_subvalue = gst_value_array_get_value (v2, i);
570       if (gst_value_compare (v1, v2_subvalue) == GST_VALUE_EQUAL)
571         return TRUE;
572     }
573   }
574
575   if (GST_VALUE_HOLDS_INT_RANGE (v2)) {
576     gint min, max;
577
578     min = gst_value_get_int_range_min (v2);
579     max = gst_value_get_int_range_max (v2);
580
581     if (G_VALUE_HOLDS_INT (v1)) {
582       gint v = g_value_get_int (v1);
583
584       return v >= min && v <= max;
585     } else {
586       /* TODO compare int ranges with int ranges
587        * or with lists if useful */
588     }
589   }
590
591   if (GST_VALUE_HOLDS_FRACTION_RANGE (v2)) {
592     const GValue *min, *max;
593
594     min = gst_value_get_fraction_range_min (v2);
595     max = gst_value_get_fraction_range_max (v2);
596
597     if (GST_VALUE_HOLDS_FRACTION (v1)) {
598       gint v_min = gst_value_compare (v1, min);
599       gint v_max = gst_value_compare (v1, max);
600
601       return (v_min == GST_VALUE_EQUAL || v_min == GST_VALUE_GREATER_THAN) &&
602           (v_max == GST_VALUE_EQUAL || v_max == GST_VALUE_LESS_THAN);
603     } else {
604       /* TODO compare fraction ranges with fraction ranges
605        * or with lists if useful */
606     }
607   }
608
609   return FALSE;
610 }
611
612 static void
613 gst_validate_pad_monitor_check_caps_fields_proxied (GstValidatePadMonitor *
614     monitor, GstCaps * caps)
615 {
616   GstStructure *structure;
617   GstStructure *otherstructure;
618   GstCaps *othercaps;
619   gint i, j;
620
621   if (!gst_validate_pad_monitor_pad_should_proxy_othercaps (monitor))
622     return;
623
624   othercaps = gst_validate_pad_monitor_get_othercaps (monitor);
625
626   for (i = 0; i < gst_caps_get_size (othercaps); i++) {
627     gboolean found = FALSE;
628     gboolean type_match = FALSE;
629
630     otherstructure = gst_caps_get_structure (othercaps, i);
631
632     /* look for a proxied version of 'otherstructure' */
633     if (_structure_is_video (otherstructure)) {
634       for (j = 0; j < gst_caps_get_size (caps); j++) {
635         structure = gst_caps_get_structure (caps, j);
636         if (_structure_is_video (structure)) {
637           type_match = TRUE;
638           if (_structures_field_is_contained (structure, otherstructure, TRUE,
639                   "width")
640               && _structures_field_is_contained (structure, otherstructure,
641                   TRUE, "height")
642               && _structures_field_is_contained (structure, otherstructure,
643                   TRUE, "framerate")
644               && _structures_field_is_contained (structure, otherstructure,
645                   FALSE, "pixel-aspect-ratio")) {
646             found = TRUE;
647             break;
648           }
649         }
650       }
651     } else if (_structure_is_audio (otherstructure)) {
652       for (j = 0; j < gst_caps_get_size (caps); j++) {
653         structure = gst_caps_get_structure (caps, j);
654         if (_structure_is_audio (structure)) {
655           type_match = TRUE;
656           if (_structures_field_is_contained (structure, otherstructure, TRUE,
657                   "rate")
658               && _structures_field_is_contained (structure, otherstructure,
659                   TRUE, "channels")) {
660             found = TRUE;
661             break;
662           }
663         }
664       }
665     }
666
667     if (type_match && !found) {
668       gchar *otherstruct_str = gst_structure_to_string (otherstructure),
669           *caps_str = gst_caps_to_string (caps);
670
671       GST_VALIDATE_REPORT (monitor, GET_CAPS_NOT_PROXYING_FIELDS,
672           "Peer pad structure '%s' has no similar version "
673           "on pad's caps '%s'", otherstruct_str, caps_str);
674
675       g_free (otherstruct_str);
676       g_free (caps_str);
677     }
678   }
679 }
680
681 static void
682 gst_validate_pad_monitor_check_late_serialized_events (GstValidatePadMonitor *
683     monitor, GstClockTime ts)
684 {
685   gint i;
686
687   if (!GST_CLOCK_TIME_IS_VALID (ts))
688     return;
689
690   GST_DEBUG_OBJECT (monitor->pad, "Timestamp to check %" GST_TIME_FORMAT,
691       GST_TIME_ARGS (ts));
692
693   for (i = 0; i < monitor->serialized_events->len; i++) {
694     SerializedEventData *data =
695         g_ptr_array_index (monitor->serialized_events, i);
696
697     GST_DEBUG_OBJECT (monitor->pad, "Event #%d (%s) ts: %" GST_TIME_FORMAT,
698         i, GST_EVENT_TYPE_NAME (data->event), GST_TIME_ARGS (data->timestamp));
699
700     if (GST_CLOCK_TIME_IS_VALID (data->timestamp) && data->timestamp < ts) {
701       gchar *event_str = _get_event_string (data->event);
702
703       GST_VALIDATE_REPORT (monitor, SERIALIZED_EVENT_WASNT_PUSHED_IN_TIME,
704           "Serialized event %s wasn't pushed before expected " "timestamp %"
705           GST_TIME_FORMAT " on pad %s:%s", event_str,
706           GST_TIME_ARGS (data->timestamp),
707           GST_DEBUG_PAD_NAME (GST_VALIDATE_PAD_MONITOR_GET_PAD (monitor)));
708
709       g_free (event_str);
710     } else {
711       /* events should be ordered by ts */
712       break;
713     }
714   }
715
716   if (i) {
717     debug_pending_event (monitor->pad, monitor->serialized_events);
718     g_ptr_array_remove_range (monitor->serialized_events, 0, i);
719   }
720 }
721
722 static void
723 gst_validate_pad_monitor_dispose (GObject * object)
724 {
725   GstValidatePadMonitor *monitor = GST_VALIDATE_PAD_MONITOR_CAST (object);
726   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (monitor);
727
728   if (pad) {
729     if (monitor->pad_probe_id)
730       gst_pad_remove_probe (pad, monitor->pad_probe_id);
731   }
732
733   if (monitor->expected_segment)
734     gst_event_unref (monitor->expected_segment);
735
736   gst_structure_free (monitor->pending_setcaps_fields);
737   g_ptr_array_unref (monitor->serialized_events);
738   g_list_free_full (monitor->expired_events, (GDestroyNotify) gst_event_unref);
739
740   G_OBJECT_CLASS (parent_class)->dispose (object);
741 }
742
743 static void
744 gst_validate_pad_monitor_class_init (GstValidatePadMonitorClass * klass)
745 {
746   GObjectClass *gobject_class;
747   GstValidateMonitorClass *monitor_klass;
748
749   gobject_class = G_OBJECT_CLASS (klass);
750   monitor_klass = GST_VALIDATE_MONITOR_CLASS (klass);
751
752   gobject_class->dispose = gst_validate_pad_monitor_dispose;
753
754   monitor_klass->setup = gst_validate_pad_monitor_do_setup;
755   monitor_klass->get_element = gst_validate_pad_monitor_get_element;
756 }
757
758 static void
759 gst_validate_pad_monitor_init (GstValidatePadMonitor * pad_monitor)
760 {
761   pad_monitor->pending_setcaps_fields =
762       gst_structure_new_empty (PENDING_FIELDS);
763   pad_monitor->serialized_events =
764       g_ptr_array_new_with_free_func ((GDestroyNotify)
765       _serialized_event_data_free);
766   pad_monitor->expired_events = NULL;
767   gst_segment_init (&pad_monitor->segment, GST_FORMAT_BYTES);
768   pad_monitor->first_buffer = TRUE;
769
770   pad_monitor->timestamp_range_start = GST_CLOCK_TIME_NONE;
771   pad_monitor->timestamp_range_end = GST_CLOCK_TIME_NONE;
772 }
773
774 /**
775  * gst_validate_pad_monitor_new:
776  * @pad: (transfer-none): a #GstPad to run Validate on
777  */
778 GstValidatePadMonitor *
779 gst_validate_pad_monitor_new (GstPad * pad, GstValidateRunner * runner,
780     GstValidateElementMonitor * parent)
781 {
782   GstValidatePadMonitor *monitor = g_object_new (GST_TYPE_VALIDATE_PAD_MONITOR,
783       "object", pad, "validate-runner", runner, "validate-parent",
784       parent, NULL);
785
786   if (GST_VALIDATE_PAD_MONITOR_GET_PAD (monitor) == NULL) {
787     g_object_unref (monitor);
788     return NULL;
789   }
790   return monitor;
791 }
792
793 static GstElement *
794 gst_validate_pad_monitor_get_element (GstValidateMonitor * monitor)
795 {
796   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (monitor);
797
798   return GST_PAD_PARENT (pad);
799 }
800
801 static void
802 gst_validate_pad_monitor_event_overrides (GstValidatePadMonitor * pad_monitor,
803     GstEvent * event)
804 {
805   GList *iter;
806
807   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
808   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
809       iter = g_list_next (iter)) {
810     GstValidateOverride *override = iter->data;
811
812     gst_validate_override_event_handler (override,
813         GST_VALIDATE_MONITOR_CAST (pad_monitor), event);
814   }
815   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
816 }
817
818 static void
819 gst_validate_pad_monitor_buffer_overrides (GstValidatePadMonitor * pad_monitor,
820     GstBuffer * buffer)
821 {
822   GList *iter;
823
824   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
825   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
826       iter = g_list_next (iter)) {
827     GstValidateOverride *override = iter->data;
828
829     gst_validate_override_buffer_handler (override,
830         GST_VALIDATE_MONITOR_CAST (pad_monitor), buffer);
831   }
832   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
833 }
834
835 static void
836 gst_validate_pad_monitor_buffer_probe_overrides (GstValidatePadMonitor *
837     pad_monitor, GstBuffer * buffer)
838 {
839   GList *iter;
840
841   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
842   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
843       iter = g_list_next (iter)) {
844     GstValidateOverride *override = iter->data;
845
846     gst_validate_override_buffer_probe_handler (override,
847         GST_VALIDATE_MONITOR_CAST (pad_monitor), buffer);
848   }
849   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
850 }
851
852 static void
853 gst_validate_pad_monitor_query_overrides (GstValidatePadMonitor * pad_monitor,
854     GstQuery * query)
855 {
856   GList *iter;
857
858   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
859   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
860       iter = g_list_next (iter)) {
861     GstValidateOverride *override = iter->data;
862
863     gst_validate_override_query_handler (override,
864         GST_VALIDATE_MONITOR_CAST (pad_monitor), query);
865   }
866   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
867 }
868
869 static void
870 gst_validate_pad_monitor_setcaps_overrides (GstValidatePadMonitor * pad_monitor,
871     GstCaps * caps)
872 {
873   GList *iter;
874
875   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
876   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
877       iter = g_list_next (iter)) {
878     GstValidateOverride *override = iter->data;
879
880     gst_validate_override_setcaps_handler (override,
881         GST_VALIDATE_MONITOR_CAST (pad_monitor), caps);
882   }
883   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
884 }
885
886 /* FIXME : This is a bit dubious, what's the point of this check ? */
887 static gboolean
888 gst_validate_pad_monitor_timestamp_is_in_received_range (GstValidatePadMonitor *
889     monitor, GstClockTime ts, GstClockTime tolerance)
890 {
891   GST_DEBUG_OBJECT (monitor->pad, "Checking if timestamp %" GST_TIME_FORMAT
892       " is in range: %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT " for pad "
893       "%s:%s with tolerance: %" GST_TIME_FORMAT, GST_TIME_ARGS (ts),
894       GST_TIME_ARGS (monitor->timestamp_range_start),
895       GST_TIME_ARGS (monitor->timestamp_range_end),
896       GST_DEBUG_PAD_NAME (GST_VALIDATE_PAD_MONITOR_GET_PAD (monitor)),
897       GST_TIME_ARGS (tolerance));
898   return !GST_CLOCK_TIME_IS_VALID (monitor->timestamp_range_start) ||
899       !GST_CLOCK_TIME_IS_VALID (monitor->timestamp_range_end) ||
900       ((monitor->timestamp_range_start >= tolerance ?
901           monitor->timestamp_range_start - tolerance : 0) <= ts
902       && (ts >= tolerance ? ts - tolerance : 0) <=
903       monitor->timestamp_range_end);
904 }
905
906 /* Iterates over internal links (sinkpads) to check that this buffer has
907  * a timestamp that is in the range of the lastly received buffers */
908 static void
909     gst_validate_pad_monitor_check_buffer_timestamp_in_received_range
910     (GstValidatePadMonitor * monitor, GstBuffer * buffer,
911     GstClockTime tolerance)
912 {
913   GstClockTime ts;
914   GstClockTime ts_end;
915   GstIterator *iter;
916   gboolean has_one = FALSE;
917   gboolean found = FALSE;
918   gboolean done;
919   GstPad *otherpad;
920   GstValidatePadMonitor *othermonitor;
921
922   if (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))
923       || !GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) {
924     GST_DEBUG_OBJECT (monitor->pad,
925         "Can't check buffer timestamps range as "
926         "buffer has no valid timestamp/duration");
927     return;
928   }
929   ts = GST_BUFFER_TIMESTAMP (buffer);
930   ts_end = ts + GST_BUFFER_DURATION (buffer);
931
932   iter =
933       gst_pad_iterate_internal_links (GST_VALIDATE_PAD_MONITOR_GET_PAD
934       (monitor));
935
936   if (iter == NULL) {
937     GST_WARNING_OBJECT (GST_VALIDATE_PAD_MONITOR_GET_PAD (monitor),
938         "No iterator available");
939     return;
940   }
941
942   done = FALSE;
943   while (!done) {
944     GValue value = { 0, };
945     switch (gst_iterator_next (iter, &value)) {
946       case GST_ITERATOR_OK:
947         otherpad = g_value_get_object (&value);
948         GST_DEBUG_OBJECT (monitor->pad, "Checking pad %s:%s input timestamps",
949             GST_DEBUG_PAD_NAME (otherpad));
950         othermonitor =
951             g_object_get_data ((GObject *) otherpad, "validate-monitor");
952         GST_VALIDATE_MONITOR_LOCK (othermonitor);
953         if (gst_validate_pad_monitor_timestamp_is_in_received_range
954             (othermonitor, ts, tolerance)
955             &&
956             gst_validate_pad_monitor_timestamp_is_in_received_range
957             (othermonitor, ts_end, tolerance)) {
958           done = TRUE;
959           found = TRUE;
960         }
961         GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
962         g_value_reset (&value);
963         has_one = TRUE;
964         break;
965       case GST_ITERATOR_RESYNC:
966         gst_iterator_resync (iter);
967         has_one = FALSE;
968         found = FALSE;
969         break;
970       case GST_ITERATOR_ERROR:
971         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
972         done = TRUE;
973         break;
974       case GST_ITERATOR_DONE:
975         done = TRUE;
976         break;
977     }
978   }
979   gst_iterator_free (iter);
980
981   if (!has_one) {
982     GST_DEBUG_OBJECT (monitor->pad, "Skipping timestamp in range check as no "
983         "internal linked pad was found");
984     return;
985   }
986   if (!found) {
987     GST_VALIDATE_REPORT (monitor, BUFFER_TIMESTAMP_OUT_OF_RECEIVED_RANGE,
988         "Timestamp %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT
989         " is out of range of received input", GST_TIME_ARGS (ts),
990         GST_TIME_ARGS (ts_end));
991   }
992 }
993
994 static void
995 gst_validate_pad_monitor_check_first_buffer (GstValidatePadMonitor *
996     pad_monitor, GstBuffer * buffer)
997 {
998   if (G_UNLIKELY (pad_monitor->first_buffer)) {
999     pad_monitor->first_buffer = FALSE;
1000
1001     if (!pad_monitor->has_segment
1002         && PAD_IS_IN_PUSH_MODE (GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor)))
1003     {
1004       GST_VALIDATE_REPORT (pad_monitor, BUFFER_BEFORE_SEGMENT,
1005           "Received buffer before Segment event");
1006     }
1007
1008     GST_DEBUG_OBJECT (pad_monitor->pad,
1009         "Checking first buffer (pts:%" GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT
1010         ")", GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1011         GST_TIME_ARGS (GST_BUFFER_DTS (buffer)));
1012
1013     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) {
1014       gint64 running_time = gst_segment_to_running_time (&pad_monitor->segment,
1015           pad_monitor->segment.format, GST_BUFFER_TIMESTAMP (buffer));
1016       /* Only check for in-segment buffers */
1017       if (GST_CLOCK_TIME_IS_VALID (running_time) && running_time != 0) {
1018         GST_VALIDATE_REPORT (pad_monitor, FIRST_BUFFER_RUNNING_TIME_IS_NOT_ZERO,
1019             "First buffer running time is not 0, it is: %" GST_TIME_FORMAT,
1020             GST_TIME_ARGS (running_time));
1021       }
1022     }
1023   }
1024 }
1025
1026 static void
1027 gst_validate_pad_monitor_check_eos (GstValidatePadMonitor *
1028     pad_monitor, GstBuffer * buffer)
1029 {
1030   if (G_UNLIKELY (pad_monitor->is_eos)) {
1031     GST_VALIDATE_REPORT (pad_monitor, BUFFER_AFTER_EOS,
1032         "Received buffer %" GST_PTR_FORMAT " after EOS", buffer);
1033   }
1034 }
1035
1036 static void
1037 gst_validate_pad_monitor_update_buffer_data (GstValidatePadMonitor *
1038     pad_monitor, GstBuffer * buffer)
1039 {
1040   pad_monitor->current_timestamp = GST_BUFFER_TIMESTAMP (buffer);
1041   pad_monitor->current_duration = GST_BUFFER_DURATION (buffer);
1042   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) {
1043     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->timestamp_range_start)) {
1044       pad_monitor->timestamp_range_start =
1045           MIN (pad_monitor->timestamp_range_start,
1046           GST_BUFFER_TIMESTAMP (buffer));
1047     } else {
1048       pad_monitor->timestamp_range_start = GST_BUFFER_TIMESTAMP (buffer);
1049     }
1050
1051     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) {
1052       GstClockTime endts =
1053           GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1054       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->timestamp_range_end)) {
1055         pad_monitor->timestamp_range_end =
1056             MAX (pad_monitor->timestamp_range_end, endts);
1057       } else {
1058         pad_monitor->timestamp_range_end = endts;
1059       }
1060     }
1061   }
1062   GST_DEBUG_OBJECT (pad_monitor->pad, "Current stored range: %" GST_TIME_FORMAT
1063       " - %" GST_TIME_FORMAT,
1064       GST_TIME_ARGS (pad_monitor->timestamp_range_start),
1065       GST_TIME_ARGS (pad_monitor->timestamp_range_end));
1066 }
1067
1068 static GstFlowReturn
1069 _combine_flows (GstFlowReturn ret1, GstFlowReturn ret2)
1070 {
1071   if (ret1 == ret2)
1072     return ret1;
1073   if (ret1 <= GST_FLOW_NOT_NEGOTIATED)
1074     return ret1;
1075   if (ret2 <= GST_FLOW_NOT_NEGOTIATED)
1076     return ret2;
1077   if (ret1 == GST_FLOW_FLUSHING || ret2 == GST_FLOW_FLUSHING)
1078     return GST_FLOW_FLUSHING;
1079   if (ret1 == GST_FLOW_OK || ret2 == GST_FLOW_OK)
1080     return GST_FLOW_OK;
1081   return ret2;
1082 }
1083
1084 static void
1085 gst_validate_pad_monitor_check_aggregated_return (GstValidatePadMonitor *
1086     monitor, GstFlowReturn ret)
1087 {
1088   GstIterator *iter;
1089   gboolean done;
1090   GstPad *otherpad;
1091   GstPad *peerpad;
1092   GstValidatePadMonitor *othermonitor;
1093   GstFlowReturn aggregated = GST_FLOW_NOT_LINKED;
1094   gboolean found_a_pad = FALSE;
1095   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (monitor);
1096
1097   iter = gst_pad_iterate_internal_links (pad);
1098   done = FALSE;
1099   while (!done) {
1100     GValue value = { 0, };
1101     switch (gst_iterator_next (iter, &value)) {
1102       case GST_ITERATOR_OK:
1103         otherpad = g_value_get_object (&value);
1104         peerpad = gst_pad_get_peer (otherpad);
1105         if (peerpad) {
1106           othermonitor =
1107               g_object_get_data ((GObject *) peerpad, "validate-monitor");
1108           if (othermonitor) {
1109             found_a_pad = TRUE;
1110             GST_VALIDATE_MONITOR_LOCK (othermonitor);
1111             aggregated =
1112                 _combine_flows (aggregated, othermonitor->last_flow_return);
1113             GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1114           }
1115
1116           gst_object_unref (peerpad);
1117         }
1118         g_value_reset (&value);
1119         break;
1120       case GST_ITERATOR_RESYNC:
1121         gst_iterator_resync (iter);
1122         break;
1123       case GST_ITERATOR_ERROR:
1124         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
1125         done = TRUE;
1126         break;
1127       case GST_ITERATOR_DONE:
1128         done = TRUE;
1129         break;
1130     }
1131   }
1132   gst_iterator_free (iter);
1133   if (!found_a_pad) {
1134     /* no peer pad found, nothing to do */
1135     return;
1136   }
1137   if (aggregated == GST_FLOW_OK || aggregated == GST_FLOW_EOS) {
1138     /* those are acceptable situations */
1139
1140     if (GST_PAD_IS_FLUSHING (pad) && ret == GST_FLOW_FLUSHING) {
1141       /* pad is flushing, always acceptable to return flushing */
1142       return;
1143     }
1144
1145     if (monitor->is_eos && ret == GST_FLOW_EOS) {
1146       /* this element received eos and returned eos */
1147       return;
1148     }
1149
1150     if (PAD_PARENT_IS_DEMUXER (monitor) && ret == GST_FLOW_EOS) {
1151       /* a demuxer can return EOS when the samples end */
1152       return;
1153     }
1154   }
1155
1156   if (aggregated != ret) {
1157     GST_VALIDATE_REPORT (monitor, WRONG_FLOW_RETURN,
1158         "Wrong combined flow return %s(%d). Expected: %s(%d)",
1159         gst_flow_get_name (ret), ret, gst_flow_get_name (aggregated),
1160         aggregated);
1161   }
1162 }
1163
1164 static void
1165     gst_validate_pad_monitor_otherpad_add_pending_serialized_event
1166     (GstValidatePadMonitor * monitor, GstEvent * event, GstClockTime last_ts)
1167 {
1168   GstIterator *iter;
1169   gboolean done;
1170   GstPad *otherpad;
1171   GstValidatePadMonitor *othermonitor;
1172
1173   if (!GST_EVENT_IS_SERIALIZED (event))
1174     return;
1175
1176   iter =
1177       gst_pad_iterate_internal_links (GST_VALIDATE_PAD_MONITOR_GET_PAD
1178       (monitor));
1179   if (iter == NULL) {
1180     /* inputselector will return NULL if the sinkpad is not the active one .... */
1181     GST_FIXME_OBJECT (GST_VALIDATE_PAD_MONITOR_GET_PAD
1182         (monitor), "No iterator");
1183     return;
1184   }
1185   done = FALSE;
1186   while (!done) {
1187     GValue value = { 0, };
1188     switch (gst_iterator_next (iter, &value)) {
1189       case GST_ITERATOR_OK:
1190         otherpad = g_value_get_object (&value);
1191         othermonitor =
1192             g_object_get_data ((GObject *) otherpad, "validate-monitor");
1193         if (othermonitor) {
1194           SerializedEventData *data = g_slice_new0 (SerializedEventData);
1195           data->timestamp = last_ts;
1196           data->event = gst_event_ref (event);
1197           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1198           GST_DEBUG_OBJECT (monitor->pad, "Storing for pad %s:%s event %p %s",
1199               GST_DEBUG_PAD_NAME (otherpad), event,
1200               GST_EVENT_TYPE_NAME (event));
1201           g_ptr_array_add (othermonitor->serialized_events, data);
1202           debug_pending_event (otherpad, othermonitor->serialized_events);
1203           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1204         }
1205         g_value_reset (&value);
1206         break;
1207       case GST_ITERATOR_RESYNC:
1208         gst_iterator_resync (iter);
1209         break;
1210       case GST_ITERATOR_ERROR:
1211         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
1212         done = TRUE;
1213         break;
1214       case GST_ITERATOR_DONE:
1215         done = TRUE;
1216         break;
1217     }
1218   }
1219   gst_iterator_free (iter);
1220 }
1221
1222 static void
1223 gst_validate_pad_monitor_otherpad_add_pending_field (GstValidatePadMonitor *
1224     monitor, GstStructure * structure, const gchar * field)
1225 {
1226   GstIterator *iter;
1227   gboolean done;
1228   GstPad *otherpad;
1229   GstValidatePadMonitor *othermonitor;
1230   const GValue *v;
1231
1232   v = gst_structure_get_value (structure, field);
1233   if (v == NULL) {
1234     GST_DEBUG_OBJECT (monitor->pad, "Not adding pending field %s as it isn't "
1235         "present on structure %" GST_PTR_FORMAT, field, structure);
1236     return;
1237   }
1238
1239   iter =
1240       gst_pad_iterate_internal_links (GST_VALIDATE_PAD_MONITOR_GET_PAD
1241       (monitor));
1242   done = FALSE;
1243   while (!done) {
1244     GValue value = { 0, };
1245     switch (gst_iterator_next (iter, &value)) {
1246       case GST_ITERATOR_OK:
1247         otherpad = g_value_get_object (&value);
1248         othermonitor =
1249             g_object_get_data ((GObject *) otherpad, "validate-monitor");
1250         if (othermonitor) {
1251           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1252           g_assert (othermonitor->pending_setcaps_fields != NULL);
1253           gst_structure_set_value (othermonitor->pending_setcaps_fields,
1254               field, v);
1255           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1256         }
1257         g_value_reset (&value);
1258         break;
1259       case GST_ITERATOR_RESYNC:
1260         gst_iterator_resync (iter);
1261         break;
1262       case GST_ITERATOR_ERROR:
1263         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
1264         done = TRUE;
1265         break;
1266       case GST_ITERATOR_DONE:
1267         done = TRUE;
1268         break;
1269     }
1270   }
1271   gst_iterator_free (iter);
1272 }
1273
1274 static void
1275 gst_validate_pad_monitor_otherpad_clear_pending_fields (GstValidatePadMonitor *
1276     monitor)
1277 {
1278   GstIterator *iter;
1279   gboolean done;
1280   GstPad *otherpad;
1281   GstValidatePadMonitor *othermonitor;
1282
1283   iter =
1284       gst_pad_iterate_internal_links (GST_VALIDATE_PAD_MONITOR_GET_PAD
1285       (monitor));
1286
1287   if (iter == NULL) {
1288     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1289
1290     return;
1291   }
1292
1293   done = FALSE;
1294   while (!done) {
1295     GValue value = { 0, };
1296     switch (gst_iterator_next (iter, &value)) {
1297       case GST_ITERATOR_OK:
1298         otherpad = g_value_get_object (&value);
1299         othermonitor =
1300             g_object_get_data ((GObject *) otherpad, "validate-monitor");
1301         if (othermonitor) {
1302           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1303           g_assert (othermonitor->pending_setcaps_fields != NULL);
1304           gst_structure_free (othermonitor->pending_setcaps_fields);
1305           othermonitor->pending_setcaps_fields =
1306               gst_structure_new_empty (PENDING_FIELDS);
1307           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1308         }
1309         g_value_reset (&value);
1310         break;
1311       case GST_ITERATOR_RESYNC:
1312         gst_iterator_resync (iter);
1313         break;
1314       case GST_ITERATOR_ERROR:
1315         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
1316         done = TRUE;
1317         break;
1318       case GST_ITERATOR_DONE:
1319         done = TRUE;
1320         break;
1321     }
1322   }
1323   gst_iterator_free (iter);
1324 }
1325
1326 static void
1327 gst_validate_pad_monitor_add_expected_newsegment (GstValidatePadMonitor *
1328     monitor, GstEvent * event)
1329 {
1330   GstIterator *iter;
1331   gboolean done;
1332   GstPad *otherpad;
1333   GstValidatePadMonitor *othermonitor;
1334
1335   iter =
1336       gst_pad_iterate_internal_links (GST_VALIDATE_PAD_MONITOR_GET_PAD
1337       (monitor));
1338
1339   if (iter == NULL) {
1340     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1341     return;
1342   }
1343
1344   done = FALSE;
1345   while (!done) {
1346     GValue value = { 0, };
1347     switch (gst_iterator_next (iter, &value)) {
1348       case GST_ITERATOR_OK:
1349         otherpad = g_value_get_object (&value);
1350         if (!otherpad)
1351           continue;
1352         othermonitor =
1353             g_object_get_data ((GObject *) otherpad, "validate-monitor");
1354         GST_VALIDATE_MONITOR_LOCK (othermonitor);
1355         gst_event_replace (&othermonitor->expected_segment, event);
1356         GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1357         g_value_reset (&value);
1358         break;
1359       case GST_ITERATOR_RESYNC:
1360         gst_iterator_resync (iter);
1361         break;
1362       case GST_ITERATOR_ERROR:
1363         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
1364         done = TRUE;
1365         break;
1366       case GST_ITERATOR_DONE:
1367         done = TRUE;
1368         break;
1369     }
1370   }
1371   gst_iterator_free (iter);
1372 }
1373
1374 static void
1375 gst_validate_pad_monitor_flush (GstValidatePadMonitor * pad_monitor)
1376 {
1377   pad_monitor->current_timestamp = GST_CLOCK_TIME_NONE;
1378   pad_monitor->current_duration = GST_CLOCK_TIME_NONE;
1379   pad_monitor->timestamp_range_start = GST_CLOCK_TIME_NONE;
1380   pad_monitor->timestamp_range_end = GST_CLOCK_TIME_NONE;
1381   pad_monitor->has_segment = FALSE;
1382   pad_monitor->is_eos = FALSE;
1383   pad_monitor->last_flow_return = GST_FLOW_OK;
1384   gst_caps_replace (&pad_monitor->last_caps, NULL);
1385   pad_monitor->caps_is_audio = pad_monitor->caps_is_video =
1386       pad_monitor->caps_is_raw = FALSE;
1387
1388   g_list_free_full (pad_monitor->expired_events,
1389       (GDestroyNotify) gst_event_unref);
1390   pad_monitor->expired_events = NULL;
1391
1392   if (pad_monitor->serialized_events->len)
1393     g_ptr_array_remove_range (pad_monitor->serialized_events, 0,
1394         pad_monitor->serialized_events->len);
1395 }
1396
1397 /* common checks for both sink and src event functions */
1398 static void
1399 gst_validate_pad_monitor_common_event_check (GstValidatePadMonitor *
1400     pad_monitor, GstEvent * event)
1401 {
1402   guint32 seqnum = gst_event_get_seqnum (event);
1403
1404   switch (GST_EVENT_TYPE (event)) {
1405     case GST_EVENT_FLUSH_START:
1406     {
1407       if (pad_monitor->pending_flush_start_seqnum) {
1408         if (seqnum == pad_monitor->pending_flush_start_seqnum) {
1409           pad_monitor->pending_flush_start_seqnum = 0;
1410         } else {
1411           GST_VALIDATE_REPORT (pad_monitor, EVENT_HAS_WRONG_SEQNUM,
1412               "The expected flush-start seqnum should be the same as the "
1413               "one from the event that caused it (probably a seek). Got: %u."
1414               " Expected: %u", seqnum, pad_monitor->pending_flush_start_seqnum);
1415         }
1416       }
1417
1418       if (pad_monitor->pending_flush_stop) {
1419         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_START_UNEXPECTED,
1420             "Received flush-start from " " when flush-stop was expected");
1421       }
1422       pad_monitor->pending_flush_stop = TRUE;
1423     }
1424       break;
1425     case GST_EVENT_FLUSH_STOP:
1426     {
1427       if (pad_monitor->pending_flush_stop_seqnum) {
1428         if (seqnum == pad_monitor->pending_flush_stop_seqnum) {
1429           pad_monitor->pending_flush_stop_seqnum = 0;
1430         } else {
1431           GST_VALIDATE_REPORT (pad_monitor, EVENT_HAS_WRONG_SEQNUM,
1432               "The expected flush-stop seqnum should be the same as the "
1433               "one from the event that caused it (probably a seek). Got: %u."
1434               " Expected: %u", seqnum, pad_monitor->pending_flush_stop_seqnum);
1435         }
1436       }
1437
1438       if (!pad_monitor->pending_flush_stop) {
1439         gchar *event_str = _get_event_string (event);
1440
1441         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_STOP_UNEXPECTED,
1442             "Unexpected flush-stop %s", event_str);
1443         g_free (event_str);
1444       }
1445       pad_monitor->pending_flush_stop = FALSE;
1446
1447       /* cleanup our data */
1448       gst_validate_pad_monitor_flush (pad_monitor);
1449     }
1450       break;
1451     default:
1452       break;
1453   }
1454 }
1455
1456 static void
1457 mark_pads_eos (GstValidatePadMonitor *pad_monitor)
1458 {
1459   GstValidatePadMonitor *peer_monitor;
1460   GstPad *peer = gst_pad_get_peer (pad_monitor->pad);
1461   GstPad *real_peer;
1462
1463   pad_monitor->is_eos = TRUE;
1464   if (peer) {
1465     real_peer = _get_actual_pad (peer);
1466     peer_monitor = g_object_get_data ((GObject *) real_peer, "validate-monitor");
1467     if (peer_monitor)
1468       peer_monitor->is_eos = TRUE;
1469     gst_object_unref (peer);
1470     gst_object_unref (real_peer);
1471   }
1472 }
1473
1474 static inline gboolean
1475 _should_check_buffers (GstValidatePadMonitor * pad_monitor,
1476     gboolean force_checks)
1477 {
1478   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1479   GstValidateMonitor *monitor = GST_VALIDATE_MONITOR (pad_monitor);
1480
1481   if (pad_monitor->first_buffer || force_checks) {
1482     if (pad_monitor->segment.rate != 1.0) {
1483       GST_INFO_OBJECT (pad_monitor, "We do not support buffer checking"
1484           " for trick modes");
1485
1486       pad_monitor->check_buffers = FALSE;
1487     } else if (!PAD_PARENT_IS_DECODER (pad_monitor)) {
1488       GST_DEBUG_OBJECT (pad, "Not on a decoder => no buffer checking");
1489
1490       pad_monitor->check_buffers = FALSE;
1491     } else if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK) {
1492       GST_DEBUG_OBJECT (pad, "Not a sinkpad => no buffer checking");
1493
1494       pad_monitor->check_buffers = FALSE;
1495     } else if (!pad_monitor->caps_is_video) {
1496       GST_DEBUG_OBJECT (pad, "Not working with video => no buffer checking");
1497
1498       pad_monitor->check_buffers = FALSE;
1499     } else if (monitor->media_descriptor == NULL) {
1500       GST_DEBUG_OBJECT (pad, "No media_descriptor set => no buffer checking");
1501
1502       pad_monitor->check_buffers = FALSE;
1503     } else if (!gst_media_descriptor_detects_frames (monitor->media_descriptor)) {
1504       GST_DEBUG_OBJECT (pad, "No frame detection media descriptor "
1505           "=> not buffer checking");
1506       pad_monitor->check_buffers = FALSE;
1507     } else if (pad_monitor->all_bufs == NULL &&
1508         !gst_media_descriptor_get_buffers (monitor->media_descriptor, pad, NULL,
1509             &pad_monitor->all_bufs)) {
1510
1511       GST_INFO_OBJECT (monitor,
1512           "The MediaInfo is marked as detecting frame, but getting frames"
1513           " from pad %" GST_PTR_FORMAT " did not work (some format conversion"
1514           " might be happening)", pad);
1515
1516       pad_monitor->check_buffers = FALSE;
1517     } else {
1518       if (!pad_monitor->current_buf)
1519         pad_monitor->current_buf = pad_monitor->all_bufs;
1520       pad_monitor->check_buffers = TRUE;
1521     }
1522   }
1523
1524   return pad_monitor->check_buffers;
1525 }
1526
1527 static void
1528 gst_validate_monitor_find_next_buffer (GstValidatePadMonitor * pad_monitor)
1529 {
1530   GList *tmp;
1531   gboolean passed_start = FALSE;
1532
1533   if (!_should_check_buffers (pad_monitor, TRUE))
1534     return;
1535
1536   for (tmp = g_list_last (pad_monitor->all_bufs); tmp; tmp = tmp->prev) {
1537     GstBuffer *cbuf = tmp->data;
1538     GstClockTime ts =
1539         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cbuf)) ? GST_BUFFER_DTS (cbuf)
1540         : GST_BUFFER_PTS (cbuf);
1541
1542     if (!GST_CLOCK_TIME_IS_VALID (ts))
1543       continue;
1544
1545     if (ts <= pad_monitor->segment.start)
1546       passed_start = TRUE;
1547
1548     if (!passed_start)
1549       continue;
1550
1551     if (!GST_BUFFER_FLAG_IS_SET (cbuf, GST_BUFFER_FLAG_DELTA_UNIT)) {
1552       break;
1553     }
1554   }
1555
1556   if (tmp == NULL)
1557     pad_monitor->current_buf = pad_monitor->all_bufs;
1558   else
1559     pad_monitor->current_buf = tmp;
1560 }
1561
1562 static gboolean
1563 gst_validate_pad_monitor_downstream_event_check (GstValidatePadMonitor *
1564     pad_monitor, GstObject * parent, GstEvent * event,
1565     GstPadEventFunction handler)
1566 {
1567   gboolean ret = TRUE;
1568   const GstSegment *segment;
1569   guint32 seqnum = gst_event_get_seqnum (event);
1570   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1571
1572   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
1573
1574   /* pre checks */
1575   switch (GST_EVENT_TYPE (event)) {
1576     case GST_EVENT_SEGMENT:
1577       /* parse segment data to be used if event is handled */
1578       gst_event_parse_segment (event, &segment);
1579
1580       GST_DEBUG_OBJECT (pad_monitor->pad, "Got segment %" GST_SEGMENT_FORMAT,
1581           segment);
1582
1583       if (pad_monitor->pending_newsegment_seqnum) {
1584         if (pad_monitor->pending_newsegment_seqnum == seqnum) {
1585           pad_monitor->pending_newsegment_seqnum = 0;
1586         } else {
1587           /* TODO is this an error? could be a segment from the start
1588            * received just before the seek segment */
1589         }
1590       }
1591
1592       /* got a segment, no need for EOS now */
1593       pad_monitor->pending_eos_seqnum = 0;
1594
1595       if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
1596         gst_validate_pad_monitor_add_expected_newsegment (pad_monitor, event);
1597       } else {
1598         /* check if this segment is the expected one */
1599         if (pad_monitor->expected_segment) {
1600           const GstSegment *exp_segment;
1601
1602           if (pad_monitor->expected_segment != event) {
1603             gst_event_parse_segment (pad_monitor->expected_segment,
1604                 &exp_segment);
1605             if (segment->format == exp_segment->format) {
1606               if ((exp_segment->rate * exp_segment->applied_rate !=
1607                       segment->rate * segment->applied_rate)
1608                   || exp_segment->start != segment->start
1609                   || exp_segment->stop != segment->stop
1610                   || exp_segment->position != segment->position) {
1611                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1612                     "Expected segment didn't match received segment event");
1613               }
1614             }
1615           }
1616           gst_event_replace (&pad_monitor->expected_segment, NULL);
1617         }
1618       }
1619       break;
1620     case GST_EVENT_CAPS:{
1621       GstCaps *caps;
1622
1623       gst_event_parse_caps (event, &caps);
1624       gst_validate_pad_monitor_setcaps_pre (pad_monitor, caps);
1625       break;
1626     }
1627     case GST_EVENT_EOS:
1628       pad_monitor->is_eos = TRUE;
1629       if (pad_monitor->pending_eos_seqnum &&
1630           pad_monitor->pending_eos_seqnum != seqnum) {
1631         GST_VALIDATE_REPORT (pad_monitor, EVENT_HAS_WRONG_SEQNUM,
1632             "The expected EOS seqnum should be the same as the "
1633             "one from the seek that caused it. Got: %u."
1634             " Expected: %u", seqnum, pad_monitor->pending_eos_seqnum);
1635       }
1636       /*
1637        * TODO add end of stream checks for
1638        *  - events not pushed
1639        *  - buffer data not pushed
1640        *  - pending events not received
1641        */
1642       break;
1643
1644       /* both flushes are handled by the common event function */
1645     case GST_EVENT_FLUSH_START:
1646     case GST_EVENT_FLUSH_STOP:
1647     case GST_EVENT_TAG:
1648     case GST_EVENT_SINK_MESSAGE:
1649     default:
1650       break;
1651   }
1652
1653   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1654   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1655   gst_validate_pad_monitor_event_overrides (pad_monitor, event);
1656   if (handler) {
1657     gst_event_ref (event);
1658     ret = pad_monitor->event_func (pad, parent, event);
1659   }
1660   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1661   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1662
1663   /* post checks */
1664   switch (GST_EVENT_TYPE (event)) {
1665     case GST_EVENT_SEGMENT:
1666       if (ret) {
1667         if (!pad_monitor->has_segment
1668             && pad_monitor->segment.format != segment->format) {
1669           gst_segment_init (&pad_monitor->segment, segment->format);
1670         }
1671         gst_segment_copy_into (segment, &pad_monitor->segment);
1672         pad_monitor->has_segment = TRUE;
1673         gst_validate_monitor_find_next_buffer (pad_monitor);
1674       }
1675       break;
1676     case GST_EVENT_CAPS:{
1677       GstCaps *caps;
1678
1679       gst_event_parse_caps (event, &caps);
1680       gst_validate_pad_monitor_setcaps_post (pad_monitor, caps, ret);
1681       break;
1682     }
1683     case GST_EVENT_FLUSH_START:
1684     case GST_EVENT_FLUSH_STOP:
1685     case GST_EVENT_EOS:
1686     case GST_EVENT_TAG:
1687     case GST_EVENT_SINK_MESSAGE:
1688     default:
1689       break;
1690   }
1691
1692   if (handler)
1693     gst_event_unref (event);
1694   return ret;
1695 }
1696
1697 static gboolean
1698 gst_validate_pad_monitor_src_event_check (GstValidatePadMonitor * pad_monitor,
1699     GstObject * parent, GstEvent * event, GstPadEventFunction handler)
1700 {
1701   gboolean ret = TRUE;
1702   gdouble rate;
1703   GstFormat format;
1704   gint64 start, stop;
1705   GstSeekFlags seek_flags;
1706   GstSeekType start_type, stop_type;
1707   guint32 seqnum = gst_event_get_seqnum (event);
1708   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1709
1710   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
1711
1712   /* pre checks */
1713   switch (GST_EVENT_TYPE (event)) {
1714     case GST_EVENT_SEEK:
1715     {
1716       gst_event_parse_seek (event, &rate, &format, &seek_flags, &start_type,
1717           &start, &stop_type, &stop);
1718       /* upstream seek - store the seek event seqnum to check
1719        * flushes and newsegments share the same */
1720
1721       /* TODO we might need to use a list as multiple seeks can be sent
1722        * before the flushes arrive here */
1723       if (seek_flags & GST_SEEK_FLAG_FLUSH) {
1724         pad_monitor->pending_flush_start_seqnum = seqnum;
1725         pad_monitor->pending_flush_stop_seqnum = seqnum;
1726       }
1727       pad_monitor->pending_newsegment_seqnum = seqnum;
1728       pad_monitor->pending_eos_seqnum = seqnum;
1729     }
1730       break;
1731       /* both flushes are handled by the common event handling function */
1732     case GST_EVENT_FLUSH_START:
1733     case GST_EVENT_FLUSH_STOP:
1734     case GST_EVENT_NAVIGATION:
1735     case GST_EVENT_LATENCY:
1736     case GST_EVENT_STEP:
1737     case GST_EVENT_QOS:
1738     default:
1739       break;
1740   }
1741
1742   if (handler) {
1743     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1744     gst_event_ref (event);
1745     ret = pad_monitor->event_func (pad, parent, event);
1746     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1747   }
1748
1749   /* post checks */
1750   switch (GST_EVENT_TYPE (event)) {
1751     case GST_EVENT_FLUSH_START:
1752     case GST_EVENT_FLUSH_STOP:
1753     case GST_EVENT_QOS:
1754     case GST_EVENT_SEEK:
1755     {
1756       if (ret == FALSE) {
1757         /* do not expect any of these events anymore */
1758         pad_monitor->pending_flush_start_seqnum = 0;
1759         pad_monitor->pending_flush_stop_seqnum = 0;
1760         pad_monitor->pending_newsegment_seqnum = 0;
1761         pad_monitor->pending_eos_seqnum = 0;
1762       }
1763     }
1764       break;
1765     case GST_EVENT_NAVIGATION:
1766     case GST_EVENT_LATENCY:
1767     case GST_EVENT_STEP:
1768     default:
1769       break;
1770   }
1771
1772   if (handler)
1773     gst_event_unref (event);
1774   return ret;
1775 }
1776
1777 static gboolean
1778 gst_validate_pad_monitor_check_right_buffer (GstValidatePadMonitor *
1779     pad_monitor, GstBuffer * buffer)
1780 {
1781   gchar *checksum;
1782   GstBuffer *wanted_buf;
1783   GstMapInfo map, wanted_map;
1784
1785   gboolean ret = TRUE;
1786   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1787
1788   if (_should_check_buffers (pad_monitor, FALSE) == FALSE)
1789     return FALSE;
1790
1791   if (pad_monitor->current_buf == NULL) {
1792     GST_INFO_OBJECT (pad, "No current buffer one pad, Why?");
1793     return FALSE;
1794   }
1795
1796   wanted_buf = pad_monitor->current_buf->data;
1797
1798   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (wanted_buf)) &&
1799       GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buffer)) &&
1800       GST_BUFFER_PTS (wanted_buf) != GST_BUFFER_PTS (buffer)) {
1801
1802     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1803         "buffer %" GST_PTR_FORMAT " PTS %ld"
1804         " different than expected: %ld", buffer,
1805         GST_BUFFER_PTS (buffer), GST_BUFFER_PTS (wanted_buf));
1806
1807     ret = FALSE;
1808   }
1809
1810   if (GST_BUFFER_DTS (wanted_buf) != GST_BUFFER_DTS (buffer)) {
1811     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1812         "buffer %" GST_PTR_FORMAT " DTS %" GST_TIME_FORMAT
1813         " different than expected: %" GST_TIME_FORMAT, buffer,
1814         GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
1815         GST_TIME_ARGS (GST_BUFFER_DTS (wanted_buf)));
1816     ret = FALSE;
1817   }
1818
1819   if (GST_BUFFER_DURATION (wanted_buf) != GST_BUFFER_DURATION (buffer)) {
1820     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1821         "buffer %" GST_PTR_FORMAT " DURATION %" GST_TIME_FORMAT
1822         " different than expected: %" GST_TIME_FORMAT, buffer,
1823         GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
1824         GST_TIME_ARGS (GST_BUFFER_DURATION (wanted_buf)));
1825     ret = FALSE;
1826   }
1827
1828   if (GST_BUFFER_FLAG_IS_SET (wanted_buf, GST_BUFFER_FLAG_DELTA_UNIT) !=
1829       GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
1830     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1831         "buffer %" GST_PTR_FORMAT "  Delta unit is set to %s but expected %s",
1832         buffer, GST_BUFFER_FLAG_IS_SET (buffer,
1833             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False",
1834         GST_BUFFER_FLAG_IS_SET (wanted_buf,
1835             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False");
1836     ret = FALSE;
1837   }
1838
1839   g_assert (gst_buffer_map (wanted_buf, &wanted_map, GST_MAP_READ));
1840   g_assert (gst_buffer_map (buffer, &map, GST_MAP_READ));
1841
1842   checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5,
1843       (const guchar *) map.data, map.size);
1844
1845   if (g_strcmp0 ((gchar *) wanted_map.data, checksum)) {
1846     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1847         "buffer %" GST_PTR_FORMAT " checksum %s different from expected: %s",
1848         buffer, checksum, wanted_map.data);
1849     ret = FALSE;
1850   }
1851
1852   gst_buffer_unmap (wanted_buf, &wanted_map);
1853   gst_buffer_unmap (buffer, &map);
1854   g_free (checksum);
1855
1856   pad_monitor->current_buf = pad_monitor->current_buf->next;
1857
1858   return ret;
1859 }
1860
1861 static GstFlowReturn
1862 gst_validate_pad_monitor_chain_func (GstPad * pad, GstObject * parent,
1863     GstBuffer * buffer)
1864 {
1865   GstValidatePadMonitor *pad_monitor =
1866       g_object_get_data ((GObject *) pad, "validate-monitor");
1867   GstFlowReturn ret;
1868
1869   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1870   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1871
1872   gst_validate_pad_monitor_check_right_buffer (pad_monitor, buffer);
1873   gst_validate_pad_monitor_check_first_buffer (pad_monitor, buffer);
1874   gst_validate_pad_monitor_update_buffer_data (pad_monitor, buffer);
1875   gst_validate_pad_monitor_check_eos (pad_monitor, buffer);
1876
1877   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1878   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1879
1880   gst_validate_pad_monitor_buffer_overrides (pad_monitor, buffer);
1881
1882   ret = pad_monitor->chain_func (pad, parent, buffer);
1883
1884   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1885   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1886
1887   pad_monitor->last_flow_return = ret;
1888   if (ret == GST_FLOW_EOS) {
1889     mark_pads_eos (pad_monitor);
1890   }
1891   if (PAD_PARENT_IS_DEMUXER (pad_monitor))
1892     gst_validate_pad_monitor_check_aggregated_return (pad_monitor, ret);
1893
1894   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1895   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1896
1897   return ret;
1898 }
1899
1900 static gboolean
1901 gst_validate_pad_monitor_event_is_tracked (GstValidatePadMonitor * monitor,
1902     GstEvent * event)
1903 {
1904   if (!GST_EVENT_IS_SERIALIZED (event)) {
1905     return FALSE;
1906   }
1907
1908   /* we don't track Tag events because they mutate too much and it is hard
1909    * to match a tag event pushed on a source pad with the one that was received
1910    * on a sink pad.
1911    * One idea would be to use seqnum, but it seems that it is undefined whether
1912    * seqnums should be maintained in tag events that are created from others
1913    * up to today. (2013-08-29)
1914    */
1915   if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
1916     return FALSE;
1917
1918   return TRUE;
1919 }
1920
1921 static gboolean
1922 gst_validate_pad_monitor_sink_event_func (GstPad * pad, GstObject * parent,
1923     GstEvent * event)
1924 {
1925   GstValidatePadMonitor *pad_monitor =
1926       g_object_get_data ((GObject *) pad, "validate-monitor");
1927   gboolean ret;
1928
1929   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1930   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1931
1932   if (gst_validate_pad_monitor_event_is_tracked (pad_monitor, event)) {
1933     GstClockTime last_ts = GST_CLOCK_TIME_NONE;
1934     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_timestamp)) {
1935       last_ts = pad_monitor->current_timestamp;
1936       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_duration)) {
1937         last_ts += pad_monitor->current_duration;
1938       }
1939     }
1940     gst_validate_pad_monitor_otherpad_add_pending_serialized_event (pad_monitor,
1941         event, last_ts);
1942   }
1943
1944   ret =
1945       gst_validate_pad_monitor_downstream_event_check (pad_monitor, parent,
1946       event, pad_monitor->event_func);
1947
1948   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1949   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1950   return ret;
1951 }
1952
1953 static gboolean
1954 gst_validate_pad_monitor_src_event_func (GstPad * pad, GstObject * parent,
1955     GstEvent * event)
1956 {
1957   GstValidatePadMonitor *pad_monitor =
1958       g_object_get_data ((GObject *) pad, "validate-monitor");
1959   gboolean ret;
1960
1961   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1962   ret = gst_validate_pad_monitor_src_event_check (pad_monitor, parent, event,
1963       pad_monitor->event_func);
1964   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1965   return ret;
1966 }
1967
1968 static gboolean
1969 gst_validate_pad_monitor_query_func (GstPad * pad, GstObject * parent,
1970     GstQuery * query)
1971 {
1972   GstValidatePadMonitor *pad_monitor =
1973       g_object_get_data ((GObject *) pad, "validate-monitor");
1974   gboolean ret;
1975
1976   gst_validate_pad_monitor_query_overrides (pad_monitor, query);
1977
1978   ret = pad_monitor->query_func (pad, parent, query);
1979
1980   if (ret) {
1981     switch (GST_QUERY_TYPE (query)) {
1982       case GST_QUERY_CAPS:{
1983         GstCaps *res;
1984         /* We shouldn't need to lock the parent as this doesn't modify
1985          * other monitors, just does some peer_pad_caps */
1986         GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1987
1988         gst_query_parse_caps_result (query, &res);
1989         if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
1990           gst_validate_pad_monitor_check_caps_fields_proxied (pad_monitor, res);
1991         }
1992         GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1993         break;
1994       }
1995       default:
1996         break;
1997     }
1998   }
1999
2000   return ret;
2001 }
2002
2003 static gboolean
2004 gst_validate_pad_monitor_activatemode_func (GstPad * pad, GstObject * parent,
2005     GstPadMode mode, gboolean active)
2006 {
2007   GstValidatePadMonitor *pad_monitor =
2008       g_object_get_data ((GObject *) pad, "validate-monitor");
2009   gboolean ret = TRUE;
2010
2011   /* TODO add overrides for activate func */
2012
2013   if (pad_monitor->activatemode_func)
2014     ret = pad_monitor->activatemode_func (pad, parent, mode, active);
2015   if (ret && active == FALSE) {
2016     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2017     gst_validate_pad_monitor_flush (pad_monitor);
2018     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2019   }
2020
2021   return ret;
2022 }
2023
2024 static gboolean
2025 gst_validate_pad_get_range_func (GstPad * pad, GstObject * parent,
2026     guint64 offset, guint size, GstBuffer ** buffer)
2027 {
2028   GstValidatePadMonitor *pad_monitor =
2029       g_object_get_data ((GObject *) pad, "validate-monitor");
2030   gboolean ret;
2031   ret = pad_monitor->getrange_func (pad, parent, offset, size, buffer);
2032   return ret;
2033 }
2034
2035 static gboolean
2036 gst_validate_pad_monitor_buffer_probe (GstPad * pad, GstBuffer * buffer,
2037     gpointer udata)
2038 {
2039   GstValidatePadMonitor *monitor = udata;
2040
2041   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2042   GST_VALIDATE_MONITOR_LOCK (monitor);
2043
2044   gst_validate_pad_monitor_check_first_buffer (monitor, buffer);
2045   gst_validate_pad_monitor_update_buffer_data (monitor, buffer);
2046   gst_validate_pad_monitor_check_eos (monitor, buffer);
2047
2048   if (PAD_PARENT_IS_DECODER (monitor) || PAD_PARENT_IS_ENCODER (monitor)) {
2049     GstClockTime tolerance = 0;
2050
2051     if (monitor->caps_is_audio)
2052       tolerance = AUDIO_TIMESTAMP_TOLERANCE;
2053
2054     gst_validate_pad_monitor_check_buffer_timestamp_in_received_range (monitor,
2055         buffer, tolerance);
2056   }
2057
2058   gst_validate_pad_monitor_check_late_serialized_events (monitor,
2059       GST_BUFFER_TIMESTAMP (buffer));
2060
2061   /* a GstValidatePadMonitor parent must be a GstValidateElementMonitor */
2062   if (PAD_PARENT_IS_DECODER (monitor)) {
2063
2064     /* should not push out of segment data */
2065     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer)) &&
2066         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer)) &&
2067         ((!gst_segment_clip (&monitor->segment, monitor->segment.format,
2068                     GST_BUFFER_TIMESTAMP (buffer),
2069                     GST_BUFFER_TIMESTAMP (buffer) +
2070                     GST_BUFFER_DURATION (buffer), NULL, NULL)) ||
2071             /* In the case of raw data, buffers should be strictly contained inside the
2072              * segment */
2073             (monitor->caps_is_raw &&
2074                 GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer) <
2075                 monitor->segment.start))
2076         ) {
2077       /* TODO is this a timestamp issue? */
2078       GST_VALIDATE_REPORT (monitor, BUFFER_IS_OUT_OF_SEGMENT,
2079           "buffer is out of segment and shouldn't be pushed. Timestamp: %"
2080           GST_TIME_FORMAT " - duration: %" GST_TIME_FORMAT ". Range: %"
2081           GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
2082           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2083           GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
2084           GST_TIME_ARGS (monitor->segment.start),
2085           GST_TIME_ARGS (monitor->segment.stop));
2086     }
2087   }
2088
2089   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2090   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2091   gst_validate_pad_monitor_buffer_probe_overrides (monitor, buffer);
2092   return TRUE;
2093 }
2094
2095 static gboolean
2096 gst_validate_pad_monitor_event_probe (GstPad * pad, GstEvent * event,
2097     gpointer udata)
2098 {
2099   GstValidatePadMonitor *monitor = GST_VALIDATE_PAD_MONITOR_CAST (udata);
2100   gboolean ret;
2101
2102   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2103   GST_VALIDATE_MONITOR_LOCK (monitor);
2104
2105   GST_DEBUG_OBJECT (pad, "event %p %s", event, GST_EVENT_TYPE_NAME (event));
2106
2107   if (GST_EVENT_IS_SERIALIZED (event)) {
2108     gint i;
2109
2110     /* Detect if events the element received are being forwarded in the same order
2111      *
2112      * Several scenarios:
2113      * 1) The element pushes the event as-is
2114      * 2) The element consumes the event and does not forward it
2115      * 3) The element consumes the event and creates another one instead
2116      * 4) The element pushes other serialized event before pushing out the
2117      *    one it received
2118      *
2119      * For each pad we have two lists to track serialized events:
2120      *  1) We received on input and expect to see (serialized_events)
2121      *  2) We received on input but don't expect to see (expired_events)
2122      *
2123      * To detect events that are pushed in a different order from the one they were
2124      * received in we check that:
2125      *
2126      * For each event being outputted:
2127      *   If it is in the expired_events list:
2128      *     RAISE WARNING
2129      *   If it is in the serialized_events list:
2130      *     If there are other events that were received before:
2131      *        Put those events on the expired_events list
2132      *     Remove that event and any previous ones from the serialized_events list
2133      *
2134      * Clear expired events list when flushing or on pad deactivation
2135      *
2136      */
2137
2138     if (g_list_find (monitor->expired_events, event)) {
2139       gchar *event_str = _get_event_string (event);
2140       /* If it's the expired events, we've failed */
2141       GST_WARNING_OBJECT (pad, "Did not expect event %p %s", event,
2142           GST_EVENT_TYPE_NAME (event));
2143       GST_VALIDATE_REPORT (monitor, EVENT_SERIALIZED_OUT_OF_ORDER,
2144           "Serialized event was pushed out of order: %s", event_str);
2145
2146       g_free (event_str);
2147       monitor->expired_events = g_list_remove (monitor->expired_events, event);
2148       gst_event_unref (event);  /* remove the ref that was on the list */
2149     } else if (monitor->serialized_events->len) {
2150       for (i = 0; i < monitor->serialized_events->len; i++) {
2151         SerializedEventData *next_event =
2152             g_ptr_array_index (monitor->serialized_events, i);
2153         GST_DEBUG_OBJECT (pad, "Checking against stored event #%d: %p %s", i,
2154             next_event->event, GST_EVENT_TYPE_NAME (next_event->event));
2155
2156         if (event == next_event->event
2157             || GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2158           /* We have found our event */
2159           GST_DEBUG_OBJECT (pad, "Found matching event");
2160
2161           while (monitor->serialized_events->len > i
2162               && GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2163             /* Swallow all expected events of the same type */
2164             g_ptr_array_remove_index (monitor->serialized_events, i);
2165             next_event = g_ptr_array_index (monitor->serialized_events, i);
2166           }
2167
2168           /* Move all previous events to expired events */
2169           if (G_UNLIKELY (i > 0)) {
2170             GST_DEBUG_OBJECT (pad,
2171                 "Moving previous expected events to expired list");
2172             while (i--) {
2173               next_event = g_ptr_array_index (monitor->serialized_events, 0);
2174               monitor->expired_events =
2175                   g_list_append (monitor->expired_events,
2176                   gst_event_ref (next_event->event));
2177               g_ptr_array_remove_index (monitor->serialized_events, 0);
2178             }
2179           }
2180           debug_pending_event (pad, monitor->serialized_events);
2181           break;
2182         }
2183       }
2184     }
2185   }
2186
2187   /* This so far is just like an event that is flowing downstream,
2188    * so we do the same checks as a sinkpad event handler */
2189   ret =
2190       gst_validate_pad_monitor_downstream_event_check (monitor, NULL, event,
2191       NULL);
2192   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2193   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2194
2195   return ret;
2196 }
2197
2198 static GstPadProbeReturn
2199 gst_validate_pad_monitor_pad_probe (GstPad * pad, GstPadProbeInfo * info,
2200     gpointer udata)
2201 {
2202   if (info->type & GST_PAD_PROBE_TYPE_BUFFER)
2203     gst_validate_pad_monitor_buffer_probe (pad, info->data, udata);
2204   else if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM)
2205     gst_validate_pad_monitor_event_probe (pad, info->data, udata);
2206
2207   return GST_PAD_PROBE_OK;
2208 }
2209
2210 static void
2211 gst_validate_pad_monitor_update_caps_info (GstValidatePadMonitor * pad_monitor,
2212     GstCaps * caps)
2213 {
2214   GstStructure *structure;
2215
2216   g_return_if_fail (gst_caps_is_fixed (caps));
2217
2218   pad_monitor->caps_is_audio = FALSE;
2219   pad_monitor->caps_is_video = FALSE;
2220
2221   structure = gst_caps_get_structure (caps, 0);
2222   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/")) {
2223     pad_monitor->caps_is_audio = TRUE;
2224   } else if (g_str_has_prefix (gst_structure_get_name (structure), "video/")) {
2225     pad_monitor->caps_is_video = TRUE;
2226   }
2227
2228   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/x-raw") ||
2229       g_str_has_prefix (gst_structure_get_name (structure), "video/x-raw")) {
2230     pad_monitor->caps_is_raw = TRUE;
2231   } else {
2232     pad_monitor->caps_is_raw = FALSE;
2233   }
2234 }
2235
2236 static void
2237 gst_validate_pad_monitor_setcaps_pre (GstValidatePadMonitor * pad_monitor,
2238     GstCaps * caps)
2239 {
2240   GstStructure *structure;
2241
2242   /* Check if caps are identical to last caps and complain if so
2243    * Only checked for sink pads as src pads might push the same caps
2244    * multiple times during unlinked/autoplugging scenarios */
2245   if (GST_PAD_IS_SINK (GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor)) &&
2246       pad_monitor->last_caps
2247       && gst_caps_is_equal (caps, pad_monitor->last_caps)) {
2248     gchar *caps_str = gst_caps_to_string (caps);
2249
2250     GST_VALIDATE_REPORT (pad_monitor, EVENT_CAPS_DUPLICATE, "%s", caps_str);
2251     g_free (caps_str);
2252
2253   }
2254
2255   gst_validate_pad_monitor_check_caps_complete (pad_monitor, caps);
2256
2257   if (caps) {
2258     structure = gst_caps_get_structure (caps, 0);
2259     if (gst_structure_n_fields (pad_monitor->pending_setcaps_fields)) {
2260       gint i;
2261       for (i = 0;
2262           i < gst_structure_n_fields (pad_monitor->pending_setcaps_fields);
2263           i++) {
2264         const gchar *name =
2265             gst_structure_nth_field_name (pad_monitor->pending_setcaps_fields,
2266             i);
2267         const GValue *v = gst_structure_get_value (structure, name);
2268         const GValue *otherv =
2269             gst_structure_get_value (pad_monitor->pending_setcaps_fields, name);
2270
2271         if (v == NULL) {
2272           gchar *caps_str = gst_caps_to_string (caps);
2273
2274           GST_VALIDATE_REPORT (pad_monitor, CAPS_EXPECTED_FIELD_NOT_FOUND,
2275               "Field %s is missing from setcaps caps '%s'", name, caps_str);
2276           g_free (caps_str);
2277         } else if (gst_value_compare (v, otherv) != GST_VALUE_EQUAL) {
2278           gchar *caps_str = gst_caps_to_string (caps),
2279               *pending_setcaps_fields_str =
2280               gst_structure_to_string (pad_monitor->pending_setcaps_fields);
2281
2282
2283           GST_VALIDATE_REPORT (pad_monitor, CAPS_FIELD_UNEXPECTED_VALUE,
2284               "Field %s from setcaps caps '%s' is different "
2285               "from expected value in caps '%s'", name, caps_str,
2286               pending_setcaps_fields_str);
2287
2288           g_free (pending_setcaps_fields_str);
2289           g_free (caps_str);
2290         }
2291       }
2292     }
2293
2294     if (gst_validate_pad_monitor_pad_should_proxy_othercaps (pad_monitor)) {
2295       if (_structure_is_video (structure)) {
2296         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2297             structure, "width");
2298         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2299             structure, "height");
2300         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2301             structure, "framerate");
2302         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2303             structure, "pixel-aspect-ratio");
2304       } else if (_structure_is_audio (structure)) {
2305         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2306             structure, "rate");
2307         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2308             structure, "channels");
2309       }
2310     }
2311   }
2312
2313   gst_structure_free (pad_monitor->pending_setcaps_fields);
2314   pad_monitor->pending_setcaps_fields =
2315       gst_structure_new_empty (PENDING_FIELDS);
2316
2317   gst_validate_pad_monitor_setcaps_overrides (pad_monitor, caps);
2318 }
2319
2320 static void
2321 gst_validate_pad_monitor_setcaps_post (GstValidatePadMonitor * pad_monitor,
2322     GstCaps * caps, gboolean ret)
2323 {
2324   if (!ret)
2325     gst_validate_pad_monitor_otherpad_clear_pending_fields (pad_monitor);
2326   else {
2327     if (pad_monitor->last_caps) {
2328       gst_caps_unref (pad_monitor->last_caps);
2329     }
2330     pad_monitor->last_caps = gst_caps_ref (caps);
2331     gst_validate_pad_monitor_update_caps_info (pad_monitor, caps);
2332   }
2333 }
2334
2335 static gboolean
2336 gst_validate_pad_monitor_do_setup (GstValidateMonitor * monitor)
2337 {
2338   GstValidatePadMonitor *pad_monitor = GST_VALIDATE_PAD_MONITOR_CAST (monitor);
2339   GstPad *pad;
2340   if (!GST_IS_PAD (GST_VALIDATE_MONITOR_GET_OBJECT (monitor))) {
2341     GST_WARNING_OBJECT (monitor, "Trying to create pad monitor with other "
2342         "type of object");
2343     return FALSE;
2344   }
2345
2346   pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
2347
2348   if (g_object_get_data ((GObject *) pad, "validate-monitor")) {
2349     GST_WARNING_OBJECT (pad_monitor,
2350         "Pad already has a validate-monitor associated");
2351     return FALSE;
2352   }
2353
2354   g_object_set_data ((GObject *) pad, "validate-monitor", pad_monitor);
2355
2356   pad_monitor->pad = pad;
2357
2358   pad_monitor->event_func = GST_PAD_EVENTFUNC (pad);
2359   pad_monitor->query_func = GST_PAD_QUERYFUNC (pad);
2360   pad_monitor->activatemode_func = GST_PAD_ACTIVATEMODEFUNC (pad);
2361   if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2362
2363     pad_monitor->chain_func = GST_PAD_CHAINFUNC (pad);
2364     if (pad_monitor->chain_func)
2365       gst_pad_set_chain_function (pad, gst_validate_pad_monitor_chain_func);
2366
2367     gst_pad_set_event_function (pad, gst_validate_pad_monitor_sink_event_func);
2368   } else {
2369     pad_monitor->getrange_func = GST_PAD_GETRANGEFUNC (pad);
2370     if (pad_monitor->getrange_func)
2371       gst_pad_set_getrange_function (pad, gst_validate_pad_get_range_func);
2372
2373     gst_pad_set_event_function (pad, gst_validate_pad_monitor_src_event_func);
2374
2375     /* add buffer/event probes */
2376     pad_monitor->pad_probe_id =
2377         gst_pad_add_probe (pad,
2378         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
2379         GST_PAD_PROBE_TYPE_EVENT_FLUSH,
2380         (GstPadProbeCallback) gst_validate_pad_monitor_pad_probe, pad_monitor,
2381         NULL);
2382   }
2383   gst_pad_set_query_function (pad, gst_validate_pad_monitor_query_func);
2384   gst_pad_set_activatemode_function (pad,
2385       gst_validate_pad_monitor_activatemode_func);
2386
2387   gst_validate_reporter_set_name (GST_VALIDATE_REPORTER (monitor),
2388       g_strdup_printf ("%s:%s", GST_DEBUG_PAD_NAME (pad)));
2389
2390   if (G_UNLIKELY (GST_PAD_PARENT (pad) == NULL))
2391     GST_FIXME ("Saw a pad not belonging to any object");
2392
2393   return TRUE;
2394 }