Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / check / elements / videorate.c
1 /* GStreamer
2  *
3  * unit test for videorate
4  *
5  * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <unistd.h>
24
25 #include <gst/check/gstcheck.h>
26
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;
31
32
33 #define VIDEO_CAPS_TEMPLATE_STRING     \
34     "video/x-raw-yuv"
35
36 #define VIDEO_CAPS_STRING               \
37     "video/x-raw-yuv, "                 \
38     "width = (int) 320, "               \
39     "height = (int) 240, "              \
40     "framerate = (fraction) 25/1 , "    \
41     "format = (fourcc) I420"
42
43 #define VIDEO_CAPS_NO_FRAMERATE_STRING  \
44     "video/x-raw-yuv, "                 \
45     "width = (int) 320, "               \
46     "height = (int) 240, "              \
47     "format = (fourcc) I420"
48
49 #define VIDEO_CAPS_NEWSIZE_STRING       \
50     "video/x-raw-yuv, "                 \
51     "width = (int) 240, "               \
52     "height = (int) 120, "              \
53     "framerate = (fraction) 25/1 , "    \
54     "format = (fourcc) I420"
55
56 #define VIDEO_CAPS_UNUSUAL_FRAMERATE    \
57     "video/x-raw-yuv, "                 \
58     "width = (int) 240, "               \
59     "height = (int) 120, "              \
60     "framerate = (fraction) 999/7 , "   \
61     "format = (fourcc) I420, "          \
62     "color-matrix=(string)sdtv"
63
64 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
65     GST_PAD_SINK,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS (VIDEO_CAPS_TEMPLATE_STRING)
68     );
69 static GstStaticPadTemplate downstreamsinktemplate =
70 GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS (VIDEO_CAPS_STRING)
74     );
75 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
76     GST_PAD_SRC,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS (VIDEO_CAPS_TEMPLATE_STRING)
79     );
80
81 static void
82 assert_videorate_stats (GstElement * videorate, const gchar * reason,
83     guint64 xin, guint64 xout, guint64 xdropped, guint64 xduplicated)
84 {
85   guint64 in, out, dropped, duplicated;
86
87   g_object_get (videorate, "in", &in, "out", &out, "drop", &dropped,
88       "duplicate", &duplicated, NULL);
89 #define _assert_equals_uint64(a, b)                                     \
90 G_STMT_START {                                                          \
91   guint64 first = a;                                                    \
92   guint64 second = 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); \
96 } G_STMT_END;
97
98
99   _assert_equals_uint64 (in, xin);
100   _assert_equals_uint64 (out, xout);
101   _assert_equals_uint64 (dropped, xdropped);
102   _assert_equals_uint64 (duplicated, xduplicated);
103 }
104
105 static GstElement *
106 setup_videorate_full (GstStaticPadTemplate * srctemplate,
107     GstStaticPadTemplate * sinktemplate)
108 {
109   GstElement *videorate;
110
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);
117
118   return videorate;
119 }
120
121 static GstElement *
122 setup_videorate (void)
123 {
124   return setup_videorate_full (&srctemplate, &sinktemplate);
125 }
126
127 static void
128 cleanup_videorate (GstElement * videorate)
129 {
130   GST_DEBUG ("cleanup_videorate");
131
132   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
133   g_list_free (buffers);
134   buffers = NULL;
135
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);
143 }
144
145 GST_START_TEST (test_one)
146 {
147   GstElement *videorate;
148   GstBuffer *inbuffer;
149   GstCaps *caps;
150
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");
155
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);
163
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);
169
170   /* cleanup */
171   cleanup_videorate (videorate);
172 }
173
174 GST_END_TEST;
175
176 GST_START_TEST (test_more)
177 {
178   GstElement *videorate;
179   GstBuffer *first, *second, *third, *outbuffer;
180   GList *l;
181   GstCaps *caps;
182   GRand *rand;
183
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);
189
190   rand = g_rand_new ();
191
192   /* first buffer */
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
196      streams */
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);
205
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);
212
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);
224
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);
229
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);
234
235   outbuffer = buffers->data;
236   fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuffer), 0);
237
238   /* third buffer */
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);
249
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);
254
255   /* submitting the third buffer has triggered flushing of three more frames */
256   assert_videorate_stats (videorate, "third buffer", 3, 4, 0, 2);
257
258   /* check timestamp and source correctness */
259   l = buffers;
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);
264
265   l = g_list_next (l);
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);
270
271   l = g_list_next (l);
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);
277
278   l = g_list_next (l);
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);
284
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);
288
289   /* now send EOS */
290   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
291
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);
296
297   /* cleanup */
298   g_rand_free (rand);
299   gst_buffer_unref (first);
300   gst_buffer_unref (second);
301   gst_buffer_unref (third);
302   cleanup_videorate (videorate);
303 }
304
305 GST_END_TEST;
306
307 /* frames at 1, 0, 2 -> second one should be ignored */
308 GST_START_TEST (test_wrong_order_from_zero)
309 {
310   GstElement *videorate;
311   GstBuffer *first, *second, *third, *outbuffer;
312   GstCaps *caps;
313
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);
319
320   /* first buffer */
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);
329
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);
337
338   /* second buffer */
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);
347
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);
353
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);
357
358   /* third buffer */
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);
367
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);
372
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);
380
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);
386
387   /* cleanup */
388   gst_buffer_unref (first);
389   gst_buffer_unref (second);
390   gst_buffer_unref (third);
391   cleanup_videorate (videorate);
392 }
393
394 GST_END_TEST;
395
396 /* send frames with 0, 1, 2, 0 seconds */
397 GST_START_TEST (test_wrong_order)
398 {
399   GstElement *videorate;
400   GstBuffer *first, *second, *third, *fourth, *outbuffer;
401   GstCaps *caps;
402
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);
408
409   /* first buffer */
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);
418
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);
426
427   /* second buffer */
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);
436
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);
445
446   /* third buffer */
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);
455
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);
460
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);
467
468   /* fourth buffer */
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);
477
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);
482
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);
487
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);
493
494
495   /* cleanup */
496   gst_buffer_unref (first);
497   gst_buffer_unref (second);
498   gst_buffer_unref (third);
499   gst_buffer_unref (fourth);
500   cleanup_videorate (videorate);
501 }
502
503 GST_END_TEST;
504
505
506 /* if no framerate is negotiated, we should not be able to push a buffer */
507 GST_START_TEST (test_no_framerate)
508 {
509   GstElement *videorate;
510   GstBuffer *inbuffer;
511   GstCaps *caps;
512
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");
517
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);
524
525   /* take a ref so we can later check refcount */
526   gst_buffer_ref (inbuffer);
527
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);
533
534   /* cleanup */
535   cleanup_videorate (videorate);
536 }
537
538 GST_END_TEST;
539
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).
544  */
545 GST_START_TEST (test_changing_size)
546 {
547   GstElement *videorate;
548   GstBuffer *first;
549   GstBuffer *second;
550   GstBuffer *third;
551   GstBuffer *fourth;
552   GstBuffer *fifth;
553   GstBuffer *outbuf;
554   GstEvent *newsegment;
555   GstCaps *caps, *caps_newsize;
556
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");
561
562   newsegment = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1,
563       0);
564   fail_unless (gst_pad_push_event (mysrcpad, newsegment) == TRUE);
565
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);
571
572   GST_DEBUG ("pushing first buffer");
573   fail_unless (gst_pad_push (mysrcpad, first) == GST_FLOW_OK);
574
575   /* second buffer */
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);
580
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);
587
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);
594
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);
602
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);
608
609   fail_unless (gst_pad_push (mysrcpad, fourth) == GST_FLOW_OK);
610   fail_unless_equals_int (g_list_length (buffers), 1);
611
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);
617
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));
625
626   gst_caps_unref (caps);
627   gst_caps_unref (caps_newsize);
628   cleanup_videorate (videorate);
629 }
630
631 GST_END_TEST;
632
633 GST_START_TEST (test_non_ok_flow)
634 {
635   GstElement *videorate;
636   GstClockTime ts;
637   GstBuffer *buf;
638   GstCaps *caps;
639
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");
644
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);
651
652   /* push a few 'normal' buffers */
653   for (ts = 0; ts < 100 * GST_SECOND; ts += GST_SECOND / 33) {
654     GstBuffer *inbuf;
655
656     inbuf = gst_buffer_copy (buf);
657     GST_BUFFER_TIMESTAMP (inbuf) = ts;
658
659     fail_unless_equals_int (gst_pad_push (mysrcpad, inbuf), GST_FLOW_OK);
660   }
661
662   /* we should have buffers according to the output framerate of 25/1 */
663   fail_unless_equals_int (g_list_length (buffers), 100 * 25);
664
665   /* now deactivate pad so we get a WRONG_STATE flow return */
666   gst_pad_set_active (mysinkpad, FALSE);
667
668   /* push buffer on deactivated pad */
669   fail_unless (gst_buffer_is_metadata_writable (buf));
670   GST_BUFFER_TIMESTAMP (buf) = ts;
671
672   /* pushing gives away our reference */
673   fail_unless_equals_int (gst_pad_push (mysrcpad, buf), GST_FLOW_WRONG_STATE);
674
675   /* cleanup */
676   cleanup_videorate (videorate);
677 }
678
679 GST_END_TEST;
680
681 GST_START_TEST (test_upstream_caps_nego)
682 {
683   GstElement *videorate;
684   GstPad *videorate_pad;
685   GstCaps *expected_caps;
686   GstCaps *caps;
687   GstStructure *structure;
688
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");
693
694   videorate_pad = gst_element_get_static_pad (videorate, "sink");
695   caps = gst_pad_get_caps (videorate_pad);
696
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);
705
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);
710
711   /* cleanup */
712   cleanup_videorate (videorate);
713 }
714
715 GST_END_TEST;
716
717
718 GST_START_TEST (test_selected_caps)
719 {
720   GstElement *videorate;
721   GstElement *pipeline;
722   GstBus *bus;
723   GstMessage *msg;
724
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));
732
733   fail_if (gst_element_set_state (pipeline,
734           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE,
735       "could not set to playing");
736
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);
740
741   /* make sure upstream nego works right and videotestsrc has selected the
742    * caps we want downstream of videorate */
743   {
744     GstStructure *s;
745     const GValue *val;
746     GstCaps *caps = NULL;
747     GstPad *videorate_pad;
748
749     videorate_pad = gst_element_get_static_pad (videorate, "sink");
750     g_object_get (videorate_pad, "caps", &caps, NULL);
751     fail_unless (caps != NULL);
752
753     GST_DEBUG ("negotiated caps: %" GST_PTR_FORMAT, caps);
754
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);
761
762     gst_caps_unref (caps);
763     gst_object_unref (videorate_pad);
764   }
765
766   /* cleanup */
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);
773 }
774
775 GST_END_TEST;
776
777 static Suite *
778 videorate_suite (void)
779 {
780   Suite *s = suite_create ("videorate");
781   TCase *tc_chain = tcase_create ("general");
782
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);
793
794   return s;
795 }
796
797 GST_CHECK_MAIN (videorate)