| 6 |
| 7 | #include "logging.h" |
| 8 | #include "OpusDecoder.h" |
| 9 | #include "audio/Resampler.h" |
| 10 |
| 11 | #include <algorithm> |
| 12 | #include <cassert> |
| 13 | #include <cmath> |
| 14 | #include <cstring> |
| 15 | #if defined HAVE_CONFIG_H || defined TGVOIP_USE_INSTALLED_OPUS |
| 16 | #include <opus/opus.h> |
| 17 | #else |
| 18 | #include "opus.h" |
| 19 | #endif |
| 20 |
| 21 | #include "VoIPController.h" |
| 22 |
| 23 | #define PACKET_SIZE (960 * 2) |
| 24 |
| 25 | using namespace tgvoip; |
| 26 |
| 27 | tgvoip::OpusDecoder::OpusDecoder(MediaStreamItf* dst, bool isAsync, bool needEC) |
| 28 | { |
| 29 | dst->SetCallback(OpusDecoder::Callback, this); |
| 30 | Initialize(isAsync, needEC); |
| 31 | } |
| 32 |
| 33 | void tgvoip::OpusDecoder::Initialize(bool isAsync, bool needEC) |
| 34 | { |
| 35 | m_async = isAsync; |
| 36 | if (m_async) |
| 37 | { |
| 38 | m_decodedQueue = new BlockingQueue<Buffer>(33); |
| 39 | m_semaphore = new Semaphore(32, 0); |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | m_decodedQueue = nullptr; |
| 44 | m_semaphore = nullptr; |
| 45 | } |
| 46 | m_dec = opus_decoder_create(48000, 1, nullptr); |
| 47 | if (needEC) |
| 48 | m_ecDec = opus_decoder_create(48000, 1, nullptr); |
| 49 | else |
| 50 | m_ecDec = nullptr; |
| 51 | m_buffer=(unsigned char *) malloc(8192); |
| 52 | m_lastDecoded = nullptr; |
| 53 | m_outputBufferSize = 0; |
| 54 | m_echoCanceller = nullptr; |
| 55 | m_frameDuration = 20; |
| 56 | m_consecutiveLostPackets = 0; |
| 57 | m_enableDTX = false; |
| 58 | m_silentPacketCount = 0; |
| 59 | m_levelMeter = nullptr; |
| 60 | m_nextLen = 0; |
| 61 | m_running = false; |
| 62 | m_remainingDataLen = 0; |
| 63 | m_processedBuffer = nullptr; |
| 64 | m_prevWasEC = false; |
| 65 | m_prevLastSample = 0; |
| 66 | } |
| 67 |
| 68 | tgvoip::OpusDecoder::~OpusDecoder() |
| 69 | { |
| 70 | opus_decoder_destroy(m_dec); |
| 71 | if (m_ecDec) |
| 72 | opus_decoder_destroy(m_ecDec); |
| 73 | std::free(m_buffer); |
| 74 | delete m_decodedQueue; |
| 75 | delete m_semaphore; |
| 76 | } |
| 77 |
| 78 | void tgvoip::OpusDecoder::SetEchoCanceller(EchoCanceller* canceller) |
| 79 | { |
| 80 | m_echoCanceller = canceller; |
| 81 | } |
| 82 |
| 83 | std::size_t tgvoip::OpusDecoder::Callback(std::uint8_t* data, std::size_t len, void* param) |
| 84 | { |
| 85 | return (reinterpret_cast<OpusDecoder*>(param)->HandleCallback(data, len)); |
| 86 | } |
| 87 |
| 88 | std::size_t tgvoip::OpusDecoder::HandleCallback(std::uint8_t* data, std::size_t len) |
| 89 | { |
| 90 | if (m_async) |
| 91 | { |
| 92 | if (!m_running) |
| 93 | { |
| 94 | std::memset(data, 0, len); |
| 95 | return 0; |
| 96 | } |
| 97 | if (m_outputBufferSize == 0) |
| 98 | { |
| 99 | m_outputBufferSize = len; |
| 100 | int packetsNeeded; |
| 101 | if (len > PACKET_SIZE) |
| 102 | packetsNeeded = static_cast<int>(len) / PACKET_SIZE; |
| 103 | else |
| 104 | packetsNeeded = 1; |
| 105 | packetsNeeded *= 2; |
| 106 | m_semaphore->Release(packetsNeeded); |
| 107 | } |
| 108 | assert(m_outputBufferSize == len && "output buffer size is supposed to be the same throughout callbacks"); |
| 109 | if (len == PACKET_SIZE) |
| 110 | { |
| 111 | Buffer lastDecoded = m_decodedQueue->GetBlocking(); |
| 112 | if (lastDecoded.IsEmpty()) |
| 113 | return 0; |
| 114 | std::memcpy(data, *lastDecoded, PACKET_SIZE); |
| 115 | m_semaphore->Release(); |
| 116 | if (m_silentPacketCount > 0) |
| 117 | { |
| 118 | --m_silentPacketCount; |
| 119 | if (m_levelMeter) |
| 120 | m_levelMeter->Update(reinterpret_cast<std::int16_t*>(data), 0); |
| 121 | return 0; |
| 122 | } |
| 123 | if (m_echoCanceller) |
| 124 | { |
| 125 | m_echoCanceller->SpeakerOutCallback(data, PACKET_SIZE); |
| 126 | } |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | LOGE("Opus decoder buffer length != 960 samples"); |
| 131 | std::abort(); |
| 132 | } |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | if (m_remainingDataLen == 0 && m_silentPacketCount == 0) |
| 137 | { |
| 138 | int duration = DecodeNextFrame(); |
| 139 | m_remainingDataLen = static_cast<std::size_t>(duration) / 20 * 960 * 2; |
| 140 | } |
| 141 | if (m_silentPacketCount > 0 || m_remainingDataLen == 0 || m_processedBuffer == nullptr) |
| 142 | { |
| 143 | if (m_silentPacketCount > 0) |
| 144 | m_silentPacketCount--; |
| 145 | std::memset(data, 0, 960 * 2); |
| 146 | if (m_levelMeter) |
| 147 | m_levelMeter->Update(reinterpret_cast<std::int16_t*>(data), 0); |
| 148 | return 0; |
| 149 | } |
| 150 | std::memcpy(data, m_processedBuffer, 960 * 2); |
| 151 | m_remainingDataLen -= 960 * 2; |
| 152 | if (m_remainingDataLen > 0) |
| 153 | { |
| 154 | std::memmove(m_processedBuffer, m_processedBuffer + 960 * 2, static_cast<std::size_t>(m_remainingDataLen)); |
| 155 | } |
| 156 | } |
| 157 | if (m_levelMeter != nullptr) |
| 158 | m_levelMeter->Update(reinterpret_cast<std::int16_t*>(data), len / 2); |
| 159 | return len; |
| 160 | } |
| 161 |
| 162 | void tgvoip::OpusDecoder::Start() |
| 163 | { |
| 164 | if (!m_async) |
| 165 | return; |
| 166 | m_running = true; |
| 167 | m_thread = new Thread(std::bind(&tgvoip::OpusDecoder::RunThread, this)); |
| 168 | m_thread->SetName("opus_decoder"); |
| 169 | m_thread->SetMaxPriority(); |
| 170 | m_thread->Start(); |
| 171 | } |
| 172 |
| 173 | void tgvoip::OpusDecoder::Stop() |
| 174 | { |
| 175 | if (!m_running || !m_async) |
| 176 | return; |
| 177 | m_running = false; |
| 178 | m_semaphore->Release(); |
| 179 | m_thread->Join(); |
| 180 | delete m_thread; |
| 181 | } |
| 182 |
| 183 | void tgvoip::OpusDecoder::RunThread() |
| 184 | { |
| 185 | LOGI("decoder: packets per frame %d", m_packetsPerFrame); |
| 186 | while (m_running) |
| 187 | { |
| 188 | int playbackDuration = DecodeNextFrame(); |
| 189 | for (int i = 0; i < playbackDuration / 20; ++i) |
| 190 | { |
| 191 | m_semaphore->Acquire(); |
| 192 | if (!m_running) |
| 193 | { |
| 194 | LOGI("==== decoder exiting ===="); |
| 195 | return; |
| 196 | } |
| 197 | try |
| 198 | { |
| 199 | Buffer buf = m_bufferPool.Get(); |
| 200 | if (m_remainingDataLen > 0) |
| 201 | { |
| 202 | for (effects::AudioEffect*& effect : m_postProcEffects) |
| 203 | { |
| 204 | effect->Process(reinterpret_cast<std::int16_t*>(m_processedBuffer + (PACKET_SIZE * i)), 960); |
| 205 | } |
| 206 | buf.CopyFrom(m_processedBuffer + (PACKET_SIZE * i), 0, PACKET_SIZE); |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | std::memset(*buf, 0, PACKET_SIZE); |
| 211 | } |
| 212 | m_decodedQueue->Put(std::move(buf)); |
| 213 | } |
| 214 | catch (const std::bad_alloc& exception) |
| 215 | { |
| 216 | LOGW("decoder: no buffers left!\nwhat():\n%s", exception.what()); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 |
| 222 | int tgvoip::OpusDecoder::DecodeNextFrame() |
| 223 | { |
| 224 | int playbackDuration = 0; |
| 225 | bool isEC = false; |
| 226 | std::size_t len = m_jitterBuffer->HandleOutput(m_buffer, 8192, 0, true, playbackDuration, isEC); |
| 227 | bool fec = false; |
| 228 | if (len == 0) |
| 229 | { |
| 230 | fec = true; |
| 231 | len = m_jitterBuffer->HandleOutput(m_buffer, 8192, 0, false, playbackDuration, isEC); |
| 232 | } |
| 233 | int size; |
| 234 | if (len != 0) |
| 235 | { |
| 236 | size = opus_decode(isEC ? m_ecDec : m_dec, m_buffer, len, reinterpret_cast<opus_int16*>(m_decodeBuffer), m_packetsPerFrame * 960, fec ? 1 : 0); |
| 237 | m_consecutiveLostPackets = 0; |
| 238 | if (m_prevWasEC != isEC && size) |
| 239 | { |
| 240 | // It turns out the waveforms generated by the PLC feature are also great to help smooth out the |
| 241 | // otherwise audible transition between the frames from different decoders. Those are basically an extrapolation |
| 242 | // of the previous successfully decoded data -- which is exactly what we need here. |
| 243 | size = opus_decode(m_prevWasEC ? m_ecDec : m_dec, nullptr, 0, reinterpret_cast<opus_int16*>(m_nextBuffer), m_packetsPerFrame * 960, 0); |
| 244 | if (size != 0) |
| 245 | { |
| 246 | std::int16_t* plcSamples = reinterpret_cast<std::int16_t*>(m_nextBuffer); |
| 247 | std::int16_t* samples = reinterpret_cast<std::int16_t*>(m_decodeBuffer); |
| 248 | constexpr float coeffs[] = { 0.999802f, 0.995062f, 0.984031f, 0.966778f, 0.943413f, 0.914084f, 0.878975f, 0.838309f, 0.792344f, 0.741368f, |
| 249 | 0.685706f, 0.625708f, 0.561754f, 0.494249f, 0.423619f, 0.350311f, 0.274788f, 0.197527f, 0.119018f, 0.039757f }; |
| 250 | for (int i = 0; i < 20; ++i) |
| 251 | { |
| 252 | samples[i] = static_cast<std::int16_t>(std::round(plcSamples[i] * coeffs[i] + samples[i] * (1.f - coeffs[i]))); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | m_prevWasEC = isEC; |
| 257 | m_prevLastSample = m_decodeBuffer[size - 1]; |
| 258 | } |
| 259 | else |
| 260 | { // do packet loss concealment |
| 261 | m_consecutiveLostPackets++; |
| 262 | if (m_consecutiveLostPackets > 2 && m_enableDTX) |
| 263 | { |
| 264 | m_silentPacketCount += m_packetsPerFrame; |
| 265 | size = static_cast<int>(m_packetsPerFrame) * 960; |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | size = opus_decode(m_prevWasEC ? m_ecDec : m_dec, nullptr, 0, reinterpret_cast<opus_int16*>(m_decodeBuffer), m_packetsPerFrame * 960, 0); |
| 270 | } |
| 271 | } |
| 272 | if (size < 0) |
| 273 | LOGW("decoder: opus_decode error %d", size); |
| 274 | m_remainingDataLen = size; |
| 275 | if (playbackDuration == 80) |
| 276 | { |
| 277 | m_processedBuffer = m_buffer; |
| 278 | audio::Resampler::Rescale60To80(reinterpret_cast<std::int16_t*>(m_decodeBuffer), |
| 279 | reinterpret_cast<std::int16_t*>(m_processedBuffer)); |
| 280 | } |
| 281 | else if (playbackDuration == 40) |
| 282 | { |
| 283 | m_processedBuffer = m_buffer; |
| 284 | audio::Resampler::Rescale60To40(reinterpret_cast<std::int16_t*>(m_decodeBuffer), |
| 285 | reinterpret_cast<std::int16_t*>(m_processedBuffer)); |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | m_processedBuffer = m_decodeBuffer; |
| 290 | } |
| 291 | return playbackDuration; |
| 292 | } |
| 293 |
| 294 | void tgvoip::OpusDecoder::SetFrameDuration(std::uint32_t duration) |
| 295 | { |
| 296 | m_frameDuration = duration; |
| 297 | m_packetsPerFrame = m_frameDuration / 20; |
| 298 | } |
| 299 |
| 300 | void tgvoip::OpusDecoder::SetJitterBuffer(std::shared_ptr<JitterBuffer> jitterBuffer) |
| 301 | { |
| 302 | m_jitterBuffer = std::move(jitterBuffer); |
| 303 | } |
| 304 |
| 305 | void tgvoip::OpusDecoder::SetDTX(bool enable) |
| 306 | { |
| 307 | m_enableDTX = enable; |
| 308 | } |
| 309 |
| 310 | void tgvoip::OpusDecoder::SetLevelMeter(AudioLevelMeter* levelMeter) |
| 311 | { |
| 312 | m_levelMeter = levelMeter; |
| 313 | } |
| 314 |
| 315 | void tgvoip::OpusDecoder::AddAudioEffect(effects::AudioEffect* effect) |
| 316 | { |
| 317 | m_postProcEffects.emplace_back(effect); |
| 318 | } |
| 319 |
| 320 | void tgvoip::OpusDecoder::RemoveAudioEffect(effects::AudioEffect* effect) |
| 321 | { |
| 322 | auto it = std::find(m_postProcEffects.begin(), m_postProcEffects.end(), effect); |
| 323 | if (it != m_postProcEffects.end()) |
| 324 | m_postProcEffects.erase(it); |
| 325 | } |
| 326 |