rtphdrext: Make write function return a signed value
[platform/upstream/gstreamer.git] / tests / check / libs / rtphdrext.c
1 /* GStreamer RTP header extension unit tests
2  * Copyright (C) 2020 Matthew Waters <matthew@centricular.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
15  * Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 #include "gst/gstcaps.h"
21 #include "gst/gstvalue.h"
22 #include "gst/rtp/gstrtphdrext.h"
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/gst.h>
28 #include <gst/check/check.h>
29 #include <gst/rtp/rtp.h>
30
31 #include "rtpdummyhdrextimpl.c"
32
33 GST_START_TEST (rtp_header_ext_write)
34 {
35   GstRTPHeaderExtension *dummy;
36   GstBuffer *buffer;
37   guint8 *data;
38   gsize size;
39   gssize written;
40
41   dummy = rtp_dummy_hdr_ext_new ();
42   gst_rtp_header_extension_set_id (dummy, 1);
43
44   buffer = gst_buffer_new ();
45   size = gst_rtp_header_extension_get_max_size (dummy, buffer);
46   fail_unless (size > 0);
47
48   data = g_malloc0 (size);
49   fail_unless (data != NULL);
50
51   written =
52       gst_rtp_header_extension_write (dummy, buffer, 0, buffer, data, size);
53   fail_unless (written > 0 && written <= size);
54   fail_unless_equals_int (GST_RTP_DUMMY_HDR_EXT (dummy)->write_count, 1);
55
56   fail_unless (gst_rtp_header_extension_read (dummy, 0, data, size, buffer));
57   fail_unless_equals_int (GST_RTP_DUMMY_HDR_EXT (dummy)->read_count, 1);
58
59   g_free (data);
60   gst_buffer_unref (buffer);
61   g_object_unref (dummy);
62 }
63
64 GST_END_TEST;
65
66 GST_START_TEST (rtp_header_ext_create_from_uri)
67 {
68   GstElementFactory *factory;
69   GstRTPHeaderExtension *dummy;
70
71   fail_unless (gst_element_register (NULL, "test-dummyrtphdrext",
72           GST_RANK_MARGINAL, GST_TYPE_RTP_DUMMY_HDR_EXT));
73
74   dummy = gst_rtp_header_extension_create_from_uri (DUMMY_HDR_EXT_URI);
75   fail_unless (GST_IS_RTP_DUMMY_HDR_EXT (dummy));
76
77   factory = gst_element_get_factory (GST_ELEMENT (dummy));
78   gst_registry_remove_feature (gst_registry_get (),
79       GST_PLUGIN_FEATURE (factory));
80   gst_object_unref (dummy);
81 }
82
83 GST_END_TEST;
84
85 GST_START_TEST (rtp_header_ext_caps_with_attributes)
86 {
87   GstRTPHeaderExtension *dummy;
88   GstCaps *caps = gst_caps_new_empty_simple ("application/x-rtp");
89   GstStructure *s = gst_caps_get_structure (caps, 0);
90   const GValue *arr, *val;
91   const gchar *attributes = "attr0 attr1";
92   const gchar *direction = "recvonly";
93
94   dummy = rtp_dummy_hdr_ext_new ();
95
96   gst_rtp_header_extension_set_id (dummy, 1);
97
98   GST_RTP_DUMMY_HDR_EXT (dummy)->direction = g_strdup (direction);
99   GST_RTP_DUMMY_HDR_EXT (dummy)->attributes = g_strdup (attributes);
100
101   fail_unless (gst_rtp_header_extension_set_caps_from_attributes (dummy, caps));
102   fail_unless (gst_structure_has_field_typed (s, "extmap-1", GST_TYPE_ARRAY));
103   arr = gst_structure_get_value (s, "extmap-1");
104   fail_unless (GST_VALUE_HOLDS_ARRAY (arr));
105   fail_unless_equals_int (gst_value_array_get_size (arr), 3);
106   val = gst_value_array_get_value (arr, 0);
107   fail_unless_equals_int (g_strcmp0 (g_value_get_string (val), direction), 0);
108   val = gst_value_array_get_value (arr, 1);
109   fail_unless_equals_int (g_strcmp0 (g_value_get_string (val),
110           gst_rtp_header_extension_get_uri (dummy)), 0);
111   val = gst_value_array_get_value (arr, 2);
112   fail_unless_equals_int (g_strcmp0 (g_value_get_string (val), attributes), 0);
113
114   g_free (GST_RTP_DUMMY_HDR_EXT (dummy)->direction);
115   GST_RTP_DUMMY_HDR_EXT (dummy)->direction = NULL;
116   g_free (GST_RTP_DUMMY_HDR_EXT (dummy)->attributes);
117   GST_RTP_DUMMY_HDR_EXT (dummy)->attributes = NULL;
118
119   fail_unless (gst_rtp_header_extension_set_attributes_from_caps (dummy, caps));
120
121   fail_unless_equals_int (g_strcmp0 (GST_RTP_DUMMY_HDR_EXT (dummy)->attributes,
122           attributes), 0);
123   fail_unless_equals_int (g_strcmp0 (GST_RTP_DUMMY_HDR_EXT (dummy)->direction,
124           direction), 0);
125
126   gst_caps_unref (caps);
127   gst_object_unref (dummy);
128 }
129
130 GST_END_TEST;
131
132 static Suite *
133 rtp_header_extension_suite (void)
134 {
135   Suite *s = suite_create ("rtp_header_extension_test");
136   TCase *tc_chain = tcase_create ("header extension test");
137
138   suite_add_tcase (s, tc_chain);
139   tcase_add_test (tc_chain, rtp_header_ext_write);
140   tcase_add_test (tc_chain, rtp_header_ext_create_from_uri);
141   tcase_add_test (tc_chain, rtp_header_ext_caps_with_attributes);
142
143   return s;
144 }
145
146 GST_CHECK_MAIN (rtp_header_extension)