summaryrefslogtreecommitdiff
path: root/tests/check/elements/rtph263.c
blob: 28c01981fe2da0c59766bb5a5437c7cf17b67360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* GStreamer
 *
 * Copyright (C) 2015 Pexip AS
 *   @author Stian Selnes <stian@pexip.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <gst/check/check.h>
#include <gst/check/gstharness.h>
#include <gst/rtp/gstrtpbuffer.h>

#define RTP_H263_CAPS_STR(p)                                            \
  "application/x-rtp,media=video,encoding-name=H263,clock-rate=90000,"  \
  "payload=" G_STRINGIFY(p)

#define H263P_RTP_CAPS_STR(p)                                           \
  "application/x-rtp,media=video,encoding-name=H263-1998,clock-rate=90000," \
  "payload="G_STRINGIFY(p)

static gboolean
have_element (const gchar * element_name)
{
  GstElement *element;
  gboolean ret;

  element = gst_element_factory_make (element_name, NULL);
  ret = element != NULL;

  if (element)
    gst_object_unref (element);

  return ret;
}

static GstBuffer *
create_rtp_buffer (guint8 * data, gsize size, guint ts, gint seqnum)
{
  GstBuffer *buf = gst_rtp_buffer_new_copy_data (data, size);
  GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;

  GST_BUFFER_PTS (buf) = (ts) * (GST_SECOND / 30);

  gst_rtp_buffer_map (buf, GST_MAP_WRITE, &rtp);
  gst_rtp_buffer_set_seq (&rtp, seqnum);
  gst_rtp_buffer_unmap (&rtp);

  return buf;
}

GST_START_TEST (test_h263depay_start_packet_too_small_mode_a)
{
  GstHarness *h = gst_harness_new ("rtph263depay");
  guint8 packet[] = {
    0x80, 0xa2, 0x17, 0x62, 0x57, 0xbb, 0x48, 0x98, 0x4a, 0x59, 0xe8, 0xdc,
    0x00, 0x00, 0x80, 0x00
  };

  gst_harness_set_src_caps_str (h, RTP_H263_CAPS_STR (34));
  fail_unless_equals_int (GST_FLOW_OK,
      gst_harness_push (h, create_rtp_buffer (packet, sizeof (packet), 0, 0)));

  /* Packet should be dropped and depayloader not crash */
  fail_unless_equals_int (0, gst_harness_buffers_received (h));

  gst_harness_teardown (h);
}

GST_END_TEST;

GST_START_TEST (test_h263depay_start_packet_too_small_mode_b)
{
  GstHarness *h = gst_harness_new ("rtph263depay");
  guint8 packet[] = {
    0x80, 0xa2, 0x17, 0x62, 0x57, 0xbb, 0x48, 0x98, 0x4a, 0x59, 0xe8, 0xdc,
    0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00
  };

  gst_harness_set_src_caps_str (h, RTP_H263_CAPS_STR (34));
  fail_unless_equals_int (GST_FLOW_OK,
      gst_harness_push (h, create_rtp_buffer (packet, sizeof (packet), 0, 0)));

  /* Packet should be dropped and depayloader not crash */
  fail_unless_equals_int (0, gst_harness_buffers_received (h));

  gst_harness_teardown (h);
}

GST_END_TEST;

GST_START_TEST (test_h263depay_start_packet_too_small_mode_c)
{
  GstHarness *h = gst_harness_new ("rtph263depay");
  guint8 packet[] = {
    0x80, 0xa2, 0x17, 0x62, 0x57, 0xbb, 0x48, 0x98, 0x4a, 0x59, 0xe8, 0xdc,
    0xc0, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  };

  gst_harness_set_src_caps_str (h, RTP_H263_CAPS_STR (34));
  fail_unless_equals_int (GST_FLOW_OK,
      gst_harness_push (h, create_rtp_buffer (packet, sizeof (packet), 0, 0)));

  /* Packet should be dropped and depayloader not crash */
  fail_unless_equals_int (0, gst_harness_buffers_received (h));

  gst_harness_teardown (h);
}

GST_END_TEST;

GST_START_TEST (test_h263pay_mode_b_snow)
{
  /* Payloading one large frame (like snow) is more likely to use mode b and
   * trigger issues in valgrind seen previously like double free, invalid read
   * etc. */
  GstHarness *h;
  guint frames = 1;
  guint i;

  if (!have_element ("avenc_h263"))
    return;

  h = gst_harness_new_parse
      ("avenc_h263 rtp-payload-size=1 ! rtph263pay mtu=1350 ");
  gst_harness_add_src_parse (h,
      "videotestsrc pattern=snow is-live=1 ! "
      "capsfilter caps=\"video/x-raw,format=I420,width=176,height=144\"", TRUE);

  for (i = 0; i < frames; i++)
    gst_harness_push_from_src (h);
  fail_unless (gst_harness_buffers_received (h) >= frames);

  gst_harness_teardown (h);
}

GST_END_TEST;

/* gst_rtp_buffer_get_payload() may return a copy of the payload. This test
 * makes sure that the rtph263pdepay also produces the correct output in this
 * case. */
GST_START_TEST (test_h263pdepay_fragmented_memory_non_writable_buffer)
{
  GstHarness *h;
  GstBuffer *header_buf, *payload_buf, *buf;
  GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
  guint8 header[] = {
    0x04, 0x00
  };
  guint8 payload[] = {
    0x80, 0x02, 0x1c, 0xb8, 0x01, 0x00, 0x11, 0xe0, 0x44, 0xc4
  };
  guint8 frame[] = {
    0x00, 0x00, 0x80, 0x02, 0x1c, 0xb8, 0x01, 0x00, 0x11, 0xe0, 0x44, 0xc4
  };

  h = gst_harness_new ("rtph263pdepay");
  gst_harness_set_src_caps_str (h, "application/x-rtp, media=video, "
      "clock-rate=90000, encoding-name=H263-1998");

  /* Packet with M=1, P=1 */
  header_buf = gst_rtp_buffer_new_allocate (sizeof (header), 0, 0);
  gst_rtp_buffer_map (header_buf, GST_MAP_WRITE, &rtp);
  gst_rtp_buffer_set_marker (&rtp, TRUE);
  memcpy (gst_rtp_buffer_get_payload (&rtp), header, sizeof (header));
  gst_rtp_buffer_unmap (&rtp);

  payload_buf = gst_buffer_new_allocate (NULL, sizeof (payload), NULL);
  gst_buffer_fill (payload_buf, 0, payload, sizeof (payload));
  buf = gst_buffer_append (header_buf, payload_buf);

  gst_harness_push (h, gst_buffer_ref (buf));
  gst_buffer_unref (buf);

  buf = gst_harness_pull (h);
  fail_unless (gst_buffer_memcmp (buf, 0, frame, sizeof (frame)) == 0);
  gst_buffer_unref (buf);

  gst_harness_teardown (h);
}

GST_END_TEST;

/* gst_rtp_buffer_get_payload() may return a copy of the payload. This test
 * makes sure that the rtph263pdepay also produces the correct output in this
 * case. */
GST_START_TEST
    (test_h263pdepay_fragmented_memory_non_writable_buffer_split_frame) {
  GstHarness *h;
  GstBuffer *header_buf, *payload_buf, *buf;
  GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
  guint8 header[] = {
    0x04, 0x00
  };
  guint8 payload[] = {
    0x80, 0x02, 0x1c, 0xb8, 0x01, 0x00, 0x11, 0xe0, 0x44, 0xc4
  };
  guint8 frame[] = {
    0x00, 0x00, 0x80, 0x02, 0x1c, 0xb8, 0x01, 0x00, 0x11, 0xe0, 0x44, 0xc4
  };

  h = gst_harness_new ("rtph263pdepay");
  gst_harness_set_src_caps_str (h, "application/x-rtp, media=video, "
      "clock-rate=90000, encoding-name=H263-1998");

  /* First packet, M=0, P=1 */
  header_buf = gst_rtp_buffer_new_allocate (sizeof (header), 0, 0);
  gst_rtp_buffer_map (header_buf, GST_MAP_WRITE, &rtp);
  gst_rtp_buffer_set_marker (&rtp, FALSE);
  gst_rtp_buffer_set_seq (&rtp, 0);
  memcpy (gst_rtp_buffer_get_payload (&rtp), header, sizeof (header));
  gst_rtp_buffer_unmap (&rtp);

  payload_buf = gst_buffer_new_allocate (NULL, sizeof (payload), NULL);
  gst_buffer_fill (payload_buf, 0, payload, sizeof (payload));
  buf = gst_buffer_append (header_buf, payload_buf);

  gst_harness_push (h, gst_buffer_ref (buf));
  gst_buffer_unref (buf);
  fail_unless_equals_int (gst_harness_buffers_received (h), 0);

  /* Second packet, M=1, P=1 */
  header_buf = gst_rtp_buffer_new_allocate (sizeof (header), 0, 0);
  gst_rtp_buffer_map (header_buf, GST_MAP_WRITE, &rtp);
  gst_rtp_buffer_set_marker (&rtp, TRUE);
  gst_rtp_buffer_set_seq (&rtp, 1);
  memcpy (gst_rtp_buffer_get_payload (&rtp), header, sizeof (header));
  gst_rtp_buffer_unmap (&rtp);

  payload_buf = gst_buffer_new_allocate (NULL, sizeof (payload), NULL);
  gst_buffer_memset (payload_buf, 0, 0, 10);
  buf = gst_buffer_append (header_buf, payload_buf);

  gst_harness_push (h, gst_buffer_ref (buf));
  gst_buffer_unref (buf);
  fail_unless_equals_int (gst_harness_buffers_received (h), 1);

  buf = gst_harness_pull (h);
  fail_unless (gst_buffer_memcmp (buf, 0, frame, sizeof (frame)) == 0);
  gst_buffer_unref (buf);

  gst_harness_teardown (h);
}

GST_END_TEST;

GST_START_TEST (test_h263pdepay_dont_push_empty_frame)
{
  GstHarness *h = gst_harness_new ("rtph263pdepay");

  /* Packet that only contains header information and an extra picture header
   * (PLEN > 0). Partly handcrafted packet. Originally this packet did not
   * have P=1 (hence it was not start of a picture). With both P=1 and M=1 we
   * only need one packet to reproduce the issue where trying to push an empty
   * frame when PLEN is set */
  guint8 packet[] = {
    0x80, 0xe8, 0xbc, 0xaa, 0x14, 0x12, 0x16, 0x5c, 0xb8, 0x4e, 0x39, 0x04,
    0x25, 0x00, 0x54, 0x39, 0xd0, 0x12, 0x06, 0x9e, 0xb5, 0x0a, 0xf5, 0xe8,
    0x32, 0xeb, 0xd0, 0x6b, 0xd6, 0xa2, 0xfa, 0xd4, 0x3d, 0xd7, 0xa0, 0x2b,
    0x24, 0x97, 0xc3, 0xbf, 0xc0, 0xbb, 0xd7, 0xa0,
  };

  gst_harness_set_src_caps_str (h, H263P_RTP_CAPS_STR (100));

  fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h,
          create_rtp_buffer (packet, sizeof (packet), 0, 0)));

  fail_unless_equals_int (gst_harness_buffers_received (h), 0);

  gst_harness_teardown (h);
}

GST_END_TEST;

GST_START_TEST (test_h263ppay_non_fixed_caps)
{
  GstHarness *h;
  guint8 frame[] = {
    0x00, 0x00, 0x80, 0x06, 0x1c, 0xa8, 0x01, 0x04, 0x91, 0xe0, 0x37, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  };

  h = gst_harness_new_parse ("rtph263ppay");

  /* Set non-fixed caps after payloader */
  gst_harness_set_caps_str (h, "video/x-h263, variant=(string)itu",
      "application/x-rtp, clock-rate=[1, MAX]");

  gst_harness_push (h, gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY,
          frame, sizeof (frame), 0, sizeof (frame), NULL, NULL));

  fail_unless_equals_int (1, gst_harness_buffers_received (h));

  gst_harness_teardown (h);
}

GST_END_TEST;

static Suite *
rtph263_suite (void)
{
  Suite *s = suite_create ("rtph263");
  TCase *tc_chain;

  suite_add_tcase (s, (tc_chain = tcase_create ("h263depay")));
  tcase_add_test (tc_chain, test_h263depay_start_packet_too_small_mode_a);
  tcase_add_test (tc_chain, test_h263depay_start_packet_too_small_mode_b);
  tcase_add_test (tc_chain, test_h263depay_start_packet_too_small_mode_c);

  suite_add_tcase (s, (tc_chain = tcase_create ("h263pay")));
  tcase_add_test (tc_chain, test_h263pay_mode_b_snow);

  suite_add_tcase (s, (tc_chain = tcase_create ("h263pdepay")));
  tcase_add_test (tc_chain,
      test_h263pdepay_fragmented_memory_non_writable_buffer);
  tcase_add_test (tc_chain,
      test_h263pdepay_fragmented_memory_non_writable_buffer_split_frame);
  tcase_add_test (tc_chain, test_h263pdepay_dont_push_empty_frame);

  suite_add_tcase (s, (tc_chain = tcase_create ("h263ppay")));
  tcase_add_test (tc_chain, test_h263ppay_non_fixed_caps);

  return s;
}

GST_CHECK_MAIN (rtph263);