aboutsummaryrefslogtreecommitdiff
path: root/libs/gst/check/gstconsistencychecker.c
blob: 576b5750275d191275874f0d98af6917144e7527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* GStreamer
 *
 * unit testing helper lib
 *
 * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
 * Copyright (C) 2012 Stefan Sauer <ensonic@users.sf.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:gstcheckconsistencychecker
 * @short_description: Data flow consistency checker for GStreamer unit tests.
 *
 * These macros and functions are for internal use of the unit tests found
 * inside the 'check' directories of various GStreamer packages.
 *
 * Since: 0.10.24
 */

#include "gstconsistencychecker.h"

struct _GstStreamConsistency
{
  /* FIXME: do we want to track some states per pad? */
  volatile gboolean flushing;
  volatile gboolean segment;
  volatile gboolean eos;
  volatile gboolean expect_flush;
  GstObject *parent;
  GList *pads;
};

typedef struct _GstStreamConsistencyProbe
{
  GstPad *pad;
  gulong probeid;
} GstStreamConsistencyProbe;

static gboolean
source_pad_data_cb (GstPad * pad, GstPadProbeInfo * info,
    GstStreamConsistency * consist)
{
  GstMiniObject *data = GST_PAD_PROBE_INFO_DATA (info);

  GST_DEBUG_OBJECT (pad, "%p: %d %d %d %d", consist, consist->flushing,
      consist->segment, consist->eos, consist->expect_flush);

  if (GST_IS_BUFFER (data)) {
    GST_DEBUG_OBJECT (pad,
        "Buffer pts %" GST_TIME_FORMAT ", dts %" GST_TIME_FORMAT,
        GST_TIME_ARGS (GST_BUFFER_PTS (GST_BUFFER_CAST (data))),
        GST_TIME_ARGS (GST_BUFFER_DTS (GST_BUFFER_CAST (data))));
    /* If an EOS went through, a buffer would be invalid */
    fail_if (consist->eos, "Buffer received after EOS");
    /* Buffers need to be preceded by a segment event */
    fail_unless (consist->segment, "Buffer received without segment");
  } else if (GST_IS_EVENT (data)) {
    GstEvent *event = (GstEvent *) data;

    GST_DEBUG_OBJECT (pad, "%s", GST_EVENT_TYPE_NAME (event));
    switch (GST_EVENT_TYPE (event)) {
      case GST_EVENT_FLUSH_START:
        /* getting two flush_start in a row seems to be okay
           fail_if (consist->flushing, "Received another FLUSH_START");
         */
        consist->flushing = TRUE;
        break;
      case GST_EVENT_FLUSH_STOP:
        /* Receiving a flush-stop is only valid after receiving a flush-start */
        fail_unless (consist->flushing,
            "Received a FLUSH_STOP without a FLUSH_START");
        fail_if (consist->eos, "Received a FLUSH_STOP after an EOS");
        consist->flushing = consist->expect_flush = FALSE;
        break;
      case GST_EVENT_STREAM_START:
      case GST_EVENT_STREAM_CONFIG:
      case GST_EVENT_CAPS:
        /* ok to have these before segment event */
        /* FIXME check order more precisely, if so spec'ed somehow ? */
        break;
      case GST_EVENT_SEGMENT:
        fail_if ((consist->expect_flush && consist->flushing),
            "Received SEGMENT while in a flushing seek");
        consist->segment = TRUE;
        consist->eos = FALSE;
        break;
      case GST_EVENT_EOS:
        /* FIXME : not 100% sure about whether two eos in a row is valid */
        fail_if (consist->eos, "Received EOS just after another EOS");
        consist->eos = TRUE;
        consist->segment = FALSE;
        break;
      case GST_EVENT_TAG:
        GST_DEBUG_OBJECT (pad, "tag %" GST_PTR_FORMAT,
            gst_event_get_structure (event));
        /* fall through */
      default:
        if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_IS_DOWNSTREAM (event)) {
          fail_if (consist->eos, "Event received after EOS");
          fail_unless (consist->segment, "Event received before segment");
        }
        /* FIXME : Figure out what to do for other events */
        break;
    }
  }

  return TRUE;
}

static gboolean
sink_pad_data_cb (GstPad * pad, GstPadProbeInfo * info,
    GstStreamConsistency * consist)
{
  GstMiniObject *data = GST_PAD_PROBE_INFO_DATA (info);

  GST_DEBUG_OBJECT (pad, "%p: %d %d %d %d", consist, consist->flushing,
      consist->segment, consist->eos, consist->expect_flush);

  if (GST_IS_BUFFER (data)) {
    GST_DEBUG_OBJECT (pad, "Buffer %" GST_TIME_FORMAT,
        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (GST_BUFFER (data))));
    /* If an EOS went through, a buffer would be invalid */
    fail_if (consist->eos, "Buffer received after EOS");
    /* Buffers need to be preceded by a segment event */
    fail_unless (consist->segment, "Buffer received without segment");
  } else if (GST_IS_EVENT (data)) {
    GstEvent *event = (GstEvent *) data;

    GST_DEBUG_OBJECT (pad, "%s", GST_EVENT_TYPE_NAME (event));
    switch (GST_EVENT_TYPE (event)) {
      case GST_EVENT_SEEK:
      {
        GstSeekFlags flags;

        gst_event_parse_seek (event, NULL, NULL, &flags, NULL, NULL, NULL,
            NULL);
        consist->expect_flush =
            ((flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH);
        break;
      }
      case GST_EVENT_SEGMENT:
        fail_if ((consist->expect_flush && consist->flushing),
            "Received SEGMENT while in a flushing seek");
        consist->segment = TRUE;
        consist->eos = FALSE;
        break;
      default:
        /* FIXME : Figure out what to do for other events */
        break;
    }
  }

  return TRUE;
}

static void
add_pad (GstStreamConsistency * consist, GstPad * pad)
{
  GstStreamConsistencyProbe *p;
  GstPadDirection dir;

  p = g_new0 (GstStreamConsistencyProbe, 1);
  p->pad = g_object_ref (pad);
  dir = gst_pad_get_direction (pad);
  if (dir == GST_PAD_SRC) {

    p->probeid =
        gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
        (GstPadProbeCallback) source_pad_data_cb, consist, NULL);

  } else if (dir == GST_PAD_SINK) {
    p->probeid =
        gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
        (GstPadProbeCallback) sink_pad_data_cb, consist, NULL);
  }
  consist->pads = g_list_prepend (consist->pads, p);
}

/**
 * gst_consistency_checker_new:
 * @pad: The #GstPad on which the dataflow will be checked.
 *
 * Sets up a data probe on the given pad which will raise assertions if the
 * data flow is inconsistent.
 *
 * Returns: A #GstStreamConsistency structure used to track data flow.
 *
 * Since: 0.10.24
 */
GstStreamConsistency *
gst_consistency_checker_new (GstPad * pad)
{
  GstStreamConsistency *consist;

  g_return_val_if_fail (pad != NULL, NULL);

  consist = g_new0 (GstStreamConsistency, 1);

  if (!consist->pads) {
    consist->parent = GST_OBJECT_PARENT (pad);
  }
  add_pad (consist, pad);
  return consist;
}

/**
 * gst_consistency_checker_add_pad:
 * @consist: The #GstStreamConsistency handle
 * @pad: The #GstPad on which the dataflow will be checked.
 *
 * Sets up a data probe on the given pad which will raise assertions if the
 * data flow is inconsistent.
 *
 * Returns: %TRUE if the pad was added
 *
 * Since: 0.10.37
 */
gboolean
gst_consistency_checker_add_pad (GstStreamConsistency * consist, GstPad * pad)
{
  g_return_val_if_fail (consist != NULL, FALSE);
  g_return_val_if_fail (pad != NULL, FALSE);
  g_return_val_if_fail (GST_OBJECT_PARENT (pad) == consist->parent, FALSE);

  add_pad (consist, pad);
  return TRUE;
}

/**
 * gst_consistency_checker_reset:
 * @consist: The #GstStreamConsistency to reset.
 *
 * Reset the stream checker's internal variables.
 *
 * Since: 0.10.24
 */

void
gst_consistency_checker_reset (GstStreamConsistency * consist)
{
  consist->eos = FALSE;
  consist->flushing = FALSE;
  consist->segment = FALSE;
}

/**
 * gst_consistency_checker_free:
 * @consist: The #GstStreamConsistency to free.
 *
 * Frees the allocated data and probes associated with @consist.
 *
 * Since: 0.10.24
 */

void
gst_consistency_checker_free (GstStreamConsistency * consist)
{
  GList *node;
  GstStreamConsistencyProbe *p;

  /* Remove the data probes */
  for (node = consist->pads; node; node = g_list_next (node)) {
    p = (GstStreamConsistencyProbe *) node->data;
    gst_pad_remove_probe (p->pad, p->probeid);
    gst_object_unref (p->pad);
    g_free (p);
  }
  g_list_free (consist->pads);
  g_free (consist);
}