1 /* GStreamer plugin for forward error correction
2  * Copyright (C) 2017 Pexip
3  *
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.
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  * Lesser General Public License for more details.
13  *
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
17  *
18  * Author: Mikhail Fludkov <misha@pexip.com>
19  */
20 
21 /**
22  * SECTION:element-rtpstorage
23  * @short_description: RTP storage for forward error correction (FEC) in rtpbin
24  * @title: rtpstorage
25  *
26  * Helper element for storing packets to aid later packet recovery from packet
27  * loss using RED/FEC (Forward Error Correction).
28  *
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.
33  *
34  * As such, when building a pipeline manually, it should have the form:
35  *
36  * ```
37  * rtpstorage ! rtpjitterbuffer ! rtpulpfecdec
38  * ```
39  *
40  * where rtpulpfecdec get passed a reference to the object pointed to by
41  * the #GstRtpStorage:internal-storage property.
42  *
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).
48  *
49  * When using #GstRtpBin, a storage element is created automatically, and
50  * can be configured upon receiving the #GstRtpBin::new-storage signal.
51  *
52  * See also: #GstRtpBin, #GstRtpUlpFecDec
53  * Since: 1.14
54  */
55 
56 #include "gstrtpstorage.h"
57 
58 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("application/x-rtp")
62     );
63 
64 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
65     GST_PAD_SRC,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS ("application/x-rtp")
68     );
69 
70 enum
71 {
72   PROP_0,
73   PROP_SIZE_TIME,
74   PROP_INTERNAL_STORAGE,
75   N_PROPERTIES
76 };
77 
78 static GParamSpec *klass_properties[N_PROPERTIES] = { NULL, };
79 
80 #define DEFAULT_SIZE_TIME (0)
81 
82 GST_DEBUG_CATEGORY (gst_rtp_storage_debug);
83 #define GST_CAT_DEFAULT (gst_rtp_storage_debug)
84 
85 G_DEFINE_TYPE (GstRtpStorage, gst_rtp_storage, GST_TYPE_ELEMENT);
86 
87 static GstFlowReturn
gst_rtp_storage_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)88 gst_rtp_storage_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
89 {
90   GstRtpStorage *self = GST_RTP_STORAGE (parent);;
91 
92   if (rtp_storage_append_buffer (self->storage, buf))
93     return gst_pad_push (self->srcpad, buf);
94   return GST_FLOW_OK;
95 }
96 
97 static void
gst_rtp_storage_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)98 gst_rtp_storage_set_property (GObject * object, guint prop_id,
99     const GValue * value, GParamSpec * pspec)
100 {
101   GstRtpStorage *self = GST_RTP_STORAGE (object);
102 
103   switch (prop_id) {
104     case PROP_SIZE_TIME:
105       GST_DEBUG_OBJECT (self, "RTP storage size set to %" GST_TIME_FORMAT,
106           GST_TIME_ARGS (g_value_get_uint64 (value)));
107       rtp_storage_set_size (self->storage, g_value_get_uint64 (value));
108       break;
109     default:
110       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111       break;
112   }
113 }
114 
115 static void
gst_rtp_storage_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)116 gst_rtp_storage_get_property (GObject * object, guint prop_id,
117     GValue * value, GParamSpec * pspec)
118 {
119   GstRtpStorage *self = GST_RTP_STORAGE (object);
120   switch (prop_id) {
121     case PROP_SIZE_TIME:
122       g_value_set_uint64 (value, rtp_storage_get_size (self->storage));
123       break;
124     case PROP_INTERNAL_STORAGE:
125     {
126       g_value_set_object (value, self->storage);
127       break;
128     }
129     default:
130       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131       break;
132   }
133 }
134 
135 static gboolean
gst_rtp_storage_src_query(GstPad * pad,GstObject * parent,GstQuery * query)136 gst_rtp_storage_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
137 {
138   GstRtpStorage *self = GST_RTP_STORAGE (parent);
139 
140   if (GST_QUERY_TYPE (query) == GST_QUERY_CUSTOM) {
141     GstStructure *s = gst_query_writable_structure (query);
142 
143     if (gst_structure_has_name (s, "GstRtpStorage")) {
144       gst_structure_set (s, "storage", G_TYPE_OBJECT, self->storage, NULL);
145       return TRUE;
146     }
147   }
148 
149   return gst_pad_query_default (pad, parent, query);
150 }
151 
152 static void
gst_rtp_storage_init(GstRtpStorage * self)153 gst_rtp_storage_init (GstRtpStorage * self)
154 {
155   self->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
156   self->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
157   GST_PAD_SET_PROXY_CAPS (self->sinkpad);
158   GST_PAD_SET_PROXY_ALLOCATION (self->sinkpad);
159   gst_pad_set_chain_function (self->sinkpad, gst_rtp_storage_chain);
160 
161   gst_pad_set_query_function (self->srcpad, gst_rtp_storage_src_query);
162 
163   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
164   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
165 
166   self->storage = rtp_storage_new ();
167 }
168 
169 static void
gst_rtp_storage_dispose(GObject * obj)170 gst_rtp_storage_dispose (GObject * obj)
171 {
172   GstRtpStorage *self = GST_RTP_STORAGE (obj);
173   g_object_unref (self->storage);
174   G_OBJECT_CLASS (gst_rtp_storage_parent_class)->dispose (obj);
175 }
176 
177 static void
gst_rtp_storage_class_init(GstRtpStorageClass * klass)178 gst_rtp_storage_class_init (GstRtpStorageClass * klass)
179 {
180   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
181   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
182 
183   GST_DEBUG_CATEGORY_INIT (gst_rtp_storage_debug,
184       "rtpstorage", 0, "RTP Storage");
185   GST_DEBUG_REGISTER_FUNCPTR (gst_rtp_storage_chain);
186 
187   gst_element_class_add_pad_template (element_class,
188       gst_static_pad_template_get (&srctemplate));
189   gst_element_class_add_pad_template (element_class,
190       gst_static_pad_template_get (&sinktemplate));
191 
192   gst_element_class_set_static_metadata (element_class,
193       "RTP storage",
194       "Analyzer/RTP",
195       "Helper element for various purposes "
196       "(ex. recovering from packet loss using RED/FEC). "
197       "Saves given number of RTP packets. "
198       "Should be instantiated before jitterbuffer",
199       "Mikhail Fludkov <misha@pexip.com>");
200 
201   gobject_class->set_property = gst_rtp_storage_set_property;
202   gobject_class->get_property = gst_rtp_storage_get_property;
203   gobject_class->dispose = gst_rtp_storage_dispose;
204 
205   klass_properties[PROP_SIZE_TIME] =
206       g_param_spec_uint64 ("size-time", "Storage size (in ns)",
207       "The amount of data to keep in the storage (in ns, 0-disable)", 0,
208       G_MAXUINT64, DEFAULT_SIZE_TIME,
209       G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
210 
211   klass_properties[PROP_INTERNAL_STORAGE] =
212       g_param_spec_object ("internal-storage", "Internal storage",
213       "Internal RtpStorage object", G_TYPE_OBJECT,
214       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
215 
216   g_object_class_install_properties (gobject_class, N_PROPERTIES,
217       klass_properties);
218 }
219