ba099f165d22ae5f6a68783bb485da7fcbdd4329
[platform/upstream/gst-plugins-good.git] / tests / check / elements / rtph263.c
1 /* GStreamer
2  *
3  * Copyright (C) 2015 Pexip AS
4  *   @author Stian Selnes <stian@pexip.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 #include <gst/check/check.h>
23 #include <gst/check/gstharness.h>
24 #include <gst/rtp/gstrtpbuffer.h>
25
26 #define RTP_H263_CAPS_STR(p)                                            \
27   "application/x-rtp,media=video,encoding-name=H263,clock-rate=90000,"  \
28   "payload=" G_STRINGIFY(p)
29
30 static gboolean
31 have_element (const gchar * element_name)
32 {
33   GstElement *element;
34   gboolean ret;
35
36   element = gst_element_factory_make (element_name, NULL);
37   ret = element != NULL;
38
39   if (element)
40     gst_object_unref (element);
41
42   return ret;
43 }
44
45 static GstBuffer *
46 create_rtp_buffer (guint8 * data, gsize size, guint ts, gint seqnum)
47 {
48   GstBuffer *buf = gst_rtp_buffer_new_copy_data (data, size);
49   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
50
51   GST_BUFFER_PTS (buf) = (ts) * (GST_SECOND / 30);
52
53   gst_rtp_buffer_map (buf, GST_MAP_WRITE, &rtp);
54   gst_rtp_buffer_set_seq (&rtp, seqnum);
55   gst_rtp_buffer_unmap (&rtp);
56
57   return buf;
58 }
59
60 GST_START_TEST (test_h263depay_start_packet_too_small_mode_a)
61 {
62   GstHarness *h = gst_harness_new ("rtph263depay");
63   guint8 packet[] = {
64     0x80, 0xa2, 0x17, 0x62, 0x57, 0xbb, 0x48, 0x98, 0x4a, 0x59, 0xe8, 0xdc,
65     0x00, 0x00, 0x80, 0x00
66   };
67
68   gst_harness_set_src_caps_str (h, RTP_H263_CAPS_STR (34));
69   fail_unless_equals_int (GST_FLOW_OK,
70       gst_harness_push (h, create_rtp_buffer (packet, sizeof (packet), 0, 0)));
71
72   /* Packet should be dropped and depayloader not crash */
73   fail_unless_equals_int (0, gst_harness_buffers_received (h));
74
75   gst_harness_teardown (h);
76 }
77
78 GST_END_TEST;
79
80 GST_START_TEST (test_h263depay_start_packet_too_small_mode_b)
81 {
82   GstHarness *h = gst_harness_new ("rtph263depay");
83   guint8 packet[] = {
84     0x80, 0xa2, 0x17, 0x62, 0x57, 0xbb, 0x48, 0x98, 0x4a, 0x59, 0xe8, 0xdc,
85     0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00
86   };
87
88   gst_harness_set_src_caps_str (h, RTP_H263_CAPS_STR (34));
89   fail_unless_equals_int (GST_FLOW_OK,
90       gst_harness_push (h, create_rtp_buffer (packet, sizeof (packet), 0, 0)));
91
92   /* Packet should be dropped and depayloader not crash */
93   fail_unless_equals_int (0, gst_harness_buffers_received (h));
94
95   gst_harness_teardown (h);
96 }
97
98 GST_END_TEST;
99
100 GST_START_TEST (test_h263depay_start_packet_too_small_mode_c)
101 {
102   GstHarness *h = gst_harness_new ("rtph263depay");
103   guint8 packet[] = {
104     0x80, 0xa2, 0x17, 0x62, 0x57, 0xbb, 0x48, 0x98, 0x4a, 0x59, 0xe8, 0xdc,
105     0xc0, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
106   };
107
108   gst_harness_set_src_caps_str (h, RTP_H263_CAPS_STR (34));
109   fail_unless_equals_int (GST_FLOW_OK,
110       gst_harness_push (h, create_rtp_buffer (packet, sizeof (packet), 0, 0)));
111
112   /* Packet should be dropped and depayloader not crash */
113   fail_unless_equals_int (0, gst_harness_buffers_received (h));
114
115   gst_harness_teardown (h);
116 }
117
118 GST_END_TEST;
119
120 GST_START_TEST (test_h263pay_mode_b_snow)
121 {
122   /* Payloading one large frame (like snow) is more likely to use mode b and
123    * trigger issues in valgrind seen previously like double free, invalid read
124    * etc. */
125   GstHarness *h;
126   guint frames = 1;
127   guint i;
128
129   if (!have_element ("avenc_h263"))
130     return;
131
132   h = gst_harness_new_parse (
133       "avenc_h263 rtp-payload-size=1 ! rtph263pay mtu=1350 ");
134   gst_harness_add_src_parse (h, "videotestsrc pattern=snow is-live=1 ! "
135       "capsfilter caps=\"video/x-raw,format=I420,width=176,height=144\"", TRUE);
136
137   for (i = 0; i < frames; i++)
138     gst_harness_push_from_src (h);
139   fail_unless (gst_harness_buffers_received (h) >= frames);
140
141   gst_harness_teardown (h);
142 }
143 GST_END_TEST;
144
145 static Suite *
146 rtph263_suite (void)
147 {
148   Suite *s = suite_create ("rtph263");
149   TCase *tc_chain;
150
151   suite_add_tcase (s, (tc_chain = tcase_create ("h263depay")));
152   tcase_add_test (tc_chain, test_h263depay_start_packet_too_small_mode_a);
153   tcase_add_test (tc_chain, test_h263depay_start_packet_too_small_mode_b);
154   tcase_add_test (tc_chain, test_h263depay_start_packet_too_small_mode_c);
155
156   suite_add_tcase (s, (tc_chain = tcase_create ("h263pay")));
157   tcase_add_test (tc_chain, test_h263pay_mode_b_snow);
158
159   return s;
160 }
161
162 GST_CHECK_MAIN (rtph263);