Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / check / pipelines / capsfilter-renegotiation.c
1 /* GStreamer
2  * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
3  *
4  * capsfilter-renegotiation.c: Unit test for capsfilter caps renegotiation
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /* Ideally this would be in core, but using videotestsrc makes it easier */
23
24 #include <gst/check/gstcheck.h>
25
26 #define FIRST_CAPS  "video/x-raw-yuv,width=(int)480,height=(int)320"
27 #define SECOND_CAPS "video/x-raw-yuv,width=(int)120,height=(int)100"
28 #define THIRD_CAPS  "video/x-raw-yuv,width=(int)[10,50],height=(int)[100,200]"
29 #define FOURTH_CAPS "video/x-raw-rgb,width=(int)300,height=(int)[25,75];" \
30                     "video/x-raw-yuv,width=(int)[30,40]," \
31                     "height=(int)[100,200],format=(fourcc)YUY2"
32
33 int buffer_count = 0;
34 GstCaps *current_caps = NULL;
35 int caps_change = 0;
36
37 static gboolean
38 buffer_probe (GstPad * pad, GstMiniObject * obj, gpointer data)
39 {
40   GstBuffer *buffer;
41   GstCaps *buffer_caps;
42   GstElement *capsfilter = GST_ELEMENT (data);
43   GstCaps *caps = NULL;
44
45   buffer = GST_BUFFER (obj);
46
47   /* increment the buffer count and check if it is time to change the caps */
48   buffer_count++;
49   if (buffer_count == 50) {
50     /* change the caps to another one */
51     caps = gst_caps_from_string (SECOND_CAPS);
52   } else if (buffer_count == 100) {
53     /* change the caps to another one, this time unfixed */
54     caps = gst_caps_from_string (THIRD_CAPS);
55   } else if (buffer_count == 150) {
56     /* change the caps to another one,
57      * this time unfixed with multiple entries */
58     caps = gst_caps_from_string (FOURTH_CAPS);
59   }
60   /* set the caps */
61   if (caps) {
62     g_object_set (capsfilter, "caps", caps, NULL);
63     gst_caps_unref (caps);
64   }
65
66   /* now check if the buffer caps has changed since last check */
67   buffer_caps = GST_BUFFER_CAPS (buffer);
68   if (current_caps == NULL && buffer_caps != NULL) {
69     /* probably the first caps, this is a change */
70     current_caps = gst_caps_copy (buffer_caps);
71     caps_change++;
72   } else if (current_caps != NULL) {
73     if (buffer_caps == NULL) {
74       /* caps was set to NULL, we consider this a change */
75       gst_caps_unref (current_caps);
76       current_caps = NULL;
77       caps_change++;
78     } else {
79       if (!gst_caps_is_equal (current_caps, buffer_caps)) {
80         /* a caps change */
81         gst_caps_unref (current_caps);
82         current_caps = gst_caps_copy (buffer_caps);
83         caps_change++;
84       }
85     }
86   }
87
88   return TRUE;
89 }
90
91 GST_START_TEST (test_capsfilter_renegotiation)
92 {
93   GstElement *capsfilter;
94   GstElement *sink;
95   GstElement *pipeline;
96   GstBus *bus;
97   GstMessage *msg;
98   GstPad *pad;
99
100   caps_change = 0;
101   buffer_count = 0;
102   if (current_caps)
103     gst_caps_unref (current_caps);
104   current_caps = NULL;
105
106   pipeline = gst_parse_launch ("videotestsrc num-buffers=200 ! capsfilter "
107       "caps=\"" FIRST_CAPS "\" name=cf ! fakesink name=sink", NULL);
108   g_assert (pipeline);
109
110   capsfilter = gst_bin_get_by_name (GST_BIN (pipeline), "cf");
111   g_assert (capsfilter);
112
113   sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
114   g_assert (sink);
115
116   pad = gst_element_get_static_pad (sink, "sink");
117   gst_pad_add_buffer_probe (pad, (GCallback) buffer_probe, capsfilter);
118   gst_object_unref (pad);
119
120   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
121
122   g_assert (gst_element_set_state (pipeline, GST_STATE_PLAYING) !=
123       GST_STATE_CHANGE_FAILURE);
124
125   msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
126       GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
127
128   g_assert (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
129
130   g_assert (caps_change == 4);
131
132   gst_element_set_state (pipeline, GST_STATE_NULL);
133
134   if (current_caps)
135     gst_caps_unref (current_caps);
136   gst_message_unref (msg);
137   g_object_unref (bus);
138   g_object_unref (G_OBJECT (pipeline));
139 }
140
141 GST_END_TEST;
142
143 static Suite *
144 capsfilter_renegotiation_suite (void)
145 {
146   Suite *s = suite_create ("CapsfilterRenegotiation");
147   TCase *tc_chain = tcase_create ("linear");
148
149   /* time out after 60s, not the default 3 */
150   tcase_set_timeout (tc_chain, 60);
151
152   suite_add_tcase (s, tc_chain);
153   tcase_add_test (tc_chain, test_capsfilter_renegotiation);
154   return s;
155 }
156
157 int
158 main (int argc, char **argv)
159 {
160   int nf;
161
162   Suite *s = capsfilter_renegotiation_suite ();
163   SRunner *sr = srunner_create (s);
164
165   gst_check_init (&argc, &argv);
166
167   srunner_run_all (sr, CK_NORMAL);
168   nf = srunner_ntests_failed (sr);
169   srunner_free (sr);
170
171   return nf;
172 }