1 /* GStreamer
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 Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 /**
21 * SECTION:rtphdrexttwcc
22 * @title: GstRtphdrext-TWCC
23 * @short_description: Helper methods for dealing with RTP header extensions
24 * in the Audio/Video RTP Profile for transport-wide-cc
25 * @see_also: #GstRTPHeaderExtension, #GstRTPBasePayload, #GstRTPBaseDepayload, gstrtpbuffer
26 *
27 * Since: 1.20
28 */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <gst/rtp/gstrtpbuffer.h>
34
35 #include "gstrtphdrext-twcc.h"
36
37 GST_DEBUG_CATEGORY_STATIC (rtphdrext_twcc_debug);
38 #define GST_CAT_DEFAULT (rtphdrext_twcc_debug)
39
40 #define gst_gl_base_filter_parent_class parent_class
41 G_DEFINE_TYPE_WITH_CODE (GstRTPHeaderExtensionTWCC,
42 gst_rtp_header_extension_twcc, GST_TYPE_RTP_HEADER_EXTENSION,
43 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "rtphdrexttwcc", 0,
44 "RTP TWCC Header Extensions");
45 );
46 GST_ELEMENT_REGISTER_DEFINE (rtphdrexttwcc, "rtphdrexttwcc", GST_RANK_MARGINAL,
47 GST_TYPE_RTP_HEADER_EXTENSION_TWCC);
48
49 #define TWCC_EXTMAP_STR "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
50
51 static void gst_rtp_header_extension_twcc_set_property (GObject * object,
52 guint prop_id, const GValue * value, GParamSpec * pspec);
53 static void gst_rtp_header_extension_twcc_get_property (GObject * object,
54 guint prop_id, GValue * value, GParamSpec * pspec);
55
56 static GstRTPHeaderExtensionFlags
57 gst_rtp_header_extension_twcc_get_supported_flags (GstRTPHeaderExtension * ext);
58 static gsize gst_rtp_header_extension_twcc_get_max_size (GstRTPHeaderExtension *
59 ext, const GstBuffer * buffer);
60 static gssize gst_rtp_header_extension_twcc_write (GstRTPHeaderExtension * ext,
61 const GstBuffer * input_meta, GstRTPHeaderExtensionFlags write_flags,
62 GstBuffer * output, guint8 * data, gsize size);
63 static gboolean gst_rtp_header_extension_twcc_read (GstRTPHeaderExtension * ext,
64 GstRTPHeaderExtensionFlags read_flags, const guint8 * data, gsize size,
65 GstBuffer * buffer);
66
67 enum
68 {
69 PROP_0,
70 PROP_N_STREAMS,
71 };
72
73 static void
gst_rtp_header_extension_twcc_class_init(GstRTPHeaderExtensionTWCCClass * klass)74 gst_rtp_header_extension_twcc_class_init (GstRTPHeaderExtensionTWCCClass *
75 klass)
76 {
77 GstRTPHeaderExtensionClass *rtp_hdr_class;
78 GstElementClass *gstelement_class;
79 GObjectClass *gobject_class;
80
81 rtp_hdr_class = (GstRTPHeaderExtensionClass *) klass;
82 gobject_class = (GObjectClass *) klass;
83 gstelement_class = (GstElementClass *) klass;
84
85 gobject_class->set_property = gst_rtp_header_extension_twcc_set_property;
86 gobject_class->get_property = gst_rtp_header_extension_twcc_get_property;
87
88 /**
89 * rtphdrexttwcc:n-streams:
90 *
91 * The number of independant RTP streams that are being used for the transport
92 * wide counter for TWCC. If set to 1 (the default), then any existing
93 * transport wide counter is kept.
94 *
95 * Since: 1.20
96 */
97 g_object_class_install_property (gobject_class, PROP_N_STREAMS,
98 g_param_spec_uint ("n-streams", "N Streams",
99 "The number of separate RTP streams this header applies to",
100 1, G_MAXUINT32, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
101
102 rtp_hdr_class->get_supported_flags =
103 gst_rtp_header_extension_twcc_get_supported_flags;
104 rtp_hdr_class->get_max_size = gst_rtp_header_extension_twcc_get_max_size;
105 rtp_hdr_class->write = gst_rtp_header_extension_twcc_write;
106 rtp_hdr_class->read = gst_rtp_header_extension_twcc_read;
107
108 gst_element_class_set_static_metadata (gstelement_class,
109 "Transport Wide Congestion Control", GST_RTP_HDREXT_ELEMENT_CLASS,
110 "Extends RTP packets to add sequence number transport wide.",
111 "Matthew Waters <matthew@centricular.com>");
112 gst_rtp_header_extension_class_set_uri (rtp_hdr_class, TWCC_EXTMAP_STR);
113 }
114
115 static void
gst_rtp_header_extension_twcc_init(GstRTPHeaderExtensionTWCC * twcc)116 gst_rtp_header_extension_twcc_init (GstRTPHeaderExtensionTWCC * twcc)
117 {
118 twcc->n_streams = 1;
119 }
120
121 static void
gst_rtp_header_extension_twcc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)122 gst_rtp_header_extension_twcc_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec)
124 {
125 GstRTPHeaderExtensionTWCC *twcc = GST_RTP_HEADER_EXTENSION_TWCC (object);
126
127 switch (prop_id) {
128 case PROP_N_STREAMS:
129 twcc->n_streams = g_value_get_uint (value);
130 break;
131 default:
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133 break;
134 }
135 }
136
137 static void
gst_rtp_header_extension_twcc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)138 gst_rtp_header_extension_twcc_get_property (GObject * object, guint prop_id,
139 GValue * value, GParamSpec * pspec)
140 {
141 GstRTPHeaderExtensionTWCC *twcc = GST_RTP_HEADER_EXTENSION_TWCC (object);
142
143 switch (prop_id) {
144 case PROP_N_STREAMS:
145 g_value_set_uint (value, twcc->n_streams);
146 break;
147 default:
148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149 break;
150 }
151 }
152
153 static GstRTPHeaderExtensionFlags
gst_rtp_header_extension_twcc_get_supported_flags(GstRTPHeaderExtension * ext)154 gst_rtp_header_extension_twcc_get_supported_flags (GstRTPHeaderExtension * ext)
155 {
156 return GST_RTP_HEADER_EXTENSION_ONE_BYTE;
157 }
158
159 static gsize
gst_rtp_header_extension_twcc_get_max_size(GstRTPHeaderExtension * ext,const GstBuffer * buffer)160 gst_rtp_header_extension_twcc_get_max_size (GstRTPHeaderExtension * ext,
161 const GstBuffer * buffer)
162 {
163 return 2;
164 }
165
166 static gssize
gst_rtp_header_extension_twcc_write(GstRTPHeaderExtension * ext,const GstBuffer * input_meta,GstRTPHeaderExtensionFlags write_flags,GstBuffer * output,guint8 * data,gsize size)167 gst_rtp_header_extension_twcc_write (GstRTPHeaderExtension * ext,
168 const GstBuffer * input_meta, GstRTPHeaderExtensionFlags write_flags,
169 GstBuffer * output, guint8 * data, gsize size)
170 {
171 GstRTPHeaderExtensionTWCC *twcc = GST_RTP_HEADER_EXTENSION_TWCC (ext);
172 GstRTPBuffer rtp = { NULL, };
173 gpointer ext_data;
174 guint ext_size;
175 gsize written = 0;
176
177 g_return_val_if_fail (size >= gst_rtp_header_extension_twcc_get_max_size (ext,
178 NULL), -1);
179 g_return_val_if_fail (write_flags &
180 gst_rtp_header_extension_twcc_get_supported_flags (ext), -1);
181
182 if (!gst_rtp_buffer_map (output, GST_MAP_READWRITE, &rtp))
183 goto map_failed;
184
185 /* if there already is a twcc-seqnum inside the packet */
186 if (gst_rtp_buffer_get_extension_onebyte_header (&rtp,
187 gst_rtp_header_extension_get_id (ext), 0, &ext_data, &ext_size)) {
188 if (ext_size < gst_rtp_header_extension_twcc_get_max_size (ext, NULL))
189 goto existing_too_small;
190
191 /* with only one stream, we read the twcc-seqnum */
192 if (twcc->n_streams == 1)
193 twcc->seqnum = GST_READ_UINT16_BE (ext_data);
194 } else {
195 /* with only one stream, we read the existing seqnum */
196 if (twcc->n_streams == 1)
197 twcc->seqnum = gst_rtp_buffer_get_seq (&rtp);
198
199 written = 2;
200 }
201 GST_WRITE_UINT16_BE (data, twcc->seqnum);
202
203 gst_rtp_buffer_unmap (&rtp);
204
205 twcc->seqnum++;
206
207 return written;
208
209 /* ERRORS */
210 map_failed:
211 {
212 GST_ERROR ("failed to map buffer %p", output);
213 return -1;
214 }
215
216 existing_too_small:
217 {
218 GST_ERROR ("Cannot rewrite twcc data of smaller size (%u)", ext_size);
219 return 0;
220 }
221 }
222
223 static gboolean
gst_rtp_header_extension_twcc_read(GstRTPHeaderExtension * ext,GstRTPHeaderExtensionFlags read_flags,const guint8 * data,gsize size,GstBuffer * buffer)224 gst_rtp_header_extension_twcc_read (GstRTPHeaderExtension * ext,
225 GstRTPHeaderExtensionFlags read_flags, const guint8 * data, gsize size,
226 GstBuffer * buffer)
227 {
228 /* TODO: does this need an extra GstMeta? */
229 return TRUE;
230 }
231