5 * Copyright (C) <2006> Mark Nauwelaerts <manauw@skynet.be>
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>
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;
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"
39 #define Y4M_CAPS_STRING "application/x-yuv4mpeg, " \
40 "y4mversion = (int) 2"
42 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS (Y4M_CAPS_STRING));
47 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
50 GST_STATIC_CAPS (VIDEO_CAPS_STRING));
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);
69 cleanup_y4menc (GstElement * y4menc)
71 GST_DEBUG ("cleanup_y4menc");
72 gst_element_set_state (y4menc, GST_STATE_NULL);
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);
81 GST_START_TEST (test_y4m)
84 GstBuffer *inbuffer, *outbuffer;
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";
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");
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);
108 num_buffers = g_list_length (buffers);
109 fail_unless (num_buffers == 1);
111 /* clean up buffers */
112 for (i = 0; i < num_buffers; ++i) {
115 outbuffer = GST_BUFFER (buffers->data);
116 fail_if (outbuffer == NULL);
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);
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));
138 buffers = g_list_remove (buffers, outbuffer);
140 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
141 gst_buffer_unref (outbuffer);
145 cleanup_y4menc (y4menc);
146 g_list_free (buffers);
155 Suite *s = suite_create ("y4menc");
156 TCase *tc_chain = tcase_create ("general");
158 suite_add_tcase (s, tc_chain);
159 tcase_add_test (tc_chain, test_y4m);
165 main (int argc, char **argv)
169 Suite *s = y4menc_suite ();
170 SRunner *sr = srunner_create (s);
172 gst_check_init (&argc, &argv);
174 srunner_run_all (sr, CK_NORMAL);
175 nf = srunner_ntests_failed (sr);