summaryrefslogtreecommitdiff
path: root/tests/check/libs/rtpdummyhdrextimpl.c
blob: e5d09af0dd188a0b2450a595c7669812e5c6eb53 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* GStreamer RTP header extension unit tests
 * Copyright (C) 2020 Matthew Waters <matthew@centricular.com>
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "gst/gstcaps.h"
#include "gst/gstvalue.h"
#include "gst/rtp/gstrtphdrext.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>
#include <gst/check/check.h>
#include <gst/rtp/rtp.h>

/* GstRTPDummyHdrExt shared between payloading and depayloading tests */

#define GST_TYPE_RTP_DUMMY_HDR_EXT \
  (gst_rtp_dummy_hdr_ext_get_type())
#define GST_RTP_DUMMY_HDR_EXT(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_DUMMY_HDR_EXT,GstRTPDummyHdrExt))
#define GST_RTP_DUMMY_HDR_EXT_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_DUMMY_HDR_EXT,GstRTPDummyHdrExtClass))
#define GST_IS_RTP_DUMMY_HDR_EXT(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_DUMMY_HDR_EXT))
#define GST_IS_RTP_DUMMY_HDR_EXT_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_DUMMY_HDR_EXT))

#define DUMMY_HDR_EXT_URI "gst:test:uri"

typedef struct _GstRTPDummyHdrExt GstRTPDummyHdrExt;
typedef struct _GstRTPDummyHdrExtClass GstRTPDummyHdrExtClass;

struct _GstRTPDummyHdrExt
{
  GstRTPHeaderExtension payload;

  GstRTPHeaderExtensionFlags supported_flags;
  guint read_count;
  guint write_count;
  guint set_attributes_count;
  guint caps_field_value;

  gchar *direction;
  gchar *attributes;
};

struct _GstRTPDummyHdrExtClass
{
  GstRTPHeaderExtensionClass parent_class;
};

GType gst_rtp_dummy_hdr_ext_get_type (void);

G_DEFINE_TYPE (GstRTPDummyHdrExt, gst_rtp_dummy_hdr_ext,
    GST_TYPE_RTP_HEADER_EXTENSION);

static GstRTPHeaderExtensionFlags
gst_rtp_dummy_hdr_ext_get_supported_flags (GstRTPHeaderExtension * ext);
static gsize gst_rtp_dummy_hdr_ext_get_max_size (GstRTPHeaderExtension * ext,
    const GstBuffer * input_meta);
static gsize gst_rtp_dummy_hdr_ext_write (GstRTPHeaderExtension * ext,
    const GstBuffer * input_meta, GstRTPHeaderExtensionFlags write_flags,
    GstBuffer * output, guint8 * data, gsize size);
static gboolean gst_rtp_dummy_hdr_ext_read (GstRTPHeaderExtension * ext,
    GstRTPHeaderExtensionFlags read_flags, const guint8 * data, gsize size,
    GstBuffer * buffer);
static gboolean
gst_rtp_dummy_hdr_ext_set_caps_from_attributes (GstRTPHeaderExtension * ext,
    GstCaps * caps);
static gboolean
gst_rtp_dummy_hdr_ext_set_attributes_from_caps (GstRTPHeaderExtension * ext,
    const GstCaps * caps);
static gboolean
gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps (GstRTPHeaderExtension * ext,
    GstCaps * caps);

static void gst_rtp_dummy_hdr_ext_finalize (GObject * object);

static void
gst_rtp_dummy_hdr_ext_class_init (GstRTPDummyHdrExtClass * klass)
{
  GstRTPHeaderExtensionClass *gstrtpheaderextension_class;
  GstElementClass *gstelement_class;
  GObjectClass *gobject_class;

  gstrtpheaderextension_class = GST_RTP_HEADER_EXTENSION_CLASS (klass);
  gstelement_class = GST_ELEMENT_CLASS (klass);
  gobject_class = G_OBJECT_CLASS (klass);

  gstrtpheaderextension_class->get_supported_flags =
      gst_rtp_dummy_hdr_ext_get_supported_flags;
  gstrtpheaderextension_class->get_max_size =
      gst_rtp_dummy_hdr_ext_get_max_size;
  gstrtpheaderextension_class->write = gst_rtp_dummy_hdr_ext_write;
  gstrtpheaderextension_class->read = gst_rtp_dummy_hdr_ext_read;
  gstrtpheaderextension_class->set_attributes_from_caps =
      gst_rtp_dummy_hdr_ext_set_attributes_from_caps;
  gstrtpheaderextension_class->set_caps_from_attributes =
      gst_rtp_dummy_hdr_ext_set_caps_from_attributes;
  gstrtpheaderextension_class->update_non_rtp_src_caps =
      gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps;

  gobject_class->finalize = gst_rtp_dummy_hdr_ext_finalize;

  gst_element_class_set_static_metadata (gstelement_class,
      "Dummy Test RTP Header Extension", GST_RTP_HDREXT_ELEMENT_CLASS,
      "Dummy Test RTP Header Extension", "Author <email@example.com>");
  gst_rtp_header_extension_class_set_uri (gstrtpheaderextension_class,
      DUMMY_HDR_EXT_URI);
}

static void
gst_rtp_dummy_hdr_ext_init (GstRTPDummyHdrExt * dummy)
{
  dummy->supported_flags =
      GST_RTP_HEADER_EXTENSION_ONE_BYTE | GST_RTP_HEADER_EXTENSION_TWO_BYTE;
}

static void
gst_rtp_dummy_hdr_ext_finalize (GObject * object)
{
  GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (object);

  g_free (dummy->attributes);
  dummy->attributes = NULL;
  g_free (dummy->direction);
  dummy->direction = NULL;

  G_OBJECT_CLASS (gst_rtp_dummy_hdr_ext_parent_class)->finalize (object);
}

static GstRTPHeaderExtension *
rtp_dummy_hdr_ext_new (void)
{
  return g_object_new (GST_TYPE_RTP_DUMMY_HDR_EXT, NULL);
}

static GstRTPHeaderExtensionFlags
gst_rtp_dummy_hdr_ext_get_supported_flags (GstRTPHeaderExtension * ext)
{
  GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);

  return dummy->supported_flags;
}

static gsize
gst_rtp_dummy_hdr_ext_get_max_size (GstRTPHeaderExtension * ext,
    const GstBuffer * input_meta)
{
  return 1;
}

#define TEST_DATA_BYTE 0x9d

static gsize
gst_rtp_dummy_hdr_ext_write (GstRTPHeaderExtension * ext,
    const GstBuffer * input_meta, GstRTPHeaderExtensionFlags write_flags,
    GstBuffer * output, guint8 * data, gsize size)
{
  GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);

  g_assert (size >= gst_rtp_dummy_hdr_ext_get_max_size (ext, NULL));

  data[0] = TEST_DATA_BYTE;

  dummy->write_count++;

  return 1;
}

static gboolean
gst_rtp_dummy_hdr_ext_read (GstRTPHeaderExtension * ext,
    GstRTPHeaderExtensionFlags read_flags, const guint8 * data,
    gsize size, GstBuffer * buffer)
{
  GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);

  fail_unless_equals_int (data[0], TEST_DATA_BYTE);

  dummy->read_count++;

  if (dummy->read_count % 5 == 1) {
    /* Every fifth buffer triggers caps change. */
    gst_rtp_header_extension_set_wants_update_non_rtp_src_caps (ext, TRUE);
  }

  return TRUE;
}

static gboolean
gst_rtp_dummy_hdr_ext_set_caps_from_attributes (GstRTPHeaderExtension * ext,
    GstCaps * caps)
{
  GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
  gchar *field_name = gst_rtp_header_extension_get_sdp_caps_field_name (ext);
  GstStructure *s = gst_caps_get_structure (caps, 0);

  if (!field_name)
    return FALSE;

  if (dummy->attributes || dummy->direction) {
    GValue arr = G_VALUE_INIT;
    GValue val = G_VALUE_INIT;

    g_value_init (&arr, GST_TYPE_ARRAY);
    g_value_init (&val, G_TYPE_STRING);

    /* direction */
    g_value_set_string (&val, dummy->direction);
    gst_value_array_append_value (&arr, &val);

    /* uri */
    g_value_set_string (&val, gst_rtp_header_extension_get_uri (ext));
    gst_value_array_append_value (&arr, &val);

    /* attributes */
    g_value_set_string (&val, dummy->attributes);
    gst_value_array_append_value (&arr, &val);

    gst_structure_set_value (s, field_name, &arr);

    g_value_unset (&val);
    g_value_unset (&arr);
  } else {
    gst_structure_set (s, field_name, G_TYPE_STRING,
        gst_rtp_header_extension_get_uri (ext), NULL);
  }

  g_free (field_name);
  return TRUE;
}

static gboolean
gst_rtp_dummy_hdr_ext_set_attributes_from_caps (GstRTPHeaderExtension * ext,
    const GstCaps * caps)
{
  GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
  gchar *field_name = gst_rtp_header_extension_get_sdp_caps_field_name (ext);
  GstStructure *s = gst_caps_get_structure (caps, 0);
  gchar *new_attrs = NULL, *new_direction = NULL;
  const gchar *ext_uri;
  const GValue *arr;

  dummy->set_attributes_count++;

  if (!field_name)
    return FALSE;

  if ((ext_uri = gst_structure_get_string (s, field_name))) {
    if (g_strcmp0 (ext_uri, gst_rtp_header_extension_get_uri (ext)) != 0) {
      /* incompatible extension uri for this instance */
      goto error;
    }
  } else if ((arr = gst_structure_get_value (s, field_name))
      && GST_VALUE_HOLDS_ARRAY (arr)
      && gst_value_array_get_size (arr) == 3) {
    const GValue *val;

    val = gst_value_array_get_value (arr, 1);
    if (!G_VALUE_HOLDS_STRING (val))
      goto error;
    if (g_strcmp0 (g_value_get_string (val),
            gst_rtp_header_extension_get_uri (ext)) != 0)
      goto error;

    val = gst_value_array_get_value (arr, 0);
    if (!G_VALUE_HOLDS_STRING (val))
      goto error;
    new_direction = g_value_dup_string (val);

    val = gst_value_array_get_value (arr, 2);
    if (!G_VALUE_HOLDS_STRING (val))
      goto error;
    new_attrs = g_value_dup_string (val);
  } else {
    /* unknown caps format */
    goto error;
  }

  g_free (dummy->attributes);
  dummy->attributes = new_attrs;
  g_free (dummy->direction);
  dummy->direction = new_direction;

  g_free (field_name);
  return TRUE;

error:
  g_free (field_name);
  g_free (new_direction);
  g_free (new_attrs);
  return FALSE;
}

static gboolean
gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps (GstRTPHeaderExtension * ext,
    GstCaps * caps)
{
  GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);

  gst_caps_set_simple (caps, "dummy-hdrext-val", G_TYPE_UINT,
      ++dummy->caps_field_value, NULL);

  return TRUE;
}