andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "voice_engine/transmit_mixer.h" |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 12 | |
kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "audio/utility/audio_frame_operations.h" |
| 16 | #include "rtc_base/format_macros.h" |
| 17 | #include "rtc_base/location.h" |
| 18 | #include "rtc_base/logging.h" |
| 19 | #include "system_wrappers/include/event_wrapper.h" |
| 20 | #include "system_wrappers/include/trace.h" |
| 21 | #include "voice_engine/channel.h" |
| 22 | #include "voice_engine/channel_manager.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "voice_engine/utility.h" |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 24 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 25 | namespace webrtc { |
| 26 | namespace voe { |
| 27 | |
solenberg | fc3a2e3 | 2017-09-26 09:35:01 -0700 | [diff] [blame] | 28 | // TODO(solenberg): The thread safety in this class is dubious. |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 29 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 30 | int32_t |
| 31 | TransmitMixer::Create(TransmitMixer*& mixer, uint32_t instanceId) |
| 32 | { |
| 33 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, -1), |
| 34 | "TransmitMixer::Create(instanceId=%d)", instanceId); |
| 35 | mixer = new TransmitMixer(instanceId); |
| 36 | if (mixer == NULL) |
| 37 | { |
| 38 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, -1), |
| 39 | "TransmitMixer::Create() unable to allocate memory" |
| 40 | "for mixer"); |
| 41 | return -1; |
| 42 | } |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | TransmitMixer::Destroy(TransmitMixer*& mixer) |
| 48 | { |
| 49 | if (mixer) |
| 50 | { |
| 51 | delete mixer; |
| 52 | mixer = NULL; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | TransmitMixer::TransmitMixer(uint32_t instanceId) : |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 57 | _instanceId(instanceId) |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 58 | { |
| 59 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, -1), |
| 60 | "TransmitMixer::TransmitMixer() - ctor"); |
| 61 | } |
| 62 | |
solenberg | fc3a2e3 | 2017-09-26 09:35:01 -0700 | [diff] [blame] | 63 | TransmitMixer::~TransmitMixer() = default; |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 64 | |
solenberg | fc3a2e3 | 2017-09-26 09:35:01 -0700 | [diff] [blame] | 65 | void TransmitMixer::SetEngineInformation(ChannelManager* channelManager) { |
| 66 | _channelManagerPtr = channelManager; |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | int32_t |
| 70 | TransmitMixer::SetAudioProcessingModule(AudioProcessing* audioProcessingModule) |
| 71 | { |
| 72 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1), |
| 73 | "TransmitMixer::SetAudioProcessingModule(" |
| 74 | "audioProcessingModule=0x%x)", |
| 75 | audioProcessingModule); |
| 76 | audioproc_ = audioProcessingModule; |
| 77 | return 0; |
| 78 | } |
| 79 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 80 | void TransmitMixer::GetSendCodecInfo(int* max_sample_rate, |
| 81 | size_t* max_channels) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 82 | *max_sample_rate = 8000; |
| 83 | *max_channels = 1; |
| 84 | for (ChannelManager::Iterator it(_channelManagerPtr); it.IsValid(); |
| 85 | it.Increment()) { |
| 86 | Channel* channel = it.GetChannel(); |
| 87 | if (channel->Sending()) { |
| 88 | CodecInst codec; |
ossu | 950c1c9 | 2017-07-11 08:19:31 -0700 | [diff] [blame] | 89 | // TODO(ossu): Investigate how this could happen. b/62909493 |
| 90 | if (channel->GetSendCodec(codec) == 0) { |
| 91 | *max_sample_rate = std::max(*max_sample_rate, codec.plfreq); |
| 92 | *max_channels = std::max(*max_channels, codec.channels); |
| 93 | } else { |
| 94 | LOG(LS_WARNING) << "Unable to get send codec for channel " |
| 95 | << channel->ChannelId(); |
| 96 | RTC_NOTREACHED(); |
| 97 | } |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | int32_t |
| 103 | TransmitMixer::PrepareDemux(const void* audioSamples, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 104 | size_t nSamples, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 105 | size_t nChannels, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 106 | uint32_t samplesPerSec, |
| 107 | uint16_t totalDelayMS, |
| 108 | int32_t clockDrift, |
| 109 | uint16_t currentMicLevel, |
| 110 | bool keyPressed) |
| 111 | { |
| 112 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, -1), |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 113 | "TransmitMixer::PrepareDemux(nSamples=%" PRIuS ", " |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 114 | "nChannels=%" PRIuS ", samplesPerSec=%u, totalDelayMS=%u, " |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 115 | "clockDrift=%d, currentMicLevel=%u)", |
| 116 | nSamples, nChannels, samplesPerSec, totalDelayMS, clockDrift, |
| 117 | currentMicLevel); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 118 | |
| 119 | // --- Resample input audio and create/store the initial audio frame |
| 120 | GenerateAudioFrame(static_cast<const int16_t*>(audioSamples), |
| 121 | nSamples, |
| 122 | nChannels, |
| 123 | samplesPerSec); |
| 124 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 125 | // --- Near-end audio processing. |
| 126 | ProcessAudio(totalDelayMS, clockDrift, currentMicLevel, keyPressed); |
| 127 | |
| 128 | if (swap_stereo_channels_ && stereo_codec_) |
| 129 | // Only bother swapping if we're using a stereo codec. |
| 130 | AudioFrameOperations::SwapStereoChannels(&_audioFrame); |
| 131 | |
| 132 | // --- Annoying typing detection (utilizes the APM/VAD decision) |
henrik.lundin | f00082d | 2016-12-05 02:22:12 -0800 | [diff] [blame] | 133 | #if WEBRTC_VOICE_ENGINE_TYPING_DETECTION |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 134 | TypingDetection(keyPressed); |
| 135 | #endif |
| 136 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 137 | // --- Measure audio level of speech after all processing. |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 138 | double sample_duration = static_cast<double>(nSamples) / samplesPerSec; |
zstein | 3c45186 | 2017-07-20 09:57:42 -0700 | [diff] [blame] | 139 | _audioLevel.ComputeLevel(_audioFrame, sample_duration); |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 140 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
henrika | ec6fbd2 | 2017-03-31 05:43:36 -0700 | [diff] [blame] | 144 | void TransmitMixer::ProcessAndEncodeAudio() { |
| 145 | RTC_DCHECK_GT(_audioFrame.samples_per_channel_, 0); |
| 146 | for (ChannelManager::Iterator it(_channelManagerPtr); it.IsValid(); |
| 147 | it.Increment()) { |
| 148 | Channel* const channel = it.GetChannel(); |
| 149 | if (channel->Sending()) { |
| 150 | channel->ProcessAndEncodeAudio(_audioFrame); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 151 | } |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | uint32_t TransmitMixer::CaptureLevel() const |
| 156 | { |
| 157 | return _captureLevel; |
| 158 | } |
| 159 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 160 | int32_t |
| 161 | TransmitMixer::StopSend() |
| 162 | { |
| 163 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1), |
| 164 | "TransmitMixer::StopSend()"); |
| 165 | _audioLevel.Clear(); |
| 166 | return 0; |
| 167 | } |
| 168 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 169 | int8_t TransmitMixer::AudioLevel() const |
| 170 | { |
| 171 | // Speech + file level [0,9] |
| 172 | return _audioLevel.Level(); |
| 173 | } |
| 174 | |
| 175 | int16_t TransmitMixer::AudioLevelFullRange() const |
| 176 | { |
| 177 | // Speech + file level [0,32767] |
| 178 | return _audioLevel.LevelFullRange(); |
| 179 | } |
| 180 | |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 181 | double TransmitMixer::GetTotalInputEnergy() const { |
zstein | 3c45186 | 2017-07-20 09:57:42 -0700 | [diff] [blame] | 182 | return _audioLevel.TotalEnergy(); |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | double TransmitMixer::GetTotalInputDuration() const { |
zstein | 3c45186 | 2017-07-20 09:57:42 -0700 | [diff] [blame] | 186 | return _audioLevel.TotalDuration(); |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 187 | } |
| 188 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 189 | void TransmitMixer::GenerateAudioFrame(const int16_t* audio, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 190 | size_t samples_per_channel, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 191 | size_t num_channels, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 192 | int sample_rate_hz) { |
| 193 | int codec_rate; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 194 | size_t num_codec_channels; |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 195 | GetSendCodecInfo(&codec_rate, &num_codec_channels); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 196 | stereo_codec_ = num_codec_channels == 2; |
| 197 | |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame] | 198 | // We want to process at the lowest rate possible without losing information. |
| 199 | // Choose the lowest native rate at least equal to the input and codec rates. |
| 200 | const int min_processing_rate = std::min(sample_rate_hz, codec_rate); |
| 201 | for (size_t i = 0; i < AudioProcessing::kNumNativeSampleRates; ++i) { |
| 202 | _audioFrame.sample_rate_hz_ = AudioProcessing::kNativeSampleRatesHz[i]; |
| 203 | if (_audioFrame.sample_rate_hz_ >= min_processing_rate) { |
| 204 | break; |
| 205 | } |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 206 | } |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame] | 207 | _audioFrame.num_channels_ = std::min(num_channels, num_codec_channels); |
| 208 | RemixAndResample(audio, samples_per_channel, num_channels, sample_rate_hz, |
| 209 | &resampler_, &_audioFrame); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 210 | } |
| 211 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 212 | void TransmitMixer::ProcessAudio(int delay_ms, int clock_drift, |
| 213 | int current_mic_level, bool key_pressed) { |
| 214 | if (audioproc_->set_stream_delay_ms(delay_ms) != 0) { |
pbos | ad85622 | 2015-11-27 09:48:36 -0800 | [diff] [blame] | 215 | // Silently ignore this failure to avoid flooding the logs. |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | GainControl* agc = audioproc_->gain_control(); |
| 219 | if (agc->set_stream_analog_level(current_mic_level) != 0) { |
pbos | ad85622 | 2015-11-27 09:48:36 -0800 | [diff] [blame] | 220 | LOG(LS_ERROR) << "set_stream_analog_level failed: current_mic_level = " |
| 221 | << current_mic_level; |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 222 | assert(false); |
| 223 | } |
| 224 | |
| 225 | EchoCancellation* aec = audioproc_->echo_cancellation(); |
| 226 | if (aec->is_drift_compensation_enabled()) { |
| 227 | aec->set_stream_drift_samples(clock_drift); |
| 228 | } |
| 229 | |
| 230 | audioproc_->set_stream_key_pressed(key_pressed); |
| 231 | |
| 232 | int err = audioproc_->ProcessStream(&_audioFrame); |
| 233 | if (err != 0) { |
| 234 | LOG(LS_ERROR) << "ProcessStream() error: " << err; |
| 235 | assert(false); |
| 236 | } |
| 237 | |
| 238 | // Store new capture level. Only updated when analog AGC is enabled. |
| 239 | _captureLevel = agc->stream_analog_level(); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 240 | } |
| 241 | |
henrik.lundin | f00082d | 2016-12-05 02:22:12 -0800 | [diff] [blame] | 242 | #if WEBRTC_VOICE_ENGINE_TYPING_DETECTION |
solenberg | fc3a2e3 | 2017-09-26 09:35:01 -0700 | [diff] [blame] | 243 | void TransmitMixer::TypingDetection(bool key_pressed) |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 244 | { |
| 245 | // We let the VAD determine if we're using this feature or not. |
| 246 | if (_audioFrame.vad_activity_ == AudioFrame::kVadUnknown) { |
| 247 | return; |
| 248 | } |
| 249 | |
solenberg | fc3a2e3 | 2017-09-26 09:35:01 -0700 | [diff] [blame] | 250 | bool vad_active = _audioFrame.vad_activity_ == AudioFrame::kVadActive; |
| 251 | bool typing_detected = typing_detection_.Process(key_pressed, vad_active); |
| 252 | |
| 253 | rtc::CritScope cs(&lock_); |
| 254 | typing_noise_detected_ = typing_detected; |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 255 | } |
| 256 | #endif |
| 257 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 258 | void TransmitMixer::EnableStereoChannelSwapping(bool enable) { |
| 259 | swap_stereo_channels_ = enable; |
| 260 | } |
| 261 | |
| 262 | bool TransmitMixer::IsStereoChannelSwappingEnabled() { |
| 263 | return swap_stereo_channels_; |
| 264 | } |
| 265 | |
solenberg | fc3a2e3 | 2017-09-26 09:35:01 -0700 | [diff] [blame] | 266 | bool TransmitMixer::typing_noise_detected() const { |
| 267 | rtc::CritScope cs(&lock_); |
| 268 | return typing_noise_detected_; |
| 269 | } |
| 270 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 271 | } // namespace voe |
| 272 | } // namespace webrtc |