1 /* GStreamer
2  *
3  * Copyright (C) 2011 Alessandro Decina <alessandro.d@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General 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 
21 #include <gst/check/gstcheck.h>
22 #include <string.h>
23 #include <gst/video/video.h>
24 
25 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
26     GST_PAD_SINK,
27     GST_PAD_ALWAYS,
28     GST_STATIC_CAPS_ANY);
29 
30 static GstStaticPadTemplate video_src_template = GST_STATIC_PAD_TEMPLATE ("src",
31     GST_PAD_SRC,
32     GST_PAD_ALWAYS,
33     GST_STATIC_CAPS ("video/x-h264")
34     );
35 
36 static GstStaticPadTemplate audio_src_template = GST_STATIC_PAD_TEMPLATE ("src",
37     GST_PAD_SRC,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("audio/mpeg")
40     );
41 
42 /* For ease of programming we use globals to keep refs for our floating
43  * src and sink pads we create; otherwise we always have to do get_pad,
44  * get_peer, and then remove references in every test function */
45 static GstPad *mysrcpad, *mysinkpad;
46 
47 #define AUDIO_CAPS_STRING "audio/mpeg, " \
48                         "channels = (int) 1, " \
49                         "rate = (int) 8000, " \
50                         "mpegversion = (int) 1, "\
51                         "parsed = (boolean) true "
52 #define VIDEO_CAPS_STRING "video/x-h264, " \
53                           "stream-format = (string) byte-stream, " \
54                           "alignment = (string) nal, " \
55                           "parsed = (boolean) true "
56 
57 #define KEYFRAME_DISTANCE 10
58 
59 typedef void (CheckOutputBuffersFunc) (GList * buffers);
60 
61 /* setup and teardown needs some special handling for muxer */
62 static GstPad *
setup_src_pad(GstElement * element,GstStaticPadTemplate * template,const gchar * sinkname,gchar ** padname)63 setup_src_pad (GstElement * element,
64     GstStaticPadTemplate * template, const gchar * sinkname, gchar ** padname)
65 {
66   GstPad *srcpad, *sinkpad;
67 
68   GST_DEBUG_OBJECT (element, "setting up sending pad");
69   /* sending pad */
70   srcpad = gst_pad_new_from_static_template (template, "src");
71   fail_if (srcpad == NULL, "Could not create a srcpad");
72   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
73 
74   if (!(sinkpad = gst_element_get_static_pad (element, sinkname)))
75     sinkpad = gst_element_get_request_pad (element, sinkname);
76   fail_if (sinkpad == NULL, "Could not get sink pad from %s",
77       GST_ELEMENT_NAME (element));
78   /* references are owned by: 1) us, 2) tsmux, 3) collect pads */
79   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
80   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
81       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
82   gst_object_unref (sinkpad);   /* because we got it higher up */
83 
84   /* references are owned by: 1) tsmux, 2) collect pads */
85   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
86 
87   if (padname)
88     *padname = g_strdup (GST_PAD_NAME (sinkpad));
89 
90   return srcpad;
91 }
92 
93 static void
teardown_src_pad(GstElement * element,const gchar * sinkname)94 teardown_src_pad (GstElement * element, const gchar * sinkname)
95 {
96   GstPad *srcpad, *sinkpad;
97 
98   /* clean up floating src pad */
99   if (!(sinkpad = gst_element_get_static_pad (element, sinkname)))
100     sinkpad = gst_element_get_request_pad (element, sinkname);
101   /* pad refs held by 1) tsmux 2) collectpads and 3) us (through _get) */
102   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
103   srcpad = gst_pad_get_peer (sinkpad);
104 
105   gst_pad_unlink (srcpad, sinkpad);
106   GST_DEBUG ("src %p", srcpad);
107 
108   /* after unlinking, pad refs still held by
109    * 1) tsmux and 2) collectpads and 3) us (through _get) */
110   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
111   gst_object_unref (sinkpad);
112   /* one more ref is held by element itself */
113 
114   /* pad refs held by both creator and this function (through _get_peer) */
115   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 2);
116   gst_object_unref (srcpad);
117   gst_object_unref (srcpad);
118 
119 }
120 
121 static GstElement *
setup_tsmux(GstStaticPadTemplate * srctemplate,const gchar * sinkname,gchar ** padname)122 setup_tsmux (GstStaticPadTemplate * srctemplate, const gchar * sinkname,
123     gchar ** padname)
124 {
125   GstElement *mux;
126 
127   GST_DEBUG ("setup_tsmux");
128   mux = gst_check_setup_element ("mpegtsmux");
129   mysrcpad = setup_src_pad (mux, srctemplate, sinkname, padname);
130   mysinkpad = gst_check_setup_sink_pad (mux, &sink_template);
131   gst_pad_set_active (mysrcpad, TRUE);
132   gst_pad_set_active (mysinkpad, TRUE);
133 
134   return mux;
135 }
136 
137 static void
cleanup_tsmux(GstElement * mux,const gchar * sinkname)138 cleanup_tsmux (GstElement * mux, const gchar * sinkname)
139 {
140   GST_DEBUG ("cleanup_mux");
141   gst_element_set_state (mux, GST_STATE_NULL);
142 
143   gst_pad_set_active (mysrcpad, FALSE);
144   gst_pad_set_active (mysinkpad, FALSE);
145   teardown_src_pad (mux, sinkname);
146   gst_check_teardown_sink_pad (mux);
147   gst_check_teardown_element (mux);
148 }
149 
150 static void
check_tsmux_pad(GstStaticPadTemplate * srctemplate,const gchar * src_caps_string,gint pes_id,gint pmt_id,const gchar * sinkname,CheckOutputBuffersFunc check_func,guint n_bufs,gssize input_buf_size,guint alignment)151 check_tsmux_pad (GstStaticPadTemplate * srctemplate,
152     const gchar * src_caps_string, gint pes_id, gint pmt_id,
153     const gchar * sinkname, CheckOutputBuffersFunc check_func, guint n_bufs,
154     gssize input_buf_size, guint alignment)
155 {
156   GstClockTime ts;
157   GstElement *mux;
158   GstBuffer *inbuffer, *outbuffer;
159   GstCaps *caps;
160   gint num_buffers;
161   gint i;
162   gint pmt_pid = -1, el_pid = -1, pcr_pid = -1, packets = 0;
163   gchar *padname;
164 
165   mux = setup_tsmux (srctemplate, sinkname, &padname);
166 
167   if (alignment != 0)
168     g_object_set (mux, "alignment", alignment, NULL);
169 
170   fail_unless (gst_element_set_state (mux,
171           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
172       "could not set to playing");
173 
174   caps = gst_caps_from_string (src_caps_string);
175   gst_check_setup_events (mysrcpad, mux, caps, GST_FORMAT_TIME);
176   gst_caps_unref (caps);
177 
178   ts = 0;
179   for (i = 0; i < n_bufs; ++i) {
180     GstFlowReturn flow;
181 
182     if (input_buf_size >= 0)
183       inbuffer = gst_buffer_new_and_alloc (input_buf_size);
184     else
185       inbuffer = gst_buffer_new_and_alloc (g_random_int_range (1, 49141));
186 
187     GST_BUFFER_TIMESTAMP (inbuffer) = ts;
188     ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
189 
190     if (i % KEYFRAME_DISTANCE == 0 && pes_id == 0xe0) {
191       GST_TRACE ("input keyframe");
192       GST_BUFFER_FLAG_UNSET (inbuffer, GST_BUFFER_FLAG_DELTA_UNIT);
193     } else {
194       GST_TRACE ("input delta");
195       GST_BUFFER_FLAG_SET (inbuffer, GST_BUFFER_FLAG_DELTA_UNIT);
196     }
197     flow = gst_pad_push (mysrcpad, inbuffer);
198     if (flow != GST_FLOW_OK)
199       fail ("Got %s flow instead of OK", gst_flow_get_name (flow));
200     ts += 40 * GST_MSECOND;
201   }
202 
203   if (check_func)
204     check_func (buffers);
205 
206   num_buffers = g_list_length (buffers);
207   /* all output might get aggregated */
208   fail_unless (num_buffers >= 1);
209 
210   /* collect buffers in adapter for convenience */
211   for (i = 0; i < num_buffers; ++i) {
212     guint8 *odata;
213     gint size;
214     GstMapInfo map;
215 
216     outbuffer = GST_BUFFER (buffers->data);
217     fail_if (outbuffer == NULL);
218     buffers = g_list_remove (buffers, outbuffer);
219     ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
220 
221     gst_buffer_map (outbuffer, &map, GST_MAP_READ);
222     odata = map.data;
223     size = map.size;
224     fail_unless (size % 188 == 0);
225 
226     for (; size; odata += 188, size -= 188) {
227       guint pid, y;
228       guint8 *data = odata;
229 
230       /* need sync_byte */
231       fail_unless (*data == 0x47);
232       data++;
233 
234       y = GST_READ_UINT16_BE (data);
235       pid = y & (0x1FFF);
236       data += 2;
237       GST_TRACE ("pid: %d", pid);
238 
239       y = (y >> 14) & 0x1;
240       /* only check packets with payload_start_indicator == 1 */
241       if (!y) {
242         GST_TRACE ("not at start");
243         continue;
244       }
245 
246       y = *data;
247       data++;
248 
249       if (y & 0x20) {
250         /* adaptation field */
251         y = *data;
252         data++;
253         data += y;
254         GST_TRACE ("adaptation %d", y);
255       }
256 
257       if (pid == 0) {
258         /* look for PAT */
259         /* pointer field */
260         y = *data;
261         data++;
262         data += y;
263         /* table_id */
264         y = *data;
265         data++;
266         fail_unless (y == 0x0);
267         /* skip */
268         data += 5;
269         /* section_number */
270         y = *data;
271         fail_unless (y == 0);
272         data++;
273         /* last_section_number */
274         y = *data;
275         fail_unless (y == 0);
276         data++;
277         /* program_number */
278         y = GST_READ_UINT16_BE (data);
279         fail_unless (y != 0);
280         data += 2;
281         /* program_map_PID */
282         y = GST_READ_UINT16_BE (data);
283         pmt_pid = y & 0x1FFF;
284         fail_unless (pmt_pid > 0x10 && pmt_pid != 0x1FF);
285       } else if (pid == pmt_pid) {
286         /* look for PMT */
287         /* pointer field */
288         y = *data;
289         data++;
290         data += y;
291         /* table_id */
292         y = *data;
293         data++;
294         fail_unless (y == 0x2);
295         /* skip */
296         data += 5;
297         /* section_number */
298         y = *data;
299         fail_unless (y == 0);
300         data++;
301         /* last_section_number */
302         y = *data;
303         fail_unless (y == 0);
304         data++;
305         /* PCR_PID */
306         y = GST_READ_UINT16_BE (data);
307         data += 2;
308         pcr_pid = y & 0x1FFF;
309         /* program_info_length */
310         y = GST_READ_UINT16_BE (data);
311         data += 2;
312         y = y & 0x0FFF;
313         data += y;
314         /* parsing only ES stream */
315         /* stream_type */
316         y = *data;
317         data++;
318         fail_unless (y == pmt_id);
319         /* elementary_PID */
320         y = GST_READ_UINT16_BE (data);
321         data += 2;
322         el_pid = y & 0x1FFF;
323         fail_unless (el_pid > 0x10 && el_pid != 0x1FF);
324       } else if (pid == el_pid) {
325         packets++;
326         /* expect to see a PES packet start */
327         y = GST_READ_UINT32_BE (data);
328         fail_unless (y >> 8 == 0x1);
329         /* stream_id */
330         y = y & 0xFF;
331         fail_unless ((pes_id & 0xF0) == (y & 0xF0));
332       }
333     }
334     gst_buffer_unmap (outbuffer, &map);
335     gst_buffer_unref (outbuffer);
336     outbuffer = NULL;
337   }
338 
339   fail_unless (pmt_pid > 0);
340   fail_unless (el_pid > 0);
341   fail_unless (pcr_pid == el_pid);
342   fail_unless (packets > 0);
343 
344   g_list_free (buffers);
345   buffers = NULL;
346 
347   cleanup_tsmux (mux, padname);
348   g_free (padname);
349 }
350 
351 
GST_START_TEST(test_video)352 GST_START_TEST (test_video)
353 {
354   check_tsmux_pad (&video_src_template, VIDEO_CAPS_STRING, 0xE0, 0x1b,
355       "sink_%d", NULL, 1, 1, 0);
356 }
357 
358 GST_END_TEST;
359 
360 
GST_START_TEST(test_audio)361 GST_START_TEST (test_audio)
362 {
363   check_tsmux_pad (&audio_src_template, AUDIO_CAPS_STRING, 0xC0, 0x03,
364       "sink_%d", NULL, 1, 1, 0);
365 }
366 
367 GST_END_TEST;
368 
369 
370 typedef struct _TestData
371 {
372   GstEvent *sink_event;
373   gint src_events;
374 } TestData;
375 
376 typedef struct _ThreadData
377 {
378   GstPad *pad;
379   GstBuffer *buffer;
380   GstFlowReturn flow_return;
381   GThread *thread;
382 } ThreadData;
383 
384 static gboolean
src_event(GstPad * pad,GstObject * parent,GstEvent * event)385 src_event (GstPad * pad, GstObject * parent, GstEvent * event)
386 {
387   TestData *data = (TestData *) gst_pad_get_element_private (pad);
388 
389   if (event->type == GST_EVENT_CUSTOM_UPSTREAM)
390     data->src_events += 1;
391 
392   gst_event_unref (event);
393   return TRUE;
394 }
395 
396 static gboolean
sink_event(GstPad * pad,GstObject * parent,GstEvent * event)397 sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
398 {
399   TestData *data = (TestData *) gst_pad_get_element_private (pad);
400 
401   if (event->type == GST_EVENT_CUSTOM_DOWNSTREAM)
402     data->sink_event = event;
403 
404   gst_event_unref (event);
405   return TRUE;
406 }
407 
408 static void
link_sinks(GstElement * mpegtsmux,GstPad ** src1,GstPad ** src2,GstPad ** src3,TestData * test_data)409 link_sinks (GstElement * mpegtsmux,
410     GstPad ** src1, GstPad ** src2, GstPad ** src3, TestData * test_data)
411 {
412   GstPad *mux_sink1, *mux_sink2, *mux_sink3;
413 
414   /* link 3 sink pads, 2 video 1 audio */
415   *src1 = gst_pad_new_from_static_template (&video_src_template, "src1");
416   gst_pad_set_active (*src1, TRUE);
417   gst_pad_set_element_private (*src1, test_data);
418   gst_pad_set_event_function (*src1, src_event);
419   mux_sink1 = gst_element_get_request_pad (mpegtsmux, "sink_1");
420   fail_unless (gst_pad_link (*src1, mux_sink1) == GST_PAD_LINK_OK);
421 
422   *src2 = gst_pad_new_from_static_template (&video_src_template, "src2");
423   gst_pad_set_active (*src2, TRUE);
424   gst_pad_set_element_private (*src2, test_data);
425   gst_pad_set_event_function (*src2, src_event);
426   mux_sink2 = gst_element_get_request_pad (mpegtsmux, "sink_2");
427   fail_unless (gst_pad_link (*src2, mux_sink2) == GST_PAD_LINK_OK);
428 
429   *src3 = gst_pad_new_from_static_template (&audio_src_template, "src3");
430   gst_pad_set_active (*src3, TRUE);
431   gst_pad_set_element_private (*src3, test_data);
432   gst_pad_set_event_function (*src3, src_event);
433   mux_sink3 = gst_element_get_request_pad (mpegtsmux, "sink_3");
434   fail_unless (gst_pad_link (*src3, mux_sink3) == GST_PAD_LINK_OK);
435 
436   gst_object_unref (mux_sink1);
437   gst_object_unref (mux_sink2);
438   gst_object_unref (mux_sink3);
439 }
440 
441 static void
link_src(GstElement * mpegtsmux,GstPad ** sink,TestData * test_data)442 link_src (GstElement * mpegtsmux, GstPad ** sink, TestData * test_data)
443 {
444   GstPad *mux_src;
445 
446   mux_src = gst_element_get_static_pad (mpegtsmux, "src");
447   *sink = gst_pad_new_from_static_template (&sink_template, "sink");
448   gst_pad_set_active (*sink, TRUE);
449   gst_pad_set_event_function (*sink, sink_event);
450   gst_pad_set_element_private (*sink, test_data);
451   fail_unless (gst_pad_link (mux_src, *sink) == GST_PAD_LINK_OK);
452 
453   gst_object_unref (mux_src);
454 }
455 
456 static void
setup_caps(GstElement * mpegtsmux,GstPad * src1,GstPad * src2,GstPad * src3)457 setup_caps (GstElement * mpegtsmux, GstPad * src1, GstPad * src2, GstPad * src3)
458 {
459   GstSegment segment;
460   GstCaps *caps;
461 
462   gst_segment_init (&segment, GST_FORMAT_TIME);
463 
464   caps = gst_caps_new_simple ("video/x-h264",
465       "stream-format", G_TYPE_STRING, "byte-stream",
466       "alignment", G_TYPE_STRING, "nal", NULL);
467   gst_pad_push_event (src1, gst_event_new_stream_start ("1"));
468   gst_pad_push_event (src1, gst_event_new_caps (caps));
469   gst_pad_push_event (src1, gst_event_new_segment (&segment));
470   gst_pad_push_event (src2, gst_event_new_stream_start ("2"));
471   gst_pad_push_event (src2, gst_event_new_caps (caps));
472   gst_pad_push_event (src2, gst_event_new_segment (&segment));
473   gst_caps_unref (caps);
474   caps = gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 4,
475       "stream-format", G_TYPE_STRING, "raw", "framed", G_TYPE_BOOLEAN, TRUE,
476       NULL);
477   gst_pad_push_event (src3, gst_event_new_stream_start ("3"));
478   gst_pad_push_event (src3, gst_event_new_caps (caps));
479   gst_pad_push_event (src3, gst_event_new_segment (&segment));
480   gst_caps_unref (caps);
481 }
482 
483 static gpointer
pad_push_thread(gpointer user_data)484 pad_push_thread (gpointer user_data)
485 {
486   ThreadData *data = (ThreadData *) user_data;
487 
488   data->flow_return = gst_pad_push (data->pad, data->buffer);
489 
490   return NULL;
491 }
492 
493 static ThreadData *
pad_push(GstPad * pad,GstBuffer * buffer,GstClockTime timestamp)494 pad_push (GstPad * pad, GstBuffer * buffer, GstClockTime timestamp)
495 {
496   ThreadData *data;
497 
498   data = g_new0 (ThreadData, 1);
499   data->pad = pad;
500   data->buffer = buffer;
501   GST_BUFFER_TIMESTAMP (buffer) = timestamp;
502   data->thread = g_thread_try_new ("gst-check", pad_push_thread, data, NULL);
503 
504   return data;
505 }
506 
GST_START_TEST(test_force_key_unit_event_downstream)507 GST_START_TEST (test_force_key_unit_event_downstream)
508 {
509   GstElement *mpegtsmux;
510   GstPad *sink;
511   GstPad *src1;
512   GstPad *src2;
513   GstPad *src3;
514   GstEvent *sink_event;
515   GstClockTime timestamp, stream_time, running_time;
516   gboolean all_headers = TRUE;
517   gint count = 0;
518   ThreadData *thread_data_1, *thread_data_2, *thread_data_3, *thread_data_4;
519   TestData test_data = { 0, };
520 
521   mpegtsmux = gst_check_setup_element ("mpegtsmux");
522 
523   link_src (mpegtsmux, &sink, &test_data);
524   link_sinks (mpegtsmux, &src1, &src2, &src3, &test_data);
525   gst_element_set_state (mpegtsmux, GST_STATE_PLAYING);
526   setup_caps (mpegtsmux, src1, src2, src3);
527 
528   /* send a force-key-unit event with running_time=2s */
529   timestamp = stream_time = running_time = 2 * GST_SECOND;
530   sink_event = gst_video_event_new_downstream_force_key_unit (timestamp,
531       stream_time, running_time, all_headers, count);
532 
533   fail_unless (gst_pad_push_event (src1, sink_event));
534   fail_unless (test_data.sink_event == NULL);
535 
536   /* push 4 buffers, make sure mpegtsmux handles the force-key-unit event when
537    * the buffer with the requested running time is collected */
538   thread_data_1 = pad_push (src1, gst_buffer_new (), 1 * GST_SECOND);
539   thread_data_2 = pad_push (src2, gst_buffer_new (), 2 * GST_SECOND);
540   thread_data_3 = pad_push (src3, gst_buffer_new (), 3 * GST_SECOND);
541 
542   g_thread_join (thread_data_1->thread);
543   fail_unless (test_data.sink_event == NULL);
544 
545   /* push again on src1 so that the buffer on src2 is collected */
546   thread_data_4 = pad_push (src1, gst_buffer_new (), 4 * GST_SECOND);
547 
548   g_thread_join (thread_data_2->thread);
549   fail_unless (test_data.sink_event != NULL);
550 
551   gst_element_set_state (mpegtsmux, GST_STATE_NULL);
552 
553   g_thread_join (thread_data_3->thread);
554   g_thread_join (thread_data_4->thread);
555 
556   g_free (thread_data_1);
557   g_free (thread_data_2);
558   g_free (thread_data_3);
559   g_free (thread_data_4);
560   gst_object_unref (src1);
561   gst_object_unref (src2);
562   gst_object_unref (src3);
563   gst_object_unref (sink);
564   gst_object_unref (mpegtsmux);
565 }
566 
567 GST_END_TEST;
568 
GST_START_TEST(test_force_key_unit_event_upstream)569 GST_START_TEST (test_force_key_unit_event_upstream)
570 {
571   GstElement *mpegtsmux;
572   GstPad *sink;
573   GstPad *src1;
574   GstPad *src2;
575   GstPad *src3;
576   GstClockTime timestamp, stream_time, running_time;
577   gboolean all_headers = TRUE;
578   gint count = 0;
579   TestData test_data = { 0, };
580   ThreadData *thread_data_1, *thread_data_2, *thread_data_3, *thread_data_4;
581   GstEvent *event;
582 
583   mpegtsmux = gst_check_setup_element ("mpegtsmux");
584 
585   link_src (mpegtsmux, &sink, &test_data);
586   link_sinks (mpegtsmux, &src1, &src2, &src3, &test_data);
587   gst_element_set_state (mpegtsmux, GST_STATE_PLAYING);
588   setup_caps (mpegtsmux, src1, src2, src3);
589 
590   /* send an upstream force-key-unit event with running_time=2s */
591   timestamp = stream_time = running_time = 2 * GST_SECOND;
592   event =
593       gst_video_event_new_upstream_force_key_unit (running_time, TRUE, count);
594   fail_unless (gst_pad_push_event (sink, event));
595 
596   fail_unless (test_data.sink_event == NULL);
597   fail_unless_equals_int (test_data.src_events, 3);
598 
599   /* send downstream events with unrelated seqnums */
600   event = gst_video_event_new_downstream_force_key_unit (timestamp,
601       stream_time, running_time, all_headers, count);
602   fail_unless (gst_pad_push_event (src1, event));
603   event = gst_video_event_new_downstream_force_key_unit (timestamp,
604       stream_time, running_time, all_headers, count);
605   fail_unless (gst_pad_push_event (src2, event));
606 
607   /* events should be skipped */
608   fail_unless (test_data.sink_event == NULL);
609 
610   /* push 4 buffers, make sure mpegtsmux handles the force-key-unit event when
611    * the buffer with the requested running time is collected */
612   thread_data_1 = pad_push (src1, gst_buffer_new (), 1 * GST_SECOND);
613   thread_data_2 = pad_push (src2, gst_buffer_new (), 2 * GST_SECOND);
614   thread_data_3 = pad_push (src3, gst_buffer_new (), 3 * GST_SECOND);
615 
616   g_thread_join (thread_data_1->thread);
617   fail_unless (test_data.sink_event == NULL);
618 
619   /* push again on src1 so that the buffer on src2 is collected */
620   thread_data_4 = pad_push (src1, gst_buffer_new (), 4 * GST_SECOND);
621 
622   g_thread_join (thread_data_2->thread);
623   fail_unless (test_data.sink_event != NULL);
624 
625   gst_element_set_state (mpegtsmux, GST_STATE_NULL);
626 
627   g_thread_join (thread_data_3->thread);
628   g_thread_join (thread_data_4->thread);
629 
630   g_free (thread_data_1);
631   g_free (thread_data_2);
632   g_free (thread_data_3);
633   g_free (thread_data_4);
634 
635   gst_object_unref (src1);
636   gst_object_unref (src2);
637   gst_object_unref (src3);
638   gst_object_unref (sink);
639   gst_object_unref (mpegtsmux);
640 }
641 
642 GST_END_TEST;
643 
644 static GstFlowReturn expected_flow;
645 
646 static GstFlowReturn
flow_test_stat_chain_func(GstPad * pad,GstObject * parent,GstBuffer * buffer)647 flow_test_stat_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer)
648 {
649   gst_buffer_unref (buffer);
650 
651   GST_INFO ("returning flow %s (%d)", gst_flow_get_name (expected_flow),
652       expected_flow);
653   return expected_flow;
654 }
655 
GST_START_TEST(test_propagate_flow_status)656 GST_START_TEST (test_propagate_flow_status)
657 {
658   GstElement *mux;
659   gchar *padname;
660   GstBuffer *inbuffer;
661   GstCaps *caps;
662   guint i;
663 
664   GstFlowReturn expected[] = { GST_FLOW_OK, GST_FLOW_FLUSHING, GST_FLOW_EOS,
665     GST_FLOW_NOT_NEGOTIATED, GST_FLOW_ERROR, GST_FLOW_NOT_SUPPORTED
666   };
667 
668   mux = setup_tsmux (&video_src_template, "sink_%d", &padname);
669   gst_pad_set_chain_function (mysinkpad, flow_test_stat_chain_func);
670 
671   fail_unless (gst_element_set_state (mux,
672           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
673       "could not set to playing");
674 
675   caps = gst_caps_from_string (VIDEO_CAPS_STRING);
676   gst_check_setup_events (mysrcpad, mux, caps, GST_FORMAT_TIME);
677   gst_caps_unref (caps);
678 
679   for (i = 0; i < G_N_ELEMENTS (expected); ++i) {
680     GstFlowReturn res;
681 
682     inbuffer = gst_buffer_new_and_alloc (1);
683     ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
684 
685     expected_flow = expected[i];
686     GST_INFO ("expecting flow %s (%d)", gst_flow_get_name (expected_flow),
687         expected_flow);
688 
689     GST_BUFFER_TIMESTAMP (inbuffer) = i * GST_SECOND;
690 
691     res = gst_pad_push (mysrcpad, inbuffer);
692 
693     fail_unless_equals_int (res, expected[i]);
694   }
695 
696   cleanup_tsmux (mux, padname);
697   g_free (padname);
698 }
699 
700 GST_END_TEST;
701 
GST_START_TEST(test_multiple_state_change)702 GST_START_TEST (test_multiple_state_change)
703 {
704   GstElement *mux;
705   gchar *padname;
706   GstSegment segment;
707   GstCaps *caps;
708   size_t i;
709 
710   /* it's just a sample of all possible permutations of all states and their
711    * transitions */
712   GstState states[] = { GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_PLAYING,
713     GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING, GST_STATE_NULL
714   };
715 
716   size_t num_transitions_to_test = 10;
717 
718   mux = setup_tsmux (&video_src_template, "sink_%d", &padname);
719   gst_pad_set_chain_function (mysinkpad, flow_test_stat_chain_func);
720   gst_segment_init (&segment, GST_FORMAT_TIME);
721 
722   caps = gst_caps_from_string (VIDEO_CAPS_STRING);
723   gst_check_setup_events (mysrcpad, mux, caps, GST_FORMAT_TIME);
724   gst_caps_unref (caps);
725 
726   for (i = 0; i < num_transitions_to_test; ++i) {
727     GstState next_state = states[i % G_N_ELEMENTS (states)];
728     fail_unless (gst_element_set_state (mux,
729             next_state) == GST_STATE_CHANGE_SUCCESS,
730         "could not set to %s", gst_element_state_get_name (next_state));
731 
732     /* push some buffers when playing - this triggers a lot of activity */
733     if (GST_STATE_PLAYING == next_state) {
734       GstBuffer *inbuffer;
735 
736       fail_unless (gst_pad_push_event (mysrcpad,
737               gst_event_new_segment (&segment)));
738 
739       inbuffer = gst_buffer_new_and_alloc (1);
740       ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
741 
742       expected_flow = GST_FLOW_OK;
743       GST_BUFFER_PTS (inbuffer) = 0;
744       fail_unless (GST_FLOW_OK == gst_pad_push (mysrcpad, inbuffer));
745     }
746   }
747 
748   cleanup_tsmux (mux, padname);
749   g_free (padname);
750 }
751 
752 GST_END_TEST;
753 
754 static void
test_align_check_output(GList * bufs)755 test_align_check_output (GList * bufs)
756 {
757   GST_LOG ("%u buffers", g_list_length (bufs));
758   while (bufs != NULL) {
759     GstBuffer *buf = bufs->data;
760     gsize size;
761 
762     size = gst_buffer_get_size (buf);
763     GST_LOG ("buffer, size = %5u", (guint) size);
764     fail_unless_equals_int (size, 7 * 188);
765     bufs = bufs->next;
766   }
767 }
768 
GST_START_TEST(test_align)769 GST_START_TEST (test_align)
770 {
771   check_tsmux_pad (&video_src_template, VIDEO_CAPS_STRING, 0xE0, 0x1b,
772       "sink_%d", test_align_check_output, 817, -1, 7);
773 }
774 
775 GST_END_TEST;
776 
777 static void
test_keyframe_propagation_check_output(GList * bufs)778 test_keyframe_propagation_check_output (GList * bufs)
779 {
780   guint keyframe_count = 0;
781 
782   GST_LOG ("%u buffers", g_list_length (bufs));
783   while (bufs != NULL) {
784     GstBuffer *buf = bufs->data;
785     gboolean keyunit;
786 
787     keyunit = !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
788 
789     if (keyunit)
790       ++keyframe_count;
791 
792     GST_LOG ("buffer, keyframe=%d", keyunit);
793     bufs = bufs->next;
794   }
795   fail_unless_equals_int (keyframe_count, 50 / KEYFRAME_DISTANCE);
796 }
797 
GST_START_TEST(test_keyframe_flag_propagation)798 GST_START_TEST (test_keyframe_flag_propagation)
799 {
800   check_tsmux_pad (&video_src_template, VIDEO_CAPS_STRING, 0xE0, 0x1b,
801       "sink_%d", test_keyframe_propagation_check_output, 50, -1, 0);
802 }
803 
804 GST_END_TEST;
805 
806 static Suite *
mpegtsmux_suite(void)807 mpegtsmux_suite (void)
808 {
809   Suite *s = suite_create ("mpegtsmux");
810   TCase *tc_chain = tcase_create ("general");
811 
812   suite_add_tcase (s, tc_chain);
813 
814   tcase_add_test (tc_chain, test_audio);
815   tcase_add_test (tc_chain, test_video);
816   tcase_add_test (tc_chain, test_force_key_unit_event_downstream);
817   tcase_add_test (tc_chain, test_force_key_unit_event_upstream);
818   tcase_add_test (tc_chain, test_propagate_flow_status);
819   tcase_add_test (tc_chain, test_multiple_state_change);
820   tcase_add_test (tc_chain, test_align);
821   tcase_add_test (tc_chain, test_keyframe_flag_propagation);
822 
823   return s;
824 }
825 
826 GST_CHECK_MAIN (mpegtsmux);
827