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