| 1 | // |
| 2 | // libtgvoip is free and unencumbered public domain software. |
| 3 | // For more information, see http://unlicense.org or the UNLICENSE file |
| 4 | // you should have received with this source code distribution. |
| 5 | // |
| 6 | |
| 7 | #include "logging.h" |
| 8 | #include "MediaStreamItf.h" |
| 9 | #include "EchoCanceller.h" |
| 10 | #include <stdint.h> |
| 11 | #include <algorithm> |
| 12 | #include <math.h> |
| 13 | #include <assert.h> |
| 14 | |
| 15 | using namespace tgvoip; |
| 16 | |
| 17 | void MediaStreamItf::SetCallback(size_t (*f)(unsigned char *, size_t, void*), void* param){ |
| 18 | std::lock_guard<std::mutex> lock(m_callback); |
| 19 | callback=f; |
| 20 | callbackParam=param; |
| 21 | } |
| 22 | |
| 23 | size_t MediaStreamItf::InvokeCallback(unsigned char *data, size_t length){ |
| 24 | std::lock_guard<std::mutex> lock(m_callback); |
| 25 | if(callback) |
| 26 | return (*callback)(data, length, callbackParam); |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | AudioMixer::AudioMixer() : processedQueue(16), semaphore(16, 0){ |
| 31 | running=false; |
| 32 | } |
| 33 | |
| 34 | AudioMixer::~AudioMixer(){ |
| 35 | } |
| 36 | |
| 37 | void AudioMixer::SetOutput(MediaStreamItf* output){ |
| 38 | output->SetCallback(OutputCallback, this); |
| 39 | } |
| 40 | |
| 41 | void AudioMixer::Start(){ |
| 42 | assert(!running); |
| 43 | running=true; |
| 44 | thread=new Thread(std::bind(&AudioMixer::RunThread, this)); |
| 45 | thread->SetName("AudioMixer"); |
| 46 | thread->Start(); |
| 47 | } |
| 48 | |
| 49 | void AudioMixer::Stop(){ |
| 50 | if(!running){ |
| 51 | LOGE("Tried to stop AudioMixer that wasn't started"); |
| 52 | return; |
| 53 | } |
| 54 | running=false; |
| 55 | semaphore.Release(); |
| 56 | thread->Join(); |
| 57 | delete thread; |
| 58 | thread=NULL; |
| 59 | } |
| 60 | |
| 61 | void AudioMixer::DoCallback(unsigned char *data, size_t length){ |
| 62 | //memset(data, 0, 960*2); |
| 63 | //LOGD("audio mixer callback, %d inputs", inputs.size()); |
| 64 | if(processedQueue.Size()==0) |
| 65 | semaphore.Release(2); |
| 66 | else |
| 67 | semaphore.Release(); |
| 68 | Buffer buf=processedQueue.GetBlocking(); |
| 69 | memcpy(data, *buf, 960*2); |
| 70 | } |
| 71 | |
| 72 | size_t AudioMixer::OutputCallback(unsigned char *data, size_t length, void *arg){ |
| 73 | ((AudioMixer*)arg)->DoCallback(data, length); |
| 74 | return 960*2; |
| 75 | } |
| 76 | |
| 77 | void AudioMixer::AddInput(std::shared_ptr<MediaStreamItf> input){ |
| 78 | MutexGuard m(inputsMutex); |
| 79 | MixerInput in; |
| 80 | in.multiplier=1; |
| 81 | in.source=input; |
| 82 | inputs.push_back(in); |
| 83 | } |
| 84 | |
| 85 | void AudioMixer::RemoveInput(std::shared_ptr<MediaStreamItf> input){ |
| 86 | MutexGuard m(inputsMutex); |
| 87 | for(std::vector<MixerInput>::iterator i=inputs.begin();i!=inputs.end();++i){ |
| 88 | if(i->source==input){ |
| 89 | inputs.erase(i); |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void AudioMixer::SetInputVolume(std::shared_ptr<MediaStreamItf> input, float volumeDB){ |
| 96 | MutexGuard m(inputsMutex); |
| 97 | for(std::vector<MixerInput>::iterator i=inputs.begin();i!=inputs.end();++i){ |
| 98 | if(i->source==input){ |
| 99 | if(volumeDB==-INFINITY) |
| 100 | i->multiplier=0; |
| 101 | else |
| 102 | i->multiplier=expf(volumeDB/20.0f * logf(10.0f)); |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void AudioMixer::RunThread(){ |
| 109 | LOGV("AudioMixer thread started"); |
| 110 | while(running){ |
| 111 | semaphore.Acquire(); |
| 112 | if(!running) |
| 113 | break; |
| 114 | |
| 115 | try{ |
| 116 | Buffer data=bufferPool.Get(); |
| 117 | //LOGV("Audio mixer processing a frame"); |
| 118 | MutexGuard m(inputsMutex); |
| 119 | int16_t *buf=reinterpret_cast<int16_t *>(*data); |
| 120 | int16_t input[960]; |
| 121 | float out[960]; |
| 122 | memset(out, 0, 960*4); |
| 123 | int usedInputs=0; |
| 124 | for(std::vector<MixerInput>::iterator in=inputs.begin(); in!=inputs.end(); ++in){ |
| 125 | size_t res=in->source->InvokeCallback(reinterpret_cast<unsigned char *>(input), 960*2); |
| 126 | if(!res || in->multiplier==0){ |
| 127 | //LOGV("AudioMixer: skipping silent packet"); |
| 128 | continue; |
| 129 | } |
| 130 | usedInputs++; |
| 131 | float k=in->multiplier; |
| 132 | if(k!=1){ |
| 133 | for(size_t i=0; i<960; i++){ |
| 134 | out[i]+=(float) input[i]*k; |
| 135 | } |
| 136 | }else{ |
| 137 | for(size_t i=0; i<960; i++){ |
| 138 | out[i]+=(float) input[i]; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | if(usedInputs>0){ |
| 143 | for(size_t i=0; i<960; i++){ |
| 144 | if(out[i]>32767.0f) |
| 145 | buf[i]=INT16_MAX; |
| 146 | else if(out[i]<-32768.0f) |
| 147 | buf[i]=INT16_MIN; |
| 148 | else |
| 149 | buf[i]=(int16_t) out[i]; |
| 150 | } |
| 151 | }else{ |
| 152 | memset(*data, 0, 960*2); |
| 153 | } |
| 154 | if(echoCanceller) |
| 155 | echoCanceller->SpeakerOutCallback(*data, 960*2); |
| 156 | processedQueue.Put(std::move(data)); |
| 157 | }catch(std::bad_alloc& x){ |
| 158 | LOGE("AudioMixer: no buffers left"); |
| 159 | continue; |
| 160 | } |
| 161 | } |
| 162 | LOGI("======== audio mixer thread exiting ========="); |
| 163 | } |
| 164 | |
| 165 | void AudioMixer::SetEchoCanceller(EchoCanceller *aec){ |
| 166 | echoCanceller=aec; |
| 167 | } |
| 168 | |
| 169 | AudioLevelMeter::AudioLevelMeter(){ |
| 170 | absMax=0; |
| 171 | count=0; |
| 172 | currentLevel=0; |
| 173 | currentLevelFullRange=0; |
| 174 | } |
| 175 | |
| 176 | float AudioLevelMeter::GetLevel(){ |
| 177 | return currentLevel/9.0f; |
| 178 | } |
| 179 | |
| 180 | void AudioLevelMeter::Update(int16_t *samples, size_t count){ |
| 181 | // Number of bars on the indicator. |
| 182 | // Note that the number of elements is specified because we are indexing it |
| 183 | // in the range of 0-32 |
| 184 | const int8_t permutation[33]={0,1,2,3,4,4,5,5,5,5,6, |
| 185 | int16_t absValue=0; |
| 186 | for(unsigned int k=0;k<count;k++){ |
| 187 | int16_t absolute=(int16_t)abs(samples[k]); |
| 188 | if (absolute>absValue) |
| 189 | absValue=absolute; |
| 190 | } |
| 191 | |
| 192 | if(absValue>absMax) |
| 193 | absMax = absValue; |
| 194 | // Update level approximately 10 times per second |
| 195 | if (this->count++==10){ |
| 196 | currentLevelFullRange=absMax; |
| 197 | this->count=0; |
| 198 | // Highest value for a int16_t is 0x7fff = 32767 |
| 199 | // Divide with 1000 to get in the range of 0-32 which is the range of |
| 200 | // the permutation vector |
| 201 | int32_t position=absMax/1000; |
| 202 | // Make it less likely that the bar stays at position 0. I.e. only if |
| 203 | // its in the range 0-250 (instead of 0-1000) |
| 204 | /*if ((position==0) && (absMax>250)){ |
| 205 | position=1; |
| 206 | }*/ |
| 207 | currentLevel=permutation[position]; |
| 208 | // Decay the absolute maximum (divide by 4) |
| 209 | absMax >>= 2; |
| 210 | } |
| 211 | } |
| 212 | |