• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 /* Ideally this would be in core, but using videotestsrc makes it easier */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include <gst/check/gstcheck.h>
29 
30 #define FIRST_CAPS  "video/x-raw,width=(int)480,height=(int)320"
31 #define SECOND_CAPS "video/x-raw,width=(int)120,height=(int)100"
32 #define THIRD_CAPS  "video/x-raw,width=(int)[10,50],height=(int)[100,200]"
33 #define FOURTH_CAPS "video/x-raw,width=(int)300,height=(int)[25,75];" \
34                     "video/x-raw,width=(int)[30,40]," \
35                     "height=(int)[100,200],format=(string)YUY2"
36 
37 int buffer_count = 0;
38 GstCaps *current_caps = NULL;
39 int caps_change = 0;
40 
41 static GstPadProbeReturn
buffer_probe(GstPad * pad,GstPadProbeInfo * info,gpointer data)42 buffer_probe (GstPad * pad, GstPadProbeInfo * info, gpointer data)
43 {
44   GstCaps *pad_caps;
45   GstElement *capsfilter = GST_ELEMENT (data);
46   GstCaps *caps = NULL;
47 
48   /* increment the buffer count and check if it is time to change the caps */
49   buffer_count++;
50   if (buffer_count == 50) {
51     /* change the caps to another one */
52     caps = gst_caps_from_string (SECOND_CAPS);
53   } else if (buffer_count == 100) {
54     /* change the caps to another one, this time unfixed */
55     caps = gst_caps_from_string (THIRD_CAPS);
56   } else if (buffer_count == 150) {
57     /* change the caps to another one,
58      * this time unfixed with multiple entries */
59     caps = gst_caps_from_string (FOURTH_CAPS);
60   }
61   /* set the caps */
62   if (caps) {
63     g_object_set (capsfilter, "caps", caps, NULL);
64     gst_caps_unref (caps);
65   }
66   /* now check if the pad caps has changed since last check */
67   pad_caps = gst_pad_get_current_caps (pad);
68   if (current_caps == NULL && pad_caps != NULL) {
69     /* probably the first caps, this is a change */
70     current_caps = gst_caps_copy (pad_caps);
71     caps_change++;
72   } else if (current_caps != NULL) {
73     if (pad_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, pad_caps)) {
80         /* a caps change */
81         gst_caps_unref (current_caps);
82         current_caps = gst_caps_copy (pad_caps);
83         caps_change++;
84       }
85     }
86   }
87   gst_caps_unref (pad_caps);
88 
89   return TRUE;
90 }
91 
92 /* launch line is a pipeline that must have a capsfilter named 'cf' that
93  * will be used to trigger the renegotiation */
94 static void
run_capsfilter_renegotiation(const gchar * launch_line)95 run_capsfilter_renegotiation (const gchar * launch_line)
96 {
97   GstElement *capsfilter;
98   GstElement *sink;
99   GstElement *pipeline;
100   GstBus *bus;
101   GstMessage *msg;
102   GstPad *pad;
103 
104   caps_change = 0;
105   buffer_count = 0;
106   if (current_caps)
107     gst_caps_unref (current_caps);
108   current_caps = NULL;
109 
110   pipeline = gst_parse_launch (launch_line, NULL);
111   fail_unless (pipeline != NULL);
112 
113   capsfilter = gst_bin_get_by_name (GST_BIN (pipeline), "cf");
114   fail_unless (capsfilter != NULL);
115 
116   sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
117   fail_unless (sink != NULL);
118 
119   pad = gst_element_get_static_pad (sink, "sink");
120   gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER, buffer_probe, capsfilter,
121       NULL);
122   gst_object_unref (pad);
123 
124   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
125 
126   fail_unless (gst_element_set_state (pipeline, GST_STATE_PLAYING) !=
127       GST_STATE_CHANGE_FAILURE);
128 
129   msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
130       GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
131 
132   fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_EOS);
133   fail_unless_equals_int (caps_change, 4);
134 
135   gst_element_set_state (pipeline, GST_STATE_NULL);
136 
137   if (current_caps)
138     gst_caps_unref (current_caps);
139   current_caps = NULL;
140   gst_message_unref (msg);
141   g_object_unref (bus);
142   gst_object_unref (sink);
143   gst_object_unref (capsfilter);
144   g_object_unref (G_OBJECT (pipeline));
145 }
146 
GST_START_TEST(test_capsfilter_renegotiation)147 GST_START_TEST (test_capsfilter_renegotiation)
148 {
149   run_capsfilter_renegotiation ("videotestsrc num-buffers=200 "
150       " ! capsfilter caps=\"" FIRST_CAPS "\" name=cf ! fakesink name=sink");
151   run_capsfilter_renegotiation ("videotestsrc num-buffers=200 "
152       " ! capsfilter caps=\"" FIRST_CAPS "\" name=cf ! fakesink name=sink");
153   run_capsfilter_renegotiation ("videotestsrc num-buffers=200 "
154       " ! capsfilter caps=\"video/x-raw, format=(string)I420, width=(int)100, height=(int)100\" "
155       " ! videoconvert ! videoscale ! capsfilter caps=\"" FIRST_CAPS
156       "\" name=cf " " ! fakesink name=sink");
157 }
158 
159 GST_END_TEST;
160 
161 static Suite *
capsfilter_renegotiation_suite(void)162 capsfilter_renegotiation_suite (void)
163 {
164   Suite *s = suite_create ("CapsfilterRenegotiation");
165   TCase *tc_chain = tcase_create ("linear");
166 
167   /* time out after 60s, not the default 3 */
168   tcase_set_timeout (tc_chain, 60);
169 
170   suite_add_tcase (s, tc_chain);
171   tcase_add_test (tc_chain, test_capsfilter_renegotiation);
172   return s;
173 }
174 
175 GST_CHECK_MAIN (capsfilter_renegotiation);
176