1 /* GStreamer RTP header extension unit tests
2 * Copyright (C) 2020 Matthew Waters <matthew@centricular.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
20 #include "gst/gstcaps.h"
21 #include "gst/gstvalue.h"
22 #include "gst/rtp/gstrtphdrext.h"
28 #include <gst/check/check.h>
29 #include <gst/rtp/rtp.h>
31 /* GstRTPDummyHdrExt shared between payloading and depayloading tests */
33 #define GST_TYPE_RTP_DUMMY_HDR_EXT \
34 (gst_rtp_dummy_hdr_ext_get_type())
35 #define GST_RTP_DUMMY_HDR_EXT(obj) \
36 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_DUMMY_HDR_EXT,GstRTPDummyHdrExt))
37 #define GST_RTP_DUMMY_HDR_EXT_CLASS(klass) \
38 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_DUMMY_HDR_EXT,GstRTPDummyHdrExtClass))
39 #define GST_IS_RTP_DUMMY_HDR_EXT(obj) \
40 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_DUMMY_HDR_EXT))
41 #define GST_IS_RTP_DUMMY_HDR_EXT_CLASS(klass) \
42 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_DUMMY_HDR_EXT))
44 #define DUMMY_HDR_EXT_URI "gst:test:uri"
46 typedef struct _GstRTPDummyHdrExt GstRTPDummyHdrExt;
47 typedef struct _GstRTPDummyHdrExtClass GstRTPDummyHdrExtClass;
49 struct _GstRTPDummyHdrExt
51 GstRTPHeaderExtension payload;
53 GstRTPHeaderExtensionFlags supported_flags;
56 guint set_attributes_count;
57 guint caps_field_value;
63 struct _GstRTPDummyHdrExtClass
65 GstRTPHeaderExtensionClass parent_class;
68 GType gst_rtp_dummy_hdr_ext_get_type (void);
70 G_DEFINE_TYPE (GstRTPDummyHdrExt, gst_rtp_dummy_hdr_ext,
71 GST_TYPE_RTP_HEADER_EXTENSION);
73 static GstRTPHeaderExtensionFlags
74 gst_rtp_dummy_hdr_ext_get_supported_flags (GstRTPHeaderExtension * ext);
75 static gsize gst_rtp_dummy_hdr_ext_get_max_size (GstRTPHeaderExtension * ext,
76 const GstBuffer * input_meta);
77 static gsize gst_rtp_dummy_hdr_ext_write (GstRTPHeaderExtension * ext,
78 const GstBuffer * input_meta, GstRTPHeaderExtensionFlags write_flags,
79 GstBuffer * output, guint8 * data, gsize size);
80 static gboolean gst_rtp_dummy_hdr_ext_read (GstRTPHeaderExtension * ext,
81 GstRTPHeaderExtensionFlags read_flags, const guint8 * data, gsize size,
84 gst_rtp_dummy_hdr_ext_set_caps_from_attributes (GstRTPHeaderExtension * ext,
87 gst_rtp_dummy_hdr_ext_set_attributes_from_caps (GstRTPHeaderExtension * ext,
88 const GstCaps * caps);
90 gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps (GstRTPHeaderExtension * ext,
93 static void gst_rtp_dummy_hdr_ext_finalize (GObject * object);
96 gst_rtp_dummy_hdr_ext_class_init (GstRTPDummyHdrExtClass * klass)
98 GstRTPHeaderExtensionClass *gstrtpheaderextension_class;
99 GstElementClass *gstelement_class;
100 GObjectClass *gobject_class;
102 gstrtpheaderextension_class = GST_RTP_HEADER_EXTENSION_CLASS (klass);
103 gstelement_class = GST_ELEMENT_CLASS (klass);
104 gobject_class = G_OBJECT_CLASS (klass);
106 gstrtpheaderextension_class->get_supported_flags =
107 gst_rtp_dummy_hdr_ext_get_supported_flags;
108 gstrtpheaderextension_class->get_max_size =
109 gst_rtp_dummy_hdr_ext_get_max_size;
110 gstrtpheaderextension_class->write = gst_rtp_dummy_hdr_ext_write;
111 gstrtpheaderextension_class->read = gst_rtp_dummy_hdr_ext_read;
112 gstrtpheaderextension_class->set_attributes_from_caps =
113 gst_rtp_dummy_hdr_ext_set_attributes_from_caps;
114 gstrtpheaderextension_class->set_caps_from_attributes =
115 gst_rtp_dummy_hdr_ext_set_caps_from_attributes;
116 gstrtpheaderextension_class->update_non_rtp_src_caps =
117 gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps;
119 gobject_class->finalize = gst_rtp_dummy_hdr_ext_finalize;
121 gst_element_class_set_static_metadata (gstelement_class,
122 "Dummy Test RTP Header Extension", GST_RTP_HDREXT_ELEMENT_CLASS,
123 "Dummy Test RTP Header Extension", "Author <email@example.com>");
124 gst_rtp_header_extension_class_set_uri (gstrtpheaderextension_class,
129 gst_rtp_dummy_hdr_ext_init (GstRTPDummyHdrExt * dummy)
131 dummy->supported_flags =
132 GST_RTP_HEADER_EXTENSION_ONE_BYTE | GST_RTP_HEADER_EXTENSION_TWO_BYTE;
136 gst_rtp_dummy_hdr_ext_finalize (GObject * object)
138 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (object);
140 g_free (dummy->attributes);
141 dummy->attributes = NULL;
142 g_free (dummy->direction);
143 dummy->direction = NULL;
145 G_OBJECT_CLASS (gst_rtp_dummy_hdr_ext_parent_class)->finalize (object);
148 static GstRTPHeaderExtension *
149 rtp_dummy_hdr_ext_new (void)
151 return g_object_new (GST_TYPE_RTP_DUMMY_HDR_EXT, NULL);
154 static GstRTPHeaderExtensionFlags
155 gst_rtp_dummy_hdr_ext_get_supported_flags (GstRTPHeaderExtension * ext)
157 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
159 return dummy->supported_flags;
163 gst_rtp_dummy_hdr_ext_get_max_size (GstRTPHeaderExtension * ext,
164 const GstBuffer * input_meta)
169 #define TEST_DATA_BYTE 0x9d
172 gst_rtp_dummy_hdr_ext_write (GstRTPHeaderExtension * ext,
173 const GstBuffer * input_meta, GstRTPHeaderExtensionFlags write_flags,
174 GstBuffer * output, guint8 * data, gsize size)
176 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
178 g_assert (size >= gst_rtp_dummy_hdr_ext_get_max_size (ext, NULL));
180 data[0] = TEST_DATA_BYTE;
182 dummy->write_count++;
188 gst_rtp_dummy_hdr_ext_read (GstRTPHeaderExtension * ext,
189 GstRTPHeaderExtensionFlags read_flags, const guint8 * data,
190 gsize size, GstBuffer * buffer)
192 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
194 fail_unless_equals_int (data[0], TEST_DATA_BYTE);
198 if (dummy->read_count % 5 == 1) {
199 /* Every fifth buffer triggers caps change. */
200 gst_rtp_header_extension_set_wants_update_non_rtp_src_caps (ext, TRUE);
207 gst_rtp_dummy_hdr_ext_set_caps_from_attributes (GstRTPHeaderExtension * ext,
210 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
211 gchar *field_name = gst_rtp_header_extension_get_sdp_caps_field_name (ext);
212 GstStructure *s = gst_caps_get_structure (caps, 0);
217 if (dummy->attributes || dummy->direction) {
218 GValue arr = G_VALUE_INIT;
219 GValue val = G_VALUE_INIT;
221 g_value_init (&arr, GST_TYPE_ARRAY);
222 g_value_init (&val, G_TYPE_STRING);
225 g_value_set_string (&val, dummy->direction);
226 gst_value_array_append_value (&arr, &val);
229 g_value_set_string (&val, gst_rtp_header_extension_get_uri (ext));
230 gst_value_array_append_value (&arr, &val);
233 g_value_set_string (&val, dummy->attributes);
234 gst_value_array_append_value (&arr, &val);
236 gst_structure_set_value (s, field_name, &arr);
238 g_value_unset (&val);
239 g_value_unset (&arr);
241 gst_structure_set (s, field_name, G_TYPE_STRING,
242 gst_rtp_header_extension_get_uri (ext), NULL);
250 gst_rtp_dummy_hdr_ext_set_attributes_from_caps (GstRTPHeaderExtension * ext,
251 const GstCaps * caps)
253 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
254 gchar *field_name = gst_rtp_header_extension_get_sdp_caps_field_name (ext);
255 GstStructure *s = gst_caps_get_structure (caps, 0);
256 gchar *new_attrs = NULL, *new_direction = NULL;
257 const gchar *ext_uri;
260 dummy->set_attributes_count++;
265 if ((ext_uri = gst_structure_get_string (s, field_name))) {
266 if (g_strcmp0 (ext_uri, gst_rtp_header_extension_get_uri (ext)) != 0) {
267 /* incompatible extension uri for this instance */
270 } else if ((arr = gst_structure_get_value (s, field_name))
271 && GST_VALUE_HOLDS_ARRAY (arr)
272 && gst_value_array_get_size (arr) == 3) {
275 val = gst_value_array_get_value (arr, 1);
276 if (!G_VALUE_HOLDS_STRING (val))
278 if (g_strcmp0 (g_value_get_string (val),
279 gst_rtp_header_extension_get_uri (ext)) != 0)
282 val = gst_value_array_get_value (arr, 0);
283 if (!G_VALUE_HOLDS_STRING (val))
285 new_direction = g_value_dup_string (val);
287 val = gst_value_array_get_value (arr, 2);
288 if (!G_VALUE_HOLDS_STRING (val))
290 new_attrs = g_value_dup_string (val);
292 /* unknown caps format */
296 g_free (dummy->attributes);
297 dummy->attributes = new_attrs;
298 g_free (dummy->direction);
299 dummy->direction = new_direction;
306 g_free (new_direction);
312 gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps (GstRTPHeaderExtension * ext,
315 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
317 gst_caps_set_simple (caps, "dummy-hdrext-val", G_TYPE_UINT,
318 ++dummy->caps_field_value, NULL);