3 * unit test for videorate
5 * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
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.
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.
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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 #include <gst/check/gstcheck.h>
27 /* For ease of programming we use globals to keep refs for our floating
28 * src and sink pads we create; otherwise we always have to do get_pad,
29 * get_peer, and then remove references in every test function */
30 static GstPad *mysrcpad, *mysinkpad;
33 #define VIDEO_CAPS_TEMPLATE_STRING \
36 #define VIDEO_CAPS_STRING \
38 "width = (int) 320, " \
39 "height = (int) 240, " \
40 "framerate = (fraction) 25/1 , " \
41 "format = (fourcc) I420"
43 #define VIDEO_CAPS_NO_FRAMERATE_STRING \
45 "width = (int) 320, " \
46 "height = (int) 240, " \
47 "format = (fourcc) I420"
49 #define VIDEO_CAPS_NEWSIZE_STRING \
51 "width = (int) 240, " \
52 "height = (int) 120, " \
53 "framerate = (fraction) 25/1 , " \
54 "format = (fourcc) I420"
56 #define VIDEO_CAPS_UNUSUAL_FRAMERATE \
58 "width = (int) 240, " \
59 "height = (int) 120, " \
60 "framerate = (fraction) 999/7 , " \
61 "format = (fourcc) I420, " \
62 "color-matrix=(string)sdtv"
64 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
67 GST_STATIC_CAPS (VIDEO_CAPS_TEMPLATE_STRING)
69 static GstStaticPadTemplate downstreamsinktemplate =
70 GST_STATIC_PAD_TEMPLATE ("sink",
73 GST_STATIC_CAPS (VIDEO_CAPS_STRING)
75 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
78 GST_STATIC_CAPS (VIDEO_CAPS_TEMPLATE_STRING)
82 assert_videorate_stats (GstElement * videorate, const gchar * reason,
83 guint64 xin, guint64 xout, guint64 xdropped, guint64 xduplicated)
85 guint64 in, out, dropped, duplicated;
87 g_object_get (videorate, "in", &in, "out", &out, "drop", &dropped,
88 "duplicate", &duplicated, NULL);
89 #define _assert_equals_uint64(a, b) \
93 fail_unless(first == second, \
94 "%s: '" #a "' (%" G_GUINT64_FORMAT ") is not equal to " \
95 "expected '" #a"' (%" G_GUINT64_FORMAT ")", reason, first, second); \
99 _assert_equals_uint64 (in, xin);
100 _assert_equals_uint64 (out, xout);
101 _assert_equals_uint64 (dropped, xdropped);
102 _assert_equals_uint64 (duplicated, xduplicated);
106 setup_videorate_full (GstStaticPadTemplate * srctemplate,
107 GstStaticPadTemplate * sinktemplate)
109 GstElement *videorate;
111 GST_DEBUG ("setup_videorate");
112 videorate = gst_check_setup_element ("videorate");
113 mysrcpad = gst_check_setup_src_pad (videorate, srctemplate, NULL);
114 mysinkpad = gst_check_setup_sink_pad (videorate, sinktemplate, NULL);
115 gst_pad_set_active (mysrcpad, TRUE);
116 gst_pad_set_active (mysinkpad, TRUE);
122 setup_videorate (void)
124 return setup_videorate_full (&srctemplate, &sinktemplate);
128 cleanup_videorate (GstElement * videorate)
130 GST_DEBUG ("cleanup_videorate");
132 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
133 g_list_free (buffers);
136 gst_element_set_state (videorate, GST_STATE_NULL);
137 gst_element_get_state (videorate, NULL, NULL, GST_CLOCK_TIME_NONE);
138 gst_pad_set_active (mysrcpad, FALSE);
139 gst_pad_set_active (mysinkpad, FALSE);
140 gst_check_teardown_src_pad (videorate);
141 gst_check_teardown_sink_pad (videorate);
142 gst_check_teardown_element (videorate);
145 GST_START_TEST (test_one)
147 GstElement *videorate;
151 videorate = setup_videorate ();
152 fail_unless (gst_element_set_state (videorate,
153 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
154 "could not set to playing");
156 inbuffer = gst_buffer_new_and_alloc (4);
157 memset (GST_BUFFER_DATA (inbuffer), 0, 4);
158 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
159 gst_buffer_set_caps (inbuffer, caps);
160 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
161 gst_caps_unref (caps);
162 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
164 /* pushing gives away my reference ... */
165 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
166 /* ... and it is now stuck inside videorate */
167 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
168 fail_unless_equals_int (g_list_length (buffers), 0);
171 cleanup_videorate (videorate);
176 GST_START_TEST (test_more)
178 GstElement *videorate;
179 GstBuffer *first, *second, *third, *outbuffer;
184 videorate = setup_videorate ();
185 fail_unless (gst_element_set_state (videorate,
186 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
187 "could not set to playing");
188 assert_videorate_stats (videorate, "creation", 0, 0, 0, 0);
190 rand = g_rand_new ();
193 first = gst_buffer_new_and_alloc (4);
194 GST_BUFFER_TIMESTAMP (first) = 0;
195 /* it shouldn't matter what the offsets are, videorate produces perfect
197 GST_BUFFER_OFFSET (first) = g_rand_int (rand);
198 GST_BUFFER_OFFSET_END (first) = g_rand_int (rand);
199 memset (GST_BUFFER_DATA (first), 1, 4);
200 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
201 gst_buffer_set_caps (first, caps);
202 gst_caps_unref (caps);
203 ASSERT_BUFFER_REFCOUNT (first, "first", 1);
204 gst_buffer_ref (first);
206 /* pushing gives away my reference ... */
207 fail_unless (gst_pad_push (mysrcpad, first) == GST_FLOW_OK);
208 /* ... and it is now stuck inside videorate */
209 ASSERT_BUFFER_REFCOUNT (first, "first", 2);
210 fail_unless_equals_int (g_list_length (buffers), 0);
211 assert_videorate_stats (videorate, "first buffer", 1, 0, 0, 0);
213 /* second buffer; inbetween second and third output frame's timestamp */
214 second = gst_buffer_new_and_alloc (4);
215 GST_BUFFER_TIMESTAMP (second) = GST_SECOND * 3 / 50;
216 GST_BUFFER_OFFSET (first) = g_rand_int (rand);
217 GST_BUFFER_OFFSET_END (first) = g_rand_int (rand);
218 memset (GST_BUFFER_DATA (second), 2, 4);
219 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
220 gst_buffer_set_caps (second, caps);
221 gst_caps_unref (caps);
222 ASSERT_BUFFER_REFCOUNT (second, "second", 1);
223 gst_buffer_ref (second);
225 /* pushing gives away one of my references ... */
226 fail_unless (gst_pad_push (mysrcpad, second) == GST_FLOW_OK);
227 /* ... and it is now stuck inside videorate */
228 ASSERT_BUFFER_REFCOUNT (second, "second", 2);
230 /* ... and the first one is pushed out, with timestamp 0 */
231 fail_unless_equals_int (g_list_length (buffers), 1);
232 assert_videorate_stats (videorate, "second buffer", 2, 1, 0, 0);
233 ASSERT_BUFFER_REFCOUNT (first, "first", 2);
235 outbuffer = buffers->data;
236 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuffer), 0);
239 third = gst_buffer_new_and_alloc (4);
240 GST_BUFFER_TIMESTAMP (third) = GST_SECOND * 12 / 50;
241 GST_BUFFER_OFFSET (first) = g_rand_int (rand);
242 GST_BUFFER_OFFSET_END (first) = g_rand_int (rand);
243 memset (GST_BUFFER_DATA (third), 3, 4);
244 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
245 gst_buffer_set_caps (third, caps);
246 gst_caps_unref (caps);
247 ASSERT_BUFFER_REFCOUNT (third, "third", 1);
248 gst_buffer_ref (third);
250 /* pushing gives away my reference ... */
251 fail_unless (gst_pad_push (mysrcpad, third) == GST_FLOW_OK);
252 /* ... and it is now stuck inside videorate */
253 ASSERT_BUFFER_REFCOUNT (third, "third", 2);
255 /* submitting the third buffer has triggered flushing of three more frames */
256 assert_videorate_stats (videorate, "third buffer", 3, 4, 0, 2);
258 /* check timestamp and source correctness */
260 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (l->data), 0);
261 fail_unless_equals_int (GST_BUFFER_DATA (l->data)[0], 1);
262 fail_unless_equals_uint64 (GST_BUFFER_OFFSET (l->data), 0);
263 fail_unless_equals_uint64 (GST_BUFFER_OFFSET_END (l->data), 1);
266 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (l->data), GST_SECOND / 25);
267 fail_unless_equals_int (GST_BUFFER_DATA (l->data)[0], 2);
268 fail_unless_equals_uint64 (GST_BUFFER_OFFSET (l->data), 1);
269 fail_unless_equals_uint64 (GST_BUFFER_OFFSET_END (l->data), 2);
272 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (l->data),
273 GST_SECOND * 2 / 25);
274 fail_unless_equals_int (GST_BUFFER_DATA (l->data)[0], 2);
275 fail_unless_equals_uint64 (GST_BUFFER_OFFSET (l->data), 2);
276 fail_unless_equals_uint64 (GST_BUFFER_OFFSET_END (l->data), 3);
279 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (l->data),
280 GST_SECOND * 3 / 25);
281 fail_unless_equals_int (GST_BUFFER_DATA (l->data)[0], 2);
282 fail_unless_equals_uint64 (GST_BUFFER_OFFSET (l->data), 3);
283 fail_unless_equals_uint64 (GST_BUFFER_OFFSET_END (l->data), 4);
285 fail_unless_equals_int (g_list_length (buffers), 4);
286 /* one held by us, three held by each output frame taken from the second */
287 ASSERT_BUFFER_REFCOUNT (second, "second", 4);
290 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
292 /* submitting eos should flush out two more frames for tick 8 and 10 */
293 /* FIXME: right now it only flushes out one, so out is 5 instead of 6 ! */
294 assert_videorate_stats (videorate, "eos", 3, 5, 0, 2);
295 fail_unless_equals_int (g_list_length (buffers), 5);
299 gst_buffer_unref (first);
300 gst_buffer_unref (second);
301 gst_buffer_unref (third);
302 cleanup_videorate (videorate);
307 /* frames at 1, 0, 2 -> second one should be ignored */
308 GST_START_TEST (test_wrong_order_from_zero)
310 GstElement *videorate;
311 GstBuffer *first, *second, *third, *outbuffer;
314 videorate = setup_videorate ();
315 fail_unless (gst_element_set_state (videorate,
316 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
317 "could not set to playing");
318 assert_videorate_stats (videorate, "start", 0, 0, 0, 0);
321 first = gst_buffer_new_and_alloc (4);
322 GST_BUFFER_TIMESTAMP (first) = GST_SECOND;
323 memset (GST_BUFFER_DATA (first), 0, 4);
324 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
325 gst_buffer_set_caps (first, caps);
326 gst_caps_unref (caps);
327 ASSERT_BUFFER_REFCOUNT (first, "first", 1);
328 gst_buffer_ref (first);
330 GST_DEBUG ("pushing first buffer");
331 /* pushing gives away my reference ... */
332 fail_unless (gst_pad_push (mysrcpad, first) == GST_FLOW_OK);
333 /* ... and it is now stuck inside videorate */
334 ASSERT_BUFFER_REFCOUNT (first, "first", 2);
335 fail_unless_equals_int (g_list_length (buffers), 0);
336 assert_videorate_stats (videorate, "first", 1, 0, 0, 0);
339 second = gst_buffer_new_and_alloc (4);
340 GST_BUFFER_TIMESTAMP (second) = 0;
341 memset (GST_BUFFER_DATA (second), 0, 4);
342 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
343 gst_buffer_set_caps (second, caps);
344 gst_caps_unref (caps);
345 ASSERT_BUFFER_REFCOUNT (second, "second", 1);
346 gst_buffer_ref (second);
348 /* pushing gives away my reference ... */
349 fail_unless (gst_pad_push (mysrcpad, second) == GST_FLOW_OK);
350 /* ... and it is now dropped because it is too old */
351 ASSERT_BUFFER_REFCOUNT (second, "second", 1);
352 fail_unless_equals_int (g_list_length (buffers), 0);
354 /* ... and the first one is still there */
355 assert_videorate_stats (videorate, "second", 2, 0, 1, 0);
356 ASSERT_BUFFER_REFCOUNT (first, "first", 2);
359 third = gst_buffer_new_and_alloc (4);
360 GST_BUFFER_TIMESTAMP (third) = 2 * GST_SECOND;
361 memset (GST_BUFFER_DATA (third), 0, 4);
362 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
363 gst_buffer_set_caps (third, caps);
364 gst_caps_unref (caps);
365 ASSERT_BUFFER_REFCOUNT (third, "third", 1);
366 gst_buffer_ref (third);
368 /* pushing gives away my reference ... */
369 fail_unless (gst_pad_push (mysrcpad, third) == GST_FLOW_OK);
370 /* ... and it is now stuck inside videorate */
371 ASSERT_BUFFER_REFCOUNT (third, "third", 2);
373 /* and now the first one should be pushed once and dupped 24 + 13 times, to
374 * reach the half point between 1 s (first) and 2 s (third) */
375 fail_unless_equals_int (g_list_length (buffers), 38);
376 ASSERT_BUFFER_REFCOUNT (first, "first", 39);
377 ASSERT_BUFFER_REFCOUNT (second, "second", 1);
378 ASSERT_BUFFER_REFCOUNT (third, "third", 2);
379 assert_videorate_stats (videorate, "third", 3, 38, 1, 37);
381 /* verify last buffer */
382 outbuffer = g_list_last (buffers)->data;
383 fail_unless (GST_IS_BUFFER (outbuffer));
384 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuffer),
385 GST_SECOND * 37 / 25);
388 gst_buffer_unref (first);
389 gst_buffer_unref (second);
390 gst_buffer_unref (third);
391 cleanup_videorate (videorate);
396 /* send frames with 0, 1, 2, 0 seconds */
397 GST_START_TEST (test_wrong_order)
399 GstElement *videorate;
400 GstBuffer *first, *second, *third, *fourth, *outbuffer;
403 videorate = setup_videorate ();
404 fail_unless (gst_element_set_state (videorate,
405 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
406 "could not set to playing");
407 assert_videorate_stats (videorate, "start", 0, 0, 0, 0);
410 first = gst_buffer_new_and_alloc (4);
411 GST_BUFFER_TIMESTAMP (first) = 0;
412 memset (GST_BUFFER_DATA (first), 0, 4);
413 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
414 gst_buffer_set_caps (first, caps);
415 gst_caps_unref (caps);
416 ASSERT_BUFFER_REFCOUNT (first, "first", 1);
417 gst_buffer_ref (first);
419 GST_DEBUG ("pushing first buffer");
420 /* pushing gives away my reference ... */
421 fail_unless (gst_pad_push (mysrcpad, first) == GST_FLOW_OK);
422 /* ... and it is now stuck inside videorate */
423 ASSERT_BUFFER_REFCOUNT (first, "first", 2);
424 fail_unless_equals_int (g_list_length (buffers), 0);
425 assert_videorate_stats (videorate, "first", 1, 0, 0, 0);
428 second = gst_buffer_new_and_alloc (4);
429 GST_BUFFER_TIMESTAMP (second) = GST_SECOND;
430 memset (GST_BUFFER_DATA (second), 0, 4);
431 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
432 gst_buffer_set_caps (second, caps);
433 gst_caps_unref (caps);
434 ASSERT_BUFFER_REFCOUNT (second, "second", 1);
435 gst_buffer_ref (second);
437 /* pushing gives away my reference ... */
438 fail_unless (gst_pad_push (mysrcpad, second) == GST_FLOW_OK);
439 /* ... and it is now stuck inside videorate */
440 ASSERT_BUFFER_REFCOUNT (second, "second", 2);
441 /* and it created 13 output buffers as copies of the first frame */
442 fail_unless_equals_int (g_list_length (buffers), 13);
443 assert_videorate_stats (videorate, "second", 2, 13, 0, 12);
444 ASSERT_BUFFER_REFCOUNT (first, "first", 14);
447 third = gst_buffer_new_and_alloc (4);
448 GST_BUFFER_TIMESTAMP (third) = 2 * GST_SECOND;
449 memset (GST_BUFFER_DATA (third), 0, 4);
450 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
451 gst_buffer_set_caps (third, caps);
452 gst_caps_unref (caps);
453 ASSERT_BUFFER_REFCOUNT (third, "third", 1);
454 gst_buffer_ref (third);
456 /* pushing gives away my reference ... */
457 fail_unless (gst_pad_push (mysrcpad, third) == GST_FLOW_OK);
458 /* ... and it is now stuck inside videorate */
459 ASSERT_BUFFER_REFCOUNT (third, "third", 2);
461 /* submitting a frame with 2 seconds triggers output of 25 more frames */
462 fail_unless_equals_int (g_list_length (buffers), 38);
463 ASSERT_BUFFER_REFCOUNT (first, "first", 14);
464 ASSERT_BUFFER_REFCOUNT (second, "second", 26);
465 /* three frames submitted; two of them output as is, and 36 duplicated */
466 assert_videorate_stats (videorate, "third", 3, 38, 0, 36);
469 fourth = gst_buffer_new_and_alloc (4);
470 GST_BUFFER_TIMESTAMP (fourth) = 0;
471 memset (GST_BUFFER_DATA (fourth), 0, 4);
472 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
473 gst_buffer_set_caps (fourth, caps);
474 gst_caps_unref (caps);
475 ASSERT_BUFFER_REFCOUNT (fourth, "fourth", 1);
476 gst_buffer_ref (fourth);
478 /* pushing gives away my reference ... */
479 fail_unless (gst_pad_push (mysrcpad, fourth) == GST_FLOW_OK);
480 /* ... and it is dropped */
481 ASSERT_BUFFER_REFCOUNT (fourth, "fourth", 1);
483 fail_unless_equals_int (g_list_length (buffers), 38);
484 ASSERT_BUFFER_REFCOUNT (first, "first", 14);
485 ASSERT_BUFFER_REFCOUNT (second, "second", 26);
486 assert_videorate_stats (videorate, "fourth", 4, 38, 1, 36);
488 /* verify last buffer */
489 outbuffer = g_list_last (buffers)->data;
490 fail_unless (GST_IS_BUFFER (outbuffer));
491 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuffer),
492 GST_SECOND * 37 / 25);
496 gst_buffer_unref (first);
497 gst_buffer_unref (second);
498 gst_buffer_unref (third);
499 gst_buffer_unref (fourth);
500 cleanup_videorate (videorate);
506 /* if no framerate is negotiated, we should not be able to push a buffer */
507 GST_START_TEST (test_no_framerate)
509 GstElement *videorate;
513 videorate = setup_videorate ();
514 fail_unless (gst_element_set_state (videorate,
515 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
516 "could not set to playing");
518 inbuffer = gst_buffer_new_and_alloc (4);
519 memset (GST_BUFFER_DATA (inbuffer), 0, 4);
520 caps = gst_caps_from_string (VIDEO_CAPS_NO_FRAMERATE_STRING);
521 gst_buffer_set_caps (inbuffer, caps);
522 gst_caps_unref (caps);
523 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
525 /* take a ref so we can later check refcount */
526 gst_buffer_ref (inbuffer);
528 /* no framerate is negotiated so pushing should fail */
529 fail_if (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
530 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
531 gst_buffer_unref (inbuffer);
532 fail_unless_equals_int (g_list_length (buffers), 0);
535 cleanup_videorate (videorate);
540 /* This test outputs 2 buffers of same dimensions (320x240), then 1 buffer of
541 * differing dimensions (240x120), and then another buffer of previous
542 * dimensions (320x240) and checks that the 3 buffers output as a result have
543 * correct caps (first 2 with 320x240 and 3rd with 240x120).
545 GST_START_TEST (test_changing_size)
547 GstElement *videorate;
554 GstEvent *newsegment;
555 GstCaps *caps, *caps_newsize;
557 videorate = setup_videorate ();
558 fail_unless (gst_element_set_state (videorate,
559 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
560 "could not set to playing");
562 newsegment = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1,
564 fail_unless (gst_pad_push_event (mysrcpad, newsegment) == TRUE);
566 first = gst_buffer_new_and_alloc (4);
567 memset (GST_BUFFER_DATA (first), 0, 4);
568 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
569 GST_BUFFER_TIMESTAMP (first) = 0;
570 gst_buffer_set_caps (first, caps);
572 GST_DEBUG ("pushing first buffer");
573 fail_unless (gst_pad_push (mysrcpad, first) == GST_FLOW_OK);
576 second = gst_buffer_new_and_alloc (4);
577 GST_BUFFER_TIMESTAMP (second) = GST_SECOND / 25;
578 memset (GST_BUFFER_DATA (second), 0, 4);
579 gst_buffer_set_caps (second, caps);
581 fail_unless (gst_pad_push (mysrcpad, second) == GST_FLOW_OK);
582 fail_unless_equals_int (g_list_length (buffers), 1);
583 outbuf = buffers->data;
584 /* first buffer should be output here */
585 fail_unless (gst_caps_is_equal (GST_BUFFER_CAPS (outbuf), caps));
586 fail_unless (GST_BUFFER_TIMESTAMP (outbuf) == 0);
588 /* third buffer with new size */
589 third = gst_buffer_new_and_alloc (4);
590 GST_BUFFER_TIMESTAMP (third) = 2 * GST_SECOND / 25;
591 memset (GST_BUFFER_DATA (third), 0, 4);
592 caps_newsize = gst_caps_from_string (VIDEO_CAPS_NEWSIZE_STRING);
593 gst_buffer_set_caps (third, caps_newsize);
595 fail_unless (gst_pad_push (mysrcpad, third) == GST_FLOW_OK);
596 /* new caps flushed the internal state, no new output yet */
597 fail_unless_equals_int (g_list_length (buffers), 1);
598 outbuf = g_list_last (buffers)->data;
599 /* first buffer should be output here */
600 fail_unless (gst_caps_is_equal (GST_BUFFER_CAPS (outbuf), caps));
601 fail_unless (GST_BUFFER_TIMESTAMP (outbuf) == 0);
603 /* fourth buffer with original size */
604 fourth = gst_buffer_new_and_alloc (4);
605 GST_BUFFER_TIMESTAMP (fourth) = 3 * GST_SECOND / 25;
606 memset (GST_BUFFER_DATA (fourth), 0, 4);
607 gst_buffer_set_caps (fourth, caps);
609 fail_unless (gst_pad_push (mysrcpad, fourth) == GST_FLOW_OK);
610 fail_unless_equals_int (g_list_length (buffers), 1);
612 /* fifth buffer with original size */
613 fifth = gst_buffer_new_and_alloc (4);
614 GST_BUFFER_TIMESTAMP (fifth) = 4 * GST_SECOND / 25;
615 memset (GST_BUFFER_DATA (fifth), 0, 4);
616 gst_buffer_set_caps (fifth, caps);
618 fail_unless (gst_pad_push (mysrcpad, fifth) == GST_FLOW_OK);
619 /* all four missing buffers here, dups of fourth buffer */
620 fail_unless_equals_int (g_list_length (buffers), 4);
621 outbuf = g_list_last (buffers)->data;
622 /* third buffer should be output here */
623 fail_unless (GST_BUFFER_TIMESTAMP (outbuf) == 3 * GST_SECOND / 25);
624 fail_unless (gst_caps_is_equal (GST_BUFFER_CAPS (outbuf), caps));
626 gst_caps_unref (caps);
627 gst_caps_unref (caps_newsize);
628 cleanup_videorate (videorate);
633 GST_START_TEST (test_non_ok_flow)
635 GstElement *videorate;
640 videorate = setup_videorate ();
641 fail_unless (gst_element_set_state (videorate,
642 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
643 "could not set to playing");
645 buf = gst_buffer_new_and_alloc (4);
646 memset (GST_BUFFER_DATA (buf), 0, 4);
647 caps = gst_caps_from_string (VIDEO_CAPS_STRING);
648 gst_buffer_set_caps (buf, caps);
649 gst_caps_unref (caps);
650 ASSERT_BUFFER_REFCOUNT (buf, "inbuffer", 1);
652 /* push a few 'normal' buffers */
653 for (ts = 0; ts < 100 * GST_SECOND; ts += GST_SECOND / 33) {
656 inbuf = gst_buffer_copy (buf);
657 GST_BUFFER_TIMESTAMP (inbuf) = ts;
659 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuf), GST_FLOW_OK);
662 /* we should have buffers according to the output framerate of 25/1 */
663 fail_unless_equals_int (g_list_length (buffers), 100 * 25);
665 /* now deactivate pad so we get a WRONG_STATE flow return */
666 gst_pad_set_active (mysinkpad, FALSE);
668 /* push buffer on deactivated pad */
669 fail_unless (gst_buffer_is_metadata_writable (buf));
670 GST_BUFFER_TIMESTAMP (buf) = ts;
672 /* pushing gives away our reference */
673 fail_unless_equals_int (gst_pad_push (mysrcpad, buf), GST_FLOW_WRONG_STATE);
676 cleanup_videorate (videorate);
681 GST_START_TEST (test_upstream_caps_nego)
683 GstElement *videorate;
684 GstPad *videorate_pad;
685 GstCaps *expected_caps;
687 GstStructure *structure;
689 videorate = setup_videorate_full (&srctemplate, &downstreamsinktemplate);
690 fail_unless (gst_element_set_state (videorate,
691 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
692 "could not set to playing");
694 videorate_pad = gst_element_get_static_pad (videorate, "sink");
695 caps = gst_pad_get_caps (videorate_pad);
697 /* assemble the expected caps */
698 structure = gst_structure_from_string (VIDEO_CAPS_STRING, NULL);
699 expected_caps = gst_caps_new_empty ();
700 gst_caps_append_structure (expected_caps, structure);
701 structure = gst_structure_copy (structure);
702 gst_structure_set (structure, "framerate", GST_TYPE_FRACTION_RANGE,
703 0, 1, G_MAXINT, 1, NULL);
704 gst_caps_append_structure (expected_caps, structure);
706 fail_unless (gst_caps_is_equal (expected_caps, caps));
707 gst_caps_unref (caps);
708 gst_caps_unref (expected_caps);
709 gst_object_unref (videorate_pad);
712 cleanup_videorate (videorate);
718 GST_START_TEST (test_selected_caps)
720 GstElement *videorate;
721 GstElement *pipeline;
725 pipeline = gst_parse_launch ("videotestsrc num-buffers=1"
726 " ! identity ! videorate name=videorate0 ! " VIDEO_CAPS_UNUSUAL_FRAMERATE
727 " ! fakesink", NULL);
728 fail_if (pipeline == NULL);
729 videorate = gst_bin_get_by_name (GST_BIN (pipeline), "videorate0");
730 fail_if (videorate == NULL);
731 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
733 fail_if (gst_element_set_state (pipeline,
734 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE,
735 "could not set to playing");
737 msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
738 GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
739 fail_if (msg == NULL || GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
741 /* make sure upstream nego works right and videotestsrc has selected the
742 * caps we want downstream of videorate */
746 GstCaps *caps = NULL;
747 GstPad *videorate_pad;
749 videorate_pad = gst_element_get_static_pad (videorate, "sink");
750 g_object_get (videorate_pad, "caps", &caps, NULL);
751 fail_unless (caps != NULL);
753 GST_DEBUG ("negotiated caps: %" GST_PTR_FORMAT, caps);
755 s = gst_caps_get_structure (caps, 0);
756 val = gst_structure_get_value (s, "framerate");
757 fail_unless (val != NULL, "no framerate field in negotiated caps");
758 fail_unless (GST_VALUE_HOLDS_FRACTION (val));
759 fail_unless_equals_int (gst_value_get_fraction_numerator (val), 999);
760 fail_unless_equals_int (gst_value_get_fraction_denominator (val), 7);
762 gst_caps_unref (caps);
763 gst_object_unref (videorate_pad);
767 gst_object_unref (bus);
768 gst_message_unref (msg);
769 gst_element_set_state (pipeline, GST_STATE_NULL);
770 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
771 gst_object_unref (videorate);
772 gst_object_unref (pipeline);
778 videorate_suite (void)
780 Suite *s = suite_create ("videorate");
781 TCase *tc_chain = tcase_create ("general");
783 suite_add_tcase (s, tc_chain);
784 tcase_add_test (tc_chain, test_one);
785 tcase_add_test (tc_chain, test_more);
786 tcase_add_test (tc_chain, test_wrong_order_from_zero);
787 tcase_add_test (tc_chain, test_wrong_order);
788 tcase_add_test (tc_chain, test_no_framerate);
789 tcase_add_test (tc_chain, test_changing_size);
790 tcase_add_test (tc_chain, test_non_ok_flow);
791 tcase_add_test (tc_chain, test_upstream_caps_nego);
792 tcase_add_test (tc_chain, test_selected_caps);
797 GST_CHECK_MAIN (videorate)