validate: Use pad.last_flowret instead of trying to compute it ourselves
[platform/upstream/gstreamer.git] / validate / gst / validate / gst-validate-pad-monitor.c
1 /* GStreamer
2  *
3  * Copyright (C) 2013 Collabora Ltd.
4  *  Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>
5  *
6  * gst-validate-pad-monitor.c - Validate PadMonitor class
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "gst-validate-internal.h"
29 #include "gst-validate-pad-monitor.h"
30 #include "gst-validate-element-monitor.h"
31 #include "gst-validate-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
939   g_list_free_full (monitor->seeks, (GDestroyNotify) seek_data_free);
940
941   G_OBJECT_CLASS (parent_class)->dispose (object);
942 }
943
944 static void
945 gst_validate_pad_monitor_class_init (GstValidatePadMonitorClass * klass)
946 {
947   GObjectClass *gobject_class;
948   GstValidateMonitorClass *monitor_klass;
949
950   gobject_class = G_OBJECT_CLASS (klass);
951   monitor_klass = GST_VALIDATE_MONITOR_CLASS (klass);
952
953   gobject_class->dispose = gst_validate_pad_monitor_dispose;
954
955   monitor_klass->setup = gst_validate_pad_monitor_do_setup;
956   monitor_klass->get_element = gst_validate_pad_monitor_get_element;
957 }
958
959 /* Called when a pad is being flushed */
960 static void
961 gst_validate_pad_monitor_flush (GstValidatePadMonitor * pad_monitor)
962 {
963   /* Note: Keep in the same order as in the GstValidatePadMonitor structure */
964
965   gst_caps_replace (&pad_monitor->last_caps, NULL);
966   pad_monitor->caps_is_audio = pad_monitor->caps_is_video =
967       pad_monitor->caps_is_raw = FALSE;
968
969   pad_monitor->first_buffer = TRUE;
970
971   pad_monitor->has_segment = FALSE;
972   pad_monitor->is_eos = FALSE;
973
974   pad_monitor->pending_buffer_discont = TRUE;
975
976   gst_event_replace (&pad_monitor->expected_segment, NULL);
977   if (pad_monitor->serialized_events->len)
978     g_ptr_array_remove_range (pad_monitor->serialized_events, 0,
979         pad_monitor->serialized_events->len);
980   g_list_free_full (pad_monitor->expired_events,
981       (GDestroyNotify) gst_event_unref);
982   pad_monitor->expired_events = NULL;
983
984   gst_segment_init (&pad_monitor->segment, GST_FORMAT_BYTES);
985   pad_monitor->current_timestamp = GST_CLOCK_TIME_NONE;
986   pad_monitor->current_duration = GST_CLOCK_TIME_NONE;
987
988   pad_monitor->timestamp_range_start = GST_CLOCK_TIME_NONE;
989   pad_monitor->timestamp_range_end = GST_CLOCK_TIME_NONE;
990 }
991
992 /* Called when the pad monitor is initialized or when
993  * the pad is deactivated */
994 static void
995 gst_validate_pad_monitor_reset (GstValidatePadMonitor * pad_monitor)
996 {
997   gst_validate_pad_monitor_flush (pad_monitor);
998
999   /* Note : For the entries that haven't been reset in _flush(), do
1000    * it here and keep in the same order as the GstValidatePadMonitor
1001    * structure */
1002
1003   pad_monitor->pending_flush_stop = FALSE;
1004   pad_monitor->pending_newsegment_seqnum = GST_SEQNUM_INVALID;
1005   pad_monitor->pending_eos_seqnum = GST_SEQNUM_INVALID;
1006
1007   if (pad_monitor->pending_setcaps_fields)
1008     gst_structure_free (pad_monitor->pending_setcaps_fields);
1009   pad_monitor->pending_setcaps_fields =
1010       gst_structure_new_empty (PENDING_FIELDS);
1011   if (pad_monitor->seeks)
1012     g_list_free_full (pad_monitor->seeks, (GDestroyNotify) seek_data_free);
1013   pad_monitor->current_seek = NULL;
1014   pad_monitor->seeks = NULL;
1015
1016   /* FIXME : Why BYTES and not UNDEFINED ? */
1017   gst_segment_init (&pad_monitor->segment, GST_FORMAT_BYTES);
1018
1019   pad_monitor->min_buf_freq = 0;
1020   pad_monitor->buffers_pushed = 0;
1021   pad_monitor->last_buffers_pushed = 0;
1022   pad_monitor->min_buf_freq_interval_ts = GST_CLOCK_TIME_NONE;
1023   pad_monitor->min_buf_freq_first_buffer_ts = GST_CLOCK_TIME_NONE;
1024   pad_monitor->min_buf_freq_start = GST_CLOCK_TIME_NONE;
1025 }
1026
1027 static void
1028 gst_validate_pad_monitor_init (GstValidatePadMonitor * pad_monitor)
1029 {
1030   pad_monitor->serialized_events =
1031       g_ptr_array_new_with_free_func ((GDestroyNotify)
1032       _serialized_event_data_free);
1033
1034   gst_validate_pad_monitor_reset (pad_monitor);
1035 }
1036
1037 /**
1038  * gst_validate_pad_monitor_new:
1039  * @pad: (transfer none): a #GstPad to run Validate on
1040  */
1041 GstValidatePadMonitor *
1042 gst_validate_pad_monitor_new (GstPad * pad, GstValidateRunner * runner,
1043     GstValidateElementMonitor * parent)
1044 {
1045   GstValidatePadMonitor *monitor = g_object_new (GST_TYPE_VALIDATE_PAD_MONITOR,
1046       "object", pad, "validate-runner", runner, "validate-parent",
1047       parent, NULL);
1048   GstObject *target =
1049       gst_validate_monitor_get_target (GST_VALIDATE_MONITOR (monitor));
1050
1051   if (target == NULL) {
1052     g_object_unref (monitor);
1053     return NULL;
1054   }
1055
1056   gst_object_unref (target);
1057   return monitor;
1058 }
1059
1060 static GstElement *
1061 gst_validate_pad_monitor_get_element (GstValidateMonitor * monitor)
1062 {
1063   GstPad *pad =
1064       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1065           (monitor)));
1066   GstElement *parent = GST_ELEMENT (gst_pad_get_parent (pad));
1067
1068   gst_object_unref (pad);
1069
1070   return parent;
1071 }
1072
1073 static void
1074 gst_validate_pad_monitor_event_overrides (GstValidatePadMonitor * pad_monitor,
1075     GstEvent * event)
1076 {
1077   GList *iter;
1078
1079   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1080   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1081       iter = g_list_next (iter)) {
1082     GstValidateOverride *override = iter->data;
1083
1084     gst_validate_override_event_handler (override,
1085         GST_VALIDATE_MONITOR_CAST (pad_monitor), event);
1086   }
1087   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1088 }
1089
1090 static void
1091 gst_validate_pad_monitor_buffer_overrides (GstValidatePadMonitor * pad_monitor,
1092     GstBuffer * buffer)
1093 {
1094   GList *iter;
1095
1096   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1097   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1098       iter = g_list_next (iter)) {
1099     GstValidateOverride *override = iter->data;
1100
1101     gst_validate_override_buffer_handler (override,
1102         GST_VALIDATE_MONITOR_CAST (pad_monitor), buffer);
1103   }
1104   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1105 }
1106
1107 static void
1108 gst_validate_pad_monitor_buffer_probe_overrides (GstValidatePadMonitor *
1109     pad_monitor, GstBuffer * buffer)
1110 {
1111   GList *iter;
1112
1113   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1114   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1115       iter = g_list_next (iter)) {
1116     GstValidateOverride *override = iter->data;
1117
1118     gst_validate_override_buffer_probe_handler (override,
1119         GST_VALIDATE_MONITOR_CAST (pad_monitor), buffer);
1120   }
1121   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1122 }
1123
1124 static void
1125 gst_validate_pad_monitor_query_overrides (GstValidatePadMonitor * pad_monitor,
1126     GstQuery * query)
1127 {
1128   GList *iter;
1129
1130   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1131   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1132       iter = g_list_next (iter)) {
1133     GstValidateOverride *override = iter->data;
1134
1135     gst_validate_override_query_handler (override,
1136         GST_VALIDATE_MONITOR_CAST (pad_monitor), query);
1137   }
1138   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1139 }
1140
1141 static void
1142 gst_validate_pad_monitor_setcaps_overrides (GstValidatePadMonitor * pad_monitor,
1143     GstCaps * caps)
1144 {
1145   GList *iter;
1146
1147   GST_VALIDATE_MONITOR_OVERRIDES_LOCK (pad_monitor);
1148   for (iter = GST_VALIDATE_MONITOR_OVERRIDES (pad_monitor).head; iter;
1149       iter = g_list_next (iter)) {
1150     GstValidateOverride *override = iter->data;
1151
1152     gst_validate_override_setcaps_handler (override,
1153         GST_VALIDATE_MONITOR_CAST (pad_monitor), caps);
1154   }
1155   GST_VALIDATE_MONITOR_OVERRIDES_UNLOCK (pad_monitor);
1156 }
1157
1158 /* FIXME : This is a bit dubious, what's the point of this check ? */
1159 static gboolean
1160 gst_validate_pad_monitor_timestamp_is_in_received_range (GstValidatePadMonitor *
1161     monitor, GstClockTime ts, GstClockTime tolerance)
1162 {
1163   GstPad *pad =
1164       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1165           (monitor)));
1166
1167   GST_DEBUG_OBJECT (pad,
1168       "Checking if timestamp %" GST_TIME_FORMAT " is in range: %"
1169       GST_TIME_FORMAT " - %" GST_TIME_FORMAT " for pad "
1170       "%s:%s with tolerance: %" GST_TIME_FORMAT, GST_TIME_ARGS (ts),
1171       GST_TIME_ARGS (monitor->timestamp_range_start),
1172       GST_TIME_ARGS (monitor->timestamp_range_end), GST_DEBUG_PAD_NAME (pad),
1173       GST_TIME_ARGS (tolerance));
1174   gst_object_unref (pad);
1175
1176   return !GST_CLOCK_TIME_IS_VALID (monitor->timestamp_range_start) ||
1177       !GST_CLOCK_TIME_IS_VALID (monitor->timestamp_range_end) ||
1178       ((monitor->timestamp_range_start >= tolerance ?
1179           monitor->timestamp_range_start - tolerance : 0) <= ts
1180       && (ts >= tolerance ? ts - tolerance : 0) <=
1181       monitor->timestamp_range_end);
1182 }
1183
1184 /* Iterates over internal links (sinkpads) to check that this buffer has
1185  * a timestamp that is in the range of the lastly received buffers */
1186 static void
1187     gst_validate_pad_monitor_check_buffer_timestamp_in_received_range
1188     (GstValidatePadMonitor * monitor, GstBuffer * buffer,
1189     GstClockTime tolerance)
1190 {
1191   GstClockTime ts;
1192   GstClockTime ts_end;
1193   GstIterator *iter;
1194   gboolean has_one = FALSE;
1195   gboolean found = FALSE;
1196   gboolean done;
1197   GstPad *otherpad;
1198   GstValidatePadMonitor *othermonitor;
1199   GstPad *pad =
1200       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1201           (monitor)));
1202
1203   if (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))
1204       || !GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) {
1205     GST_DEBUG_OBJECT (pad,
1206         "Can't check buffer timestamps range as "
1207         "buffer has no valid timestamp/duration");
1208     goto done;
1209   }
1210
1211   ts = GST_BUFFER_TIMESTAMP (buffer);
1212   ts_end = ts + GST_BUFFER_DURATION (buffer);
1213
1214   iter = gst_pad_iterate_internal_links (pad);
1215
1216   if (iter == NULL) {
1217     GST_WARNING_OBJECT (pad, "No iterator available");
1218     goto done;
1219   }
1220
1221   done = FALSE;
1222   while (!done) {
1223     GValue value = { 0, };
1224     switch (gst_iterator_next (iter, &value)) {
1225       case GST_ITERATOR_OK:
1226         otherpad = g_value_get_object (&value);
1227         GST_DEBUG_OBJECT (pad, "Checking pad %s:%s input timestamps",
1228             GST_DEBUG_PAD_NAME (otherpad));
1229         othermonitor = _GET_PAD_MONITOR (otherpad);
1230         if (!othermonitor)
1231           continue;
1232
1233         GST_VALIDATE_MONITOR_LOCK (othermonitor);
1234         if (gst_validate_pad_monitor_timestamp_is_in_received_range
1235             (othermonitor, ts, tolerance)
1236             &&
1237             gst_validate_pad_monitor_timestamp_is_in_received_range
1238             (othermonitor, ts_end, tolerance)) {
1239           done = TRUE;
1240           found = TRUE;
1241         }
1242         GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1243         g_value_reset (&value);
1244         has_one = TRUE;
1245         break;
1246       case GST_ITERATOR_RESYNC:
1247         gst_iterator_resync (iter);
1248         has_one = FALSE;
1249         found = FALSE;
1250         break;
1251       case GST_ITERATOR_ERROR:
1252         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1253         done = TRUE;
1254         break;
1255       case GST_ITERATOR_DONE:
1256         done = TRUE;
1257         break;
1258     }
1259   }
1260   gst_iterator_free (iter);
1261
1262   if (!has_one) {
1263     GST_DEBUG_OBJECT (pad, "Skipping timestamp in range check as no "
1264         "internal linked pad was found");
1265     goto done;
1266   }
1267   if (!found) {
1268     GST_VALIDATE_REPORT (monitor, BUFFER_TIMESTAMP_OUT_OF_RECEIVED_RANGE,
1269         "Timestamp %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT
1270         " is out of range of received input", GST_TIME_ARGS (ts),
1271         GST_TIME_ARGS (ts_end));
1272   }
1273 done:
1274   if (pad)
1275     gst_object_unref (pad);
1276 }
1277
1278 static void
1279 gst_validate_pad_monitor_check_discont (GstValidatePadMonitor * pad_monitor,
1280     GstBuffer * buffer)
1281 {
1282   if (pad_monitor->pending_buffer_discont) {
1283     if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
1284       GST_VALIDATE_REPORT (pad_monitor, BUFFER_MISSING_DISCONT,
1285           "Buffer is missing a DISCONT flag");
1286     pad_monitor->pending_buffer_discont = FALSE;
1287   }
1288 }
1289
1290 static void
1291 gst_validate_pad_monitor_check_first_buffer (GstValidatePadMonitor *
1292     pad_monitor, GstBuffer * buffer)
1293 {
1294   GstPad *pad =
1295       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1296           (pad_monitor)));
1297
1298   if (G_UNLIKELY (pad_monitor->first_buffer)) {
1299     pad_monitor->first_buffer = FALSE;
1300
1301     if (!pad_monitor->has_segment && PAD_IS_IN_PUSH_MODE (pad)) {
1302       GST_VALIDATE_REPORT (pad_monitor, BUFFER_BEFORE_SEGMENT,
1303           "Received buffer before Segment event");
1304     }
1305
1306     GST_DEBUG_OBJECT (pad,
1307         "Checking first buffer (pts:%" GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT
1308         ")", GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1309         GST_TIME_ARGS (GST_BUFFER_DTS (buffer)));
1310
1311   }
1312
1313   gst_object_unref (pad);
1314 }
1315
1316 static void
1317 gst_validate_pad_monitor_check_eos (GstValidatePadMonitor *
1318     pad_monitor, GstBuffer * buffer)
1319 {
1320   if (G_UNLIKELY (pad_monitor->is_eos)) {
1321     GST_VALIDATE_REPORT (pad_monitor, BUFFER_AFTER_EOS,
1322         "Received buffer %" GST_PTR_FORMAT " after EOS", buffer);
1323   }
1324 }
1325
1326 static void
1327 gst_validate_pad_monitor_update_buffer_data (GstValidatePadMonitor *
1328     pad_monitor, GstBuffer * buffer)
1329 {
1330   GstPad *pad =
1331       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1332           (pad_monitor)));
1333   pad_monitor->current_timestamp = GST_BUFFER_TIMESTAMP (buffer);
1334   pad_monitor->current_duration = GST_BUFFER_DURATION (buffer);
1335   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) {
1336     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->timestamp_range_start)) {
1337       pad_monitor->timestamp_range_start =
1338           MIN (pad_monitor->timestamp_range_start,
1339           GST_BUFFER_TIMESTAMP (buffer));
1340     } else {
1341       pad_monitor->timestamp_range_start = GST_BUFFER_TIMESTAMP (buffer);
1342     }
1343
1344     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) {
1345       GstClockTime endts =
1346           GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1347       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->timestamp_range_end)) {
1348         pad_monitor->timestamp_range_end =
1349             MAX (pad_monitor->timestamp_range_end, endts);
1350       } else {
1351         pad_monitor->timestamp_range_end = endts;
1352       }
1353     }
1354   }
1355   GST_DEBUG_OBJECT (pad, "Current stored range: %" GST_TIME_FORMAT
1356       " - %" GST_TIME_FORMAT,
1357       GST_TIME_ARGS (pad_monitor->timestamp_range_start),
1358       GST_TIME_ARGS (pad_monitor->timestamp_range_end));
1359
1360   gst_object_unref (pad);
1361 }
1362
1363 static GstFlowReturn
1364 _combine_flows (GstFlowReturn ret1, GstFlowReturn ret2)
1365 {
1366   if (ret1 == ret2)
1367     return ret1;
1368   if (ret1 <= GST_FLOW_NOT_NEGOTIATED)
1369     return ret1;
1370   if (ret2 <= GST_FLOW_NOT_NEGOTIATED)
1371     return ret2;
1372   if (ret1 == GST_FLOW_FLUSHING || ret2 == GST_FLOW_FLUSHING)
1373     return GST_FLOW_FLUSHING;
1374   if (ret1 == GST_FLOW_OK || ret2 == GST_FLOW_OK)
1375     return GST_FLOW_OK;
1376   return ret2;
1377 }
1378
1379 static void
1380 gst_validate_pad_monitor_check_aggregated_return (GstValidatePadMonitor *
1381     monitor, GstObject * parent, GstFlowReturn ret)
1382 {
1383   GstIterator *iter;
1384   gboolean done;
1385   GstPad *otherpad;
1386   GstPad *peerpad;
1387   GstState state, pending;
1388   GstFlowReturn aggregated = GST_FLOW_NOT_LINKED;
1389   gboolean found_a_pad = FALSE;
1390   GstPad *pad =
1391       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1392           (monitor)));
1393
1394   iter = gst_pad_iterate_internal_links (pad);
1395   done = FALSE;
1396   while (!done) {
1397     GValue value = { 0, };
1398     switch (gst_iterator_next (iter, &value)) {
1399       case GST_ITERATOR_OK:
1400         otherpad = g_value_get_object (&value);
1401         peerpad = gst_pad_get_peer (otherpad);
1402         if (peerpad) {
1403           found_a_pad = TRUE;
1404           aggregated =
1405               _combine_flows (aggregated,
1406               gst_pad_get_last_flow_return (peerpad));
1407
1408           gst_object_unref (peerpad);
1409         }
1410         g_value_reset (&value);
1411         break;
1412       case GST_ITERATOR_RESYNC:
1413         gst_iterator_resync (iter);
1414         break;
1415       case GST_ITERATOR_ERROR:
1416         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1417         done = TRUE;
1418         break;
1419       case GST_ITERATOR_DONE:
1420         done = TRUE;
1421         break;
1422     }
1423   }
1424   gst_iterator_free (iter);
1425   if (!found_a_pad) {
1426     /* no peer pad found, nothing to do */
1427     goto done;
1428   }
1429
1430   if (aggregated == GST_FLOW_FLUSHING) {
1431     gst_element_get_state (GST_ELEMENT (parent), &state, &pending, 0);
1432     if (state < GST_STATE_PAUSED || pending < GST_STATE_PAUSED) {
1433       /* Aggregated is flushing, we might have been aggregating a combination
1434        * of pads that are not what was present on the element during the actual
1435        * data flow combination (pads might have been removed meanwhile) */
1436
1437       goto done;
1438     }
1439   } else if (aggregated == GST_FLOW_OK || aggregated == GST_FLOW_EOS) {
1440
1441     /* those are acceptable situations */
1442     if (GST_PAD_IS_FLUSHING (pad) && ret == GST_FLOW_FLUSHING) {
1443       /* pad is flushing, always acceptable to return flushing */
1444       goto done;
1445     }
1446
1447     gst_element_get_state (GST_ELEMENT (parent), &state, &pending, 0);
1448     if (ret == GST_FLOW_FLUSHING && (state < GST_STATE_PAUSED
1449             || pending < GST_STATE_PAUSED)) {
1450       /* Element is being teared down, accept FLOW_FLUSHING */
1451
1452       goto done;
1453     }
1454
1455     if (monitor->is_eos && ret == GST_FLOW_EOS) {
1456       /* this element received eos and returned eos */
1457       goto done;
1458     }
1459
1460     if (PAD_PARENT_IS_DEMUXER (monitor) && ret == GST_FLOW_EOS) {
1461       /* a demuxer can return EOS when the samples end */
1462       goto done;
1463     }
1464   }
1465
1466   if (aggregated != ret) {
1467     GST_VALIDATE_REPORT (monitor, WRONG_FLOW_RETURN,
1468         "Wrong combined flow return %s(%d). Expected: %s(%d)",
1469         gst_flow_get_name (ret), ret, gst_flow_get_name (aggregated),
1470         aggregated);
1471   }
1472
1473 done:
1474   gst_object_unref (pad);
1475 }
1476
1477 static void
1478     gst_validate_pad_monitor_otherpad_add_pending_serialized_event
1479     (GstValidatePadMonitor * monitor, GstEvent * event, GstClockTime last_ts)
1480 {
1481   GstIterator *iter;
1482   gboolean done;
1483   GstPad *otherpad;
1484   GstValidatePadMonitor *othermonitor;
1485   GstPad *pad;
1486
1487
1488   if (!GST_EVENT_IS_SERIALIZED (event))
1489     return;
1490
1491   pad =
1492       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1493           (monitor)));
1494   iter = gst_pad_iterate_internal_links (pad);
1495   if (iter == NULL) {
1496     /* inputselector will return NULL if the sinkpad is not the active one .... */
1497     GST_FIXME_OBJECT (pad, "No iterator");
1498     gst_object_unref (pad);
1499     return;
1500   }
1501
1502   done = FALSE;
1503   while (!done) {
1504     GValue value = { 0, };
1505     switch (gst_iterator_next (iter, &value)) {
1506       case GST_ITERATOR_OK:
1507         otherpad = g_value_get_object (&value);
1508         othermonitor = _GET_PAD_MONITOR (otherpad);
1509         if (othermonitor) {
1510           SerializedEventData *data = g_slice_new0 (SerializedEventData);
1511           data->timestamp = last_ts;
1512           data->event = gst_event_ref (event);
1513           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1514           GST_DEBUG_OBJECT (pad, "Storing for pad %s:%s event %p %s",
1515               GST_DEBUG_PAD_NAME (otherpad), event,
1516               GST_EVENT_TYPE_NAME (event));
1517           g_ptr_array_add (othermonitor->serialized_events, data);
1518           debug_pending_event (otherpad, othermonitor->serialized_events);
1519           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1520         }
1521         g_value_reset (&value);
1522         break;
1523       case GST_ITERATOR_RESYNC:
1524         gst_iterator_resync (iter);
1525         break;
1526       case GST_ITERATOR_ERROR:
1527         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1528         done = TRUE;
1529         break;
1530       case GST_ITERATOR_DONE:
1531         done = TRUE;
1532         break;
1533     }
1534   }
1535   gst_iterator_free (iter);
1536   gst_object_unref (pad);
1537 }
1538
1539 static void
1540 gst_validate_pad_monitor_otherpad_add_pending_field (GstValidatePadMonitor *
1541     monitor, GstStructure * structure, const gchar * field)
1542 {
1543   GstIterator *iter;
1544   gboolean done;
1545   GstPad *otherpad;
1546   GstValidatePadMonitor *othermonitor;
1547   const GValue *v;
1548   GstPad *pad;
1549
1550   v = gst_structure_get_value (structure, field);
1551   pad =
1552       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1553           (monitor)));
1554
1555   if (v == NULL) {
1556     GST_DEBUG_OBJECT (pad, "Not adding pending field %s as it isn't "
1557         "present on structure %" GST_PTR_FORMAT, field, structure);
1558     gst_object_unref (pad);
1559     return;
1560   }
1561
1562   iter = gst_pad_iterate_internal_links (pad);
1563   done = FALSE;
1564   while (!done) {
1565     GValue value = { 0, };
1566     switch (gst_iterator_next (iter, &value)) {
1567       case GST_ITERATOR_OK:
1568         otherpad = g_value_get_object (&value);
1569         othermonitor = _GET_PAD_MONITOR (otherpad);
1570         if (othermonitor) {
1571           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1572           g_assert (othermonitor->pending_setcaps_fields != NULL);
1573           gst_structure_set_value (othermonitor->pending_setcaps_fields,
1574               field, v);
1575           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1576         }
1577         g_value_reset (&value);
1578         break;
1579       case GST_ITERATOR_RESYNC:
1580         gst_iterator_resync (iter);
1581         break;
1582       case GST_ITERATOR_ERROR:
1583         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1584         done = TRUE;
1585         break;
1586       case GST_ITERATOR_DONE:
1587         done = TRUE;
1588         break;
1589     }
1590   }
1591   gst_iterator_free (iter);
1592   gst_object_unref (pad);
1593 }
1594
1595 static void
1596 gst_validate_pad_monitor_otherpad_clear_pending_fields (GstValidatePadMonitor *
1597     monitor)
1598 {
1599   GstIterator *iter;
1600   gboolean done;
1601   GstPad *otherpad;
1602   GstValidatePadMonitor *othermonitor;
1603
1604   GstPad *pad =
1605       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1606           (monitor)));
1607
1608   iter = gst_pad_iterate_internal_links (pad);
1609   if (iter == NULL) {
1610     gst_object_unref (pad);
1611     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1612
1613     return;
1614   }
1615
1616   done = FALSE;
1617   while (!done) {
1618     GValue value = { 0, };
1619     switch (gst_iterator_next (iter, &value)) {
1620       case GST_ITERATOR_OK:
1621         otherpad = g_value_get_object (&value);
1622         othermonitor = _GET_PAD_MONITOR (otherpad);
1623         if (othermonitor) {
1624           GST_VALIDATE_MONITOR_LOCK (othermonitor);
1625           g_assert (othermonitor->pending_setcaps_fields != NULL);
1626           gst_structure_free (othermonitor->pending_setcaps_fields);
1627           othermonitor->pending_setcaps_fields =
1628               gst_structure_new_empty (PENDING_FIELDS);
1629           GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1630         }
1631         g_value_reset (&value);
1632         break;
1633       case GST_ITERATOR_RESYNC:
1634         gst_iterator_resync (iter);
1635         break;
1636       case GST_ITERATOR_ERROR:
1637         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1638         done = TRUE;
1639         break;
1640       case GST_ITERATOR_DONE:
1641         done = TRUE;
1642         break;
1643     }
1644   }
1645   gst_object_unref (pad);
1646   gst_iterator_free (iter);
1647 }
1648
1649 static void
1650 gst_validate_pad_monitor_add_expected_newsegment (GstValidatePadMonitor *
1651     monitor, GstEvent * event)
1652 {
1653   GstIterator *iter;
1654   gboolean done;
1655   GstPad *otherpad;
1656   GstValidatePadMonitor *othermonitor;
1657   GstPad *pad =
1658       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1659           (monitor)));
1660
1661   iter = gst_pad_iterate_internal_links (pad);
1662   if (iter == NULL) {
1663     GST_DEBUG_OBJECT (monitor, "No internally linked pad");
1664     gst_object_unref (pad);
1665     return;
1666   }
1667
1668   done = FALSE;
1669   while (!done) {
1670     GValue value = { 0, };
1671     switch (gst_iterator_next (iter, &value)) {
1672       case GST_ITERATOR_OK:
1673         otherpad = g_value_get_object (&value);
1674         if (!otherpad) {
1675           g_value_reset (&value);
1676           continue;
1677         }
1678
1679         othermonitor = _GET_PAD_MONITOR (otherpad);
1680         if (!othermonitor) {
1681           g_value_reset (&value);
1682           continue;
1683         }
1684
1685         GST_VALIDATE_MONITOR_LOCK (othermonitor);
1686         gst_event_replace (&othermonitor->expected_segment, event);
1687         GST_VALIDATE_MONITOR_UNLOCK (othermonitor);
1688         g_value_reset (&value);
1689         break;
1690       case GST_ITERATOR_RESYNC:
1691         gst_iterator_resync (iter);
1692         break;
1693       case GST_ITERATOR_ERROR:
1694         GST_WARNING_OBJECT (pad, "Internal links pad iteration error");
1695         done = TRUE;
1696         break;
1697       case GST_ITERATOR_DONE:
1698         done = TRUE;
1699         break;
1700     }
1701   }
1702   gst_iterator_free (iter);
1703   gst_object_unref (pad);
1704 }
1705
1706 /* common checks for both sink and src event functions */
1707 static void
1708 gst_validate_pad_monitor_common_event_check (GstValidatePadMonitor *
1709     pad_monitor, GstEvent * event)
1710 {
1711   guint32 seqnum = gst_event_get_seqnum (event);
1712
1713   if (seqnum == GST_SEQNUM_INVALID)
1714     GST_VALIDATE_REPORT (pad_monitor, EVENT_INVALID_SEQNUM,
1715         "Event %p (%s) has an invalid SEQNUM", event,
1716         GST_EVENT_TYPE_NAME (event));
1717
1718   switch (GST_EVENT_TYPE (event)) {
1719     case GST_EVENT_FLUSH_START:
1720     {
1721       if (pad_monitor->seeks) {
1722         GstValidatePadSeekData *seekdata =
1723             seek_data_for_seqnum (pad_monitor, seqnum);
1724
1725         if (!seekdata)
1726           GST_VALIDATE_REPORT (pad_monitor, FLUSH_START_HAS_WRONG_SEQNUM,
1727               "Got: %" G_GUINT32_FORMAT " Expected: %" G_GUINT32_FORMAT, seqnum,
1728               ((GstValidatePadSeekData *) pad_monitor->seeks->data)->seqnum);
1729         else {
1730           if (!(seekdata->flags & GST_SEEK_FLAG_FLUSH)) {
1731             GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_START_UNEXPECTED,
1732                 "Received flush-start for a non-flushing seek");
1733           }
1734         }
1735       }
1736
1737       if (pad_monitor->pending_flush_stop) {
1738         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_START_UNEXPECTED,
1739             "Received flush-start from when flush-stop was expected");
1740       }
1741       pad_monitor->pending_flush_stop = TRUE;
1742       /* Remove the current segment seekdata */
1743       if (pad_monitor->current_seek) {
1744         pad_monitor->seeks =
1745             g_list_remove (pad_monitor->seeks, pad_monitor->current_seek);
1746         seek_data_free (pad_monitor->current_seek);
1747         pad_monitor->current_seek = NULL;
1748       }
1749     }
1750       break;
1751     case GST_EVENT_FLUSH_STOP:
1752     {
1753       if (pad_monitor->seeks && !seek_data_for_seqnum (pad_monitor, seqnum)) {
1754         GST_VALIDATE_REPORT (pad_monitor, FLUSH_STOP_HAS_WRONG_SEQNUM,
1755             "Got: %" G_GUINT32_FORMAT " Expected: %" G_GUINT32_FORMAT, seqnum,
1756             ((GstValidatePadSeekData *) pad_monitor->seeks->data)->seqnum);
1757       }
1758
1759       pad_monitor->pending_newsegment_seqnum = seqnum;
1760       pad_monitor->pending_eos_seqnum = seqnum;
1761
1762       if (!pad_monitor->pending_flush_stop) {
1763         gchar *event_str = _get_event_string (event);
1764
1765         GST_VALIDATE_REPORT (pad_monitor, EVENT_FLUSH_STOP_UNEXPECTED,
1766             "Unexpected flush-stop %s", event_str);
1767         g_free (event_str);
1768       }
1769       pad_monitor->pending_flush_stop = FALSE;
1770
1771       /* Buffers following a FLUSH should have the DISCONT flag set */
1772       pad_monitor->pending_buffer_discont = TRUE;
1773
1774       /* cleanup our data */
1775       gst_validate_pad_monitor_flush (pad_monitor);
1776     }
1777       break;
1778     default:
1779       break;
1780   }
1781 }
1782
1783 static void
1784 mark_pads_eos (GstValidatePadMonitor * pad_monitor)
1785 {
1786   GstValidatePadMonitor *peer_monitor;
1787   GstPad *real_peer;
1788   GstPad *pad =
1789       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1790           (pad_monitor)));
1791   GstPad *peer = gst_pad_get_peer (pad);
1792
1793   gst_object_unref (pad);
1794   pad_monitor->is_eos = TRUE;
1795   if (peer) {
1796     real_peer = _get_actual_pad (peer);
1797     peer_monitor = _GET_PAD_MONITOR (real_peer);
1798     if (peer_monitor)
1799       peer_monitor->is_eos = TRUE;
1800     gst_object_unref (peer);
1801     gst_object_unref (real_peer);
1802   }
1803 }
1804
1805 static inline gboolean
1806 _should_check_buffers (GstValidatePadMonitor * pad_monitor,
1807     gboolean force_checks)
1808 {
1809   GstPad *pad =
1810       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1811           (pad_monitor)));
1812   GstValidateMonitor *monitor = GST_VALIDATE_MONITOR (pad_monitor);
1813
1814   if (pad_monitor->first_buffer || force_checks) {
1815     if (pad_monitor->segment.rate != 1.0) {
1816       GST_INFO_OBJECT (pad_monitor, "We do not support buffer checking"
1817           " for trick modes");
1818
1819       pad_monitor->check_buffers = FALSE;
1820     } else if (!PAD_PARENT_IS_DECODER (pad_monitor)) {
1821       GST_DEBUG_OBJECT (pad, "Not on a decoder => no buffer checking");
1822
1823       pad_monitor->check_buffers = FALSE;
1824     } else if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK) {
1825       GST_DEBUG_OBJECT (pad, "Not a sinkpad => no buffer checking");
1826
1827       pad_monitor->check_buffers = FALSE;
1828     } else if (!pad_monitor->caps_is_video) {
1829       GST_DEBUG_OBJECT (pad, "Not working with video => no buffer checking");
1830
1831       pad_monitor->check_buffers = FALSE;
1832     } else if (monitor->media_descriptor == NULL) {
1833       GST_DEBUG_OBJECT (pad, "No media_descriptor set => no buffer checking");
1834
1835       pad_monitor->check_buffers = FALSE;
1836     } else if (!gst_validate_media_descriptor_detects_frames
1837         (monitor->media_descriptor)) {
1838       GST_DEBUG_OBJECT (pad,
1839           "No frame detection media descriptor => no buffer checking");
1840       pad_monitor->check_buffers = FALSE;
1841     } else if (pad_monitor->all_bufs == NULL &&
1842         !gst_validate_media_descriptor_get_buffers (monitor->media_descriptor,
1843             pad, NULL, &pad_monitor->all_bufs)) {
1844
1845       GST_INFO_OBJECT (monitor,
1846           "The MediaInfo is marked as detecting frame, but getting frames"
1847           " from pad %" GST_PTR_FORMAT " did not work (some format conversion"
1848           " might be happening)", pad);
1849
1850       pad_monitor->check_buffers = FALSE;
1851     } else {
1852       if (!pad_monitor->current_buf)
1853         pad_monitor->current_buf = pad_monitor->all_bufs;
1854       pad_monitor->check_buffers = TRUE;
1855     }
1856   }
1857   gst_object_unref (pad);
1858
1859   return pad_monitor->check_buffers;
1860 }
1861
1862 static void
1863 gst_validate_monitor_find_next_buffer (GstValidatePadMonitor * pad_monitor)
1864 {
1865   GList *tmp;
1866   gboolean passed_start = FALSE;
1867
1868   if (!_should_check_buffers (pad_monitor, TRUE))
1869     return;
1870
1871   for (tmp = g_list_last (pad_monitor->all_bufs); tmp; tmp = tmp->prev) {
1872     GstBuffer *cbuf = (GstBuffer *) tmp->data;
1873     GstClockTime ts =
1874         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cbuf)) ? GST_BUFFER_DTS (cbuf)
1875         : GST_BUFFER_PTS (cbuf);
1876
1877     if (!GST_CLOCK_TIME_IS_VALID (ts))
1878       continue;
1879
1880     if (ts <= pad_monitor->segment.start)
1881       passed_start = TRUE;
1882
1883     if (!passed_start)
1884       continue;
1885
1886     if (!GST_BUFFER_FLAG_IS_SET (cbuf, GST_BUFFER_FLAG_DELTA_UNIT)) {
1887       break;
1888     }
1889   }
1890
1891   if (tmp == NULL)
1892     pad_monitor->current_buf = pad_monitor->all_bufs;
1893   else
1894     pad_monitor->current_buf = tmp;
1895 }
1896
1897 static void
1898 post_segment_message (GstValidatePadMonitor * pad_monitor, GstPad * pad,
1899     const GstSegment * segment, guint32 seqnum)
1900 {
1901   GstValidateMonitor *element_monitor =
1902       GST_VALIDATE_MONITOR_GET_PARENT (pad_monitor);
1903   GstElement *element;
1904   GstStructure *structure;
1905   GstMessage *msg;
1906
1907   if (element_monitor == NULL)
1908     return;
1909
1910   element = gst_validate_monitor_get_element (element_monitor);
1911   if (element == NULL)
1912     return;
1913
1914   GST_DEBUG_OBJECT (pad,
1915       "Posting application message for seqnum:%" G_GUINT32_FORMAT " %"
1916       GST_SEGMENT_FORMAT, seqnum, segment);
1917
1918   structure =
1919       gst_structure_new ("validate-segment", "segment", GST_TYPE_SEGMENT,
1920       segment, NULL);
1921   msg = gst_message_new_application ((GstObject *) element, structure);
1922   gst_message_set_seqnum (msg, seqnum);
1923   gst_element_post_message (element, msg);
1924
1925   gst_object_unref (element);
1926
1927   return;
1928 }
1929
1930 /* Checks whether a segment is just an update of another,
1931  * That is to say that only the base and offset field differ and all
1932  * other fields are identical */
1933 static gboolean
1934 is_segment_update (GstSegment * a, const GstSegment * b)
1935 {
1936   /* Note : We never care about the position field, it is only
1937    * used for internal usage by elements */
1938   if (a->rate == b->rate &&
1939       a->applied_rate == b->applied_rate &&
1940       a->format == b->format && a->time == b->time) {
1941     /* Changes in base/offset are considered updates */
1942     /* Updating the end position of a segment is an update */
1943     /* Updating the duration of a segment is an update */
1944     if (a->rate > 0.0) {
1945       if (a->start == b->start)
1946         return TRUE;
1947     } else {
1948       if (a->stop == b->stop)
1949         return TRUE;
1950     }
1951   }
1952   return FALSE;
1953 }
1954
1955 static GstFlowReturn
1956 gst_validate_pad_monitor_downstream_event_check (GstValidatePadMonitor *
1957     pad_monitor, GstObject * parent, GstEvent * event,
1958     GstPadEventFunction handler)
1959 {
1960   GstFlowReturn ret = GST_FLOW_OK;
1961   const GstSegment *segment;
1962   guint32 seqnum = gst_event_get_seqnum (event);
1963   GstPad *pad =
1964       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
1965           (pad_monitor)));
1966
1967   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
1968
1969   /* pre checks */
1970   switch (GST_EVENT_TYPE (event)) {
1971     case GST_EVENT_STREAM_START:
1972       /* Buffers following a STREAM_START should have the DISCONT flag set */
1973       pad_monitor->pending_buffer_discont = TRUE;
1974       break;
1975     case GST_EVENT_SEGMENT:
1976     {
1977       GstValidatePadSeekData *seekdata =
1978           seek_data_for_seqnum (pad_monitor, seqnum);
1979
1980       /* parse segment data to be used if event is handled */
1981       gst_event_parse_segment (event, &segment);
1982
1983       GST_DEBUG_OBJECT (pad,
1984           "Got segment seqnum:%" G_GUINT32_FORMAT " %" GST_SEGMENT_FORMAT,
1985           seqnum, segment);
1986
1987       if (pad_monitor->pending_newsegment_seqnum != GST_SEQNUM_INVALID) {
1988         /* FIXME: Convert to more robust checks */
1989         if (pad_monitor->pending_newsegment_seqnum != seqnum) {
1990           GST_VALIDATE_REPORT (pad_monitor, SEGMENT_HAS_WRONG_SEQNUM,
1991               "Got: %u Expected: %u", seqnum,
1992               pad_monitor->pending_newsegment_seqnum);
1993         }
1994       }
1995
1996       if (seekdata && seekdata != pad_monitor->current_seek) {
1997         /* Check for accurate seeks */
1998         if (seekdata->flags & GST_SEEK_FLAG_ACCURATE) {
1999           if (segment->time != seekdata->start)
2000             GST_VALIDATE_REPORT (pad_monitor, SEGMENT_HAS_WRONG_START,
2001                 "After an accurate seek, got: %" GST_TIME_FORMAT
2002                 " Expected: %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->time),
2003                 GST_TIME_ARGS (seekdata->start));
2004         }
2005       }
2006
2007       pad_monitor->pending_eos_seqnum = seqnum;
2008
2009       if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2010         gst_validate_pad_monitor_add_expected_newsegment (pad_monitor, event);
2011       } else {
2012         /* check if this segment is the expected one */
2013         if (pad_monitor->expected_segment) {
2014           const GstSegment *exp_segment;
2015
2016           if (pad_monitor->expected_segment != event) {
2017             gst_event_parse_segment (pad_monitor->expected_segment,
2018                 &exp_segment);
2019             if (segment->format == exp_segment->format) {
2020               if ((exp_segment->rate * exp_segment->applied_rate !=
2021                       segment->rate * segment->applied_rate))
2022                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
2023                     "Rate * applied_rate %f != expected %f",
2024                     segment->rate * segment->applied_rate,
2025                     exp_segment->rate * exp_segment->applied_rate);
2026               if (exp_segment->start != segment->start)
2027                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
2028                     "Start %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
2029                     GST_TIME_ARGS (segment->start),
2030                     GST_TIME_ARGS (exp_segment->start));
2031               if (exp_segment->stop != segment->stop)
2032                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
2033                     "Stop %" GST_TIME_FORMAT " != expected %" GST_TIME_FORMAT,
2034                     GST_TIME_ARGS (segment->stop),
2035                     GST_TIME_ARGS (exp_segment->stop));
2036               if (exp_segment->position != segment->position)
2037                 GST_VALIDATE_REPORT (pad_monitor, EVENT_NEW_SEGMENT_MISMATCH,
2038                     "Position %" GST_TIME_FORMAT " != expected %"
2039                     GST_TIME_FORMAT, GST_TIME_ARGS (segment->position),
2040                     GST_TIME_ARGS (exp_segment->position));
2041             }
2042           }
2043           gst_event_replace (&pad_monitor->expected_segment, NULL);
2044         }
2045       }
2046
2047       /* Drop all expected seekdata from before this segment */
2048       if (seekdata) {
2049         while (pad_monitor->seeks && pad_monitor->seeks->data != seekdata) {
2050           GstValidatePadSeekData *tmp =
2051               (GstValidatePadSeekData *) pad_monitor->seeks->data;
2052           pad_monitor->seeks =
2053               g_list_delete_link (pad_monitor->seeks, pad_monitor->seeks);
2054           seek_data_free (tmp);
2055         }
2056       }
2057       pad_monitor->current_seek = seekdata;
2058     }
2059       break;
2060     case GST_EVENT_CAPS:{
2061       GstCaps *caps;
2062
2063       gst_event_parse_caps (event, &caps);
2064       gst_validate_pad_monitor_setcaps_pre (pad_monitor, caps);
2065       break;
2066     }
2067     case GST_EVENT_EOS:
2068       pad_monitor->is_eos = TRUE;
2069       /* FIXME : This feels and looks wrong ... */
2070       if (pad_monitor->pending_eos_seqnum == GST_SEQNUM_INVALID) {
2071         GST_VALIDATE_REPORT (pad_monitor, EVENT_EOS_WITHOUT_SEGMENT,
2072             "EOS %" GST_PTR_FORMAT " received before a segment was received",
2073             event);
2074       } else if (pad_monitor->pending_eos_seqnum != seqnum) {
2075         GST_VALIDATE_REPORT (pad_monitor, EOS_HAS_WRONG_SEQNUM,
2076             "Got: %u. Expected: %u", seqnum, pad_monitor->pending_eos_seqnum);
2077       }
2078
2079       /*
2080        * TODO add end of stream checks for
2081        *  - events not pushed
2082        *  - buffer data not pushed
2083        *  - pending events not received
2084        */
2085       break;
2086
2087       /* both flushes are handled by the common event function */
2088     case GST_EVENT_FLUSH_START:
2089     case GST_EVENT_FLUSH_STOP:
2090     case GST_EVENT_TAG:
2091     case GST_EVENT_SINK_MESSAGE:
2092     default:
2093       break;
2094   }
2095
2096   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2097   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2098   gst_validate_pad_monitor_event_overrides (pad_monitor, event);
2099   if (handler) {
2100     gst_event_ref (event);
2101     if (pad_monitor->event_full_func)
2102       ret = pad_monitor->event_full_func (pad, parent, event);
2103     else if (pad_monitor->event_func (pad, parent, event))
2104       ret = GST_FLOW_OK;
2105     else
2106       ret = GST_FLOW_ERROR;
2107   }
2108   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2109   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2110
2111   /* post checks */
2112   switch (GST_EVENT_TYPE (event)) {
2113     case GST_EVENT_SEGMENT:
2114       if (ret == GST_FLOW_OK) {
2115         /* If the new segment is not an update of the previous one, then
2116          * the following buffer should have the DISCONT flag set */
2117         if (!is_segment_update (&pad_monitor->segment, segment))
2118           pad_monitor->pending_buffer_discont = TRUE;
2119         if (!pad_monitor->has_segment
2120             && pad_monitor->segment.format != segment->format) {
2121           gst_segment_init (&pad_monitor->segment, segment->format);
2122         }
2123         gst_segment_copy_into (segment, &pad_monitor->segment);
2124         pad_monitor->has_segment = TRUE;
2125         gst_validate_monitor_find_next_buffer (pad_monitor);
2126         if (PAD_PARENT_IS_SINK (pad_monitor))
2127           post_segment_message (pad_monitor, pad, segment, seqnum);
2128       }
2129       break;
2130     case GST_EVENT_CAPS:{
2131       GstCaps *caps;
2132
2133       gst_event_parse_caps (event, &caps);
2134       gst_validate_pad_monitor_setcaps_post (pad_monitor, caps,
2135           ret == GST_FLOW_OK);
2136       break;
2137     }
2138     case GST_EVENT_FLUSH_START:
2139     case GST_EVENT_FLUSH_STOP:
2140     case GST_EVENT_EOS:
2141     case GST_EVENT_TAG:
2142     case GST_EVENT_SINK_MESSAGE:
2143     default:
2144       break;
2145   }
2146
2147   if (handler)
2148     gst_event_unref (event);
2149   gst_object_unref (pad);
2150   return ret;
2151 }
2152
2153 static GstValidatePadSeekData *
2154 _store_seek_event_data (GstValidatePadMonitor * pad_monitor, GstEvent * event)
2155 {
2156   GstValidatePadSeekData *data = g_slice_new0 (GstValidatePadSeekData);
2157
2158   data->seqnum = gst_event_get_seqnum (event);
2159   gst_event_parse_seek (event, &data->rate, &data->format, &data->flags,
2160       &data->start_type, &data->start, &data->stop_type, &data->stop);
2161
2162   pad_monitor->seeks = g_list_append (pad_monitor->seeks, data);
2163
2164   return data;
2165 }
2166
2167 static gboolean
2168 gst_validate_pad_monitor_src_event_check (GstValidatePadMonitor * pad_monitor,
2169     GstObject * parent, GstEvent * event, GstPadEventFunction handler)
2170 {
2171   gboolean ret = TRUE;
2172   GstPad *pad =
2173       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2174           (pad_monitor)));
2175
2176   gst_validate_pad_monitor_common_event_check (pad_monitor, event);
2177
2178   if (handler) {
2179     GstValidatePadSeekData *seekdata = NULL;
2180
2181     GST_DEBUG_OBJECT (pad, "event %" GST_PTR_FORMAT, event);
2182
2183     /* Safely store pending accurate seek values */
2184     if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK)
2185       seekdata = _store_seek_event_data (pad_monitor, event);
2186     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2187     ret = pad_monitor->event_func (pad, parent, event);
2188
2189     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2190
2191     if (seekdata && !ret) {
2192       /* Remove failed seek from list */
2193       GST_LOG_OBJECT (pad, "Failed seek, removing stored seek data");
2194       pad_monitor->seeks = g_list_remove (pad_monitor->seeks, seekdata);
2195       g_slice_free (GstValidatePadSeekData, seekdata);
2196     }
2197   }
2198
2199   gst_object_unref (pad);
2200   return ret;
2201 }
2202
2203 static gboolean
2204 gst_validate_pad_monitor_check_right_buffer (GstValidatePadMonitor *
2205     pad_monitor, GstBuffer * buffer)
2206 {
2207   gchar *checksum;
2208   GstBuffer *wanted_buf;
2209   GstMapInfo map, wanted_map;
2210
2211   gboolean ret = TRUE;
2212   GstPad *pad;
2213
2214
2215   if (_should_check_buffers (pad_monitor, FALSE) == FALSE)
2216     return FALSE;
2217
2218   pad =
2219       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2220           (pad_monitor)));
2221   if (pad_monitor->current_buf == NULL) {
2222     GST_INFO_OBJECT (pad, "No current buffer one pad, Why?");
2223     gst_object_unref (pad);
2224     return FALSE;
2225   }
2226
2227   wanted_buf = pad_monitor->current_buf->data;
2228
2229   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (wanted_buf)) &&
2230       GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buffer)) &&
2231       GST_BUFFER_PTS (wanted_buf) != GST_BUFFER_PTS (buffer)) {
2232
2233     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2234         "buffer %" GST_PTR_FORMAT " PTS %" GST_TIME_FORMAT
2235         " different than expected: %" GST_TIME_FORMAT, buffer,
2236         GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2237         GST_TIME_ARGS (GST_BUFFER_PTS (wanted_buf)));
2238
2239     ret = FALSE;
2240   }
2241
2242   if (GST_BUFFER_DTS (wanted_buf) != GST_BUFFER_DTS (buffer)) {
2243     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2244         "buffer %" GST_PTR_FORMAT " DTS %" GST_TIME_FORMAT
2245         " different than expected: %" GST_TIME_FORMAT, buffer,
2246         GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
2247         GST_TIME_ARGS (GST_BUFFER_DTS (wanted_buf)));
2248     ret = FALSE;
2249   }
2250
2251   if (GST_BUFFER_DURATION (wanted_buf) != GST_BUFFER_DURATION (buffer)) {
2252     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2253         "buffer %" GST_PTR_FORMAT " DURATION %" GST_TIME_FORMAT
2254         " different than expected: %" GST_TIME_FORMAT, buffer,
2255         GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
2256         GST_TIME_ARGS (GST_BUFFER_DURATION (wanted_buf)));
2257     ret = FALSE;
2258   }
2259
2260   if (GST_BUFFER_FLAG_IS_SET (wanted_buf, GST_BUFFER_FLAG_DELTA_UNIT) !=
2261       GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
2262     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2263         "buffer %" GST_PTR_FORMAT "  Delta unit is set to %s but expected %s",
2264         buffer, GST_BUFFER_FLAG_IS_SET (buffer,
2265             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False",
2266         GST_BUFFER_FLAG_IS_SET (wanted_buf,
2267             GST_BUFFER_FLAG_DELTA_UNIT) ? "True" : "False");
2268     ret = FALSE;
2269   }
2270
2271   g_assert (gst_buffer_map (wanted_buf, &wanted_map, GST_MAP_READ));
2272   g_assert (gst_buffer_map (buffer, &map, GST_MAP_READ));
2273
2274   checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5,
2275       (const guchar *) map.data, map.size);
2276
2277   if (g_strcmp0 ((gchar *) wanted_map.data, checksum)) {
2278     GST_VALIDATE_REPORT (pad_monitor, WRONG_BUFFER,
2279         "buffer %" GST_PTR_FORMAT " checksum %s different from expected: %s",
2280         buffer, checksum, wanted_map.data);
2281     ret = FALSE;
2282   }
2283
2284   gst_buffer_unmap (wanted_buf, &wanted_map);
2285   gst_buffer_unmap (buffer, &map);
2286   g_free (checksum);
2287   gst_object_unref (pad);
2288
2289   pad_monitor->current_buf = pad_monitor->current_buf->next;
2290
2291   return ret;
2292 }
2293
2294 static void
2295 gst_validate_pad_monitor_check_return (GstValidatePadMonitor * pad_monitor,
2296     GstFlowReturn ret)
2297 {
2298   GstValidateMonitor *parent = GST_VALIDATE_MONITOR (pad_monitor);
2299
2300   if (ret != GST_FLOW_ERROR)
2301     return;
2302
2303   while (GST_VALIDATE_MONITOR_GET_PARENT (parent))
2304     parent = GST_VALIDATE_MONITOR_GET_PARENT (parent);
2305
2306   if (GST_IS_VALIDATE_PIPELINE_MONITOR (parent)) {
2307     GstValidatePipelineMonitor *m = GST_VALIDATE_PIPELINE_MONITOR (parent);
2308
2309     GST_VALIDATE_MONITOR_LOCK (m);
2310     if (m->got_error == FALSE) {
2311       GST_VALIDATE_REPORT (pad_monitor, FLOW_ERROR_WITHOUT_ERROR_MESSAGE,
2312           "Pad return GST_FLOW_ERROR but no GST_MESSAGE_ERROR was received on"
2313           " the bus");
2314
2315       /* Only report it the first time */
2316       m->got_error = TRUE;
2317     }
2318     GST_VALIDATE_MONITOR_UNLOCK (m);
2319   }
2320 }
2321
2322 static GstFlowReturn
2323 gst_validate_pad_monitor_chain_func (GstPad * pad, GstObject * parent,
2324     GstBuffer * buffer)
2325 {
2326   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2327   GstFlowReturn ret;
2328
2329   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2330   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2331
2332   gst_validate_pad_monitor_check_discont (pad_monitor, buffer);
2333   gst_validate_pad_monitor_check_right_buffer (pad_monitor, buffer);
2334   gst_validate_pad_monitor_check_first_buffer (pad_monitor, buffer);
2335   gst_validate_pad_monitor_update_buffer_data (pad_monitor, buffer);
2336   gst_validate_pad_monitor_check_eos (pad_monitor, buffer);
2337
2338   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2339   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2340
2341   gst_validate_pad_monitor_buffer_overrides (pad_monitor, buffer);
2342
2343   ret = pad_monitor->chain_func (pad, parent, buffer);
2344
2345   gst_validate_pad_monitor_check_return (pad_monitor, ret);
2346
2347   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2348   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2349
2350   if (ret == GST_FLOW_EOS) {
2351     mark_pads_eos (pad_monitor);
2352   }
2353   if (PAD_PARENT_IS_DEMUXER (pad_monitor))
2354     gst_validate_pad_monitor_check_aggregated_return (pad_monitor, parent, ret);
2355
2356   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2357   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2358
2359   return ret;
2360 }
2361
2362 static gboolean
2363 gst_validate_pad_monitor_event_is_tracked (GstValidatePadMonitor * monitor,
2364     GstEvent * event)
2365 {
2366   if (!GST_EVENT_IS_SERIALIZED (event)) {
2367     return FALSE;
2368   }
2369
2370   /* we don't track Tag events because they mutate too much and it is hard
2371    * to match a tag event pushed on a source pad with the one that was received
2372    * on a sink pad.
2373    * One idea would be to use seqnum, but it seems that it is undefined whether
2374    * seqnums should be maintained in tag events that are created from others
2375    * up to today. (2013-08-29)
2376    */
2377   if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
2378     return FALSE;
2379
2380   return TRUE;
2381 }
2382
2383 static GstFlowReturn
2384 gst_validate_pad_monitor_sink_event_full_func (GstPad * pad, GstObject * parent,
2385     GstEvent * event)
2386 {
2387   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2388   GstFlowReturn ret;
2389
2390   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (pad_monitor);
2391   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2392
2393   GST_DEBUG_OBJECT (pad, "event %p %s", event, GST_EVENT_TYPE_NAME (event));
2394
2395   if (gst_validate_pad_monitor_event_is_tracked (pad_monitor, event)) {
2396     GstClockTime last_ts = GST_CLOCK_TIME_NONE;
2397     if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_timestamp)) {
2398       last_ts = pad_monitor->current_timestamp;
2399       if (GST_CLOCK_TIME_IS_VALID (pad_monitor->current_duration)) {
2400         last_ts += pad_monitor->current_duration;
2401       }
2402     }
2403     gst_validate_pad_monitor_otherpad_add_pending_serialized_event (pad_monitor,
2404         event, last_ts);
2405   }
2406
2407   ret =
2408       gst_validate_pad_monitor_downstream_event_check (pad_monitor, parent,
2409       event, pad_monitor->event_func);
2410
2411   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2412   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (pad_monitor);
2413   return ret;
2414 }
2415
2416 static gboolean
2417 gst_validate_pad_monitor_sink_event_func (GstPad * pad, GstObject * parent,
2418     GstEvent * event)
2419 {
2420   if (gst_validate_pad_monitor_sink_event_full_func (pad, parent,
2421           event) == GST_FLOW_OK)
2422     return TRUE;
2423   return FALSE;
2424 }
2425
2426 static gboolean
2427 gst_validate_pad_monitor_src_event_func (GstPad * pad, GstObject * parent,
2428     GstEvent * event)
2429 {
2430   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2431   gboolean ret;
2432
2433   GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2434   ret = gst_validate_pad_monitor_src_event_check (pad_monitor, parent, event,
2435       pad_monitor->event_func);
2436   GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2437   return ret;
2438 }
2439
2440 static gboolean
2441 gst_validate_pad_monitor_query_func (GstPad * pad, GstObject * parent,
2442     GstQuery * query)
2443 {
2444   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2445   gboolean ret;
2446
2447   gst_validate_pad_monitor_query_overrides (pad_monitor, query);
2448   ret = pad_monitor->query_func (pad, parent, query);
2449
2450   if (ret) {
2451     switch (GST_QUERY_TYPE (query)) {
2452       case GST_QUERY_ACCEPT_CAPS:
2453       {
2454         gboolean result;
2455
2456         gst_caps_replace (&pad_monitor->last_refused_caps, NULL);
2457         gst_query_parse_accept_caps_result (query, &result);
2458         if (!result) {
2459           GstCaps *refused_caps;
2460
2461           gst_query_parse_accept_caps (query, &refused_caps);
2462           pad_monitor->last_refused_caps = gst_caps_copy (refused_caps);
2463
2464         }
2465
2466         break;
2467       }
2468       case GST_QUERY_CAPS:{
2469         GstCaps *res;
2470         GstCaps *filter;
2471
2472         /* We shouldn't need to lock the parent as this doesn't modify
2473          * other monitors, just does some peer_pad_caps */
2474         GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2475
2476         gst_query_parse_caps (query, &filter);
2477         gst_query_parse_caps_result (query, &res);
2478
2479         gst_caps_replace (&pad_monitor->last_query_res, NULL);
2480         gst_caps_replace (&pad_monitor->last_query_filter, NULL);
2481         pad_monitor->last_query_res =
2482             res ? gst_caps_copy (res) : gst_caps_ref (GST_CAPS_NONE);
2483         pad_monitor->last_query_filter =
2484             filter ? gst_caps_copy (filter) : gst_caps_ref (GST_CAPS_NONE);
2485
2486         if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
2487           gst_validate_pad_monitor_check_caps_fields_proxied (pad_monitor, res,
2488               filter);
2489         }
2490         GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2491         break;
2492       }
2493       default:
2494         break;
2495     }
2496   }
2497
2498   return ret;
2499 }
2500
2501 static gboolean
2502 gst_validate_pad_monitor_activatemode_func (GstPad * pad, GstObject * parent,
2503     GstPadMode mode, gboolean active)
2504 {
2505   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2506   gboolean ret = TRUE;
2507
2508   /* TODO add overrides for activate func */
2509   GST_DEBUG_OBJECT (pad, "active:%d", active);
2510
2511   if (pad_monitor->activatemode_func)
2512     ret = pad_monitor->activatemode_func (pad, parent, mode, active);
2513   if (ret && active == FALSE) {
2514     GST_VALIDATE_MONITOR_LOCK (pad_monitor);
2515     gst_validate_pad_monitor_reset (pad_monitor);
2516     GST_VALIDATE_MONITOR_UNLOCK (pad_monitor);
2517   }
2518
2519   return ret;
2520 }
2521
2522 static GstFlowReturn
2523 gst_validate_pad_monitor_get_range_func (GstPad * pad, GstObject * parent,
2524     guint64 offset, guint length, GstBuffer ** buffer)
2525 {
2526   GstValidatePadMonitor *pad_monitor = _GET_PAD_MONITOR (pad);
2527
2528   if (pad_monitor->get_range_func) {
2529     GstPad *peer = gst_pad_get_peer (pad);
2530     GstTask *task = NULL;
2531     GThread *thread = NULL;
2532
2533     if (peer) {
2534       GST_OBJECT_LOCK (peer);
2535       task = GST_PAD_TASK (peer);
2536       if (task && GST_TASK_STATE (task) == GST_TASK_STARTED) {
2537         GST_OBJECT_LOCK (task);
2538         /* Only doing pointer comparison, no need to hold a ref */
2539         thread = task->thread;
2540         GST_OBJECT_UNLOCK (task);
2541       }
2542       GST_OBJECT_UNLOCK (peer);
2543
2544       if (thread && thread != g_thread_self ()) {
2545         GST_VALIDATE_REPORT (pad_monitor, PULL_RANGE_FROM_WRONG_THREAD,
2546             "Pulling from wrong thread, expected pad thread: %p, got %p",
2547             task->thread, g_thread_self ());
2548       }
2549
2550       gst_object_unref (peer);
2551     }
2552
2553     return pad_monitor->get_range_func (pad, parent, offset, length, buffer);
2554   }
2555
2556   return GST_FLOW_NOT_SUPPORTED;
2557
2558 }
2559
2560 /* The interval between two buffer frequency checks */
2561 #define BUF_FREQ_CHECK_INTERVAL (GST_SECOND)
2562
2563 static void
2564 gst_validate_pad_monitor_check_buffer_freq (GstValidatePadMonitor * monitor,
2565     GstPad * pad)
2566 {
2567   GstClockTime ts;
2568
2569   if (!GST_PAD_IS_SRC (pad))
2570     return;
2571
2572   if (!monitor->min_buf_freq)
2573     return;
2574
2575   ts = gst_util_get_timestamp ();
2576   monitor->buffers_pushed++;
2577
2578   /* Same logic as in fpsdisplaysink to compute the buffer frequency */
2579   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID
2580           (monitor->min_buf_freq_first_buffer_ts))) {
2581     monitor->min_buf_freq_first_buffer_ts = ts;
2582     monitor->min_buf_freq_interval_ts = ts;
2583     return;
2584   }
2585
2586   if (GST_CLOCK_DIFF (monitor->min_buf_freq_interval_ts,
2587           ts) > BUF_FREQ_CHECK_INTERVAL) {
2588     guint time_diff;
2589     gdouble fps;
2590
2591     time_diff = (gdouble) (ts - monitor->min_buf_freq_interval_ts) / GST_SECOND;
2592     fps =
2593         (gdouble) (monitor->buffers_pushed -
2594         monitor->last_buffers_pushed) / time_diff;
2595
2596     if (fps < monitor->min_buf_freq) {
2597       if (GST_CLOCK_TIME_IS_VALID (monitor->min_buf_freq_start) &&
2598           GST_CLOCK_DIFF (monitor->min_buf_freq_first_buffer_ts,
2599               ts) < monitor->min_buf_freq_start) {
2600         GST_DEBUG_OBJECT (pad,
2601             "buffer frequency is too low (%.2f) but ignore for now (buffer-frequency-start =%"
2602             GST_TIME_FORMAT ")", fps,
2603             GST_TIME_ARGS (monitor->min_buf_freq_start));
2604       } else {
2605         GST_VALIDATE_REPORT (monitor, CONFIG_BUFFER_FREQUENCY_TOO_LOW,
2606             "Buffers are not pushed fast enough on this pad: %.2f/sec (minimum: %.2f)",
2607             fps, monitor->min_buf_freq);
2608       }
2609     }
2610
2611     monitor->last_buffers_pushed = monitor->buffers_pushed;
2612     monitor->min_buf_freq_interval_ts = ts;
2613   }
2614 }
2615
2616 static gboolean
2617 gst_validate_pad_monitor_buffer_probe (GstPad * pad, GstBuffer * buffer,
2618     gpointer udata, gboolean pull_mode)
2619 {
2620   GstValidatePadMonitor *monitor = udata;
2621
2622   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2623   GST_VALIDATE_MONITOR_LOCK (monitor);
2624
2625   if (!pull_mode)
2626     gst_validate_pad_monitor_check_discont (monitor, buffer);
2627   gst_validate_pad_monitor_check_first_buffer (monitor, buffer);
2628   gst_validate_pad_monitor_update_buffer_data (monitor, buffer);
2629   gst_validate_pad_monitor_check_eos (monitor, buffer);
2630
2631   if (PAD_PARENT_IS_DECODER (monitor) || PAD_PARENT_IS_ENCODER (monitor)) {
2632     GstClockTime tolerance = 0;
2633
2634     if (monitor->caps_is_audio)
2635       tolerance = AUDIO_TIMESTAMP_TOLERANCE;
2636
2637     gst_validate_pad_monitor_check_buffer_timestamp_in_received_range (monitor,
2638         buffer, tolerance);
2639   }
2640
2641   gst_validate_pad_monitor_check_late_serialized_events (monitor,
2642       GST_BUFFER_TIMESTAMP (buffer));
2643
2644   /* a GstValidatePadMonitor parent must be a GstValidateElementMonitor */
2645   if (PAD_PARENT_IS_DECODER (monitor)) {
2646
2647     /* should not push out of segment data */
2648     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer)) &&
2649         GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer)) &&
2650         ((!gst_segment_clip (&monitor->segment, monitor->segment.format,
2651                     GST_BUFFER_TIMESTAMP (buffer),
2652                     GST_BUFFER_TIMESTAMP (buffer) +
2653                     GST_BUFFER_DURATION (buffer), NULL, NULL)) ||
2654             /* In the case of raw data, buffers should be strictly contained inside the
2655              * segment */
2656             (monitor->caps_is_raw &&
2657                 GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer) <
2658                 monitor->segment.start))
2659         ) {
2660       /* TODO is this a timestamp issue? */
2661       GST_VALIDATE_REPORT (monitor, BUFFER_IS_OUT_OF_SEGMENT,
2662           "buffer is out of segment and shouldn't be pushed. Timestamp: %"
2663           GST_TIME_FORMAT " - Duration: %" GST_TIME_FORMAT ". Range: %"
2664           GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
2665           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2666           GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
2667           GST_TIME_ARGS (monitor->segment.start),
2668           GST_TIME_ARGS (monitor->segment.stop));
2669     }
2670   }
2671
2672   gst_validate_pad_monitor_check_buffer_freq (monitor, pad);
2673
2674   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2675   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2676   gst_validate_pad_monitor_buffer_probe_overrides (monitor, buffer);
2677   return TRUE;
2678 }
2679
2680 static void
2681 gst_validate_pad_monitor_event_probe (GstPad * pad, GstEvent * event,
2682     gpointer udata)
2683 {
2684   GstValidatePadMonitor *monitor = GST_VALIDATE_PAD_MONITOR_CAST (udata);
2685   guint32 seqnum = gst_event_get_seqnum (event);
2686
2687   GST_VALIDATE_PAD_MONITOR_PARENT_LOCK (monitor);
2688   GST_VALIDATE_MONITOR_LOCK (monitor);
2689
2690   GST_DEBUG_OBJECT (pad, "event %p %s seqnum:%" G_GUINT32_FORMAT, event,
2691       GST_EVENT_TYPE_NAME (event), seqnum);
2692
2693   if (GST_EVENT_IS_SERIALIZED (event)) {
2694     gint i;
2695
2696     /* Detect if events the element received are being forwarded in the same order
2697      *
2698      * Several scenarios:
2699      * 1) The element pushes the event as-is
2700      * 2) The element consumes the event and does not forward it
2701      * 3) The element consumes the event and creates another one instead
2702      * 4) The element pushes other serialized event before pushing out the
2703      *    one it received
2704      *
2705      * For each pad we have two lists to track serialized events:
2706      *  1) We received on input and expect to see (serialized_events)
2707      *  2) We received on input but don't expect to see (expired_events)
2708      *
2709      * To detect events that are pushed in a different order from the one they were
2710      * received in we check that:
2711      *
2712      * For each event being outputted:
2713      *   If it is in the expired_events list:
2714      *     RAISE WARNING
2715      *   If it is in the serialized_events list:
2716      *     If there are other events that were received before:
2717      *        Put those events on the expired_events list
2718      *     Remove that event and any previous ones from the serialized_events list
2719      *
2720      * Clear expired events list when flushing or on pad deactivation
2721      *
2722      */
2723
2724     if (g_list_find (monitor->expired_events, event)) {
2725       gchar *event_str = _get_event_string (event);
2726       /* If it's the expired events, we've failed */
2727       GST_WARNING_OBJECT (pad, "Did not expect event %p %s", event,
2728           GST_EVENT_TYPE_NAME (event));
2729       GST_VALIDATE_REPORT (monitor, EVENT_SERIALIZED_OUT_OF_ORDER,
2730           "Serialized event was pushed out of order: %s", event_str);
2731
2732       g_free (event_str);
2733       monitor->expired_events = g_list_remove (monitor->expired_events, event);
2734       gst_event_unref (event);  /* remove the ref that was on the list */
2735     } else if (monitor->serialized_events->len) {
2736       for (i = 0; i < monitor->serialized_events->len; i++) {
2737         SerializedEventData *next_event =
2738             g_ptr_array_index (monitor->serialized_events, i);
2739         GST_DEBUG_OBJECT (pad, "Checking against stored event #%d: %p %s", i,
2740             next_event->event, GST_EVENT_TYPE_NAME (next_event->event));
2741
2742         if (event == next_event->event
2743             || GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2744           /* We have found our event */
2745           GST_DEBUG_OBJECT (pad, "Found matching event");
2746
2747           while (monitor->serialized_events->len > i
2748               && GST_EVENT_TYPE (event) == GST_EVENT_TYPE (next_event->event)) {
2749             /* Swallow all expected events of the same type */
2750             g_ptr_array_remove_index (monitor->serialized_events, i);
2751             next_event = g_ptr_array_index (monitor->serialized_events, i);
2752           }
2753
2754           /* Move all previous events to expired events */
2755           if (G_UNLIKELY (i > 0)) {
2756             GST_DEBUG_OBJECT (pad,
2757                 "Moving previous expected events to expired list");
2758             while (i--) {
2759               next_event = g_ptr_array_index (monitor->serialized_events, 0);
2760               monitor->expired_events =
2761                   g_list_append (monitor->expired_events,
2762                   gst_event_ref (next_event->event));
2763               g_ptr_array_remove_index (monitor->serialized_events, 0);
2764             }
2765           }
2766           debug_pending_event (pad, monitor->serialized_events);
2767           break;
2768         }
2769       }
2770     }
2771   }
2772
2773   /* This so far is just like an event that is flowing downstream,
2774    * so we do the same checks as a sinkpad event handler */
2775   gst_validate_pad_monitor_downstream_event_check (monitor, NULL, event, NULL);
2776   GST_VALIDATE_MONITOR_UNLOCK (monitor);
2777   GST_VALIDATE_PAD_MONITOR_PARENT_UNLOCK (monitor);
2778 }
2779
2780 static GstPadProbeReturn
2781 gst_validate_pad_monitor_pad_probe (GstPad * pad, GstPadProbeInfo * info,
2782     gpointer udata)
2783 {
2784   if (info->type & GST_PAD_PROBE_TYPE_BUFFER)
2785     gst_validate_pad_monitor_buffer_probe (pad, info->data, udata,
2786         GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_PULL);
2787   else if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM)
2788     gst_validate_pad_monitor_event_probe (pad, info->data, udata);
2789
2790   return GST_PAD_PROBE_OK;
2791 }
2792
2793 static void
2794 gst_validate_pad_monitor_update_caps_info (GstValidatePadMonitor * pad_monitor,
2795     GstCaps * caps)
2796 {
2797   GstStructure *structure;
2798
2799   g_return_if_fail (gst_caps_is_fixed (caps));
2800
2801   pad_monitor->caps_is_audio = FALSE;
2802   pad_monitor->caps_is_video = FALSE;
2803
2804   structure = gst_caps_get_structure (caps, 0);
2805   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/")) {
2806     pad_monitor->caps_is_audio = TRUE;
2807   } else if (g_str_has_prefix (gst_structure_get_name (structure), "video/")) {
2808     pad_monitor->caps_is_video = TRUE;
2809   }
2810
2811   if (g_str_has_prefix (gst_structure_get_name (structure), "audio/x-raw") ||
2812       g_str_has_prefix (gst_structure_get_name (structure), "video/x-raw")) {
2813     pad_monitor->caps_is_raw = TRUE;
2814   } else {
2815     pad_monitor->caps_is_raw = FALSE;
2816   }
2817 }
2818
2819 static void
2820 gst_validate_pad_monitor_setcaps_pre (GstValidatePadMonitor * pad_monitor,
2821     GstCaps * caps)
2822 {
2823   GstStructure *structure;
2824   GstPad *pad =
2825       GST_PAD (gst_validate_monitor_get_target (GST_VALIDATE_MONITOR
2826           (pad_monitor)));
2827
2828   /* Check if caps are identical to last caps and complain if so
2829    * Only checked for sink pads as src pads might push the same caps
2830    * multiple times during unlinked/autoplugging scenarios */
2831   if (GST_PAD_IS_SINK (pad) && pad_monitor->last_caps
2832       && gst_caps_is_equal (caps, pad_monitor->last_caps)) {
2833     gchar *caps_str = gst_caps_to_string (caps);
2834
2835     GST_VALIDATE_REPORT (pad_monitor, EVENT_CAPS_DUPLICATE, "%s", caps_str);
2836     g_free (caps_str);
2837
2838   }
2839
2840   gst_validate_pad_monitor_check_caps_complete (pad_monitor, caps);
2841
2842   if (caps) {
2843     structure = gst_caps_get_structure (caps, 0);
2844     if (gst_structure_n_fields (pad_monitor->pending_setcaps_fields)) {
2845       gint i;
2846       for (i = 0;
2847           i < gst_structure_n_fields (pad_monitor->pending_setcaps_fields);
2848           i++) {
2849         const gchar *name =
2850             gst_structure_nth_field_name (pad_monitor->pending_setcaps_fields,
2851             i);
2852         const GValue *v = gst_structure_get_value (structure, name);
2853         const GValue *otherv =
2854             gst_structure_get_value (pad_monitor->pending_setcaps_fields, name);
2855
2856         if (v == NULL) {
2857           gchar *caps_str = gst_caps_to_string (caps);
2858
2859           GST_VALIDATE_REPORT (pad_monitor, CAPS_EXPECTED_FIELD_NOT_FOUND,
2860               "Field %s is missing from setcaps caps '%s'", name, caps_str);
2861           g_free (caps_str);
2862         } else if (gst_value_compare (v, otherv) != GST_VALUE_EQUAL) {
2863           gchar *caps_str = gst_caps_to_string (caps),
2864               *pending_setcaps_fields_str =
2865               gst_structure_to_string (pad_monitor->pending_setcaps_fields);
2866
2867
2868           GST_VALIDATE_REPORT (pad_monitor, CAPS_FIELD_UNEXPECTED_VALUE,
2869               "Field %s from setcaps caps '%s' is different "
2870               "from expected value in caps '%s'", name, caps_str,
2871               pending_setcaps_fields_str);
2872
2873           g_free (pending_setcaps_fields_str);
2874           g_free (caps_str);
2875         }
2876       }
2877     }
2878
2879     if (GST_PAD_IS_SINK (pad) &&
2880         gst_validate_pad_monitor_pad_should_proxy_othercaps (pad_monitor)) {
2881       if (_structure_is_video (structure)) {
2882         GST_DEBUG_OBJECT (pad,
2883             "Adding video common pending fields to other pad: %" GST_PTR_FORMAT,
2884             structure);
2885         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2886             structure, "width");
2887         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2888             structure, "height");
2889         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2890             structure, "framerate");
2891         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2892             structure, "pixel-aspect-ratio");
2893       } else if (_structure_is_audio (structure)) {
2894         GST_DEBUG_OBJECT (pad,
2895             "Adding audio common pending fields to other pad: %" GST_PTR_FORMAT,
2896             structure);
2897         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2898             structure, "rate");
2899         gst_validate_pad_monitor_otherpad_add_pending_field (pad_monitor,
2900             structure, "channels");
2901       }
2902     }
2903   }
2904
2905   gst_structure_free (pad_monitor->pending_setcaps_fields);
2906   pad_monitor->pending_setcaps_fields =
2907       gst_structure_new_empty (PENDING_FIELDS);
2908   gst_object_unref (pad);
2909
2910   gst_validate_pad_monitor_setcaps_overrides (pad_monitor, caps);
2911 }
2912
2913 static void
2914 gst_validate_pad_monitor_setcaps_post (GstValidatePadMonitor * pad_monitor,
2915     GstCaps * caps, gboolean ret)
2916 {
2917   if (!ret)
2918     gst_validate_pad_monitor_otherpad_clear_pending_fields (pad_monitor);
2919   else {
2920     if (pad_monitor->last_caps) {
2921       gst_caps_unref (pad_monitor->last_caps);
2922     }
2923     pad_monitor->last_caps = gst_caps_ref (caps);
2924     gst_validate_pad_monitor_update_caps_info (pad_monitor, caps);
2925   }
2926 }
2927
2928 static void
2929 gst_validate_pad_monitor_get_min_buffer_frequency (GstValidatePadMonitor *
2930     monitor, GstPad * pad)
2931 {
2932   GList *config, *l;
2933
2934   if (!GST_PAD_IS_SRC (pad))
2935     return;
2936
2937   config = gst_validate_plugin_get_config (NULL);
2938   for (l = config; l != NULL; l = g_list_next (l)) {
2939     GstStructure *s = l->data;
2940     gdouble min_buf_freq;
2941     const gchar *pad_name;
2942     GstElement *element = NULL;
2943
2944     if (!gst_structure_get_double (s, "min-buffer-frequency", &min_buf_freq)) {
2945       gint max_int;
2946
2947       if (!gst_structure_get_int (s, "min-buffer-frequency", &max_int))
2948         goto next;
2949
2950       min_buf_freq = max_int;
2951     }
2952
2953     pad_name = gst_structure_get_string (s, "name");
2954     if (!pad_name)
2955       pad_name = "src";
2956
2957     if (g_strcmp0 (GST_PAD_NAME (pad), pad_name))
2958       goto next;
2959
2960     element = gst_pad_get_parent_element (pad);
2961
2962     if (!gst_validate_element_matches_target (element, s))
2963       goto next;
2964
2965     monitor->min_buf_freq = min_buf_freq;
2966
2967     gst_validate_utils_get_clocktime (s, "buffer-frequency-start",
2968         &monitor->min_buf_freq_start);
2969
2970     GST_DEBUG_OBJECT (pad, "pad has a minimum buffer frequency of %f",
2971         min_buf_freq);
2972   next:
2973     g_clear_object (&element);
2974   }
2975 }
2976
2977 static gboolean
2978 gst_validate_pad_monitor_do_setup (GstValidateMonitor * monitor)
2979 {
2980   GstValidatePadMonitor *pad_monitor = GST_VALIDATE_PAD_MONITOR_CAST (monitor);
2981   GstPad *pad = (gpointer) gst_validate_monitor_get_target (monitor);
2982
2983   if (!GST_IS_PAD (pad)) {
2984     GST_WARNING_OBJECT (monitor, "Trying to create pad monitor with other "
2985         "type of object");
2986     gst_object_unref (pad);
2987     return FALSE;
2988   }
2989
2990   if (_GET_PAD_MONITOR (pad)) {
2991     GST_WARNING_OBJECT (pad_monitor,
2992         "Pad already has a validate-monitor associated");
2993     gst_object_unref (pad);
2994     return FALSE;
2995   }
2996
2997   _SET_PAD_MONITOR (pad, pad_monitor);
2998
2999   pad_monitor->event_func = GST_PAD_EVENTFUNC (pad);
3000   pad_monitor->event_full_func = GST_PAD_EVENTFULLFUNC (pad);
3001   pad_monitor->query_func = GST_PAD_QUERYFUNC (pad);
3002   pad_monitor->activatemode_func = GST_PAD_ACTIVATEMODEFUNC (pad);
3003   pad_monitor->get_range_func = GST_PAD_GETRANGEFUNC (pad);
3004   if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
3005
3006     pad_monitor->chain_func = GST_PAD_CHAINFUNC (pad);
3007     if (pad_monitor->chain_func)
3008       gst_pad_set_chain_function (pad, gst_validate_pad_monitor_chain_func);
3009
3010     if (pad_monitor->event_full_func)
3011       gst_pad_set_event_full_function (pad,
3012           gst_validate_pad_monitor_sink_event_full_func);
3013     else
3014       gst_pad_set_event_function (pad,
3015           gst_validate_pad_monitor_sink_event_func);
3016   } else {
3017     gst_pad_set_event_function (pad, gst_validate_pad_monitor_src_event_func);
3018
3019     /* add buffer/event probes */
3020     pad_monitor->pad_probe_id =
3021         gst_pad_add_probe (pad,
3022         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
3023         GST_PAD_PROBE_TYPE_EVENT_FLUSH,
3024         (GstPadProbeCallback) gst_validate_pad_monitor_pad_probe, pad_monitor,
3025         NULL);
3026   }
3027   gst_pad_set_query_function (pad, gst_validate_pad_monitor_query_func);
3028   gst_pad_set_activatemode_function (pad,
3029       gst_validate_pad_monitor_activatemode_func);
3030
3031   if (GST_PAD_IS_SRC (pad)) {
3032     gst_pad_set_getrange_function (pad,
3033         gst_validate_pad_monitor_get_range_func);
3034   }
3035
3036   gst_validate_reporter_set_name (GST_VALIDATE_REPORTER (monitor),
3037       g_strdup_printf ("%s:%s", GST_DEBUG_PAD_NAME (pad)));
3038
3039   if (G_UNLIKELY (GST_PAD_PARENT (pad) == NULL))
3040     GST_FIXME ("Saw a pad not belonging to any object");
3041
3042   gst_validate_pad_monitor_get_min_buffer_frequency (pad_monitor, pad);
3043
3044   gst_object_unref (pad);
3045   return TRUE;
3046 }