1 /* Gstreamer
2 * Copyright (C) <2014> Jesper Larsen <knorr.jesper@gmail.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 Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include <gst/check/gstcheck.h>
21 #include <gst/mpegts/mpegts.h>
22
23 static const guint8 pat_data_check[] = {
24 0x00, 0xB0, 0x11, 0x00, 0x00, 0xc1, 0x00,
25 0x00, 0x00, 0x00, 0xe0, 0x30, 0x00, 0x01,
26 0xe0, 0x31, 0x98, 0xdf, 0x37, 0xc4
27 };
28
29 static const guint8 pmt_data_check[] = {
30 0x02, 0xb0, 0x29, 0x00, 0x01, 0xc1, 0x00,
31 0x00, 0xff, 0xff, 0xf0, 0x06, 0x05, 0x04,
32 0x48, 0x44, 0x4d, 0x56, 0x1b, 0xe0, 0x40,
33 0xF0, 0x06, 0x05, 0x04, 0x48, 0x44, 0x4d,
34 0x56, 0x1b, 0xe0, 0x41, 0xF0, 0x06, 0x05,
35 0x04, 0x48, 0x44, 0x4d, 0x56, 0x15, 0x41,
36 0x5f, 0x5b
37 };
38
39 static const guint8 nit_data_check[] = {
40 0x40, 0xf0, 0x49, 0x1f, 0xff, 0xc1, 0x00,
41 0x00, 0xf0, 0x0e, 0x40, 0x0c, 0x4e, 0x65,
42 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x20, 0x6e,
43 0x61, 0x6d, 0x65, 0xf0, 0x2e, 0x1f, 0xff,
44 0x1f, 0xfe, 0xf0, 0x11, 0x40, 0x0f, 0x41,
45 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
46 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b,
47 0x1f, 0xff, 0x1f, 0xfe, 0xf0, 0x11, 0x40,
48 0x0f, 0x41, 0x6e, 0x6f, 0x74, 0x68, 0x65,
49 0x72, 0x20, 0x6e, 0x65, 0x74, 0x77, 0x6f,
50 0x72, 0x6b, 0xce, 0x03, 0xf5, 0x94
51 };
52
53 static const guint8 sdt_data_check[] = {
54 0x42, 0xf0, 0x38, 0x1f, 0xff, 0xc1, 0x00,
55 0x00, 0x1f, 0xff, 0xff, 0x00, 0x00, 0xFF,
56 0x90, 0x11, 0x48, 0x0f, 0x01, 0x08, 0x50,
57 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72,
58 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x00, 0x01,
59 0xFF, 0xB0, 0x11, 0x48, 0x0f, 0x01, 0x08,
60 0x50, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65,
61 0x72, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x25,
62 0xe5, 0x02, 0xd9
63 };
64
65 static const guint8 stt_data_check[] = {
66 0xcd, 0xf0, 0x11, 0x00, 0x00, 0xc1, 0x00,
67 0x00, 0x00, 0x23, 0xb4, 0xe6, 0x5C, 0x0c,
68 0xc0, 0x00, 0xc4, 0x86, 0x56, 0xa5
69 };
70
GST_START_TEST(test_mpegts_pat)71 GST_START_TEST (test_mpegts_pat)
72 {
73 GstMpegtsPatProgram *program;
74 GPtrArray *pat;
75 GstMpegtsSection *pat_section;
76 gint i;
77 guint8 *data;
78 gsize data_size;
79
80 /* Check creation of PAT */
81 pat = gst_mpegts_pat_new ();
82
83 for (i = 0; i < 2; i++) {
84 program = gst_mpegts_pat_program_new ();
85
86 program->program_number = i;
87 program->network_or_program_map_PID = 0x30 + i;
88
89 g_ptr_array_add (pat, program);
90 }
91
92 pat_section = gst_mpegts_section_from_pat (pat, 0);
93 fail_if (pat_section == NULL);
94
95 /* Re-parse the PAT from section */
96 pat = gst_mpegts_section_get_pat (pat_section);
97
98 fail_unless (pat->len == 2);
99
100 for (i = 0; i < 2; i++) {
101 program = g_ptr_array_index (pat, i);
102
103 assert_equals_int (program->program_number, i);
104 assert_equals_int (program->network_or_program_map_PID, 0x30 + i);
105 }
106 g_ptr_array_unref (pat);
107 pat = NULL;
108
109 /* Packetize the section, and check the data integrity */
110 data = gst_mpegts_section_packetize (pat_section, &data_size);
111 fail_if (data == NULL);
112
113 for (i = 0; i < data_size; i++) {
114 if (data[i] != pat_data_check[i])
115 fail ("0x%X != 0x%X in byte %d of PAT section", data[i],
116 pat_data_check[i], i);
117 }
118
119 /* Check assertion on bad CRC. Reset parsed data, and make the CRC corrupt */
120 pat_section->data[pat_section->section_length - 1]++;
121 pat_section->destroy_parsed (pat_section->cached_parsed);
122 pat_section->cached_parsed = NULL;
123
124 pat = gst_mpegts_section_get_pat (pat_section);
125 fail_unless (pat == NULL);
126
127 gst_mpegts_section_unref (pat_section);
128 }
129
130 GST_END_TEST;
131
GST_START_TEST(test_mpegts_pmt)132 GST_START_TEST (test_mpegts_pmt)
133 {
134 GstMpegtsPMT *pmt;
135 GstMpegtsPMTStream *stream;
136 GstMpegtsDescriptor *desc;
137 GstMpegtsSection *pmt_section;
138 guint8 *data;
139 gsize data_size;
140 gint i;
141
142 /* Check creation of PMT */
143 pmt = gst_mpegts_pmt_new ();
144
145 pmt->pcr_pid = 0x1FFF;
146 pmt->program_number = 1;
147
148 desc = gst_mpegts_descriptor_from_registration ("HDMV", NULL, 0);
149 g_ptr_array_add (pmt->descriptors, desc);
150
151 for (i = 0; i < 2; i++) {
152 stream = gst_mpegts_pmt_stream_new ();
153
154 stream->stream_type = GST_MPEGTS_STREAM_TYPE_VIDEO_H264;
155 stream->pid = 0x40 + i;
156
157 desc = gst_mpegts_descriptor_from_registration ("HDMV", NULL, 0);
158
159 g_ptr_array_add (stream->descriptors, desc);
160 g_ptr_array_add (pmt->streams, stream);
161 }
162
163 pmt_section = gst_mpegts_section_from_pmt (pmt, 0x30);
164 fail_if (pmt_section == NULL);
165
166 /* Re-parse the PMT from section */
167 pmt = (GstMpegtsPMT *) gst_mpegts_section_get_pmt (pmt_section);
168
169 fail_unless (pmt->pcr_pid == 0x1FFF);
170 fail_unless (pmt->program_number == 1);
171 fail_unless (pmt->descriptors->len == 1);
172 fail_unless (pmt->streams->len == 2);
173
174 desc = (GstMpegtsDescriptor *) gst_mpegts_find_descriptor (pmt->descriptors,
175 GST_MTS_DESC_REGISTRATION);
176 fail_if (desc == NULL);
177
178 for (i = 0; i < 2; i++) {
179 stream = g_ptr_array_index (pmt->streams, i);
180
181 fail_unless (stream->stream_type == GST_MPEGTS_STREAM_TYPE_VIDEO_H264);
182 fail_unless (stream->pid == 0x40 + i);
183 fail_unless (stream->descriptors->len == 1);
184
185 desc =
186 (GstMpegtsDescriptor *) gst_mpegts_find_descriptor (stream->descriptors,
187 GST_MTS_DESC_REGISTRATION);
188 fail_if (desc == NULL);
189 }
190
191 /* Packetize the section, and check data integrity */
192 data = gst_mpegts_section_packetize (pmt_section, &data_size);
193
194 for (i = 0; i < data_size; i++) {
195 if (data[i] != pmt_data_check[i])
196 fail ("0x%X != 0x%X in byte %d of PMT section", data[i],
197 pmt_data_check[i], i);
198 }
199
200 /* Check assertion on bad CRC. Reset parsed data, and make the CRC corrupt */
201 pmt_section->data[pmt_section->section_length - 1]++;
202 pmt_section->destroy_parsed (pmt_section->cached_parsed);
203 pmt_section->cached_parsed = NULL;
204 pmt = (GstMpegtsPMT *) gst_mpegts_section_get_pmt (pmt_section);
205
206 fail_unless (pmt == NULL);
207
208 gst_mpegts_section_unref (pmt_section);
209 }
210
211 GST_END_TEST;
212
213
GST_START_TEST(test_mpegts_nit)214 GST_START_TEST (test_mpegts_nit)
215 {
216 GstMpegtsNITStream *stream;
217 GstMpegtsNIT *nit;
218 GstMpegtsDescriptor *desc;
219 GstMpegtsSection *nit_section;
220 gchar *name;
221 guint8 *data;
222 gsize data_size;
223 gint i;
224
225 /* Check creation of NIT */
226 nit = gst_mpegts_nit_new ();
227
228 nit->actual_network = TRUE;
229 nit->network_id = 0x1FFF;
230
231 desc = gst_mpegts_descriptor_from_dvb_network_name ("Network name");
232
233 fail_if (desc == NULL);
234
235 g_ptr_array_add (nit->descriptors, desc);
236
237 for (i = 0; i < 2; i++) {
238 stream = gst_mpegts_nit_stream_new ();
239 stream->transport_stream_id = 0x1FFF;
240 stream->original_network_id = 0x1FFE;
241
242 desc = gst_mpegts_descriptor_from_dvb_network_name ("Another network");
243
244 g_ptr_array_add (stream->descriptors, desc);
245 g_ptr_array_add (nit->streams, stream);
246 }
247
248 nit_section = gst_mpegts_section_from_nit (nit);
249 fail_if (nit_section == NULL);
250
251 /* Re-parse the NIT from section */
252 nit = (GstMpegtsNIT *) gst_mpegts_section_get_nit (nit_section);
253
254 fail_unless (nit->descriptors->len == 1);
255 fail_unless (nit->streams->len == 2);
256 fail_unless (nit->actual_network == TRUE);
257 fail_unless (nit->network_id == 0x1FFF);
258
259 desc = (GstMpegtsDescriptor *) gst_mpegts_find_descriptor (nit->descriptors,
260 GST_MTS_DESC_DVB_NETWORK_NAME);
261
262 fail_if (desc == NULL);
263
264 fail_unless (gst_mpegts_descriptor_parse_dvb_network_name (desc,
265 &name) == TRUE);
266
267 g_free (name);
268
269 for (i = 0; i < 2; i++) {
270 stream = g_ptr_array_index (nit->streams, i);
271
272 fail_unless (stream->transport_stream_id == 0x1FFF);
273 fail_unless (stream->original_network_id == 0x1FFE);
274 fail_unless (stream->descriptors->len == 1);
275
276 desc =
277 (GstMpegtsDescriptor *) gst_mpegts_find_descriptor (stream->descriptors,
278 GST_MTS_DESC_DVB_NETWORK_NAME);
279
280 fail_unless (gst_mpegts_descriptor_parse_dvb_network_name (desc,
281 &name) == TRUE);
282 g_free (name);
283 }
284
285 /* Packetize the section, and check data integrity */
286 data = gst_mpegts_section_packetize (nit_section, &data_size);
287
288 fail_if (data == NULL);
289
290 for (i = 0; i < data_size; i++) {
291 if (data[i] != nit_data_check[i])
292 fail ("0x%X != 0x%X in byte %d of NIT section", data[i],
293 nit_data_check[i], i);
294 }
295
296 /* Check assertion on bad CRC. Reset parsed data, and make the CRC corrupt */
297 nit_section->data[nit_section->section_length - 1]++;
298 nit_section->destroy_parsed (nit_section->cached_parsed);
299 nit_section->cached_parsed = NULL;
300 nit = (GstMpegtsNIT *) gst_mpegts_section_get_nit (nit_section);
301
302 fail_unless (nit == NULL);
303
304 gst_mpegts_section_unref (nit_section);
305 }
306
307 GST_END_TEST;
308
GST_START_TEST(test_mpegts_sdt)309 GST_START_TEST (test_mpegts_sdt)
310 {
311 GstMpegtsSDTService *service;
312 GstMpegtsSDT *sdt;
313 GstMpegtsDescriptor *desc;
314 GstMpegtsSection *sdt_section;
315 guint8 *data;
316 gsize data_size;
317 gint i;
318
319 /* Check creation of SDT */
320 sdt = gst_mpegts_sdt_new ();
321
322 sdt->actual_ts = TRUE;
323 sdt->original_network_id = 0x1FFF;
324 sdt->transport_stream_id = 0x1FFF;
325
326 for (i = 0; i < 2; i++) {
327 service = gst_mpegts_sdt_service_new ();
328 service->service_id = i;
329 service->EIT_schedule_flag = TRUE;
330 service->EIT_present_following_flag = TRUE;
331 service->running_status = GST_MPEGTS_RUNNING_STATUS_RUNNING + i;
332 service->free_CA_mode = TRUE;
333
334 desc = gst_mpegts_descriptor_from_dvb_service
335 (GST_DVB_SERVICE_DIGITAL_TELEVISION, "Name", "Provider");
336
337 g_ptr_array_add (service->descriptors, desc);
338 g_ptr_array_add (sdt->services, service);
339 }
340
341 sdt_section = gst_mpegts_section_from_sdt (sdt);
342 fail_if (sdt_section == NULL);
343
344 /* Re-parse the SDT from section */
345 sdt = (GstMpegtsSDT *) gst_mpegts_section_get_sdt (sdt_section);
346
347 fail_unless (sdt->services->len == 2);
348 fail_unless (sdt->actual_ts == TRUE);
349 fail_unless (sdt->original_network_id == 0x1FFF);
350 fail_unless (sdt->transport_stream_id == 0x1FFF);
351
352 for (i = 0; i < 2; i++) {
353 service = g_ptr_array_index (sdt->services, i);
354
355 fail_if (service == NULL);
356 fail_unless (service->descriptors->len == 1);
357 fail_unless (service->service_id == i);
358 fail_unless (service->EIT_schedule_flag == TRUE);
359 fail_unless (service->EIT_present_following_flag == TRUE);
360 fail_unless (service->running_status ==
361 GST_MPEGTS_RUNNING_STATUS_RUNNING + i);
362 fail_unless (service->free_CA_mode == TRUE);
363
364 desc = (GstMpegtsDescriptor *)
365 gst_mpegts_find_descriptor (service->descriptors,
366 GST_MTS_DESC_DVB_SERVICE);
367
368 fail_if (desc == NULL);
369
370 fail_unless (gst_mpegts_descriptor_parse_dvb_service (desc,
371 NULL, NULL, NULL) == TRUE);
372 }
373
374 /* Packetize the section, and check data integrity */
375 data = gst_mpegts_section_packetize (sdt_section, &data_size);
376
377 fail_if (data == NULL);
378
379 for (i = 0; i < data_size; i++) {
380 if (data[i] != sdt_data_check[i])
381 fail ("0x%X != 0x%X in byte %d of SDT section", data[i],
382 sdt_data_check[i], i);
383 }
384
385 /* Check assertion on bad CRC. Reset parsed data, and make the CRC corrupt */
386 sdt_section->data[sdt_section->section_length - 1]++;
387 sdt_section->destroy_parsed (sdt_section->cached_parsed);
388 sdt_section->cached_parsed = NULL;
389 sdt = (GstMpegtsSDT *) gst_mpegts_section_get_sdt (sdt_section);
390
391 fail_unless (sdt == NULL);
392
393 gst_mpegts_section_unref (sdt_section);
394 }
395
396 GST_END_TEST;
397
GST_START_TEST(test_mpegts_atsc_stt)398 GST_START_TEST (test_mpegts_atsc_stt)
399 {
400 const GstMpegtsAtscSTT *stt;
401 GstMpegtsSection *section;
402 guint8 *data;
403 GstDateTime *dt;
404
405 data = g_memdup (stt_data_check, 20);
406
407 section = gst_mpegts_section_new (0x1ffb, data, 20);
408 stt = gst_mpegts_section_get_atsc_stt (section);
409 fail_if (stt == NULL);
410
411 fail_unless (stt->protocol_version == 0);
412 fail_unless (stt->system_time == 0x23b4e65c);
413 fail_unless (stt->gps_utc_offset == 12);
414 fail_unless (stt->ds_status == 1);
415 fail_unless (stt->ds_dayofmonth == 0);
416 fail_unless (stt->ds_hour == 0);
417
418 dt = gst_mpegts_atsc_stt_get_datetime_utc ((GstMpegtsAtscSTT *) stt);
419 fail_unless (gst_date_time_get_day (dt) == 30);
420 fail_unless (gst_date_time_get_month (dt) == 12);
421 fail_unless (gst_date_time_get_year (dt) == 1998);
422 fail_unless (gst_date_time_get_hour (dt) == 13);
423 fail_unless (gst_date_time_get_minute (dt) == 0);
424 fail_unless (gst_date_time_get_second (dt) == 0);
425
426 gst_date_time_unref (dt);
427 gst_mpegts_section_unref (section);
428 }
429
430 GST_END_TEST;
431
432
433 static const guint8 registration_descriptor[] = {
434 0x05, 0x04, 0x48, 0x44, 0x4d, 0x56
435 };
436
GST_START_TEST(test_mpegts_descriptors)437 GST_START_TEST (test_mpegts_descriptors)
438 {
439 GstMpegtsDescriptor *desc;
440 guint i;
441
442 /*
443 * Registration descriptor (0x05)
444 */
445
446 /* Check creation of descriptor */
447 desc = gst_mpegts_descriptor_from_registration ("HDMV", NULL, 0);
448 fail_if (desc == NULL);
449 fail_unless (desc->length == 4);
450 fail_unless (desc->tag == 0x05);
451 for (i = 0; i < 6; i++) {
452 if (registration_descriptor[i] != desc->data[i])
453 fail ("0x%X != 0x%X in byte %d of registration descriptor",
454 desc->data[i], registration_descriptor[i], i);
455 }
456 gst_mpegts_descriptor_free (desc);
457 }
458
459 GST_END_TEST;
460
461 static const guint8 network_name_descriptor[] = {
462 0x40, 0x04, 0x4e, 0x61, 0x6d, 0x65
463 };
464
465 static const guint8 service_descriptor[] = {
466 0x48, 0x0f, 0x01, 0x08, 0x50, 0x72, 0x6f,
467 0x76, 0x69, 0x64, 0x65, 0x72, 0x04, 0x4e,
468 0x61, 0x6d, 0x65
469 };
470
GST_START_TEST(test_mpegts_dvb_descriptors)471 GST_START_TEST (test_mpegts_dvb_descriptors)
472 {
473 GstMpegtsDescriptor *desc;
474 GstMpegtsDVBServiceType service_type;
475 gchar *string, *provider;
476 gchar long_string[257];
477 gboolean ret;
478 guint i;
479
480 /*
481 * Network name descriptor (0x40)
482 */
483
484 /* Check creation of descriptor */
485 desc = gst_mpegts_descriptor_from_dvb_network_name ("Name");
486 fail_if (desc == NULL);
487 fail_unless (desc->length == 4);
488 fail_unless (desc->tag == 0x40);
489
490 for (i = 0; i < 6; i++) {
491 if (desc->data[i] != network_name_descriptor[i])
492 fail ("0x%X != 0x%X in byte %d of network name descriptor",
493 desc->data[i], network_name_descriptor[i], i);
494 }
495
496 /* Check parsing of descriptor */
497 ret = gst_mpegts_descriptor_parse_dvb_network_name (desc, &string);
498 fail_unless (ret == TRUE);
499 fail_unless (strcmp (string, "Name") == 0);
500 g_free (string);
501 gst_mpegts_descriptor_free (desc);
502
503 /* Descriptor should fail if string is more than 255 bytes */
504 memset (long_string, 0x41, 256);
505 long_string[256] = 0x00;
506 fail_if (gst_mpegts_descriptor_from_dvb_network_name (long_string) != NULL);
507
508 /*
509 * Service descriptor (0x48)
510 */
511
512 /* Check creation of descriptor with data */
513 desc = gst_mpegts_descriptor_from_dvb_service
514 (GST_DVB_SERVICE_DIGITAL_TELEVISION, "Name", "Provider");
515 fail_if (desc == NULL);
516 fail_unless (desc->length == 15);
517 fail_unless (desc->tag == 0x48);
518
519 for (i = 0; i < 17; i++) {
520 if (desc->data[i] != service_descriptor[i])
521 fail ("0x%X != 0x%X in byte %d of service descriptor",
522 desc->data[i], service_descriptor[i], i);
523 }
524
525 /* Check parsing of descriptor with data */
526 ret = gst_mpegts_descriptor_parse_dvb_service
527 (desc, &service_type, &string, &provider);
528 fail_unless (ret == TRUE);
529 fail_unless (service_type == GST_DVB_SERVICE_DIGITAL_TELEVISION);
530 fail_unless (strcmp (string, "Name") == 0);
531 fail_unless (strcmp (provider, "Provider") == 0);
532 g_free (string);
533 g_free (provider);
534 gst_mpegts_descriptor_free (desc);
535
536 /* Check creation of descriptor without data */
537 desc = gst_mpegts_descriptor_from_dvb_service
538 (GST_DVB_SERVICE_DIGITAL_TELEVISION, NULL, NULL);
539 fail_if (desc == NULL);
540 fail_unless (desc->length == 3);
541 fail_unless (desc->tag == 0x48);
542
543 /* Check parsing of descriptor without data */
544 ret = gst_mpegts_descriptor_parse_dvb_service (desc, NULL, NULL, NULL);
545 fail_unless (ret == TRUE);
546 gst_mpegts_descriptor_free (desc);
547
548 /* Descriptor should fail if string is more than 255 bytes */
549 memset (long_string, 0x41, 256);
550 long_string[256] = 0x00;
551 desc =
552 gst_mpegts_descriptor_from_dvb_service
553 (GST_DVB_SERVICE_DIGITAL_TELEVISION, long_string, NULL);
554 fail_if (desc != NULL);
555 desc =
556 gst_mpegts_descriptor_from_dvb_service
557 (GST_DVB_SERVICE_DIGITAL_TELEVISION, NULL, long_string);
558 fail_if (desc != NULL);
559 }
560
561 GST_END_TEST;
562
563 static Suite *
mpegts_suite(void)564 mpegts_suite (void)
565 {
566 Suite *s = suite_create ("MPEG Transport Stream helper library");
567
568 TCase *tc_chain = tcase_create ("general");
569
570 gst_mpegts_initialize ();
571
572 suite_add_tcase (s, tc_chain);
573 tcase_add_test (tc_chain, test_mpegts_pat);
574 tcase_add_test (tc_chain, test_mpegts_pmt);
575 tcase_add_test (tc_chain, test_mpegts_nit);
576 tcase_add_test (tc_chain, test_mpegts_sdt);
577 tcase_add_test (tc_chain, test_mpegts_atsc_stt);
578 tcase_add_test (tc_chain, test_mpegts_descriptors);
579 tcase_add_test (tc_chain, test_mpegts_dvb_descriptors);
580
581 return s;
582 }
583
584 GST_CHECK_MAIN (mpegts);
585