1 /* GStreamer plugin for forward error correction
2 * Copyright (C) 2017 Pexip
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 * Author: Mikhail Fludkov <misha@pexip.com>
22 * SECTION:element-rtpstorage
23 * @short_description: RTP storage for forward error correction (FEC) in rtpbin
26 * Helper element for storing packets to aid later packet recovery from packet
27 * loss using RED/FEC (Forward Error Correction).
29 * The purpose of this element is to store a moving window of packets which
30 * downstream elements such as #GstRtpUlpFecDec can request in order to perform
31 * recovery of lost packets upon receiving custom GstRtpPacketLost events,
32 * usually from #GstRtpJitterBuffer.
34 * As such, when building a pipeline manually, it should have the form:
37 * rtpstorage ! rtpjitterbuffer ! rtpulpfecdec
40 * where rtpulpfecdec get passed a reference to the object pointed to by
41 * the #GstRtpStorage:internal-storage property.
43 * The #GstRtpStorage:size-time property should be configured with a value
44 * equal to the #GstRtpJitterBuffer latency, plus some tolerance, in the order
45 * of milliseconds, for example in the example found at
46 * <https://github.com/sdroege/gstreamer-rs/blob/master/examples/src/bin/rtpfecclient.rs>,
47 * `size-time` is configured as 200 + 50 milliseconds (latency + tolerance).
49 * When using #GstRtpBin, a storage element is created automatically, and
50 * can be configured upon receiving the #GstRtpBin::new-storage signal.
52 * See also: #GstRtpBin, #GstRtpUlpFecDec
56 #include "gstrtpelements.h"
57 #include "gstrtpstorage.h"
59 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
62 GST_STATIC_CAPS ("application/x-rtp")
65 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
68 GST_STATIC_CAPS ("application/x-rtp")
75 PROP_INTERNAL_STORAGE,
79 static GParamSpec *klass_properties[N_PROPERTIES] = { NULL, };
81 #define DEFAULT_SIZE_TIME (0)
83 GST_DEBUG_CATEGORY (gst_rtp_storage_debug);
84 #define GST_CAT_DEFAULT (gst_rtp_storage_debug)
86 G_DEFINE_TYPE (GstRtpStorage, gst_rtp_storage, GST_TYPE_ELEMENT);
87 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpstorage, "rtpstorage", GST_RANK_NONE,
88 GST_TYPE_RTP_STORAGE, rtp_element_init (plugin));
91 gst_rtp_storage_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
93 GstRtpStorage *self = GST_RTP_STORAGE (parent);;
95 if (rtp_storage_append_buffer (self->storage, buf))
96 return gst_pad_push (self->srcpad, buf);
101 gst_rtp_storage_set_property (GObject * object, guint prop_id,
102 const GValue * value, GParamSpec * pspec)
104 GstRtpStorage *self = GST_RTP_STORAGE (object);
108 GST_DEBUG_OBJECT (self, "RTP storage size set to %" GST_TIME_FORMAT,
109 GST_TIME_ARGS (g_value_get_uint64 (value)));
110 rtp_storage_set_size (self->storage, g_value_get_uint64 (value));
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 gst_rtp_storage_get_property (GObject * object, guint prop_id,
120 GValue * value, GParamSpec * pspec)
122 GstRtpStorage *self = GST_RTP_STORAGE (object);
125 g_value_set_uint64 (value, rtp_storage_get_size (self->storage));
127 case PROP_INTERNAL_STORAGE:
129 g_value_set_object (value, self->storage);
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139 gst_rtp_storage_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
141 GstRtpStorage *self = GST_RTP_STORAGE (parent);
143 if (GST_QUERY_TYPE (query) == GST_QUERY_CUSTOM) {
144 GstStructure *s = gst_query_writable_structure (query);
146 if (gst_structure_has_name (s, "GstRtpStorage")) {
147 gst_structure_set (s, "storage", G_TYPE_OBJECT, self->storage, NULL);
152 return gst_pad_query_default (pad, parent, query);
156 gst_rtp_storage_init (GstRtpStorage * self)
158 self->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
159 self->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
160 GST_PAD_SET_PROXY_CAPS (self->sinkpad);
161 GST_PAD_SET_PROXY_ALLOCATION (self->sinkpad);
162 gst_pad_set_chain_function (self->sinkpad, gst_rtp_storage_chain);
164 gst_pad_set_query_function (self->srcpad, gst_rtp_storage_src_query);
166 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
167 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
169 self->storage = rtp_storage_new ();
173 gst_rtp_storage_dispose (GObject * obj)
175 GstRtpStorage *self = GST_RTP_STORAGE (obj);
176 g_object_unref (self->storage);
177 G_OBJECT_CLASS (gst_rtp_storage_parent_class)->dispose (obj);
181 gst_rtp_storage_class_init (GstRtpStorageClass * klass)
183 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
184 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
186 GST_DEBUG_CATEGORY_INIT (gst_rtp_storage_debug,
187 "rtpstorage", 0, "RTP Storage");
188 GST_DEBUG_REGISTER_FUNCPTR (gst_rtp_storage_chain);
190 gst_element_class_add_pad_template (element_class,
191 gst_static_pad_template_get (&srctemplate));
192 gst_element_class_add_pad_template (element_class,
193 gst_static_pad_template_get (&sinktemplate));
195 gst_element_class_set_static_metadata (element_class,
198 "Helper element for various purposes "
199 "(ex. recovering from packet loss using RED/FEC). "
200 "Saves given number of RTP packets. "
201 "Should be instantiated before jitterbuffer",
202 "Mikhail Fludkov <misha@pexip.com>");
204 gobject_class->set_property = gst_rtp_storage_set_property;
205 gobject_class->get_property = gst_rtp_storage_get_property;
206 gobject_class->dispose = gst_rtp_storage_dispose;
208 klass_properties[PROP_SIZE_TIME] =
209 g_param_spec_uint64 ("size-time", "Storage size (in ns)",
210 "The amount of data to keep in the storage (in ns, 0-disable)", 0,
211 G_MAXUINT64, DEFAULT_SIZE_TIME,
212 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
214 klass_properties[PROP_INTERNAL_STORAGE] =
215 g_param_spec_object ("internal-storage", "Internal storage",
216 "Internal RtpStorage object", G_TYPE_OBJECT,
217 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
219 g_object_class_install_properties (gobject_class, N_PROPERTIES,