validate: Do not check if first buffer running time is 0
[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   GstValidateReportingDetails 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_SHOW_NONE:
278       ret = GST_VALIDATE_REPORTER_DROP;
279       break;
280     case GST_VALIDATE_SHOW_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   }
1014 }
1015
1016 static void
1017 gst_validate_pad_monitor_check_eos (GstValidatePadMonitor *
1018     pad_monitor, GstBuffer * buffer)
1019 {
1020   if (G_UNLIKELY (pad_monitor->is_eos)) {
1021     GST_VALIDATE_REPORT (pad_monitor, BUFFER_AFTER_EOS,
1022         "Received buffer %" GST_PTR_FORMAT " after EOS", buffer);
1023   }
1024 }
1025
1026 static void
1027 gst_validate_pad_monitor_update_buffer_data (GstValidatePadMonitor *
1028     pad_monitor, GstBuffer * buffer)
1029 {
1030   pad_monitor->current_timestamp = GST_BUFFER_TIMESTAMP (buffer);
1031   pad_monitor->current_duration = GST_BUFFER_DURATION (buffer);
1032   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) {
1033     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->timestamp_range_start)) {
1034       pad_monitor->timestamp_range_start =
1035           MIN (pad_monitor->timestamp_range_start,
1036           GST_BUFFER_TIMESTAMP (buffer));
1037     } else {
1038       pad_monitor->timestamp_range_start = GST_BUFFER_TIMESTAMP (buffer);
1039     }
1040
1041     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) {
1042       GstClockTime endts =
1043           GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1044       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->timestamp_range_end)) {
1045         pad_monitor->timestamp_range_end =
1046             MAX (pad_monitor->timestamp_range_end, endts);
1047       } else {
1048         pad_monitor->timestamp_range_end = endts;
1049       }
1050     }
1051   }
1052   GST_DEBUG_OBJECT (pad_monitor->pad, "Current stored range: %" GST_TIME_FORMAT
1053       " - %" GST_TIME_FORMAT,
1054       GST_TIME_ARGS (pad_monitor->timestamp_range_start),
1055       GST_TIME_ARGS (pad_monitor->timestamp_range_end));
1056 }
1057
1058 static GstFlowReturn
1059 _combine_flows (GstFlowReturn ret1, GstFlowReturn ret2)
1060 {
1061   if (ret1 == ret2)
1062     return ret1;
1063   if (ret1 <= GST_FLOW_NOT_NEGOTIATED)
1064     return ret1;
1065   if (ret2 <= GST_FLOW_NOT_NEGOTIATED)
1066     return ret2;
1067   if (ret1 == GST_FLOW_FLUSHING || ret2 == GST_FLOW_FLUSHING)
1068     return GST_FLOW_FLUSHING;
1069   if (ret1 == GST_FLOW_OK || ret2 == GST_FLOW_OK)
1070     return GST_FLOW_OK;
1071   return ret2;
1072 }
1073
1074 static void
1075 gst_validate_pad_monitor_check_aggregated_return (GstValidatePadMonitor *
1076     monitor, GstFlowReturn ret)
1077 {
1078   GstIterator *iter;
1079   gboolean done;
1080   GstPad *otherpad;
1081   GstPad *peerpad;
1082   GstValidatePadMonitor *othermonitor;
1083   GstFlowReturn aggregated = GST_FLOW_NOT_LINKED;
1084   gboolean found_a_pad = FALSE;
1085   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (monitor);
1086
1087   iter = gst_pad_iterate_internal_links (pad);
1088   done = FALSE;
1089   while (!done) {
1090     GValue value = { 0, };
1091     switch (gst_iterator_next (iter, &value)) {
1092       case GST_ITERATOR_OK:
1093         otherpad = g_value_get_object (&value);
1094         peerpad = gst_pad_get_peer (otherpad);
1095         if (peerpad) {
1096           othermonitor =
1097               g_object_get_data ((GObject *) peerpad, "validate-monitor");
1098           if (othermonitor) {
1099             found_a_pad = TRUE;
1100             GST_VALIDATE_MONITOR_LOCK (othermonitor);
1101             aggregated =
1102                 _combine_flows (aggregated, othermonitor->last_flow_return);
1103             GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1104           }
1105
1106           gst_object_unref (peerpad);
1107         }
1108         g_value_reset (&value);
1109         break;
1110       case GST_ITERATOR_RESYNC:
1111         gst_iterator_resync (iter);
1112         break;
1113       case GST_ITERATOR_ERROR:
1114         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
1115         done = TRUE;
1116         break;
1117       case GST_ITERATOR_DONE:
1118         done = TRUE;
1119         break;
1120     }
1121   }
1122   gst_iterator_free (iter);
1123   if (!found_a_pad) {
1124     /* no peer pad found, nothing to do */
1125     return;
1126   }
1127   if (aggregated == GST_FLOW_OK || aggregated == GST_FLOW_EOS) {
1128     /* those are acceptable situations */
1129
1130     if (GST_PAD_IS_FLUSHING (pad) && ret == GST_FLOW_FLUSHING) {
1131       /* pad is flushing, always acceptable to return flushing */
1132       return;
1133     }
1134
1135     if (monitor->is_eos && ret == GST_FLOW_EOS) {
1136       /* this element received eos and returned eos */
1137       return;
1138     }
1139
1140     if (PAD_PARENT_IS_DEMUXER (monitor) && ret == GST_FLOW_EOS) {
1141       /* a demuxer can return EOS when the samples end */
1142       return;
1143     }
1144   }
1145
1146   if (aggregated != ret) {
1147     GST_VALIDATE_REPORT (monitor, WRONG_FLOW_RETURN,
1148         "Wrong combined flow return %s(%d). Expected: %s(%d)",
1149         gst_flow_get_name (ret), ret, gst_flow_get_name (aggregated),
1150         aggregated);
1151   }
1152 }
1153
1154 static void
1155     gst_validate_pad_monitor_otherpad_add_pending_serialized_event
1156     (GstValidatePadMonitor * monitor, GstEvent * event, GstClockTime last_ts)
1157 {
1158   GstIterator *iter;
1159   gboolean done;
1160   GstPad *otherpad;
1161   GstValidatePadMonitor *othermonitor;
1162
1163   if (!GST_EVENT_IS_SERIALIZED (event))
1164     return;
1165
1166   iter =
1167       gst_pad_iterate_internal_links (GST_VALIDATE_PAD_MONITOR_GET_PAD
1168       (monitor));
1169   if (iter == NULL) {
1170     /* inputselector will return NULL if the sinkpad is not the active one .... */
1171     GST_FIXME_OBJECT (GST_VALIDATE_PAD_MONITOR_GET_PAD
1172         (monitor), "No iterator");
1173     return;
1174   }
1175   done = FALSE;
1176   while (!done) {
1177     GValue value = { 0, };
1178     switch (gst_iterator_next (iter, &value)) {
1179       case GST_ITERATOR_OK:
1180         otherpad = g_value_get_object (&value);
1181         othermonitor =
1182             g_object_get_data ((GObject *) otherpad, "validate-monitor");
1183         if (othermonitor) {
1184           SerializedEventData *data = g_slice_new0 (SerializedEventData);
1185           data->timestamp = last_ts;
1186           data->event = gst_event_ref (event);
1187           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1188           GST_DEBUG_OBJECT (monitor->pad, "Storing for pad %s:%s event %p %s",
1189               GST_DEBUG_PAD_NAME (otherpad), event,
1190               GST_EVENT_TYPE_NAME (event));
1191           g_ptr_array_add (othermonitor->serialized_events, data);
1192           debug_pending_event (otherpad, othermonitor->serialized_events);
1193           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1194         }
1195         g_value_reset (&value);
1196         break;
1197       case GST_ITERATOR_RESYNC:
1198         gst_iterator_resync (iter);
1199         break;
1200       case GST_ITERATOR_ERROR:
1201         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
1202         done = TRUE;
1203         break;
1204       case GST_ITERATOR_DONE:
1205         done = TRUE;
1206         break;
1207     }
1208   }
1209   gst_iterator_free (iter);
1210 }
1211
1212 static void
1213 gst_validate_pad_monitor_otherpad_add_pending_field (GstValidatePadMonitor *
1214     monitor, GstStructure * structure, const gchar * field)
1215 {
1216   GstIterator *iter;
1217   gboolean done;
1218   GstPad *otherpad;
1219   GstValidatePadMonitor *othermonitor;
1220   const GValue *v;
1221
1222   v = gst_structure_get_value (structure, field);
1223   if (v == NULL) {
1224     GST_DEBUG_OBJECT (monitor->pad, "Not adding pending field %s as it isn't "
1225         "present on structure %" GST_PTR_FORMAT, field, structure);
1226     return;
1227   }
1228
1229   iter =
1230       gst_pad_iterate_internal_links (GST_VALIDATE_PAD_MONITOR_GET_PAD
1231       (monitor));
1232   done = FALSE;
1233   while (!done) {
1234     GValue value = { 0, };
1235     switch (gst_iterator_next (iter, &value)) {
1236       case GST_ITERATOR_OK:
1237         otherpad = g_value_get_object (&value);
1238         othermonitor =
1239             g_object_get_data ((GObject *) otherpad, "validate-monitor");
1240         if (othermonitor) {
1241           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1242           g_assert (othermonitor->pending_setcaps_fields != NULL);
1243           gst_structure_set_value (othermonitor->pending_setcaps_fields,
1244               field, v);
1245           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1246         }
1247         g_value_reset (&value);
1248         break;
1249       case GST_ITERATOR_RESYNC:
1250         gst_iterator_resync (iter);
1251         break;
1252       case GST_ITERATOR_ERROR:
1253         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
1254         done = TRUE;
1255         break;
1256       case GST_ITERATOR_DONE:
1257         done = TRUE;
1258         break;
1259     }
1260   }
1261   gst_iterator_free (iter);
1262 }
1263
1264 static void
1265 gst_validate_pad_monitor_otherpad_clear_pending_fields (GstValidatePadMonitor *
1266     monitor)
1267 {
1268   GstIterator *iter;
1269   gboolean done;
1270   GstPad *otherpad;
1271   GstValidatePadMonitor *othermonitor;
1272
1273   iter =
1274       gst_pad_iterate_internal_links (GST_VALIDATE_PAD_MONITOR_GET_PAD
1275       (monitor));
1276
1277   if (iter == NULL) {
1278     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1279
1280     return;
1281   }
1282
1283   done = FALSE;
1284   while (!done) {
1285     GValue value = { 0, };
1286     switch (gst_iterator_next (iter, &value)) {
1287       case GST_ITERATOR_OK:
1288         otherpad = g_value_get_object (&value);
1289         othermonitor =
1290             g_object_get_data ((GObject *) otherpad, "validate-monitor");
1291         if (othermonitor) {
1292           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1293           g_assert (othermonitor->pending_setcaps_fields != NULL);
1294           gst_structure_free (othermonitor->pending_setcaps_fields);
1295           othermonitor->pending_setcaps_fields =
1296               gst_structure_new_empty (PENDING_FIELDS);
1297           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1298         }
1299         g_value_reset (&value);
1300         break;
1301       case GST_ITERATOR_RESYNC:
1302         gst_iterator_resync (iter);
1303         break;
1304       case GST_ITERATOR_ERROR:
1305         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
1306         done = TRUE;
1307         break;
1308       case GST_ITERATOR_DONE:
1309         done = TRUE;
1310         break;
1311     }
1312   }
1313   gst_iterator_free (iter);
1314 }
1315
1316 static void
1317 gst_validate_pad_monitor_add_expected_newsegment (GstValidatePadMonitor *
1318     monitor, GstEvent * event)
1319 {
1320   GstIterator *iter;
1321   gboolean done;
1322   GstPad *otherpad;
1323   GstValidatePadMonitor *othermonitor;
1324
1325   iter =
1326       gst_pad_iterate_internal_links (GST_VALIDATE_PAD_MONITOR_GET_PAD
1327       (monitor));
1328
1329   if (iter == NULL) {
1330     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1331     return;
1332   }
1333
1334   done = FALSE;
1335   while (!done) {
1336     GValue value = { 0, };
1337     switch (gst_iterator_next (iter, &value)) {
1338       case GST_ITERATOR_OK:
1339         otherpad = g_value_get_object (&value);
1340         if (!otherpad)
1341           continue;
1342         othermonitor =
1343             g_object_get_data ((GObject *) otherpad, "validate-monitor");
1344         GST_VALIDATE_MONITOR_LOCK (othermonitor);
1345         gst_event_replace (&othermonitor->expected_segment, event);
1346         GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1347         g_value_reset (&value);
1348         break;
1349       case GST_ITERATOR_RESYNC:
1350         gst_iterator_resync (iter);
1351         break;
1352       case GST_ITERATOR_ERROR:
1353         GST_WARNING_OBJECT (monitor->pad, "Internal links pad iteration error");
1354         done = TRUE;
1355         break;
1356       case GST_ITERATOR_DONE:
1357         done = TRUE;
1358         break;
1359     }
1360   }
1361   gst_iterator_free (iter);
1362 }
1363
1364 static void
1365 gst_validate_pad_monitor_flush (GstValidatePadMonitor * pad_monitor)
1366 {
1367   pad_monitor->current_timestamp = GST_CLOCK_TIME_NONE;
1368   pad_monitor->current_duration = GST_CLOCK_TIME_NONE;
1369   pad_monitor->timestamp_range_start = GST_CLOCK_TIME_NONE;
1370   pad_monitor->timestamp_range_end = GST_CLOCK_TIME_NONE;
1371   pad_monitor->has_segment = FALSE;
1372   pad_monitor->is_eos = FALSE;
1373   pad_monitor->last_flow_return = GST_FLOW_OK;
1374   gst_caps_replace (&pad_monitor->last_caps, NULL);
1375   pad_monitor->caps_is_audio = pad_monitor->caps_is_video =
1376       pad_monitor->caps_is_raw = FALSE;
1377
1378   g_list_free_full (pad_monitor->expired_events,
1379       (GDestroyNotify) gst_event_unref);
1380   pad_monitor->expired_events = NULL;
1381
1382   if (pad_monitor->serialized_events->len)
1383     g_ptr_array_remove_range (pad_monitor->serialized_events, 0,
1384         pad_monitor->serialized_events->len);
1385 }
1386
1387 /* common checks for both sink and src event functions */
1388 static void
1389 gst_validate_pad_monitor_common_event_check (GstValidatePadMonitor *
1390     pad_monitor, GstEvent * event)
1391 {
1392   guint32 seqnum = gst_event_get_seqnum (event);
1393
1394   switch (GST_EVENT_TYPE (event)) {
1395     case GST_EVENT_FLUSH_START:
1396     {
1397       if (pad_monitor->pending_flush_start_seqnum) {
1398         if (seqnum == pad_monitor->pending_flush_start_seqnum) {
1399           pad_monitor->pending_flush_start_seqnum = 0;
1400         } else {
1401           GST_VALIDATE_REPORT (pad_monitor, FLUSH_START_HAS_WRONG_SEQNUM,
1402               "Got: %u Expected: %u", seqnum,
1403               pad_monitor->pending_flush_start_seqnum);
1404         }
1405       }
1406
1407       if (pad_monitor->pending_flush_stop) {
1408         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_START_UNEXPECTED,
1409             "Received flush-start from " " when flush-stop was expected");
1410       }
1411       pad_monitor->pending_flush_stop = TRUE;
1412     }
1413       break;
1414     case GST_EVENT_FLUSH_STOP:
1415     {
1416       if (pad_monitor->pending_flush_stop_seqnum) {
1417         if (seqnum == pad_monitor->pending_flush_stop_seqnum) {
1418           pad_monitor->pending_flush_stop_seqnum = 0;
1419         } else {
1420           GST_VALIDATE_REPORT (pad_monitor, FLUSH_STOP_HAS_WRONG_SEQNUM,
1421               "Got: %u Expected: %u", seqnum,
1422               pad_monitor->pending_flush_stop_seqnum);
1423         }
1424       }
1425
1426       pad_monitor->pending_newsegment_seqnum = seqnum;
1427       pad_monitor->pending_eos_seqnum = seqnum;
1428
1429       if (!pad_monitor->pending_flush_stop) {
1430         gchar *event_str = _get_event_string (event);
1431
1432         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_STOP_UNEXPECTED,
1433             "Unexpected flush-stop %s", event_str);
1434         g_free (event_str);
1435       }
1436       pad_monitor->pending_flush_stop = FALSE;
1437
1438       /* cleanup our data */
1439       gst_validate_pad_monitor_flush (pad_monitor);
1440     }
1441       break;
1442     default:
1443       break;
1444   }
1445 }
1446
1447 static void
1448 mark_pads_eos (GstValidatePadMonitor * pad_monitor)
1449 {
1450   GstValidatePadMonitor *peer_monitor;
1451   GstPad *peer = gst_pad_get_peer (pad_monitor->pad);
1452   GstPad *real_peer;
1453
1454   pad_monitor->is_eos = TRUE;
1455   if (peer) {
1456     real_peer = _get_actual_pad (peer);
1457     peer_monitor =
1458         g_object_get_data ((GObject *) real_peer, "validate-monitor");
1459     if (peer_monitor)
1460       peer_monitor->is_eos = TRUE;
1461     gst_object_unref (peer);
1462     gst_object_unref (real_peer);
1463   }
1464 }
1465
1466 static inline gboolean
1467 _should_check_buffers (GstValidatePadMonitor * pad_monitor,
1468     gboolean force_checks)
1469 {
1470   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1471   GstValidateMonitor *monitor = GST_VALIDATE_MONITOR (pad_monitor);
1472
1473   if (pad_monitor->first_buffer || force_checks) {
1474     if (pad_monitor->segment.rate != 1.0) {
1475       GST_INFO_OBJECT (pad_monitor, "We do not support buffer checking"
1476           " for trick modes");
1477
1478       pad_monitor->check_buffers = FALSE;
1479     } else if (!PAD_PARENT_IS_DECODER (pad_monitor)) {
1480       GST_DEBUG_OBJECT (pad, "Not on a decoder => no buffer checking");
1481
1482       pad_monitor->check_buffers = FALSE;
1483     } else if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK) {
1484       GST_DEBUG_OBJECT (pad, "Not a sinkpad => no buffer checking");
1485
1486       pad_monitor->check_buffers = FALSE;
1487     } else if (!pad_monitor->caps_is_video) {
1488       GST_DEBUG_OBJECT (pad, "Not working with video => no buffer checking");
1489
1490       pad_monitor->check_buffers = FALSE;
1491     } else if (monitor->media_descriptor == NULL) {
1492       GST_DEBUG_OBJECT (pad, "No media_descriptor set => no buffer checking");
1493
1494       pad_monitor->check_buffers = FALSE;
1495     } else if (!gst_media_descriptor_detects_frames (monitor->media_descriptor)) {
1496       GST_DEBUG_OBJECT (pad, "No frame detection media descriptor "
1497           "=> not buffer checking");
1498       pad_monitor->check_buffers = FALSE;
1499     } else if (pad_monitor->all_bufs == NULL &&
1500         !gst_media_descriptor_get_buffers (monitor->media_descriptor, pad, NULL,
1501             &pad_monitor->all_bufs)) {
1502
1503       GST_INFO_OBJECT (monitor,
1504           "The MediaInfo is marked as detecting frame, but getting frames"
1505           " from pad %" GST_PTR_FORMAT " did not work (some format conversion"
1506           " might be happening)", pad);
1507
1508       pad_monitor->check_buffers = FALSE;
1509     } else {
1510       if (!pad_monitor->current_buf)
1511         pad_monitor->current_buf = pad_monitor->all_bufs;
1512       pad_monitor->check_buffers = TRUE;
1513     }
1514   }
1515
1516   return pad_monitor->check_buffers;
1517 }
1518
1519 static void
1520 gst_validate_monitor_find_next_buffer (GstValidatePadMonitor * pad_monitor)
1521 {
1522   GList *tmp;
1523   gboolean passed_start = FALSE;
1524
1525   if (!_should_check_buffers (pad_monitor, TRUE))
1526     return;
1527
1528   for (tmp = g_list_last (pad_monitor->all_bufs); tmp; tmp = tmp->prev) {
1529     GstBuffer *cbuf = tmp->data;
1530     GstClockTime ts =
1531         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cbuf)) ? GST_BUFFER_DTS (cbuf)
1532         : GST_BUFFER_PTS (cbuf);
1533
1534     if (!GST_CLOCK_TIME_IS_VALID (ts))
1535       continue;
1536
1537     if (ts <= pad_monitor->segment.start)
1538       passed_start = TRUE;
1539
1540     if (!passed_start)
1541       continue;
1542
1543     if (!GST_BUFFER_FLAG_IS_SET (cbuf, GST_BUFFER_FLAG_DELTA_UNIT)) {
1544       break;
1545     }
1546   }
1547
1548   if (tmp == NULL)
1549     pad_monitor->current_buf = pad_monitor->all_bufs;
1550   else
1551     pad_monitor->current_buf = tmp;
1552 }
1553
1554 static gboolean
1555 gst_validate_pad_monitor_downstream_event_check (GstValidatePadMonitor *
1556     pad_monitor, GstObject * parent, GstEvent * event,
1557     GstPadEventFunction handler)
1558 {
1559   gboolean ret = TRUE;
1560   const GstSegment *segment;
1561   guint32 seqnum = gst_event_get_seqnum (event);
1562   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1563
1564   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
1565
1566   /* pre checks */
1567   switch (GST_EVENT_TYPE (event)) {
1568     case GST_EVENT_SEGMENT:
1569       /* parse segment data to be used if event is handled */
1570       gst_event_parse_segment (event, &segment);
1571
1572       GST_DEBUG_OBJECT (pad_monitor->pad, "Got segment %" GST_SEGMENT_FORMAT,
1573           segment);
1574
1575       if (pad_monitor->pending_newsegment_seqnum) {
1576         if (pad_monitor->pending_newsegment_seqnum == seqnum) {
1577           pad_monitor->pending_newsegment_seqnum = 0;
1578         } else {
1579           GST_VALIDATE_REPORT (pad_monitor, SEGMENT_HAS_WRONG_SEQNUM,
1580               "Got: %u Expected: %u", seqnum, pad_monitor->pending_eos_seqnum);
1581         }
1582       }
1583
1584       pad_monitor->pending_eos_seqnum = seqnum;
1585
1586       if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
1587         gst_validate_pad_monitor_add_expected_newsegment (pad_monitor, event);
1588       } else {
1589         /* check if this segment is the expected one */
1590         if (pad_monitor->expected_segment) {
1591           const GstSegment *exp_segment;
1592
1593           if (pad_monitor->expected_segment != event) {
1594             gst_event_parse_segment (pad_monitor->expected_segment,
1595                 &exp_segment);
1596             if (segment->format == exp_segment->format) {
1597               if ((exp_segment->rate * exp_segment->applied_rate !=
1598                       segment->rate * segment->applied_rate))
1599                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1600                     "Rate * applied_rate %d != expected %d",
1601                     segment->rate * segment->applied_rate,
1602                     exp_segment->rate * exp_segment->applied_rate);
1603               if (exp_segment->start != segment->start)
1604                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1605                     "Start %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
1606                     GST_TIME_ARGS (segment->start),
1607                     GST_TIME_ARGS (exp_segment->start));
1608               if (exp_segment->stop != segment->stop)
1609                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1610                     "Stop %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
1611                     GST_TIME_ARGS (segment->stop),
1612                     GST_TIME_ARGS (exp_segment->stop));
1613               if (exp_segment->position != segment->position)
1614                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1615                     "Position %" GST_TIME_FORMAT " != expected %"
1616                     GST_TIME_FORMAT, GST_TIME_ARGS (segment->position),
1617                     GST_TIME_ARGS (exp_segment->position));
1618             }
1619           }
1620           gst_event_replace (&pad_monitor->expected_segment, NULL);
1621         }
1622       }
1623       break;
1624     case GST_EVENT_CAPS:{
1625       GstCaps *caps;
1626
1627       gst_event_parse_caps (event, &caps);
1628       gst_validate_pad_monitor_setcaps_pre (pad_monitor, caps);
1629       break;
1630     }
1631     case GST_EVENT_EOS:
1632       pad_monitor->is_eos = TRUE;
1633       if (pad_monitor->pending_eos_seqnum == 0) {
1634         GST_VALIDATE_REPORT (pad_monitor, EVENT_EOS_WITHOUT_SEGMENT,
1635             "EOS %" GST_PTR_FORMAT " received before a segment was received",
1636             event);
1637       } else if (pad_monitor->pending_eos_seqnum != seqnum) {
1638         GST_VALIDATE_REPORT (pad_monitor, EOS_HAS_WRONG_SEQNUM,
1639             "Got: %u. Expected: %u", seqnum, pad_monitor->pending_eos_seqnum);
1640       }
1641
1642       /*
1643        * TODO add end of stream checks for
1644        *  - events not pushed
1645        *  - buffer data not pushed
1646        *  - pending events not received
1647        */
1648       break;
1649
1650       /* both flushes are handled by the common event function */
1651     case GST_EVENT_FLUSH_START:
1652     case GST_EVENT_FLUSH_STOP:
1653     case GST_EVENT_TAG:
1654     case GST_EVENT_SINK_MESSAGE:
1655     default:
1656       break;
1657   }
1658
1659   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1660   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1661   gst_validate_pad_monitor_event_overrides (pad_monitor, event);
1662   if (handler) {
1663     gst_event_ref (event);
1664     ret = pad_monitor->event_func (pad, parent, event);
1665   }
1666   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1667   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1668
1669   /* post checks */
1670   switch (GST_EVENT_TYPE (event)) {
1671     case GST_EVENT_SEGMENT:
1672       if (ret) {
1673         if (!pad_monitor->has_segment
1674             && pad_monitor->segment.format != segment->format) {
1675           gst_segment_init (&pad_monitor->segment, segment->format);
1676         }
1677         gst_segment_copy_into (segment, &pad_monitor->segment);
1678         pad_monitor->has_segment = TRUE;
1679         gst_validate_monitor_find_next_buffer (pad_monitor);
1680       }
1681       break;
1682     case GST_EVENT_CAPS:{
1683       GstCaps *caps;
1684
1685       gst_event_parse_caps (event, &caps);
1686       gst_validate_pad_monitor_setcaps_post (pad_monitor, caps, ret);
1687       break;
1688     }
1689     case GST_EVENT_FLUSH_START:
1690     case GST_EVENT_FLUSH_STOP:
1691     case GST_EVENT_EOS:
1692     case GST_EVENT_TAG:
1693     case GST_EVENT_SINK_MESSAGE:
1694     default:
1695       break;
1696   }
1697
1698   if (handler)
1699     gst_event_unref (event);
1700   return ret;
1701 }
1702
1703 static gboolean
1704 gst_validate_pad_monitor_src_event_check (GstValidatePadMonitor * pad_monitor,
1705     GstObject * parent, GstEvent * event, GstPadEventFunction handler)
1706 {
1707   gboolean ret = TRUE;
1708   gdouble rate;
1709   GstFormat format;
1710   gint64 start, stop;
1711   GstSeekFlags seek_flags;
1712   GstSeekType start_type, stop_type;
1713   guint32 seqnum = gst_event_get_seqnum (event);
1714   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1715
1716   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
1717
1718   /* pre checks */
1719   switch (GST_EVENT_TYPE (event)) {
1720     case GST_EVENT_SEEK:
1721     {
1722       gst_event_parse_seek (event, &rate, &format, &seek_flags, &start_type,
1723           &start, &stop_type, &stop);
1724       /* upstream seek - store the seek event seqnum to check
1725        * flushes and newsegments share the same */
1726
1727       /* TODO we might need to use a list as multiple seeks can be sent
1728        * before the flushes arrive here */
1729       if (seek_flags & GST_SEEK_FLAG_FLUSH) {
1730         pad_monitor->pending_flush_start_seqnum = seqnum;
1731         pad_monitor->pending_flush_stop_seqnum = seqnum;
1732       }
1733     }
1734       break;
1735       /* both flushes are handled by the common event handling function */
1736     case GST_EVENT_FLUSH_START:
1737     case GST_EVENT_FLUSH_STOP:
1738     case GST_EVENT_NAVIGATION:
1739     case GST_EVENT_LATENCY:
1740     case GST_EVENT_STEP:
1741     case GST_EVENT_QOS:
1742     default:
1743       break;
1744   }
1745
1746   if (handler) {
1747     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1748     gst_event_ref (event);
1749     ret = pad_monitor->event_func (pad, parent, event);
1750     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1751   }
1752
1753   /* post checks */
1754   switch (GST_EVENT_TYPE (event)) {
1755     case GST_EVENT_FLUSH_START:
1756     case GST_EVENT_FLUSH_STOP:
1757     case GST_EVENT_QOS:
1758     case GST_EVENT_SEEK:
1759     {
1760       if (ret == FALSE) {
1761         /* do not expect any of these events anymore */
1762         pad_monitor->pending_flush_start_seqnum = 0;
1763         pad_monitor->pending_flush_stop_seqnum = 0;
1764         pad_monitor->pending_newsegment_seqnum = 0;
1765         pad_monitor->pending_eos_seqnum = 0;
1766       }
1767     }
1768       break;
1769     case GST_EVENT_NAVIGATION:
1770     case GST_EVENT_LATENCY:
1771     case GST_EVENT_STEP:
1772     default:
1773       break;
1774   }
1775
1776   if (handler)
1777     gst_event_unref (event);
1778   return ret;
1779 }
1780
1781 static gboolean
1782 gst_validate_pad_monitor_check_right_buffer (GstValidatePadMonitor *
1783     pad_monitor, GstBuffer * buffer)
1784 {
1785   gchar *checksum;
1786   GstBuffer *wanted_buf;
1787   GstMapInfo map, wanted_map;
1788
1789   gboolean ret = TRUE;
1790   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1791
1792   if (_should_check_buffers (pad_monitor, FALSE) == FALSE)
1793     return FALSE;
1794
1795   if (pad_monitor->current_buf == NULL) {
1796     GST_INFO_OBJECT (pad, "No current buffer one pad, Why?");
1797     return FALSE;
1798   }
1799
1800   wanted_buf = pad_monitor->current_buf->data;
1801
1802   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (wanted_buf)) &&
1803       GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buffer)) &&
1804       GST_BUFFER_PTS (wanted_buf) != GST_BUFFER_PTS (buffer)) {
1805
1806     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1807         "buffer %" GST_PTR_FORMAT " PTS %ld"
1808         " different than expected: %ld", buffer,
1809         GST_BUFFER_PTS (buffer), GST_BUFFER_PTS (wanted_buf));
1810
1811     ret = FALSE;
1812   }
1813
1814   if (GST_BUFFER_DTS (wanted_buf) != GST_BUFFER_DTS (buffer)) {
1815     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1816         "buffer %" GST_PTR_FORMAT " DTS %" GST_TIME_FORMAT
1817         " different than expected: %" GST_TIME_FORMAT, buffer,
1818         GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
1819         GST_TIME_ARGS (GST_BUFFER_DTS (wanted_buf)));
1820     ret = FALSE;
1821   }
1822
1823   if (GST_BUFFER_DURATION (wanted_buf) != GST_BUFFER_DURATION (buffer)) {
1824     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1825         "buffer %" GST_PTR_FORMAT " DURATION %" GST_TIME_FORMAT
1826         " different than expected: %" GST_TIME_FORMAT, buffer,
1827         GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
1828         GST_TIME_ARGS (GST_BUFFER_DURATION (wanted_buf)));
1829     ret = FALSE;
1830   }
1831
1832   if (GST_BUFFER_FLAG_IS_SET (wanted_buf, GST_BUFFER_FLAG_DELTA_UNIT) !=
1833       GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
1834     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1835         "buffer %" GST_PTR_FORMAT "  Delta unit is set to %s but expected %s",
1836         buffer, GST_BUFFER_FLAG_IS_SET (buffer,
1837             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False",
1838         GST_BUFFER_FLAG_IS_SET (wanted_buf,
1839             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False");
1840     ret = FALSE;
1841   }
1842
1843   g_assert (gst_buffer_map (wanted_buf, &wanted_map, GST_MAP_READ));
1844   g_assert (gst_buffer_map (buffer, &map, GST_MAP_READ));
1845
1846   checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5,
1847       (const guchar *) map.data, map.size);
1848
1849   if (g_strcmp0 ((gchar *) wanted_map.data, checksum)) {
1850     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1851         "buffer %" GST_PTR_FORMAT " checksum %s different from expected: %s",
1852         buffer, checksum, wanted_map.data);
1853     ret = FALSE;
1854   }
1855
1856   gst_buffer_unmap (wanted_buf, &wanted_map);
1857   gst_buffer_unmap (buffer, &map);
1858   g_free (checksum);
1859
1860   pad_monitor->current_buf = pad_monitor->current_buf->next;
1861
1862   return ret;
1863 }
1864
1865 static GstFlowReturn
1866 gst_validate_pad_monitor_chain_func (GstPad * pad, GstObject * parent,
1867     GstBuffer * buffer)
1868 {
1869   GstValidatePadMonitor *pad_monitor =
1870       g_object_get_data ((GObject *) pad, "validate-monitor");
1871   GstFlowReturn ret;
1872
1873   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1874   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1875
1876   gst_validate_pad_monitor_check_right_buffer (pad_monitor, buffer);
1877   gst_validate_pad_monitor_check_first_buffer (pad_monitor, buffer);
1878   gst_validate_pad_monitor_update_buffer_data (pad_monitor, buffer);
1879   gst_validate_pad_monitor_check_eos (pad_monitor, buffer);
1880
1881   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1882   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1883
1884   gst_validate_pad_monitor_buffer_overrides (pad_monitor, buffer);
1885
1886   ret = pad_monitor->chain_func (pad, parent, buffer);
1887
1888   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1889   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1890
1891   pad_monitor->last_flow_return = ret;
1892   if (ret == GST_FLOW_EOS) {
1893     mark_pads_eos (pad_monitor);
1894   }
1895   if (PAD_PARENT_IS_DEMUXER (pad_monitor))
1896     gst_validate_pad_monitor_check_aggregated_return (pad_monitor, ret);
1897
1898   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1899   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1900
1901   return ret;
1902 }
1903
1904 static gboolean
1905 gst_validate_pad_monitor_event_is_tracked (GstValidatePadMonitor * monitor,
1906     GstEvent * event)
1907 {
1908   if (!GST_EVENT_IS_SERIALIZED (event)) {
1909     return FALSE;
1910   }
1911
1912   /* we don't track Tag events because they mutate too much and it is hard
1913    * to match a tag event pushed on a source pad with the one that was received
1914    * on a sink pad.
1915    * One idea would be to use seqnum, but it seems that it is undefined whether
1916    * seqnums should be maintained in tag events that are created from others
1917    * up to today. (2013-08-29)
1918    */
1919   if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
1920     return FALSE;
1921
1922   return TRUE;
1923 }
1924
1925 static gboolean
1926 gst_validate_pad_monitor_sink_event_func (GstPad * pad, GstObject * parent,
1927     GstEvent * event)
1928 {
1929   GstValidatePadMonitor *pad_monitor =
1930       g_object_get_data ((GObject *) pad, "validate-monitor");
1931   gboolean ret;
1932
1933   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1934   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1935
1936   if (gst_validate_pad_monitor_event_is_tracked (pad_monitor, event)) {
1937     GstClockTime last_ts = GST_CLOCK_TIME_NONE;
1938     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_timestamp)) {
1939       last_ts = pad_monitor->current_timestamp;
1940       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_duration)) {
1941         last_ts += pad_monitor->current_duration;
1942       }
1943     }
1944     gst_validate_pad_monitor_otherpad_add_pending_serialized_event (pad_monitor,
1945         event, last_ts);
1946   }
1947
1948   ret =
1949       gst_validate_pad_monitor_downstream_event_check (pad_monitor, parent,
1950       event, pad_monitor->event_func);
1951
1952   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1953   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1954   return ret;
1955 }
1956
1957 static gboolean
1958 gst_validate_pad_monitor_src_event_func (GstPad * pad, GstObject * parent,
1959     GstEvent * event)
1960 {
1961   GstValidatePadMonitor *pad_monitor =
1962       g_object_get_data ((GObject *) pad, "validate-monitor");
1963   gboolean ret;
1964
1965   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1966   ret = gst_validate_pad_monitor_src_event_check (pad_monitor, parent, event,
1967       pad_monitor->event_func);
1968   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1969   return ret;
1970 }
1971
1972 static gboolean
1973 gst_validate_pad_monitor_query_func (GstPad * pad, GstObject * parent,
1974     GstQuery * query)
1975 {
1976   GstValidatePadMonitor *pad_monitor =
1977       g_object_get_data ((GObject *) pad, "validate-monitor");
1978   gboolean ret;
1979
1980   gst_validate_pad_monitor_query_overrides (pad_monitor, query);
1981
1982   ret = pad_monitor->query_func (pad, parent, query);
1983
1984   if (ret) {
1985     switch (GST_QUERY_TYPE (query)) {
1986       case GST_QUERY_CAPS:{
1987         GstCaps *res;
1988         /* We shouldn't need to lock the parent as this doesn't modify
1989          * other monitors, just does some peer_pad_caps */
1990         GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1991
1992         gst_query_parse_caps_result (query, &res);
1993         if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
1994           gst_validate_pad_monitor_check_caps_fields_proxied (pad_monitor, res);
1995         }
1996         GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1997         break;
1998       }
1999       default:
2000         break;
2001     }
2002   }
2003
2004   return ret;
2005 }
2006
2007 static gboolean
2008 gst_validate_pad_monitor_activatemode_func (GstPad * pad, GstObject * parent,
2009     GstPadMode mode, gboolean active)
2010 {
2011   GstValidatePadMonitor *pad_monitor =
2012       g_object_get_data ((GObject *) pad, "validate-monitor");
2013   gboolean ret = TRUE;
2014
2015   /* TODO add overrides for activate func */
2016
2017   if (pad_monitor->activatemode_func)
2018     ret = pad_monitor->activatemode_func (pad, parent, mode, active);
2019   if (ret && active == FALSE) {
2020     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2021     gst_validate_pad_monitor_flush (pad_monitor);
2022     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2023   }
2024
2025   return ret;
2026 }
2027
2028 static gboolean
2029 gst_validate_pad_get_range_func (GstPad * pad, GstObject * parent,
2030     guint64 offset, guint size, GstBuffer ** buffer)
2031 {
2032   GstValidatePadMonitor *pad_monitor =
2033       g_object_get_data ((GObject *) pad, "validate-monitor");
2034   gboolean ret;
2035   ret = pad_monitor->getrange_func (pad, parent, offset, size, buffer);
2036   return ret;
2037 }
2038
2039 static gboolean
2040 gst_validate_pad_monitor_buffer_probe (GstPad * pad, GstBuffer * buffer,
2041     gpointer udata)
2042 {
2043   GstValidatePadMonitor *monitor = udata;
2044
2045   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2046   GST_VALIDATE_MONITOR_LOCK (monitor);
2047
2048   gst_validate_pad_monitor_check_first_buffer (monitor, buffer);
2049   gst_validate_pad_monitor_update_buffer_data (monitor, buffer);
2050   gst_validate_pad_monitor_check_eos (monitor, buffer);
2051
2052   if (PAD_PARENT_IS_DECODER (monitor) || PAD_PARENT_IS_ENCODER (monitor)) {
2053     GstClockTime tolerance = 0;
2054
2055     if (monitor->caps_is_audio)
2056       tolerance = AUDIO_TIMESTAMP_TOLERANCE;
2057
2058     gst_validate_pad_monitor_check_buffer_timestamp_in_received_range (monitor,
2059         buffer, tolerance);
2060   }
2061
2062   gst_validate_pad_monitor_check_late_serialized_events (monitor,
2063       GST_BUFFER_TIMESTAMP (buffer));
2064
2065   /* a GstValidatePadMonitor parent must be a GstValidateElementMonitor */
2066   if (PAD_PARENT_IS_DECODER (monitor)) {
2067
2068     /* should not push out of segment data */
2069     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer)) &&
2070         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer)) &&
2071         ((!gst_segment_clip (&monitor->segment, monitor->segment.format,
2072                     GST_BUFFER_TIMESTAMP (buffer),
2073                     GST_BUFFER_TIMESTAMP (buffer) +
2074                     GST_BUFFER_DURATION (buffer), NULL, NULL)) ||
2075             /* In the case of raw data, buffers should be strictly contained inside the
2076              * segment */
2077             (monitor->caps_is_raw &&
2078                 GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer) <
2079                 monitor->segment.start))
2080         ) {
2081       /* TODO is this a timestamp issue? */
2082       GST_VALIDATE_REPORT (monitor, BUFFER_IS_OUT_OF_SEGMENT,
2083           "buffer is out of segment and shouldn't be pushed. Timestamp: %"
2084           GST_TIME_FORMAT " - duration: %" GST_TIME_FORMAT ". Range: %"
2085           GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
2086           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2087           GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
2088           GST_TIME_ARGS (monitor->segment.start),
2089           GST_TIME_ARGS (monitor->segment.stop));
2090     }
2091   }
2092
2093   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2094   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2095   gst_validate_pad_monitor_buffer_probe_overrides (monitor, buffer);
2096   return TRUE;
2097 }
2098
2099 static gboolean
2100 gst_validate_pad_monitor_event_probe (GstPad * pad, GstEvent * event,
2101     gpointer udata)
2102 {
2103   GstValidatePadMonitor *monitor = GST_VALIDATE_PAD_MONITOR_CAST (udata);
2104   gboolean ret;
2105
2106   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2107   GST_VALIDATE_MONITOR_LOCK (monitor);
2108
2109   GST_DEBUG_OBJECT (pad, "event %p %s", event, GST_EVENT_TYPE_NAME (event));
2110
2111   if (GST_EVENT_IS_SERIALIZED (event)) {
2112     gint i;
2113
2114     /* Detect if events the element received are being forwarded in the same order
2115      *
2116      * Several scenarios:
2117      * 1) The element pushes the event as-is
2118      * 2) The element consumes the event and does not forward it
2119      * 3) The element consumes the event and creates another one instead
2120      * 4) The element pushes other serialized event before pushing out the
2121      *    one it received
2122      *
2123      * For each pad we have two lists to track serialized events:
2124      *  1) We received on input and expect to see (serialized_events)
2125      *  2) We received on input but don't expect to see (expired_events)
2126      *
2127      * To detect events that are pushed in a different order from the one they were
2128      * received in we check that:
2129      *
2130      * For each event being outputted:
2131      *   If it is in the expired_events list:
2132      *     RAISE WARNING
2133      *   If it is in the serialized_events list:
2134      *     If there are other events that were received before:
2135      *        Put those events on the expired_events list
2136      *     Remove that event and any previous ones from the serialized_events list
2137      *
2138      * Clear expired events list when flushing or on pad deactivation
2139      *
2140      */
2141
2142     if (g_list_find (monitor->expired_events, event)) {
2143       gchar *event_str = _get_event_string (event);
2144       /* If it's the expired events, we've failed */
2145       GST_WARNING_OBJECT (pad, "Did not expect event %p %s", event,
2146           GST_EVENT_TYPE_NAME (event));
2147       GST_VALIDATE_REPORT (monitor, EVENT_SERIALIZED_OUT_OF_ORDER,
2148           "Serialized event was pushed out of order: %s", event_str);
2149
2150       g_free (event_str);
2151       monitor->expired_events = g_list_remove (monitor->expired_events, event);
2152       gst_event_unref (event);  /* remove the ref that was on the list */
2153     } else if (monitor->serialized_events->len) {
2154       for (i = 0; i < monitor->serialized_events->len; i++) {
2155         SerializedEventData *next_event =
2156             g_ptr_array_index (monitor->serialized_events, i);
2157         GST_DEBUG_OBJECT (pad, "Checking against stored event #%d: %p %s", i,
2158             next_event->event, GST_EVENT_TYPE_NAME (next_event->event));
2159
2160         if (event == next_event->event
2161             || GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2162           /* We have found our event */
2163           GST_DEBUG_OBJECT (pad, "Found matching event");
2164
2165           while (monitor->serialized_events->len > i
2166               && GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2167             /* Swallow all expected events of the same type */
2168             g_ptr_array_remove_index (monitor->serialized_events, i);
2169             next_event = g_ptr_array_index (monitor->serialized_events, i);
2170           }
2171
2172           /* Move all previous events to expired events */
2173           if (G_UNLIKELY (i > 0)) {
2174             GST_DEBUG_OBJECT (pad,
2175                 "Moving previous expected events to expired list");
2176             while (i--) {
2177               next_event = g_ptr_array_index (monitor->serialized_events, 0);
2178               monitor->expired_events =
2179                   g_list_append (monitor->expired_events,
2180                   gst_event_ref (next_event->event));
2181               g_ptr_array_remove_index (monitor->serialized_events, 0);
2182             }
2183           }
2184           debug_pending_event (pad, monitor->serialized_events);
2185           break;
2186         }
2187       }
2188     }
2189   }
2190
2191   /* This so far is just like an event that is flowing downstream,
2192    * so we do the same checks as a sinkpad event handler */
2193   ret =
2194       gst_validate_pad_monitor_downstream_event_check (monitor, NULL, event,
2195       NULL);
2196   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2197   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2198
2199   return ret;
2200 }
2201
2202 static GstPadProbeReturn
2203 gst_validate_pad_monitor_pad_probe (GstPad * pad, GstPadProbeInfo * info,
2204     gpointer udata)
2205 {
2206   if (info->type & GST_PAD_PROBE_TYPE_BUFFER)
2207     gst_validate_pad_monitor_buffer_probe (pad, info->data, udata);
2208   else if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM)
2209     gst_validate_pad_monitor_event_probe (pad, info->data, udata);
2210
2211   return GST_PAD_PROBE_OK;
2212 }
2213
2214 static void
2215 gst_validate_pad_monitor_update_caps_info (GstValidatePadMonitor * pad_monitor,
2216     GstCaps * caps)
2217 {
2218   GstStructure *structure;
2219
2220   g_return_if_fail (gst_caps_is_fixed (caps));
2221
2222   pad_monitor->caps_is_audio = FALSE;
2223   pad_monitor->caps_is_video = FALSE;
2224
2225   structure = gst_caps_get_structure (caps, 0);
2226   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/")) {
2227     pad_monitor->caps_is_audio = TRUE;
2228   } else if (g_str_has_prefix (gst_structure_get_name (structure), "video/")) {
2229     pad_monitor->caps_is_video = TRUE;
2230   }
2231
2232   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/x-raw") ||
2233       g_str_has_prefix (gst_structure_get_name (structure), "video/x-raw")) {
2234     pad_monitor->caps_is_raw = TRUE;
2235   } else {
2236     pad_monitor->caps_is_raw = FALSE;
2237   }
2238 }
2239
2240 static void
2241 gst_validate_pad_monitor_setcaps_pre (GstValidatePadMonitor * pad_monitor,
2242     GstCaps * caps)
2243 {
2244   GstStructure *structure;
2245
2246   /* Check if caps are identical to last caps and complain if so
2247    * Only checked for sink pads as src pads might push the same caps
2248    * multiple times during unlinked/autoplugging scenarios */
2249   if (GST_PAD_IS_SINK (GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor)) &&
2250       pad_monitor->last_caps
2251       && gst_caps_is_equal (caps, pad_monitor->last_caps)) {
2252     gchar *caps_str = gst_caps_to_string (caps);
2253
2254     GST_VALIDATE_REPORT (pad_monitor, EVENT_CAPS_DUPLICATE, "%s", caps_str);
2255     g_free (caps_str);
2256
2257   }
2258
2259   gst_validate_pad_monitor_check_caps_complete (pad_monitor, caps);
2260
2261   if (caps) {
2262     structure = gst_caps_get_structure (caps, 0);
2263     if (gst_structure_n_fields (pad_monitor->pending_setcaps_fields)) {
2264       gint i;
2265       for (i = 0;
2266           i < gst_structure_n_fields (pad_monitor->pending_setcaps_fields);
2267           i++) {
2268         const gchar *name =
2269             gst_structure_nth_field_name (pad_monitor->pending_setcaps_fields,
2270             i);
2271         const GValue *v = gst_structure_get_value (structure, name);
2272         const GValue *otherv =
2273             gst_structure_get_value (pad_monitor->pending_setcaps_fields, name);
2274
2275         if (v == NULL) {
2276           gchar *caps_str = gst_caps_to_string (caps);
2277
2278           GST_VALIDATE_REPORT (pad_monitor, CAPS_EXPECTED_FIELD_NOT_FOUND,
2279               "Field %s is missing from setcaps caps '%s'", name, caps_str);
2280           g_free (caps_str);
2281         } else if (gst_value_compare (v, otherv) != GST_VALUE_EQUAL) {
2282           gchar *caps_str = gst_caps_to_string (caps),
2283               *pending_setcaps_fields_str =
2284               gst_structure_to_string (pad_monitor->pending_setcaps_fields);
2285
2286
2287           GST_VALIDATE_REPORT (pad_monitor, CAPS_FIELD_UNEXPECTED_VALUE,
2288               "Field %s from setcaps caps '%s' is different "
2289               "from expected value in caps '%s'", name, caps_str,
2290               pending_setcaps_fields_str);
2291
2292           g_free (pending_setcaps_fields_str);
2293           g_free (caps_str);
2294         }
2295       }
2296     }
2297
2298     if (gst_validate_pad_monitor_pad_should_proxy_othercaps (pad_monitor)) {
2299       if (_structure_is_video (structure)) {
2300         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2301             structure, "width");
2302         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2303             structure, "height");
2304         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2305             structure, "framerate");
2306         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2307             structure, "pixel-aspect-ratio");
2308       } else if (_structure_is_audio (structure)) {
2309         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2310             structure, "rate");
2311         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2312             structure, "channels");
2313       }
2314     }
2315   }
2316
2317   gst_structure_free (pad_monitor->pending_setcaps_fields);
2318   pad_monitor->pending_setcaps_fields =
2319       gst_structure_new_empty (PENDING_FIELDS);
2320
2321   gst_validate_pad_monitor_setcaps_overrides (pad_monitor, caps);
2322 }
2323
2324 static void
2325 gst_validate_pad_monitor_setcaps_post (GstValidatePadMonitor * pad_monitor,
2326     GstCaps * caps, gboolean ret)
2327 {
2328   if (!ret)
2329     gst_validate_pad_monitor_otherpad_clear_pending_fields (pad_monitor);
2330   else {
2331     if (pad_monitor->last_caps) {
2332       gst_caps_unref (pad_monitor->last_caps);
2333     }
2334     pad_monitor->last_caps = gst_caps_ref (caps);
2335     gst_validate_pad_monitor_update_caps_info (pad_monitor, caps);
2336   }
2337 }
2338
2339 static gboolean
2340 gst_validate_pad_monitor_do_setup (GstValidateMonitor * monitor)
2341 {
2342   GstValidatePadMonitor *pad_monitor = GST_VALIDATE_PAD_MONITOR_CAST (monitor);
2343   GstPad *pad;
2344   if (!GST_IS_PAD (GST_VALIDATE_MONITOR_GET_OBJECT (monitor))) {
2345     GST_WARNING_OBJECT (monitor, "Trying to create pad monitor with other "
2346         "type of object");
2347     return FALSE;
2348   }
2349
2350   pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
2351
2352   if (g_object_get_data ((GObject *) pad, "validate-monitor")) {
2353     GST_WARNING_OBJECT (pad_monitor,
2354         "Pad already has a validate-monitor associated");
2355     return FALSE;
2356   }
2357
2358   g_object_set_data ((GObject *) pad, "validate-monitor", pad_monitor);
2359
2360   pad_monitor->pad = pad;
2361
2362   pad_monitor->event_func = GST_PAD_EVENTFUNC (pad);
2363   pad_monitor->query_func = GST_PAD_QUERYFUNC (pad);
2364   pad_monitor->activatemode_func = GST_PAD_ACTIVATEMODEFUNC (pad);
2365   if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2366
2367     pad_monitor->chain_func = GST_PAD_CHAINFUNC (pad);
2368     if (pad_monitor->chain_func)
2369       gst_pad_set_chain_function (pad, gst_validate_pad_monitor_chain_func);
2370
2371     gst_pad_set_event_function (pad, gst_validate_pad_monitor_sink_event_func);
2372   } else {
2373     pad_monitor->getrange_func = GST_PAD_GETRANGEFUNC (pad);
2374     if (pad_monitor->getrange_func)
2375       gst_pad_set_getrange_function (pad, gst_validate_pad_get_range_func);
2376
2377     gst_pad_set_event_function (pad, gst_validate_pad_monitor_src_event_func);
2378
2379     /* add buffer/event probes */
2380     pad_monitor->pad_probe_id =
2381         gst_pad_add_probe (pad,
2382         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
2383         GST_PAD_PROBE_TYPE_EVENT_FLUSH,
2384         (GstPadProbeCallback) gst_validate_pad_monitor_pad_probe, pad_monitor,
2385         NULL);
2386   }
2387   gst_pad_set_query_function (pad, gst_validate_pad_monitor_query_func);
2388   gst_pad_set_activatemode_function (pad,
2389       gst_validate_pad_monitor_activatemode_func);
2390
2391   gst_validate_reporter_set_name (GST_VALIDATE_REPORTER (monitor),
2392       g_strdup_printf ("%s:%s", GST_DEBUG_PAD_NAME (pad)));
2393
2394   if (G_UNLIKELY (GST_PAD_PARENT (pad) == NULL))
2395     GST_FIXME ("Saw a pad not belonging to any object");
2396
2397   return TRUE;
2398 }