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