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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include <gst/check/gstcheck.h>
24 
25 /* For ease of programming we use globals to keep refs for our floating
26  * src and sink pads we create; otherwise we always have to do get_pad,
27  * get_peer, and then remove references in every test function */
28 static GstPad *mysrcpad, *mysinkpad;
29 
30 #define VIDEO_CAPS_STRING "video/x-raw, " \
31                            "format = (string) I420, "\
32                            "width = (int) 384, " \
33                            "height = (int) 288, " \
34                            "framerate = (fraction) 25/1, " \
35                            "pixel-aspect-ratio = (fraction) 1/1"
36 
37 #define Y4M_CAPS_STRING "application/x-yuv4mpeg, " \
38                         "y4mversion = (int) 2"
39 
40 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
41     GST_PAD_SINK,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS (Y4M_CAPS_STRING));
44 
45 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS (VIDEO_CAPS_STRING));
49 
50 
51 static GstElement *
setup_y4menc(void)52 setup_y4menc (void)
53 {
54   GstElement *y4menc;
55 
56   GST_DEBUG ("setup_y4menc");
57   y4menc = gst_check_setup_element ("y4menc");
58   mysrcpad = gst_check_setup_src_pad (y4menc, &srctemplate);
59   mysinkpad = gst_check_setup_sink_pad (y4menc, &sinktemplate);
60   gst_pad_set_active (mysrcpad, TRUE);
61   gst_pad_set_active (mysinkpad, TRUE);
62 
63   return y4menc;
64 }
65 
66 static void
cleanup_y4menc(GstElement * y4menc)67 cleanup_y4menc (GstElement * y4menc)
68 {
69   GST_DEBUG ("cleanup_y4menc");
70   gst_element_set_state (y4menc, GST_STATE_NULL);
71 
72   gst_pad_set_active (mysrcpad, FALSE);
73   gst_pad_set_active (mysinkpad, FALSE);
74   gst_check_teardown_src_pad (y4menc);
75   gst_check_teardown_sink_pad (y4menc);
76   gst_check_teardown_element (y4menc);
77 }
78 
GST_START_TEST(test_y4m)79 GST_START_TEST (test_y4m)
80 {
81   GstElement *y4menc;
82   GstBuffer *inbuffer, *outbuffer;
83   GstCaps *caps;
84   int i, num_buffers, size;
85   const gchar *data0 = "YUV4MPEG2 W384 H288 Ip F25:1 A1:1\n";
86   const gchar *data1 = "YUV4MPEG2 C420 W384 H288 Ip F25:1 A1:1\n";
87   const gchar *data2 = "FRAME\n";
88 
89   y4menc = setup_y4menc ();
90   fail_unless (gst_element_set_state (y4menc,
91           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
92       "could not set to playing");
93 
94   /* corresponds to I420 buffer for the size mentioned in the caps */
95   size = 384 * 288 * 3 / 2;
96   inbuffer = gst_buffer_new_and_alloc (size);
97   /* makes valgrind's memcheck happier */
98   gst_buffer_memset (inbuffer, 0, 0, size);
99   caps = gst_caps_from_string (VIDEO_CAPS_STRING);
100   gst_check_setup_events (mysrcpad, y4menc, caps, GST_FORMAT_TIME);
101   gst_caps_unref (caps);
102   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
103   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
104   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
105 
106   num_buffers = g_list_length (buffers);
107   fail_unless (num_buffers == 1);
108 
109   /* clean up buffers */
110   for (i = 0; i < num_buffers; ++i) {
111     GstMapInfo map;
112     gchar *data;
113     gsize outsize;
114 
115     outbuffer = GST_BUFFER (buffers->data);
116     fail_if (outbuffer == NULL);
117 
118     switch (i) {
119       case 0:
120         gst_buffer_map (outbuffer, &map, GST_MAP_READ);
121         outsize = map.size;
122         data = (gchar *) map.data;
123 
124         fail_unless (outsize > size);
125         fail_unless (memcmp (data, data0, strlen (data0)) == 0 ||
126             memcmp (data, data1, strlen (data1)) == 0);
127         /* so we know there is a newline */
128         data = strchr (data, '\n');
129         fail_unless (data != NULL);
130         data++;
131         fail_unless (memcmp (data2, data, strlen (data2)) == 0);
132         data += strlen (data2);
133         /* remainder must be frame data */
134         fail_unless (data - (gchar *) map.data + size == outsize);
135         gst_buffer_unmap (outbuffer, &map);
136         break;
137       default:
138         break;
139     }
140     buffers = g_list_remove (buffers, outbuffer);
141 
142     ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
143     gst_buffer_unref (outbuffer);
144     outbuffer = NULL;
145   }
146 
147   cleanup_y4menc (y4menc);
148   g_list_free (buffers);
149   buffers = NULL;
150 }
151 
152 GST_END_TEST;
153 
154 static Suite *
y4menc_suite(void)155 y4menc_suite (void)
156 {
157   Suite *s = suite_create ("y4menc");
158   TCase *tc_chain = tcase_create ("general");
159 
160   suite_add_tcase (s, tc_chain);
161   tcase_add_test (tc_chain, test_y4m);
162 
163   return s;
164 }
165 
166 GST_CHECK_MAIN (y4menc);
167