3 * unit test for jpegenc
5 * Copyright (C) <2010> Thiago Santos <thiago.sousa.santos@collabora.co.uk>
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>
26 #include <gst/app/gstappsink.h>
28 /* For ease of programming we use globals to keep refs for our floating
29 * sink pads we create; otherwise we always have to do get_pad,
30 * get_peer, and then remove references in every test function */
31 static GstPad *mysinkpad;
32 static GstPad *mysrcpad;
34 #define JPEG_CAPS_STRING "image/jpeg"
36 #define JPEG_CAPS_RESTRICTIVE "image/jpeg, " \
37 "width = (int) [100, 200], " \
38 "framerate = (fraction) 25/1, " \
39 "extraparameter = (string) { abc, def }"
41 static GstStaticPadTemplate jpeg_sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS (JPEG_CAPS_STRING));
46 static GstStaticPadTemplate any_sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
51 static GstStaticPadTemplate jpeg_restrictive_sinktemplate =
52 GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS (JPEG_CAPS_RESTRICTIVE));
57 static GstStaticPadTemplate any_srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
64 setup_jpegenc (GstStaticPadTemplate * sinktemplate)
68 GST_DEBUG ("setup_jpegenc");
69 jpegenc = gst_check_setup_element ("jpegenc");
70 mysinkpad = gst_check_setup_sink_pad (jpegenc, sinktemplate, NULL);
71 mysrcpad = gst_check_setup_src_pad (jpegenc, &any_srctemplate, NULL);
72 gst_pad_set_active (mysrcpad, TRUE);
73 gst_pad_set_active (mysinkpad, TRUE);
79 cleanup_jpegenc (GstElement * jpegenc)
81 GST_DEBUG ("cleanup_jpegenc");
82 gst_element_set_state (jpegenc, GST_STATE_NULL);
84 gst_pad_set_active (mysrcpad, FALSE);
85 gst_pad_set_active (mysinkpad, FALSE);
86 gst_check_teardown_sink_pad (jpegenc);
87 gst_check_teardown_src_pad (jpegenc);
88 gst_check_teardown_element (jpegenc);
92 create_video_buffer (GstCaps * caps)
101 ("videotestsrc num-buffers=1 ! capsfilter name=cf ! appsink name=sink",
103 g_assert (pipeline != NULL);
105 cf = gst_bin_get_by_name (GST_BIN (pipeline), "cf");
106 sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
108 g_object_set (G_OBJECT (cf), "caps", caps, NULL);
110 gst_element_set_state (pipeline, GST_STATE_PLAYING);
112 buffer = gst_app_sink_pull_buffer (GST_APP_SINK (sink));
114 gst_element_set_state (pipeline, GST_STATE_NULL);
115 gst_object_unref (pipeline);
116 gst_object_unref (sink);
117 gst_object_unref (cf);
122 GST_START_TEST (test_jpegenc_getcaps)
127 GstStructure *structure;
132 /* we are going to do some get caps to confirm it doesn't return non-subset
135 jpegenc = setup_jpegenc (&any_sinktemplate);
136 sinkpad = gst_element_get_static_pad (jpegenc, "sink");
137 /* this should assert if non-subset */
138 caps = gst_pad_get_caps (sinkpad);
139 gst_caps_unref (caps);
140 gst_object_unref (sinkpad);
141 cleanup_jpegenc (jpegenc);
143 jpegenc = setup_jpegenc (&jpeg_sinktemplate);
144 sinkpad = gst_element_get_static_pad (jpegenc, "sink");
145 /* this should assert if non-subset */
146 caps = gst_pad_get_caps (sinkpad);
147 gst_caps_unref (caps);
148 gst_object_unref (sinkpad);
149 cleanup_jpegenc (jpegenc);
151 /* now use a more restricted one and check the resulting caps */
152 jpegenc = setup_jpegenc (&jpeg_restrictive_sinktemplate);
153 sinkpad = gst_element_get_static_pad (jpegenc, "sink");
154 /* this should assert if non-subset */
155 caps = gst_pad_get_caps (sinkpad);
156 structure = gst_caps_get_structure (caps, 0);
158 /* check the width */
159 value = gst_structure_get_value (structure, "width");
160 fail_unless (gst_value_get_int_range_min (value) == 100);
161 fail_unless (gst_value_get_int_range_max (value) == 200);
163 fail_unless (gst_structure_get_fraction (structure, "framerate", &fps_n,
165 fail_unless (fps_n == 25);
166 fail_unless (fps_d == 1);
168 gst_caps_unref (caps);
169 gst_object_unref (sinkpad);
170 cleanup_jpegenc (jpegenc);
176 GST_START_TEST (test_jpegenc_different_caps)
181 GstCaps *allowed_caps;
183 /* now use a more restricted one and check the resulting caps */
184 jpegenc = setup_jpegenc (&any_sinktemplate);
185 gst_element_set_state (jpegenc, GST_STATE_PLAYING);
187 /* push first buffer with 800x600 resolution */
188 caps = gst_caps_new_simple ("video/x-raw-yuv", "width", G_TYPE_INT,
189 800, "height", G_TYPE_INT, 600, "framerate",
190 GST_TYPE_FRACTION, 1, 1, "format", GST_TYPE_FOURCC,
191 GST_MAKE_FOURCC ('I', '4', '2', '0'), NULL);
192 buffer = create_video_buffer (caps);
193 gst_caps_unref (caps);
194 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
196 /* check the allowed caps to see if a second buffer with a different
197 * caps could be negotiated */
198 allowed_caps = gst_pad_get_allowed_caps (mysrcpad);
200 /* the caps we want to negotiate to */
201 caps = gst_caps_new_simple ("video/x-raw-yuv", "width", G_TYPE_INT,
202 640, "height", G_TYPE_INT, 480, "framerate",
203 GST_TYPE_FRACTION, 1, 1, "format", GST_TYPE_FOURCC,
204 GST_MAKE_FOURCC ('I', '4', '2', '0'), NULL);
205 fail_unless (gst_caps_can_intersect (allowed_caps, caps));
207 /* push second buffer with 640x480 resolution */
208 buffer = create_video_buffer (caps);
209 gst_caps_unref (caps);
210 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
212 gst_caps_unref (allowed_caps);
213 gst_element_set_state (jpegenc, GST_STATE_NULL);
214 cleanup_jpegenc (jpegenc);
222 Suite *s = suite_create ("jpegenc");
223 TCase *tc_chain = tcase_create ("general");
225 suite_add_tcase (s, tc_chain);
226 tcase_add_test (tc_chain, test_jpegenc_getcaps);
227 tcase_add_test (tc_chain, test_jpegenc_different_caps);
233 main (int argc, char **argv)
237 Suite *s = jpegenc_suite ();
238 SRunner *sr = srunner_create (s);
240 gst_check_init (&argc, &argv);
242 srunner_run_all (sr, CK_NORMAL);
243 nf = srunner_ntests_failed (sr);