1 /* GStreamer
2  *
3  * Unit test for msdkh264enc
4  *
5  * Copyright (c) 2018 Wang,Fei <fei.w.wang@intel.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 h264enc_sinktemp = GST_STATIC_PAD_TEMPLATE ("sink",
26     GST_PAD_SINK,
27     GST_PAD_ALWAYS,
28     GST_STATIC_CAPS ("video/x-h264, "
29         "width = (int) [1, MAX], "
30         "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
31 
32 static GstStaticPadTemplate h264enc_srctemp = GST_STATIC_PAD_TEMPLATE ("src",
33     GST_PAD_SRC,
34     GST_PAD_ALWAYS,
35     GST_STATIC_CAPS ("video/x-raw, "
36         "format = (string) NV12, "
37         "width = (int) [1, MAX], "
38         "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
39 
40 
41 static GstPad *sinkpad, *srcpad;
42 
43 static GstElement *
setup_element(const gchar * caps)44 setup_element (const gchar * caps)
45 {
46   GstElement *element = NULL;
47   GstCaps *srccaps = NULL;
48   GstBus *bus = NULL;
49 
50   if (caps) {
51     srccaps = gst_caps_from_string (caps);
52     fail_unless (srccaps != NULL);
53   }
54   element = gst_check_setup_element ("msdkh264enc");
55   fail_unless (element != NULL);
56   srcpad = gst_check_setup_src_pad (element, &h264enc_srctemp);
57   sinkpad = gst_check_setup_sink_pad (element, &h264enc_sinktemp);
58   gst_pad_set_active (srcpad, TRUE);
59   gst_pad_set_active (sinkpad, TRUE);
60   gst_check_setup_events (srcpad, element, srccaps, GST_FORMAT_TIME);
61 
62   bus = gst_bus_new ();
63   gst_element_set_bus (element, bus);
64 
65   fail_unless (gst_element_set_state (element,
66           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
67       "could not set to playing");
68 
69   if (srccaps)
70     gst_caps_unref (srccaps);
71 
72   buffers = NULL;
73   return element;
74 
75 }
76 
77 static void
cleanup_element(GstElement * element)78 cleanup_element (GstElement * element)
79 {
80   GstBus *bus;
81 
82   /* Free parsed buffers */
83   gst_check_drop_buffers ();
84 
85   bus = GST_ELEMENT_BUS (element);
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 (element);
92   gst_check_teardown_sink_pad (element);
93   gst_check_teardown_element (element);
94 }
95 
96 
GST_START_TEST(msdk_h264enc)97 GST_START_TEST (msdk_h264enc)
98 {
99   GstElement *msdkh264enc;
100   GstBuffer *buffer;
101   gint i;
102   GList *l;
103   GstCaps *outcaps, *sinkcaps;
104   GstSegment seg;
105 
106   msdkh264enc =
107       setup_element
108       ("video/x-raw,format=(string)NV12,width=(int)320,height=(int)240,framerate=(fraction)25/1,interlace-mode=(string)progressive");
109 
110   gst_segment_init (&seg, GST_FORMAT_TIME);
111   seg.stop = gst_util_uint64_scale (10, GST_SECOND, 25);
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   fail_unless_equals_int (g_list_length (buffers), 10);
128 
129   outcaps =
130       gst_caps_from_string
131       ("video/x-h264,width=(int)320,height=(int)240,framerate=(fraction)25/1");
132 
133   for (l = buffers, i = 0; l; l = l->next, i++) {
134     buffer = l->data;
135 
136     fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
137         gst_util_uint64_scale (1, GST_SECOND, 25));
138 
139     sinkcaps = gst_pad_get_current_caps (sinkpad);
140     fail_unless (gst_caps_can_intersect (sinkcaps, outcaps));
141     gst_caps_unref (sinkcaps);
142   }
143 
144   gst_caps_unref (outcaps);
145   cleanup_element (msdkh264enc);
146 }
147 
148 GST_END_TEST;
149 
150 static Suite *
msdkh264enc_suite(void)151 msdkh264enc_suite (void)
152 {
153   Suite *s = suite_create ("msdkh264enc");
154 
155   TCase *tc_chain = tcase_create ("general");
156 
157   suite_add_tcase (s, tc_chain);
158   tcase_add_test (tc_chain, msdk_h264enc);
159 
160   return s;
161 }
162 
163 GST_CHECK_MAIN (msdkh264enc);
164