1 /* GStreamer
2 *
3 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4 * Author: Thiago Santos <ts.santos@sisa.samsung.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <gst/gst.h>
26 #include <gst/check/gstcheck.h>
27 #include <gst/check/gstharness.h>
28 #include <gst/video/video.h>
29 #include <gst/app/app.h>
30
31 static GstPad *mysrcpad, *mysinkpad;
32 static GstElement *enc;
33 static GList *events = NULL;
34
35 #define TEST_VIDEO_WIDTH 640
36 #define TEST_VIDEO_HEIGHT 480
37 #define TEST_VIDEO_FPS_N 30
38 #define TEST_VIDEO_FPS_D 1
39
40 #define GST_VIDEO_ENCODER_TESTER_TYPE gst_video_encoder_tester_get_type()
41 static GType gst_video_encoder_tester_get_type (void);
42
43 typedef struct _GstVideoEncoderTester GstVideoEncoderTester;
44 typedef struct _GstVideoEncoderTesterClass GstVideoEncoderTesterClass;
45
46 struct _GstVideoEncoderTester
47 {
48 GstVideoEncoder parent;
49
50 GstFlowReturn pre_push_result;
51 };
52
53 struct _GstVideoEncoderTesterClass
54 {
55 GstVideoEncoderClass parent_class;
56 };
57
58 G_DEFINE_TYPE (GstVideoEncoderTester, gst_video_encoder_tester,
59 GST_TYPE_VIDEO_ENCODER);
60
61 static gboolean
gst_video_encoder_tester_start(GstVideoEncoder * enc)62 gst_video_encoder_tester_start (GstVideoEncoder * enc)
63 {
64 return TRUE;
65 }
66
67 static gboolean
gst_video_encoder_tester_stop(GstVideoEncoder * enc)68 gst_video_encoder_tester_stop (GstVideoEncoder * enc)
69 {
70 return TRUE;
71 }
72
73 static gboolean
gst_video_encoder_tester_set_format(GstVideoEncoder * enc,GstVideoCodecState * state)74 gst_video_encoder_tester_set_format (GstVideoEncoder * enc,
75 GstVideoCodecState * state)
76 {
77 GstVideoCodecState *res = gst_video_encoder_set_output_state (enc,
78 gst_caps_new_simple ("video/x-test-custom", "width", G_TYPE_INT,
79 480, "height", G_TYPE_INT, 360, NULL),
80 NULL);
81
82 gst_video_codec_state_unref (res);
83 return TRUE;
84 }
85
86 static GstFlowReturn
gst_video_encoder_tester_handle_frame(GstVideoEncoder * enc,GstVideoCodecFrame * frame)87 gst_video_encoder_tester_handle_frame (GstVideoEncoder * enc,
88 GstVideoCodecFrame * frame)
89 {
90 guint8 *data;
91 GstMapInfo map;
92 guint64 input_num;
93 GstClockTimeDiff deadline;
94
95 deadline = gst_video_encoder_get_max_encode_time (enc, frame);
96 if (deadline < 0) {
97 /* Calling finish_frame() with frame->output_buffer == NULL means to drop it */
98 goto out;
99 }
100
101 gst_buffer_map (frame->input_buffer, &map, GST_MAP_READ);
102 input_num = *((guint64 *) map.data);
103 gst_buffer_unmap (frame->input_buffer, &map);
104
105 data = g_malloc (sizeof (guint64));
106 *(guint64 *) data = input_num;
107
108 frame->output_buffer = gst_buffer_new_wrapped (data, sizeof (guint64));
109 frame->pts = GST_BUFFER_PTS (frame->input_buffer);
110 frame->duration = GST_BUFFER_DURATION (frame->input_buffer);
111
112 out:
113 return gst_video_encoder_finish_frame (enc, frame);
114 }
115
116 static GstFlowReturn
gst_video_encoder_tester_pre_push(GstVideoEncoder * enc,GstVideoCodecFrame * frame)117 gst_video_encoder_tester_pre_push (GstVideoEncoder * enc,
118 GstVideoCodecFrame * frame)
119 {
120 GstVideoEncoderTester *tester = (GstVideoEncoderTester *) enc;
121
122 return tester->pre_push_result;
123 }
124
125 static void
gst_video_encoder_tester_class_init(GstVideoEncoderTesterClass * klass)126 gst_video_encoder_tester_class_init (GstVideoEncoderTesterClass * klass)
127 {
128 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
129 GstVideoEncoderClass *videoencoder_class = GST_VIDEO_ENCODER_CLASS (klass);
130
131 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
132 GST_PAD_SINK, GST_PAD_ALWAYS,
133 GST_STATIC_CAPS ("video/x-raw"));
134
135 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
136 GST_PAD_SRC, GST_PAD_ALWAYS,
137 GST_STATIC_CAPS ("video/x-test-custom"));
138
139 gst_element_class_add_static_pad_template (element_class, &sink_templ);
140 gst_element_class_add_static_pad_template (element_class, &src_templ);
141
142 gst_element_class_set_metadata (element_class,
143 "VideoEncoderTester", "Encoder/Video", "yep", "me");
144
145 videoencoder_class->start = gst_video_encoder_tester_start;
146 videoencoder_class->stop = gst_video_encoder_tester_stop;
147 videoencoder_class->handle_frame = gst_video_encoder_tester_handle_frame;
148 videoencoder_class->pre_push = gst_video_encoder_tester_pre_push;
149 videoencoder_class->set_format = gst_video_encoder_tester_set_format;
150 }
151
152 static void
gst_video_encoder_tester_init(GstVideoEncoderTester * tester)153 gst_video_encoder_tester_init (GstVideoEncoderTester * tester)
154 {
155 tester->pre_push_result = GST_FLOW_OK;
156 }
157
158 static gboolean
_mysinkpad_event(GstPad * pad,GstObject * parent,GstEvent * event)159 _mysinkpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
160 {
161 events = g_list_append (events, event);
162 return TRUE;
163 }
164
165 static void
setup_videoencodertester(void)166 setup_videoencodertester (void)
167 {
168 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
169 GST_PAD_SINK,
170 GST_PAD_ALWAYS,
171 GST_STATIC_CAPS ("video/x-test-custom")
172 );
173 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
174 GST_PAD_SRC,
175 GST_PAD_ALWAYS,
176 GST_STATIC_CAPS ("video/x-raw")
177 );
178
179 enc = g_object_new (GST_VIDEO_ENCODER_TESTER_TYPE, NULL);
180 mysrcpad = gst_check_setup_src_pad (enc, &srctemplate);
181 mysinkpad = gst_check_setup_sink_pad (enc, &sinktemplate);
182
183 gst_pad_set_event_function (mysinkpad, _mysinkpad_event);
184 }
185
186 static void
cleanup_videoencodertest(void)187 cleanup_videoencodertest (void)
188 {
189 gst_pad_set_active (mysrcpad, FALSE);
190 gst_pad_set_active (mysinkpad, FALSE);
191
192 gst_element_set_state (enc, GST_STATE_NULL);
193
194 gst_check_teardown_src_pad (enc);
195 gst_check_teardown_sink_pad (enc);
196 gst_check_teardown_element (enc);
197
198 g_list_free_full (events, (GDestroyNotify) gst_event_unref);
199 events = NULL;
200 }
201
202 static GstBuffer *
create_test_buffer(guint64 num)203 create_test_buffer (guint64 num)
204 {
205 GstBuffer *buffer;
206 guint64 *data = g_malloc (sizeof (guint64));
207
208 *data = num;
209
210 buffer = gst_buffer_new_wrapped (data, sizeof (guint64));
211
212 GST_BUFFER_PTS (buffer) =
213 gst_util_uint64_scale_round (num, GST_SECOND * TEST_VIDEO_FPS_D,
214 TEST_VIDEO_FPS_N);
215 GST_BUFFER_DURATION (buffer) =
216 gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
217 TEST_VIDEO_FPS_N);
218
219 return buffer;
220 }
221
222 static GstCaps *
create_test_caps(void)223 create_test_caps (void)
224 {
225 return gst_caps_new_simple ("video/x-raw", "width", G_TYPE_INT,
226 TEST_VIDEO_WIDTH, "height", G_TYPE_INT, TEST_VIDEO_HEIGHT, "framerate",
227 GST_TYPE_FRACTION, TEST_VIDEO_FPS_N, TEST_VIDEO_FPS_D,
228 "format", G_TYPE_STRING, "GRAY8", NULL);
229 }
230
231 static void
send_startup_events(void)232 send_startup_events (void)
233 {
234 GstCaps *caps;
235
236 fail_unless (gst_pad_push_event (mysrcpad,
237 gst_event_new_stream_start ("randomvalue")));
238
239 /* push caps */
240 caps = create_test_caps ();
241 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_caps (caps)));
242 gst_caps_unref (caps);
243 }
244
245 #define NUM_BUFFERS 100
GST_START_TEST(videoencoder_playback)246 GST_START_TEST (videoencoder_playback)
247 {
248 GstSegment segment;
249 GstBuffer *buffer;
250 guint64 i;
251 GList *iter;
252
253 setup_videoencodertester ();
254
255 gst_pad_set_active (mysrcpad, TRUE);
256 gst_element_set_state (enc, GST_STATE_PLAYING);
257 gst_pad_set_active (mysinkpad, TRUE);
258
259 send_startup_events ();
260
261 /* push a new segment */
262 gst_segment_init (&segment, GST_FORMAT_TIME);
263 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
264
265 /* push buffers, the data is actually a number so we can track them */
266 for (i = 0; i < NUM_BUFFERS; i++) {
267 buffer = create_test_buffer (i);
268
269 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
270 }
271
272 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
273
274 /* check that all buffers were received by our source pad */
275 fail_unless (g_list_length (buffers) == NUM_BUFFERS);
276 i = 0;
277 for (iter = buffers; iter; iter = g_list_next (iter)) {
278 GstMapInfo map;
279 guint64 num;
280
281 buffer = iter->data;
282
283 gst_buffer_map (buffer, &map, GST_MAP_READ);
284
285 num = *(guint64 *) map.data;
286 fail_unless (i == num);
287 fail_unless (GST_BUFFER_PTS (buffer) == gst_util_uint64_scale_round (i,
288 GST_SECOND * TEST_VIDEO_FPS_D, TEST_VIDEO_FPS_N));
289 fail_unless (GST_BUFFER_DURATION (buffer) ==
290 gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
291 TEST_VIDEO_FPS_N));
292
293 gst_buffer_unmap (buffer, &map);
294 i++;
295 }
296
297 g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
298 buffers = NULL;
299
300 cleanup_videoencodertest ();
301 }
302
303 GST_END_TEST;
304
305 /* make sure tags sent right before eos are pushed */
GST_START_TEST(videoencoder_tags_before_eos)306 GST_START_TEST (videoencoder_tags_before_eos)
307 {
308 GstSegment segment;
309 GstBuffer *buffer;
310 GstTagList *tags;
311
312 setup_videoencodertester ();
313
314 gst_pad_set_active (mysrcpad, TRUE);
315 gst_element_set_state (enc, GST_STATE_PLAYING);
316 gst_pad_set_active (mysinkpad, TRUE);
317
318 send_startup_events ();
319
320 /* push a new segment */
321 gst_segment_init (&segment, GST_FORMAT_TIME);
322 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
323
324 /* push buffer */
325 buffer = create_test_buffer (0);
326 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
327
328 /* clean received events list */
329 g_list_free_full (events, (GDestroyNotify) gst_event_unref);
330 events = NULL;
331
332 /* push a tag event */
333 tags = gst_tag_list_new (GST_TAG_COMMENT, "test-comment", NULL);
334 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_tag (tags)));
335
336 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
337
338 /* check that the tag was received */
339 {
340 GstEvent *tag_event = events->data;
341 gchar *str;
342
343 fail_unless (GST_EVENT_TYPE (tag_event) == GST_EVENT_TAG);
344 gst_event_parse_tag (tag_event, &tags);
345 fail_unless (gst_tag_list_get_string (tags, GST_TAG_COMMENT, &str));
346 fail_unless (strcmp (str, "test-comment") == 0);
347 g_free (str);
348 }
349
350 g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
351 buffers = NULL;
352 g_list_free_full (events, (GDestroyNotify) gst_event_unref);
353 events = NULL;
354
355 cleanup_videoencodertest ();
356 }
357
358 GST_END_TEST;
359
360 /* make sure events sent right before eos are pushed */
GST_START_TEST(videoencoder_events_before_eos)361 GST_START_TEST (videoencoder_events_before_eos)
362 {
363 GstSegment segment;
364 GstBuffer *buffer;
365 GstMessage *msg;
366
367 setup_videoencodertester ();
368
369 gst_pad_set_active (mysrcpad, TRUE);
370 gst_element_set_state (enc, GST_STATE_PLAYING);
371 gst_pad_set_active (mysinkpad, TRUE);
372
373 send_startup_events ();
374
375 /* push a new segment */
376 gst_segment_init (&segment, GST_FORMAT_TIME);
377 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
378
379 /* push buffer */
380 buffer = create_test_buffer (0);
381 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
382
383 /* clean received events list */
384 g_list_free_full (events, (GDestroyNotify) gst_event_unref);
385 events = NULL;
386
387 /* push a serialized event */
388 msg =
389 gst_message_new_element (GST_OBJECT (mysrcpad),
390 gst_structure_new_empty ("test"));
391 fail_unless (gst_pad_push_event (mysrcpad,
392 gst_event_new_sink_message ("sink-test", msg)));
393 gst_message_unref (msg);
394
395 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
396
397 /* check that the tag was received */
398 {
399 GstEvent *msg_event = events->data;
400 const GstStructure *structure;
401
402 fail_unless (GST_EVENT_TYPE (msg_event) == GST_EVENT_SINK_MESSAGE);
403 fail_unless (gst_event_has_name (msg_event, "sink-test"));
404 gst_event_parse_sink_message (msg_event, &msg);
405 structure = gst_message_get_structure (msg);
406 fail_unless (gst_structure_has_name (structure, "test"));
407 gst_message_unref (msg);
408 }
409
410 g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
411 buffers = NULL;
412 g_list_free_full (events, (GDestroyNotify) gst_event_unref);
413 events = NULL;
414
415 cleanup_videoencodertest ();
416 }
417
418 GST_END_TEST;
419
GST_START_TEST(videoencoder_flush_events)420 GST_START_TEST (videoencoder_flush_events)
421 {
422 GstSegment segment;
423 GstBuffer *buffer;
424 guint i;
425 GList *events_iter;
426
427 setup_videoencodertester ();
428
429 gst_pad_set_active (mysrcpad, TRUE);
430 gst_element_set_state (enc, GST_STATE_PLAYING);
431 gst_pad_set_active (mysinkpad, TRUE);
432
433 send_startup_events ();
434
435 /* push a new segment */
436 gst_segment_init (&segment, GST_FORMAT_TIME);
437 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
438
439 /* push buffers, the data is actually a number so we can track them */
440 for (i = 0; i < NUM_BUFFERS; i++) {
441 if (i % 10 == 0) {
442 GstTagList *tags;
443
444 tags = gst_tag_list_new (GST_TAG_TRACK_NUMBER, i, NULL);
445 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_tag (tags)));
446 } else {
447 buffer = create_test_buffer (i);
448
449 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
450 }
451 }
452
453 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
454
455 events_iter = events;
456 /* make sure the usual events have been received */
457 {
458 GstEvent *sstart = events_iter->data;
459 fail_unless (GST_EVENT_TYPE (sstart) == GST_EVENT_STREAM_START);
460 events_iter = g_list_next (events_iter);
461 }
462 {
463 GstEvent *caps_event = events_iter->data;
464 fail_unless (GST_EVENT_TYPE (caps_event) == GST_EVENT_CAPS);
465 events_iter = g_list_next (events_iter);
466 }
467 {
468 GstEvent *segment_event = events_iter->data;
469 fail_unless (GST_EVENT_TYPE (segment_event) == GST_EVENT_SEGMENT);
470 events_iter = g_list_next (events_iter);
471 }
472
473 /* check that EOS was received */
474 fail_unless (GST_PAD_IS_EOS (mysrcpad));
475 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_flush_start ()));
476 fail_unless (GST_PAD_IS_EOS (mysrcpad));
477
478 /* Check that we have tags */
479 {
480 GstEvent *tags = gst_pad_get_sticky_event (mysrcpad, GST_EVENT_TAG, 0);
481
482 fail_unless (tags != NULL);
483 gst_event_unref (tags);
484 }
485
486 /* Check that we still have a segment set */
487 {
488 GstEvent *segment =
489 gst_pad_get_sticky_event (mysrcpad, GST_EVENT_SEGMENT, 0);
490
491 fail_unless (segment != NULL);
492 gst_event_unref (segment);
493 }
494
495 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_flush_stop (TRUE)));
496 fail_if (GST_PAD_IS_EOS (mysrcpad));
497
498 /* Check that the segment was flushed on FLUSH_STOP */
499 {
500 GstEvent *segment =
501 gst_pad_get_sticky_event (mysrcpad, GST_EVENT_SEGMENT, 0);
502
503 fail_unless (segment == NULL);
504 }
505
506 /* Check the tags were not lost on FLUSH_STOP */
507 {
508 GstEvent *tags = gst_pad_get_sticky_event (mysrcpad, GST_EVENT_TAG, 0);
509
510 fail_unless (tags != NULL);
511 gst_event_unref (tags);
512
513 }
514
515 g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
516 buffers = NULL;
517
518 cleanup_videoencodertest ();
519 }
520
521 GST_END_TEST;
522
523 /* When pre_push fails the correct GstFlowReturn should be returned and there
524 * should be no leaks */
GST_START_TEST(videoencoder_pre_push_fails)525 GST_START_TEST (videoencoder_pre_push_fails)
526 {
527 GstVideoEncoderTester *tester;
528 GstHarness *h;
529
530 tester = g_object_new (GST_VIDEO_ENCODER_TESTER_TYPE, NULL);
531 tester->pre_push_result = GST_FLOW_ERROR;
532
533 h = gst_harness_new_with_element (GST_ELEMENT (tester), "sink", "src");
534 gst_harness_set_src_caps (h, create_test_caps ());
535
536 fail_unless_equals_int (gst_harness_push (h, create_test_buffer (0)),
537 GST_FLOW_ERROR);
538
539 gst_harness_teardown (h);
540 gst_object_unref (tester);
541 }
542
543 GST_END_TEST;
544
GST_START_TEST(videoencoder_qos)545 GST_START_TEST (videoencoder_qos)
546 {
547 GstSegment segment;
548 GstBuffer *buffer;
549 GstClockTime ts, rt;
550 GstBus *bus;
551 GstMessage *msg;
552
553 setup_videoencodertester ();
554
555 gst_video_encoder_set_qos_enabled (GST_VIDEO_ENCODER (enc), TRUE);
556
557 gst_pad_set_active (mysrcpad, TRUE);
558 gst_element_set_state (enc, GST_STATE_PLAYING);
559 gst_pad_set_active (mysinkpad, TRUE);
560
561 bus = gst_bus_new ();
562 gst_element_set_bus (enc, bus);
563
564 send_startup_events ();
565
566 /* push a new segment */
567 gst_segment_init (&segment, GST_FORMAT_TIME);
568 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
569
570 /* push the first buffer */
571 buffer = create_test_buffer (0);
572 ts = GST_BUFFER_PTS (buffer);
573 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
574 buffer = NULL;
575
576 /* pretend this buffer was late in the sink */
577 rt = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, ts);
578 fail_unless (gst_pad_push_event (mysinkpad,
579 gst_event_new_qos (GST_QOS_TYPE_UNDERFLOW, 1.5, 500 * GST_MSECOND,
580 rt)));
581
582 /* push a second buffer which will be dropped as it's already late */
583 buffer = create_test_buffer (1);
584 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
585 buffer = NULL;
586
587 /* A QoS message was sent by the encoder */
588 msg = gst_bus_pop_filtered (bus, GST_MESSAGE_QOS);
589 g_assert (msg != NULL);
590 gst_message_unref (msg);
591
592 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
593
594 gst_bus_set_flushing (bus, TRUE);
595 gst_object_unref (bus);
596
597 g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
598 buffers = NULL;
599
600 cleanup_videoencodertest ();
601 }
602
603 GST_END_TEST;
604
605 static Suite *
gst_videoencoder_suite(void)606 gst_videoencoder_suite (void)
607 {
608 Suite *s = suite_create ("GstVideoEncoder");
609 TCase *tc = tcase_create ("general");
610
611 suite_add_tcase (s, tc);
612 tcase_add_test (tc, videoencoder_playback);
613
614 tcase_add_test (tc, videoencoder_tags_before_eos);
615 tcase_add_test (tc, videoencoder_events_before_eos);
616 tcase_add_test (tc, videoencoder_flush_events);
617 tcase_add_test (tc, videoencoder_pre_push_fails);
618 tcase_add_test (tc, videoencoder_qos);
619
620 return s;
621 }
622
623 GST_CHECK_MAIN (gst_videoencoder);
624