• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * (C) 2011 Collabora Ltd.
3  *  Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General 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 
21 /**
22  * SECTION:element-spanplc
23  * @title: spanplc
24  *
25  * The spanplc (Packet Loss Concealment) element provides a synthetic
26  * fill-in signal, to minimise the audible effect of lost packets in
27  * VoIP applications
28  *
29  */
30 
31 
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 
36 #include "gstspanplc.h"
37 
38 #include <gst/audio/audio.h>
39 
40 G_DEFINE_TYPE (GstSpanPlc, gst_span_plc, GST_TYPE_ELEMENT);
41 
42 GST_DEBUG_CATEGORY_STATIC (gst_span_plc_debug);
43 #define GST_CAT_DEFAULT gst_span_plc_debug
44 
45 
46 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("audio/x-raw, format=\"" GST_AUDIO_NE (S16) "\", "
50         "rate = " GST_AUDIO_RATE_RANGE " , channels = (int) 1")
51     );
52 
53 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("audio/x-raw, format=\"" GST_AUDIO_NE (S16) "\", "
57         "rate = " GST_AUDIO_RATE_RANGE " , channels = (int) 1")
58     );
59 
60 static void gst_span_plc_dispose (GObject * object);
61 
62 static GstStateChangeReturn gst_span_plc_change_state (GstElement * element,
63     GstStateChange transition);
64 static void gst_span_plc_setcaps_sink (GstSpanPlc * plc, GstCaps * caps);
65 static GstFlowReturn gst_span_plc_chain (GstPad * pad, GstObject * parent,
66     GstBuffer * buf);
67 static gboolean gst_span_plc_event_sink (GstPad * pad, GstObject * parent,
68     GstEvent * event);
69 
70 /* initialize the plugin's class */
71 static void
gst_span_plc_class_init(GstSpanPlcClass * klass)72 gst_span_plc_class_init (GstSpanPlcClass * klass)
73 {
74   GObjectClass *gobject_class = (GObjectClass *) klass;
75   GstElementClass *gstelement_class = (GstElementClass *) klass;
76 
77   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
78   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
79 
80   gst_element_class_set_static_metadata (gstelement_class, "SpanDSP PLC",
81       "Filter/Effect/Audio",
82       "Adds packet loss concealment to audio",
83       "Youness Alaoui <youness.alaoui@collabora.co.uk>");
84 
85   gobject_class->dispose = gst_span_plc_dispose;
86 
87   gstelement_class->change_state =
88       GST_DEBUG_FUNCPTR (gst_span_plc_change_state);
89 
90   GST_DEBUG_CATEGORY_INIT (gst_span_plc_debug, "spanplc",
91       0, "spanDSP's packet loss concealment");
92 
93 }
94 
95 static void
gst_span_plc_init(GstSpanPlc * plc)96 gst_span_plc_init (GstSpanPlc * plc)
97 {
98   GST_DEBUG_OBJECT (plc, "init");
99 
100   plc->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
101   plc->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
102 
103   gst_pad_set_chain_function (plc->sinkpad,
104       GST_DEBUG_FUNCPTR (gst_span_plc_chain));
105   gst_pad_set_event_function (plc->sinkpad,
106       GST_DEBUG_FUNCPTR (gst_span_plc_event_sink));
107 
108   gst_element_add_pad (GST_ELEMENT (plc), plc->srcpad);
109   gst_element_add_pad (GST_ELEMENT (plc), plc->sinkpad);
110 
111   plc->plc_state = NULL;
112 
113   GST_DEBUG_OBJECT (plc, "init complete");
114 }
115 
116 static void
gst_span_plc_dispose(GObject * object)117 gst_span_plc_dispose (GObject * object)
118 {
119   GstSpanPlc *plc = GST_SPAN_PLC (object);
120 
121   if (plc->plc_state)
122     plc_free (plc->plc_state);
123   plc->plc_state = NULL;
124 
125   G_OBJECT_CLASS (gst_span_plc_parent_class)->dispose (object);
126 }
127 
128 static void
gst_span_plc_flush(GstSpanPlc * plc,gboolean renew)129 gst_span_plc_flush (GstSpanPlc * plc, gboolean renew)
130 {
131   if (plc->plc_state)
132     plc_free (plc->plc_state);
133   if (renew)
134     plc->plc_state = plc_init (NULL);
135   else
136     plc->plc_state = NULL;
137 }
138 
139 static GstStateChangeReturn
gst_span_plc_change_state(GstElement * element,GstStateChange transition)140 gst_span_plc_change_state (GstElement * element, GstStateChange transition)
141 {
142   GstSpanPlc *plc = GST_SPAN_PLC (element);
143   GstStateChangeReturn ret;
144 
145   switch (transition) {
146     case GST_STATE_CHANGE_NULL_TO_READY:
147       gst_span_plc_flush (plc, TRUE);
148       break;
149     default:
150       break;
151   }
152 
153   ret = GST_ELEMENT_CLASS (gst_span_plc_parent_class)->change_state (element,
154       transition);
155 
156   switch (transition) {
157     case GST_STATE_CHANGE_READY_TO_NULL:
158       gst_span_plc_flush (plc, FALSE);
159     default:
160       break;
161   }
162 
163   return ret;
164 }
165 
166 static void
gst_span_plc_setcaps_sink(GstSpanPlc * plc,GstCaps * caps)167 gst_span_plc_setcaps_sink (GstSpanPlc * plc, GstCaps * caps)
168 {
169   GstStructure *s = NULL;
170   gint sample_rate;
171 
172   s = gst_caps_get_structure (caps, 0);
173   if (!s)
174     return;
175 
176   gst_structure_get_int (s, "rate", &sample_rate);
177   if (sample_rate != plc->sample_rate) {
178     GST_DEBUG_OBJECT (plc, "setcaps: got sample rate : %d", sample_rate);
179     plc->sample_rate = sample_rate;
180     gst_span_plc_flush (plc, TRUE);
181   }
182 }
183 
184 static GstFlowReturn
gst_span_plc_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)185 gst_span_plc_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
186 {
187   GstSpanPlc *plc = GST_SPAN_PLC (parent);
188   GstMapInfo map;
189 
190   buffer = gst_buffer_make_writable (buffer);
191   gst_buffer_map (buffer, &map, GST_MAP_READWRITE);
192   plc_rx (plc->plc_state, (int16_t *) map.data, map.size / 2);
193   gst_buffer_unmap (buffer, &map);
194 
195   return gst_pad_push (plc->srcpad, buffer);
196 }
197 
198 static void
gst_span_plc_send_fillin(GstSpanPlc * plc,GstClockTime timestamp,GstClockTime duration)199 gst_span_plc_send_fillin (GstSpanPlc * plc, GstClockTime timestamp,
200     GstClockTime duration)
201 {
202   guint buf_size;
203   GstBuffer *buffer = NULL;
204   GstMapInfo map;
205 
206   buf_size = ((float) duration / GST_SECOND) * plc->sample_rate;
207   buf_size *= sizeof (guint16);
208   buffer = gst_buffer_new_and_alloc (buf_size);
209   GST_DEBUG_OBJECT (plc, "Missing packet of %" GST_TIME_FORMAT
210       " == %d bytes", GST_TIME_ARGS (duration), buf_size);
211   gst_buffer_map (buffer, &map, GST_MAP_READWRITE);
212   plc_fillin (plc->plc_state, (int16_t *) map.data, map.size / 2);
213   gst_buffer_unmap (buffer, &map);
214   GST_BUFFER_PTS (buffer) = timestamp;
215   GST_BUFFER_DURATION (buffer) = duration;
216   gst_pad_push (plc->srcpad, buffer);
217 }
218 
219 static gboolean
gst_span_plc_event_sink(GstPad * pad,GstObject * parent,GstEvent * event)220 gst_span_plc_event_sink (GstPad * pad, GstObject * parent, GstEvent * event)
221 {
222   GstSpanPlc *plc = GST_SPAN_PLC (parent);
223 
224   GST_DEBUG_OBJECT (plc, "received event %s", GST_EVENT_TYPE_NAME (event));
225 
226   switch (GST_EVENT_TYPE (event)) {
227     case GST_EVENT_CAPS:
228     {
229       GstCaps *caps;
230       gst_event_parse_caps (event, &caps);
231       gst_span_plc_setcaps_sink (plc, caps);
232       break;
233     }
234     case GST_EVENT_GAP:
235     {
236       GstClockTime timestamp;
237       GstClockTime duration;
238 
239       gst_event_parse_gap (event, &timestamp, &duration);
240       gst_span_plc_send_fillin (plc, timestamp, duration);
241       gst_event_unref (event);
242       return TRUE;
243     }
244     case GST_EVENT_FLUSH_STOP:
245       gst_span_plc_flush (plc, TRUE);
246       break;
247     default:
248       break;
249   }
250 
251   return gst_pad_push_event (plc->srcpad, event);
252 }
253