Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / y4menc.c
1 /* GStreamer
2  *
3  * unit test for y4menc
4  *
5  * Copyright (C) <2006> Mark Nauwelaerts <manauw@skynet.be>
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 #define VIDEO_CAPS_STRING "video/x-raw-yuv, " \
33                            "format = (fourcc) I420, "\
34                            "width = (int) 384, " \
35                            "height = (int) 288, " \
36                            "framerate = (fraction) 25/1, " \
37                            "pixel-aspect-ratio = (fraction) 1/1"
38
39 #define Y4M_CAPS_STRING "application/x-yuv4mpeg, " \
40                         "y4mversion = (int) 2"
41
42 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS (Y4M_CAPS_STRING));
46
47 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
48     GST_PAD_SRC,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS (VIDEO_CAPS_STRING));
51
52
53 static GstElement *
54 setup_y4menc (void)
55 {
56   GstElement *y4menc;
57
58   GST_DEBUG ("setup_y4menc");
59   y4menc = gst_check_setup_element ("y4menc");
60   mysrcpad = gst_check_setup_src_pad (y4menc, &srctemplate, NULL);
61   mysinkpad = gst_check_setup_sink_pad (y4menc, &sinktemplate, NULL);
62   gst_pad_set_active (mysrcpad, TRUE);
63   gst_pad_set_active (mysinkpad, TRUE);
64
65   return y4menc;
66 }
67
68 static void
69 cleanup_y4menc (GstElement * y4menc)
70 {
71   GST_DEBUG ("cleanup_y4menc");
72   gst_element_set_state (y4menc, GST_STATE_NULL);
73
74   gst_pad_set_active (mysrcpad, FALSE);
75   gst_pad_set_active (mysinkpad, FALSE);
76   gst_check_teardown_src_pad (y4menc);
77   gst_check_teardown_sink_pad (y4menc);
78   gst_check_teardown_element (y4menc);
79 }
80
81 GST_START_TEST (test_y4m)
82 {
83   GstElement *y4menc;
84   GstBuffer *inbuffer, *outbuffer;
85   GstCaps *caps;
86   int i, num_buffers, size;
87   const gchar *data0 = "YUV4MPEG2 W384 H288 Ip F25:1 A1:1\n";
88   const gchar *data1 = "YUV4MPEG2 C420 W384 H288 Ip F25:1 A1:1\n";
89   const gchar *data2 = "FRAME\n";
90
91   y4menc = setup_y4menc ();
92   fail_unless (gst_element_set_state (y4menc,
93           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
94       "could not set to playing");
95
96   /* corresponds to I420 buffer for the size mentioned in the caps */
97   size = 384 * 288 * 3 / 2;
98   inbuffer = gst_buffer_new_and_alloc (size);
99   /* makes valgrind's memcheck happier */
100   memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer));
101   caps = gst_caps_from_string (VIDEO_CAPS_STRING);
102   gst_buffer_set_caps (inbuffer, caps);
103   gst_caps_unref (caps);
104   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
105   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
106   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
107
108   num_buffers = g_list_length (buffers);
109   fail_unless (num_buffers == 1);
110
111   /* clean up buffers */
112   for (i = 0; i < num_buffers; ++i) {
113     gchar *data;
114
115     outbuffer = GST_BUFFER (buffers->data);
116     fail_if (outbuffer == NULL);
117
118     switch (i) {
119       case 0:
120         fail_unless (GST_BUFFER_SIZE (outbuffer) > size);
121         fail_unless (memcmp (data0, GST_BUFFER_DATA (outbuffer),
122                 strlen (data0)) == 0 ||
123             memcmp (data1, GST_BUFFER_DATA (outbuffer), strlen (data1)) == 0);
124         /* so we know there is a newline */
125         data = (gchar *) GST_BUFFER_DATA (outbuffer);
126         data = strchr (data, '\n');
127         fail_unless (data != NULL);
128         data++;
129         fail_unless (memcmp (data2, data, strlen (data2)) == 0);
130         data += strlen (data2);
131         /* remainder must be frame data */
132         fail_unless ((data - (gchar *) GST_BUFFER_DATA (outbuffer)) + size ==
133             GST_BUFFER_SIZE (outbuffer));
134         break;
135       default:
136         break;
137     }
138     buffers = g_list_remove (buffers, outbuffer);
139
140     ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
141     gst_buffer_unref (outbuffer);
142     outbuffer = NULL;
143   }
144
145   cleanup_y4menc (y4menc);
146   g_list_free (buffers);
147   buffers = NULL;
148 }
149
150 GST_END_TEST;
151
152 static Suite *
153 y4menc_suite (void)
154 {
155   Suite *s = suite_create ("y4menc");
156   TCase *tc_chain = tcase_create ("general");
157
158   suite_add_tcase (s, tc_chain);
159   tcase_add_test (tc_chain, test_y4m);
160
161   return s;
162 }
163
164 int
165 main (int argc, char **argv)
166 {
167   int nf;
168
169   Suite *s = y4menc_suite ();
170   SRunner *sr = srunner_create (s);
171
172   gst_check_init (&argc, &argv);
173
174   srunner_run_all (sr, CK_NORMAL);
175   nf = srunner_ntests_failed (sr);
176   srunner_free (sr);
177
178   return nf;
179 }