• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * unit test for RTP RFC 6464 Header Extensions
4  *
5  * Copyright (C) <2020-2021> Guillaume Desmottes <guillaume.desmottes@collabora.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include <gst/check/gstcheck.h>
24 #include <gst/rtp/rtp.h>
25 #include <gst/sdp/gstsdpmessage.h>
26 #include <gst/audio/audio.h>
27 #include <gst/check/gstharness.h>
28 
29 #define URN "urn:ietf:params:rtp-hdrext:ssrc-audio-level"
30 
31 #define SDP "v=0\r\n" \
32     "o=- 123456 2 IN IP4 127.0.0.1 \r\n" \
33     "s=-\r\n" \
34     "t=0 0\r\n" \
35     "a=maxptime:60\r\n" \
36     "a=sendrecv\r\n" \
37     "m=audio 55815 RTP/SAVPF 100\r\n" \
38     "c=IN IP4 1.1.1.1\r\n" \
39     "a=rtpmap:100 opus/48000/2\r\n"
40 
41 #define SDP_NO_VAD SDP \
42     "a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\n"
43 #define SDP_VAD_ON SDP \
44     "a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level vad=on\r\n"
45 #define SDP_VAD_OFF SDP \
46     "a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level vad=off\r\n"
47 #define SDP_VAD_WRONG SDP \
48     "a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level vad=badger\r\n"
49 
50 static GstCaps *
create_caps(const gchar * sdp)51 create_caps (const gchar * sdp)
52 {
53   GstSDPMessage *message;
54   glong length = -1;
55   const GstSDPMedia *media;
56   GstCaps *caps;
57 
58   gst_sdp_message_new (&message);
59   gst_sdp_message_parse_buffer ((guint8 *) sdp, length, message);
60   media = gst_sdp_message_get_media (message, 0);
61   fail_unless (media != NULL);
62 
63   caps = gst_sdp_media_get_caps_from_media (media, 100);
64   gst_sdp_media_attributes_to_caps (media, caps);
65   gst_sdp_message_free (message);
66   return caps;
67 }
68 
69 static void
check_caps(GstRTPHeaderExtension * ext,gboolean vad)70 check_caps (GstRTPHeaderExtension * ext, gboolean vad)
71 {
72   GstCaps *caps;
73   GstStructure *s;
74   const GValue *arr, *val;
75 
76   caps = gst_caps_new_empty_simple ("application/x-rtp");
77   fail_unless (gst_rtp_header_extension_set_caps_from_attributes (ext, caps));
78   s = gst_caps_get_structure (caps, 0);
79 
80   arr = gst_structure_get_value (s, "extmap-1");
81   fail_unless (arr != NULL);
82   fail_unless (GST_VALUE_HOLDS_ARRAY (arr));
83   fail_unless (gst_value_array_get_size (arr) == 3);
84 
85   val = gst_value_array_get_value (arr, 0);
86   fail_unless_equals_string (g_value_get_string (val), "");
87 
88   val = gst_value_array_get_value (arr, 1);
89   fail_unless_equals_string (g_value_get_string (val), URN);
90 
91   val = gst_value_array_get_value (arr, 2);
92   if (vad) {
93     fail_unless_equals_string (g_value_get_string (val), "vad=on");
94   } else {
95     fail_unless_equals_string (g_value_get_string (val), "vad=off");
96   }
97 
98   gst_caps_unref (caps);
99 }
100 
GST_START_TEST(rtphdrext_client_audio_level_sdp)101 GST_START_TEST (rtphdrext_client_audio_level_sdp)
102 {
103   GstRTPHeaderExtension *ext;
104   GstCaps *caps;
105   gboolean vad = FALSE;
106 
107   ext = gst_rtp_header_extension_create_from_uri (URN);
108   fail_unless (ext != NULL);
109   gst_rtp_header_extension_set_id (ext, 1);
110 
111   /* vad default to on */
112   caps = create_caps (SDP_NO_VAD);
113   fail_unless (gst_rtp_header_extension_set_attributes_from_caps (ext, caps));
114   gst_caps_unref (caps);
115   g_object_get (ext, "vad", &vad, NULL);
116   fail_unless (vad);
117   check_caps (ext, TRUE);
118 
119   /* vad is disabled */
120   caps = create_caps (SDP_VAD_OFF);
121   fail_unless (gst_rtp_header_extension_set_attributes_from_caps (ext, caps));
122   gst_caps_unref (caps);
123   g_object_get (ext, "vad", &vad, NULL);
124   fail_if (vad);
125 
126   /* vad is enabled */
127   caps = create_caps (SDP_VAD_ON);
128   fail_unless (gst_rtp_header_extension_set_attributes_from_caps (ext, caps));
129   gst_caps_unref (caps);
130   g_object_get (ext, "vad", &vad, NULL);
131   fail_unless (vad);
132 
133   /* invalid vad */
134   caps = create_caps (SDP_VAD_WRONG);
135   fail_if (gst_rtp_header_extension_set_attributes_from_caps (ext, caps));
136   gst_caps_unref (caps);
137 
138   gst_object_unref (ext);
139 }
140 
141 GST_END_TEST;
142 
GST_START_TEST(rtphdrext_client_audio_level_one_byte)143 GST_START_TEST (rtphdrext_client_audio_level_one_byte)
144 {
145   GstRTPHeaderExtension *ext;
146   GstRTPHeaderExtensionFlags flags;
147   GstBuffer *buffer;
148   guint8 *data;
149   gsize size, written;
150   GstAudioLevelMeta *meta;
151   guint8 level = 12;
152   gboolean voice = TRUE;
153 
154   ext = gst_rtp_header_extension_create_from_uri (URN);
155   fail_unless (ext != NULL);
156   gst_rtp_header_extension_set_id (ext, 1);
157 
158   flags = gst_rtp_header_extension_get_supported_flags (ext);
159   fail_unless (flags & GST_RTP_HEADER_EXTENSION_ONE_BYTE);
160 
161   buffer = gst_buffer_new ();
162   meta = gst_buffer_add_audio_level_meta (buffer, level, voice);
163 
164   size = gst_rtp_header_extension_get_max_size (ext, buffer);
165   fail_unless (size > 0);
166   data = g_malloc0 (size);
167   fail_unless (data != NULL);
168 
169   /* Write extension */
170   written =
171       gst_rtp_header_extension_write (ext, buffer,
172       GST_RTP_HEADER_EXTENSION_ONE_BYTE, buffer, data, size);
173   fail_unless (written == 1);
174 
175   /* Read it back */
176   fail_unless (gst_buffer_remove_meta (buffer, (GstMeta *) meta));
177   fail_unless (gst_rtp_header_extension_read (ext,
178           GST_RTP_HEADER_EXTENSION_ONE_BYTE, data, size, buffer));
179   meta = gst_buffer_get_audio_level_meta (buffer);
180   fail_unless (meta != NULL);
181   fail_unless_equals_int (meta->level, level);
182   fail_unless (meta->voice_activity == voice);
183 
184   g_free (data);
185   gst_buffer_unref (buffer);
186   gst_object_unref (ext);
187 }
188 
189 GST_END_TEST;
190 
GST_START_TEST(rtphdrext_client_audio_level_two_bytes)191 GST_START_TEST (rtphdrext_client_audio_level_two_bytes)
192 {
193   GstRTPHeaderExtension *ext;
194   GstRTPHeaderExtensionFlags flags;
195   GstBuffer *buffer;
196   guint8 *data;
197   gsize size, written;
198   GstAudioLevelMeta *meta;
199   guint8 level = 12;
200   gboolean voice = TRUE;
201 
202   ext = gst_rtp_header_extension_create_from_uri (URN);
203   fail_unless (ext != NULL);
204   gst_rtp_header_extension_set_id (ext, 1);
205 
206   flags = gst_rtp_header_extension_get_supported_flags (ext);
207   fail_unless (flags & GST_RTP_HEADER_EXTENSION_TWO_BYTE);
208 
209   buffer = gst_buffer_new ();
210   meta = gst_buffer_add_audio_level_meta (buffer, level, voice);
211 
212   size = gst_rtp_header_extension_get_max_size (ext, buffer);
213   fail_unless (size > 0);
214   data = g_malloc0 (size);
215   fail_unless (data != NULL);
216 
217   /* Write extension */
218   written =
219       gst_rtp_header_extension_write (ext, buffer,
220       GST_RTP_HEADER_EXTENSION_TWO_BYTE, buffer, data, size);
221   fail_unless (written == 2);
222 
223   /* Read it back */
224   fail_unless (gst_buffer_remove_meta (buffer, (GstMeta *) meta));
225   fail_unless (gst_rtp_header_extension_read (ext,
226           GST_RTP_HEADER_EXTENSION_TWO_BYTE, data, size, buffer));
227   meta = gst_buffer_get_audio_level_meta (buffer);
228   fail_unless (meta != NULL);
229   fail_unless_equals_int (meta->level, level);
230   fail_unless (meta->voice_activity == voice);
231 
232   g_free (data);
233   gst_buffer_unref (buffer);
234   gst_object_unref (ext);
235 }
236 
237 GST_END_TEST;
238 
GST_START_TEST(rtphdrext_client_audio_level_no_meta)239 GST_START_TEST (rtphdrext_client_audio_level_no_meta)
240 {
241   GstRTPHeaderExtension *ext;
242   GstBuffer *buffer;
243   guint8 *data;
244   gsize size, written;
245 
246   ext = gst_rtp_header_extension_create_from_uri (URN);
247   fail_unless (ext != NULL);
248   gst_rtp_header_extension_set_id (ext, 1);
249 
250   buffer = gst_buffer_new ();
251 
252   size = gst_rtp_header_extension_get_max_size (ext, buffer);
253   fail_unless (size > 0);
254   data = g_malloc0 (size);
255   fail_unless (data != NULL);
256 
257   written =
258       gst_rtp_header_extension_write (ext, buffer,
259       GST_RTP_HEADER_EXTENSION_ONE_BYTE, buffer, data, size);
260   fail_unless (written == 0);
261 
262   written =
263       gst_rtp_header_extension_write (ext, buffer,
264       GST_RTP_HEADER_EXTENSION_TWO_BYTE, buffer, data, size);
265   fail_unless (written == 0);
266 
267   g_free (data);
268   gst_buffer_unref (buffer);
269   gst_object_unref (ext);
270 }
271 
272 GST_END_TEST;
273 
GST_START_TEST(rtphdrext_client_audio_level_payloader_depayloader)274 GST_START_TEST (rtphdrext_client_audio_level_payloader_depayloader)
275 {
276   GstHarness *h;
277   GstBuffer *b;
278   GstFlowReturn fret;
279   GstAudioLevelMeta *meta;
280 
281   h = gst_harness_new_parse ("rtpL16pay ! "
282       "application/x-rtp, extmap-1=(string)< \"\", " URN " , \"vad=on\" >"
283       " ! rtpL16depay");
284 
285   gst_harness_set_src_caps_str (h, "audio/x-raw, rate=44100, channels=1,"
286       " layout=interleaved, format=S16BE");
287 
288   b = gst_buffer_new_allocate (NULL, 100, NULL);
289   gst_buffer_add_audio_level_meta (b, 12, TRUE);
290   fret = gst_harness_push (h, b);
291   fail_unless (fret == GST_FLOW_OK);
292 
293   b = gst_harness_pull (h);
294   meta = gst_buffer_get_audio_level_meta (b);
295 
296   fail_unless (meta != NULL);
297   fail_unless (meta->level == 12);
298   fail_unless (meta->voice_activity == TRUE);
299 
300   gst_buffer_unref (b);
301   gst_harness_teardown (h);
302 }
303 
304 GST_END_TEST;
305 
306 
GST_START_TEST(rtphdrext_client_audio_level_payloader_api)307 GST_START_TEST (rtphdrext_client_audio_level_payloader_api)
308 {
309   GstHarness *h;
310   GstRTPHeaderExtension *ext;
311   GstBuffer *b;
312   GstFlowReturn fret;
313   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
314   guint8 *data;
315   guint size;
316   guint8 level;
317   gboolean voice_activity;
318 
319   h = gst_harness_new ("rtpL16pay");
320   gst_harness_set_src_caps_str (h, "audio/x-raw, rate=44100, channels=1,"
321       " layout=interleaved, format=S16BE");
322 
323   ext = gst_rtp_header_extension_create_from_uri (URN);
324   gst_rtp_header_extension_set_id (ext, 2);
325   fail_unless (ext);
326   g_signal_emit_by_name (h->element, "add-extension", ext);
327 
328   b = gst_buffer_new_allocate (NULL, 100, NULL);
329   gst_buffer_add_audio_level_meta (b, 12, TRUE);
330   fret = gst_harness_push (h, b);
331   fail_unless (fret == GST_FLOW_OK);
332 
333   b = gst_harness_pull (h);
334   fail_unless (gst_rtp_buffer_map (b, GST_MAP_READ, &rtp));
335   fail_unless (gst_rtp_buffer_get_extension_onebyte_header (&rtp, 2, 0,
336           (gpointer *) & data, &size));
337   fail_unless (size == 1);
338   level = data[0] & 0x7F;
339   voice_activity = (data[0] & 0x80) >> 7;
340   fail_unless (level == 12);
341   fail_unless (voice_activity == TRUE);
342   gst_rtp_buffer_unmap (&rtp);
343   gst_buffer_unref (b);
344 
345   gst_object_unref (ext);
346   gst_harness_teardown (h);
347 }
348 
349 GST_END_TEST;
350 
351 
352 static Suite *
rtphdrext_client_audio_level_suite(void)353 rtphdrext_client_audio_level_suite (void)
354 {
355   Suite *s = suite_create ("rtphdrext_client_audio_level");
356   TCase *tc_chain = tcase_create ("general");
357 
358   suite_add_tcase (s, tc_chain);
359 
360   tcase_add_test (tc_chain, rtphdrext_client_audio_level_sdp);
361   tcase_add_test (tc_chain, rtphdrext_client_audio_level_one_byte);
362   tcase_add_test (tc_chain, rtphdrext_client_audio_level_two_bytes);
363   tcase_add_test (tc_chain, rtphdrext_client_audio_level_no_meta);
364   tcase_add_test (tc_chain, rtphdrext_client_audio_level_payloader_depayloader);
365   tcase_add_test (tc_chain, rtphdrext_client_audio_level_payloader_api);
366 
367   return s;
368 }
369 
370 GST_CHECK_MAIN (rtphdrext_client_audio_level)
371