ad350e78b948e44365aefaa596c726fdf598b649
[platform/upstream/gstreamer.git] / ext / webrtcdsp / gstwebrtcechoprobe.cpp
1 /*
2  * WebRTC Audio Processing Elements
3  *
4  *  Copyright 2016 Collabora Ltd
5  *    @author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  */
22
23 /**
24  * SECTION:element-webrtcechoprobe
25  *
26  * This echo probe is to be used with the webrtcdsp element. See #gst-plugins-bad-plugins-webrtcdsp
27  * documentation for more details.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "gstwebrtcechoprobe.h"
35
36 #include <webrtc/modules/interface/module_common_types.h>
37 #include <gst/audio/audio.h>
38
39 GST_DEBUG_CATEGORY_EXTERN (webrtc_dsp_debug);
40 #define GST_CAT_DEFAULT (webrtc_dsp_debug)
41
42 #define MAX_ADAPTER_SIZE (1*1024*1024)
43
44 static GstStaticPadTemplate gst_webrtc_echo_probe_sink_template =
45 GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("audio/x-raw, "
49         "format = (string) " GST_AUDIO_NE (S16) ", "
50         "layout = (string) interleaved, "
51         "rate = (int) { 48000, 32000, 16000, 8000 }, "
52         "channels = (int) [1, MAX];"
53         "audio/x-raw, "
54         "format = (string) " GST_AUDIO_NE (F32) ", "
55         "layout = (string) non-interleaved, "
56         "rate = (int) { 48000, 32000, 16000, 8000 }, "
57         "channels = (int) [1, MAX]")
58     );
59
60 static GstStaticPadTemplate gst_webrtc_echo_probe_src_template =
61 GST_STATIC_PAD_TEMPLATE ("src",
62     GST_PAD_SRC,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("audio/x-raw, "
65         "format = (string) " GST_AUDIO_NE (S16) ", "
66         "layout = (string) interleaved, "
67         "rate = (int) { 48000, 32000, 16000, 8000 }, "
68         "channels = (int) [1, MAX];"
69         "audio/x-raw, "
70         "format = (string) " GST_AUDIO_NE (F32) ", "
71         "layout = (string) non-interleaved, "
72         "rate = (int) { 48000, 32000, 16000, 8000 }, "
73         "channels = (int) [1, MAX]")
74     );
75
76 G_LOCK_DEFINE_STATIC (gst_aec_probes);
77 static GList *gst_aec_probes = NULL;
78
79 G_DEFINE_TYPE (GstWebrtcEchoProbe, gst_webrtc_echo_probe,
80     GST_TYPE_AUDIO_FILTER);
81
82 static gboolean
83 gst_webrtc_echo_probe_setup (GstAudioFilter * filter, const GstAudioInfo * info)
84 {
85   GstWebrtcEchoProbe *self = GST_WEBRTC_ECHO_PROBE (filter);
86
87   GST_LOG_OBJECT (self, "setting format to %s with %i Hz and %i channels",
88       info->finfo->description, info->rate, info->channels);
89
90   GST_WEBRTC_ECHO_PROBE_LOCK (self);
91
92   self->info = *info;
93   self->interleaved = (info->layout == GST_AUDIO_LAYOUT_INTERLEAVED);
94
95   if (!self->interleaved)
96     gst_planar_audio_adapter_configure (self->padapter, info);
97
98   /* WebRTC library works with 10ms buffers, compute once this size */
99   self->period_samples = info->rate / 100;
100   self->period_size = self->period_samples * info->bpf;
101
102   if (self->interleaved &&
103       (webrtc::AudioFrame::kMaxDataSizeSamples * 2) < self->period_size)
104     goto period_too_big;
105
106   GST_WEBRTC_ECHO_PROBE_UNLOCK (self);
107
108   return TRUE;
109
110 period_too_big:
111   GST_WEBRTC_ECHO_PROBE_UNLOCK (self);
112   GST_WARNING_OBJECT (self, "webrtcdsp format produce too big period "
113       "(maximum is %" G_GSIZE_FORMAT " samples and we have %u samples), "
114       "reduce the number of channels or the rate.",
115       webrtc::AudioFrame::kMaxDataSizeSamples, self->period_size / 2);
116   return FALSE;
117 }
118
119 static gboolean
120 gst_webrtc_echo_probe_stop (GstBaseTransform * btrans)
121 {
122   GstWebrtcEchoProbe *self = GST_WEBRTC_ECHO_PROBE (btrans);
123
124   GST_WEBRTC_ECHO_PROBE_LOCK (self);
125   gst_adapter_clear (self->adapter);
126   gst_planar_audio_adapter_clear (self->padapter);
127   GST_WEBRTC_ECHO_PROBE_UNLOCK (self);
128
129   return TRUE;
130 }
131
132 static gboolean
133 gst_webrtc_echo_probe_src_event (GstBaseTransform * btrans, GstEvent * event)
134 {
135   GstBaseTransformClass *klass;
136   GstWebrtcEchoProbe *self = GST_WEBRTC_ECHO_PROBE (btrans);
137   GstClockTime latency;
138   GstClockTime upstream_latency = 0;
139   GstQuery *query;
140
141   klass = GST_BASE_TRANSFORM_CLASS (gst_webrtc_echo_probe_parent_class);
142
143   switch (GST_EVENT_TYPE (event)) {
144     case GST_EVENT_LATENCY:
145       gst_event_parse_latency (event, &latency);
146       query = gst_query_new_latency ();
147
148       if (gst_pad_query (btrans->srcpad, query)) {
149         gst_query_parse_latency (query, NULL, &upstream_latency, NULL);
150
151         if (!GST_CLOCK_TIME_IS_VALID (upstream_latency))
152           upstream_latency = 0;
153       }
154
155       GST_WEBRTC_ECHO_PROBE_LOCK (self);
156       self->latency = latency;
157       self->delay = upstream_latency / GST_MSECOND;
158       GST_WEBRTC_ECHO_PROBE_UNLOCK (self);
159
160       GST_DEBUG_OBJECT (self, "We have a latency of %" GST_TIME_FORMAT
161           " and delay of %ims", GST_TIME_ARGS (latency),
162           (gint) (upstream_latency / GST_MSECOND));
163       break;
164     default:
165       break;
166   }
167
168   return klass->src_event (btrans, event);
169 }
170
171 static GstFlowReturn
172 gst_webrtc_echo_probe_transform_ip (GstBaseTransform * btrans,
173     GstBuffer * buffer)
174 {
175   GstWebrtcEchoProbe *self = GST_WEBRTC_ECHO_PROBE (btrans);
176   GstBuffer *newbuf = NULL;
177
178   GST_WEBRTC_ECHO_PROBE_LOCK (self);
179   newbuf = gst_buffer_copy (buffer);
180   /* Moves the buffer timestamp to be in Running time */
181   GST_BUFFER_PTS (newbuf) = gst_segment_to_running_time (&btrans->segment,
182       GST_FORMAT_TIME, GST_BUFFER_PTS (buffer));
183
184   if (self->interleaved) {
185     gst_adapter_push (self->adapter, newbuf);
186
187     if (gst_adapter_available (self->adapter) > MAX_ADAPTER_SIZE)
188       gst_adapter_flush (self->adapter,
189           gst_adapter_available (self->adapter) - MAX_ADAPTER_SIZE);
190   } else {
191     gsize available;
192
193     gst_planar_audio_adapter_push (self->padapter, newbuf);
194     available =
195         gst_planar_audio_adapter_available (self->padapter) * self->info.bpf;
196     if (available > MAX_ADAPTER_SIZE)
197       gst_planar_audio_adapter_flush (self->padapter,
198           (available - MAX_ADAPTER_SIZE) / self->info.bpf);
199   }
200
201   GST_WEBRTC_ECHO_PROBE_UNLOCK (self);
202
203   return GST_FLOW_OK;
204 }
205
206 static void
207 gst_webrtc_echo_probe_finalize (GObject * object)
208 {
209   GstWebrtcEchoProbe *self = GST_WEBRTC_ECHO_PROBE (object);
210
211   G_LOCK (gst_aec_probes);
212   gst_aec_probes = g_list_remove (gst_aec_probes, self);
213   G_UNLOCK (gst_aec_probes);
214
215   gst_object_unref (self->adapter);
216   gst_object_unref (self->padapter);
217   self->adapter = NULL;
218   self->padapter = NULL;
219
220   G_OBJECT_CLASS (gst_webrtc_echo_probe_parent_class)->finalize (object);
221 }
222
223 static void
224 gst_webrtc_echo_probe_init (GstWebrtcEchoProbe * self)
225 {
226   self->adapter = gst_adapter_new ();
227   self->padapter = gst_planar_audio_adapter_new ();
228   gst_audio_info_init (&self->info);
229   g_mutex_init (&self->lock);
230
231   self->latency = GST_CLOCK_TIME_NONE;
232
233   G_LOCK (gst_aec_probes);
234   gst_aec_probes = g_list_prepend (gst_aec_probes, self);
235   G_UNLOCK (gst_aec_probes);
236 }
237
238 static void
239 gst_webrtc_echo_probe_class_init (GstWebrtcEchoProbeClass * klass)
240 {
241   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
242   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
243   GstBaseTransformClass *btrans_class = GST_BASE_TRANSFORM_CLASS (klass);
244   GstAudioFilterClass *audiofilter_class = GST_AUDIO_FILTER_CLASS (klass);
245
246   gobject_class->finalize = gst_webrtc_echo_probe_finalize;
247
248   btrans_class->passthrough_on_same_caps = TRUE;
249   btrans_class->src_event = GST_DEBUG_FUNCPTR (gst_webrtc_echo_probe_src_event);
250   btrans_class->transform_ip =
251       GST_DEBUG_FUNCPTR (gst_webrtc_echo_probe_transform_ip);
252   btrans_class->stop = GST_DEBUG_FUNCPTR (gst_webrtc_echo_probe_stop);
253
254   audiofilter_class->setup = GST_DEBUG_FUNCPTR (gst_webrtc_echo_probe_setup);
255
256   gst_element_class_add_static_pad_template (element_class,
257       &gst_webrtc_echo_probe_src_template);
258   gst_element_class_add_static_pad_template (element_class,
259       &gst_webrtc_echo_probe_sink_template);
260
261   gst_element_class_set_static_metadata (element_class,
262       "Accoustic Echo Canceller probe",
263       "Generic/Audio",
264       "Gathers playback buffers for webrtcdsp",
265       "Nicolas Dufresne <nicolas.dufrsesne@collabora.com>");
266 }
267
268
269 GstWebrtcEchoProbe *
270 gst_webrtc_acquire_echo_probe (const gchar * name)
271 {
272   GstWebrtcEchoProbe *ret = NULL;
273   GList *l;
274
275   G_LOCK (gst_aec_probes);
276   for (l = gst_aec_probes; l; l = l->next) {
277     GstWebrtcEchoProbe *probe = GST_WEBRTC_ECHO_PROBE (l->data);
278
279     GST_WEBRTC_ECHO_PROBE_LOCK (probe);
280     if (!probe->acquired && g_strcmp0 (GST_OBJECT_NAME (probe), name) == 0) {
281       probe->acquired = TRUE;
282       ret = GST_WEBRTC_ECHO_PROBE (gst_object_ref (probe));
283       GST_WEBRTC_ECHO_PROBE_UNLOCK (probe);
284       break;
285     }
286     GST_WEBRTC_ECHO_PROBE_UNLOCK (probe);
287   }
288   G_UNLOCK (gst_aec_probes);
289
290   return ret;
291 }
292
293 void
294 gst_webrtc_release_echo_probe (GstWebrtcEchoProbe * probe)
295 {
296   GST_WEBRTC_ECHO_PROBE_LOCK (probe);
297   probe->acquired = FALSE;
298   GST_WEBRTC_ECHO_PROBE_UNLOCK (probe);
299   gst_object_unref (probe);
300 }
301
302 gint
303 gst_webrtc_echo_probe_read (GstWebrtcEchoProbe * self, GstClockTime rec_time,
304     gpointer _frame, GstBuffer ** buf)
305 {
306   webrtc::AudioFrame * frame = (webrtc::AudioFrame *) _frame;
307   GstClockTimeDiff diff;
308   gsize avail, skip, offset, size;
309   gint delay = -1;
310
311   GST_WEBRTC_ECHO_PROBE_LOCK (self);
312
313   if (!GST_CLOCK_TIME_IS_VALID (self->latency) ||
314       !GST_AUDIO_INFO_IS_VALID (&self->info))
315     goto done;
316
317   if (self->interleaved)
318     avail = gst_adapter_available (self->adapter) / self->info.bpf;
319   else
320     avail = gst_planar_audio_adapter_available (self->padapter);
321
322   /* In delay agnostic mode, just return 10ms of data */
323   if (!GST_CLOCK_TIME_IS_VALID (rec_time)) {
324     if (avail < self->period_samples)
325       goto done;
326
327     size = self->period_samples;
328     skip = 0;
329     offset = 0;
330
331     goto copy;
332   }
333
334   if (avail == 0) {
335     diff = G_MAXINT64;
336   } else {
337     GstClockTime play_time;
338     guint64 distance;
339
340     if (self->interleaved) {
341       play_time = gst_adapter_prev_pts (self->adapter, &distance);
342       distance /= self->info.bpf;
343     } else {
344       play_time = gst_planar_audio_adapter_prev_pts (self->padapter, &distance);
345     }
346
347     if (GST_CLOCK_TIME_IS_VALID (play_time)) {
348       play_time += gst_util_uint64_scale_int (distance, GST_SECOND,
349           self->info.rate);
350       play_time += self->latency;
351
352       diff = GST_CLOCK_DIFF (rec_time, play_time) / GST_MSECOND;
353     } else {
354       /* We have no timestamp, assume perfect delay */
355       diff = self->delay;
356     }
357   }
358
359   if (diff > self->delay) {
360     skip = (diff - self->delay) * self->info.rate / 1000;
361     skip = MIN (self->period_samples, skip);
362     offset = 0;
363   } else {
364     skip = 0;
365     offset = (self->delay - diff) * self->info.rate / 1000;
366     offset = MIN (avail, offset);
367   }
368
369   size = MIN (avail - offset, self->period_samples - skip);
370
371 copy:
372   if (self->interleaved) {
373     skip *= self->info.bpf;
374     offset *= self->info.bpf;
375     size *= self->info.bpf;
376
377     if (size < self->period_size)
378       memset (frame->data_, 0, self->period_size);
379
380     if (size) {
381       gst_adapter_copy (self->adapter, (guint8 *) frame->data_ + skip,
382           offset, size);
383       gst_adapter_flush (self->adapter, offset + size);
384     }
385   } else {
386     GstBuffer *ret, *taken, *tmp;
387
388     if (size) {
389       gst_planar_audio_adapter_flush (self->padapter, offset);
390
391       /* we need to fill silence at the beginning and/or the end of each
392        * channel plane in order to have exactly period_samples in the buffer */
393       if (size < self->period_samples) {
394         GstAudioMeta *meta;
395         gint bps = self->info.finfo->width / 8;
396         gsize padding = self->period_samples - (skip + size);
397         gint c;
398
399         taken = gst_planar_audio_adapter_take_buffer (self->padapter, size,
400             GST_MAP_READ);
401         meta = gst_buffer_get_audio_meta (taken);
402         ret = gst_buffer_new ();
403
404         for (c = 0; c < meta->info.channels; c++) {
405           /* need some silence at the beginning */
406           if (skip) {
407             tmp = gst_buffer_new_allocate (NULL, skip * bps, NULL);
408             gst_buffer_memset (tmp, 0, 0, skip * bps);
409             ret = gst_buffer_append (ret, tmp);
410           }
411
412           tmp = gst_buffer_copy_region (taken, GST_BUFFER_COPY_MEMORY,
413               meta->offsets[c], size * bps);
414           ret = gst_buffer_append (ret, tmp);
415
416           /* need some silence at the end */
417           if (padding) {
418             tmp = gst_buffer_new_allocate (NULL, padding * bps, NULL);
419             gst_buffer_memset (tmp, 0, 0, padding * bps);
420             ret = gst_buffer_append (ret, tmp);
421           }
422         }
423
424         gst_buffer_unref (taken);
425         gst_buffer_add_audio_meta (ret, &self->info, self->period_samples,
426             NULL);
427       } else {
428         ret = gst_planar_audio_adapter_take_buffer (self->padapter, size,
429           GST_MAP_READWRITE);
430       }
431     } else {
432       ret = gst_buffer_new_allocate (NULL, self->period_size, NULL);
433       gst_buffer_memset (ret, 0, 0, self->period_size);
434       gst_buffer_add_audio_meta (ret, &self->info, self->period_samples,
435           NULL);
436     }
437
438     *buf = ret;
439   }
440
441   frame->num_channels_ = self->info.channels;
442   frame->sample_rate_hz_ = self->info.rate;
443   frame->samples_per_channel_ = self->period_samples;
444
445   delay = self->delay;
446
447 done:
448   GST_WEBRTC_ECHO_PROBE_UNLOCK (self);
449
450   return delay;
451 }