Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / libs / gst / check / gstconsistencychecker.c
1 /* GStreamer
2  *
3  * unit testing helper lib
4  *
5  * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:gstcheckconsistencychecker
25  * @short_description: Data flow consistency checker for GStreamer unit tests.
26  *
27  * These macros and functions are for internal use of the unit tests found
28  * inside the 'check' directories of various GStreamer packages.
29  *
30  * Since: 0.10.24
31  */
32
33 #include "gstconsistencychecker.h"
34
35 struct _GstStreamConsistency
36 {
37   gboolean flushing;
38   gboolean segment;
39   gboolean eos;
40   gulong probeid;
41   GstPad *pad;
42 };
43
44 static gboolean
45 source_pad_data_cb (GstPad * pad, GstPadProbeInfo * info,
46     GstStreamConsistency * consist)
47 {
48   GstMiniObject *data = GST_PAD_PROBE_INFO_DATA (info);
49
50   if (GST_IS_BUFFER (data)) {
51     GST_DEBUG_OBJECT (pad,
52         "Buffer pts %" GST_TIME_FORMAT ", dts %" GST_TIME_FORMAT,
53         GST_TIME_ARGS (GST_BUFFER_PTS (GST_BUFFER_CAST (data))),
54         GST_TIME_ARGS (GST_BUFFER_DTS (GST_BUFFER_CAST (data))));
55     /* If an EOS went through, a buffer would be invalid */
56     fail_if (consist->eos, "Buffer received after EOS");
57     /* Buffers need to be preceded by a segment event */
58     fail_unless (consist->segment, "Buffer received without segment");
59   } else if (GST_IS_EVENT (data)) {
60     GstEvent *event = (GstEvent *) data;
61
62     GST_DEBUG_OBJECT (pad, "%s", GST_EVENT_TYPE_NAME (event));
63     switch (GST_EVENT_TYPE (event)) {
64       case GST_EVENT_FLUSH_START:
65         consist->flushing = TRUE;
66         break;
67       case GST_EVENT_FLUSH_STOP:
68         /* Receiving a flush-stop is only valid after receiving a flush-start */
69         fail_unless (consist->flushing,
70             "Received a FLUSH_STOP without a FLUSH_START");
71         fail_if (consist->eos, "Received a FLUSH_STOP after an EOS");
72         consist->flushing = FALSE;
73         break;
74       case GST_EVENT_SEGMENT:
75         consist->segment = TRUE;
76         consist->eos = FALSE;
77         break;
78       case GST_EVENT_EOS:
79         /* FIXME : not 100% sure about whether two eos in a row is valid */
80         fail_if (consist->eos, "Received EOS just after another EOS");
81         consist->eos = TRUE;
82         consist->segment = FALSE;
83         break;
84       case GST_EVENT_TAG:
85         GST_DEBUG_OBJECT (pad, "tag %" GST_PTR_FORMAT,
86             gst_event_get_structure (event));
87         /* fall through */
88       default:
89         if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_IS_DOWNSTREAM (event)) {
90           fail_if (consist->eos, "Event received after EOS");
91           fail_unless (consist->segment, "Event received before segment");
92         }
93         /* FIXME : Figure out what to do for other events */
94         break;
95     }
96   }
97
98   return TRUE;
99 }
100
101 /**
102  * gst_consistency_checker_new:
103  * @pad: The #GstPad on which the dataflow will be checked.
104  *
105  * Sets up a data probe on the given pad which will raise assertions if the
106  * data flow is inconsistent.
107  *
108  * Currently only works for source pads.
109  *
110  * Returns: A #GstStreamConsistency structure used to track data flow.
111  *
112  * Since: 0.10.24
113  */
114
115 GstStreamConsistency *
116 gst_consistency_checker_new (GstPad * pad)
117 {
118   GstStreamConsistency *consist;
119
120   g_return_val_if_fail (pad != NULL, NULL);
121
122   consist = g_new0 (GstStreamConsistency, 1);
123   consist->pad = g_object_ref (pad);
124   consist->probeid =
125       gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
126       (GstPadProbeCallback) source_pad_data_cb, consist, NULL);
127
128   return consist;
129 }
130
131 /**
132  * gst_consistency_checker_reset:
133  * @consist: The #GstStreamConsistency to reset.
134  *
135  * Reset the stream checker's internal variables.
136  *
137  * Since: 0.10.24
138  */
139
140 void
141 gst_consistency_checker_reset (GstStreamConsistency * consist)
142 {
143   consist->eos = FALSE;
144   consist->flushing = FALSE;
145   consist->segment = FALSE;
146 }
147
148 /**
149  * gst_consistency_checker_free:
150  * @consist: The #GstStreamConsistency to free.
151  *
152  * Frees the allocated data and probe associated with @consist.
153  *
154  * Since: 0.10.24
155  */
156
157 void
158 gst_consistency_checker_free (GstStreamConsistency * consist)
159 {
160   /* Remove the data probe */
161   gst_pad_remove_probe (consist->pad, consist->probeid);
162   g_object_unref (consist->pad);
163   g_free (consist);
164 }