MediaStreamItf.cpp 5.1 KB

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