1 /* GStreamer
2  *
3  * Copyright (c) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include <gst/check/gstcheck.h>
22 
23 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
24     GST_PAD_SINK,
25     GST_PAD_ALWAYS,
26     GST_STATIC_CAPS ("video/x-vp8, "
27         "width = (int) [1, MAX], "
28         "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
29 
30 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
31     GST_PAD_SRC,
32     GST_PAD_ALWAYS,
33     GST_STATIC_CAPS ("video/x-raw, "
34         "format = (string) I420, "
35         "width = (int) [1, MAX], "
36         "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
37 
38 static GstPad *sinkpad, *srcpad;
39 
40 static GstElement *
setup_vp8enc(const gchar * src_caps_str)41 setup_vp8enc (const gchar * src_caps_str)
42 {
43   GstElement *vp8enc;
44   GstCaps *srccaps = NULL;
45   GstBus *bus;
46 
47   if (src_caps_str) {
48     srccaps = gst_caps_from_string (src_caps_str);
49     fail_unless (srccaps != NULL);
50   }
51 
52   vp8enc = gst_check_setup_element ("vp8enc");
53   fail_unless (vp8enc != NULL);
54   srcpad = gst_check_setup_src_pad (vp8enc, &srctemplate);
55   sinkpad = gst_check_setup_sink_pad (vp8enc, &sinktemplate);
56   gst_pad_set_active (srcpad, TRUE);
57   gst_pad_set_active (sinkpad, TRUE);
58   gst_check_setup_events (srcpad, vp8enc, srccaps, GST_FORMAT_TIME);
59 
60   bus = gst_bus_new ();
61   gst_element_set_bus (vp8enc, bus);
62 
63   fail_unless (gst_element_set_state (vp8enc,
64           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
65       "could not set to playing");
66 
67   if (srccaps)
68     gst_caps_unref (srccaps);
69 
70   buffers = NULL;
71   return vp8enc;
72 }
73 
74 static void
cleanup_vp8enc(GstElement * vp8enc)75 cleanup_vp8enc (GstElement * vp8enc)
76 {
77   GstBus *bus;
78 
79   /* Free parsed buffers */
80   gst_check_drop_buffers ();
81 
82   bus = GST_ELEMENT_BUS (vp8enc);
83   gst_bus_set_flushing (bus, TRUE);
84   gst_object_unref (bus);
85 
86   gst_pad_set_active (srcpad, FALSE);
87   gst_pad_set_active (sinkpad, FALSE);
88   gst_check_teardown_src_pad (vp8enc);
89   gst_check_teardown_sink_pad (vp8enc);
90   gst_check_teardown_element (vp8enc);
91 }
92 
GST_START_TEST(test_encode_simple)93 GST_START_TEST (test_encode_simple)
94 {
95   GstElement *vp8enc;
96   GstBuffer *buffer;
97   gint i;
98   GList *l;
99   GstCaps *outcaps;
100   GstSegment seg;
101 
102   vp8enc =
103       setup_vp8enc
104       ("video/x-raw,format=(string)I420,width=(int)320,height=(int)240,framerate=(fraction)25/1");
105 
106   g_object_set (vp8enc, "lag-in-frames", 5, NULL);
107 
108   gst_segment_init (&seg, GST_FORMAT_TIME);
109   seg.stop = gst_util_uint64_scale (20, GST_SECOND, 25);
110   fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
111 
112   buffer = gst_buffer_new_and_alloc (320 * 240 + 2 * 160 * 120);
113   gst_buffer_memset (buffer, 0, 0, -1);
114 
115   for (i = 0; i < 20; i++) {
116     GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (i, GST_SECOND, 25);
117     GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25);
118     fail_unless (gst_pad_push (srcpad, gst_buffer_ref (buffer)) == GST_FLOW_OK);
119   }
120 
121   gst_buffer_unref (buffer);
122 
123   /* Only 5 buffers are allowed to be queued now */
124   fail_unless (g_list_length (buffers) > 15);
125 
126   fail_unless (gst_pad_push_event (srcpad, gst_event_new_eos ()));
127 
128 
129   /* All buffers must be there now */
130   fail_unless_equals_int (g_list_length (buffers), 20);
131 
132   outcaps =
133       gst_caps_from_string
134       ("video/x-vp8,width=(int)320,height=(int)240,framerate=(fraction)25/1");
135 
136   for (l = buffers, i = 0; l; l = l->next, i++) {
137     buffer = l->data;
138 
139     if (i == 0)
140       fail_if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT));
141 
142     fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (buffer),
143         gst_util_uint64_scale (i, GST_SECOND, 25));
144     fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
145         gst_util_uint64_scale (1, GST_SECOND, 25));
146   }
147 
148   gst_caps_unref (outcaps);
149 
150   cleanup_vp8enc (vp8enc);
151 }
152 
153 GST_END_TEST;
154 
155 static Suite *
vp8enc_suite(void)156 vp8enc_suite (void)
157 {
158   Suite *s = suite_create ("vp8enc");
159   TCase *tc_chain = tcase_create ("general");
160 
161   suite_add_tcase (s, tc_chain);
162 
163   tcase_add_test (tc_chain, test_encode_simple);
164 
165   return s;
166 }
167 
168 GST_CHECK_MAIN (vp8enc);
169