validate-scenario: fix g-i warning in annotation
[platform/upstream/gstreamer.git] / subprojects / gst-devtools / validate / gst / validate / gst-validate-pad-monitor.c
1 /* GStreamer
2  *
3  * Copyright (C) 2013 Collabora Ltd.
4  *  Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>
5  *
6  * gst-validate-pad-monitor.c - Validate PadMonitor class
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "gst-validate-internal.h"
29 #include "gst-validate-pad-monitor.h"
30 #include "gst-validate-element-monitor.h"
31 #include "gst-validate-pipeline-monitor.h"
32 #include "gst-validate-reporter.h"
33 #include "gst-validate-utils.h"
34 #include "validate.h"
35 #include <string.h>
36 #include <stdarg.h>
37
38 /**
39  * SECTION:gst-validate-pad-monitor
40  * @short_description: Class that wraps a #GstPad for Validate checks
41  *
42  * TODO
43  */
44 #define _GET_PAD_MONITOR(p) g_object_get_qdata ((GObject*) p, _Q_VALIDATE_MONITOR)
45 #define _SET_PAD_MONITOR(p,d) g_object_set_qdata ((GObject*) p, _Q_VALIDATE_MONITOR, d)
46
47 static GstValidateInterceptionReturn
48 gst_validate_pad_monitor_intercept_report (GstValidateReporter * reporter,
49     GstValidateReport * report);
50
51 #define _do_init \
52   G_IMPLEMENT_INTERFACE (GST_TYPE_VALIDATE_REPORTER, _reporter_iface_init)
53
54 static void
55 _reporter_iface_init (GstValidateReporterInterface * iface)
56 {
57   iface->intercept_report = gst_validate_pad_monitor_intercept_report;
58 }
59
60 #define gst_validate_pad_monitor_parent_class parent_class
61 G_DEFINE_TYPE_WITH_CODE (GstValidatePadMonitor, gst_validate_pad_monitor,
62     GST_TYPE_VALIDATE_MONITOR, _do_init);
63
64 #define PENDING_FIELDS "pending-fields"
65 #define AUDIO_TIMESTAMP_TOLERANCE (GST_MSECOND * 100)
66
67 #define PAD_PARENT_IS_DEMUXER(m) \
68     (GST_VALIDATE_MONITOR_GET_PARENT(m) ? \
69         GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_DEMUXER ( \
70             GST_VALIDATE_MONITOR_GET_PARENT(m)) : \
71         FALSE)
72
73 #define PAD_PARENT_IS_DECODER(m) \
74     (GST_VALIDATE_MONITOR_GET_PARENT(m) ? \
75         GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_DECODER ( \
76             GST_VALIDATE_MONITOR_GET_PARENT(m)) : \
77         FALSE)
78
79 #define PAD_PARENT_IS_ENCODER(m) \
80     (GST_VALIDATE_MONITOR_GET_PARENT(m) ? \
81         GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_ENCODER ( \
82             GST_VALIDATE_MONITOR_GET_PARENT(m)) : \
83         FALSE)
84
85 #define PAD_PARENT_IS_SINK(m) \
86     (GST_VALIDATE_MONITOR_GET_PARENT(m) ? \
87         GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_SINK ( \
88             GST_VALIDATE_MONITOR_GET_PARENT(m)) : \
89         FALSE)
90
91
92 /*
93  * Locking the parent should always be done before locking the
94  * pad-monitor to prevent deadlocks in case another monitor from
95  * another pad on the same element starts an operation that also
96  * requires locking itself and some other monitors from internally
97  * linked pads.
98  *
99  * An example:
100  * An element has a sink and a src pad. Some test starts running at sinkpad
101  * and it locks the parent, and then it locks itself. In case it needs to get
102  * some information from the srcpad, it is able to lock the srcpad and get it
103  * because the srcpad should never lock itself before locking the parent (which
104  * it won't be able as sinkpad already locked it).
105  *
106  * As a side one, it is possible that srcpad locks itself without locking the
107  * parent in case it wants to do a check that won't need to use other internally
108  * linked pads (sinkpad). But in this case it might lock and unlock freely without
109  * causing deadlocks.
110  */
111 #define GST_VALIDATE_PAD_MONITOR_PARENT_LOCK(m)                  \
112 G_STMT_START {                                             \
113   if (G_LIKELY (GST_VALIDATE_MONITOR_GET_PARENT (m))) {          \
114     GST_VALIDATE_MONITOR_LOCK (GST_VALIDATE_MONITOR_GET_PARENT (m));   \
115   } else {                                                 \
116     GST_WARNING_OBJECT (m, "No parent found, can't lock"); \
117   }                                                        \
118 } G_STMT_END
119
120 #define GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK(m)                  \
121 G_STMT_START {                                               \
122   if (G_LIKELY (GST_VALIDATE_MONITOR_GET_PARENT (m))) {            \
123     GST_VALIDATE_MONITOR_UNLOCK (GST_VALIDATE_MONITOR_GET_PARENT (m));   \
124   } else {                                                   \
125     GST_WARNING_OBJECT (m, "No parent found, can't unlock"); \
126   }                                                          \
127 } G_STMT_END
128
129 /* Structure used to store all seek-related information */
130 struct _GstValidatePadSeekData
131 {
132   guint32 seqnum;
133   gdouble rate;
134   GstFormat format;
135   GstSeekFlags flags;
136   GstSeekType start_type, stop_type;
137   gint64 start, stop;
138 };
139
140 typedef struct
141 {
142   GstClockTime timestamp;
143   GstEvent *event;
144 } SerializedEventData;
145
146 static GstPad *
147 _get_actual_pad (GstPad * pad)
148 {
149   pad = gst_object_ref (pad);
150
151   while (GST_IS_PROXY_PAD (pad)) {
152     GstPad *next_pad;
153
154     if (GST_PAD_IS_SINK (pad)) {
155       if (GST_IS_GHOST_PAD (pad))
156         next_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
157       else
158         next_pad = GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (pad)));
159     } else {
160       next_pad = gst_pad_get_peer (pad);
161     }
162
163     gst_object_unref (pad);
164     if (!next_pad)
165       return NULL;
166
167     pad = next_pad;
168   }
169
170   return pad;
171 }
172
173 static gboolean
174 _find_master_report_on_pad (GstPad * pad, GstValidateReport * report)
175 {
176   GstValidatePadMonitor *pad_monitor;
177   GstValidateReport *prev_report;
178   gboolean result = FALSE;
179   GstPad *tmppad = pad;
180
181   pad = _get_actual_pad (pad);
182   if (pad == NULL) {
183     GST_ERROR_OBJECT (tmppad, "Does not have a target yet");
184
185     return FALSE;
186   }
187
188   pad_monitor = _GET_PAD_MONITOR (pad);
189
190   /* For some reason this pad isn't monitored */
191   if (pad_monitor == NULL)
192     goto done;
193
194   prev_report = gst_validate_reporter_get_report ((GstValidateReporter *)
195       pad_monitor, report->issue->issue_id);
196
197   if (prev_report) {
198     if (prev_report->master_report)
199       result = gst_validate_report_set_master_report (report,
200           prev_report->master_report);
201     else
202       result = gst_validate_report_set_master_report (report, prev_report);
203   }
204
205 done:
206   gst_object_unref (pad);
207
208   return result;
209 }
210
211 static gboolean
212 _find_master_report_for_sink_pad (GstValidatePadMonitor * pad_monitor,
213     GstValidateReport * report)
214 {
215   GstPad *peerpad;
216   gboolean result = FALSE;
217   GstPad *pad =
218       GST_PAD_CAST (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
219           (pad_monitor)));
220
221   peerpad = gst_pad_get_peer (pad);
222   gst_object_unref (pad);
223
224   /* If the peer src pad already has a similar report no need to look
225    * any further */
226   if (peerpad && _find_master_report_on_pad (peerpad, report))
227     result = TRUE;
228
229   if (peerpad)
230     gst_object_unref (peerpad);
231
232   return result;
233 }
234
235 static gboolean
236 _find_master_report_for_src_pad (GstValidatePadMonitor * pad_monitor,
237     GstValidateReport * report)
238 {
239   GstIterator *iter;
240   gboolean done;
241   gboolean result = FALSE;
242   GstPad *target =
243       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
244           (pad_monitor)));
245
246   iter = gst_pad_iterate_internal_links (target);
247   done = FALSE;
248   while (!done) {
249     GValue value = { 0, };
250     switch (gst_iterator_next (iter, &value)) {
251       case GST_ITERATOR_OK:
252       {
253         GstPad *pad = g_value_get_object (&value);
254
255         if (_find_master_report_on_pad (pad, report)) {
256           result = TRUE;
257           done = TRUE;
258         }
259
260         g_value_reset (&value);
261         break;
262       }
263       case GST_ITERATOR_RESYNC:
264         gst_iterator_resync (iter);
265         break;
266       case GST_ITERATOR_ERROR:
267         GST_WARNING_OBJECT (target, "Internal links pad iteration error");
268         done = TRUE;
269         break;
270       case GST_ITERATOR_DONE:
271         done = TRUE;
272         break;
273     }
274   }
275   gst_object_unref (target);
276   gst_iterator_free (iter);
277
278   return result;
279 }
280
281 static GstValidateInterceptionReturn
282 _concatenate_issues (GstValidatePadMonitor * pad_monitor,
283     GstValidateReport * report)
284 {
285   GstPad *pad =
286       GST_PAD_CAST (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
287           (pad_monitor)));
288
289   if (GST_PAD_IS_SINK (pad)
290       && _find_master_report_for_sink_pad (pad_monitor, report)) {
291     gst_object_unref (pad);
292     return GST_VALIDATE_REPORTER_KEEP;
293   } else if (GST_PAD_IS_SRC (pad)
294       && _find_master_report_for_src_pad (pad_monitor, report)) {
295     gst_object_unref (pad);
296     return GST_VALIDATE_REPORTER_KEEP;
297   }
298
299   gst_object_unref (pad);
300   return GST_VALIDATE_REPORTER_REPORT;
301 }
302
303 static GstValidateInterceptionReturn
304 gst_validate_pad_monitor_intercept_report (GstValidateReporter *
305     reporter, GstValidateReport * report)
306 {
307   GstValidateReporterInterface *iface_class, *old_iface_class;
308   GstValidatePadMonitor *pad_monitor = GST_VALIDATE_PAD_MONITOR (reporter);
309   GstValidateReportingDetails monitor_reporting_level;
310   GstValidateInterceptionReturn ret;
311
312   monitor_reporting_level =
313       gst_validate_reporter_get_reporting_level (reporter);
314
315   iface_class =
316       G_TYPE_INSTANCE_GET_INTERFACE (reporter, GST_TYPE_VALIDATE_REPORTER,
317       GstValidateReporterInterface);
318   old_iface_class = g_type_interface_peek_parent (iface_class);
319
320   old_iface_class->intercept_report (reporter, report);
321
322   switch (monitor_reporting_level) {
323     case GST_VALIDATE_SHOW_NONE:
324       ret = GST_VALIDATE_REPORTER_DROP;
325       break;
326     case GST_VALIDATE_SHOW_UNKNOWN:
327       ret = _concatenate_issues (pad_monitor, report);
328       break;
329     default:
330       ret = GST_VALIDATE_REPORTER_REPORT;
331       break;
332   }
333
334   gst_validate_report_set_reporting_level (report, monitor_reporting_level);
335   return ret;
336 }
337
338 static void
339 debug_pending_event (GstPad * pad, GPtrArray * array)
340 {
341   guint i, len;
342
343   len = array->len;
344   for (i = 0; i < len; i++) {
345     SerializedEventData *data = g_ptr_array_index (array, i);
346     GST_DEBUG_OBJECT (pad, "event #%d %" GST_TIME_FORMAT " %s %p",
347         i, GST_TIME_ARGS (data->timestamp),
348         GST_EVENT_TYPE_NAME (data->event), data->event);
349   }
350 }
351
352 static void
353 _serialized_event_data_free (SerializedEventData * serialized_event)
354 {
355   gst_event_unref (serialized_event->event);
356   g_slice_free (SerializedEventData, serialized_event);
357 }
358
359 static gboolean gst_validate_pad_monitor_do_setup (GstValidateMonitor *
360     monitor);
361 static GstElement *gst_validate_pad_monitor_get_element (GstValidateMonitor *
362     monitor);
363 static void
364 gst_validate_pad_monitor_setcaps_pre (GstValidatePadMonitor * pad_monitor,
365     GstCaps * caps);
366 static void gst_validate_pad_monitor_setcaps_post (GstValidatePadMonitor *
367     pad_monitor, GstCaps * caps, gboolean ret);
368
369 #define PAD_IS_IN_PUSH_MODE(p) ((p)->mode == GST_PAD_MODE_PUSH)
370
371 static gboolean
372 _structure_is_raw_video (GstStructure * structure)
373 {
374   return gst_structure_has_name (structure, "video/x-raw");
375 }
376
377 static gboolean
378 _structure_is_raw_audio (GstStructure * structure)
379 {
380   return gst_structure_has_name (structure, "audio/x-raw");
381 }
382
383 static gchar *
384 _get_event_string (GstEvent * event)
385 {
386   const GstStructure *st;
387
388   if ((st = gst_event_get_structure (event)))
389     return gst_structure_to_string (st);
390   else
391     return g_strdup_printf ("%s", GST_EVENT_TYPE_NAME (event));
392 }
393
394 static void
395 _check_field_type (GstValidatePadMonitor * monitor,
396     GstStructure * structure, gboolean mandatory, const gchar * field, ...)
397 {
398   va_list var_args;
399   GType type;
400   gchar *joined_types = NULL;
401   const gchar *rejected_types[5];
402   gint rejected_types_index = 0;
403   gchar *struct_str;
404
405   if (!gst_structure_has_field (structure, field)) {
406     if (mandatory) {
407       gchar *str = gst_structure_to_string (structure);
408
409       GST_VALIDATE_REPORT (monitor, CAPS_IS_MISSING_FIELD,
410           "Field '%s' is missing from structure: %s", field, str);
411       g_free (str);
412     } else {
413       GST_DEBUG_OBJECT (monitor, "Field %s is missing but is not mandatory",
414           field);
415     }
416     return;
417   }
418
419   memset ((gchar **) rejected_types, 0, sizeof (rejected_types));
420   va_start (var_args, field);
421   while ((type = va_arg (var_args, GType)) != 0) {
422     if (gst_structure_has_field_typed (structure, field, type)) {
423       va_end (var_args);
424       return;
425     }
426     rejected_types[rejected_types_index++] = g_type_name (type);
427   }
428   va_end (var_args);
429
430   joined_types = g_strjoinv (" / ", (gchar **) rejected_types);
431   struct_str = gst_structure_to_string (structure);
432   GST_VALIDATE_REPORT (monitor, CAPS_FIELD_HAS_BAD_TYPE,
433       "Field '%s' has wrong type %s in structure '%s'. Expected: %s", field,
434       g_type_name (gst_structure_get_field_type (structure, field)), struct_str,
435       joined_types);
436   g_free (joined_types);
437   g_free (struct_str);
438 }
439
440 static void
441 gst_validate_pad_monitor_check_raw_video_caps_complete (GstValidatePadMonitor *
442     monitor, GstStructure * structure)
443 {
444   _check_field_type (monitor, structure, TRUE, "width", G_TYPE_INT,
445       GST_TYPE_INT_RANGE, 0);
446   _check_field_type (monitor, structure, TRUE, "height", G_TYPE_INT,
447       GST_TYPE_INT_RANGE, 0);
448   _check_field_type (monitor, structure, TRUE, "framerate", GST_TYPE_FRACTION,
449       GST_TYPE_FRACTION_RANGE, 0);
450   _check_field_type (monitor, structure, FALSE, "pixel-aspect-ratio",
451       GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE, 0);
452   _check_field_type (monitor, structure, TRUE, "format", G_TYPE_STRING,
453       GST_TYPE_LIST);
454 }
455
456 static void
457 gst_validate_pad_monitor_check_raw_audio_caps_complete (GstValidatePadMonitor *
458     monitor, GstStructure * structure)
459 {
460   gint channels;
461   _check_field_type (monitor, structure, TRUE, "format", G_TYPE_STRING,
462       GST_TYPE_LIST, 0);
463   _check_field_type (monitor, structure, TRUE, "layout", G_TYPE_STRING,
464       GST_TYPE_LIST, 0);
465   _check_field_type (monitor, structure, TRUE, "rate", G_TYPE_INT,
466       GST_TYPE_LIST, GST_TYPE_INT_RANGE, 0);
467   _check_field_type (monitor, structure, TRUE, "channels", G_TYPE_INT,
468       GST_TYPE_LIST, GST_TYPE_INT_RANGE, 0);
469   if (gst_structure_get_int (structure, "channels", &channels)) {
470     if (channels > 2)
471       _check_field_type (monitor, structure, TRUE, "channel-mask",
472           GST_TYPE_BITMASK, GST_TYPE_LIST, 0);
473   }
474 }
475
476 static void
477 gst_validate_pad_monitor_check_caps_complete (GstValidatePadMonitor * monitor,
478     GstCaps * caps)
479 {
480   GstStructure *structure;
481   gint i;
482
483   GST_DEBUG_OBJECT (monitor, "Checking caps %" GST_PTR_FORMAT, caps);
484
485   for (i = 0; i < gst_caps_get_size (caps); i++) {
486     structure = gst_caps_get_structure (caps, i);
487
488     if (_structure_is_raw_video (structure)) {
489       gst_validate_pad_monitor_check_raw_video_caps_complete (monitor,
490           structure);
491
492     } else if (_structure_is_raw_audio (structure)) {
493       gst_validate_pad_monitor_check_raw_audio_caps_complete (monitor,
494           structure);
495     }
496   }
497 }
498
499 static GstCaps *
500 gst_validate_pad_monitor_get_othercaps (GstValidatePadMonitor * monitor,
501     GstCaps * filter)
502 {
503   GstCaps *caps = gst_caps_new_empty ();
504   GstIterator *iter;
505   gboolean done;
506   GstPad *otherpad;
507   GstCaps *peercaps;
508   GstPad *pad =
509       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
510           (monitor)));
511
512   iter = gst_pad_iterate_internal_links (pad);
513   done = FALSE;
514   while (!done) {
515     GValue value = { 0, };
516     switch (gst_iterator_next (iter, &value)) {
517       case GST_ITERATOR_OK:
518         otherpad = g_value_get_object (&value);
519
520         /* TODO What would be the correct caps operation to merge the caps in
521          * case one sink is internally linked to multiple srcs? */
522         peercaps = gst_pad_peer_query_caps (otherpad, filter);
523         if (peercaps)
524           caps = gst_caps_merge (caps, peercaps);
525
526         g_value_reset (&value);
527         break;
528       case GST_ITERATOR_RESYNC:
529         gst_iterator_resync (iter);
530         gst_caps_unref (caps);
531         caps = gst_caps_new_empty ();
532         break;
533       case GST_ITERATOR_ERROR:
534         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
535         done = TRUE;
536         break;
537       case GST_ITERATOR_DONE:
538         done = TRUE;
539         break;
540     }
541   }
542   GST_DEBUG_OBJECT (pad, "Otherpad caps: %" GST_PTR_FORMAT, caps);
543
544   gst_iterator_free (iter);
545   gst_object_unref (pad);
546
547   return caps;
548 }
549
550 static gboolean
551 _structure_is_video (GstStructure * structure)
552 {
553   const gchar *name = gst_structure_get_name (structure);
554
555   return g_strstr_len (name, 6, "video/")
556       && g_strcmp0 (name, "video/quicktime") != 0;
557 }
558
559 static gboolean
560 _structure_is_audio (GstStructure * structure)
561 {
562   const gchar *name = gst_structure_get_name (structure);
563
564   return g_strstr_len (name, 6, "audio/") != NULL;
565 }
566
567 static gboolean
568 gst_validate_pad_monitor_pad_should_proxy_othercaps (GstValidatePadMonitor *
569     monitor)
570 {
571   GstValidateMonitor *parent = GST_VALIDATE_MONITOR_GET_PARENT (monitor);
572
573   if (!parent)
574     return FALSE;
575
576   /* We only know how to handle othercaps checks for codecs so far */
577   return (GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_DECODER (parent) ||
578       GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_ENCODER (parent)) &&
579       !GST_VALIDATE_ELEMENT_MONITOR_ELEMENT_IS_CONVERTER (parent);
580 }
581
582
583 /* Check if the field @f from @s2 (if present) is represented in @s1
584  * Represented here means either equal or @s1's value is in a list/range
585  * from @s2
586  */
587 static gboolean
588 _structures_field_is_contained (GstStructure * s1, GstStructure * s2,
589     gboolean mandatory, const gchar * f)
590 {
591   const GValue *v1;
592   const GValue *v2;
593
594   v2 = gst_structure_get_value (s2, f);
595   if (!v2)
596     return TRUE;                /* nothing to compare to */
597
598   v1 = gst_structure_get_value (s1, f);
599   if (!v1)
600     return !mandatory;
601
602   if (!gst_value_is_fixed (v1))
603     return TRUE;
604
605   if (gst_value_compare (v1, v2) == GST_VALUE_EQUAL)
606     return TRUE;
607
608   if (GST_VALUE_HOLDS_LIST (v2)) {
609     gint i;
610     for (i = 0; i < gst_value_list_get_size (v2); i++) {
611       const GValue *v2_subvalue = gst_value_list_get_value (v2, i);
612       if (gst_value_compare (v1, v2_subvalue) == GST_VALUE_EQUAL)
613         return TRUE;
614     }
615   }
616
617   if (GST_VALUE_HOLDS_ARRAY (v2)) {
618     gint i;
619     for (i = 0; i < gst_value_array_get_size (v2); i++) {
620       const GValue *v2_subvalue = gst_value_array_get_value (v2, i);
621       if (gst_value_compare (v1, v2_subvalue) == GST_VALUE_EQUAL)
622         return TRUE;
623     }
624   }
625
626   if (GST_VALUE_HOLDS_INT_RANGE (v2)) {
627     gint min, max;
628
629     min = gst_value_get_int_range_min (v2);
630     max = gst_value_get_int_range_max (v2);
631
632     if (G_VALUE_HOLDS_INT (v1)) {
633       gint v = g_value_get_int (v1);
634
635       return v >= min && v <= max;
636     } else {
637       /* TODO compare int ranges with int ranges
638        * or with lists if useful */
639     }
640   }
641
642   if (GST_VALUE_HOLDS_FRACTION_RANGE (v2)) {
643     const GValue *min, *max;
644
645     min = gst_value_get_fraction_range_min (v2);
646     max = gst_value_get_fraction_range_max (v2);
647
648     if (GST_VALUE_HOLDS_FRACTION (v1)) {
649       gint v_min = gst_value_compare (v1, min);
650       gint v_max = gst_value_compare (v1, max);
651
652       return (v_min == GST_VALUE_EQUAL || v_min == GST_VALUE_GREATER_THAN) &&
653           (v_max == GST_VALUE_EQUAL || v_max == GST_VALUE_LESS_THAN);
654     } else {
655       /* TODO compare fraction ranges with fraction ranges
656        * or with lists if useful */
657     }
658   }
659
660   return FALSE;
661 }
662
663 static void
664 _check_and_copy_structure_field (GstStructure * from, GstStructure * to,
665     const gchar * name)
666 {
667   if (gst_structure_has_field (from, name)) {
668     gst_structure_set_value (to, name, gst_structure_get_value (from, name));
669   }
670 }
671
672 static GstCaps *
673 gst_validate_pad_monitor_copy_caps_fields_into_caps (GstValidatePadMonitor *
674     monitor, GstCaps * from_caps, GstCaps * into_caps)
675 {
676   gint i, j, into_size, from_size;
677   GstStructure *structure;
678   GstCaps *res = gst_caps_new_empty ();
679
680   into_size = gst_caps_get_size (into_caps);
681   from_size = gst_caps_get_size (from_caps);
682
683   for (i = 0; i < into_size; i++) {
684     GstStructure *s = gst_caps_get_structure (into_caps, i);
685
686     for (j = 0; j < from_size; j++) {
687       GstStructure *new_structure = gst_structure_copy (s);
688
689       structure = gst_caps_get_structure (from_caps, j);
690       if (_structure_is_video (structure)) {
691         _check_and_copy_structure_field (structure, new_structure, "width");
692         _check_and_copy_structure_field (structure, new_structure, "height");
693         _check_and_copy_structure_field (structure, new_structure, "framerate");
694         _check_and_copy_structure_field (structure, new_structure,
695             "pixel-aspect-ratio");
696       } else if (_structure_is_audio (s)) {
697         _check_and_copy_structure_field (structure, new_structure, "rate");
698         _check_and_copy_structure_field (structure, new_structure, "channels");
699       }
700
701       gst_caps_append_structure (res, new_structure);
702     }
703   }
704   return res;
705 }
706
707 static GstCaps *
708 gst_validate_pad_monitor_transform_caps (GstValidatePadMonitor * monitor,
709     GstCaps * caps)
710 {
711   GstCaps *othercaps;
712   GstCaps *new_caps;
713   GstIterator *iter;
714   gboolean done;
715   GstPad *otherpad;
716   GstCaps *template_caps;
717   GstPad *pad;
718
719
720   GST_DEBUG_OBJECT (monitor, "Transform caps %" GST_PTR_FORMAT, caps);
721
722   if (caps == NULL)
723     return NULL;
724
725   othercaps = gst_caps_new_empty ();
726
727   pad =
728       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
729           (monitor)));
730   iter = gst_pad_iterate_internal_links (pad);
731   done = FALSE;
732   while (!done) {
733     GValue value = { 0, };
734     switch (gst_iterator_next (iter, &value)) {
735       case GST_ITERATOR_OK:
736         otherpad = g_value_get_object (&value);
737         template_caps = gst_pad_get_pad_template_caps (otherpad);
738
739         new_caps =
740             gst_validate_pad_monitor_copy_caps_fields_into_caps (monitor, caps,
741             template_caps);
742         if (!gst_caps_is_empty (new_caps))
743           gst_caps_append (othercaps, new_caps);
744         else
745           gst_caps_unref (new_caps);
746
747         gst_caps_unref (template_caps);
748         g_value_reset (&value);
749         break;
750       case GST_ITERATOR_RESYNC:
751         gst_iterator_resync (iter);
752         gst_caps_unref (othercaps);
753         othercaps = gst_caps_new_empty ();
754         break;
755       case GST_ITERATOR_ERROR:
756         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
757         done = TRUE;
758         break;
759       case GST_ITERATOR_DONE:
760         done = TRUE;
761         break;
762     }
763   }
764   gst_iterator_free (iter);
765
766   GST_DEBUG_OBJECT (pad, "Transformed caps: %" GST_PTR_FORMAT, othercaps);
767   gst_object_unref (pad);
768
769   return othercaps;
770 }
771
772 static void
773 gst_validate_pad_monitor_check_caps_fields_proxied (GstValidatePadMonitor *
774     monitor, GstCaps * caps, GstCaps * filter)
775 {
776   GstStructure *structure;
777   GstStructure *otherstructure;
778   GstCaps *othercaps;
779   GstCaps *otherfilter;
780   gint i, j;
781
782   if (!gst_validate_pad_monitor_pad_should_proxy_othercaps (monitor))
783     return;
784
785   otherfilter = gst_validate_pad_monitor_transform_caps (monitor, filter);
786   othercaps = gst_validate_pad_monitor_get_othercaps (monitor, otherfilter);
787   if (otherfilter)
788     gst_caps_unref (otherfilter);
789
790   for (i = 0; i < gst_caps_get_size (othercaps); i++) {
791     gboolean found = FALSE;
792     gboolean type_match = FALSE;
793
794     otherstructure = gst_caps_get_structure (othercaps, i);
795
796     /* look for a proxied version of 'otherstructure' */
797     if (_structure_is_video (otherstructure)) {
798       for (j = 0; j < gst_caps_get_size (caps); j++) {
799         structure = gst_caps_get_structure (caps, j);
800         if (_structure_is_video (structure)) {
801           type_match = TRUE;
802           if (_structures_field_is_contained (structure, otherstructure, TRUE,
803                   "width")
804               && _structures_field_is_contained (structure, otherstructure,
805                   TRUE, "height")
806               && _structures_field_is_contained (structure, otherstructure,
807                   TRUE, "framerate")
808               && _structures_field_is_contained (structure, otherstructure,
809                   FALSE, "pixel-aspect-ratio")) {
810             found = TRUE;
811             break;
812           }
813         }
814       }
815     } else if (_structure_is_audio (otherstructure)) {
816       for (j = 0; j < gst_caps_get_size (caps); j++) {
817         structure = gst_caps_get_structure (caps, j);
818         if (_structure_is_audio (structure)) {
819           type_match = TRUE;
820           if (_structures_field_is_contained (structure, otherstructure, TRUE,
821                   "rate")
822               && _structures_field_is_contained (structure, otherstructure,
823                   TRUE, "channels")) {
824             found = TRUE;
825             break;
826           }
827         }
828       }
829     }
830
831     if (type_match && !found) {
832       gchar *otherstruct_str = gst_structure_to_string (otherstructure),
833           *caps_str = gst_caps_to_string (caps);
834
835       GST_VALIDATE_REPORT (monitor, GET_CAPS_NOT_PROXYING_FIELDS,
836           "Peer pad structure '%s' has no similar version "
837           "on pad's caps '%s'", otherstruct_str, caps_str);
838
839       g_free (otherstruct_str);
840       g_free (caps_str);
841     }
842   }
843
844   gst_caps_unref (othercaps);
845 }
846
847 static void
848 gst_validate_pad_monitor_check_late_serialized_events (GstValidatePadMonitor *
849     monitor, GstClockTime ts)
850 {
851   gint i;
852   GstPad *pad;
853
854   if (!GST_CLOCK_TIME_IS_VALID (ts))
855     return;
856
857   pad =
858       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
859           (monitor)));
860
861   GST_DEBUG_OBJECT (pad, "Timestamp to check %" GST_TIME_FORMAT,
862       GST_TIME_ARGS (ts));
863
864   for (i = 0; i < monitor->serialized_events->len; i++) {
865     SerializedEventData *data =
866         g_ptr_array_index (monitor->serialized_events, i);
867
868     GST_DEBUG_OBJECT (pad, "Event #%d (%s) ts: %" GST_TIME_FORMAT,
869         i, GST_EVENT_TYPE_NAME (data->event), GST_TIME_ARGS (data->timestamp));
870
871     if (GST_CLOCK_TIME_IS_VALID (data->timestamp) && data->timestamp < ts) {
872       gchar *event_str = _get_event_string (data->event);
873
874       GST_VALIDATE_REPORT (monitor, SERIALIZED_EVENT_WASNT_PUSHED_IN_TIME,
875           "Serialized event %s wasn't pushed before expected timestamp %"
876           GST_TIME_FORMAT " on pad %s:%s", event_str,
877           GST_TIME_ARGS (data->timestamp), GST_DEBUG_PAD_NAME (pad));
878
879       g_free (event_str);
880     } else {
881       /* events should be ordered by ts */
882       break;
883     }
884   }
885
886   if (i) {
887     debug_pending_event (pad, monitor->serialized_events);
888     g_ptr_array_remove_range (monitor->serialized_events, 0, i);
889   }
890
891   gst_object_unref (pad);
892 }
893
894 static void
895 seek_data_free (GstValidatePadSeekData * data)
896 {
897   g_slice_free (GstValidatePadSeekData, data);
898 }
899
900 static GstValidatePadSeekData *
901 seek_data_for_seqnum (GstValidatePadMonitor * monitor, guint32 seqnum)
902 {
903   GList *tmp;
904
905   for (tmp = monitor->seeks; tmp; tmp = tmp->next) {
906     GstValidatePadSeekData *data = (GstValidatePadSeekData *) tmp->data;
907     if (data->seqnum == seqnum)
908       return data;
909   }
910
911   return NULL;
912 }
913
914 static void
915 gst_validate_pad_monitor_dispose (GObject * object)
916 {
917   GstValidatePadMonitor *monitor = GST_VALIDATE_PAD_MONITOR_CAST (object);
918   GstPad *pad =
919       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
920           (monitor)));
921
922   if (pad) {
923     if (monitor->pad_probe_id)
924       gst_pad_remove_probe (pad, monitor->pad_probe_id);
925     gst_object_unref (pad);
926   }
927
928   if (monitor->expected_segment)
929     gst_event_unref (monitor->expected_segment);
930
931   gst_structure_free (monitor->pending_setcaps_fields);
932   g_ptr_array_unref (monitor->serialized_events);
933   g_list_free_full (monitor->expired_events, (GDestroyNotify) gst_event_unref);
934   g_list_free_full (monitor->all_bufs, (GDestroyNotify) gst_buffer_unref);
935   gst_caps_replace (&monitor->last_caps, NULL);
936   gst_caps_replace (&monitor->last_query_res, NULL);
937   gst_caps_replace (&monitor->last_query_filter, NULL);
938   gst_caps_replace (&monitor->last_refused_caps, NULL);
939
940   g_list_free_full (monitor->seeks, (GDestroyNotify) seek_data_free);
941
942   G_OBJECT_CLASS (parent_class)->dispose (object);
943 }
944
945 static void
946 gst_validate_pad_monitor_class_init (GstValidatePadMonitorClass * klass)
947 {
948   GObjectClass *gobject_class;
949   GstValidateMonitorClass *monitor_klass;
950
951   gobject_class = G_OBJECT_CLASS (klass);
952   monitor_klass = GST_VALIDATE_MONITOR_CLASS (klass);
953
954   gobject_class->dispose = gst_validate_pad_monitor_dispose;
955
956   monitor_klass->setup = gst_validate_pad_monitor_do_setup;
957   monitor_klass->get_element = gst_validate_pad_monitor_get_element;
958 }
959
960 /* Called when a pad is being flushed */
961 static void
962 gst_validate_pad_monitor_flush (GstValidatePadMonitor * pad_monitor)
963 {
964   /* Note: Keep in the same order as in the GstValidatePadMonitor structure */
965
966   gst_caps_replace (&pad_monitor->last_caps, NULL);
967   pad_monitor->caps_is_audio = pad_monitor->caps_is_video =
968       pad_monitor->caps_is_raw = FALSE;
969
970   pad_monitor->first_buffer = TRUE;
971
972   pad_monitor->has_segment = FALSE;
973   pad_monitor->is_eos = FALSE;
974
975   pad_monitor->pending_buffer_discont = TRUE;
976
977   gst_event_replace (&pad_monitor->expected_segment, NULL);
978   if (pad_monitor->serialized_events->len)
979     g_ptr_array_remove_range (pad_monitor->serialized_events, 0,
980         pad_monitor->serialized_events->len);
981   g_list_free_full (pad_monitor->expired_events,
982       (GDestroyNotify) gst_event_unref);
983   pad_monitor->expired_events = NULL;
984
985   gst_segment_init (&pad_monitor->segment, GST_FORMAT_BYTES);
986   pad_monitor->current_timestamp = GST_CLOCK_TIME_NONE;
987   pad_monitor->current_duration = GST_CLOCK_TIME_NONE;
988
989   pad_monitor->timestamp_range_start = GST_CLOCK_TIME_NONE;
990   pad_monitor->timestamp_range_end = GST_CLOCK_TIME_NONE;
991 }
992
993 /* Called when the pad monitor is initialized or when
994  * the pad is deactivated */
995 static void
996 gst_validate_pad_monitor_reset (GstValidatePadMonitor * pad_monitor)
997 {
998   gst_validate_pad_monitor_flush (pad_monitor);
999
1000   /* Note : For the entries that haven't been reset in _flush(), do
1001    * it here and keep in the same order as the GstValidatePadMonitor
1002    * structure */
1003
1004   pad_monitor->pending_flush_stop = FALSE;
1005   pad_monitor->pending_newsegment_seqnum = GST_SEQNUM_INVALID;
1006   pad_monitor->pending_eos_seqnum = GST_SEQNUM_INVALID;
1007
1008   if (pad_monitor->pending_setcaps_fields)
1009     gst_structure_free (pad_monitor->pending_setcaps_fields);
1010   pad_monitor->pending_setcaps_fields =
1011       gst_structure_new_empty (PENDING_FIELDS);
1012   if (pad_monitor->seeks)
1013     g_list_free_full (pad_monitor->seeks, (GDestroyNotify) seek_data_free);
1014   pad_monitor->current_seek = NULL;
1015   pad_monitor->seeks = NULL;
1016
1017   /* FIXME : Why BYTES and not UNDEFINED ? */
1018   gst_segment_init (&pad_monitor->segment, GST_FORMAT_BYTES);
1019
1020   pad_monitor->min_buf_freq = 0;
1021   pad_monitor->buffers_pushed = 0;
1022   pad_monitor->last_buffers_pushed = 0;
1023   pad_monitor->min_buf_freq_interval_ts = GST_CLOCK_TIME_NONE;
1024   pad_monitor->min_buf_freq_first_buffer_ts = GST_CLOCK_TIME_NONE;
1025   pad_monitor->min_buf_freq_start = GST_CLOCK_TIME_NONE;
1026 }
1027
1028 static void
1029 gst_validate_pad_monitor_init (GstValidatePadMonitor * pad_monitor)
1030 {
1031   pad_monitor->serialized_events =
1032       g_ptr_array_new_with_free_func ((GDestroyNotify)
1033       _serialized_event_data_free);
1034
1035   gst_validate_pad_monitor_reset (pad_monitor);
1036 }
1037
1038 /**
1039  * gst_validate_pad_monitor_new:
1040  * @pad: (transfer none): a #GstPad to run Validate on
1041  * @runner: (transfer none): a #GstValidateRunner
1042  * @parent: (nullable): The parent of the new monitor
1043  *
1044  * Returns: (transfer full): A #GstValidatePadMonitor
1045  */
1046 GstValidatePadMonitor *
1047 gst_validate_pad_monitor_new (GstPad * pad, GstValidateRunner * runner,
1048     GstValidateElementMonitor * parent)
1049 {
1050   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1051   g_return_val_if_fail (runner != NULL, NULL);
1052
1053   return g_object_new (GST_TYPE_VALIDATE_PAD_MONITOR,
1054       "object", pad, "validate-runner", runner, "validate-parent",
1055       parent, NULL);
1056 }
1057
1058 static GstElement *
1059 gst_validate_pad_monitor_get_element (GstValidateMonitor * monitor)
1060 {
1061   GstPad *pad =
1062       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1063           (monitor)));
1064   GstElement *parent = GST_ELEMENT (gst_pad_get_parent (pad));
1065
1066   gst_object_unref (pad);
1067
1068   return parent;
1069 }
1070
1071 static void
1072 gst_validate_pad_monitor_event_overrides (GstValidatePadMonitor * pad_monitor,
1073     GstEvent * event)
1074 {
1075   GList *iter;
1076
1077   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1078   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1079       iter = g_list_next (iter)) {
1080     GstValidateOverride *override = iter->data;
1081
1082     gst_validate_override_event_handler (override,
1083         GST_VALIDATE_MONITOR_CAST (pad_monitor), event);
1084   }
1085   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1086 }
1087
1088 static void
1089 gst_validate_pad_monitor_buffer_overrides (GstValidatePadMonitor * pad_monitor,
1090     GstBuffer * buffer)
1091 {
1092   GList *iter;
1093
1094   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1095   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1096       iter = g_list_next (iter)) {
1097     GstValidateOverride *override = iter->data;
1098
1099     gst_validate_override_buffer_handler (override,
1100         GST_VALIDATE_MONITOR_CAST (pad_monitor), buffer);
1101   }
1102   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1103 }
1104
1105 static void
1106 gst_validate_pad_monitor_buffer_probe_overrides (GstValidatePadMonitor *
1107     pad_monitor, GstBuffer * buffer)
1108 {
1109   GList *iter;
1110
1111   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1112   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1113       iter = g_list_next (iter)) {
1114     GstValidateOverride *override = iter->data;
1115
1116     gst_validate_override_buffer_probe_handler (override,
1117         GST_VALIDATE_MONITOR_CAST (pad_monitor), buffer);
1118   }
1119   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1120 }
1121
1122 static void
1123 gst_validate_pad_monitor_query_overrides (GstValidatePadMonitor * pad_monitor,
1124     GstQuery * query)
1125 {
1126   GList *iter;
1127
1128   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1129   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1130       iter = g_list_next (iter)) {
1131     GstValidateOverride *override = iter->data;
1132
1133     gst_validate_override_query_handler (override,
1134         GST_VALIDATE_MONITOR_CAST (pad_monitor), query);
1135   }
1136   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1137 }
1138
1139 static void
1140 gst_validate_pad_monitor_setcaps_overrides (GstValidatePadMonitor * pad_monitor,
1141     GstCaps * caps)
1142 {
1143   GList *iter;
1144
1145   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1146   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1147       iter = g_list_next (iter)) {
1148     GstValidateOverride *override = iter->data;
1149
1150     gst_validate_override_setcaps_handler (override,
1151         GST_VALIDATE_MONITOR_CAST (pad_monitor), caps);
1152   }
1153   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1154 }
1155
1156 /* FIXME : This is a bit dubious, what's the point of this check ? */
1157 static gboolean
1158 gst_validate_pad_monitor_timestamp_is_in_received_range (GstValidatePadMonitor *
1159     monitor, GstClockTime ts, GstClockTime tolerance)
1160 {
1161   GstPad *pad =
1162       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1163           (monitor)));
1164
1165   GST_DEBUG_OBJECT (pad,
1166       "Checking if timestamp %" GST_TIME_FORMAT " is in range: %"
1167       GST_TIME_FORMAT " - %" GST_TIME_FORMAT " for pad "
1168       "%s:%s with tolerance: %" GST_TIME_FORMAT, GST_TIME_ARGS (ts),
1169       GST_TIME_ARGS (monitor->timestamp_range_start),
1170       GST_TIME_ARGS (monitor->timestamp_range_end), GST_DEBUG_PAD_NAME (pad),
1171       GST_TIME_ARGS (tolerance));
1172   gst_object_unref (pad);
1173
1174   return !GST_CLOCK_TIME_IS_VALID (monitor->timestamp_range_start) ||
1175       !GST_CLOCK_TIME_IS_VALID (monitor->timestamp_range_end) ||
1176       ((monitor->timestamp_range_start >= tolerance ?
1177           monitor->timestamp_range_start - tolerance : 0) <= ts
1178       && (ts >= tolerance ? ts - tolerance : 0) <=
1179       monitor->timestamp_range_end);
1180 }
1181
1182 /* Iterates over internal links (sinkpads) to check that this buffer has
1183  * a timestamp that is in the range of the lastly received buffers */
1184 static void
1185     gst_validate_pad_monitor_check_buffer_timestamp_in_received_range
1186     (GstValidatePadMonitor * monitor, GstBuffer * buffer,
1187     GstClockTime tolerance)
1188 {
1189   GstClockTime ts;
1190   GstClockTime ts_end;
1191   GstIterator *iter;
1192   gboolean has_one = FALSE;
1193   gboolean found = FALSE;
1194   gboolean done;
1195   GstPad *otherpad;
1196   GstValidatePadMonitor *othermonitor;
1197   GstPad *pad =
1198       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1199           (monitor)));
1200
1201   if (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))
1202       || !GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) {
1203     GST_DEBUG_OBJECT (pad,
1204         "Can't check buffer timestamps range as "
1205         "buffer has no valid timestamp/duration");
1206     goto done;
1207   }
1208
1209   ts = GST_BUFFER_TIMESTAMP (buffer);
1210   ts_end = ts + GST_BUFFER_DURATION (buffer);
1211
1212   iter = gst_pad_iterate_internal_links (pad);
1213
1214   if (iter == NULL) {
1215     GST_WARNING_OBJECT (pad, "No iterator available");
1216     goto done;
1217   }
1218
1219   done = FALSE;
1220   while (!done) {
1221     GValue value = { 0, };
1222     switch (gst_iterator_next (iter, &value)) {
1223       case GST_ITERATOR_OK:
1224         otherpad = g_value_get_object (&value);
1225         GST_DEBUG_OBJECT (pad, "Checking pad %s:%s input timestamps",
1226             GST_DEBUG_PAD_NAME (otherpad));
1227         othermonitor = _GET_PAD_MONITOR (otherpad);
1228         if (!othermonitor)
1229           continue;
1230
1231         GST_VALIDATE_MONITOR_LOCK (othermonitor);
1232         if (gst_validate_pad_monitor_timestamp_is_in_received_range
1233             (othermonitor, ts, tolerance)
1234             &&
1235             gst_validate_pad_monitor_timestamp_is_in_received_range
1236             (othermonitor, ts_end, tolerance)) {
1237           done = TRUE;
1238           found = TRUE;
1239         }
1240         GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1241         g_value_reset (&value);
1242         has_one = TRUE;
1243         break;
1244       case GST_ITERATOR_RESYNC:
1245         gst_iterator_resync (iter);
1246         has_one = FALSE;
1247         found = FALSE;
1248         break;
1249       case GST_ITERATOR_ERROR:
1250         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1251         done = TRUE;
1252         break;
1253       case GST_ITERATOR_DONE:
1254         done = TRUE;
1255         break;
1256     }
1257   }
1258   gst_iterator_free (iter);
1259
1260   if (!has_one) {
1261     GST_DEBUG_OBJECT (pad, "Skipping timestamp in range check as no "
1262         "internal linked pad was found");
1263     goto done;
1264   }
1265   if (!found) {
1266     GST_VALIDATE_REPORT (monitor, BUFFER_TIMESTAMP_OUT_OF_RECEIVED_RANGE,
1267         "Timestamp %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT
1268         " is out of range of received input", GST_TIME_ARGS (ts),
1269         GST_TIME_ARGS (ts_end));
1270   }
1271 done:
1272   if (pad)
1273     gst_object_unref (pad);
1274 }
1275
1276 static void
1277 gst_validate_pad_monitor_check_discont (GstValidatePadMonitor * pad_monitor,
1278     GstBuffer * buffer)
1279 {
1280   if (pad_monitor->pending_buffer_discont) {
1281     if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
1282       GST_VALIDATE_REPORT (pad_monitor, BUFFER_MISSING_DISCONT,
1283           "Buffer is missing a DISCONT flag");
1284     pad_monitor->pending_buffer_discont = FALSE;
1285   }
1286 }
1287
1288 static void
1289 gst_validate_pad_monitor_check_first_buffer (GstValidatePadMonitor *
1290     pad_monitor, GstBuffer * buffer)
1291 {
1292   GstPad *pad =
1293       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1294           (pad_monitor)));
1295
1296   if (G_UNLIKELY (pad_monitor->first_buffer)) {
1297     pad_monitor->first_buffer = FALSE;
1298
1299     if (!pad_monitor->has_segment && PAD_IS_IN_PUSH_MODE (pad)) {
1300       GST_VALIDATE_REPORT (pad_monitor, BUFFER_BEFORE_SEGMENT,
1301           "Received buffer before Segment event");
1302     }
1303
1304     GST_DEBUG_OBJECT (pad,
1305         "Checking first buffer (pts:%" GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT
1306         ")", GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1307         GST_TIME_ARGS (GST_BUFFER_DTS (buffer)));
1308
1309   }
1310
1311   gst_object_unref (pad);
1312 }
1313
1314 static void
1315 gst_validate_pad_monitor_check_eos (GstValidatePadMonitor *
1316     pad_monitor, GstBuffer * buffer)
1317 {
1318   if (G_UNLIKELY (pad_monitor->is_eos)) {
1319     GST_VALIDATE_REPORT (pad_monitor, BUFFER_AFTER_EOS,
1320         "Received buffer %" GST_PTR_FORMAT " after EOS", buffer);
1321   }
1322 }
1323
1324 static void
1325 gst_validate_pad_monitor_update_buffer_data (GstValidatePadMonitor *
1326     pad_monitor, GstBuffer * buffer)
1327 {
1328   GstPad *pad =
1329       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1330           (pad_monitor)));
1331   pad_monitor->current_timestamp = GST_BUFFER_TIMESTAMP (buffer);
1332   pad_monitor->current_duration = GST_BUFFER_DURATION (buffer);
1333   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) {
1334     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->timestamp_range_start)) {
1335       pad_monitor->timestamp_range_start =
1336           MIN (pad_monitor->timestamp_range_start,
1337           GST_BUFFER_TIMESTAMP (buffer));
1338     } else {
1339       pad_monitor->timestamp_range_start = GST_BUFFER_TIMESTAMP (buffer);
1340     }
1341
1342     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) {
1343       GstClockTime endts =
1344           GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1345       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->timestamp_range_end)) {
1346         pad_monitor->timestamp_range_end =
1347             MAX (pad_monitor->timestamp_range_end, endts);
1348       } else {
1349         pad_monitor->timestamp_range_end = endts;
1350       }
1351     }
1352   }
1353   GST_DEBUG_OBJECT (pad, "Current stored range: %" GST_TIME_FORMAT
1354       " - %" GST_TIME_FORMAT,
1355       GST_TIME_ARGS (pad_monitor->timestamp_range_start),
1356       GST_TIME_ARGS (pad_monitor->timestamp_range_end));
1357
1358   gst_object_unref (pad);
1359 }
1360
1361 static GstFlowReturn
1362 _combine_flows (GstFlowReturn ret1, GstFlowReturn ret2)
1363 {
1364   if (ret1 == ret2)
1365     return ret1;
1366   if (ret1 <= GST_FLOW_NOT_NEGOTIATED)
1367     return ret1;
1368   if (ret2 <= GST_FLOW_NOT_NEGOTIATED)
1369     return ret2;
1370   if (ret1 == GST_FLOW_FLUSHING || ret2 == GST_FLOW_FLUSHING)
1371     return GST_FLOW_FLUSHING;
1372   if (ret1 == GST_FLOW_OK || ret2 == GST_FLOW_OK)
1373     return GST_FLOW_OK;
1374   return ret2;
1375 }
1376
1377 static void
1378 gst_validate_pad_monitor_check_aggregated_return (GstValidatePadMonitor *
1379     monitor, GstObject * parent, GstFlowReturn ret)
1380 {
1381   GstIterator *iter;
1382   gboolean done;
1383   GstPad *otherpad;
1384   GstPad *peerpad;
1385   GstState state, pending;
1386   GstFlowReturn aggregated = GST_FLOW_NOT_LINKED;
1387   gboolean found_a_pad = FALSE;
1388   GstPad *pad =
1389       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1390           (monitor)));
1391
1392   iter = gst_pad_iterate_internal_links (pad);
1393   done = FALSE;
1394   while (!done) {
1395     GValue value = { 0, };
1396     switch (gst_iterator_next (iter, &value)) {
1397       case GST_ITERATOR_OK:
1398         otherpad = g_value_get_object (&value);
1399         peerpad = gst_pad_get_peer (otherpad);
1400         if (peerpad) {
1401           found_a_pad = TRUE;
1402           aggregated =
1403               _combine_flows (aggregated,
1404               gst_pad_get_last_flow_return (peerpad));
1405
1406           gst_object_unref (peerpad);
1407         }
1408         g_value_reset (&value);
1409         break;
1410       case GST_ITERATOR_RESYNC:
1411         gst_iterator_resync (iter);
1412         break;
1413       case GST_ITERATOR_ERROR:
1414         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1415         done = TRUE;
1416         break;
1417       case GST_ITERATOR_DONE:
1418         done = TRUE;
1419         break;
1420     }
1421   }
1422   gst_iterator_free (iter);
1423   if (!found_a_pad) {
1424     /* no peer pad found, nothing to do */
1425     goto done;
1426   }
1427
1428   if (aggregated == GST_FLOW_FLUSHING) {
1429     gst_element_get_state (GST_ELEMENT (parent), &state, &pending, 0);
1430     if (state < GST_STATE_PAUSED || pending < GST_STATE_PAUSED) {
1431       /* Aggregated is flushing, we might have been aggregating a combination
1432        * of pads that are not what was present on the element during the actual
1433        * data flow combination (pads might have been removed meanwhile) */
1434
1435       goto done;
1436     }
1437   } else if (aggregated == GST_FLOW_OK || aggregated == GST_FLOW_EOS) {
1438
1439     /* those are acceptable situations */
1440     if (GST_PAD_IS_FLUSHING (pad) && ret == GST_FLOW_FLUSHING) {
1441       /* pad is flushing, always acceptable to return flushing */
1442       goto done;
1443     }
1444
1445     gst_element_get_state (GST_ELEMENT (parent), &state, &pending, 0);
1446     if (ret == GST_FLOW_FLUSHING && (state < GST_STATE_PAUSED
1447             || pending < GST_STATE_PAUSED)) {
1448       /* Element is being teared down, accept FLOW_FLUSHING */
1449
1450       goto done;
1451     }
1452
1453     if (monitor->is_eos && ret == GST_FLOW_EOS) {
1454       /* this element received eos and returned eos */
1455       goto done;
1456     }
1457
1458     if (PAD_PARENT_IS_DEMUXER (monitor) && ret == GST_FLOW_EOS) {
1459       /* a demuxer can return EOS when the samples end */
1460       goto done;
1461     }
1462   }
1463
1464   if (aggregated != ret) {
1465     GST_VALIDATE_REPORT (monitor, WRONG_FLOW_RETURN,
1466         "Wrong combined flow return %s(%d). Expected: %s(%d)",
1467         gst_flow_get_name (ret), ret, gst_flow_get_name (aggregated),
1468         aggregated);
1469   }
1470
1471 done:
1472   gst_object_unref (pad);
1473 }
1474
1475 static void
1476     gst_validate_pad_monitor_otherpad_add_pending_serialized_event
1477     (GstValidatePadMonitor * monitor, GstEvent * event, GstClockTime last_ts)
1478 {
1479   GstIterator *iter;
1480   gboolean done;
1481   GstPad *otherpad;
1482   GstValidatePadMonitor *othermonitor;
1483   GstPad *pad;
1484
1485
1486   if (!GST_EVENT_IS_SERIALIZED (event))
1487     return;
1488
1489   pad =
1490       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1491           (monitor)));
1492   iter = gst_pad_iterate_internal_links (pad);
1493   if (iter == NULL) {
1494     /* inputselector will return NULL if the sinkpad is not the active one .... */
1495     GST_FIXME_OBJECT (pad, "No iterator");
1496     gst_object_unref (pad);
1497     return;
1498   }
1499
1500   done = FALSE;
1501   while (!done) {
1502     GValue value = { 0, };
1503     switch (gst_iterator_next (iter, &value)) {
1504       case GST_ITERATOR_OK:
1505         otherpad = g_value_get_object (&value);
1506         othermonitor = _GET_PAD_MONITOR (otherpad);
1507         if (othermonitor) {
1508           SerializedEventData *data = g_slice_new0 (SerializedEventData);
1509           data->timestamp = last_ts;
1510           data->event = gst_event_ref (event);
1511           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1512           GST_DEBUG_OBJECT (pad, "Storing for pad %s:%s event %p %s",
1513               GST_DEBUG_PAD_NAME (otherpad), event,
1514               GST_EVENT_TYPE_NAME (event));
1515           g_ptr_array_add (othermonitor->serialized_events, data);
1516           debug_pending_event (otherpad, othermonitor->serialized_events);
1517           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1518         }
1519         g_value_reset (&value);
1520         break;
1521       case GST_ITERATOR_RESYNC:
1522         gst_iterator_resync (iter);
1523         break;
1524       case GST_ITERATOR_ERROR:
1525         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1526         done = TRUE;
1527         break;
1528       case GST_ITERATOR_DONE:
1529         done = TRUE;
1530         break;
1531     }
1532   }
1533   gst_iterator_free (iter);
1534   gst_object_unref (pad);
1535 }
1536
1537 static void
1538 gst_validate_pad_monitor_otherpad_add_pending_field (GstValidatePadMonitor *
1539     monitor, GstStructure * structure, const gchar * field)
1540 {
1541   GstIterator *iter;
1542   gboolean done;
1543   GstPad *otherpad;
1544   GstValidatePadMonitor *othermonitor;
1545   const GValue *v;
1546   GstPad *pad;
1547
1548   v = gst_structure_get_value (structure, field);
1549   pad =
1550       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1551           (monitor)));
1552
1553   if (v == NULL) {
1554     GST_DEBUG_OBJECT (pad, "Not adding pending field %s as it isn't "
1555         "present on structure %" GST_PTR_FORMAT, field, structure);
1556     gst_object_unref (pad);
1557     return;
1558   }
1559
1560   iter = gst_pad_iterate_internal_links (pad);
1561   done = FALSE;
1562   while (!done) {
1563     GValue value = { 0, };
1564     switch (gst_iterator_next (iter, &value)) {
1565       case GST_ITERATOR_OK:
1566         otherpad = g_value_get_object (&value);
1567         othermonitor = _GET_PAD_MONITOR (otherpad);
1568         if (othermonitor) {
1569           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1570           g_assert (othermonitor->pending_setcaps_fields != NULL);
1571           gst_structure_set_value (othermonitor->pending_setcaps_fields,
1572               field, v);
1573           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1574         }
1575         g_value_reset (&value);
1576         break;
1577       case GST_ITERATOR_RESYNC:
1578         gst_iterator_resync (iter);
1579         break;
1580       case GST_ITERATOR_ERROR:
1581         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1582         done = TRUE;
1583         break;
1584       case GST_ITERATOR_DONE:
1585         done = TRUE;
1586         break;
1587     }
1588   }
1589   gst_iterator_free (iter);
1590   gst_object_unref (pad);
1591 }
1592
1593 static void
1594 gst_validate_pad_monitor_otherpad_clear_pending_fields (GstValidatePadMonitor *
1595     monitor)
1596 {
1597   GstIterator *iter;
1598   gboolean done;
1599   GstPad *otherpad;
1600   GstValidatePadMonitor *othermonitor;
1601
1602   GstPad *pad =
1603       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1604           (monitor)));
1605
1606   iter = gst_pad_iterate_internal_links (pad);
1607   if (iter == NULL) {
1608     gst_object_unref (pad);
1609     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1610
1611     return;
1612   }
1613
1614   done = FALSE;
1615   while (!done) {
1616     GValue value = { 0, };
1617     switch (gst_iterator_next (iter, &value)) {
1618       case GST_ITERATOR_OK:
1619         otherpad = g_value_get_object (&value);
1620         othermonitor = _GET_PAD_MONITOR (otherpad);
1621         if (othermonitor) {
1622           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1623           g_assert (othermonitor->pending_setcaps_fields != NULL);
1624           gst_structure_free (othermonitor->pending_setcaps_fields);
1625           othermonitor->pending_setcaps_fields =
1626               gst_structure_new_empty (PENDING_FIELDS);
1627           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1628         }
1629         g_value_reset (&value);
1630         break;
1631       case GST_ITERATOR_RESYNC:
1632         gst_iterator_resync (iter);
1633         break;
1634       case GST_ITERATOR_ERROR:
1635         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1636         done = TRUE;
1637         break;
1638       case GST_ITERATOR_DONE:
1639         done = TRUE;
1640         break;
1641     }
1642   }
1643   gst_object_unref (pad);
1644   gst_iterator_free (iter);
1645 }
1646
1647 static void
1648 gst_validate_pad_monitor_add_expected_newsegment (GstValidatePadMonitor *
1649     monitor, GstEvent * event)
1650 {
1651   GstIterator *iter;
1652   gboolean done;
1653   GstPad *otherpad;
1654   GstValidatePadMonitor *othermonitor;
1655   GstPad *pad =
1656       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1657           (monitor)));
1658
1659   iter = gst_pad_iterate_internal_links (pad);
1660   if (iter == NULL) {
1661     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1662     gst_object_unref (pad);
1663     return;
1664   }
1665
1666   done = FALSE;
1667   while (!done) {
1668     GValue value = { 0, };
1669     switch (gst_iterator_next (iter, &value)) {
1670       case GST_ITERATOR_OK:
1671         otherpad = g_value_get_object (&value);
1672         if (!otherpad) {
1673           g_value_reset (&value);
1674           continue;
1675         }
1676
1677         othermonitor = _GET_PAD_MONITOR (otherpad);
1678         if (!othermonitor) {
1679           g_value_reset (&value);
1680           continue;
1681         }
1682
1683         GST_VALIDATE_MONITOR_LOCK (othermonitor);
1684         gst_event_replace (&othermonitor->expected_segment, event);
1685         GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1686         g_value_reset (&value);
1687         break;
1688       case GST_ITERATOR_RESYNC:
1689         gst_iterator_resync (iter);
1690         break;
1691       case GST_ITERATOR_ERROR:
1692         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1693         done = TRUE;
1694         break;
1695       case GST_ITERATOR_DONE:
1696         done = TRUE;
1697         break;
1698     }
1699   }
1700   gst_iterator_free (iter);
1701   gst_object_unref (pad);
1702 }
1703
1704 /* common checks for both sink and src event functions */
1705 static void
1706 gst_validate_pad_monitor_common_event_check (GstValidatePadMonitor *
1707     pad_monitor, GstEvent * event)
1708 {
1709   guint32 seqnum = gst_event_get_seqnum (event);
1710
1711   if (seqnum == GST_SEQNUM_INVALID)
1712     GST_VALIDATE_REPORT (pad_monitor, EVENT_INVALID_SEQNUM,
1713         "Event %p (%s) has an invalid SEQNUM", event,
1714         GST_EVENT_TYPE_NAME (event));
1715
1716   switch (GST_EVENT_TYPE (event)) {
1717     case GST_EVENT_FLUSH_START:
1718     {
1719       if (pad_monitor->seeks) {
1720         GstValidatePadSeekData *seekdata =
1721             seek_data_for_seqnum (pad_monitor, seqnum);
1722
1723         if (!seekdata)
1724           GST_VALIDATE_REPORT (pad_monitor, FLUSH_START_HAS_WRONG_SEQNUM,
1725               "Got: %" G_GUINT32_FORMAT " Expected: %" G_GUINT32_FORMAT, seqnum,
1726               ((GstValidatePadSeekData *) pad_monitor->seeks->data)->seqnum);
1727         else {
1728           if (!(seekdata->flags & GST_SEEK_FLAG_FLUSH)) {
1729             GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_START_UNEXPECTED,
1730                 "Received flush-start for a non-flushing seek");
1731           }
1732         }
1733       }
1734
1735       if (pad_monitor->pending_flush_stop) {
1736         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_START_UNEXPECTED,
1737             "Received flush-start from when flush-stop was expected");
1738       }
1739       pad_monitor->pending_flush_stop = TRUE;
1740       /* Remove the current segment seekdata */
1741       if (pad_monitor->current_seek) {
1742         pad_monitor->seeks =
1743             g_list_remove (pad_monitor->seeks, pad_monitor->current_seek);
1744         seek_data_free (pad_monitor->current_seek);
1745         pad_monitor->current_seek = NULL;
1746       }
1747     }
1748       break;
1749     case GST_EVENT_FLUSH_STOP:
1750     {
1751       if (pad_monitor->seeks && !seek_data_for_seqnum (pad_monitor, seqnum)) {
1752         GST_VALIDATE_REPORT (pad_monitor, FLUSH_STOP_HAS_WRONG_SEQNUM,
1753             "Got: %" G_GUINT32_FORMAT " Expected: %" G_GUINT32_FORMAT, seqnum,
1754             ((GstValidatePadSeekData *) pad_monitor->seeks->data)->seqnum);
1755       }
1756
1757       pad_monitor->pending_newsegment_seqnum = seqnum;
1758       pad_monitor->pending_eos_seqnum = seqnum;
1759
1760       if (!pad_monitor->pending_flush_stop) {
1761         gchar *event_str = _get_event_string (event);
1762
1763         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_STOP_UNEXPECTED,
1764             "Unexpected flush-stop %s", event_str);
1765         g_free (event_str);
1766       }
1767       pad_monitor->pending_flush_stop = FALSE;
1768
1769       /* Buffers following a FLUSH should have the DISCONT flag set */
1770       pad_monitor->pending_buffer_discont = TRUE;
1771
1772       /* cleanup our data */
1773       gst_validate_pad_monitor_flush (pad_monitor);
1774     }
1775       break;
1776     default:
1777       break;
1778   }
1779 }
1780
1781 static void
1782 mark_pads_eos (GstValidatePadMonitor * pad_monitor)
1783 {
1784   GstValidatePadMonitor *peer_monitor;
1785   GstPad *real_peer;
1786   GstPad *pad =
1787       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1788           (pad_monitor)));
1789   GstPad *peer = gst_pad_get_peer (pad);
1790
1791   gst_object_unref (pad);
1792   pad_monitor->is_eos = TRUE;
1793   if (peer) {
1794     real_peer = _get_actual_pad (peer);
1795     peer_monitor = _GET_PAD_MONITOR (real_peer);
1796     if (peer_monitor)
1797       peer_monitor->is_eos = TRUE;
1798     gst_object_unref (peer);
1799     gst_object_unref (real_peer);
1800   }
1801 }
1802
1803 static inline gboolean
1804 _should_check_buffers (GstValidatePadMonitor * pad_monitor,
1805     gboolean force_checks)
1806 {
1807   GstPad *pad =
1808       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1809           (pad_monitor)));
1810   GstValidateMonitor *monitor = GST_VALIDATE_MONITOR (pad_monitor);
1811
1812   if (pad_monitor->first_buffer || force_checks) {
1813     if (pad_monitor->segment.rate != 1.0) {
1814       GST_INFO_OBJECT (pad_monitor, "We do not support buffer checking"
1815           " for trick modes");
1816
1817       pad_monitor->check_buffers = FALSE;
1818     } else if (!PAD_PARENT_IS_DECODER (pad_monitor)) {
1819       GST_DEBUG_OBJECT (pad, "Not on a decoder => no buffer checking");
1820
1821       pad_monitor->check_buffers = FALSE;
1822     } else if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK) {
1823       GST_DEBUG_OBJECT (pad, "Not a sinkpad => no buffer checking");
1824
1825       pad_monitor->check_buffers = FALSE;
1826     } else if (!pad_monitor->caps_is_video) {
1827       GST_DEBUG_OBJECT (pad, "Not working with video => no buffer checking");
1828
1829       pad_monitor->check_buffers = FALSE;
1830     } else if (monitor->media_descriptor == NULL) {
1831       GST_DEBUG_OBJECT (pad, "No media_descriptor set => no buffer checking");
1832
1833       pad_monitor->check_buffers = FALSE;
1834     } else if (!gst_validate_media_descriptor_detects_frames
1835         (monitor->media_descriptor)) {
1836       GST_DEBUG_OBJECT (pad,
1837           "No frame detection media descriptor => no buffer checking");
1838       pad_monitor->check_buffers = FALSE;
1839     } else if (pad_monitor->all_bufs == NULL &&
1840         !gst_validate_media_descriptor_get_buffers (monitor->media_descriptor,
1841             pad, NULL, &pad_monitor->all_bufs)) {
1842
1843       GST_INFO_OBJECT (monitor,
1844           "The MediaInfo is marked as detecting frame, but getting frames"
1845           " from pad %" GST_PTR_FORMAT " did not work (some format conversion"
1846           " might be happening)", pad);
1847
1848       pad_monitor->check_buffers = FALSE;
1849     } else {
1850       if (!pad_monitor->current_buf)
1851         pad_monitor->current_buf = pad_monitor->all_bufs;
1852       pad_monitor->check_buffers = TRUE;
1853     }
1854   }
1855   gst_object_unref (pad);
1856
1857   return pad_monitor->check_buffers;
1858 }
1859
1860 static void
1861 gst_validate_monitor_find_next_buffer (GstValidatePadMonitor * pad_monitor)
1862 {
1863   GList *tmp;
1864   gboolean passed_start = FALSE;
1865
1866   if (!_should_check_buffers (pad_monitor, TRUE))
1867     return;
1868
1869   for (tmp = g_list_last (pad_monitor->all_bufs); tmp; tmp = tmp->prev) {
1870     GstBuffer *cbuf = (GstBuffer *) tmp->data;
1871     GstClockTime ts =
1872         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cbuf)) ? GST_BUFFER_DTS (cbuf)
1873         : GST_BUFFER_PTS (cbuf);
1874
1875     if (!GST_CLOCK_TIME_IS_VALID (ts))
1876       continue;
1877
1878     if (ts <= pad_monitor->segment.start)
1879       passed_start = TRUE;
1880
1881     if (!passed_start)
1882       continue;
1883
1884     if (!GST_BUFFER_FLAG_IS_SET (cbuf, GST_BUFFER_FLAG_DELTA_UNIT)) {
1885       break;
1886     }
1887   }
1888
1889   if (tmp == NULL)
1890     pad_monitor->current_buf = pad_monitor->all_bufs;
1891   else
1892     pad_monitor->current_buf = tmp;
1893 }
1894
1895 static void
1896 post_segment_message (GstValidatePadMonitor * pad_monitor, GstPad * pad,
1897     const GstSegment * segment, guint32 seqnum)
1898 {
1899   GstValidateMonitor *element_monitor =
1900       GST_VALIDATE_MONITOR_GET_PARENT (pad_monitor);
1901   GstElement *element;
1902   GstStructure *structure;
1903   GstMessage *msg;
1904
1905   if (element_monitor == NULL)
1906     return;
1907
1908   element = gst_validate_monitor_get_element (element_monitor);
1909   if (element == NULL)
1910     return;
1911
1912   GST_DEBUG_OBJECT (pad,
1913       "Posting application message for seqnum:%" G_GUINT32_FORMAT " %"
1914       GST_SEGMENT_FORMAT, seqnum, segment);
1915
1916   structure =
1917       gst_structure_new ("validate-segment", "segment", GST_TYPE_SEGMENT,
1918       segment, NULL);
1919   msg = gst_message_new_application ((GstObject *) element, structure);
1920   gst_message_set_seqnum (msg, seqnum);
1921   gst_element_post_message (element, msg);
1922
1923   gst_object_unref (element);
1924
1925   return;
1926 }
1927
1928 /* Checks whether a segment is just an update of another,
1929  * That is to say that only the base and offset field differ and all
1930  * other fields are identical */
1931 static gboolean
1932 is_segment_update (GstSegment * a, const GstSegment * b)
1933 {
1934   /* Note : We never care about the position field, it is only
1935    * used for internal usage by elements */
1936   if (a->rate == b->rate &&
1937       a->applied_rate == b->applied_rate &&
1938       a->format == b->format && a->time == b->time) {
1939     /* Changes in base/offset are considered updates */
1940     /* Updating the end position of a segment is an update */
1941     /* Updating the duration of a segment is an update */
1942     if (a->rate > 0.0) {
1943       if (a->start == b->start)
1944         return TRUE;
1945     } else {
1946       if (a->stop == b->stop)
1947         return TRUE;
1948     }
1949   }
1950   return FALSE;
1951 }
1952
1953 static GstFlowReturn
1954 gst_validate_pad_monitor_downstream_event_check (GstValidatePadMonitor *
1955     pad_monitor, GstObject * parent, GstEvent * event,
1956     GstPadEventFunction handler)
1957 {
1958   GstFlowReturn ret = GST_FLOW_OK;
1959   const GstSegment *segment;
1960   guint32 seqnum = gst_event_get_seqnum (event);
1961   GstPad *pad =
1962       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1963           (pad_monitor)));
1964
1965   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
1966
1967   /* pre checks */
1968   switch (GST_EVENT_TYPE (event)) {
1969     case GST_EVENT_STREAM_START:
1970       /* Buffers following a STREAM_START should have the DISCONT flag set */
1971       pad_monitor->pending_buffer_discont = TRUE;
1972       break;
1973     case GST_EVENT_SEGMENT:
1974     {
1975       GstValidatePadSeekData *seekdata =
1976           seek_data_for_seqnum (pad_monitor, seqnum);
1977
1978       /* parse segment data to be used if event is handled */
1979       gst_event_parse_segment (event, &segment);
1980
1981       GST_DEBUG_OBJECT (pad,
1982           "Got segment seqnum:%" G_GUINT32_FORMAT " %" GST_SEGMENT_FORMAT,
1983           seqnum, segment);
1984
1985       if (pad_monitor->pending_newsegment_seqnum != GST_SEQNUM_INVALID) {
1986         /* FIXME: Convert to more robust checks */
1987         if (pad_monitor->pending_newsegment_seqnum != seqnum) {
1988           GST_VALIDATE_REPORT (pad_monitor, SEGMENT_HAS_WRONG_SEQNUM,
1989               "Got: %u Expected: %u", seqnum,
1990               pad_monitor->pending_newsegment_seqnum);
1991         }
1992       }
1993
1994       if (seekdata && seekdata != pad_monitor->current_seek) {
1995         /* Check for accurate seeks */
1996         if (seekdata->flags & GST_SEEK_FLAG_ACCURATE) {
1997           if (segment->time != seekdata->start)
1998             GST_VALIDATE_REPORT (pad_monitor, SEGMENT_HAS_WRONG_START,
1999                 "After an accurate seek, got: %" GST_TIME_FORMAT
2000                 " Expected: %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->time),
2001                 GST_TIME_ARGS (seekdata->start));
2002         }
2003       }
2004
2005       pad_monitor->pending_eos_seqnum = seqnum;
2006
2007       if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2008         gst_validate_pad_monitor_add_expected_newsegment (pad_monitor, event);
2009       } else {
2010         /* check if this segment is the expected one */
2011         if (pad_monitor->expected_segment) {
2012           const GstSegment *exp_segment;
2013
2014           if (pad_monitor->expected_segment != event) {
2015             gst_event_parse_segment (pad_monitor->expected_segment,
2016                 &exp_segment);
2017             if (segment->format == exp_segment->format) {
2018               if ((exp_segment->rate * exp_segment->applied_rate !=
2019                       segment->rate * segment->applied_rate))
2020                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
2021                     "Rate * applied_rate %f != expected %f",
2022                     segment->rate * segment->applied_rate,
2023                     exp_segment->rate * exp_segment->applied_rate);
2024               if (exp_segment->start != segment->start)
2025                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
2026                     "Start %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
2027                     GST_TIME_ARGS (segment->start),
2028                     GST_TIME_ARGS (exp_segment->start));
2029               if (exp_segment->stop != segment->stop)
2030                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
2031                     "Stop %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
2032                     GST_TIME_ARGS (segment->stop),
2033                     GST_TIME_ARGS (exp_segment->stop));
2034               if (exp_segment->position != segment->position)
2035                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
2036                     "Position %" GST_TIME_FORMAT " != expected %"
2037                     GST_TIME_FORMAT, GST_TIME_ARGS (segment->position),
2038                     GST_TIME_ARGS (exp_segment->position));
2039             }
2040           }
2041           gst_event_replace (&pad_monitor->expected_segment, NULL);
2042         }
2043       }
2044
2045       /* Drop all expected seekdata from before this segment */
2046       if (seekdata) {
2047         while (pad_monitor->seeks && pad_monitor->seeks->data != seekdata) {
2048           GstValidatePadSeekData *tmp =
2049               (GstValidatePadSeekData *) pad_monitor->seeks->data;
2050           pad_monitor->seeks =
2051               g_list_delete_link (pad_monitor->seeks, pad_monitor->seeks);
2052           seek_data_free (tmp);
2053         }
2054       }
2055       pad_monitor->current_seek = seekdata;
2056     }
2057       break;
2058     case GST_EVENT_CAPS:{
2059       GstCaps *caps;
2060
2061       gst_event_parse_caps (event, &caps);
2062       gst_validate_pad_monitor_setcaps_pre (pad_monitor, caps);
2063       break;
2064     }
2065     case GST_EVENT_EOS:
2066       pad_monitor->is_eos = TRUE;
2067       /* FIXME : This feels and looks wrong ... */
2068       if (pad_monitor->pending_eos_seqnum == GST_SEQNUM_INVALID) {
2069         GST_VALIDATE_REPORT (pad_monitor, EVENT_EOS_WITHOUT_SEGMENT,
2070             "EOS %" GST_PTR_FORMAT " received before a segment was received",
2071             event);
2072       } else if (pad_monitor->pending_eos_seqnum != seqnum) {
2073         GST_VALIDATE_REPORT (pad_monitor, EOS_HAS_WRONG_SEQNUM,
2074             "Got: %u. Expected: %u", seqnum, pad_monitor->pending_eos_seqnum);
2075       }
2076
2077       /*
2078        * TODO add end of stream checks for
2079        *  - events not pushed
2080        *  - buffer data not pushed
2081        *  - pending events not received
2082        */
2083       break;
2084
2085       /* both flushes are handled by the common event function */
2086     case GST_EVENT_FLUSH_START:
2087     case GST_EVENT_FLUSH_STOP:
2088     case GST_EVENT_TAG:
2089     case GST_EVENT_SINK_MESSAGE:
2090     default:
2091       break;
2092   }
2093
2094   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2095   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2096   gst_validate_pad_monitor_event_overrides (pad_monitor, event);
2097   if (handler) {
2098     gst_event_ref (event);
2099     if (pad_monitor->event_full_func)
2100       ret = pad_monitor->event_full_func (pad, parent, event);
2101     else if (pad_monitor->event_func (pad, parent, event))
2102       ret = GST_FLOW_OK;
2103     else
2104       ret = GST_FLOW_ERROR;
2105   }
2106   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2107   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2108
2109   /* post checks */
2110   switch (GST_EVENT_TYPE (event)) {
2111     case GST_EVENT_SEGMENT:
2112       if (ret == GST_FLOW_OK) {
2113         /* If the new segment is not an update of the previous one, then
2114          * the following buffer should have the DISCONT flag set */
2115         if (!is_segment_update (&pad_monitor->segment, segment))
2116           pad_monitor->pending_buffer_discont = TRUE;
2117         if (!pad_monitor->has_segment
2118             && pad_monitor->segment.format != segment->format) {
2119           gst_segment_init (&pad_monitor->segment, segment->format);
2120         }
2121         gst_segment_copy_into (segment, &pad_monitor->segment);
2122         pad_monitor->has_segment = TRUE;
2123         gst_validate_monitor_find_next_buffer (pad_monitor);
2124         if (PAD_PARENT_IS_SINK (pad_monitor))
2125           post_segment_message (pad_monitor, pad, segment, seqnum);
2126       }
2127       break;
2128     case GST_EVENT_CAPS:{
2129       GstCaps *caps;
2130
2131       gst_event_parse_caps (event, &caps);
2132       gst_validate_pad_monitor_setcaps_post (pad_monitor, caps,
2133           ret == GST_FLOW_OK);
2134       break;
2135     }
2136     case GST_EVENT_FLUSH_START:
2137     case GST_EVENT_FLUSH_STOP:
2138     case GST_EVENT_EOS:
2139     case GST_EVENT_TAG:
2140     case GST_EVENT_SINK_MESSAGE:
2141     default:
2142       break;
2143   }
2144
2145   if (handler)
2146     gst_event_unref (event);
2147   gst_object_unref (pad);
2148   return ret;
2149 }
2150
2151 static GstValidatePadSeekData *
2152 _store_seek_event_data (GstValidatePadMonitor * pad_monitor, GstEvent * event)
2153 {
2154   GstValidatePadSeekData *data = g_slice_new0 (GstValidatePadSeekData);
2155
2156   data->seqnum = gst_event_get_seqnum (event);
2157   gst_event_parse_seek (event, &data->rate, &data->format, &data->flags,
2158       &data->start_type, &data->start, &data->stop_type, &data->stop);
2159
2160   pad_monitor->seeks = g_list_append (pad_monitor->seeks, data);
2161
2162   return data;
2163 }
2164
2165 static gboolean
2166 gst_validate_pad_monitor_src_event_check (GstValidatePadMonitor * pad_monitor,
2167     GstObject * parent, GstEvent * event, GstPadEventFunction handler)
2168 {
2169   gboolean ret = TRUE;
2170   GstPad *pad =
2171       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2172           (pad_monitor)));
2173
2174   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
2175
2176   if (handler) {
2177     GstValidatePadSeekData *seekdata = NULL;
2178
2179     GST_DEBUG_OBJECT (pad, "event %" GST_PTR_FORMAT, event);
2180
2181     /* Safely store pending accurate seek values */
2182     if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK)
2183       seekdata = _store_seek_event_data (pad_monitor, event);
2184     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2185     ret = pad_monitor->event_func (pad, parent, event);
2186
2187     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2188
2189     if (seekdata && !ret) {
2190       /* Remove failed seek from list */
2191       GST_LOG_OBJECT (pad, "Failed seek, removing stored seek data");
2192       pad_monitor->seeks = g_list_remove (pad_monitor->seeks, seekdata);
2193       g_slice_free (GstValidatePadSeekData, seekdata);
2194     }
2195   }
2196
2197   gst_object_unref (pad);
2198   return ret;
2199 }
2200
2201 static gboolean
2202 gst_validate_pad_monitor_check_right_buffer (GstValidatePadMonitor *
2203     pad_monitor, GstBuffer * buffer)
2204 {
2205   gchar *checksum;
2206   GstBuffer *wanted_buf;
2207   GstMapInfo map, wanted_map;
2208
2209   gboolean ret = TRUE;
2210   GstPad *pad;
2211
2212
2213   if (_should_check_buffers (pad_monitor, FALSE) == FALSE)
2214     return FALSE;
2215
2216   pad =
2217       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2218           (pad_monitor)));
2219   if (pad_monitor->current_buf == NULL) {
2220     GST_INFO_OBJECT (pad, "No current buffer one pad, Why?");
2221     gst_object_unref (pad);
2222     return FALSE;
2223   }
2224
2225   wanted_buf = pad_monitor->current_buf->data;
2226
2227   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (wanted_buf)) &&
2228       GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buffer)) &&
2229       GST_BUFFER_PTS (wanted_buf) != GST_BUFFER_PTS (buffer)) {
2230
2231     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2232         "buffer %" GST_PTR_FORMAT " PTS %" GST_TIME_FORMAT
2233         " different than expected: %" GST_TIME_FORMAT, buffer,
2234         GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2235         GST_TIME_ARGS (GST_BUFFER_PTS (wanted_buf)));
2236
2237     ret = FALSE;
2238   }
2239
2240   if (GST_BUFFER_DTS (wanted_buf) != GST_BUFFER_DTS (buffer)) {
2241     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2242         "buffer %" GST_PTR_FORMAT " DTS %" GST_TIME_FORMAT
2243         " different than expected: %" GST_TIME_FORMAT, buffer,
2244         GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
2245         GST_TIME_ARGS (GST_BUFFER_DTS (wanted_buf)));
2246     ret = FALSE;
2247   }
2248
2249   if (GST_BUFFER_DURATION (wanted_buf) != GST_BUFFER_DURATION (buffer)) {
2250     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2251         "buffer %" GST_PTR_FORMAT " DURATION %" GST_TIME_FORMAT
2252         " different than expected: %" GST_TIME_FORMAT, buffer,
2253         GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
2254         GST_TIME_ARGS (GST_BUFFER_DURATION (wanted_buf)));
2255     ret = FALSE;
2256   }
2257
2258   if (GST_BUFFER_FLAG_IS_SET (wanted_buf, GST_BUFFER_FLAG_DELTA_UNIT) !=
2259       GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
2260     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2261         "buffer %" GST_PTR_FORMAT "  Delta unit is set to %s but expected %s",
2262         buffer, GST_BUFFER_FLAG_IS_SET (buffer,
2263             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False",
2264         GST_BUFFER_FLAG_IS_SET (wanted_buf,
2265             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False");
2266     ret = FALSE;
2267   }
2268
2269   g_assert (gst_buffer_map (wanted_buf, &wanted_map, GST_MAP_READ));
2270   g_assert (gst_buffer_map (buffer, &map, GST_MAP_READ));
2271
2272   checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5,
2273       (const guchar *) map.data, map.size);
2274
2275   if (g_strcmp0 ((gchar *) wanted_map.data, checksum)) {
2276     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2277         "buffer %" GST_PTR_FORMAT " checksum %s different from expected: %s",
2278         buffer, checksum, wanted_map.data);
2279     ret = FALSE;
2280   }
2281
2282   gst_buffer_unmap (wanted_buf, &wanted_map);
2283   gst_buffer_unmap (buffer, &map);
2284   g_free (checksum);
2285   gst_object_unref (pad);
2286
2287   pad_monitor->current_buf = pad_monitor->current_buf->next;
2288
2289   return ret;
2290 }
2291
2292 static void
2293 gst_validate_pad_monitor_check_return (GstValidatePadMonitor * pad_monitor,
2294     GstFlowReturn ret)
2295 {
2296   GstValidateMonitor *parent = GST_VALIDATE_MONITOR (pad_monitor);
2297
2298   if (ret != GST_FLOW_ERROR)
2299     return;
2300
2301   while (GST_VALIDATE_MONITOR_GET_PARENT (parent))
2302     parent = GST_VALIDATE_MONITOR_GET_PARENT (parent);
2303
2304   if (GST_IS_VALIDATE_PIPELINE_MONITOR (parent)) {
2305     GstValidatePipelineMonitor *m = GST_VALIDATE_PIPELINE_MONITOR (parent);
2306
2307     GST_VALIDATE_MONITOR_LOCK (m);
2308     if (m->got_error == FALSE) {
2309       GST_VALIDATE_REPORT (pad_monitor, FLOW_ERROR_WITHOUT_ERROR_MESSAGE,
2310           "Pad return GST_FLOW_ERROR but no GST_MESSAGE_ERROR was received on"
2311           " the bus");
2312
2313       /* Only report it the first time */
2314       m->got_error = TRUE;
2315     }
2316     GST_VALIDATE_MONITOR_UNLOCK (m);
2317   }
2318 }
2319
2320 static GstFlowReturn
2321 gst_validate_pad_monitor_chain_func (GstPad * pad, GstObject * parent,
2322     GstBuffer * buffer)
2323 {
2324   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2325   GstFlowReturn ret;
2326
2327   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2328   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2329
2330   gst_validate_pad_monitor_check_discont (pad_monitor, buffer);
2331   gst_validate_pad_monitor_check_right_buffer (pad_monitor, buffer);
2332   gst_validate_pad_monitor_check_first_buffer (pad_monitor, buffer);
2333   gst_validate_pad_monitor_update_buffer_data (pad_monitor, buffer);
2334   gst_validate_pad_monitor_check_eos (pad_monitor, buffer);
2335
2336   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2337   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2338
2339   gst_validate_pad_monitor_buffer_overrides (pad_monitor, buffer);
2340
2341   ret = pad_monitor->chain_func (pad, parent, buffer);
2342
2343   gst_validate_pad_monitor_check_return (pad_monitor, ret);
2344
2345   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2346   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2347
2348   if (ret == GST_FLOW_EOS) {
2349     mark_pads_eos (pad_monitor);
2350   }
2351   if (PAD_PARENT_IS_DEMUXER (pad_monitor))
2352     gst_validate_pad_monitor_check_aggregated_return (pad_monitor, parent, ret);
2353
2354   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2355   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2356
2357   return ret;
2358 }
2359
2360 static gboolean
2361 gst_validate_pad_monitor_event_is_tracked (GstValidatePadMonitor * monitor,
2362     GstEvent * event)
2363 {
2364   if (!GST_EVENT_IS_SERIALIZED (event)) {
2365     return FALSE;
2366   }
2367
2368   /* we don't track Tag events because they mutate too much and it is hard
2369    * to match a tag event pushed on a source pad with the one that was received
2370    * on a sink pad.
2371    * One idea would be to use seqnum, but it seems that it is undefined whether
2372    * seqnums should be maintained in tag events that are created from others
2373    * up to today. (2013-08-29)
2374    */
2375   if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
2376     return FALSE;
2377
2378   return TRUE;
2379 }
2380
2381 static GstFlowReturn
2382 gst_validate_pad_monitor_sink_event_full_func (GstPad * pad, GstObject * parent,
2383     GstEvent * event)
2384 {
2385   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2386   GstFlowReturn ret;
2387
2388   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2389   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2390
2391   GST_DEBUG_OBJECT (pad, "event %p %s", event, GST_EVENT_TYPE_NAME (event));
2392
2393   if (gst_validate_pad_monitor_event_is_tracked (pad_monitor, event)) {
2394     GstClockTime last_ts = GST_CLOCK_TIME_NONE;
2395     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_timestamp)) {
2396       last_ts = pad_monitor->current_timestamp;
2397       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_duration)) {
2398         last_ts += pad_monitor->current_duration;
2399       }
2400     }
2401     gst_validate_pad_monitor_otherpad_add_pending_serialized_event (pad_monitor,
2402         event, last_ts);
2403   }
2404
2405   ret =
2406       gst_validate_pad_monitor_downstream_event_check (pad_monitor, parent,
2407       event, pad_monitor->event_func);
2408
2409   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2410   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2411   return ret;
2412 }
2413
2414 static gboolean
2415 gst_validate_pad_monitor_sink_event_func (GstPad * pad, GstObject * parent,
2416     GstEvent * event)
2417 {
2418   if (gst_validate_pad_monitor_sink_event_full_func (pad, parent,
2419           event) == GST_FLOW_OK)
2420     return TRUE;
2421   return FALSE;
2422 }
2423
2424 static gboolean
2425 gst_validate_pad_monitor_src_event_func (GstPad * pad, GstObject * parent,
2426     GstEvent * event)
2427 {
2428   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2429   gboolean ret;
2430
2431   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2432   ret = gst_validate_pad_monitor_src_event_check (pad_monitor, parent, event,
2433       pad_monitor->event_func);
2434   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2435   return ret;
2436 }
2437
2438 static gboolean
2439 gst_validate_pad_monitor_query_func (GstPad * pad, GstObject * parent,
2440     GstQuery * query)
2441 {
2442   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2443   gboolean ret;
2444
2445   gst_validate_pad_monitor_query_overrides (pad_monitor, query);
2446   ret = pad_monitor->query_func (pad, parent, query);
2447
2448   if (ret) {
2449     switch (GST_QUERY_TYPE (query)) {
2450       case GST_QUERY_ACCEPT_CAPS:
2451       {
2452         gboolean result;
2453
2454         gst_caps_replace (&pad_monitor->last_refused_caps, NULL);
2455         gst_query_parse_accept_caps_result (query, &result);
2456         if (!result) {
2457           GstCaps *refused_caps;
2458
2459           gst_query_parse_accept_caps (query, &refused_caps);
2460           pad_monitor->last_refused_caps = gst_caps_copy (refused_caps);
2461
2462         }
2463
2464         break;
2465       }
2466       case GST_QUERY_CAPS:{
2467         GstCaps *res;
2468         GstCaps *filter;
2469
2470         /* We shouldn't need to lock the parent as this doesn't modify
2471          * other monitors, just does some peer_pad_caps */
2472         GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2473
2474         gst_query_parse_caps (query, &filter);
2475         gst_query_parse_caps_result (query, &res);
2476
2477         gst_caps_replace (&pad_monitor->last_query_res, NULL);
2478         gst_caps_replace (&pad_monitor->last_query_filter, NULL);
2479         pad_monitor->last_query_res =
2480             res ? gst_caps_copy (res) : gst_caps_ref (GST_CAPS_NONE);
2481         pad_monitor->last_query_filter =
2482             filter ? gst_caps_copy (filter) : gst_caps_ref (GST_CAPS_NONE);
2483
2484         if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2485           gst_validate_pad_monitor_check_caps_fields_proxied (pad_monitor, res,
2486               filter);
2487         }
2488         GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2489         break;
2490       }
2491       default:
2492         break;
2493     }
2494   }
2495
2496   return ret;
2497 }
2498
2499 static gboolean
2500 gst_validate_pad_monitor_activatemode_func (GstPad * pad, GstObject * parent,
2501     GstPadMode mode, gboolean active)
2502 {
2503   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2504   gboolean ret = TRUE;
2505
2506   /* TODO add overrides for activate func */
2507   GST_DEBUG_OBJECT (pad, "active:%d", active);
2508
2509   if (pad_monitor->activatemode_func)
2510     ret = pad_monitor->activatemode_func (pad, parent, mode, active);
2511   if (ret && active == FALSE) {
2512     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2513     gst_validate_pad_monitor_reset (pad_monitor);
2514     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2515   }
2516
2517   return ret;
2518 }
2519
2520 static GstFlowReturn
2521 gst_validate_pad_monitor_get_range_func (GstPad * pad, GstObject * parent,
2522     guint64 offset, guint length, GstBuffer ** buffer)
2523 {
2524   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2525
2526   if (pad_monitor->get_range_func) {
2527     GstPad *peer = gst_pad_get_peer (pad);
2528     GstTask *task = NULL;
2529     GThread *thread = NULL;
2530
2531     if (peer) {
2532       GST_OBJECT_LOCK (peer);
2533       task = GST_PAD_TASK (peer);
2534       if (task && GST_TASK_STATE (task) == GST_TASK_STARTED) {
2535         GST_OBJECT_LOCK (task);
2536         /* Only doing pointer comparison, no need to hold a ref */
2537         thread = task->thread;
2538         GST_OBJECT_UNLOCK (task);
2539       }
2540       GST_OBJECT_UNLOCK (peer);
2541
2542       if (thread && thread != g_thread_self ()) {
2543         GST_VALIDATE_REPORT (pad_monitor, PULL_RANGE_FROM_WRONG_THREAD,
2544             "Pulling from wrong thread, expected pad thread: %p, got %p",
2545             task->thread, g_thread_self ());
2546       }
2547
2548       gst_object_unref (peer);
2549     }
2550
2551     return pad_monitor->get_range_func (pad, parent, offset, length, buffer);
2552   }
2553
2554   return GST_FLOW_NOT_SUPPORTED;
2555
2556 }
2557
2558 /* The interval between two buffer frequency checks */
2559 #define BUF_FREQ_CHECK_INTERVAL (GST_SECOND)
2560
2561 static void
2562 gst_validate_pad_monitor_check_buffer_freq (GstValidatePadMonitor * monitor,
2563     GstPad * pad)
2564 {
2565   GstClockTime ts;
2566
2567   if (!GST_PAD_IS_SRC (pad))
2568     return;
2569
2570   if (!monitor->min_buf_freq)
2571     return;
2572
2573   ts = gst_util_get_timestamp ();
2574   monitor->buffers_pushed++;
2575
2576   /* Same logic as in fpsdisplaysink to compute the buffer frequency */
2577   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID
2578           (monitor->min_buf_freq_first_buffer_ts))) {
2579     monitor->min_buf_freq_first_buffer_ts = ts;
2580     monitor->min_buf_freq_interval_ts = ts;
2581     return;
2582   }
2583
2584   if (GST_CLOCK_DIFF (monitor->min_buf_freq_interval_ts,
2585           ts) > BUF_FREQ_CHECK_INTERVAL) {
2586     guint time_diff;
2587     gdouble fps;
2588
2589     time_diff = (gdouble) (ts - monitor->min_buf_freq_interval_ts) / GST_SECOND;
2590     fps =
2591         (gdouble) (monitor->buffers_pushed -
2592         monitor->last_buffers_pushed) / time_diff;
2593
2594     if (fps < monitor->min_buf_freq) {
2595       if (GST_CLOCK_TIME_IS_VALID (monitor->min_buf_freq_start) &&
2596           GST_CLOCK_DIFF (monitor->min_buf_freq_first_buffer_ts,
2597               ts) < monitor->min_buf_freq_start) {
2598         GST_DEBUG_OBJECT (pad,
2599             "buffer frequency is too low (%.2f) but ignore for now (buffer-frequency-start =%"
2600             GST_TIME_FORMAT ")", fps,
2601             GST_TIME_ARGS (monitor->min_buf_freq_start));
2602       } else {
2603         GST_VALIDATE_REPORT (monitor, CONFIG_BUFFER_FREQUENCY_TOO_LOW,
2604             "Buffers are not pushed fast enough on this pad: %.2f/sec (minimum: %.2f)",
2605             fps, monitor->min_buf_freq);
2606       }
2607     }
2608
2609     monitor->last_buffers_pushed = monitor->buffers_pushed;
2610     monitor->min_buf_freq_interval_ts = ts;
2611   }
2612 }
2613
2614 static gboolean
2615 gst_validate_pad_monitor_buffer_probe (GstPad * pad, GstBuffer * buffer,
2616     gpointer udata, gboolean pull_mode)
2617 {
2618   GstValidatePadMonitor *monitor = udata;
2619
2620   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2621   GST_VALIDATE_MONITOR_LOCK (monitor);
2622
2623   if (!pull_mode)
2624     gst_validate_pad_monitor_check_discont (monitor, buffer);
2625   gst_validate_pad_monitor_check_first_buffer (monitor, buffer);
2626   gst_validate_pad_monitor_update_buffer_data (monitor, buffer);
2627   gst_validate_pad_monitor_check_eos (monitor, buffer);
2628
2629   if (PAD_PARENT_IS_DECODER (monitor) || PAD_PARENT_IS_ENCODER (monitor)) {
2630     GstClockTime tolerance = 0;
2631
2632     if (monitor->caps_is_audio)
2633       tolerance = AUDIO_TIMESTAMP_TOLERANCE;
2634
2635     gst_validate_pad_monitor_check_buffer_timestamp_in_received_range (monitor,
2636         buffer, tolerance);
2637   }
2638
2639   gst_validate_pad_monitor_check_late_serialized_events (monitor,
2640       GST_BUFFER_TIMESTAMP (buffer));
2641
2642   /* a GstValidatePadMonitor parent must be a GstValidateElementMonitor */
2643   if (PAD_PARENT_IS_DECODER (monitor)) {
2644
2645     /* should not push out of segment data */
2646     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer)) &&
2647         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer)) &&
2648         ((!gst_segment_clip (&monitor->segment, monitor->segment.format,
2649                     GST_BUFFER_TIMESTAMP (buffer),
2650                     GST_BUFFER_TIMESTAMP (buffer) +
2651                     GST_BUFFER_DURATION (buffer), NULL, NULL)) ||
2652             /* In the case of raw data, buffers should be strictly contained inside the
2653              * segment */
2654             (monitor->caps_is_raw &&
2655                 GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer) <
2656                 monitor->segment.start))
2657         ) {
2658       /* TODO is this a timestamp issue? */
2659       GST_VALIDATE_REPORT (monitor, BUFFER_IS_OUT_OF_SEGMENT,
2660           "buffer is out of segment and shouldn't be pushed. Timestamp: %"
2661           GST_TIME_FORMAT " - Duration: %" GST_TIME_FORMAT ". Range: %"
2662           GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
2663           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2664           GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
2665           GST_TIME_ARGS (monitor->segment.start),
2666           GST_TIME_ARGS (monitor->segment.stop));
2667     }
2668   }
2669
2670   gst_validate_pad_monitor_check_buffer_freq (monitor, pad);
2671
2672   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2673   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2674   gst_validate_pad_monitor_buffer_probe_overrides (monitor, buffer);
2675   return TRUE;
2676 }
2677
2678 static void
2679 gst_validate_pad_monitor_event_probe (GstPad * pad, GstEvent * event,
2680     gpointer udata)
2681 {
2682   GstValidatePadMonitor *monitor = GST_VALIDATE_PAD_MONITOR_CAST (udata);
2683   guint32 seqnum = gst_event_get_seqnum (event);
2684
2685   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2686   GST_VALIDATE_MONITOR_LOCK (monitor);
2687
2688   GST_DEBUG_OBJECT (pad, "event %p %s seqnum:%" G_GUINT32_FORMAT, event,
2689       GST_EVENT_TYPE_NAME (event), seqnum);
2690
2691   if (GST_EVENT_IS_SERIALIZED (event)) {
2692     gint i;
2693
2694     /* Detect if events the element received are being forwarded in the same order
2695      *
2696      * Several scenarios:
2697      * 1) The element pushes the event as-is
2698      * 2) The element consumes the event and does not forward it
2699      * 3) The element consumes the event and creates another one instead
2700      * 4) The element pushes other serialized event before pushing out the
2701      *    one it received
2702      *
2703      * For each pad we have two lists to track serialized events:
2704      *  1) We received on input and expect to see (serialized_events)
2705      *  2) We received on input but don't expect to see (expired_events)
2706      *
2707      * To detect events that are pushed in a different order from the one they were
2708      * received in we check that:
2709      *
2710      * For each event being outputted:
2711      *   If it is in the expired_events list:
2712      *     RAISE WARNING
2713      *   If it is in the serialized_events list:
2714      *     If there are other events that were received before:
2715      *        Put those events on the expired_events list
2716      *     Remove that event and any previous ones from the serialized_events list
2717      *
2718      * Clear expired events list when flushing or on pad deactivation
2719      *
2720      */
2721
2722     if (g_list_find (monitor->expired_events, event)) {
2723       gchar *event_str = _get_event_string (event);
2724       /* If it's the expired events, we've failed */
2725       GST_WARNING_OBJECT (pad, "Did not expect event %p %s", event,
2726           GST_EVENT_TYPE_NAME (event));
2727       GST_VALIDATE_REPORT (monitor, EVENT_SERIALIZED_OUT_OF_ORDER,
2728           "Serialized event was pushed out of order: %s", event_str);
2729
2730       g_free (event_str);
2731       monitor->expired_events = g_list_remove (monitor->expired_events, event);
2732       gst_event_unref (event);  /* remove the ref that was on the list */
2733     } else if (monitor->serialized_events->len) {
2734       for (i = 0; i < monitor->serialized_events->len; i++) {
2735         SerializedEventData *next_event =
2736             g_ptr_array_index (monitor->serialized_events, i);
2737         GST_DEBUG_OBJECT (pad, "Checking against stored event #%d: %p %s", i,
2738             next_event->event, GST_EVENT_TYPE_NAME (next_event->event));
2739
2740         if (event == next_event->event
2741             || GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2742           /* We have found our event */
2743           GST_DEBUG_OBJECT (pad, "Found matching event");
2744
2745           while (monitor->serialized_events->len > i
2746               && GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2747             /* Swallow all expected events of the same type */
2748             g_ptr_array_remove_index (monitor->serialized_events, i);
2749             next_event = g_ptr_array_index (monitor->serialized_events, i);
2750           }
2751
2752           /* Move all previous events to expired events */
2753           if (G_UNLIKELY (i > 0)) {
2754             GST_DEBUG_OBJECT (pad,
2755                 "Moving previous expected events to expired list");
2756             while (i--) {
2757               next_event = g_ptr_array_index (monitor->serialized_events, 0);
2758               monitor->expired_events =
2759                   g_list_append (monitor->expired_events,
2760                   gst_event_ref (next_event->event));
2761               g_ptr_array_remove_index (monitor->serialized_events, 0);
2762             }
2763           }
2764           debug_pending_event (pad, monitor->serialized_events);
2765           break;
2766         }
2767       }
2768     }
2769   }
2770
2771   /* This so far is just like an event that is flowing downstream,
2772    * so we do the same checks as a sinkpad event handler */
2773   gst_validate_pad_monitor_downstream_event_check (monitor, NULL, event, NULL);
2774   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2775   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2776 }
2777
2778 static GstPadProbeReturn
2779 gst_validate_pad_monitor_pad_probe (GstPad * pad, GstPadProbeInfo * info,
2780     gpointer udata)
2781 {
2782   if (info->type & GST_PAD_PROBE_TYPE_BUFFER)
2783     gst_validate_pad_monitor_buffer_probe (pad, info->data, udata,
2784         GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_PULL);
2785   else if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM)
2786     gst_validate_pad_monitor_event_probe (pad, info->data, udata);
2787
2788   return GST_PAD_PROBE_OK;
2789 }
2790
2791 static void
2792 gst_validate_pad_monitor_update_caps_info (GstValidatePadMonitor * pad_monitor,
2793     GstCaps * caps)
2794 {
2795   GstStructure *structure;
2796
2797   g_return_if_fail (gst_caps_is_fixed (caps));
2798
2799   pad_monitor->caps_is_audio = FALSE;
2800   pad_monitor->caps_is_video = FALSE;
2801
2802   structure = gst_caps_get_structure (caps, 0);
2803   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/")) {
2804     pad_monitor->caps_is_audio = TRUE;
2805   } else if (g_str_has_prefix (gst_structure_get_name (structure), "video/")) {
2806     pad_monitor->caps_is_video = TRUE;
2807   }
2808
2809   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/x-raw") ||
2810       g_str_has_prefix (gst_structure_get_name (structure), "video/x-raw")) {
2811     pad_monitor->caps_is_raw = TRUE;
2812   } else {
2813     pad_monitor->caps_is_raw = FALSE;
2814   }
2815 }
2816
2817 static void
2818 gst_validate_pad_monitor_setcaps_pre (GstValidatePadMonitor * pad_monitor,
2819     GstCaps * caps)
2820 {
2821   GstStructure *structure;
2822   GstPad *pad =
2823       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2824           (pad_monitor)));
2825
2826   /* Check if caps are identical to last caps and complain if so
2827    * Only checked for sink pads as src pads might push the same caps
2828    * multiple times during unlinked/autoplugging scenarios */
2829   if (GST_PAD_IS_SINK (pad) && pad_monitor->last_caps
2830       && gst_caps_is_equal (caps, pad_monitor->last_caps)) {
2831     gchar *caps_str = gst_caps_to_string (caps);
2832
2833     GST_VALIDATE_REPORT (pad_monitor, EVENT_CAPS_DUPLICATE, "%s", caps_str);
2834     g_free (caps_str);
2835
2836   }
2837
2838   gst_validate_pad_monitor_check_caps_complete (pad_monitor, caps);
2839
2840   if (caps) {
2841     structure = gst_caps_get_structure (caps, 0);
2842     if (gst_structure_n_fields (pad_monitor->pending_setcaps_fields)) {
2843       gint i;
2844       for (i = 0;
2845           i < gst_structure_n_fields (pad_monitor->pending_setcaps_fields);
2846           i++) {
2847         const gchar *name =
2848             gst_structure_nth_field_name (pad_monitor->pending_setcaps_fields,
2849             i);
2850         const GValue *v = gst_structure_get_value (structure, name);
2851         const GValue *otherv =
2852             gst_structure_get_value (pad_monitor->pending_setcaps_fields, name);
2853
2854         if (v == NULL) {
2855           gchar *caps_str = gst_caps_to_string (caps);
2856
2857           GST_VALIDATE_REPORT (pad_monitor, CAPS_EXPECTED_FIELD_NOT_FOUND,
2858               "Field %s is missing from setcaps caps '%s'", name, caps_str);
2859           g_free (caps_str);
2860         } else if (gst_value_compare (v, otherv) != GST_VALUE_EQUAL) {
2861           gchar *caps_str = gst_caps_to_string (caps),
2862               *pending_setcaps_fields_str =
2863               gst_structure_to_string (pad_monitor->pending_setcaps_fields);
2864
2865
2866           GST_VALIDATE_REPORT (pad_monitor, CAPS_FIELD_UNEXPECTED_VALUE,
2867               "Field %s from setcaps caps '%s' is different "
2868               "from expected value in caps '%s'", name, caps_str,
2869               pending_setcaps_fields_str);
2870
2871           g_free (pending_setcaps_fields_str);
2872           g_free (caps_str);
2873         }
2874       }
2875     }
2876
2877     if (GST_PAD_IS_SINK (pad) &&
2878         gst_validate_pad_monitor_pad_should_proxy_othercaps (pad_monitor)) {
2879       if (_structure_is_video (structure)) {
2880         GST_DEBUG_OBJECT (pad,
2881             "Adding video common pending fields to other pad: %" GST_PTR_FORMAT,
2882             structure);
2883         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2884             structure, "width");
2885         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2886             structure, "height");
2887         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2888             structure, "framerate");
2889         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2890             structure, "pixel-aspect-ratio");
2891       } else if (_structure_is_audio (structure)) {
2892         GST_DEBUG_OBJECT (pad,
2893             "Adding audio common pending fields to other pad: %" GST_PTR_FORMAT,
2894             structure);
2895         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2896             structure, "rate");
2897         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2898             structure, "channels");
2899       }
2900     }
2901   }
2902
2903   gst_structure_free (pad_monitor->pending_setcaps_fields);
2904   pad_monitor->pending_setcaps_fields =
2905       gst_structure_new_empty (PENDING_FIELDS);
2906   gst_object_unref (pad);
2907
2908   gst_validate_pad_monitor_setcaps_overrides (pad_monitor, caps);
2909 }
2910
2911 static void
2912 gst_validate_pad_monitor_setcaps_post (GstValidatePadMonitor * pad_monitor,
2913     GstCaps * caps, gboolean ret)
2914 {
2915   if (!ret)
2916     gst_validate_pad_monitor_otherpad_clear_pending_fields (pad_monitor);
2917   else {
2918     if (pad_monitor->last_caps) {
2919       gst_caps_unref (pad_monitor->last_caps);
2920     }
2921     pad_monitor->last_caps = gst_caps_ref (caps);
2922     gst_validate_pad_monitor_update_caps_info (pad_monitor, caps);
2923   }
2924 }
2925
2926 static void
2927 gst_validate_pad_monitor_get_min_buffer_frequency (GstValidatePadMonitor *
2928     monitor, GstPad * pad)
2929 {
2930   GList *config, *l;
2931
2932   if (!GST_PAD_IS_SRC (pad))
2933     return;
2934
2935   config = gst_validate_plugin_get_config (NULL);
2936   for (l = config; l != NULL; l = g_list_next (l)) {
2937     GstStructure *s = l->data;
2938     gdouble min_buf_freq;
2939     const gchar *pad_name;
2940     GstElement *element = NULL;
2941
2942     if (!gst_structure_get_double (s, "min-buffer-frequency", &min_buf_freq)) {
2943       gint max_int;
2944
2945       if (!gst_structure_get_int (s, "min-buffer-frequency", &max_int))
2946         goto next;
2947
2948       min_buf_freq = max_int;
2949     }
2950
2951     pad_name = gst_structure_get_string (s, "name");
2952     if (!pad_name)
2953       pad_name = "src";
2954
2955     if (g_strcmp0 (GST_PAD_NAME (pad), pad_name))
2956       goto next;
2957
2958     element = gst_pad_get_parent_element (pad);
2959
2960     if (!gst_validate_element_matches_target (element, s))
2961       goto next;
2962
2963     monitor->min_buf_freq = min_buf_freq;
2964
2965     gst_validate_utils_get_clocktime (s, "buffer-frequency-start",
2966         &monitor->min_buf_freq_start);
2967
2968     GST_DEBUG_OBJECT (pad, "pad has a minimum buffer frequency of %f",
2969         min_buf_freq);
2970   next:
2971     g_clear_object (&element);
2972   }
2973 }
2974
2975 static gboolean
2976 gst_validate_pad_monitor_do_setup (GstValidateMonitor * monitor)
2977 {
2978   GstValidatePadMonitor *pad_monitor = GST_VALIDATE_PAD_MONITOR_CAST (monitor);
2979   GstPad *pad = (gpointer) gst_validate_monitor_get_target (monitor);
2980
2981   if (!GST_IS_PAD (pad)) {
2982     GST_WARNING_OBJECT (monitor, "Trying to create pad monitor with other "
2983         "type of object");
2984     gst_object_unref (pad);
2985     return FALSE;
2986   }
2987
2988   if (_GET_PAD_MONITOR (pad)) {
2989     GST_WARNING_OBJECT (pad_monitor,
2990         "Pad already has a validate-monitor associated");
2991     gst_object_unref (pad);
2992     return FALSE;
2993   }
2994
2995   _SET_PAD_MONITOR (pad, pad_monitor);
2996
2997   pad_monitor->event_func = GST_PAD_EVENTFUNC (pad);
2998   pad_monitor->event_full_func = GST_PAD_EVENTFULLFUNC (pad);
2999   pad_monitor->query_func = GST_PAD_QUERYFUNC (pad);
3000   pad_monitor->activatemode_func = GST_PAD_ACTIVATEMODEFUNC (pad);
3001   pad_monitor->get_range_func = GST_PAD_GETRANGEFUNC (pad);
3002   if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
3003
3004     pad_monitor->chain_func = GST_PAD_CHAINFUNC (pad);
3005     if (pad_monitor->chain_func)
3006       gst_pad_set_chain_function (pad, gst_validate_pad_monitor_chain_func);
3007
3008     if (pad_monitor->event_full_func)
3009       gst_pad_set_event_full_function (pad,
3010           gst_validate_pad_monitor_sink_event_full_func);
3011     else
3012       gst_pad_set_event_function (pad,
3013           gst_validate_pad_monitor_sink_event_func);
3014   } else {
3015     gst_pad_set_event_function (pad, gst_validate_pad_monitor_src_event_func);
3016
3017     /* add buffer/event probes */
3018     pad_monitor->pad_probe_id =
3019         gst_pad_add_probe (pad,
3020         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
3021         GST_PAD_PROBE_TYPE_EVENT_FLUSH,
3022         (GstPadProbeCallback) gst_validate_pad_monitor_pad_probe, pad_monitor,
3023         NULL);
3024   }
3025   gst_pad_set_query_function (pad, gst_validate_pad_monitor_query_func);
3026   gst_pad_set_activatemode_function (pad,
3027       gst_validate_pad_monitor_activatemode_func);
3028
3029   if (GST_PAD_IS_SRC (pad)) {
3030     gst_pad_set_getrange_function (pad,
3031         gst_validate_pad_monitor_get_range_func);
3032   }
3033
3034   gst_validate_reporter_set_name (GST_VALIDATE_REPORTER (monitor),
3035       g_strdup_printf ("%s:%s", GST_DEBUG_PAD_NAME (pad)));
3036
3037   if (G_UNLIKELY (GST_PAD_PARENT (pad) == NULL))
3038     GST_FIXME ("Saw a pad not belonging to any object");
3039
3040   gst_validate_pad_monitor_get_min_buffer_frequency (pad_monitor, pad);
3041
3042   gst_object_unref (pad);
3043   return TRUE;
3044 }