rtpbasedepayload: handle caps change partway through buffer list
[platform/upstream/gstreamer.git] / tests / check / libs / rtpdummyhdrextimpl.c
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 *direction;
60   gchar *attributes;
61 };
62
63 struct _GstRTPDummyHdrExtClass
64 {
65   GstRTPHeaderExtensionClass parent_class;
66 };
67
68 GType gst_rtp_dummy_hdr_ext_get_type (void);
69
70 G_DEFINE_TYPE (GstRTPDummyHdrExt, gst_rtp_dummy_hdr_ext,
71     GST_TYPE_RTP_HEADER_EXTENSION);
72
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,
82     GstBuffer * buffer);
83 static gboolean
84 gst_rtp_dummy_hdr_ext_set_caps_from_attributes (GstRTPHeaderExtension * ext,
85     GstCaps * caps);
86 static gboolean
87 gst_rtp_dummy_hdr_ext_set_attributes_from_caps (GstRTPHeaderExtension * ext,
88     const GstCaps * caps);
89 static gboolean
90 gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps (GstRTPHeaderExtension * ext,
91     GstCaps * caps);
92
93 static void gst_rtp_dummy_hdr_ext_finalize (GObject * object);
94
95 static void
96 gst_rtp_dummy_hdr_ext_class_init (GstRTPDummyHdrExtClass * klass)
97 {
98   GstRTPHeaderExtensionClass *gstrtpheaderextension_class;
99   GstElementClass *gstelement_class;
100   GObjectClass *gobject_class;
101
102   gstrtpheaderextension_class = GST_RTP_HEADER_EXTENSION_CLASS (klass);
103   gstelement_class = GST_ELEMENT_CLASS (klass);
104   gobject_class = G_OBJECT_CLASS (klass);
105
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;
118
119   gobject_class->finalize = gst_rtp_dummy_hdr_ext_finalize;
120
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,
125       DUMMY_HDR_EXT_URI);
126 }
127
128 static void
129 gst_rtp_dummy_hdr_ext_init (GstRTPDummyHdrExt * dummy)
130 {
131   dummy->supported_flags =
132       GST_RTP_HEADER_EXTENSION_ONE_BYTE | GST_RTP_HEADER_EXTENSION_TWO_BYTE;
133 }
134
135 static void
136 gst_rtp_dummy_hdr_ext_finalize (GObject * object)
137 {
138   GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (object);
139
140   g_free (dummy->attributes);
141   dummy->attributes = NULL;
142   g_free (dummy->direction);
143   dummy->direction = NULL;
144
145   G_OBJECT_CLASS (gst_rtp_dummy_hdr_ext_parent_class)->finalize (object);
146 }
147
148 static GstRTPHeaderExtension *
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
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
163 gst_rtp_dummy_hdr_ext_get_max_size (GstRTPHeaderExtension * ext,
164     const GstBuffer * input_meta)
165 {
166   return 1;
167 }
168
169 #define TEST_DATA_BYTE 0x9d
170
171 static gsize
172 gst_rtp_dummy_hdr_ext_write (GstRTPHeaderExtension * ext,
173     const GstBuffer * input_meta, GstRTPHeaderExtensionFlags write_flags,
174     GstBuffer * output, guint8 * data, gsize size)
175 {
176   GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
177
178   g_assert (size >= gst_rtp_dummy_hdr_ext_get_max_size (ext, NULL));
179
180   data[0] = TEST_DATA_BYTE;
181
182   dummy->write_count++;
183
184   return 1;
185 }
186
187 static gboolean
188 gst_rtp_dummy_hdr_ext_read (GstRTPHeaderExtension * ext,
189     GstRTPHeaderExtensionFlags read_flags, const guint8 * data,
190     gsize size, GstBuffer * buffer)
191 {
192   GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
193
194   fail_unless_equals_int (data[0], TEST_DATA_BYTE);
195
196   dummy->read_count++;
197
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);
201   }
202
203   return TRUE;
204 }
205
206 static gboolean
207 gst_rtp_dummy_hdr_ext_set_caps_from_attributes (GstRTPHeaderExtension * ext,
208     GstCaps * caps)
209 {
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);
213
214   if (!field_name)
215     return FALSE;
216
217   if (dummy->attributes || dummy->direction) {
218     GValue arr = G_VALUE_INIT;
219     GValue val = G_VALUE_INIT;
220
221     g_value_init (&arr, GST_TYPE_ARRAY);
222     g_value_init (&val, G_TYPE_STRING);
223
224     /* direction */
225     g_value_set_string (&val, dummy->direction);
226     gst_value_array_append_value (&arr, &val);
227
228     /* uri */
229     g_value_set_string (&val, gst_rtp_header_extension_get_uri (ext));
230     gst_value_array_append_value (&arr, &val);
231
232     /* attributes */
233     g_value_set_string (&val, dummy->attributes);
234     gst_value_array_append_value (&arr, &val);
235
236     gst_structure_set_value (s, field_name, &arr);
237
238     g_value_unset (&val);
239     g_value_unset (&arr);
240   } else {
241     gst_structure_set (s, field_name, G_TYPE_STRING,
242         gst_rtp_header_extension_get_uri (ext), NULL);
243   }
244
245   g_free (field_name);
246   return TRUE;
247 }
248
249 static gboolean
250 gst_rtp_dummy_hdr_ext_set_attributes_from_caps (GstRTPHeaderExtension * ext,
251     const GstCaps * caps)
252 {
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;
258   const GValue *arr;
259
260   dummy->set_attributes_count++;
261
262   if (!field_name)
263     return FALSE;
264
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 */
268       goto error;
269     }
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) {
273     const GValue *val;
274
275     val = gst_value_array_get_value (arr, 1);
276     if (!G_VALUE_HOLDS_STRING (val))
277       goto error;
278     if (g_strcmp0 (g_value_get_string (val),
279             gst_rtp_header_extension_get_uri (ext)) != 0)
280       goto error;
281
282     val = gst_value_array_get_value (arr, 0);
283     if (!G_VALUE_HOLDS_STRING (val))
284       goto error;
285     new_direction = g_value_dup_string (val);
286
287     val = gst_value_array_get_value (arr, 2);
288     if (!G_VALUE_HOLDS_STRING (val))
289       goto error;
290     new_attrs = g_value_dup_string (val);
291   } else {
292     /* unknown caps format */
293     goto error;
294   }
295
296   g_free (dummy->attributes);
297   dummy->attributes = new_attrs;
298   g_free (dummy->direction);
299   dummy->direction = new_direction;
300
301   g_free (field_name);
302   return TRUE;
303
304 error:
305   g_free (field_name);
306   g_free (new_direction);
307   g_free (new_attrs);
308   return FALSE;
309 }
310
311 static gboolean
312 gst_rtp_dummy_hdr_ext_update_non_rtp_src_caps (GstRTPHeaderExtension * ext,
313     GstCaps * caps)
314 {
315   GstRTPDummyHdrExt *dummy = GST_RTP_DUMMY_HDR_EXT (ext);
316
317   gst_caps_set_simple (caps, "dummy-hdrext-val", G_TYPE_UINT,
318       ++dummy->caps_field_value, NULL);
319
320   return TRUE;
321 }