| 6 |
| 7 | #include "logging.h" |
| 8 | #include "OpusEncoder.h" |
| 9 | #include "VoIPServerConfig.h" |
| 10 |
| 11 | #include <algorithm> |
| 12 | #include <cassert> |
| 13 | #include <cstring> |
| 14 | #if defined HAVE_CONFIG_H || defined TGVOIP_USE_INSTALLED_OPUS |
| 15 | #include <opus/opus.h> |
| 16 | #else |
| 17 | #include "opus.h" |
| 18 | #endif |
| 19 |
| 20 | namespace |
| 21 | { |
| 22 |
| 23 | int serverConfigValueToBandwidth(int config) |
| 24 | { |
| 25 | switch (config) |
| 26 | { |
| 27 | case 0: |
| 28 | return OPUS_BANDWIDTH_NARROWBAND; |
| 29 | case 1: |
| 30 | return OPUS_BANDWIDTH_MEDIUMBAND; |
| 31 | case 2: |
| 32 | return OPUS_BANDWIDTH_WIDEBAND; |
| 33 | case 3: |
| 34 | return OPUS_BANDWIDTH_SUPERWIDEBAND; |
| 35 | case 4: |
| 36 | default: |
| 37 | return OPUS_BANDWIDTH_FULLBAND; |
| 38 | } |
| 39 | } |
| 40 |
| 41 | } // namespace |
| 42 |
| 43 | tgvoip::OpusEncoder::OpusEncoder(MediaStreamItf* source, bool needSecondary) |
| 44 | : m_queue(10) |
| 45 | { |
| 46 | this->m_source = source; |
| 47 | source->SetCallback(tgvoip::OpusEncoder::Callback, this); |
| 48 | m_enc = opus_encoder_create(48000, 1, OPUS_APPLICATION_VOIP, nullptr); |
| 49 | opus_encoder_ctl(m_enc, OPUS_SET_COMPLEXITY(10)); |
| 50 | opus_encoder_ctl(m_enc, OPUS_SET_PACKET_LOSS_PERC(1)); |
| 51 | opus_encoder_ctl(m_enc, OPUS_SET_INBAND_FEC(1)); |
| 52 | opus_encoder_ctl(m_enc, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE)); |
| 53 | opus_encoder_ctl(m_enc, OPUS_SET_BANDWIDTH(OPUS_AUTO)); |
| 54 | m_requestedBitrate = 20000; |
| 55 | m_currentBitrate = 0; |
| 56 | m_running = false; |
| 57 | m_echoCanceller = nullptr; |
| 58 | m_complexity = 10; |
| 59 | m_frameDuration = 20; |
| 60 | m_levelMeter = nullptr; |
| 61 | m_vadNoVoiceBitrate = static_cast<std::uint32_t>(ServerConfig::GetSharedInstance()->GetInt("audio_vad_no_voice_bitrate", 6000)); |
| 62 | m_vadModeVoiceBandwidth = serverConfigValueToBandwidth(ServerConfig::GetSharedInstance()->GetInt("audio_vad_bandwidth", 3)); |
| 63 | m_vadModeNoVoiceBandwidth = serverConfigValueToBandwidth(ServerConfig::GetSharedInstance()->GetInt("audio_vad_no_voice_bandwidth", 0)); |
| 64 | m_secondaryEnabledBandwidth = serverConfigValueToBandwidth(ServerConfig::GetSharedInstance()->GetInt("audio_extra_ec_bandwidth", 2)); |
| 65 | m_secondaryEncoderEnabled = false; |
| 66 |
| 67 | if (needSecondary) |
| 68 | { |
| 69 | m_secondaryEncoder = opus_encoder_create(48000, 1, OPUS_APPLICATION_VOIP, nullptr); |
| 70 | opus_encoder_ctl(m_secondaryEncoder, OPUS_SET_COMPLEXITY(10)); |
| 71 | opus_encoder_ctl(m_secondaryEncoder, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE)); |
| 72 | opus_encoder_ctl(m_secondaryEncoder, OPUS_SET_BITRATE(8000)); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | m_secondaryEncoder = nullptr; |
| 77 | } |
| 78 | } |
| 79 |
| 80 | tgvoip::OpusEncoder::~OpusEncoder() |
| 81 | { |
| 82 | opus_encoder_destroy(m_enc); |
| 83 | if (m_secondaryEncoder != nullptr) |
| 84 | opus_encoder_destroy(m_secondaryEncoder); |
| 85 | } |
| 86 |
| 87 | void tgvoip::OpusEncoder::Start() |
| 88 | { |
| 89 | if (m_running) |
| 90 | return; |
| 91 | m_running = true; |
| 92 | m_thread = new Thread(std::bind(&tgvoip::OpusEncoder::RunThread, this)); |
| 93 | m_thread->SetName("OpusEncoder"); |
| 94 | m_thread->SetMaxPriority(); |
| 95 | m_thread->Start(); |
| 96 | } |
| 97 |
| 98 | void tgvoip::OpusEncoder::Stop() |
| 99 | { |
| 100 | if (!m_running) |
| 101 | return; |
| 102 | m_running = false; |
| 103 | m_queue.Put(Buffer()); |
| 104 | m_thread->Join(); |
| 105 | delete m_thread; |
| 106 | } |
| 107 |
| 108 | void tgvoip::OpusEncoder::SetBitrate(std::uint32_t bitrate) |
| 109 | { |
| 110 | m_requestedBitrate = bitrate; |
| 111 | } |
| 112 |
| 113 | void tgvoip::OpusEncoder::Encode(std::int16_t* data, std::size_t len) |
| 114 | { |
| 115 | if (m_requestedBitrate != m_currentBitrate) |
| 116 | { |
| 117 | opus_encoder_ctl(m_enc, OPUS_SET_BITRATE(m_requestedBitrate)); |
| 118 | m_currentBitrate = m_requestedBitrate; |
| 119 | LOGV("opus_encoder: setting bitrate to %u", m_currentBitrate); |
| 120 | } |
| 121 | if (m_levelMeter) |
| 122 | m_levelMeter->Update(data, len); |
| 123 | if (m_secondaryEncoderEnabled != m_wasSecondaryEncoderEnabled) |
| 124 | { |
| 125 | m_wasSecondaryEncoderEnabled = m_secondaryEncoderEnabled; |
| 126 | } |
| 127 | std::int32_t r = opus_encode(m_enc, data, static_cast<int>(len), m_buffer, 4096); |
| 128 | if (r <= 0) |
| 129 | { |
| 130 | LOGE("Error encoding: %d", r); |
| 131 | } |
| 132 | else if (r == 1) |
| 133 | { |
| 134 | LOGW("DTX"); |
| 135 | } |
| 136 | else if (m_running) |
| 137 | { |
| 138 | std::int32_t secondaryLen = 0; |
| 139 | std::uint8_t secondaryBuffer[128]; |
| 140 | if (m_secondaryEncoderEnabled && m_secondaryEncoder != nullptr) |
| 141 | { |
| 142 | secondaryLen = opus_encode(m_secondaryEncoder, data, static_cast<int>(len), secondaryBuffer, sizeof(secondaryBuffer)); |
| 143 | } |
| 144 | InvokeCallback(m_buffer, static_cast<std::size_t>(r), secondaryBuffer, static_cast<std::size_t>(secondaryLen)); |
| 145 | } |
| 146 | } |
| 147 |
| 148 | std::size_t tgvoip::OpusEncoder::Callback(std::uint8_t* data, std::size_t len, void* param) |
| 149 | { |
| 150 | assert(len == 960 * 2); |
| 151 | OpusEncoder* e = reinterpret_cast<OpusEncoder*>(param); |
| 152 | try |
| 153 | { |
| 154 | Buffer buf = e->m_bufferPool.Get(); |
| 155 | buf.CopyFrom(data, 0, 960 * 2); |
| 156 | e->m_queue.Put(std::move(buf)); |
| 157 | } |
| 158 | catch (const std::bad_alloc& exception) |
| 159 | { |
| 160 | LOGW("opus_encoder: no buffer slots left.\nwhat():\n%s", exception.what()); |
| 161 | if (e->m_complexity > 1) |
| 162 | { |
| 163 | --e->m_complexity; |
| 164 | opus_encoder_ctl(e->m_enc, OPUS_SET_COMPLEXITY(e->m_complexity)); |
| 165 | } |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 |
| 170 | std::uint32_t tgvoip::OpusEncoder::GetBitrate() const |
| 171 | { |
| 172 | return m_requestedBitrate; |
| 173 | } |
| 174 |
| 175 | void tgvoip::OpusEncoder::SetEchoCanceller(EchoCanceller* aec) |
| 176 | { |
| 177 | m_echoCanceller = aec; |
| 178 | } |
| 179 |
| 180 | void tgvoip::OpusEncoder::RunThread() |
| 181 | { |
| 182 | std::uint32_t bufferedCount = 0; |
| 183 | std::uint32_t packetsPerFrame = m_frameDuration / 20; |
| 184 | LOGV("starting encoder, packets per frame=%d", packetsPerFrame); |
| 185 | std::int16_t* frame; |
| 186 | if (packetsPerFrame > 1) |
| 187 | frame = reinterpret_cast<std::int16_t*>(std::malloc(960 * 2 * packetsPerFrame)); |
| 188 | else |
| 189 | frame = nullptr; |
| 190 | bool frameHasVoice = false; |
| 191 | bool wasVadMode = false; |
| 192 | while (m_running) |
| 193 | { |
| 194 | Buffer _packet = m_queue.GetBlocking(); |
| 195 | if (!_packet.IsEmpty()) |
| 196 | { |
| 197 | std::int16_t* packet = reinterpret_cast<std::int16_t*>(*_packet); |
| 198 | bool hasVoice = true; |
| 199 | if (m_echoCanceller) |
| 200 | m_echoCanceller->ProcessInput(packet, 960, hasVoice); |
| 201 | if (!m_postProcEffects.empty()) |
| 202 | { |
| 203 | for (effects::AudioEffect* effect : m_postProcEffects) |
| 204 | { |
| 205 | effect->Process(packet, 960); |
| 206 | } |
| 207 | } |
| 208 | if (packetsPerFrame == 1) |
| 209 | { |
| 210 | Encode(packet, 960); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | std::memcpy(frame + (960 * bufferedCount), packet, 960 * 2); |
| 215 | frameHasVoice = frameHasVoice || hasVoice; |
| 216 | bufferedCount++; |
| 217 | if (bufferedCount == packetsPerFrame) |
| 218 | { |
| 219 | if (m_vadMode) |
| 220 | { |
| 221 | if (frameHasVoice) |
| 222 | { |
| 223 | opus_encoder_ctl(m_enc, OPUS_SET_BITRATE(m_currentBitrate)); |
| 224 | if (m_secondaryEncoder) |
| 225 | { |
| 226 | opus_encoder_ctl(m_secondaryEncoder, OPUS_SET_BITRATE(m_currentBitrate)); |
| 227 | } |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | opus_encoder_ctl(m_enc, OPUS_SET_BITRATE(m_vadNoVoiceBitrate)); |
| 232 | if (m_secondaryEncoder) |
| 233 | { |
| 234 | opus_encoder_ctl(m_secondaryEncoder, OPUS_SET_BITRATE(m_vadNoVoiceBitrate)); |
| 235 | } |
| 236 | } |
| 237 | wasVadMode = true; |
| 238 | } |
| 239 | else if (wasVadMode) |
| 240 | { |
| 241 | wasVadMode = false; |
| 242 | opus_encoder_ctl(m_enc, OPUS_SET_BITRATE(m_currentBitrate)); |
| 243 | if (m_secondaryEncoder) |
| 244 | { |
| 245 | opus_encoder_ctl(m_secondaryEncoder, OPUS_SET_BITRATE(m_currentBitrate)); |
| 246 | } |
| 247 | } |
| 248 | Encode(frame, 960 * packetsPerFrame); |
| 249 | bufferedCount = 0; |
| 250 | frameHasVoice = false; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | if (frame != nullptr) |
| 260 | std::free(frame); |
| 261 | } |
| 262 |
| 263 | void tgvoip::OpusEncoder::SetOutputFrameDuration(std::uint32_t duration) |
| 264 | { |
| 265 | m_frameDuration = duration; |
| 266 | } |
| 267 |
| 268 | void tgvoip::OpusEncoder::SetPacketLoss(int percent) |
| 269 | { |
| 270 | m_packetLossPercent = std::min(20, percent); |
| 271 | opus_encoder_ctl(m_enc, OPUS_SET_PACKET_LOSS_PERC(m_packetLossPercent)); |
| 272 | opus_encoder_ctl(m_enc, OPUS_SET_INBAND_FEC(percent > 0 && !m_secondaryEncoderEnabled ? 1 : 0)); |
| 273 | } |
| 274 |
| 275 | int tgvoip::OpusEncoder::GetPacketLoss() const |
| 276 | { |
| 277 | return m_packetLossPercent; |
| 278 | } |
| 279 |
| 280 | void tgvoip::OpusEncoder::SetDTX(bool enable) |
| 281 | { |
| 282 | opus_encoder_ctl(m_enc, OPUS_SET_DTX(enable ? 1 : 0)); |
| 283 | } |
| 284 |
| 285 | void tgvoip::OpusEncoder::SetLevelMeter(tgvoip::AudioLevelMeter* levelMeter) |
| 286 | { |
| 287 | this->m_levelMeter = levelMeter; |
| 288 | } |
| 289 |
| 290 | void tgvoip::OpusEncoder::SetCallback(std::function<void(std::uint8_t*, std::size_t, std::uint8_t*, std::size_t)> callback) |
| 291 | { |
| 292 | m_callback = std::move(callback); |
| 293 | } |
| 294 |
| 295 | void tgvoip::OpusEncoder::InvokeCallback(std::uint8_t* data, std::size_t length, std::uint8_t* secondaryData, std::size_t secondaryLength) |
| 296 | { |
| 297 | m_callback(data, length, secondaryData, secondaryLength); |
| 298 | } |
| 299 |
| 300 | void tgvoip::OpusEncoder::SetSecondaryEncoderEnabled(bool enabled) |
| 301 | { |
| 302 | m_secondaryEncoderEnabled = enabled; |
| 303 | } |
| 304 |
| 305 | void tgvoip::OpusEncoder::SetVadMode(bool vad) |
| 306 | { |
| 307 | m_vadMode = vad; |
| 308 | } |
| 309 | void tgvoip::OpusEncoder::AddAudioEffect(effects::AudioEffect* effect) |
| 310 | { |
| 311 | m_postProcEffects.emplace_back(effect); |
| 312 | } |
| 313 |
| 314 | void tgvoip::OpusEncoder::RemoveAudioEffect(effects::AudioEffect* effect) |
| 315 | { |
| 316 | auto it = std::find(m_postProcEffects.begin(), m_postProcEffects.end(), effect); |
| 317 | if (it != m_postProcEffects.end()) |
| 318 | m_postProcEffects.erase(it); |
| 319 | } |
| 320 |
| 321 | int tgvoip::OpusEncoder::GetComplexity() const |
| 322 | { |
| 323 | return m_complexity; |
| 324 | } |
| 325 |