pad-monitor: Give better details about segment mismatch issues
[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     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, FLUSH_START_HAS_WRONG_SEQNUM,
1412               "Got: %u Expected: %u", seqnum,
1413               pad_monitor->pending_flush_start_seqnum);
1414         }
1415       }
1416
1417       if (pad_monitor->pending_flush_stop) {
1418         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_START_UNEXPECTED,
1419             "Received flush-start from " " when flush-stop was expected");
1420       }
1421       pad_monitor->pending_flush_stop = TRUE;
1422     }
1423       break;
1424     case GST_EVENT_FLUSH_STOP:
1425     {
1426       if (pad_monitor->pending_flush_stop_seqnum) {
1427         if (seqnum == pad_monitor->pending_flush_stop_seqnum) {
1428           pad_monitor->pending_flush_stop_seqnum = 0;
1429         } else {
1430           GST_VALIDATE_REPORT (pad_monitor, FLUSH_STOP_HAS_WRONG_SEQNUM,
1431               "Got: %u Expected: %u", seqnum,
1432               pad_monitor->pending_flush_stop_seqnum);
1433         }
1434       }
1435
1436       pad_monitor->pending_newsegment_seqnum = seqnum;
1437       pad_monitor->pending_eos_seqnum = seqnum;
1438
1439       if (!pad_monitor->pending_flush_stop) {
1440         gchar *event_str = _get_event_string (event);
1441
1442         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_STOP_UNEXPECTED,
1443             "Unexpected flush-stop %s", event_str);
1444         g_free (event_str);
1445       }
1446       pad_monitor->pending_flush_stop = FALSE;
1447
1448       /* cleanup our data */
1449       gst_validate_pad_monitor_flush (pad_monitor);
1450     }
1451       break;
1452     default:
1453       break;
1454   }
1455 }
1456
1457 static void
1458 mark_pads_eos (GstValidatePadMonitor * pad_monitor)
1459 {
1460   GstValidatePadMonitor *peer_monitor;
1461   GstPad *peer = gst_pad_get_peer (pad_monitor->pad);
1462   GstPad *real_peer;
1463
1464   pad_monitor->is_eos = TRUE;
1465   if (peer) {
1466     real_peer = _get_actual_pad (peer);
1467     peer_monitor =
1468         g_object_get_data ((GObject *) real_peer, "validate-monitor");
1469     if (peer_monitor)
1470       peer_monitor->is_eos = TRUE;
1471     gst_object_unref (peer);
1472     gst_object_unref (real_peer);
1473   }
1474 }
1475
1476 static inline gboolean
1477 _should_check_buffers (GstValidatePadMonitor * pad_monitor,
1478     gboolean force_checks)
1479 {
1480   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1481   GstValidateMonitor *monitor = GST_VALIDATE_MONITOR (pad_monitor);
1482
1483   if (pad_monitor->first_buffer || force_checks) {
1484     if (pad_monitor->segment.rate != 1.0) {
1485       GST_INFO_OBJECT (pad_monitor, "We do not support buffer checking"
1486           " for trick modes");
1487
1488       pad_monitor->check_buffers = FALSE;
1489     } else if (!PAD_PARENT_IS_DECODER (pad_monitor)) {
1490       GST_DEBUG_OBJECT (pad, "Not on a decoder => no buffer checking");
1491
1492       pad_monitor->check_buffers = FALSE;
1493     } else if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK) {
1494       GST_DEBUG_OBJECT (pad, "Not a sinkpad => no buffer checking");
1495
1496       pad_monitor->check_buffers = FALSE;
1497     } else if (!pad_monitor->caps_is_video) {
1498       GST_DEBUG_OBJECT (pad, "Not working with video => no buffer checking");
1499
1500       pad_monitor->check_buffers = FALSE;
1501     } else if (monitor->media_descriptor == NULL) {
1502       GST_DEBUG_OBJECT (pad, "No media_descriptor set => no buffer checking");
1503
1504       pad_monitor->check_buffers = FALSE;
1505     } else if (!gst_media_descriptor_detects_frames (monitor->media_descriptor)) {
1506       GST_DEBUG_OBJECT (pad, "No frame detection media descriptor "
1507           "=> not buffer checking");
1508       pad_monitor->check_buffers = FALSE;
1509     } else if (pad_monitor->all_bufs == NULL &&
1510         !gst_media_descriptor_get_buffers (monitor->media_descriptor, pad, NULL,
1511             &pad_monitor->all_bufs)) {
1512
1513       GST_INFO_OBJECT (monitor,
1514           "The MediaInfo is marked as detecting frame, but getting frames"
1515           " from pad %" GST_PTR_FORMAT " did not work (some format conversion"
1516           " might be happening)", pad);
1517
1518       pad_monitor->check_buffers = FALSE;
1519     } else {
1520       if (!pad_monitor->current_buf)
1521         pad_monitor->current_buf = pad_monitor->all_bufs;
1522       pad_monitor->check_buffers = TRUE;
1523     }
1524   }
1525
1526   return pad_monitor->check_buffers;
1527 }
1528
1529 static void
1530 gst_validate_monitor_find_next_buffer (GstValidatePadMonitor * pad_monitor)
1531 {
1532   GList *tmp;
1533   gboolean passed_start = FALSE;
1534
1535   if (!_should_check_buffers (pad_monitor, TRUE))
1536     return;
1537
1538   for (tmp = g_list_last (pad_monitor->all_bufs); tmp; tmp = tmp->prev) {
1539     GstBuffer *cbuf = tmp->data;
1540     GstClockTime ts =
1541         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cbuf)) ? GST_BUFFER_DTS (cbuf)
1542         : GST_BUFFER_PTS (cbuf);
1543
1544     if (!GST_CLOCK_TIME_IS_VALID (ts))
1545       continue;
1546
1547     if (ts <= pad_monitor->segment.start)
1548       passed_start = TRUE;
1549
1550     if (!passed_start)
1551       continue;
1552
1553     if (!GST_BUFFER_FLAG_IS_SET (cbuf, GST_BUFFER_FLAG_DELTA_UNIT)) {
1554       break;
1555     }
1556   }
1557
1558   if (tmp == NULL)
1559     pad_monitor->current_buf = pad_monitor->all_bufs;
1560   else
1561     pad_monitor->current_buf = tmp;
1562 }
1563
1564 static gboolean
1565 gst_validate_pad_monitor_downstream_event_check (GstValidatePadMonitor *
1566     pad_monitor, GstObject * parent, GstEvent * event,
1567     GstPadEventFunction handler)
1568 {
1569   gboolean ret = TRUE;
1570   const GstSegment *segment;
1571   guint32 seqnum = gst_event_get_seqnum (event);
1572   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1573
1574   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
1575
1576   /* pre checks */
1577   switch (GST_EVENT_TYPE (event)) {
1578     case GST_EVENT_SEGMENT:
1579       /* parse segment data to be used if event is handled */
1580       gst_event_parse_segment (event, &segment);
1581
1582       GST_DEBUG_OBJECT (pad_monitor->pad, "Got segment %" GST_SEGMENT_FORMAT,
1583           segment);
1584
1585       if (pad_monitor->pending_newsegment_seqnum) {
1586         if (pad_monitor->pending_newsegment_seqnum == seqnum) {
1587           pad_monitor->pending_newsegment_seqnum = 0;
1588         } else {
1589           GST_VALIDATE_REPORT (pad_monitor, SEGMENT_HAS_WRONG_SEQNUM,
1590               "Got: %u Expected: %u", seqnum, pad_monitor->pending_eos_seqnum);
1591         }
1592       }
1593
1594       pad_monitor->pending_eos_seqnum = seqnum;
1595
1596       if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
1597         gst_validate_pad_monitor_add_expected_newsegment (pad_monitor, event);
1598       } else {
1599         /* check if this segment is the expected one */
1600         if (pad_monitor->expected_segment) {
1601           const GstSegment *exp_segment;
1602
1603           if (pad_monitor->expected_segment != event) {
1604             gst_event_parse_segment (pad_monitor->expected_segment,
1605                 &exp_segment);
1606             if (segment->format == exp_segment->format) {
1607               if ((exp_segment->rate * exp_segment->applied_rate !=
1608                       segment->rate * segment->applied_rate))
1609                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1610                     "Rate * applied_rate %d != expected %d",
1611                     segment->rate * segment->applied_rate,
1612                     exp_segment->rate * exp_segment->applied_rate);
1613               if (exp_segment->start != segment->start)
1614                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1615                     "Start %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
1616                     GST_TIME_ARGS (segment->start),
1617                     GST_TIME_ARGS (exp_segment->start));
1618               if (exp_segment->stop != segment->stop)
1619                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1620                     "Stop %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
1621                     GST_TIME_ARGS (segment->stop),
1622                     GST_TIME_ARGS (exp_segment->stop));
1623               if (exp_segment->position != segment->position)
1624                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
1625                     "Position %" GST_TIME_FORMAT " != expected %"
1626                     GST_TIME_FORMAT, GST_TIME_ARGS (segment->position),
1627                     GST_TIME_ARGS (exp_segment->position));
1628             }
1629           }
1630           gst_event_replace (&pad_monitor->expected_segment, NULL);
1631         }
1632       }
1633       break;
1634     case GST_EVENT_CAPS:{
1635       GstCaps *caps;
1636
1637       gst_event_parse_caps (event, &caps);
1638       gst_validate_pad_monitor_setcaps_pre (pad_monitor, caps);
1639       break;
1640     }
1641     case GST_EVENT_EOS:
1642       pad_monitor->is_eos = TRUE;
1643       if (pad_monitor->pending_eos_seqnum == 0) {
1644         GST_VALIDATE_REPORT (pad_monitor, EVENT_EOS_WITHOUT_SEGMENT,
1645             "EOS %" GST_PTR_FORMAT " received before a segment was received",
1646             event);
1647       } else if (pad_monitor->pending_eos_seqnum != seqnum) {
1648         GST_VALIDATE_REPORT (pad_monitor, EOS_HAS_WRONG_SEQNUM,
1649             "Got: %u. Expected: %u", seqnum, pad_monitor->pending_eos_seqnum);
1650       }
1651
1652       /*
1653        * TODO add end of stream checks for
1654        *  - events not pushed
1655        *  - buffer data not pushed
1656        *  - pending events not received
1657        */
1658       break;
1659
1660       /* both flushes are handled by the common event function */
1661     case GST_EVENT_FLUSH_START:
1662     case GST_EVENT_FLUSH_STOP:
1663     case GST_EVENT_TAG:
1664     case GST_EVENT_SINK_MESSAGE:
1665     default:
1666       break;
1667   }
1668
1669   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1670   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1671   gst_validate_pad_monitor_event_overrides (pad_monitor, event);
1672   if (handler) {
1673     gst_event_ref (event);
1674     ret = pad_monitor->event_func (pad, parent, event);
1675   }
1676   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1677   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1678
1679   /* post checks */
1680   switch (GST_EVENT_TYPE (event)) {
1681     case GST_EVENT_SEGMENT:
1682       if (ret) {
1683         if (!pad_monitor->has_segment
1684             && pad_monitor->segment.format != segment->format) {
1685           gst_segment_init (&pad_monitor->segment, segment->format);
1686         }
1687         gst_segment_copy_into (segment, &pad_monitor->segment);
1688         pad_monitor->has_segment = TRUE;
1689         gst_validate_monitor_find_next_buffer (pad_monitor);
1690       }
1691       break;
1692     case GST_EVENT_CAPS:{
1693       GstCaps *caps;
1694
1695       gst_event_parse_caps (event, &caps);
1696       gst_validate_pad_monitor_setcaps_post (pad_monitor, caps, ret);
1697       break;
1698     }
1699     case GST_EVENT_FLUSH_START:
1700     case GST_EVENT_FLUSH_STOP:
1701     case GST_EVENT_EOS:
1702     case GST_EVENT_TAG:
1703     case GST_EVENT_SINK_MESSAGE:
1704     default:
1705       break;
1706   }
1707
1708   if (handler)
1709     gst_event_unref (event);
1710   return ret;
1711 }
1712
1713 static gboolean
1714 gst_validate_pad_monitor_src_event_check (GstValidatePadMonitor * pad_monitor,
1715     GstObject * parent, GstEvent * event, GstPadEventFunction handler)
1716 {
1717   gboolean ret = TRUE;
1718   gdouble rate;
1719   GstFormat format;
1720   gint64 start, stop;
1721   GstSeekFlags seek_flags;
1722   GstSeekType start_type, stop_type;
1723   guint32 seqnum = gst_event_get_seqnum (event);
1724   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1725
1726   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
1727
1728   /* pre checks */
1729   switch (GST_EVENT_TYPE (event)) {
1730     case GST_EVENT_SEEK:
1731     {
1732       gst_event_parse_seek (event, &rate, &format, &seek_flags, &start_type,
1733           &start, &stop_type, &stop);
1734       /* upstream seek - store the seek event seqnum to check
1735        * flushes and newsegments share the same */
1736
1737       /* TODO we might need to use a list as multiple seeks can be sent
1738        * before the flushes arrive here */
1739       if (seek_flags & GST_SEEK_FLAG_FLUSH) {
1740         pad_monitor->pending_flush_start_seqnum = seqnum;
1741         pad_monitor->pending_flush_stop_seqnum = seqnum;
1742       }
1743     }
1744       break;
1745       /* both flushes are handled by the common event handling function */
1746     case GST_EVENT_FLUSH_START:
1747     case GST_EVENT_FLUSH_STOP:
1748     case GST_EVENT_NAVIGATION:
1749     case GST_EVENT_LATENCY:
1750     case GST_EVENT_STEP:
1751     case GST_EVENT_QOS:
1752     default:
1753       break;
1754   }
1755
1756   if (handler) {
1757     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1758     gst_event_ref (event);
1759     ret = pad_monitor->event_func (pad, parent, event);
1760     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1761   }
1762
1763   /* post checks */
1764   switch (GST_EVENT_TYPE (event)) {
1765     case GST_EVENT_FLUSH_START:
1766     case GST_EVENT_FLUSH_STOP:
1767     case GST_EVENT_QOS:
1768     case GST_EVENT_SEEK:
1769     {
1770       if (ret == FALSE) {
1771         /* do not expect any of these events anymore */
1772         pad_monitor->pending_flush_start_seqnum = 0;
1773         pad_monitor->pending_flush_stop_seqnum = 0;
1774         pad_monitor->pending_newsegment_seqnum = 0;
1775         pad_monitor->pending_eos_seqnum = 0;
1776       }
1777     }
1778       break;
1779     case GST_EVENT_NAVIGATION:
1780     case GST_EVENT_LATENCY:
1781     case GST_EVENT_STEP:
1782     default:
1783       break;
1784   }
1785
1786   if (handler)
1787     gst_event_unref (event);
1788   return ret;
1789 }
1790
1791 static gboolean
1792 gst_validate_pad_monitor_check_right_buffer (GstValidatePadMonitor *
1793     pad_monitor, GstBuffer * buffer)
1794 {
1795   gchar *checksum;
1796   GstBuffer *wanted_buf;
1797   GstMapInfo map, wanted_map;
1798
1799   gboolean ret = TRUE;
1800   GstPad *pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
1801
1802   if (_should_check_buffers (pad_monitor, FALSE) == FALSE)
1803     return FALSE;
1804
1805   if (pad_monitor->current_buf == NULL) {
1806     GST_INFO_OBJECT (pad, "No current buffer one pad, Why?");
1807     return FALSE;
1808   }
1809
1810   wanted_buf = pad_monitor->current_buf->data;
1811
1812   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (wanted_buf)) &&
1813       GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buffer)) &&
1814       GST_BUFFER_PTS (wanted_buf) != GST_BUFFER_PTS (buffer)) {
1815
1816     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1817         "buffer %" GST_PTR_FORMAT " PTS %ld"
1818         " different than expected: %ld", buffer,
1819         GST_BUFFER_PTS (buffer), GST_BUFFER_PTS (wanted_buf));
1820
1821     ret = FALSE;
1822   }
1823
1824   if (GST_BUFFER_DTS (wanted_buf) != GST_BUFFER_DTS (buffer)) {
1825     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1826         "buffer %" GST_PTR_FORMAT " DTS %" GST_TIME_FORMAT
1827         " different than expected: %" GST_TIME_FORMAT, buffer,
1828         GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
1829         GST_TIME_ARGS (GST_BUFFER_DTS (wanted_buf)));
1830     ret = FALSE;
1831   }
1832
1833   if (GST_BUFFER_DURATION (wanted_buf) != GST_BUFFER_DURATION (buffer)) {
1834     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1835         "buffer %" GST_PTR_FORMAT " DURATION %" GST_TIME_FORMAT
1836         " different than expected: %" GST_TIME_FORMAT, buffer,
1837         GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
1838         GST_TIME_ARGS (GST_BUFFER_DURATION (wanted_buf)));
1839     ret = FALSE;
1840   }
1841
1842   if (GST_BUFFER_FLAG_IS_SET (wanted_buf, GST_BUFFER_FLAG_DELTA_UNIT) !=
1843       GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
1844     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1845         "buffer %" GST_PTR_FORMAT "  Delta unit is set to %s but expected %s",
1846         buffer, GST_BUFFER_FLAG_IS_SET (buffer,
1847             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False",
1848         GST_BUFFER_FLAG_IS_SET (wanted_buf,
1849             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False");
1850     ret = FALSE;
1851   }
1852
1853   g_assert (gst_buffer_map (wanted_buf, &wanted_map, GST_MAP_READ));
1854   g_assert (gst_buffer_map (buffer, &map, GST_MAP_READ));
1855
1856   checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5,
1857       (const guchar *) map.data, map.size);
1858
1859   if (g_strcmp0 ((gchar *) wanted_map.data, checksum)) {
1860     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
1861         "buffer %" GST_PTR_FORMAT " checksum %s different from expected: %s",
1862         buffer, checksum, wanted_map.data);
1863     ret = FALSE;
1864   }
1865
1866   gst_buffer_unmap (wanted_buf, &wanted_map);
1867   gst_buffer_unmap (buffer, &map);
1868   g_free (checksum);
1869
1870   pad_monitor->current_buf = pad_monitor->current_buf->next;
1871
1872   return ret;
1873 }
1874
1875 static GstFlowReturn
1876 gst_validate_pad_monitor_chain_func (GstPad * pad, GstObject * parent,
1877     GstBuffer * buffer)
1878 {
1879   GstValidatePadMonitor *pad_monitor =
1880       g_object_get_data ((GObject *) pad, "validate-monitor");
1881   GstFlowReturn ret;
1882
1883   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1884   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1885
1886   gst_validate_pad_monitor_check_right_buffer (pad_monitor, buffer);
1887   gst_validate_pad_monitor_check_first_buffer (pad_monitor, buffer);
1888   gst_validate_pad_monitor_update_buffer_data (pad_monitor, buffer);
1889   gst_validate_pad_monitor_check_eos (pad_monitor, buffer);
1890
1891   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1892   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1893
1894   gst_validate_pad_monitor_buffer_overrides (pad_monitor, buffer);
1895
1896   ret = pad_monitor->chain_func (pad, parent, buffer);
1897
1898   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1899   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1900
1901   pad_monitor->last_flow_return = ret;
1902   if (ret == GST_FLOW_EOS) {
1903     mark_pads_eos (pad_monitor);
1904   }
1905   if (PAD_PARENT_IS_DEMUXER (pad_monitor))
1906     gst_validate_pad_monitor_check_aggregated_return (pad_monitor, ret);
1907
1908   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1909   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1910
1911   return ret;
1912 }
1913
1914 static gboolean
1915 gst_validate_pad_monitor_event_is_tracked (GstValidatePadMonitor * monitor,
1916     GstEvent * event)
1917 {
1918   if (!GST_EVENT_IS_SERIALIZED (event)) {
1919     return FALSE;
1920   }
1921
1922   /* we don't track Tag events because they mutate too much and it is hard
1923    * to match a tag event pushed on a source pad with the one that was received
1924    * on a sink pad.
1925    * One idea would be to use seqnum, but it seems that it is undefined whether
1926    * seqnums should be maintained in tag events that are created from others
1927    * up to today. (2013-08-29)
1928    */
1929   if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
1930     return FALSE;
1931
1932   return TRUE;
1933 }
1934
1935 static gboolean
1936 gst_validate_pad_monitor_sink_event_func (GstPad * pad, GstObject * parent,
1937     GstEvent * event)
1938 {
1939   GstValidatePadMonitor *pad_monitor =
1940       g_object_get_data ((GObject *) pad, "validate-monitor");
1941   gboolean ret;
1942
1943   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
1944   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1945
1946   if (gst_validate_pad_monitor_event_is_tracked (pad_monitor, event)) {
1947     GstClockTime last_ts = GST_CLOCK_TIME_NONE;
1948     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_timestamp)) {
1949       last_ts = pad_monitor->current_timestamp;
1950       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_duration)) {
1951         last_ts += pad_monitor->current_duration;
1952       }
1953     }
1954     gst_validate_pad_monitor_otherpad_add_pending_serialized_event (pad_monitor,
1955         event, last_ts);
1956   }
1957
1958   ret =
1959       gst_validate_pad_monitor_downstream_event_check (pad_monitor, parent,
1960       event, pad_monitor->event_func);
1961
1962   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1963   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
1964   return ret;
1965 }
1966
1967 static gboolean
1968 gst_validate_pad_monitor_src_event_func (GstPad * pad, GstObject * parent,
1969     GstEvent * event)
1970 {
1971   GstValidatePadMonitor *pad_monitor =
1972       g_object_get_data ((GObject *) pad, "validate-monitor");
1973   gboolean ret;
1974
1975   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
1976   ret = gst_validate_pad_monitor_src_event_check (pad_monitor, parent, event,
1977       pad_monitor->event_func);
1978   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
1979   return ret;
1980 }
1981
1982 static gboolean
1983 gst_validate_pad_monitor_query_func (GstPad * pad, GstObject * parent,
1984     GstQuery * query)
1985 {
1986   GstValidatePadMonitor *pad_monitor =
1987       g_object_get_data ((GObject *) pad, "validate-monitor");
1988   gboolean ret;
1989
1990   gst_validate_pad_monitor_query_overrides (pad_monitor, query);
1991
1992   ret = pad_monitor->query_func (pad, parent, query);
1993
1994   if (ret) {
1995     switch (GST_QUERY_TYPE (query)) {
1996       case GST_QUERY_CAPS:{
1997         GstCaps *res;
1998         /* We shouldn't need to lock the parent as this doesn't modify
1999          * other monitors, just does some peer_pad_caps */
2000         GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2001
2002         gst_query_parse_caps_result (query, &res);
2003         if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2004           gst_validate_pad_monitor_check_caps_fields_proxied (pad_monitor, res);
2005         }
2006         GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2007         break;
2008       }
2009       default:
2010         break;
2011     }
2012   }
2013
2014   return ret;
2015 }
2016
2017 static gboolean
2018 gst_validate_pad_monitor_activatemode_func (GstPad * pad, GstObject * parent,
2019     GstPadMode mode, gboolean active)
2020 {
2021   GstValidatePadMonitor *pad_monitor =
2022       g_object_get_data ((GObject *) pad, "validate-monitor");
2023   gboolean ret = TRUE;
2024
2025   /* TODO add overrides for activate func */
2026
2027   if (pad_monitor->activatemode_func)
2028     ret = pad_monitor->activatemode_func (pad, parent, mode, active);
2029   if (ret && active == FALSE) {
2030     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2031     gst_validate_pad_monitor_flush (pad_monitor);
2032     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2033   }
2034
2035   return ret;
2036 }
2037
2038 static gboolean
2039 gst_validate_pad_get_range_func (GstPad * pad, GstObject * parent,
2040     guint64 offset, guint size, GstBuffer ** buffer)
2041 {
2042   GstValidatePadMonitor *pad_monitor =
2043       g_object_get_data ((GObject *) pad, "validate-monitor");
2044   gboolean ret;
2045   ret = pad_monitor->getrange_func (pad, parent, offset, size, buffer);
2046   return ret;
2047 }
2048
2049 static gboolean
2050 gst_validate_pad_monitor_buffer_probe (GstPad * pad, GstBuffer * buffer,
2051     gpointer udata)
2052 {
2053   GstValidatePadMonitor *monitor = udata;
2054
2055   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2056   GST_VALIDATE_MONITOR_LOCK (monitor);
2057
2058   gst_validate_pad_monitor_check_first_buffer (monitor, buffer);
2059   gst_validate_pad_monitor_update_buffer_data (monitor, buffer);
2060   gst_validate_pad_monitor_check_eos (monitor, buffer);
2061
2062   if (PAD_PARENT_IS_DECODER (monitor) || PAD_PARENT_IS_ENCODER (monitor)) {
2063     GstClockTime tolerance = 0;
2064
2065     if (monitor->caps_is_audio)
2066       tolerance = AUDIO_TIMESTAMP_TOLERANCE;
2067
2068     gst_validate_pad_monitor_check_buffer_timestamp_in_received_range (monitor,
2069         buffer, tolerance);
2070   }
2071
2072   gst_validate_pad_monitor_check_late_serialized_events (monitor,
2073       GST_BUFFER_TIMESTAMP (buffer));
2074
2075   /* a GstValidatePadMonitor parent must be a GstValidateElementMonitor */
2076   if (PAD_PARENT_IS_DECODER (monitor)) {
2077
2078     /* should not push out of segment data */
2079     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer)) &&
2080         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer)) &&
2081         ((!gst_segment_clip (&monitor->segment, monitor->segment.format,
2082                     GST_BUFFER_TIMESTAMP (buffer),
2083                     GST_BUFFER_TIMESTAMP (buffer) +
2084                     GST_BUFFER_DURATION (buffer), NULL, NULL)) ||
2085             /* In the case of raw data, buffers should be strictly contained inside the
2086              * segment */
2087             (monitor->caps_is_raw &&
2088                 GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer) <
2089                 monitor->segment.start))
2090         ) {
2091       /* TODO is this a timestamp issue? */
2092       GST_VALIDATE_REPORT (monitor, BUFFER_IS_OUT_OF_SEGMENT,
2093           "buffer is out of segment and shouldn't be pushed. Timestamp: %"
2094           GST_TIME_FORMAT " - duration: %" GST_TIME_FORMAT ". Range: %"
2095           GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
2096           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2097           GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
2098           GST_TIME_ARGS (monitor->segment.start),
2099           GST_TIME_ARGS (monitor->segment.stop));
2100     }
2101   }
2102
2103   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2104   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2105   gst_validate_pad_monitor_buffer_probe_overrides (monitor, buffer);
2106   return TRUE;
2107 }
2108
2109 static gboolean
2110 gst_validate_pad_monitor_event_probe (GstPad * pad, GstEvent * event,
2111     gpointer udata)
2112 {
2113   GstValidatePadMonitor *monitor = GST_VALIDATE_PAD_MONITOR_CAST (udata);
2114   gboolean ret;
2115
2116   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2117   GST_VALIDATE_MONITOR_LOCK (monitor);
2118
2119   GST_DEBUG_OBJECT (pad, "event %p %s", event, GST_EVENT_TYPE_NAME (event));
2120
2121   if (GST_EVENT_IS_SERIALIZED (event)) {
2122     gint i;
2123
2124     /* Detect if events the element received are being forwarded in the same order
2125      *
2126      * Several scenarios:
2127      * 1) The element pushes the event as-is
2128      * 2) The element consumes the event and does not forward it
2129      * 3) The element consumes the event and creates another one instead
2130      * 4) The element pushes other serialized event before pushing out the
2131      *    one it received
2132      *
2133      * For each pad we have two lists to track serialized events:
2134      *  1) We received on input and expect to see (serialized_events)
2135      *  2) We received on input but don't expect to see (expired_events)
2136      *
2137      * To detect events that are pushed in a different order from the one they were
2138      * received in we check that:
2139      *
2140      * For each event being outputted:
2141      *   If it is in the expired_events list:
2142      *     RAISE WARNING
2143      *   If it is in the serialized_events list:
2144      *     If there are other events that were received before:
2145      *        Put those events on the expired_events list
2146      *     Remove that event and any previous ones from the serialized_events list
2147      *
2148      * Clear expired events list when flushing or on pad deactivation
2149      *
2150      */
2151
2152     if (g_list_find (monitor->expired_events, event)) {
2153       gchar *event_str = _get_event_string (event);
2154       /* If it's the expired events, we've failed */
2155       GST_WARNING_OBJECT (pad, "Did not expect event %p %s", event,
2156           GST_EVENT_TYPE_NAME (event));
2157       GST_VALIDATE_REPORT (monitor, EVENT_SERIALIZED_OUT_OF_ORDER,
2158           "Serialized event was pushed out of order: %s", event_str);
2159
2160       g_free (event_str);
2161       monitor->expired_events = g_list_remove (monitor->expired_events, event);
2162       gst_event_unref (event);  /* remove the ref that was on the list */
2163     } else if (monitor->serialized_events->len) {
2164       for (i = 0; i < monitor->serialized_events->len; i++) {
2165         SerializedEventData *next_event =
2166             g_ptr_array_index (monitor->serialized_events, i);
2167         GST_DEBUG_OBJECT (pad, "Checking against stored event #%d: %p %s", i,
2168             next_event->event, GST_EVENT_TYPE_NAME (next_event->event));
2169
2170         if (event == next_event->event
2171             || GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2172           /* We have found our event */
2173           GST_DEBUG_OBJECT (pad, "Found matching event");
2174
2175           while (monitor->serialized_events->len > i
2176               && GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2177             /* Swallow all expected events of the same type */
2178             g_ptr_array_remove_index (monitor->serialized_events, i);
2179             next_event = g_ptr_array_index (monitor->serialized_events, i);
2180           }
2181
2182           /* Move all previous events to expired events */
2183           if (G_UNLIKELY (i > 0)) {
2184             GST_DEBUG_OBJECT (pad,
2185                 "Moving previous expected events to expired list");
2186             while (i--) {
2187               next_event = g_ptr_array_index (monitor->serialized_events, 0);
2188               monitor->expired_events =
2189                   g_list_append (monitor->expired_events,
2190                   gst_event_ref (next_event->event));
2191               g_ptr_array_remove_index (monitor->serialized_events, 0);
2192             }
2193           }
2194           debug_pending_event (pad, monitor->serialized_events);
2195           break;
2196         }
2197       }
2198     }
2199   }
2200
2201   /* This so far is just like an event that is flowing downstream,
2202    * so we do the same checks as a sinkpad event handler */
2203   ret =
2204       gst_validate_pad_monitor_downstream_event_check (monitor, NULL, event,
2205       NULL);
2206   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2207   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2208
2209   return ret;
2210 }
2211
2212 static GstPadProbeReturn
2213 gst_validate_pad_monitor_pad_probe (GstPad * pad, GstPadProbeInfo * info,
2214     gpointer udata)
2215 {
2216   if (info->type & GST_PAD_PROBE_TYPE_BUFFER)
2217     gst_validate_pad_monitor_buffer_probe (pad, info->data, udata);
2218   else if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM)
2219     gst_validate_pad_monitor_event_probe (pad, info->data, udata);
2220
2221   return GST_PAD_PROBE_OK;
2222 }
2223
2224 static void
2225 gst_validate_pad_monitor_update_caps_info (GstValidatePadMonitor * pad_monitor,
2226     GstCaps * caps)
2227 {
2228   GstStructure *structure;
2229
2230   g_return_if_fail (gst_caps_is_fixed (caps));
2231
2232   pad_monitor->caps_is_audio = FALSE;
2233   pad_monitor->caps_is_video = FALSE;
2234
2235   structure = gst_caps_get_structure (caps, 0);
2236   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/")) {
2237     pad_monitor->caps_is_audio = TRUE;
2238   } else if (g_str_has_prefix (gst_structure_get_name (structure), "video/")) {
2239     pad_monitor->caps_is_video = TRUE;
2240   }
2241
2242   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/x-raw") ||
2243       g_str_has_prefix (gst_structure_get_name (structure), "video/x-raw")) {
2244     pad_monitor->caps_is_raw = TRUE;
2245   } else {
2246     pad_monitor->caps_is_raw = FALSE;
2247   }
2248 }
2249
2250 static void
2251 gst_validate_pad_monitor_setcaps_pre (GstValidatePadMonitor * pad_monitor,
2252     GstCaps * caps)
2253 {
2254   GstStructure *structure;
2255
2256   /* Check if caps are identical to last caps and complain if so
2257    * Only checked for sink pads as src pads might push the same caps
2258    * multiple times during unlinked/autoplugging scenarios */
2259   if (GST_PAD_IS_SINK (GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor)) &&
2260       pad_monitor->last_caps
2261       && gst_caps_is_equal (caps, pad_monitor->last_caps)) {
2262     gchar *caps_str = gst_caps_to_string (caps);
2263
2264     GST_VALIDATE_REPORT (pad_monitor, EVENT_CAPS_DUPLICATE, "%s", caps_str);
2265     g_free (caps_str);
2266
2267   }
2268
2269   gst_validate_pad_monitor_check_caps_complete (pad_monitor, caps);
2270
2271   if (caps) {
2272     structure = gst_caps_get_structure (caps, 0);
2273     if (gst_structure_n_fields (pad_monitor->pending_setcaps_fields)) {
2274       gint i;
2275       for (i = 0;
2276           i < gst_structure_n_fields (pad_monitor->pending_setcaps_fields);
2277           i++) {
2278         const gchar *name =
2279             gst_structure_nth_field_name (pad_monitor->pending_setcaps_fields,
2280             i);
2281         const GValue *v = gst_structure_get_value (structure, name);
2282         const GValue *otherv =
2283             gst_structure_get_value (pad_monitor->pending_setcaps_fields, name);
2284
2285         if (v == NULL) {
2286           gchar *caps_str = gst_caps_to_string (caps);
2287
2288           GST_VALIDATE_REPORT (pad_monitor, CAPS_EXPECTED_FIELD_NOT_FOUND,
2289               "Field %s is missing from setcaps caps '%s'", name, caps_str);
2290           g_free (caps_str);
2291         } else if (gst_value_compare (v, otherv) != GST_VALUE_EQUAL) {
2292           gchar *caps_str = gst_caps_to_string (caps),
2293               *pending_setcaps_fields_str =
2294               gst_structure_to_string (pad_monitor->pending_setcaps_fields);
2295
2296
2297           GST_VALIDATE_REPORT (pad_monitor, CAPS_FIELD_UNEXPECTED_VALUE,
2298               "Field %s from setcaps caps '%s' is different "
2299               "from expected value in caps '%s'", name, caps_str,
2300               pending_setcaps_fields_str);
2301
2302           g_free (pending_setcaps_fields_str);
2303           g_free (caps_str);
2304         }
2305       }
2306     }
2307
2308     if (gst_validate_pad_monitor_pad_should_proxy_othercaps (pad_monitor)) {
2309       if (_structure_is_video (structure)) {
2310         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2311             structure, "width");
2312         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2313             structure, "height");
2314         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2315             structure, "framerate");
2316         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2317             structure, "pixel-aspect-ratio");
2318       } else if (_structure_is_audio (structure)) {
2319         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2320             structure, "rate");
2321         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2322             structure, "channels");
2323       }
2324     }
2325   }
2326
2327   gst_structure_free (pad_monitor->pending_setcaps_fields);
2328   pad_monitor->pending_setcaps_fields =
2329       gst_structure_new_empty (PENDING_FIELDS);
2330
2331   gst_validate_pad_monitor_setcaps_overrides (pad_monitor, caps);
2332 }
2333
2334 static void
2335 gst_validate_pad_monitor_setcaps_post (GstValidatePadMonitor * pad_monitor,
2336     GstCaps * caps, gboolean ret)
2337 {
2338   if (!ret)
2339     gst_validate_pad_monitor_otherpad_clear_pending_fields (pad_monitor);
2340   else {
2341     if (pad_monitor->last_caps) {
2342       gst_caps_unref (pad_monitor->last_caps);
2343     }
2344     pad_monitor->last_caps = gst_caps_ref (caps);
2345     gst_validate_pad_monitor_update_caps_info (pad_monitor, caps);
2346   }
2347 }
2348
2349 static gboolean
2350 gst_validate_pad_monitor_do_setup (GstValidateMonitor * monitor)
2351 {
2352   GstValidatePadMonitor *pad_monitor = GST_VALIDATE_PAD_MONITOR_CAST (monitor);
2353   GstPad *pad;
2354   if (!GST_IS_PAD (GST_VALIDATE_MONITOR_GET_OBJECT (monitor))) {
2355     GST_WARNING_OBJECT (monitor, "Trying to create pad monitor with other "
2356         "type of object");
2357     return FALSE;
2358   }
2359
2360   pad = GST_VALIDATE_PAD_MONITOR_GET_PAD (pad_monitor);
2361
2362   if (g_object_get_data ((GObject *) pad, "validate-monitor")) {
2363     GST_WARNING_OBJECT (pad_monitor,
2364         "Pad already has a validate-monitor associated");
2365     return FALSE;
2366   }
2367
2368   g_object_set_data ((GObject *) pad, "validate-monitor", pad_monitor);
2369
2370   pad_monitor->pad = pad;
2371
2372   pad_monitor->event_func = GST_PAD_EVENTFUNC (pad);
2373   pad_monitor->query_func = GST_PAD_QUERYFUNC (pad);
2374   pad_monitor->activatemode_func = GST_PAD_ACTIVATEMODEFUNC (pad);
2375   if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2376
2377     pad_monitor->chain_func = GST_PAD_CHAINFUNC (pad);
2378     if (pad_monitor->chain_func)
2379       gst_pad_set_chain_function (pad, gst_validate_pad_monitor_chain_func);
2380
2381     gst_pad_set_event_function (pad, gst_validate_pad_monitor_sink_event_func);
2382   } else {
2383     pad_monitor->getrange_func = GST_PAD_GETRANGEFUNC (pad);
2384     if (pad_monitor->getrange_func)
2385       gst_pad_set_getrange_function (pad, gst_validate_pad_get_range_func);
2386
2387     gst_pad_set_event_function (pad, gst_validate_pad_monitor_src_event_func);
2388
2389     /* add buffer/event probes */
2390     pad_monitor->pad_probe_id =
2391         gst_pad_add_probe (pad,
2392         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
2393         GST_PAD_PROBE_TYPE_EVENT_FLUSH,
2394         (GstPadProbeCallback) gst_validate_pad_monitor_pad_probe, pad_monitor,
2395         NULL);
2396   }
2397   gst_pad_set_query_function (pad, gst_validate_pad_monitor_query_func);
2398   gst_pad_set_activatemode_function (pad,
2399       gst_validate_pad_monitor_activatemode_func);
2400
2401   gst_validate_reporter_set_name (GST_VALIDATE_REPORTER (monitor),
2402       g_strdup_printf ("%s:%s", GST_DEBUG_PAD_NAME (pad)));
2403
2404   if (G_UNLIKELY (GST_PAD_PARENT (pad) == NULL))
2405     GST_FIXME ("Saw a pad not belonging to any object");
2406
2407   return TRUE;
2408 }