1 /* GStreamer
2  *
3  * unit test for qtdemux
4  *
5  * Copyright (C) <2016> Edward Hervey <edward@centricular.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 "qtdemux.h"
24 #include <glib/gprintf.h>
25 
26 typedef struct
27 {
28   GstPad *srcpad;
29   guint expected_size;
30   GstClockTime expected_time;
31 } CommonTestData;
32 
33 static GstPadProbeReturn
qtdemux_probe(GstPad * pad,GstPadProbeInfo * info,CommonTestData * data)34 qtdemux_probe (GstPad * pad, GstPadProbeInfo * info, CommonTestData * data)
35 {
36   if (GST_IS_EVENT (GST_PAD_PROBE_INFO_DATA (info))) {
37     GstEvent *ev = GST_PAD_PROBE_INFO_EVENT (info);
38 
39     switch (GST_EVENT_TYPE (ev)) {
40       case GST_EVENT_SEGMENT:
41       {
42         const GstSegment *segment;
43         gst_event_parse_segment (ev, &segment);
44         fail_unless (GST_CLOCK_TIME_IS_VALID (segment->format));
45         fail_unless (GST_CLOCK_TIME_IS_VALID (segment->start));
46         fail_unless (GST_CLOCK_TIME_IS_VALID (segment->base));
47         fail_unless (GST_CLOCK_TIME_IS_VALID (segment->time));
48         fail_unless (GST_CLOCK_TIME_IS_VALID (segment->position));
49         break;
50       }
51         break;
52       default:
53         break;
54     }
55 
56     return GST_PAD_PROBE_OK;
57   } else if (GST_IS_BUFFER (GST_PAD_PROBE_INFO_DATA (info))) {
58     GstBuffer *buf = GST_PAD_PROBE_INFO_BUFFER (info);
59 
60     fail_unless_equals_int (gst_buffer_get_size (buf), data->expected_size);
61     fail_unless_equals_uint64 (GST_BUFFER_PTS (buf), data->expected_time);
62   }
63 
64   return GST_PAD_PROBE_DROP;
65 }
66 
67 static void
qtdemux_pad_added_cb(GstElement * element,GstPad * pad,CommonTestData * data)68 qtdemux_pad_added_cb (GstElement * element, GstPad * pad, CommonTestData * data)
69 {
70   data->srcpad = pad;
71   gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
72       (GstPadProbeCallback) qtdemux_probe, data, NULL);
73 }
74 
GST_START_TEST(test_qtdemux_input_gap)75 GST_START_TEST (test_qtdemux_input_gap)
76 {
77   GstElement *qtdemux;
78   GstPad *sinkpad;
79   CommonTestData data = { 0, };
80   GstBuffer *inbuf;
81   GstSegment segment;
82   GstEvent *event;
83   guint i, offset;
84   GstClockTime pts;
85 
86   /* The goal of this test is to check that qtdemux can properly handle
87    * fragmented input from dashdemux, with gaps in it.
88    *
89    * Input segment :
90    *   - TIME
91    * Input buffers :
92    *   - The offset is set on buffers, it corresponds to the offset
93    *     within the current fragment.
94    *   - Buffer of the beginning of a fragment has the PTS set, others
95    *     don't.
96    *   - By extension, the beginning of a fragment also has an offset
97    *     of 0.
98    */
99 
100   qtdemux = gst_element_factory_make ("qtdemux", NULL);
101   gst_element_set_state (qtdemux, GST_STATE_PLAYING);
102   sinkpad = gst_element_get_static_pad (qtdemux, "sink");
103 
104   /* We'll want to know when the source pad is added */
105   g_signal_connect (qtdemux, "pad-added", (GCallback) qtdemux_pad_added_cb,
106       &data);
107 
108   /* Send the initial STREAM_START and segment (TIME) event */
109   event = gst_event_new_stream_start ("TEST");
110   GST_DEBUG ("Pushing stream-start event");
111   fail_unless (gst_pad_send_event (sinkpad, event) == TRUE);
112   gst_segment_init (&segment, GST_FORMAT_TIME);
113   event = gst_event_new_segment (&segment);
114   GST_DEBUG ("Pushing segment event");
115   fail_unless (gst_pad_send_event (sinkpad, event) == TRUE);
116 
117   /* Feed the init buffer, should create the source pad */
118   inbuf = gst_buffer_new_and_alloc (init_mp4_len);
119   gst_buffer_fill (inbuf, 0, init_mp4, init_mp4_len);
120   GST_BUFFER_PTS (inbuf) = 0;
121   GST_BUFFER_OFFSET (inbuf) = 0;
122   GST_DEBUG ("Pushing header buffer");
123   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
124 
125   /* Now send the trun of the first fragment */
126   inbuf = gst_buffer_new_and_alloc (seg_1_moof_size);
127   gst_buffer_fill (inbuf, 0, seg_1_m4f, seg_1_moof_size);
128   GST_BUFFER_PTS (inbuf) = 0;
129   GST_BUFFER_OFFSET (inbuf) = 0;
130   /* We are simulating that this fragment can happen at any point */
131   GST_BUFFER_FLAG_SET (inbuf, GST_BUFFER_FLAG_DISCONT);
132   GST_DEBUG ("Pushing trun buffer");
133   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
134   fail_if (data.srcpad == NULL);
135 
136   /* We are now ready to send some buffers with gaps */
137   offset = seg_1_sample_0_offset;
138   pts = 0;
139 
140   GST_DEBUG ("Pushing gap'ed buffers");
141   for (i = 0; i < 129; i++) {
142     /* Let's send one every 3 */
143     if ((i % 3) == 0) {
144       GST_DEBUG ("Pushing buffer #%d offset:%" G_GUINT32_FORMAT, i, offset);
145       inbuf = gst_buffer_new_and_alloc (seg_1_sample_sizes[i]);
146       gst_buffer_fill (inbuf, 0, seg_1_m4f + offset, seg_1_sample_sizes[i]);
147       GST_BUFFER_OFFSET (inbuf) = offset;
148       GST_BUFFER_FLAG_SET (inbuf, GST_BUFFER_FLAG_DISCONT);
149       data.expected_time =
150           gst_util_uint64_scale (pts, GST_SECOND, seg_1_timescale);
151       data.expected_size = seg_1_sample_sizes[i];
152       fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
153     }
154     /* Finally move offset forward */
155     offset += seg_1_sample_sizes[i];
156     pts += seg_1_sample_duration;
157   }
158 
159   gst_object_unref (sinkpad);
160   gst_element_set_state (qtdemux, GST_STATE_NULL);
161   gst_object_unref (qtdemux);
162 }
163 
164 GST_END_TEST;
165 
166 typedef struct
167 {
168   GstPad *sinkpad;
169   GstPad *pending_pad;
170   GstEventType *expected_events;
171   guint step;
172   guint total_step;
173   guint expected_num_srcpad;
174   guint num_srcpad;
175 } ReconfigTestData;
176 
177 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
178     GST_PAD_SINK,
179     GST_PAD_SOMETIMES,
180     GST_STATIC_CAPS_ANY);
181 
182 static gboolean
_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)183 _sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
184 {
185   gst_event_unref (event);
186   return TRUE;
187 }
188 
189 static GstFlowReturn
_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)190 _sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
191 {
192   gst_buffer_unref (buffer);
193   return GST_FLOW_OK;
194 }
195 
196 static GstPadProbeReturn
qtdemux_block_for_reconfig(GstPad * pad,GstPadProbeInfo * info,ReconfigTestData * data)197 qtdemux_block_for_reconfig (GstPad * pad, GstPadProbeInfo * info,
198     ReconfigTestData * data)
199 {
200   fail_unless (data->pending_pad);
201   fail_unless (data->pending_pad == pad);
202 
203   GST_DEBUG_OBJECT (pad, "Unblock pad");
204 
205   if (gst_pad_is_linked (data->sinkpad)) {
206     GstPad *peer = gst_pad_get_peer (data->sinkpad);
207     fail_unless (peer);
208     gst_pad_unlink (peer, data->sinkpad);
209   }
210 
211   fail_unless (gst_pad_link (data->pending_pad, data->sinkpad) ==
212       GST_PAD_LINK_OK);
213   data->pending_pad = NULL;
214 
215   return GST_PAD_PROBE_REMOVE;
216 }
217 
218 static GstPadProbeReturn
qtdemux_probe_for_reconfig(GstPad * pad,GstPadProbeInfo * info,ReconfigTestData * data)219 qtdemux_probe_for_reconfig (GstPad * pad, GstPadProbeInfo * info,
220     ReconfigTestData * data)
221 {
222   GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
223   GstEventType expected;
224 
225   if (data->step < data->total_step) {
226     expected = data->expected_events[data->step];
227   } else {
228     expected = GST_EVENT_UNKNOWN;
229   }
230 
231   GST_DEBUG ("Got event %p %s", event, GST_EVENT_TYPE_NAME (event));
232 
233   fail_unless (GST_EVENT_TYPE (event) == expected,
234       "Received unexpected event: %s (expected: %s)",
235       GST_EVENT_TYPE_NAME (event), gst_event_type_get_name (expected));
236   data->step++;
237 
238   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS && data->step < data->total_step) {
239     /* If current EOS is for draining, there should be pending srcpad */
240     fail_unless (data->pending_pad != NULL);
241   }
242 
243   return GST_PAD_PROBE_OK;
244 }
245 
246 static void
qtdemux_pad_added_cb_for_reconfig(GstElement * element,GstPad * pad,ReconfigTestData * data)247 qtdemux_pad_added_cb_for_reconfig (GstElement * element, GstPad * pad,
248     ReconfigTestData * data)
249 {
250   data->num_srcpad++;
251 
252   fail_unless (data->num_srcpad <= data->expected_num_srcpad);
253   fail_unless (data->pending_pad == NULL);
254 
255   GST_DEBUG_OBJECT (pad, "New pad added");
256 
257   data->pending_pad = pad;
258   gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
259       (GstPadProbeCallback) qtdemux_block_for_reconfig, data, NULL);
260 
261   if (!data->sinkpad) {
262     GstPad *sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
263 
264     gst_pad_set_event_function (sinkpad, _sink_event);
265     gst_pad_set_chain_function (sinkpad, _sink_chain);
266 
267     gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
268         (GstPadProbeCallback) qtdemux_probe_for_reconfig, data, NULL);
269     gst_pad_set_active (sinkpad, TRUE);
270     data->sinkpad = sinkpad;
271   }
272 }
273 
GST_START_TEST(test_qtdemux_duplicated_moov)274 GST_START_TEST (test_qtdemux_duplicated_moov)
275 {
276   GstElement *qtdemux;
277   GstPad *sinkpad;
278   ReconfigTestData data = { 0, };
279   GstBuffer *inbuf;
280   GstSegment segment;
281   GstEvent *event;
282   GstEventType expected[] = {
283     GST_EVENT_STREAM_START,
284     GST_EVENT_CAPS,
285     GST_EVENT_SEGMENT,
286     GST_EVENT_TAG,
287     GST_EVENT_TAG,
288     GST_EVENT_EOS
289   };
290 
291   data.expected_events = expected;
292   data.expected_num_srcpad = 1;
293   data.total_step = G_N_ELEMENTS (expected);
294 
295   /* The goal of this test is to check that qtdemux can properly handle
296    * duplicated moov without redundant events and pad exposing
297    *
298    * Testing step
299    *  - Push events stream-start and segment to qtdemux
300    *  - Push init and media data
301    *  - Push the same init and media data again
302    *
303    * Expected behaviour
304    *  - Expose srcpad only once
305    *  - No additional downstream events when the second init and media data is
306    *    pushed to qtdemux
307    */
308 
309   qtdemux = gst_element_factory_make ("qtdemux", NULL);
310   gst_element_set_state (qtdemux, GST_STATE_PLAYING);
311   sinkpad = gst_element_get_static_pad (qtdemux, "sink");
312 
313   /* We'll want to know when the source pad is added */
314   g_signal_connect (qtdemux, "pad-added", (GCallback)
315       qtdemux_pad_added_cb_for_reconfig, &data);
316 
317   /* Send the initial STREAM_START and segment (TIME) event */
318   event = gst_event_new_stream_start ("TEST");
319   GST_DEBUG ("Pushing stream-start event");
320   fail_unless (gst_pad_send_event (sinkpad, event) == TRUE);
321   gst_segment_init (&segment, GST_FORMAT_TIME);
322   event = gst_event_new_segment (&segment);
323   GST_DEBUG ("Pushing segment event");
324   fail_unless (gst_pad_send_event (sinkpad, event) == TRUE);
325 
326   /* Feed the init buffer, should create the source pad */
327   inbuf = gst_buffer_new_and_alloc (init_mp4_len);
328   gst_buffer_fill (inbuf, 0, init_mp4, init_mp4_len);
329   GST_BUFFER_PTS (inbuf) = 0;
330   GST_BUFFER_OFFSET (inbuf) = 0;
331   GST_DEBUG ("Pushing moov buffer");
332   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
333   fail_if (data.sinkpad == NULL);
334   fail_unless_equals_int (data.num_srcpad, 1);
335 
336   /* Now send the moof and mdat of the first fragment */
337   inbuf = gst_buffer_new_and_alloc (seg_1_m4f_len);
338   gst_buffer_fill (inbuf, 0, seg_1_m4f, seg_1_m4f_len);
339   GST_BUFFER_PTS (inbuf) = 0;
340   GST_BUFFER_OFFSET (inbuf) = 0;
341   GST_DEBUG ("Pushing moof and mdat buffer");
342   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
343 
344   /* Resend the init, moof and mdat, no additional event and pad are expected */
345   inbuf = gst_buffer_new_and_alloc (init_mp4_len);
346   gst_buffer_fill (inbuf, 0, init_mp4, init_mp4_len);
347   GST_BUFFER_PTS (inbuf) = 0;
348   GST_BUFFER_OFFSET (inbuf) = 0;
349   GST_BUFFER_FLAG_SET (inbuf, GST_BUFFER_FLAG_DISCONT);
350   GST_DEBUG ("Pushing moov buffer again");
351   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
352   fail_if (data.sinkpad == NULL);
353   fail_unless_equals_int (data.num_srcpad, 1);
354 
355   inbuf = gst_buffer_new_and_alloc (seg_1_m4f_len);
356   gst_buffer_fill (inbuf, 0, seg_1_m4f, seg_1_m4f_len);
357   GST_BUFFER_PTS (inbuf) = 0;
358   GST_BUFFER_OFFSET (inbuf) = init_mp4_len;
359   GST_DEBUG ("Pushing moof and mdat buffer again");
360   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
361   fail_unless (gst_pad_send_event (sinkpad, gst_event_new_eos ()) == TRUE);
362   fail_unless_equals_int (data.step, data.total_step);
363   fail_unless (data.pending_pad == NULL);
364 
365   gst_object_unref (sinkpad);
366   gst_pad_set_active (data.sinkpad, FALSE);
367   gst_object_unref (data.sinkpad);
368   gst_element_set_state (qtdemux, GST_STATE_NULL);
369   gst_object_unref (qtdemux);
370 }
371 
372 GST_END_TEST;
373 
GST_START_TEST(test_qtdemux_stream_change)374 GST_START_TEST (test_qtdemux_stream_change)
375 {
376   GstElement *qtdemux;
377   GstPad *sinkpad;
378   ReconfigTestData data = { 0, };
379   GstBuffer *inbuf;
380   GstSegment segment;
381   GstEvent *event;
382   const gchar *upstream_id;
383   const gchar *stream_id = NULL;
384   gchar *expected_stream_id = NULL;
385   guint track_id;
386   GstEventType expected[] = {
387     /* 1st group */
388     GST_EVENT_STREAM_START,
389     GST_EVENT_CAPS,
390     GST_EVENT_SEGMENT,
391     GST_EVENT_TAG,
392     GST_EVENT_TAG,
393     /* 2nd group (track-id change without upstream stream-start) */
394     GST_EVENT_EOS,
395     GST_EVENT_STREAM_START,
396     GST_EVENT_CAPS,
397     GST_EVENT_SEGMENT,
398     GST_EVENT_TAG,
399     GST_EVENT_TAG,
400     /* 3rd group (no track-id change with upstream stream-start) */
401     GST_EVENT_EOS,
402     GST_EVENT_STREAM_START,
403     GST_EVENT_CAPS,
404     GST_EVENT_SEGMENT,
405     GST_EVENT_TAG,
406     GST_EVENT_TAG,
407     /* last group (track-id change with upstream stream-start) */
408     GST_EVENT_EOS,
409     GST_EVENT_STREAM_START,
410     GST_EVENT_CAPS,
411     GST_EVENT_SEGMENT,
412     GST_EVENT_TAG,
413     GST_EVENT_TAG,
414     GST_EVENT_EOS
415   };
416 
417   data.expected_events = expected;
418   data.expected_num_srcpad = 4;
419   data.total_step = G_N_ELEMENTS (expected);
420 
421   /* The goal of this test is to check that qtdemux can properly handle
422    * stream change regardless of track-id change.
423    * This test is simulating DASH bitrate switching (for both playbin and plabyin3)
424    * and period-change for playbin3
425    *
426    * NOTE: During bitrate switching in DASH, track-id might be changed
427    * NOTE: stream change with new stream-start to qtdemux is playbin3 specific behaviour,
428    * because playbin configures new demux per period and existing demux never ever get
429    * new stream-start again.
430    *
431    * Testing step
432    *  [GROUP 1]
433    *  - Push events stream-start and segment to qtdemux
434    *  - Push init and media data to qtdemux
435    *  [GROUP 2]
436    *  - Push different (track-id change) init and media data to qtdemux
437    *    without new downstream sticky events to qtdemux
438    *  [GROUP 3]
439    *  - Push events stream-start and segment to qtdemux again
440    *  - Push the init and media data which are the same as GROUP 2
441    *  [GROUP 4]
442    *  - Push events stream-start and segment to qtdemux again
443    *  - Push different (track-id change) init and media data to qtdemux
444    *
445    * Expected behaviour
446    *  - Demux exposes srcpad four times, per test GROUP, regardless of track-id change
447    *  - Whenever exposing new pads, downstream sticky events should be detected
448    *    at demux srcpad
449    */
450 
451   qtdemux = gst_element_factory_make ("qtdemux", NULL);
452   gst_element_set_state (qtdemux, GST_STATE_PLAYING);
453   sinkpad = gst_element_get_static_pad (qtdemux, "sink");
454 
455   /* We'll want to know when the source pad is added */
456   g_signal_connect (qtdemux, "pad-added", (GCallback)
457       qtdemux_pad_added_cb_for_reconfig, &data);
458 
459   /***************
460    * TEST GROUP 1
461    * (track-id: 2)
462    **************/
463   /* Send the initial STREAM_START and segment (TIME) event */
464   upstream_id = "TEST-GROUP-1";
465   track_id = 2;
466   expected_stream_id = g_strdup_printf ("%s/%03u", upstream_id, track_id);
467   event = gst_event_new_stream_start (upstream_id);
468   GST_DEBUG ("Pushing stream-start event");
469   fail_unless (gst_pad_send_event (sinkpad, event) == TRUE);
470   gst_segment_init (&segment, GST_FORMAT_TIME);
471   event = gst_event_new_segment (&segment);
472   GST_DEBUG ("Pushing segment event");
473   fail_unless (gst_pad_send_event (sinkpad, event) == TRUE);
474 
475   /* Feed the init buffer, should create the source pad */
476   inbuf = gst_buffer_new_and_alloc (init_mp4_len);
477   gst_buffer_fill (inbuf, 0, init_mp4, init_mp4_len);
478   GST_BUFFER_PTS (inbuf) = 0;
479   GST_BUFFER_OFFSET (inbuf) = 0;
480   GST_DEBUG ("Pushing moov buffer");
481   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
482   fail_if (data.sinkpad == NULL);
483   fail_unless_equals_int (data.num_srcpad, 1);
484 
485   /* Check stream-id */
486   event = gst_pad_get_sticky_event (data.sinkpad, GST_EVENT_STREAM_START, 0);
487   fail_unless (event != NULL);
488   gst_event_parse_stream_start (event, &stream_id);
489   fail_unless_equals_string (stream_id, expected_stream_id);
490   g_free (expected_stream_id);
491   gst_event_unref (event);
492 
493   /* Now send the moof and mdat of the first fragment */
494   inbuf = gst_buffer_new_and_alloc (seg_1_m4f_len);
495   gst_buffer_fill (inbuf, 0, seg_1_m4f, seg_1_m4f_len);
496   GST_BUFFER_PTS (inbuf) = 0;
497   GST_BUFFER_OFFSET (inbuf) = init_mp4_len;
498   GST_DEBUG ("Pushing moof and mdat buffer");
499   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
500 
501 
502   /***************
503    * TEST GROUP 2
504    * (track-id: 1)
505    * - track-id change without new upstream stream-start event
506    **************/
507   /* Resend the init */
508   inbuf = gst_buffer_new_and_alloc (BBB_32k_init_mp4_len);
509   gst_buffer_fill (inbuf, 0, BBB_32k_init_mp4, BBB_32k_init_mp4_len);
510   GST_BUFFER_PTS (inbuf) = 0;
511   GST_BUFFER_OFFSET (inbuf) = 0;
512   GST_BUFFER_FLAG_SET (inbuf, GST_BUFFER_FLAG_DISCONT);
513   GST_DEBUG ("Pushing moov buffer again");
514   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
515   fail_if (data.sinkpad == NULL);
516   /* new srcpad should be exposed */
517   fail_unless_equals_int (data.num_srcpad, 2);
518 
519   /* Check stream-id */
520   upstream_id = "TEST-GROUP-1"; /* upstream-id does not changed from GROUP 1 */
521   track_id = 1;                 /* track-id is changed from 2 to 1 */
522   expected_stream_id = g_strdup_printf ("%s/%03u", upstream_id, track_id);
523   event = gst_pad_get_sticky_event (data.sinkpad, GST_EVENT_STREAM_START, 0);
524   fail_unless (event != NULL);
525   gst_event_parse_stream_start (event, &stream_id);
526   fail_unless_equals_string (stream_id, expected_stream_id);
527   g_free (expected_stream_id);
528   gst_event_unref (event);
529 
530   /* push the moof and mdat again */
531   inbuf = gst_buffer_new_and_alloc (BBB_32k_1_mp4_len);
532   gst_buffer_fill (inbuf, 0, BBB_32k_1_mp4, BBB_32k_1_mp4_len);
533   GST_BUFFER_PTS (inbuf) = 0;
534   GST_BUFFER_OFFSET (inbuf) = BBB_32k_init_mp4_len;
535   GST_DEBUG ("Pushing moof and mdat buffer");
536   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
537 
538   /***************
539    * TEST GROUP 3
540    * (track-id: 1)
541    * - Push new stream-start and segment to qtdemux
542    * - Reuse init and media data of GROUP 2 (no track-id change)
543    **************/
544   /* Send STREAM_START and segment (TIME) event */
545   upstream_id = "TEST-GROUP-3";
546   track_id = 1;
547   expected_stream_id = g_strdup_printf ("%s/%03u", upstream_id, track_id);
548   event = gst_event_new_stream_start (upstream_id);
549   GST_DEBUG ("Pushing stream-start event");
550   fail_unless (gst_pad_send_event (sinkpad, event) == TRUE);
551   gst_segment_init (&segment, GST_FORMAT_TIME);
552   event = gst_event_new_segment (&segment);
553   GST_DEBUG ("Pushing segment event");
554   fail_unless (gst_pad_send_event (sinkpad, event) == TRUE);
555 
556   /* Resend the init */
557   inbuf = gst_buffer_new_and_alloc (BBB_32k_init_mp4_len);
558   gst_buffer_fill (inbuf, 0, BBB_32k_init_mp4, BBB_32k_init_mp4_len);
559   GST_BUFFER_PTS (inbuf) = 0;
560   GST_BUFFER_OFFSET (inbuf) = 0;
561   GST_BUFFER_FLAG_SET (inbuf, GST_BUFFER_FLAG_DISCONT);
562   GST_DEBUG ("Pushing moov buffer again");
563   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
564   fail_if (data.sinkpad == NULL);
565   /* new srcpad should be exposed */
566   fail_unless_equals_int (data.num_srcpad, 3);
567 
568   /* Check stream-id */
569   event = gst_pad_get_sticky_event (data.sinkpad, GST_EVENT_STREAM_START, 0);
570   fail_unless (event != NULL);
571   gst_event_parse_stream_start (event, &stream_id);
572   fail_unless_equals_string (stream_id, expected_stream_id);
573   g_free (expected_stream_id);
574   gst_event_unref (event);
575 
576   /* push the moof and mdat again */
577   inbuf = gst_buffer_new_and_alloc (BBB_32k_1_mp4_len);
578   gst_buffer_fill (inbuf, 0, BBB_32k_1_mp4, BBB_32k_1_mp4_len);
579   GST_BUFFER_PTS (inbuf) = 0;
580   GST_BUFFER_OFFSET (inbuf) = BBB_32k_init_mp4_len;
581   GST_DEBUG ("Pushing moof and mdat buffer");
582   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
583 
584   /***************
585    * TEST GROUP 4
586    * (track-id: 2)
587    * - Push new stream-start and segment to qtdemux
588    * - track-id change from 1 to 2
589    **************/
590   /* Send STREAM_START and segment (TIME) event */
591   upstream_id = "TEST-GROUP-4";
592   track_id = 2;
593   expected_stream_id = g_strdup_printf ("%s/%03u", upstream_id, track_id);
594   event = gst_event_new_stream_start (upstream_id);
595   GST_DEBUG ("Pushing stream-start event");
596   fail_unless (gst_pad_send_event (sinkpad, event) == TRUE);
597   gst_segment_init (&segment, GST_FORMAT_TIME);
598   event = gst_event_new_segment (&segment);
599   GST_DEBUG ("Pushing segment event");
600   fail_unless (gst_pad_send_event (sinkpad, event) == TRUE);
601 
602   /* Resend the init */
603   inbuf = gst_buffer_new_and_alloc (init_mp4_len);
604   gst_buffer_fill (inbuf, 0, init_mp4, init_mp4_len);
605   GST_BUFFER_PTS (inbuf) = 0;
606   GST_BUFFER_OFFSET (inbuf) = 0;
607   GST_BUFFER_FLAG_SET (inbuf, GST_BUFFER_FLAG_DISCONT);
608   GST_DEBUG ("Pushing moov buffer again");
609   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
610   fail_if (data.sinkpad == NULL);
611   /* new srcpad should be exposed */
612   fail_unless_equals_int (data.num_srcpad, 4);
613 
614   /* Check stream-id */
615   event = gst_pad_get_sticky_event (data.sinkpad, GST_EVENT_STREAM_START, 0);
616   fail_unless (event != NULL);
617   gst_event_parse_stream_start (event, &stream_id);
618   fail_unless_equals_string (stream_id, expected_stream_id);
619   g_free (expected_stream_id);
620   gst_event_unref (event);
621 
622   /* push the moof and mdat again */
623   inbuf = gst_buffer_new_and_alloc (seg_1_m4f_len);
624   gst_buffer_fill (inbuf, 0, seg_1_m4f, seg_1_m4f_len);
625   GST_BUFFER_PTS (inbuf) = 0;
626   GST_BUFFER_OFFSET (inbuf) = init_mp4_len;
627   GST_DEBUG ("Pushing moof and mdat buffer again");
628   fail_unless (gst_pad_chain (sinkpad, inbuf) == GST_FLOW_OK);
629   fail_unless (gst_pad_send_event (sinkpad, gst_event_new_eos ()) == TRUE);
630   fail_unless_equals_int (data.step, data.total_step);
631   fail_unless (data.pending_pad == NULL);
632 
633   gst_object_unref (sinkpad);
634   gst_pad_set_active (data.sinkpad, FALSE);
635   gst_object_unref (data.sinkpad);
636   gst_element_set_state (qtdemux, GST_STATE_NULL);
637   gst_object_unref (qtdemux);
638 }
639 
640 GST_END_TEST;
641 
642 static Suite *
qtdemux_suite(void)643 qtdemux_suite (void)
644 {
645   Suite *s = suite_create ("qtdemux");
646   TCase *tc_chain = tcase_create ("general");
647 
648   suite_add_tcase (s, tc_chain);
649   tcase_add_test (tc_chain, test_qtdemux_input_gap);
650   tcase_add_test (tc_chain, test_qtdemux_duplicated_moov);
651   tcase_add_test (tc_chain, test_qtdemux_stream_change);
652 
653   return s;
654 }
655 
656 GST_CHECK_MAIN (qtdemux)
657