• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include <gst/check/gstcheck.h>
21 
22 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
23     GST_PAD_SINK,
24     GST_PAD_ALWAYS,
25     GST_STATIC_CAPS ("video/x-h264, "
26         "width = (int) [1, MAX], "
27         "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
28 
29 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
30     GST_PAD_SRC,
31     GST_PAD_ALWAYS,
32     GST_STATIC_CAPS ("video/x-raw, "
33         "format = (string) NV12, "
34         "width = (int) [1, MAX], "
35         "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
36 
37 static GstPad *sinkpad, *srcpad;
38 
39 static GstElement *
setup_nvenc(const gchar * src_caps_str,GstCaps ** srccaps)40 setup_nvenc (const gchar * src_caps_str, GstCaps ** srccaps)
41 {
42   GstElement *nvenc;
43   GstCaps *caps = NULL;
44   GstBus *bus;
45 
46   caps = gst_caps_from_string (src_caps_str);
47   fail_unless (caps != NULL);
48 
49   nvenc = gst_check_setup_element ("nvh264enc");
50   fail_unless (nvenc != NULL);
51   srcpad = gst_check_setup_src_pad (nvenc, &srctemplate);
52   sinkpad = gst_check_setup_sink_pad (nvenc, &sinktemplate);
53   gst_pad_set_active (srcpad, TRUE);
54   gst_pad_set_active (sinkpad, TRUE);
55 
56   bus = gst_bus_new ();
57   gst_element_set_bus (nvenc, bus);
58 
59   *srccaps = caps;
60 
61   buffers = NULL;
62   return nvenc;
63 }
64 
65 static void
cleanup_nvenc(GstElement * nvenc)66 cleanup_nvenc (GstElement * nvenc)
67 {
68   GstBus *bus;
69 
70   /* Free parsed buffers */
71   gst_check_drop_buffers ();
72 
73   bus = GST_ELEMENT_BUS (nvenc);
74   gst_bus_set_flushing (bus, TRUE);
75   gst_object_unref (bus);
76 
77   gst_pad_set_active (srcpad, FALSE);
78   gst_pad_set_active (sinkpad, FALSE);
79   gst_check_teardown_src_pad (nvenc);
80   gst_check_teardown_sink_pad (nvenc);
81   gst_check_teardown_element (nvenc);
82 }
83 
GST_START_TEST(test_encode_simple)84 GST_START_TEST (test_encode_simple)
85 {
86   GstElement *nvenc;
87   GstBuffer *buffer;
88   gint i;
89   GList *iter;
90   GstCaps *outcaps, *sinkcaps, *srccaps;
91   GstSegment seg;
92 
93   nvenc =
94       setup_nvenc
95       ("video/x-raw,format=(string)NV12,width=(int)320,height=(int)240,"
96       "framerate=(fraction)25/1,interlace-mode=(string)progressive", &srccaps);
97 
98   ASSERT_SET_STATE (nvenc, GST_STATE_PLAYING, GST_STATE_CHANGE_SUCCESS);
99 
100   gst_segment_init (&seg, GST_FORMAT_TIME);
101   seg.stop = gst_util_uint64_scale (10, GST_SECOND, 25);
102 
103   gst_check_setup_events (srcpad, nvenc, srccaps, GST_FORMAT_TIME);
104   fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
105 
106   buffer = gst_buffer_new_allocate (NULL, 320 * 240 + 2 * 160 * 120, NULL);
107   gst_buffer_memset (buffer, 0, 0, -1);
108 
109   for (i = 0; i < 10; i++) {
110     GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (i, GST_SECOND, 25);
111     GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25);
112     fail_unless (gst_pad_push (srcpad, gst_buffer_ref (buffer)) == GST_FLOW_OK);
113   }
114 
115   gst_buffer_unref (buffer);
116 
117   fail_unless (gst_pad_push_event (srcpad, gst_event_new_eos ()));
118 
119   /* All buffers must be there now */
120   fail_unless_equals_int (g_list_length (buffers), 10);
121 
122   outcaps =
123       gst_caps_from_string
124       ("video/x-h264,width=(int)320,height=(int)240,framerate=(fraction)25/1");
125 
126   for (iter = buffers; iter; iter = g_list_next (iter)) {
127     buffer = iter->data;
128 
129     fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
130         gst_util_uint64_scale (1, GST_SECOND, 25));
131 
132     sinkcaps = gst_pad_get_current_caps (sinkpad);
133     fail_unless (gst_caps_can_intersect (sinkcaps, outcaps));
134     gst_caps_unref (sinkcaps);
135   }
136 
137   gst_caps_unref (outcaps);
138   gst_caps_unref (srccaps);
139 
140   cleanup_nvenc (nvenc);
141 }
142 
143 GST_END_TEST;
144 
145 
GST_START_TEST(test_reuse)146 GST_START_TEST (test_reuse)
147 {
148   GstElement *nvenc;
149   GstBuffer *buffer;
150   gint i, loop;
151   GList *iter;
152   GstCaps *outcaps, *sinkcaps;
153   GstSegment seg;
154   GstCaps *srccaps;
155 
156   nvenc =
157       setup_nvenc
158       ("video/x-raw,format=(string)NV12,width=(int)320,height=(int)240,"
159       "framerate=(fraction)25/1,interlace-mode=(string)progressive", &srccaps);
160 
161   for (loop = 0; loop < 2; loop++) {
162     ASSERT_SET_STATE (nvenc, GST_STATE_PLAYING, GST_STATE_CHANGE_SUCCESS);
163 
164     gst_segment_init (&seg, GST_FORMAT_TIME);
165     seg.stop = gst_util_uint64_scale (10, GST_SECOND, 25);
166 
167     gst_check_setup_events (srcpad, nvenc, srccaps, GST_FORMAT_TIME);
168     fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
169 
170     gst_segment_init (&seg, GST_FORMAT_TIME);
171     seg.stop = gst_util_uint64_scale (10, GST_SECOND, 25);
172 
173     fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
174 
175     buffer = gst_buffer_new_allocate (NULL, 320 * 240 + 2 * 160 * 120, NULL);
176     gst_buffer_memset (buffer, 0, 0, -1);
177 
178     for (i = 0; i < 10; i++) {
179       GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (i, GST_SECOND, 25);
180       GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25);
181       fail_unless (gst_pad_push (srcpad,
182               gst_buffer_ref (buffer)) == GST_FLOW_OK);
183     }
184 
185     gst_buffer_unref (buffer);
186 
187     fail_unless (gst_pad_push_event (srcpad, gst_event_new_eos ()));
188 
189     /* All buffers must be there now */
190     fail_unless_equals_int (g_list_length (buffers), 10);
191 
192     outcaps =
193         gst_caps_from_string
194         ("video/x-h264,width=(int)320,height=(int)240,framerate=(fraction)25/1");
195 
196     for (iter = buffers; iter; iter = g_list_next (iter)) {
197       buffer = iter->data;
198 
199       fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
200           gst_util_uint64_scale (1, GST_SECOND, 25));
201 
202       sinkcaps = gst_pad_get_current_caps (sinkpad);
203       fail_unless (gst_caps_can_intersect (sinkcaps, outcaps));
204       gst_caps_unref (sinkcaps);
205     }
206     gst_check_drop_buffers ();
207     gst_caps_unref (outcaps);
208 
209     ASSERT_SET_STATE (nvenc, GST_STATE_READY, GST_STATE_CHANGE_SUCCESS);
210   }
211 
212   gst_caps_unref (srccaps);
213 
214   cleanup_nvenc (nvenc);
215 }
216 
217 GST_END_TEST;
218 
219 
220 static gboolean
check_nvenc_available(void)221 check_nvenc_available (void)
222 {
223   gboolean ret = TRUE;
224   GstElement *nvenc;
225 
226   nvenc = gst_element_factory_make ("nvh264enc", NULL);
227   if (!nvenc) {
228     GST_WARNING ("nvh264enc is not available, possibly driver load failure");
229     return FALSE;
230   }
231 
232   /* GST_STATE_READY is meaning that driver could be loaded */
233   if (gst_element_set_state (nvenc,
234           GST_STATE_PAUSED) != GST_STATE_CHANGE_SUCCESS) {
235     GST_WARNING ("cannot open device");
236     ret = FALSE;
237   }
238 
239   gst_element_set_state (nvenc, GST_STATE_NULL);
240   gst_object_unref (nvenc);
241 
242   return ret;
243 }
244 
245 static Suite *
nvenc_suite(void)246 nvenc_suite (void)
247 {
248   Suite *s;
249   TCase *tc_chain;
250 
251   /* HACK: cuda device init/deinit with fork seems to problematic */
252   g_setenv ("CK_FORK", "no", TRUE);
253 
254   s = suite_create ("nvenc");
255   tc_chain = tcase_create ("general");
256 
257   suite_add_tcase (s, tc_chain);
258 
259   if (!check_nvenc_available ()) {
260     GST_DEBUG ("Skip nvenc test since cannot open device");
261     goto end;
262   }
263 
264   tcase_add_test (tc_chain, test_encode_simple);
265   tcase_add_test (tc_chain, test_reuse);
266 
267 end:
268   return s;
269 }
270 
271 GST_CHECK_MAIN (nvenc);
272