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