| 6 |
| 7 | #include "logging.h" |
| 8 | #include "PrivateDefines.h" |
| 9 | #include "MediaStreamItf.h" |
| 10 | #include "EchoCanceller.h" |
| 11 |
| 12 | #include <cassert> |
| 13 | #include <cmath> |
| 14 | #include <cstdint> |
| 15 | #include <cstring> |
| 16 | #include <utility> |
| 17 |
| 18 | using namespace tgvoip; |
| 19 |
| 20 | void MediaStreamItf::SetCallback(std::function<std::size_t(std::uint8_t*, std::size_t, void*)> callback, void* param) |
| 21 | { |
| 22 | std::lock_guard<std::mutex> lock(m_mutexCallback); |
| 23 | m_callback = std::move(callback); |
| 24 | m_callbackParam = param; |
| 25 | } |
| 26 |
| 27 | std::size_t MediaStreamItf::InvokeCallback(std::uint8_t* data, std::size_t length) const |
| 28 | { |
| 29 | CallbackType callback; |
| 30 | void* callbackParam; |
| 31 | { |
| 32 | std::lock_guard<std::mutex> lock(m_mutexCallback); |
| 33 | callback = m_callback; |
| 34 | callbackParam = m_callbackParam; |
| 35 | } |
| 36 | if (callback != nullptr) |
| 37 | return callback(data, length, callbackParam); |
| 38 | return 0; |
| 39 | } |
| 40 |
| 41 | AudioMixer::AudioMixer() |
| 42 | : m_processedQueue(16) |
| 43 | , m_semaphore(16, 0) |
| 44 | { |
| 45 | } |
| 46 |
| 47 | AudioMixer::~AudioMixer() = default; |
| 48 |
| 49 | void AudioMixer::SetOutput(MediaStreamItf* output) |
| 50 | { |
| 51 | output->SetCallback(OutputCallback, this); |
| 52 | } |
| 53 |
| 54 | void AudioMixer::Start() |
| 55 | { |
| 56 | assert(!m_running); |
| 57 | m_running = true; |
| 58 | m_thread = new Thread(std::bind(&AudioMixer::RunThread, this)); |
| 59 | m_thread->SetName("AudioMixer"); |
| 60 | m_thread->Start(); |
| 61 | } |
| 62 |
| 63 | void AudioMixer::Stop() |
| 64 | { |
| 65 | if (!m_running) |
| 66 | { |
| 67 | LOGE("Tried to stop AudioMixer that wasn't started"); |
| 68 | return; |
| 69 | } |
| 70 | m_running = false; |
| 71 | m_semaphore.Release(); |
| 72 | m_thread->Join(); |
| 73 | delete m_thread; |
| 74 | m_thread = nullptr; |
| 75 | } |
| 76 |
| 77 | void AudioMixer::DoCallback(std::uint8_t* data, std::size_t length) |
| 78 | { |
| 79 | if (m_processedQueue.Size() == 0) |
| 80 | m_semaphore.Release(2); |
| 81 | else |
| 82 | m_semaphore.Release(); |
| 83 | Buffer buf = m_processedQueue.GetBlocking(); |
| 84 | std::memcpy(data, *buf, 960 * 2); |
| 85 | } |
| 86 |
| 87 | std::size_t AudioMixer::OutputCallback(std::uint8_t* data, std::size_t length, void* arg) |
| 88 | { |
| 89 | reinterpret_cast<AudioMixer*>(arg)->DoCallback(data, length); |
| 90 | return 960 * 2; |
| 91 | } |
| 92 |
| 93 | void AudioMixer::AddInput(MediaStreamItfPtr input) |
| 94 | { |
| 95 | MutexGuard m(m_inputsMutex); |
| 96 | m_inputs.emplace(std::move(input), 1); |
| 97 | } |
| 98 |
| 99 | void AudioMixer::RemoveInput(const MediaStreamItfPtr& input) |
| 100 | { |
| 101 | MutexGuard m(m_inputsMutex); |
| 102 | auto it = m_inputs.find(input); |
| 103 | if (it != m_inputs.end()) |
| 104 | m_inputs.erase(it); |
| 105 | } |
| 106 |
| 107 | void AudioMixer::SetInputVolume(const MediaStreamItfPtr& input, float volumeDB) |
| 108 | { |
| 109 | MutexGuard m(m_inputsMutex); |
| 110 | auto it = m_inputs.find(input); |
| 111 | if (it != m_inputs.end()) |
| 112 | { |
| 113 | if (volumeDB == -std::numeric_limits<float>::infinity()) |
| 114 | it->second = 0; |
| 115 | else |
| 116 | it->second = std::exp(volumeDB / 20.0f * std::log(10.0f)); |
| 117 | } |
| 118 | } |
| 119 |
| 120 | void AudioMixer::RunThread() |
| 121 | { |
| 122 | LOGV("AudioMixer thread started"); |
| 123 | while (m_running) |
| 124 | { |
| 125 | m_semaphore.Acquire(); |
| 126 | if (!m_running) |
| 127 | break; |
| 128 |
| 129 | try |
| 130 | { |
| 131 | Buffer data = m_bufferPool.Get(); |
| 132 | MutexGuard m(m_inputsMutex); |
| 133 | std::int16_t* buf = reinterpret_cast<std::int16_t*>(*data); |
| 134 |
| 135 | constexpr std::size_t SIZE = 960; |
| 136 | constexpr std::size_t INT16_SIZE = sizeof(std::int16_t); |
| 137 |
| 138 | std::array<std::int16_t, SIZE> input; |
| 139 | std::array<float, SIZE> out; |
| 140 | out.fill(0); |
| 141 | int usedInputs = 0; |
| 142 | for (auto& [source, multiplier] : m_inputs) |
| 143 | { |
| 144 | std::size_t res = source->InvokeCallback(reinterpret_cast<std::uint8_t*>(input.data()), STD_ARRAY_SIZEOF(input)); |
| 145 | if (res == 0 || multiplier == 0) |
| 146 | { |
| 147 | continue; |
| 148 | } |
| 149 | ++usedInputs; |
| 150 | for (std::size_t i = 0; i < SIZE; ++i) |
| 151 | { |
| 152 | out[i] += input[i] * multiplier; |
| 153 | } |
| 154 | } |
| 155 | if (usedInputs > 0) |
| 156 | { |
| 157 | for (std::size_t i = 0; i < SIZE; ++i) |
| 158 | { |
| 159 | if (out[i] > 32767.0f) |
| 160 | buf[i] = std::numeric_limits<std::int16_t>::max(); |
| 161 | else if (out[i] < -32768.0f) |
| 162 | buf[i] = std::numeric_limits<std::int16_t>::min(); |
| 163 | else |
| 164 | buf[i] = static_cast<std::int16_t>(out[i]); |
| 165 | } |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | std::memset(buf, 0, SIZE * INT16_SIZE); |
| 170 | } |
| 171 | if (m_echoCanceller != nullptr) |
| 172 | m_echoCanceller->SpeakerOutCallback(reinterpret_cast<std::uint8_t*>(buf), SIZE * INT16_SIZE); |
| 173 | m_processedQueue.Put(std::move(data)); |
| 174 | } |
| 175 | catch (const std::bad_alloc& exception) |
| 176 | { |
| 177 | LOGE("AudioMixer: no buffers left.\nwhat():\n%s", exception.what()); |
| 178 | continue; |
| 179 | } |
| 180 | } |
| 181 | LOGI("======== audio mixer thread exiting ========="); |
| 182 | } |
| 183 |
| 184 | void AudioMixer::SetEchoCanceller(EchoCanceller* aec) |
| 185 | { |
| 186 | m_echoCanceller = aec; |
| 187 | } |
| 188 |
| 189 | CallbackWrapper::CallbackWrapper() = default; |
| 190 |
| 191 | CallbackWrapper::~CallbackWrapper() = default; |
| 192 |
| 193 | void CallbackWrapper::Start() |
| 194 | { |
| 195 | } |
| 196 |
| 197 | void CallbackWrapper::Stop() |
| 198 | { |
| 199 | } |
| 200 |
| 201 | AudioLevelMeter::AudioLevelMeter() |
| 202 | { |
| 203 | m_absMax = 0; |
| 204 | m_count = 0; |
| 205 | m_currentLevel = 0; |
| 206 | m_currentLevelFullRange = 0; |
| 207 | } |
| 208 |
| 209 | float AudioLevelMeter::GetLevel() |
| 210 | { |
| 211 | return m_currentLevel / 9.0f; |
| 212 | } |
| 213 |
| 214 | void AudioLevelMeter::Update(std::int16_t* samples, std::size_t count) |
| 215 | { |
| 216 | // Number of bars on the indicator. |
| 217 | // Note that the number of elements is specified because we are indexing it |
| 218 | // in the range of 0-32 |
| 219 | const std::array<std::int8_t, 33> permutation = |
| 220 | { |
| 221 | 0, 1, 2, 3, 4, 4, 5, 5, 5, 5, 6, |
| 222 | 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, |
| 223 | 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 |
| 224 | }; |
| 225 | std::int16_t absValue = 0; |
| 226 | for (std::size_t k = 0; k < count; ++k) |
| 227 | { |
| 228 | std::int16_t absolute = static_cast<std::int16_t>(std::abs(samples[k])); |
| 229 | if (absolute > absValue) |
| 230 | absValue = absolute; |
| 231 | } |
| 232 |
| 233 | if (absValue > m_absMax) |
| 234 | m_absMax = absValue; |
| 235 | // Update level approximately 10 times per second |
| 236 | if (this->m_count++ == 10) |
| 237 | { |
| 238 | m_currentLevelFullRange = m_absMax; |
| 239 | this->m_count = 0; |
| 240 | // Highest value for a std::int16_t is 0x7fff = 32767 |
| 241 | // Divide with 1000 to get in the range of 0-32 which is the range of |
| 242 | // the permutation vector |
| 243 | std::size_t position = static_cast<std::size_t>(m_absMax) / 1000; |
| 244 | // Make it less likely that the bar stays at position 0. I.e. only if |
| 245 | // its in the range 0-250 (instead of 0-1000) |
| 246 | m_currentLevel = permutation[position]; |
| 247 | // Decay the absolute maximum (divide by 4) |
| 248 | m_absMax >>= 2; |
| 249 | } |
| 250 | } |
| 251 |