1 /* GStreamer RTP header extension unit tests
2 * Copyright (C) 2020 Matthew Waters <matthew@centricular.com>
3 *
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.
8 *
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.
13 *
14 * You should have received a copy of the GNU Library General
15 * Public
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.
19 */
20 #include "gst/gstcaps.h"
21 #include "gst/gstvalue.h"
22 #include "gst/rtp/gstrtphdrext.h"
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/gst.h>
28 #include <gst/check/check.h>
29 #include <gst/rtp/rtp.h>
30
31 /* GstRTPDummyHdrExt shared between payloading and depayloading tests */
32
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))
43
44 #define DUMMY_HDR_EXT_URI "gst:test:uri"
45
46 typedef struct _GstRTPDummyHdrExt GstRTPDummyHdrExt;
47 typedef struct _GstRTPDummyHdrExtClass GstRTPDummyHdrExtClass;
48
49 struct _GstRTPDummyHdrExt
50 {
51 GstRTPHeaderExtension payload;
52
53 GstRTPHeaderExtensionFlags supported_flags;
54 guint read_count;
55 guint write_count;
56 guint set_attributes_count;
57 guint caps_field_value;
58
59 gchar *attributes;
60
61 gsize max_size;
62 };
63
64 struct _GstRTPDummyHdrExtClass
65 {
66 GstRTPHeaderExtensionClass parent_class;
67 };
68
69 GType gst_rtp_dummy_hdr_ext_get_type (void);
70
71 G_DEFINE_TYPE (GstRTPDummyHdrExt, gst_rtp_dummy_hdr_ext,
72 GST_TYPE_RTP_HEADER_EXTENSION);
73
74 static GstRTPHeaderExtensionFlags
75 gst_rtp_dummy_hdr_ext_get_supported_flags (GstRTPHeaderExtension * ext);
76 static gsize gst_rtp_dummy_hdr_ext_get_max_size (GstRTPHeaderExtension * ext,
77 const GstBuffer * input_meta);
78 static gssize gst_rtp_dummy_hdr_ext_write (GstRTPHeaderExtension * ext,
79 const GstBuffer * input_meta, GstRTPHeaderExtensionFlags write_flags,
80 GstBuffer * output, guint8 * data, gsize size);
81 static gboolean gst_rtp_dummy_hdr_ext_read (GstRTPHeaderExtension * ext,
82 GstRTPHeaderExtensionFlags read_flags, const guint8 * data, gsize size,
83 GstBuffer * buffer);
84 static gboolean
85 gst_rtp_dummy_hdr_ext_set_caps_from_attributes (GstRTPHeaderExtension * ext,
86 GstCaps * caps);
87 static gboolean
88 gst_rtp_dummy_hdr_ext_set_attributes (GstRTPHeaderExtension * ext,
89 GstRTPHeaderExtensionDirection direction, const gchar * attributes);
90 static gboolean
91 gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps (GstRTPHeaderExtension * ext,
92 GstCaps * caps);
93
94 static void gst_rtp_dummy_hdr_ext_finalize (GObject * object);
95
96 static void
gst_rtp_dummy_hdr_ext_class_init(GstRTPDummyHdrExtClass * klass)97 gst_rtp_dummy_hdr_ext_class_init (GstRTPDummyHdrExtClass * klass)
98 {
99 GstRTPHeaderExtensionClass *gstrtpheaderextension_class;
100 GstElementClass *gstelement_class;
101 GObjectClass *gobject_class;
102
103 gstrtpheaderextension_class = GST_RTP_HEADER_EXTENSION_CLASS (klass);
104 gstelement_class = GST_ELEMENT_CLASS (klass);
105 gobject_class = G_OBJECT_CLASS (klass);
106
107 gstrtpheaderextension_class->get_supported_flags =
108 gst_rtp_dummy_hdr_ext_get_supported_flags;
109 gstrtpheaderextension_class->get_max_size =
110 gst_rtp_dummy_hdr_ext_get_max_size;
111 gstrtpheaderextension_class->write = gst_rtp_dummy_hdr_ext_write;
112 gstrtpheaderextension_class->read = gst_rtp_dummy_hdr_ext_read;
113 gstrtpheaderextension_class->set_attributes =
114 gst_rtp_dummy_hdr_ext_set_attributes;
115 gstrtpheaderextension_class->set_caps_from_attributes =
116 gst_rtp_dummy_hdr_ext_set_caps_from_attributes;
117 gstrtpheaderextension_class->update_non_rtp_src_caps =
118 gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps;
119
120 gobject_class->finalize = gst_rtp_dummy_hdr_ext_finalize;
121
122 gst_element_class_set_static_metadata (gstelement_class,
123 "Dummy Test RTP Header Extension", GST_RTP_HDREXT_ELEMENT_CLASS,
124 "Dummy Test RTP Header Extension", "Author <email@example.com>");
125 gst_rtp_header_extension_class_set_uri (gstrtpheaderextension_class,
126 DUMMY_HDR_EXT_URI);
127 }
128
129 static void
gst_rtp_dummy_hdr_ext_init(GstRTPDummyHdrExt * dummy)130 gst_rtp_dummy_hdr_ext_init (GstRTPDummyHdrExt * dummy)
131 {
132 dummy->supported_flags =
133 GST_RTP_HEADER_EXTENSION_ONE_BYTE | GST_RTP_HEADER_EXTENSION_TWO_BYTE;
134 dummy->max_size = 1;
135 }
136
137 static void
gst_rtp_dummy_hdr_ext_finalize(GObject * object)138 gst_rtp_dummy_hdr_ext_finalize (GObject * object)
139 {
140 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (object);
141
142 g_free (dummy->attributes);
143 dummy->attributes = NULL;
144
145 G_OBJECT_CLASS (gst_rtp_dummy_hdr_ext_parent_class)->finalize (object);
146 }
147
148 static GstRTPHeaderExtension *
rtp_dummy_hdr_ext_new(void)149 rtp_dummy_hdr_ext_new (void)
150 {
151 return g_object_new (GST_TYPE_RTP_DUMMY_HDR_EXT, NULL);
152 }
153
154 static GstRTPHeaderExtensionFlags
gst_rtp_dummy_hdr_ext_get_supported_flags(GstRTPHeaderExtension * ext)155 gst_rtp_dummy_hdr_ext_get_supported_flags (GstRTPHeaderExtension * ext)
156 {
157 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
158
159 return dummy->supported_flags;
160 }
161
162 static gsize
gst_rtp_dummy_hdr_ext_get_max_size(GstRTPHeaderExtension * ext,const GstBuffer * input_meta)163 gst_rtp_dummy_hdr_ext_get_max_size (GstRTPHeaderExtension * ext,
164 const GstBuffer * input_meta)
165 {
166 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
167
168 return dummy->max_size;
169 }
170
171 #define TEST_DATA_BYTE 0x9d
172
173 static gssize
gst_rtp_dummy_hdr_ext_write(GstRTPHeaderExtension * ext,const GstBuffer * input_meta,GstRTPHeaderExtensionFlags write_flags,GstBuffer * output,guint8 * data,gsize size)174 gst_rtp_dummy_hdr_ext_write (GstRTPHeaderExtension * ext,
175 const GstBuffer * input_meta, GstRTPHeaderExtensionFlags write_flags,
176 GstBuffer * output, guint8 * data, gsize size)
177 {
178 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
179
180 g_assert (size >= gst_rtp_dummy_hdr_ext_get_max_size (ext, NULL));
181
182 data[0] = TEST_DATA_BYTE;
183
184 dummy->write_count++;
185
186 return 1;
187 }
188
189 static gboolean
gst_rtp_dummy_hdr_ext_read(GstRTPHeaderExtension * ext,GstRTPHeaderExtensionFlags read_flags,const guint8 * data,gsize size,GstBuffer * buffer)190 gst_rtp_dummy_hdr_ext_read (GstRTPHeaderExtension * ext,
191 GstRTPHeaderExtensionFlags read_flags, const guint8 * data,
192 gsize size, GstBuffer * buffer)
193 {
194 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
195
196 fail_unless_equals_int (data[0], TEST_DATA_BYTE);
197
198 dummy->read_count++;
199
200 if (dummy->read_count % 5 == 1) {
201 /* Every fifth buffer triggers caps change. */
202 gst_rtp_header_extension_set_wants_update_non_rtp_src_caps (ext, TRUE);
203 }
204
205 return TRUE;
206 }
207
208 static gboolean
gst_rtp_dummy_hdr_ext_set_caps_from_attributes(GstRTPHeaderExtension * ext,GstCaps * caps)209 gst_rtp_dummy_hdr_ext_set_caps_from_attributes (GstRTPHeaderExtension * ext,
210 GstCaps * caps)
211 {
212 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
213
214 return gst_rtp_header_extension_set_caps_from_attributes_helper (ext, caps,
215 dummy->attributes);
216 }
217
218 static gboolean
gst_rtp_dummy_hdr_ext_set_attributes(GstRTPHeaderExtension * ext,GstRTPHeaderExtensionDirection direction,const gchar * attributes)219 gst_rtp_dummy_hdr_ext_set_attributes (GstRTPHeaderExtension * ext,
220 GstRTPHeaderExtensionDirection direction, const gchar * attributes)
221 {
222 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
223
224 dummy->set_attributes_count++;
225
226 g_free (dummy->attributes);
227 dummy->attributes = g_strdup (attributes);
228
229 return TRUE;
230 }
231
232 static gboolean
gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps(GstRTPHeaderExtension * ext,GstCaps * caps)233 gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps (GstRTPHeaderExtension * ext,
234 GstCaps * caps)
235 {
236 GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
237
238 gst_caps_set_simple (caps, "dummy-hdrext-val", G_TYPE_UINT,
239 ++dummy->caps_field_value, NULL);
240
241 return TRUE;
242 }
243