• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * unit testing helper lib
4  *
5  * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
6  * Copyright (C) 2012 Stefan Sauer <ensonic@users.sf.net>
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 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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 /**
25  * SECTION:gstcheckconsistencychecker
26  * @title: GstStreamConsistencyChecker
27  * @short_description: Data flow consistency checker for GStreamer unit tests.
28  *
29  * These macros and functions are for internal use of the unit tests found
30  * inside the 'check' directories of various GStreamer packages.
31  */
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include "gstconsistencychecker.h"
37 
38 struct _GstStreamConsistency
39 {
40   /* FIXME: do we want to track some states per pad? */
41   gboolean flushing;
42   gboolean segment;
43   gboolean eos;
44   gboolean expect_flush;
45   gboolean saw_serialized_event;
46   gboolean saw_stream_start;
47   GstObject *parent;
48   GList *pads;
49 };
50 
51 typedef struct _GstStreamConsistencyProbe
52 {
53   GstPad *pad;
54   gulong probeid;
55 } GstStreamConsistencyProbe;
56 
57 static gboolean
source_pad_data_cb(GstPad * pad,GstPadProbeInfo * info,GstStreamConsistency * consist)58 source_pad_data_cb (GstPad * pad, GstPadProbeInfo * info,
59     GstStreamConsistency * consist)
60 {
61   GstMiniObject *data = GST_PAD_PROBE_INFO_DATA (info);
62 
63   GST_DEBUG_OBJECT (pad, "%p: %d %d %d %d", consist, consist->flushing,
64       consist->segment, consist->eos, consist->expect_flush);
65 
66   if (GST_IS_BUFFER (data)) {
67     GST_DEBUG_OBJECT (pad,
68         "Buffer pts %" GST_TIME_FORMAT ", dts %" GST_TIME_FORMAT,
69         GST_TIME_ARGS (GST_BUFFER_PTS (GST_BUFFER_CAST (data))),
70         GST_TIME_ARGS (GST_BUFFER_DTS (GST_BUFFER_CAST (data))));
71     /* If an EOS went through, a buffer would be invalid */
72     fail_if (consist->eos, "Buffer received after EOS on pad %s:%s",
73         GST_DEBUG_PAD_NAME (pad));
74     /* Buffers need to be preceded by a segment event */
75     fail_unless (consist->segment, "Buffer received without segment "
76         "on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
77   } else if (GST_IS_EVENT (data)) {
78     GstEvent *event = (GstEvent *) data;
79 
80     GST_DEBUG_OBJECT (pad, "Event : %s", GST_EVENT_TYPE_NAME (event));
81     switch (GST_EVENT_TYPE (event)) {
82       case GST_EVENT_FLUSH_START:
83         /* getting two flush_start in a row seems to be okay
84            fail_if (consist->flushing, "Received another FLUSH_START");
85          */
86         consist->flushing = TRUE;
87         break;
88       case GST_EVENT_FLUSH_STOP:
89         /* Receiving a flush-stop is only valid after receiving a flush-start */
90         fail_unless (consist->flushing,
91             "Received a FLUSH_STOP without a FLUSH_START on pad %s:%s",
92             GST_DEBUG_PAD_NAME (pad));
93         fail_if (consist->eos, "Received a FLUSH_STOP after an EOS on "
94             "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
95         consist->flushing = consist->expect_flush = FALSE;
96         break;
97       case GST_EVENT_STREAM_START:
98         fail_if (consist->saw_serialized_event && !consist->saw_stream_start,
99             "Got a STREAM_START event after a serialized event on pad %s:%s",
100             GST_DEBUG_PAD_NAME (pad));
101         consist->saw_stream_start = TRUE;
102         break;
103       case GST_EVENT_CAPS:
104         /* ok to have these before segment event */
105         /* FIXME check order more precisely, if so spec'ed somehow ? */
106         break;
107       case GST_EVENT_SEGMENT:
108         fail_if ((consist->expect_flush && consist->flushing),
109             "Received SEGMENT while in a flushing seek on pad %s:%s",
110             GST_DEBUG_PAD_NAME (pad));
111         consist->segment = TRUE;
112         consist->eos = FALSE;
113         break;
114       case GST_EVENT_EOS:
115         /* FIXME : not 100% sure about whether two eos in a row is valid */
116         fail_if (consist->eos, "Received EOS just after another EOS on "
117             "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
118         consist->eos = TRUE;
119         consist->segment = FALSE;
120         break;
121       case GST_EVENT_TAG:
122         GST_DEBUG_OBJECT (pad, "tag %" GST_PTR_FORMAT,
123             gst_event_get_structure (event));
124         /* fall through */
125       default:
126         if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_IS_DOWNSTREAM (event)) {
127           fail_if (consist->eos, "Event received after EOS");
128           fail_unless (consist->segment, "Event %s received before segment "
129               "on pad %s:%s", GST_EVENT_TYPE_NAME (event),
130               GST_DEBUG_PAD_NAME (pad));
131         }
132         /* FIXME : Figure out what to do for other events */
133         break;
134     }
135     if (GST_EVENT_IS_SERIALIZED (event)) {
136       fail_if (!consist->saw_stream_start
137           && GST_EVENT_TYPE (event) != GST_EVENT_STREAM_START,
138           "Got a serialized event (%s) before a STREAM_START on pad %s:%s",
139           GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (pad));
140       consist->saw_serialized_event = TRUE;
141     }
142   }
143 
144   return TRUE;
145 }
146 
147 static gboolean
sink_pad_data_cb(GstPad * pad,GstPadProbeInfo * info,GstStreamConsistency * consist)148 sink_pad_data_cb (GstPad * pad, GstPadProbeInfo * info,
149     GstStreamConsistency * consist)
150 {
151   GstMiniObject *data = GST_PAD_PROBE_INFO_DATA (info);
152 
153   GST_DEBUG_OBJECT (pad, "%p: %d %d %d %d", consist, consist->flushing,
154       consist->segment, consist->eos, consist->expect_flush);
155 
156   if (GST_IS_BUFFER (data)) {
157     GST_DEBUG_OBJECT (pad, "Buffer %" GST_TIME_FORMAT,
158         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (GST_BUFFER (data))));
159     /* If an EOS went through, a buffer would be invalid */
160     fail_if (consist->eos, "Buffer received after EOS on pad %s:%s",
161         GST_DEBUG_PAD_NAME (pad));
162     /* Buffers need to be preceded by a segment event */
163     fail_unless (consist->segment, "Buffer received without segment "
164         "on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
165   } else if (GST_IS_EVENT (data)) {
166     GstEvent *event = (GstEvent *) data;
167 
168     GST_DEBUG_OBJECT (pad, "%s", GST_EVENT_TYPE_NAME (event));
169     switch (GST_EVENT_TYPE (event)) {
170       case GST_EVENT_SEEK:
171       {
172         GstSeekFlags flags;
173 
174         gst_event_parse_seek (event, NULL, NULL, &flags, NULL, NULL, NULL,
175             NULL);
176         consist->expect_flush =
177             ((flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH);
178         break;
179       }
180       case GST_EVENT_SEGMENT:
181         fail_if ((consist->expect_flush && consist->flushing),
182             "Received SEGMENT while in a flushing seek on pad %s:%s",
183             GST_DEBUG_PAD_NAME (pad));
184         consist->segment = TRUE;
185         consist->eos = FALSE;
186         break;
187       default:
188         /* FIXME : Figure out what to do for other events */
189         break;
190     }
191   }
192 
193   return TRUE;
194 }
195 
196 static void
add_pad(GstStreamConsistency * consist,GstPad * pad)197 add_pad (GstStreamConsistency * consist, GstPad * pad)
198 {
199   GstStreamConsistencyProbe *p;
200   GstPadDirection dir;
201 
202   p = g_new0 (GstStreamConsistencyProbe, 1);
203   p->pad = g_object_ref (pad);
204   dir = gst_pad_get_direction (pad);
205   if (dir == GST_PAD_SRC) {
206 
207     p->probeid =
208         gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
209         (GstPadProbeCallback) source_pad_data_cb, consist, NULL);
210 
211   } else if (dir == GST_PAD_SINK) {
212     p->probeid =
213         gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
214         (GstPadProbeCallback) sink_pad_data_cb, consist, NULL);
215   }
216   consist->pads = g_list_prepend (consist->pads, p);
217 }
218 
219 /**
220  * gst_consistency_checker_new: (skip)
221  * @pad: The #GstPad on which the dataflow will be checked.
222  *
223  * Sets up a data probe on the given pad which will raise assertions if the
224  * data flow is inconsistent.
225  *
226  * Returns: A #GstStreamConsistency structure used to track data flow.
227  */
228 GstStreamConsistency *
gst_consistency_checker_new(GstPad * pad)229 gst_consistency_checker_new (GstPad * pad)
230 {
231   GstStreamConsistency *consist;
232 
233   g_return_val_if_fail (pad != NULL, NULL);
234 
235   consist = g_new0 (GstStreamConsistency, 1);
236 
237   if (!consist->pads) {
238     consist->parent = GST_OBJECT_PARENT (pad);
239   }
240   add_pad (consist, pad);
241   return consist;
242 }
243 
244 /**
245  * gst_consistency_checker_add_pad:
246  * @consist: The #GstStreamConsistency handle
247  * @pad: The #GstPad on which the dataflow will be checked.
248  *
249  * Sets up a data probe on the given pad which will raise assertions if the
250  * data flow is inconsistent.
251  *
252  * Returns: %TRUE if the pad was added
253  */
254 gboolean
gst_consistency_checker_add_pad(GstStreamConsistency * consist,GstPad * pad)255 gst_consistency_checker_add_pad (GstStreamConsistency * consist, GstPad * pad)
256 {
257   g_return_val_if_fail (consist != NULL, FALSE);
258   g_return_val_if_fail (pad != NULL, FALSE);
259   g_return_val_if_fail (GST_OBJECT_PARENT (pad) == consist->parent, FALSE);
260 
261   add_pad (consist, pad);
262   return TRUE;
263 }
264 
265 /**
266  * gst_consistency_checker_reset:
267  * @consist: The #GstStreamConsistency to reset.
268  *
269  * Reset the stream checker's internal variables.
270  */
271 
272 void
gst_consistency_checker_reset(GstStreamConsistency * consist)273 gst_consistency_checker_reset (GstStreamConsistency * consist)
274 {
275   consist->flushing = FALSE;
276   consist->segment = FALSE;
277   consist->eos = FALSE;
278   consist->expect_flush = FALSE;
279   consist->saw_serialized_event = FALSE;
280   consist->saw_stream_start = FALSE;
281 }
282 
283 /**
284  * gst_consistency_checker_free:
285  * @consist: The #GstStreamConsistency to free.
286  *
287  * Frees the allocated data and probes associated with @consist.
288  */
289 
290 void
gst_consistency_checker_free(GstStreamConsistency * consist)291 gst_consistency_checker_free (GstStreamConsistency * consist)
292 {
293   GList *node;
294   GstStreamConsistencyProbe *p;
295 
296   /* Remove the data probes */
297   for (node = consist->pads; node; node = g_list_next (node)) {
298     p = (GstStreamConsistencyProbe *) node->data;
299     gst_pad_remove_probe (p->pad, p->probeid);
300     gst_object_unref (p->pad);
301     g_free (p);
302   }
303   g_list_free (consist->pads);
304   g_free (consist);
305 }
306