• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   element = gst_check_setup_element ("msdkh264enc");
51   if (caps) {
52     srccaps = gst_caps_from_string (caps);
53     fail_unless (srccaps != NULL);
54   }
55   srcpad = gst_check_setup_src_pad (element, &h264enc_srctemp);
56   sinkpad = gst_check_setup_sink_pad (element, &h264enc_sinktemp);
57   gst_pad_set_active (srcpad, TRUE);
58   gst_pad_set_active (sinkpad, TRUE);
59   gst_check_setup_events (srcpad, element, srccaps, GST_FORMAT_TIME);
60 
61   bus = gst_bus_new ();
62   gst_element_set_bus (element, bus);
63 
64   fail_unless (gst_element_set_state (element,
65           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
66       "could not set to playing");
67 
68   gst_caps_unref (srccaps);
69 
70   buffers = NULL;
71   return element;
72 }
73 
74 static void
cleanup_element(GstElement * element)75 cleanup_element (GstElement * element)
76 {
77   GstBus *bus;
78 
79   /* Free parsed buffers */
80   gst_check_drop_buffers ();
81 
82   bus = GST_ELEMENT_BUS (element);
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 (element);
89   gst_check_teardown_sink_pad (element);
90   gst_check_teardown_element (element);
91 }
92 
93 
GST_START_TEST(msdk_h264enc)94 GST_START_TEST (msdk_h264enc)
95 {
96   GstElement *msdkh264enc;
97   GstBuffer *buffer;
98   gint i;
99   GList *l;
100   GstCaps *outcaps, *sinkcaps;
101   GstSegment seg;
102 
103   msdkh264enc =
104       setup_element
105       ("video/x-raw,format=(string)NV12,width=(int)320,height=(int)240,framerate=(fraction)25/1,interlace-mode=(string)progressive");
106 
107   gst_segment_init (&seg, GST_FORMAT_TIME);
108   seg.stop = gst_util_uint64_scale (10, GST_SECOND, 25);
109   fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
110 
111   buffer = gst_buffer_new_allocate (NULL, 320 * 240 + 2 * 160 * 120, NULL);
112   gst_buffer_memset (buffer, 0, 0, -1);
113 
114   for (i = 0; i < 10; i++) {
115     GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (i, GST_SECOND, 25);
116     GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25);
117     fail_unless (gst_pad_push (srcpad, gst_buffer_ref (buffer)) == GST_FLOW_OK);
118   }
119 
120   gst_buffer_unref (buffer);
121 
122   fail_unless (gst_pad_push_event (srcpad, gst_event_new_eos ()));
123 
124   fail_unless_equals_int (g_list_length (buffers), 10);
125 
126   outcaps =
127       gst_caps_from_string
128       ("video/x-h264,width=(int)320,height=(int)240,framerate=(fraction)25/1");
129 
130   for (l = buffers, i = 0; l; l = l->next, i++) {
131     buffer = l->data;
132 
133     fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
134         gst_util_uint64_scale (1, GST_SECOND, 25));
135 
136     sinkcaps = gst_pad_get_current_caps (sinkpad);
137     fail_unless (gst_caps_can_intersect (sinkcaps, outcaps));
138     gst_caps_unref (sinkcaps);
139   }
140 
141   gst_caps_unref (outcaps);
142   cleanup_element (msdkh264enc);
143 }
144 
145 GST_END_TEST;
146 
147 static Suite *
msdkh264enc_suite(void)148 msdkh264enc_suite (void)
149 {
150   Suite *s = suite_create ("msdkh264enc");
151   TCase *tc_chain = tcase_create ("general");
152   GstElementFactory *factory;
153 
154   suite_add_tcase (s, tc_chain);
155 
156   factory = gst_element_factory_find ("msdkh264enc");
157   if (factory) {
158     tcase_add_test (tc_chain, msdk_h264enc);
159     gst_object_unref (factory);
160   }
161 
162   return s;
163 }
164 
165 GST_CHECK_MAIN (msdkh264enc);
166