1 /* GStreamer
2  *
3  * Copyright (c) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  * Copyright (c) 2010 David Schleef <ds@schleef.org>
5  * Copyright (c) 2014 Thijs Vermeir <thijs.vermeir@barco.com>
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 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
26     GST_PAD_SINK,
27     GST_PAD_ALWAYS,
28     GST_STATIC_CAPS ("video/x-h265, "
29         "width = (int) [1, MAX], "
30         "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
31 
32 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
33     GST_PAD_SRC,
34     GST_PAD_ALWAYS,
35     GST_STATIC_CAPS ("video/x-raw, "
36         "format = (string) I420, "
37         "width = (int) [1, MAX], "
38         "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
39 
40 static GstPad *sinkpad, *srcpad;
41 
42 static GstElement *
setup_x265enc(const gchar * src_caps_str)43 setup_x265enc (const gchar * src_caps_str)
44 {
45   GstElement *x265enc;
46   GstCaps *srccaps = NULL;
47   GstBus *bus;
48 
49   if (src_caps_str) {
50     srccaps = gst_caps_from_string (src_caps_str);
51     fail_unless (srccaps != NULL);
52   }
53 
54   x265enc = gst_check_setup_element ("x265enc");
55   fail_unless (x265enc != NULL);
56   srcpad = gst_check_setup_src_pad (x265enc, &srctemplate);
57   sinkpad = gst_check_setup_sink_pad (x265enc, &sinktemplate);
58   gst_pad_set_active (srcpad, TRUE);
59   gst_pad_set_active (sinkpad, TRUE);
60 
61   gst_check_setup_events (srcpad, x265enc, srccaps, GST_FORMAT_TIME);
62 
63   bus = gst_bus_new ();
64   gst_element_set_bus (x265enc, bus);
65 
66   fail_unless (gst_element_set_state (x265enc,
67           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
68       "could not set to playing");
69 
70   if (srccaps)
71     gst_caps_unref (srccaps);
72 
73   buffers = NULL;
74   return x265enc;
75 }
76 
77 static void
cleanup_x265enc(GstElement * x265enc)78 cleanup_x265enc (GstElement * x265enc)
79 {
80   GstBus *bus;
81 
82   /* Free parsed buffers */
83   gst_check_drop_buffers ();
84 
85   bus = GST_ELEMENT_BUS (x265enc);
86   gst_bus_set_flushing (bus, TRUE);
87   gst_object_unref (bus);
88 
89   gst_pad_set_active (srcpad, FALSE);
90   gst_pad_set_active (sinkpad, FALSE);
91   gst_check_teardown_src_pad (x265enc);
92   gst_check_teardown_sink_pad (x265enc);
93   gst_check_teardown_element (x265enc);
94 }
95 
GST_START_TEST(test_encode_simple)96 GST_START_TEST (test_encode_simple)
97 {
98   GstElement *x265enc;
99   GstBuffer *buffer;
100   gint i;
101   GList *l;
102   GstCaps *outcaps, *sinkcaps;
103   GstSegment seg;
104 
105   x265enc =
106       setup_x265enc
107       ("video/x-raw,format=(string)I420,width=(int)320,height=(int)240,framerate=(fraction)25/1");
108 
109   gst_segment_init (&seg, GST_FORMAT_TIME);
110   seg.stop = gst_util_uint64_scale (10, GST_SECOND, 25);
111 
112   fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
113 
114   buffer = gst_buffer_new_allocate (NULL, 320 * 240 + 2 * 160 * 120, NULL);
115   gst_buffer_memset (buffer, 0, 0, -1);
116 
117   for (i = 0; i < 10; i++) {
118     GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (i, GST_SECOND, 25);
119     GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25);
120     fail_unless (gst_pad_push (srcpad, gst_buffer_ref (buffer)) == GST_FLOW_OK);
121   }
122 
123   gst_buffer_unref (buffer);
124 
125   fail_unless (gst_pad_push_event (srcpad, gst_event_new_eos ()));
126 
127   /* All buffers must be there now */
128   fail_unless_equals_int (g_list_length (buffers), 10);
129 
130   outcaps =
131       gst_caps_from_string
132       ("video/x-h265,width=(int)320,height=(int)240,framerate=(fraction)25/1");
133 
134   for (l = buffers, i = 0; l; l = l->next, i++) {
135     buffer = l->data;
136 
137     fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
138         gst_util_uint64_scale (1, GST_SECOND, 25));
139 
140     sinkcaps = gst_pad_get_current_caps (sinkpad);
141     fail_unless (gst_caps_can_intersect (sinkcaps, outcaps));
142     gst_caps_unref (sinkcaps);
143   }
144 
145   gst_caps_unref (outcaps);
146 
147   cleanup_x265enc (x265enc);
148 }
149 
150 GST_END_TEST;
151 
152 static Suite *
x265enc_suite(void)153 x265enc_suite (void)
154 {
155   Suite *s = suite_create ("x265enc");
156   TCase *tc_chain = tcase_create ("general");
157 
158   suite_add_tcase (s, tc_chain);
159 
160   tcase_add_test (tc_chain, test_encode_simple);
161 
162   return s;
163 }
164 
165 GST_CHECK_MAIN (x265enc);
166