Ring Daemon 16.0.0
Loading...
Searching...
No Matches
webrtc.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include "webrtc.h"
19#include "logger.h"
20
21#include <webrtc/modules/audio_processing/include/audio_processing.h>
22
23namespace jami {
24
25inline size_t
27{
28 return (size_t) (webrtc::AudioProcessing::kChunkSizeMs * format.sample_rate / 1000);
29}
30
31constexpr int webrtcNoError = webrtc::AudioProcessing::kNoError;
32
34 : AudioProcessor(format.withSampleFormat(AV_SAMPLE_FMT_FLTP), webrtcFrameSize(format))
35{
36 JAMI_LOG("[webrtc-ap] WebRTCAudioProcessor, frame size = {:d} (={:d} ms), channels = {:d}",
40 webrtc::Config config;
41 config.Set<webrtc::ExtendedFilter>(new webrtc::ExtendedFilter(true));
42 config.Set<webrtc::DelayAgnostic>(new webrtc::DelayAgnostic(true));
43
44 apm.reset(webrtc::AudioProcessing::Create(config));
45
46 webrtc::StreamConfig streamConfig((int) format_.sample_rate, (int) format_.nb_channels);
47 webrtc::ProcessingConfig pconfig = {
48 streamConfig, /* input stream */
49 streamConfig, /* output stream */
50 streamConfig, /* reverse input stream */
51 streamConfig, /* reverse output stream */
52 };
53
54 if (apm->Initialize(pconfig) != webrtcNoError) {
55 JAMI_ERROR("[webrtc-ap] Error initialising audio processing module");
56 }
57}
58
59void
61{
62 JAMI_LOG("[webrtc-ap] enableNoiseSuppression {}", enabled);
63 if (apm->noise_suppression()->Enable(enabled) != webrtcNoError) {
64 JAMI_ERROR("[webrtc-ap] Error enabling noise suppression");
65 }
66 if (apm->noise_suppression()->set_level(webrtc::NoiseSuppression::kVeryHigh) != webrtcNoError) {
67 JAMI_ERROR("[webrtc-ap] Error setting noise suppression level");
68 }
69 if (apm->high_pass_filter()->Enable(enabled) != webrtcNoError) {
70 JAMI_ERROR("[webrtc-ap] Error enabling high pass filter");
71 }
72}
73
74void
76{
77 JAMI_LOG("[webrtc-ap] enableAutomaticGainControl {}", enabled);
78 if (apm->gain_control()->Enable(enabled) != webrtcNoError) {
79 JAMI_ERROR("[webrtc-ap] Error enabling automatic gain control");
80 }
81 if (apm->gain_control()->set_analog_level_limits(0, 255) != webrtcNoError) {
82 JAMI_ERROR("[webrtc-ap] Error setting automatic gain control analog level limits");
83 }
84 if (apm->gain_control()->set_mode(webrtc::GainControl::kAdaptiveAnalog) != webrtcNoError) {
85 JAMI_ERROR("[webrtc-ap] Error setting automatic gain control mode");
86 }
87}
88
89void
91{
92 JAMI_LOG("[webrtc-ap] enableEchoCancel {}", enabled);
93
94 if (apm->echo_cancellation()->Enable(enabled) != webrtcNoError) {
95 JAMI_ERROR("[webrtc-ap] Error enabling echo cancellation");
96 }
97 if (apm->echo_cancellation()->set_suppression_level(
98 webrtc::EchoCancellation::SuppressionLevel::kHighSuppression)
99 != webrtcNoError) {
100 JAMI_ERROR("[webrtc-ap] Error setting echo cancellation level");
101 }
102 if (apm->echo_cancellation()->enable_drift_compensation(true) != webrtcNoError) {
103 JAMI_ERROR("[webrtc-ap] Error enabling echo cancellation drift compensation");
104 }
105}
106
107void
109{
110 JAMI_LOG("[webrtc-ap] enableVoiceActivityDetection {}", enabled);
111 if (apm->voice_detection()->Enable(enabled) != webrtcNoError) {
112 JAMI_ERROR("[webrtc-ap] Error enabling voice activation detection");
113 }
114 if (apm->voice_detection()->set_likelihood(webrtc::VoiceDetection::kVeryLowLikelihood)
115 != webrtcNoError) {
116 JAMI_ERROR("[webrtc-ap] Error setting voice detection likelihood");
117 }
118 // asserted to be 10 in voice_detection_impl.cc
119 if (apm->voice_detection()->set_frame_size_ms(10) != webrtcNoError) {
120 JAMI_ERROR("[webrtc-ap] Error setting voice detection frame size");
121 }
122}
123
124std::shared_ptr<AudioFrame>
126{
127 if (tidyQueues()) {
128 return {};
129 }
130
132
133 auto playback = playbackQueue_.dequeue();
134 auto record = recordQueue_.dequeue();
135 if (!playback || !record) {
136 return {};
137 }
138 webrtc::StreamConfig sc((int) format_.sample_rate, (int) format_.nb_channels);
139
140 // process reverse in place
141 float** playData = (float**) playback->pointer()->extended_data;
142 if (apm->ProcessReverseStream(playData, sc, sc, playData) != webrtcNoError) {
143 JAMI_ERR("[webrtc-ap] ProcessReverseStream failed");
144 }
145
146 // process deinterleaved float recorded data
147 // TODO: maybe implement this to see if it's better than automatic drift compensation
148 // (it MUST be called prior to ProcessStream)
149 // delay = (t_render - t_analyze) + (t_process - t_capture)
150 if (apm->set_stream_delay_ms(0) != webrtcNoError) {
151 JAMI_ERR("[webrtc-ap] set_stream_delay_ms failed");
152 }
153
154 if (apm->gain_control()->set_stream_analog_level(analogLevel_) != webrtcNoError) {
155 JAMI_ERR("[webrtc-ap] set_stream_analog_level failed");
156 }
157 apm->echo_cancellation()->set_stream_drift_samples(driftSamples);
158
159 // process in place
160 float** recData = (float**) record->pointer()->extended_data;
161 if (apm->ProcessStream(recData, sc, sc, recData) != webrtcNoError) {
162 JAMI_ERR("[webrtc-ap] ProcessStream failed");
163 }
164
165 analogLevel_ = apm->gain_control()->stream_analog_level();
166 record->has_voice = apm->voice_detection()->is_enabled()
167 && getStabilizedVoiceActivity(apm->voice_detection()->stream_has_voice());
168 return record;
169}
170
171} // namespace jami
int samples() const
Gets the numbers of samples available for reading.
std::shared_ptr< AudioFrame > dequeue()
Notifies owner of a new frame.
unsigned int frameDurationMs_
AudioFrameResizer playbackQueue_
bool tidyQueues()
Helper method for audio processors, should be called at start of getProcessed() Pops frames from audi...
bool getStabilizedVoiceActivity(bool voiceStatus)
Stablilizes voice activity.
AudioFrameResizer recordQueue_
std::shared_ptr< AudioFrame > getProcessed() override
Process and return a single AudioFrame.
Definition webrtc.cpp:125
void enableEchoCancel(bool enabled) override
Set the status of echo cancellation.
Definition webrtc.cpp:90
WebRTCAudioProcessor(AudioFormat format, unsigned frameSize)
Definition webrtc.cpp:33
void enableNoiseSuppression(bool enabled) override
Set the status of noise suppression includes de-reverb, de-noise, high pass filter,...
Definition webrtc.cpp:60
void enableVoiceActivityDetection(bool enabled) override
Set the status of voice activity detection.
Definition webrtc.cpp:108
void enableAutomaticGainControl(bool enabled) override
Set the status of automatic gain control.
Definition webrtc.cpp:75
#define JAMI_ERR(...)
Definition logger.h:218
#define JAMI_ERROR(formatstr,...)
Definition logger.h:228
#define JAMI_LOG(formatstr,...)
Definition logger.h:225
void emitSignal(Args... args)
Definition ring_signal.h:64
constexpr int webrtcNoError
Definition webrtc.cpp:31
size_t webrtcFrameSize(AudioFormat format)
Definition webrtc.cpp:26
Structure to hold sample rate and channel number associated with audio data.