1 /*
2 * GStreamer AVTP Plugin
3 * Copyright (C) 2019 Intel Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later
9 * 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 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
20 */
21 #include "../../../ext/avtp/gstavtpcrfsync.c"
22 #undef GST_CAT_DEFAULT
23
24 #include <avtp.h>
25 #include <avtp_crf.h>
26 #include <avtp_aaf.h>
27 #include <avtp_cvf.h>
28 #include <gst/check/gstcheck.h>
29 #include <gst/check/gstharness.h>
30 #include "../../../ext/avtp/gstavtpcrfutil.h"
31
32 #define STREAM_ID 0xDEADC0DEDEADC0DE
33
34 struct buffer_tstamps
35 {
36 GstClockTime buf_pts;
37 GstClockTime buf_dts;
38 GstClockTime avtp_ts;
39 GstClockTime h264_ts;
40 };
41
42 static GstHarness *
setup_harness(void)43 setup_harness (void)
44 {
45 GstHarness *h;
46
47 h = gst_harness_new_parse ("avtpcrfsync streamid=0xDEADC0DEDEADC0DE");
48 gst_harness_set_src_caps_str (h, "application/x-avtp");
49
50 return h;
51 }
52
53 static void
fill_buffer_video_data(struct avtp_stream_pdu * pdu)54 fill_buffer_video_data (struct avtp_stream_pdu *pdu)
55 {
56 const gint DATA_LEN = sizeof (guint32) + 3;
57
58 avtp_cvf_pdu_init (pdu, AVTP_CVF_FORMAT_SUBTYPE_H264);
59 avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_STREAM_ID, STREAM_ID);
60 avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_TV, 1);
61 avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_M, 1);
62 avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_TIMESTAMP, 0);
63 avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_H264_PTV, 1);
64 avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_H264_TIMESTAMP, 0);
65 avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_STREAM_DATA_LEN, DATA_LEN);
66 }
67
68 static void
fill_buffer_audio_data(struct avtp_stream_pdu * pdu)69 fill_buffer_audio_data (struct avtp_stream_pdu *pdu)
70 {
71 const int DATA_LEN = 4;
72
73 avtp_aaf_pdu_init (pdu);
74 avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_TV, 1);
75 avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_STREAM_ID, 0xDEADC0DEDEADC0DE);
76 avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_FORMAT, AVTP_AAF_FORMAT_INT_16BIT);
77 avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_NSR, AVTP_AAF_PCM_NSR_48KHZ);
78 avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_CHAN_PER_FRAME, 2);
79 avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_BIT_DEPTH, 16);
80 avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_SP, AVTP_AAF_PCM_SP_NORMAL);
81 avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_TIMESTAMP, 0);
82 avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_STREAM_DATA_LEN, DATA_LEN);
83 }
84
85 static GstBuffer *
create_input_buffer(GstHarness * h,guint32 subtype)86 create_input_buffer (GstHarness * h, guint32 subtype)
87 {
88 struct avtp_stream_pdu *pdu;
89 GstMapInfo info;
90 GstBuffer *buf;
91 const gint DATA_LEN = sizeof (guint32) + 3;
92
93 buf = gst_harness_create_buffer (h, sizeof (struct avtp_stream_pdu) +
94 DATA_LEN);
95
96 gst_buffer_map (buf, &info, GST_MAP_WRITE);
97 pdu = (struct avtp_stream_pdu *) info.data;
98
99 if (subtype == AVTP_SUBTYPE_AAF)
100 fill_buffer_audio_data (pdu);
101 else if (subtype == AVTP_SUBTYPE_CVF)
102 fill_buffer_video_data (pdu);
103
104 gst_buffer_unmap (buf, &info);
105
106 return buf;
107 }
108
109 static void
set_buffer_tstamps(GstBuffer * buf,struct buffer_tstamps * orig)110 set_buffer_tstamps (GstBuffer * buf, struct buffer_tstamps *orig)
111 {
112 struct avtp_stream_pdu *pdu;
113 GstMapInfo info;
114 guint32 type;
115 gint r;
116
117 gst_buffer_map (buf, &info, GST_MAP_WRITE);
118 pdu = (struct avtp_stream_pdu *) info.data;
119
120 GST_BUFFER_PTS (buf) = orig->buf_pts;
121 GST_BUFFER_DTS (buf) = orig->buf_dts;
122
123 r = avtp_pdu_get ((struct avtp_common_pdu *) pdu, AVTP_FIELD_SUBTYPE, &type);
124 g_assert (r == 0);
125 if (type == AVTP_SUBTYPE_AAF)
126 avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_TIMESTAMP, orig->avtp_ts);
127 else if (type == AVTP_SUBTYPE_CVF) {
128 avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_TIMESTAMP, orig->avtp_ts);
129 avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_H264_TIMESTAMP, orig->h264_ts);
130 }
131 gst_buffer_unmap (buf, &info);
132 }
133
134 static void
validate_tstamps(GstAvtpCrfBase * avtpcrfbase,GstBuffer * buf,struct buffer_tstamps * expected)135 validate_tstamps (GstAvtpCrfBase * avtpcrfbase, GstBuffer * buf,
136 struct buffer_tstamps *expected)
137 {
138 GstClockTime tstamp;
139 struct avtp_stream_pdu *pdu;
140 GstMapInfo info;
141 int res;
142
143 gst_buffer_map (buf, &info, GST_MAP_READ);
144 pdu = (struct avtp_stream_pdu *) info.data;
145
146 fail_unless_equals_uint64 (GST_BUFFER_PTS (buf), expected->buf_pts);
147 fail_unless_equals_uint64 (GST_BUFFER_DTS (buf), expected->buf_dts);
148
149 tstamp = get_avtp_tstamp (avtpcrfbase, pdu);
150 fail_unless_equals_uint64 (tstamp, expected->avtp_ts);
151
152 if (h264_tstamp_valid (pdu)) {
153 res = avtp_cvf_pdu_get (pdu, AVTP_CVF_FIELD_H264_TIMESTAMP, &tstamp);
154 fail_unless (res == 0);
155 fail_unless_equals_uint64 (tstamp, expected->h264_ts);
156 }
157
158 gst_buffer_unmap (buf, &info);
159 }
160
161 static void
test_crf_tstamps(GstHarness * h,GstBuffer * buf,struct buffer_tstamps * orig,struct buffer_tstamps * expected)162 test_crf_tstamps (GstHarness * h, GstBuffer * buf, struct buffer_tstamps *orig,
163 struct buffer_tstamps *expected)
164 {
165 GstAvtpCrfBase *avtpcrfbase;
166 GstBuffer *bufout;
167
168 avtpcrfbase = (GstAvtpCrfBase *) gst_harness_find_element (h, "avtpcrfsync");
169 set_buffer_tstamps (buf, orig);
170
171 bufout = gst_harness_push_and_pull (h, buf);
172
173 validate_tstamps (avtpcrfbase, bufout, expected);
174
175 gst_object_unref (avtpcrfbase);
176 }
177
GST_START_TEST(test_properties)178 GST_START_TEST (test_properties)
179 {
180 const guint64 streamid = 0xAABBCCDDEEFF0001;
181 const gchar *address = "01:AA:BB:CC:DD:EE";
182 const gchar *ifname = "enp1s0";
183 GstElement *element;
184 guint64 val64;
185 gchar *str;
186
187 element = gst_check_setup_element ("avtpcrfsync");
188
189 g_object_set (G_OBJECT (element), "ifname", ifname, NULL);
190 g_object_get (G_OBJECT (element), "ifname", &str, NULL);
191 fail_unless_equals_string (str, ifname);
192 g_free (str);
193
194 g_object_set (G_OBJECT (element), "address", address, NULL);
195 g_object_get (G_OBJECT (element), "address", &str, NULL);
196 fail_unless_equals_string (str, address);
197 g_free (str);
198
199 g_object_set (G_OBJECT (element), "streamid", streamid, NULL);
200 g_object_get (G_OBJECT (element), "streamid", &val64, NULL);
201 fail_unless_equals_uint64_hex (val64, streamid);
202
203 gst_object_unref (element);
204 }
205
206 GST_END_TEST;
207
GST_START_TEST(test_set_avtp_tstamp)208 GST_START_TEST (test_set_avtp_tstamp)
209 {
210 GstAvtpCrfSync *avtpcrfsync = g_object_new (GST_TYPE_AVTP_CRF_SYNC, NULL);
211 struct avtp_stream_pdu pdu;
212 GstClockTime tstamp;
213 int res;
214
215 avtp_aaf_pdu_init (&pdu);
216 avtp_aaf_pdu_set (&pdu, AVTP_AAF_FIELD_TV, 1);
217 set_avtp_tstamp (avtpcrfsync, &pdu, 12345);
218 res = avtp_aaf_pdu_get (&pdu, AVTP_AAF_FIELD_TIMESTAMP, &tstamp);
219 fail_unless (res == 0);
220 fail_unless_equals_uint64 (tstamp, 12345);
221
222 avtp_cvf_pdu_init (&pdu, AVTP_CVF_FORMAT_SUBTYPE_H264);
223 avtp_cvf_pdu_set (&pdu, AVTP_CVF_FIELD_TV, 1);
224 set_avtp_tstamp (avtpcrfsync, &pdu, 12345);
225 res = avtp_cvf_pdu_get (&pdu, AVTP_CVF_FIELD_TIMESTAMP, &tstamp);
226 fail_unless (res == 0);
227 fail_unless_equals_uint64 (tstamp, 12345);
228
229 g_object_unref (avtpcrfsync);
230 }
231
232 GST_END_TEST;
233
GST_START_TEST(test_set_avtp_mr_bit)234 GST_START_TEST (test_set_avtp_mr_bit)
235 {
236 GstAvtpCrfSync *avtpcrfsync = g_object_new (GST_TYPE_AVTP_CRF_SYNC, NULL);
237 struct avtp_stream_pdu pdu;
238 guint64 mr_bit;
239 int res;
240
241 avtp_aaf_pdu_init (&pdu);
242 set_avtp_mr_bit (avtpcrfsync, &pdu, 1);
243 res = avtp_aaf_pdu_get (&pdu, AVTP_AAF_FIELD_MR, &mr_bit);
244 fail_unless (res == 0);
245 fail_unless_equals_uint64 (mr_bit, 1);
246
247 avtp_cvf_pdu_init (&pdu, AVTP_CVF_FORMAT_SUBTYPE_H264);
248 set_avtp_mr_bit (avtpcrfsync, &pdu, 1);
249 res = avtp_cvf_pdu_get (&pdu, AVTP_CVF_FIELD_MR, &mr_bit);
250 fail_unless (res == 0);
251 fail_unless_equals_uint64 (mr_bit, 1);
252
253 g_object_unref (avtpcrfsync);
254 }
255
256 GST_END_TEST;
257
GST_START_TEST(test_crf_cvf_data)258 GST_START_TEST (test_crf_cvf_data)
259 {
260 struct buffer_tstamps orig, expected;
261 GstAvtpCrfBase *avtpcrfbase;
262 GstBuffer *buf;
263 GstHarness *h;
264
265 h = setup_harness ();
266
267 buf = create_input_buffer (h, AVTP_SUBTYPE_CVF);
268 avtpcrfbase = (GstAvtpCrfBase *) gst_harness_find_element (h, "avtpcrfsync");
269 avtpcrfbase->thread_data.average_period = 3300;
270 avtpcrfbase->thread_data.current_ts = 110000;
271 gst_object_unref (avtpcrfbase);
272
273 orig = (struct buffer_tstamps) {
274 .buf_pts = 103000,.buf_dts = 100000,.avtp_ts = 110000,.h264_ts = 108000};
275 expected = (struct buffer_tstamps) {
276 .buf_pts = 104204,.buf_dts = 100000,.avtp_ts = 110000,.h264_ts = 109204};
277 test_crf_tstamps (h, buf, &orig, &expected);
278
279 orig = (struct buffer_tstamps) {
280 .buf_pts = 107000,.buf_dts = 105000,.avtp_ts = 113000,.h264_ts = 118500};
281 expected = (struct buffer_tstamps) {
282 .buf_pts = 108400,.buf_dts = 105300,.avtp_ts = 113300,.h264_ts = 119900};
283 test_crf_tstamps (h, buf, &orig, &expected);
284
285 /* test for invalid DTS */
286 orig = (struct buffer_tstamps) {
287 .buf_pts = 107000,.buf_dts = GST_CLOCK_TIME_NONE,.avtp_ts =
288 113000,.h264_ts = 118500};
289 expected = (struct buffer_tstamps) {
290 .buf_pts = 108400,.buf_dts = GST_CLOCK_TIME_NONE,.avtp_ts =
291 113300,.h264_ts = 119900};
292 test_crf_tstamps (h, buf, &orig, &expected);
293
294 gst_buffer_unref (buf);
295 gst_harness_teardown (h);
296 }
297
298 GST_END_TEST;
299
GST_START_TEST(test_crf_aaf_data)300 GST_START_TEST (test_crf_aaf_data)
301 {
302 struct buffer_tstamps orig, expected;
303 GstAvtpCrfBase *avtpcrfbase;
304 GstBuffer *buf;
305 GstHarness *h;
306
307 h = setup_harness ();
308
309 buf = create_input_buffer (h, AVTP_SUBTYPE_AAF);
310 avtpcrfbase = (GstAvtpCrfBase *) gst_harness_find_element (h, "avtpcrfsync");
311 avtpcrfbase->thread_data.average_period = 3300;
312 avtpcrfbase->thread_data.current_ts = 110000;
313 gst_object_unref (avtpcrfbase);
314
315 orig = (struct buffer_tstamps) {
316 .buf_pts = 108000,.buf_dts = 0,.avtp_ts = 110000,.h264_ts = 0};
317 expected = (struct buffer_tstamps) {
318 .buf_pts = 108000,.buf_dts = 0,.avtp_ts = 110000,.h264_ts = 0};
319 test_crf_tstamps (h, buf, &orig, &expected);
320
321 orig = (struct buffer_tstamps) {
322 .buf_pts = 110000,.buf_dts = 0,.avtp_ts = 113000,.h264_ts = 0};
323 expected = (struct buffer_tstamps) {
324 .buf_pts = 110300,.buf_dts = 0,.avtp_ts = 113300,.h264_ts = 0};
325 test_crf_tstamps (h, buf, &orig, &expected);
326
327 gst_buffer_unref (buf);
328 gst_harness_teardown (h);
329 }
330
331 GST_END_TEST;
332
GST_START_TEST(test_crf_period_zero)333 GST_START_TEST (test_crf_period_zero)
334 {
335 struct buffer_tstamps orig, expected;
336 GstAvtpCrfBase *avtpcrfbase;
337 GstBuffer *buf;
338 GstHarness *h;
339
340 h = setup_harness ();
341
342 buf = create_input_buffer (h, AVTP_SUBTYPE_CVF);
343 avtpcrfbase = (GstAvtpCrfBase *) gst_harness_find_element (h, "avtpcrfsync");
344 avtpcrfbase->thread_data.average_period = 0.0;
345 avtpcrfbase->thread_data.current_ts = 110;
346 gst_object_unref (avtpcrfbase);
347
348 orig = (struct buffer_tstamps) {
349 .buf_pts = 100,.buf_dts = 105,.avtp_ts = 112,.h264_ts = 110};
350 expected = (struct buffer_tstamps) {
351 .buf_pts = 100,.buf_dts = 105,.avtp_ts = 112,.h264_ts = 110};
352 test_crf_tstamps (h, buf, &orig, &expected);
353
354 gst_buffer_unref (buf);
355 gst_harness_teardown (h);
356 }
357
358 GST_END_TEST;
359
360 static Suite *
avtpcrfsync_suite(void)361 avtpcrfsync_suite (void)
362 {
363 Suite *s = suite_create ("avtpcrfsync");
364 TCase *tc_chain = tcase_create ("general");
365
366 GST_DEBUG_CATEGORY_INIT (avtpcrfsync_debug, "avtpcrfsync", 0,
367 "CRF Synchronizer");
368
369 suite_add_tcase (s, tc_chain);
370 tcase_add_test (tc_chain, test_properties);
371 tcase_add_test (tc_chain, test_set_avtp_tstamp);
372 tcase_add_test (tc_chain, test_set_avtp_mr_bit);
373 tcase_add_test (tc_chain, test_crf_cvf_data);
374 tcase_add_test (tc_chain, test_crf_aaf_data);
375 tcase_add_test (tc_chain, test_crf_period_zero);
376
377 return s;
378 }
379
380 GST_CHECK_MAIN (avtpcrfsync);
381