1 /* GStreamer
2 *
3 * Copyright (C) 2020 Pexip AS
4 * @author Havard Graff <havard@pexip.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/check/check.h>
26 #include <gst/check/gstharness.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28
29 #define buffer_from_array(a) gst_buffer_new_memdup (a, G_N_ELEMENTS (a))
30
31 static guint8 opus_data[] = {
32 0xf8, 0xb5, 0x0e, 0x7d, 0x91, 0xcc, 0x05, 0x82,
33 0x75, 0x72, 0x48, 0xbd, 0xd3, 0x22, 0x24, 0x2e,
34 0x59, 0x63, 0xf8, 0xff, 0x5d, 0x59, 0x27, 0xd8,
35 0xad, 0x4b, 0xe8, 0xd7, 0xfa, 0x99, 0xaa, 0x46,
36 0xb4, 0xf6, 0x29, 0x16, 0x21, 0x86, 0x2a, 0xb5,
37 0x83, 0x7d, 0x3a, 0xce, 0xb3, 0xee, 0x37, 0x3b,
38 0xf7, 0xb5, 0x03, 0xe7, 0x13, 0x3b, 0xf6, 0x90,
39 0x06, 0xea, 0x79, 0xbe, 0x89, 0xc3, 0x2b, 0x1f,
40 0x7f, 0x88, 0x5e, 0xe0, 0xe1, 0x88, 0x59, 0x47,
41 0x11, 0x10, 0x94, 0xab, 0x5d, 0xa6, 0x3f, 0x5d,
42 0xa7, 0xd7, 0x0e, 0x7d, 0x07, 0x85, 0x0d, 0x2f,
43 0x7b, 0x3f, 0xf7, 0xc1, 0x8c, 0xb2, 0xda, 0xac,
44 0x79, 0x15, 0xda, 0xc7, 0xd2, 0x6e, 0xcc, 0x88,
45 0x61, 0x29, 0xcd, 0x78, 0xf4, 0x6d, 0x1b, 0xa6,
46 0xe6, 0xd1, 0x7c, 0x76, 0xc2, 0x86, 0x78, 0x3c,
47 0xc2, 0x2e, 0x26, 0xd4, 0xdf, 0x7f, 0x3b, 0x98,
48 0x7a, 0x7c, 0xbe, 0x1a, 0x17, 0xd2, 0x2d, 0xa5,
49 0x90, 0x2a, 0x1b, 0x0b, 0x43, 0x65, 0x63, 0x37,
50 0xe5, 0x0d, 0x5c, 0x9c, 0x6c, 0x38, 0xef, 0x2a,
51 0xe8, 0x49, 0x47, 0x05, 0x6d, 0x83, 0xcf, 0x6d,
52 };
53
GST_START_TEST(test_pay_to_depay)54 GST_START_TEST (test_pay_to_depay)
55 {
56 GstHarness *h = gst_harness_new_parse ("rtpopuspay ! rtpopusdepay");
57 GstBuffer *buf = buffer_from_array (opus_data);
58 gst_harness_set_src_caps_str (h, "audio/x-opus,channel-mapping-family=0");
59 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, buf));
60 gst_buffer_unref (gst_harness_pull (h));
61
62 gst_harness_teardown (h);
63 }
64
65 GST_END_TEST
GST_START_TEST(test_depay_to_pay)66 GST_START_TEST (test_depay_to_pay)
67 {
68 GstHarness *h = gst_harness_new_parse ("rtpopusdepay ! rtpopuspay");
69 guint8 opus_rtp_buf[] = {
70 0x80, 0x60, 0x54, 0xfd, 0x3b, 0x5a, 0x93, 0xf9, 0x1c, 0x33, 0x2b, 0xbb,
71 };
72 GstBuffer *buf = buffer_from_array (opus_rtp_buf);
73 gst_harness_set_src_caps_str (h,
74 "application/x-rtp,encoding-name=OPUS,media=audio,clock-rate=48000,payload=96");
75 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, buf));
76 gst_buffer_unref (gst_harness_pull (h));
77
78 gst_harness_teardown (h);
79 }
80
81 GST_END_TEST;
GST_START_TEST(test_pay_to_depay_multichannel)82 GST_START_TEST (test_pay_to_depay_multichannel)
83 {
84 GstHarness *h = gst_harness_new_parse ("rtpopuspay ! rtpopusdepay");
85 GstBuffer *buf;
86 GstCaps *caps;
87 GstStructure *s;
88 const GValue *channel_mapping;
89 gint val, i;
90
91 static const int expected_channel_mapping[] = { 0, 4, 1, 2, 3, 5 };
92
93 buf = buffer_from_array (opus_data);
94
95 gst_harness_set_src_caps_str (h, "audio/x-opus,channel-mapping-family=1,"
96 "rate=48000,channels=6,stream-count=4,coupled-count=2,channel-mapping=<0,4,1,2,3,5>");
97 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, buf));
98 gst_buffer_unref (gst_harness_pull (h));
99
100 caps = gst_pad_get_current_caps (h->srcpad);
101 s = gst_caps_get_structure (caps, 0);
102
103 assert_equals_string (gst_structure_get_name (s), "audio/x-opus");
104
105 fail_unless (gst_structure_get_int (s, "rate", &val));
106 fail_unless_equals_int (val, 48000);
107 fail_unless (gst_structure_get_int (s, "channels", &val));
108 fail_unless_equals_int (val, 6);
109 fail_unless (gst_structure_get_int (s, "channel-mapping-family", &val));
110 fail_unless_equals_int (val, 1);
111 fail_unless (gst_structure_get_int (s, "stream-count", &val));
112 fail_unless_equals_int (val, 4);
113 fail_unless (gst_structure_get_int (s, "coupled-count", &val));
114 fail_unless_equals_int (val, 2);
115
116 channel_mapping = gst_structure_get_value (s, "channel-mapping");
117 g_assert (GST_VALUE_HOLDS_ARRAY (channel_mapping));
118
119 for (i = 0; i != gst_value_array_get_size (channel_mapping); ++i) {
120 fail_unless_equals_int (expected_channel_mapping[i],
121 g_value_get_int (gst_value_array_get_value (channel_mapping, i)));
122 }
123
124 gst_caps_unref (caps);
125 gst_harness_teardown (h);
126 }
127
128 GST_END_TEST;
GST_START_TEST(test_depay_to_pay_multichannel)129 GST_START_TEST (test_depay_to_pay_multichannel)
130 {
131 GstHarness *h = gst_harness_new_parse ("rtpopusdepay ! rtpopuspay");
132 guint8 opus_rtp_buf[] = {
133 0x80, 0x60, 0x54, 0xfd, 0x3b, 0x5a, 0x93, 0xf9, 0x1c, 0x33, 0x2b, 0xbb,
134 };
135 GstBuffer *buf;
136 GstCaps *caps;
137 GstStructure *s;
138 gint val;
139
140 buf = buffer_from_array (opus_rtp_buf);
141
142 gst_harness_set_src_caps_str (h,
143 "application/x-rtp,encoding-name=OPUS,media=audio,clock-rate=48000,payload=96,"
144 "encoding-params=6,num_streams=4,coupled_streams=2,channel_mapping=\"0,4,1,2,3,5\"");
145 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, buf));
146 gst_buffer_unref (gst_harness_pull (h));
147
148 caps = gst_pad_get_current_caps (h->srcpad);
149 s = gst_caps_get_structure (caps, 0);
150
151 assert_equals_string (gst_structure_get_name (s), "application/x-rtp");
152
153 fail_unless (gst_structure_get_int (s, "encoding-params", &val));
154 fail_unless_equals_int (val, 6);
155 fail_unless_equals_string (gst_structure_get_string (s, "channel_mapping"),
156 "0,4,1,2,3,5");
157 fail_unless (gst_structure_get_int (s, "num_streams", &val));
158 fail_unless_equals_int (val, 4);
159 fail_unless (gst_structure_get_int (s, "coupled_streams", &val));
160 fail_unless_equals_int (val, 2);
161
162 gst_caps_unref (caps);
163 gst_harness_teardown (h);
164 }
165
166 GST_END_TEST;
167
168 static Suite *
rtpopus_suite(void)169 rtpopus_suite (void)
170 {
171 Suite *s = suite_create ("rtpopus");
172 TCase *tc_chain;
173
174 suite_add_tcase (s, (tc_chain = tcase_create ("rtpopus")));
175 tcase_add_test (tc_chain, test_pay_to_depay);
176 tcase_add_test (tc_chain, test_depay_to_pay);
177 tcase_add_test (tc_chain, test_pay_to_depay_multichannel);
178 tcase_add_test (tc_chain, test_depay_to_pay_multichannel);
179
180 return s;
181 }
182
183 GST_CHECK_MAIN (rtpopus);
184