Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / jpegenc.c
1 /* GStreamer
2  *
3  * unit test for jpegenc
4  *
5  * Copyright (C) <2010> Thiago Santos <thiago.sousa.santos@collabora.co.uk>
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 #include <gst/app/gstappsink.h>
27
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;
33
34 #define JPEG_CAPS_STRING "image/jpeg"
35
36 #define JPEG_CAPS_RESTRICTIVE "image/jpeg, " \
37     "width = (int) [100, 200], " \
38     "framerate = (fraction) 25/1, " \
39     "extraparameter = (string) { abc, def }"
40
41 static GstStaticPadTemplate jpeg_sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
42     GST_PAD_SINK,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS (JPEG_CAPS_STRING));
45
46 static GstStaticPadTemplate any_sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
47     GST_PAD_SINK,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS_ANY);
50
51 static GstStaticPadTemplate jpeg_restrictive_sinktemplate =
52 GST_STATIC_PAD_TEMPLATE ("sink",
53     GST_PAD_SINK,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS (JPEG_CAPS_RESTRICTIVE));
56
57 static GstStaticPadTemplate any_srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS_ANY);
61
62
63 static GstElement *
64 setup_jpegenc (GstStaticPadTemplate * sinktemplate)
65 {
66   GstElement *jpegenc;
67
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);
74
75   return jpegenc;
76 }
77
78 static void
79 cleanup_jpegenc (GstElement * jpegenc)
80 {
81   GST_DEBUG ("cleanup_jpegenc");
82   gst_element_set_state (jpegenc, GST_STATE_NULL);
83
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);
89 }
90
91 static GstBuffer *
92 create_video_buffer (GstCaps * caps)
93 {
94   GstElement *pipeline;
95   GstElement *cf;
96   GstElement *sink;
97   GstBuffer *buffer;
98
99   pipeline =
100       gst_parse_launch
101       ("videotestsrc num-buffers=1 ! capsfilter name=cf ! appsink name=sink",
102       NULL);
103   g_assert (pipeline != NULL);
104
105   cf = gst_bin_get_by_name (GST_BIN (pipeline), "cf");
106   sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
107
108   g_object_set (G_OBJECT (cf), "caps", caps, NULL);
109
110   gst_element_set_state (pipeline, GST_STATE_PLAYING);
111
112   buffer = gst_app_sink_pull_buffer (GST_APP_SINK (sink));
113
114   gst_element_set_state (pipeline, GST_STATE_NULL);
115   gst_object_unref (pipeline);
116   gst_object_unref (sink);
117   gst_object_unref (cf);
118   return buffer;
119 }
120
121
122 GST_START_TEST (test_jpegenc_getcaps)
123 {
124   GstElement *jpegenc;
125   GstPad *sinkpad;
126   GstCaps *caps;
127   GstStructure *structure;
128   gint fps_n;
129   gint fps_d;
130   const GValue *value;
131
132   /* we are going to do some get caps to confirm it doesn't return non-subset
133    * caps */
134
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);
142
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);
150
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);
157
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);
162
163   fail_unless (gst_structure_get_fraction (structure, "framerate", &fps_n,
164           &fps_d));
165   fail_unless (fps_n == 25);
166   fail_unless (fps_d == 1);
167
168   gst_caps_unref (caps);
169   gst_object_unref (sinkpad);
170   cleanup_jpegenc (jpegenc);
171 }
172
173 GST_END_TEST;
174
175
176 GST_START_TEST (test_jpegenc_different_caps)
177 {
178   GstElement *jpegenc;
179   GstBuffer *buffer;
180   GstCaps *caps;
181   GstCaps *allowed_caps;
182
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);
186
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);
195
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);
199
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));
206
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);
211
212   gst_caps_unref (allowed_caps);
213   gst_element_set_state (jpegenc, GST_STATE_NULL);
214   cleanup_jpegenc (jpegenc);
215 }
216
217 GST_END_TEST;
218
219 static Suite *
220 jpegenc_suite (void)
221 {
222   Suite *s = suite_create ("jpegenc");
223   TCase *tc_chain = tcase_create ("general");
224
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);
228
229   return s;
230 }
231
232 int
233 main (int argc, char **argv)
234 {
235   int nf;
236
237   Suite *s = jpegenc_suite ();
238   SRunner *sr = srunner_create (s);
239
240   gst_check_init (&argc, &argv);
241
242   srunner_run_all (sr, CK_NORMAL);
243   nf = srunner_ntests_failed (sr);
244   srunner_free (sr);
245
246   return nf;
247 }