VoIPGroupController.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 "VoIPController.h"
  7. #include "logging.h"
  8. #include "VoIPServerConfig.h"
  9. #include "PrivateDefines.h"
  10. #include <assert.h>
  11. #include <math.h>
  12. #include <time.h>
  13. using namespace tgvoip;
  14. using namespace std;
  15. VoIPGroupController::VoIPGroupController(int32_t timeDifference){
  16. audioMixer=new AudioMixer();
  17. memset(&callbacks, 0, sizeof(callbacks));
  18. userSelfID=0;
  19. this->timeDifference=timeDifference;
  20. LOGV("Created VoIPGroupController; timeDifference=%d", timeDifference);
  21. }
  22. VoIPGroupController::~VoIPGroupController(){
  23. if(audioOutput){
  24. audioOutput->Stop();
  25. }
  26. LOGD("before stop audio mixer");
  27. audioMixer->Stop();
  28. delete audioMixer;
  29. for(vector<GroupCallParticipant>::iterator p=participants.begin();p!=participants.end();p++){
  30. if(p->levelMeter)
  31. delete p->levelMeter;
  32. }
  33. }
  34. void VoIPGroupController::SetGroupCallInfo(unsigned char *encryptionKey, unsigned char *reflectorGroupTag, unsigned char *reflectorSelfTag, unsigned char *reflectorSelfSecret, unsigned char* reflectorSelfTagHash, int32_t selfUserID, IPv4Address reflectorAddress, IPv6Address reflectorAddressV6, uint16_t reflectorPort){
  35. Endpoint e;
  36. e.address=reflectorAddress;
  37. e.v6address=reflectorAddressV6;
  38. e.port=reflectorPort;
  39. memcpy(e.peerTag, reflectorGroupTag, 16);
  40. e.type=Endpoint::Type::UDP_RELAY;
  41. e.id=FOURCC('G','R','P','R');
  42. endpoints[e.id]=e;
  43. groupReflector=e;
  44. currentEndpoint=e.id;
  45. memcpy(this->encryptionKey, encryptionKey, 256);
  46. memcpy(this->reflectorSelfTag, reflectorSelfTag, 16);
  47. memcpy(this->reflectorSelfSecret, reflectorSelfSecret, 16);
  48. memcpy(this->reflectorSelfTagHash, reflectorSelfTagHash, 16);
  49. uint8_t sha256[SHA256_LENGTH];
  50. crypto.sha256((uint8_t*) encryptionKey, 256, sha256);
  51. memcpy(callID, sha256+(SHA256_LENGTH-16), 16);
  52. memcpy(keyFingerprint, sha256+(SHA256_LENGTH-16), 8);
  53. this->userSelfID=selfUserID;
  54. //LOGD("reflectorSelfTag = %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X", reflectorSelfTag[0], reflectorSelfTag[1], reflectorSelfTag[2], reflectorSelfTag[3], reflectorSelfTag[4], reflectorSelfTag[5], reflectorSelfTag[6], reflectorSelfTag[7], reflectorSelfTag[8], reflectorSelfTag[9], reflectorSelfTag[10], reflectorSelfTag[11], reflectorSelfTag[12], reflectorSelfTag[13], reflectorSelfTag[14], reflectorSelfTag[15]);
  55. //LOGD("reflectorSelfSecret = %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X", reflectorSelfSecret[0], reflectorSelfSecret[1], reflectorSelfSecret[2], reflectorSelfSecret[3], reflectorSelfSecret[4], reflectorSelfSecret[5], reflectorSelfSecret[6], reflectorSelfSecret[7], reflectorSelfSecret[8], reflectorSelfSecret[9], reflectorSelfSecret[10], reflectorSelfSecret[11], reflectorSelfSecret[12], reflectorSelfSecret[13], reflectorSelfSecret[14], reflectorSelfSecret[15]);
  56. //LOGD("reflectorSelfTagHash = %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X", reflectorSelfTagHash[0], reflectorSelfTagHash[1], reflectorSelfTagHash[2], reflectorSelfTagHash[3], reflectorSelfTagHash[4], reflectorSelfTagHash[5], reflectorSelfTagHash[6], reflectorSelfTagHash[7], reflectorSelfTagHash[8], reflectorSelfTagHash[9], reflectorSelfTagHash[10], reflectorSelfTagHash[11], reflectorSelfTagHash[12], reflectorSelfTagHash[13], reflectorSelfTagHash[14], reflectorSelfTagHash[15]);
  57. }
  58. void VoIPGroupController::AddGroupCallParticipant(int32_t userID, unsigned char *memberTagHash, unsigned char* serializedStreams, size_t streamsLength){
  59. if(userID==userSelfID)
  60. return;
  61. if(userSelfID==0)
  62. return;
  63. //if(streamsLength==0)
  64. // return;
  65. MutexGuard m(participantsMutex);
  66. LOGV("Adding group call user %d, streams length %u", userID, (unsigned int)streamsLength);
  67. for(vector<GroupCallParticipant>::iterator p=participants.begin();p!=participants.end();++p){
  68. if(p->userID==userID){
  69. LOGE("user %d already added", userID);
  70. abort();
  71. break;
  72. }
  73. }
  74. GroupCallParticipant p;
  75. p.userID=userID;
  76. memcpy(p.memberTagHash, memberTagHash, sizeof(p.memberTagHash));
  77. p.levelMeter=new AudioLevelMeter();
  78. BufferInputStream ss(serializedStreams, streamsLength);
  79. vector<shared_ptr<Stream>> streams=DeserializeStreams(ss);
  80. unsigned char audioStreamID=0;
  81. for(vector<shared_ptr<Stream>>::iterator _s=streams.begin();_s!=streams.end();++_s){
  82. shared_ptr<Stream>& s=*_s;
  83. s->userID=userID;
  84. if(s->type==STREAM_TYPE_AUDIO && s->codec==CODEC_OPUS && !audioStreamID){
  85. audioStreamID=s->id;
  86. s->jitterBuffer=make_shared<JitterBuffer>(nullptr, s->frameDuration);
  87. if(s->frameDuration>50)
  88. s->jitterBuffer->SetMinPacketCount((uint32_t) ServerConfig::GetSharedInstance()->GetInt("jitter_initial_delay_60", 2));
  89. else if(s->frameDuration>30)
  90. s->jitterBuffer->SetMinPacketCount((uint32_t) ServerConfig::GetSharedInstance()->GetInt("jitter_initial_delay_40", 4));
  91. else
  92. s->jitterBuffer->SetMinPacketCount((uint32_t) ServerConfig::GetSharedInstance()->GetInt("jitter_initial_delay_20", 6));
  93. s->callbackWrapper=make_shared<CallbackWrapper>();
  94. s->decoder=make_shared<OpusDecoder>(s->callbackWrapper, false, false);
  95. s->decoder->SetJitterBuffer(s->jitterBuffer);
  96. s->decoder->SetFrameDuration(s->frameDuration);
  97. s->decoder->SetDTX(true);
  98. s->decoder->SetLevelMeter(p.levelMeter);
  99. audioMixer->AddInput(s->callbackWrapper);
  100. }
  101. incomingStreams.push_back(s);
  102. }
  103. if(!audioStreamID){
  104. LOGW("User %d has no usable audio stream", userID);
  105. }
  106. p.streams.insert(p.streams.end(), streams.begin(), streams.end());
  107. participants.push_back(p);
  108. LOGI("Added group call participant %d", userID);
  109. }
  110. void VoIPGroupController::RemoveGroupCallParticipant(int32_t userID){
  111. MutexGuard m(participantsMutex);
  112. vector<shared_ptr<Stream>>::iterator stm=incomingStreams.begin();
  113. while(stm!=incomingStreams.end()){
  114. if((*stm)->userID==userID){
  115. LOGI("Removed stream %d belonging to user %d", (*stm)->id, userID);
  116. audioMixer->RemoveInput((*stm)->callbackWrapper);
  117. (*stm)->decoder->Stop();
  118. //delete (*stm)->decoder;
  119. //delete (*stm)->jitterBuffer;
  120. //delete (*stm)->callbackWrapper;
  121. stm=incomingStreams.erase(stm);
  122. continue;
  123. }
  124. ++stm;
  125. }
  126. for(vector<GroupCallParticipant>::iterator p=participants.begin();p!=participants.end();++p){
  127. if(p->userID==userID){
  128. if(p->levelMeter)
  129. delete p->levelMeter;
  130. participants.erase(p);
  131. LOGI("Removed group call participant %d", userID);
  132. break;
  133. }
  134. }
  135. }
  136. vector<shared_ptr<VoIPController::Stream>> VoIPGroupController::DeserializeStreams(BufferInputStream& in){
  137. vector<shared_ptr<Stream>> res;
  138. try{
  139. unsigned char count=in.ReadByte();
  140. for(unsigned char i=0;i<count;i++){
  141. uint16_t len=(uint16_t) in.ReadInt16();
  142. BufferInputStream inner=in.GetPartBuffer(len, true);
  143. shared_ptr<Stream> s=make_shared<Stream>();
  144. s->id=inner.ReadByte();
  145. s->type=inner.ReadByte();
  146. s->codec=(uint32_t) inner.ReadInt32();
  147. uint32_t flags=(uint32_t) inner.ReadInt32();
  148. s->enabled=(flags & STREAM_FLAG_ENABLED)==STREAM_FLAG_ENABLED;
  149. s->frameDuration=(uint16_t) inner.ReadInt16();
  150. res.push_back(s);
  151. }
  152. }catch(out_of_range& x){
  153. LOGW("Error deserializing streams: %s", x.what());
  154. }
  155. return res;
  156. }
  157. void VoIPGroupController::SetParticipantStreams(int32_t userID, unsigned char *serializedStreams, size_t length){
  158. LOGD("Set participant streams for %d", userID);
  159. MutexGuard m(participantsMutex);
  160. for(vector<GroupCallParticipant>::iterator p=participants.begin();p!=participants.end();++p){
  161. if(p->userID==userID){
  162. BufferInputStream in(serializedStreams, length);
  163. vector<shared_ptr<Stream>> streams=DeserializeStreams(in);
  164. for(vector<shared_ptr<Stream>>::iterator ns=streams.begin();ns!=streams.end();++ns){
  165. bool found=false;
  166. for(vector<shared_ptr<Stream>>::iterator s=p->streams.begin();s!=p->streams.end();++s){
  167. if((*s)->id==(*ns)->id){
  168. (*s)->enabled=(*ns)->enabled;
  169. if(groupCallbacks.participantAudioStateChanged)
  170. groupCallbacks.participantAudioStateChanged(this, userID, (*s)->enabled);
  171. found=true;
  172. break;
  173. }
  174. }
  175. if(!found){
  176. LOGW("Tried to add stream %d for user %d but adding/removing streams is not supported", (*ns)->id, userID);
  177. }
  178. }
  179. break;
  180. }
  181. }
  182. }
  183. size_t VoIPGroupController::GetInitialStreams(unsigned char *buf, size_t size){
  184. BufferOutputStream s(buf, size);
  185. s.WriteByte(1); // streams count
  186. s.WriteInt16(12); // this object length
  187. s.WriteByte(1); // stream id
  188. s.WriteByte(STREAM_TYPE_AUDIO);
  189. s.WriteInt32(CODEC_OPUS);
  190. s.WriteInt32(STREAM_FLAG_ENABLED | STREAM_FLAG_DTX); // flags
  191. s.WriteInt16(60); // frame duration
  192. return s.GetLength();
  193. }
  194. void VoIPGroupController::SendInit(){
  195. SendRecentPacketsRequest();
  196. }
  197. void VoIPGroupController::ProcessIncomingPacket(NetworkPacket &packet, Endpoint& srcEndpoint){
  198. //LOGD("Received incoming packet from %s:%u, %u bytes", packet.address->ToString().c_str(), packet.port, packet.length);
  199. if(packet.length<17 || packet.length>2000){
  200. LOGW("Received packet has wrong length %d", (int)packet.length);
  201. return;
  202. }
  203. BufferOutputStream sigData(packet.length);
  204. sigData.WriteBytes(packet.data, packet.length-16);
  205. sigData.WriteBytes(reflectorSelfSecret, 16);
  206. unsigned char sig[32];
  207. crypto.sha256(sigData.GetBuffer(), sigData.GetLength(), sig);
  208. if(memcmp(sig, packet.data+(packet.length-16), 16)!=0){
  209. LOGW("Received packet has incorrect signature");
  210. return;
  211. }
  212. // reflector special response
  213. if(memcmp(packet.data, reflectorSelfTagHash, 16)==0 && packet.length>60){
  214. //LOGI("possible reflector special response");
  215. unsigned char firstBlock[16];
  216. unsigned char iv[16];
  217. memcpy(iv, packet.data+16, 16);
  218. unsigned char key[32];
  219. crypto.sha256(reflectorSelfSecret, 16, key);
  220. crypto.aes_cbc_decrypt(packet.data+32, firstBlock, 16, key, iv);
  221. BufferInputStream in(firstBlock, 16);
  222. in.Seek(8);
  223. size_t len=(size_t) in.ReadInt32();
  224. int32_t tlid=in.ReadInt32();
  225. //LOGD("special response: len=%d, tlid=0x%08X", len, tlid);
  226. if(len%4==0 && len+60<=packet.length && packet.length<=1500){
  227. lastRecvPacketTime=GetCurrentTime();
  228. memcpy(iv, packet.data+16, 16);
  229. unsigned char buf[1500];
  230. crypto.aes_cbc_decrypt(packet.data+32, buf, len+16, key, iv);
  231. try{
  232. if(tlid==TLID_UDP_REFLECTOR_LAST_PACKETS_INFO){
  233. MutexGuard m(sentPacketsMutex);
  234. //LOGV("received udpReflector.lastPacketsInfo");
  235. in=BufferInputStream(buf, len+16);
  236. in.Seek(16);
  237. /*int32_t date=*/in.ReadInt32();
  238. /*int64_t queryID=*/in.ReadInt64();
  239. int32_t vectorMagic=in.ReadInt32();
  240. if(vectorMagic!=TLID_VECTOR){
  241. LOGW("last packets info: expected vector, got %08X", vectorMagic);
  242. return;
  243. }
  244. int32_t recvCount=in.ReadInt32();
  245. //LOGV("%d received packets", recvCount);
  246. for(int i=0;i<recvCount;i++){
  247. uint32_t p=(uint32_t) in.ReadInt32();
  248. //LOGV("Relay received packet: %08X", p);
  249. uint16_t id=(uint16_t) (p & 0xFFFF);
  250. //LOGV("ack id %04X", id);
  251. for(vector<PacketIdMapping>::iterator pkt=recentSentPackets.begin();pkt!=recentSentPackets.end();++pkt){
  252. //LOGV("== sent id %04X", pkt->id);
  253. if(pkt->id==id){
  254. if(!pkt->ackTime){
  255. pkt->ackTime=GetCurrentTime();
  256. conctl->PacketAcknowledged(pkt->seq);
  257. //LOGV("relay acknowledged packet %u", pkt->seq);
  258. if(seqgt(pkt->seq, lastRemoteAckSeq))
  259. lastRemoteAckSeq=pkt->seq;
  260. }
  261. break;
  262. }
  263. }
  264. }
  265. vectorMagic=in.ReadInt32();
  266. if(vectorMagic!=TLID_VECTOR){
  267. LOGW("last packets info: expected vector, got %08X", vectorMagic);
  268. return;
  269. }
  270. int32_t sentCount=in.ReadInt32();
  271. //LOGV("%d sent packets", sentCount);
  272. for(int i=0;i<sentCount;i++){
  273. /*int32_t p=*/in.ReadInt32();
  274. //LOGV("Sent packet: %08X", p);
  275. }
  276. if(udpConnectivityState!=UDP_AVAILABLE)
  277. udpConnectivityState=UDP_AVAILABLE;
  278. if(state!=STATE_ESTABLISHED)
  279. SetState(STATE_ESTABLISHED);
  280. if(!audioInput){
  281. InitializeAudio();
  282. if(state!=STATE_FAILED){
  283. // audioOutput->Start();
  284. }
  285. }
  286. }
  287. }catch(out_of_range& x){
  288. LOGE("Error parsing special response: %s", x.what());
  289. }
  290. return;
  291. }
  292. }
  293. if(packet.length<32)
  294. return;
  295. // it's a packet relayed from another participant - find the sender
  296. MutexGuard m(participantsMutex);
  297. GroupCallParticipant* sender=NULL;
  298. for(vector<GroupCallParticipant>::iterator p=participants.begin();p!=participants.end();++p){
  299. if(memcmp(packet.data, p->memberTagHash, 16)==0){
  300. //LOGV("received data packet from user %d", p->userID);
  301. sender=&*p;
  302. break;
  303. }
  304. }
  305. if(!sender){
  306. LOGV("Received data packet is from unknown user");
  307. return;
  308. }
  309. if(memcmp(packet.data+16, keyFingerprint, 8)!=0){
  310. LOGW("received packet has wrong key fingerprint");
  311. return;
  312. }
  313. BufferInputStream in(packet.data, packet.length-16);
  314. in.Seek(16+8); // peer tag + key fingerprint
  315. unsigned char msgKey[16];
  316. in.ReadBytes(msgKey, 16);
  317. unsigned char decrypted[1500];
  318. unsigned char aesKey[32], aesIv[32];
  319. KDF2(msgKey, 0, aesKey, aesIv);
  320. size_t decryptedLen=in.Remaining()-16;
  321. if(decryptedLen>sizeof(decrypted))
  322. return;
  323. //LOGV("-> MSG KEY: %08x %08x %08x %08x, hashed %u", *reinterpret_cast<int32_t*>(msgKey), *reinterpret_cast<int32_t*>(msgKey+4), *reinterpret_cast<int32_t*>(msgKey+8), *reinterpret_cast<int32_t*>(msgKey+12), decryptedLen-4);
  324. uint8_t *decryptOffset = packet.data + in.GetOffset();
  325. if ((((intptr_t)decryptOffset) % sizeof(long)) != 0) {
  326. LOGE("alignment2 packet.data+in.GetOffset()");
  327. }
  328. if (decryptedLen % sizeof(long) != 0) {
  329. LOGE("alignment2 decryptedLen");
  330. }
  331. crypto.aes_ige_decrypt(packet.data+in.GetOffset(), decrypted, decryptedLen, aesKey, aesIv);
  332. in=BufferInputStream(decrypted, decryptedLen);
  333. //LOGD("received packet length: %d", in.ReadInt32());
  334. BufferOutputStream buf(decryptedLen+32);
  335. size_t x=0;
  336. buf.WriteBytes(encryptionKey+88+x, 32);
  337. buf.WriteBytes(decrypted+4, decryptedLen-4);
  338. unsigned char msgKeyLarge[32];
  339. crypto.sha256(buf.GetBuffer(), buf.GetLength(), msgKeyLarge);
  340. if(memcmp(msgKey, msgKeyLarge+8, 16)!=0){
  341. LOGW("Received packet from user %d has wrong hash", sender->userID);
  342. return;
  343. }
  344. uint32_t innerLen=(uint32_t) in.ReadInt32();
  345. if(innerLen>decryptedLen-4){
  346. LOGW("Received packet has wrong inner length (%d with total of %u)", (int)innerLen, (unsigned int)decryptedLen);
  347. return;
  348. }
  349. if(decryptedLen-innerLen<12){
  350. LOGW("Received packet has too little padding (%u)", (unsigned int)(decryptedLen-innerLen));
  351. return;
  352. }
  353. in=BufferInputStream(decrypted+4, (size_t) innerLen);
  354. uint32_t tlid=(uint32_t) in.ReadInt32();
  355. if(tlid!=TLID_DECRYPTED_AUDIO_BLOCK){
  356. LOGW("Received packet has unknown TL ID 0x%08x", tlid);
  357. return;
  358. }
  359. in.Seek(in.GetOffset()+16); // random bytes
  360. int32_t flags=in.ReadInt32();
  361. if(!(flags & PFLAG_HAS_SEQ) || !(flags & PFLAG_HAS_SENDER_TAG_HASH)){
  362. LOGW("Received packet has wrong flags");
  363. return;
  364. }
  365. /*uint32_t seq=(uint32_t) */in.ReadInt32();
  366. unsigned char senderTagHash[16];
  367. in.ReadBytes(senderTagHash, 16);
  368. if(memcmp(senderTagHash, sender->memberTagHash, 16)!=0){
  369. LOGW("Received packet has wrong inner sender tag hash");
  370. return;
  371. }
  372. //int32_t oneMoreInnerLengthWhyDoWeEvenNeedThis;
  373. if(flags & PFLAG_HAS_DATA){
  374. /*oneMoreInnerLengthWhyDoWeEvenNeedThis=*/in.ReadTlLength();
  375. }
  376. unsigned char type=(unsigned char) ((flags >> 24) & 0xFF);
  377. lastRecvPacketTime=GetCurrentTime();
  378. if(type==PKT_STREAM_DATA || type==PKT_STREAM_DATA_X2 || type==PKT_STREAM_DATA_X3){
  379. if(state!=STATE_ESTABLISHED && receivedInitAck)
  380. SetState(STATE_ESTABLISHED);
  381. int count;
  382. switch(type){
  383. case PKT_STREAM_DATA_X2:
  384. count=2;
  385. break;
  386. case PKT_STREAM_DATA_X3:
  387. count=3;
  388. break;
  389. case PKT_STREAM_DATA:
  390. default:
  391. count=1;
  392. break;
  393. }
  394. int i;
  395. //if(srcEndpoint->type==Endpoint::Type::UDP_RELAY && srcEndpoint!=peerPreferredRelay){
  396. // peerPreferredRelay=srcEndpoint;
  397. //}
  398. for(i=0;i<count;i++){
  399. unsigned char streamID=in.ReadByte();
  400. unsigned char sflags=(unsigned char) (streamID & 0xC0);
  401. uint16_t sdlen=(uint16_t) (sflags & STREAM_DATA_FLAG_LEN16 ? in.ReadInt16() : in.ReadByte());
  402. uint32_t pts=(uint32_t) in.ReadInt32();
  403. //LOGD("stream data, pts=%d, len=%d, rem=%d", pts, sdlen, in.Remaining());
  404. audioTimestampIn=pts;
  405. /*if(!audioOutStarted && audioOutput){
  406. audioOutput->Start();
  407. audioOutStarted=true;
  408. }*/
  409. if(in.GetOffset()+sdlen>in.GetLength()){
  410. return;
  411. }
  412. for(vector<shared_ptr<Stream>>::iterator stm=sender->streams.begin();stm!=sender->streams.end();++stm){
  413. if((*stm)->id==streamID){
  414. if((*stm)->jitterBuffer){
  415. (*stm)->jitterBuffer->HandleInput(decrypted+4+in.GetOffset(), sdlen, pts, false);
  416. }
  417. break;
  418. }
  419. }
  420. if(i<count-1)
  421. in.Seek(in.GetOffset()+sdlen);
  422. }
  423. }
  424. }
  425. void VoIPGroupController::SendUdpPing(Endpoint& endpoint){
  426. }
  427. void VoIPGroupController::SetNetworkType(int type){
  428. networkType=type;
  429. UpdateDataSavingState();
  430. UpdateAudioBitrateLimit();
  431. string itfName=udpSocket->GetLocalInterfaceInfo(NULL, NULL);
  432. if(itfName!=activeNetItfName){
  433. udpSocket->OnActiveInterfaceChanged();
  434. LOGI("Active network interface changed: %s -> %s", activeNetItfName.c_str(), itfName.c_str());
  435. bool isFirstChange=activeNetItfName.length()==0;
  436. activeNetItfName=itfName;
  437. if(isFirstChange)
  438. return;
  439. udpConnectivityState=UDP_UNKNOWN;
  440. udpPingCount=0;
  441. lastUdpPingTime=0;
  442. if(proxyProtocol==PROXY_SOCKS5)
  443. InitUDPProxy();
  444. selectCanceller->CancelSelect();
  445. }
  446. }
  447. void VoIPGroupController::SendRecentPacketsRequest(){
  448. BufferOutputStream out(1024);
  449. out.WriteInt32(TLID_UDP_REFLECTOR_REQUEST_PACKETS_INFO); // TL function
  450. out.WriteInt32(GetCurrentUnixtime()); // date:int
  451. out.WriteInt64(0); // query_id:long
  452. out.WriteInt32(64); // recv_num:int
  453. out.WriteInt32(0); // sent_num:int
  454. SendSpecialReflectorRequest(out.GetBuffer(), out.GetLength());
  455. }
  456. void VoIPGroupController::SendSpecialReflectorRequest(unsigned char *data, size_t len){
  457. BufferOutputStream out(1024);
  458. unsigned char buf[1500];
  459. crypto.rand_bytes(buf, 8);
  460. out.WriteBytes(buf, 8);
  461. out.WriteInt32((int32_t)len);
  462. out.WriteBytes(data, len);
  463. if(out.GetLength()%16!=0){
  464. size_t paddingLen=16-(out.GetLength()%16);
  465. crypto.rand_bytes(buf, paddingLen);
  466. out.WriteBytes(buf, paddingLen);
  467. }
  468. unsigned char iv[16];
  469. crypto.rand_bytes(iv, 16);
  470. unsigned char key[32];
  471. crypto.sha256(reflectorSelfSecret, 16, key);
  472. unsigned char _iv[16];
  473. memcpy(_iv, iv, 16);
  474. size_t encryptedLen=out.GetLength();
  475. crypto.aes_cbc_encrypt(out.GetBuffer(), buf, encryptedLen, key, _iv);
  476. out.Reset();
  477. out.WriteBytes(reflectorSelfTag, 16);
  478. out.WriteBytes(iv, 16);
  479. out.WriteBytes(buf, encryptedLen);
  480. out.WriteBytes(reflectorSelfSecret, 16);
  481. crypto.sha256(out.GetBuffer(), out.GetLength(), buf);
  482. out.Rewind(16);
  483. out.WriteBytes(buf, 16);
  484. NetworkPacket pkt={0};
  485. pkt.address=&groupReflector.address;
  486. pkt.port=groupReflector.port;
  487. pkt.protocol=PROTO_UDP;
  488. pkt.data=out.GetBuffer();
  489. pkt.length=out.GetLength();
  490. ActuallySendPacket(pkt, groupReflector);
  491. }
  492. void VoIPGroupController::SendRelayPings(){
  493. //LOGV("Send relay pings 2");
  494. double currentTime=GetCurrentTime();
  495. if(currentTime-groupReflector.lastPingTime>=0.25){
  496. SendRecentPacketsRequest();
  497. groupReflector.lastPingTime=currentTime;
  498. }
  499. }
  500. void VoIPGroupController::OnAudioOutputReady(){
  501. encoder->SetDTX(true);
  502. audioMixer->SetOutput(audioOutput);
  503. audioMixer->SetEchoCanceller(echoCanceller);
  504. audioMixer->Start();
  505. audioOutput->Start();
  506. audioOutStarted=true;
  507. encoder->SetLevelMeter(&selfLevelMeter);
  508. }
  509. void VoIPGroupController::WritePacketHeader(uint32_t seq, BufferOutputStream *s, unsigned char type, uint32_t length){
  510. s->WriteInt32(TLID_DECRYPTED_AUDIO_BLOCK);
  511. int64_t randomID;
  512. crypto.rand_bytes((uint8_t *) &randomID, 8);
  513. s->WriteInt64(randomID);
  514. unsigned char randBytes[7];
  515. crypto.rand_bytes(randBytes, 7);
  516. s->WriteByte(7);
  517. s->WriteBytes(randBytes, 7);
  518. uint32_t pflags=PFLAG_HAS_SEQ | PFLAG_HAS_SENDER_TAG_HASH;
  519. if(length>0)
  520. pflags|=PFLAG_HAS_DATA;
  521. pflags|=((uint32_t) type) << 24;
  522. s->WriteInt32(pflags);
  523. if(type==PKT_STREAM_DATA || type==PKT_STREAM_DATA_X2 || type==PKT_STREAM_DATA_X3){
  524. conctl->PacketSent(seq, length);
  525. }
  526. /*if(pflags & PFLAG_HAS_CALL_ID){
  527. s->WriteBytes(callID, 16);
  528. }*/
  529. //s->WriteInt32(lastRemoteSeq);
  530. s->WriteInt32(seq);
  531. s->WriteBytes(reflectorSelfTagHash, 16);
  532. if(length>0){
  533. if(length<=253){
  534. s->WriteByte((unsigned char) length);
  535. }else{
  536. s->WriteByte(254);
  537. s->WriteByte((unsigned char) (length & 0xFF));
  538. s->WriteByte((unsigned char) ((length >> 8) & 0xFF));
  539. s->WriteByte((unsigned char) ((length >> 16) & 0xFF));
  540. }
  541. }
  542. }
  543. void VoIPGroupController::SendPacket(unsigned char *data, size_t len, Endpoint& ep, PendingOutgoingPacket& srcPacket){
  544. if(stopping)
  545. return;
  546. if(ep.type==Endpoint::Type::TCP_RELAY && !useTCP)
  547. return;
  548. BufferOutputStream out(len+128);
  549. //LOGV("send group packet %u", len);
  550. out.WriteBytes(reflectorSelfTag, 16);
  551. if(len>0){
  552. BufferOutputStream inner(len+128);
  553. inner.WriteInt32((uint32_t)len);
  554. inner.WriteBytes(data, len);
  555. size_t padLen=16-inner.GetLength()%16;
  556. if(padLen<12)
  557. padLen+=16;
  558. unsigned char padding[28];
  559. crypto.rand_bytes((uint8_t *) padding, padLen);
  560. inner.WriteBytes(padding, padLen);
  561. assert(inner.GetLength()%16==0);
  562. unsigned char key[32], iv[32], msgKey[16];
  563. out.WriteBytes(keyFingerprint, 8);
  564. BufferOutputStream buf(len+32);
  565. size_t x=0;
  566. buf.WriteBytes(encryptionKey+88+x, 32);
  567. buf.WriteBytes(inner.GetBuffer()+4, inner.GetLength()-4);
  568. unsigned char msgKeyLarge[32];
  569. crypto.sha256(buf.GetBuffer(), buf.GetLength(), msgKeyLarge);
  570. memcpy(msgKey, msgKeyLarge+8, 16);
  571. KDF2(msgKey, 0, key, iv);
  572. out.WriteBytes(msgKey, 16);
  573. //LOGV("<- MSG KEY: %08x %08x %08x %08x, hashed %u", *reinterpret_cast<int32_t*>(msgKey), *reinterpret_cast<int32_t*>(msgKey+4), *reinterpret_cast<int32_t*>(msgKey+8), *reinterpret_cast<int32_t*>(msgKey+12), inner.GetLength()-4);
  574. unsigned char aesOut[MSC_STACK_FALLBACK(inner.GetLength(), 1500)];
  575. crypto.aes_ige_encrypt(inner.GetBuffer(), aesOut, inner.GetLength(), key, iv);
  576. out.WriteBytes(aesOut, inner.GetLength());
  577. }
  578. // relay signature
  579. out.WriteBytes(reflectorSelfSecret, 16);
  580. unsigned char sig[32];
  581. crypto.sha256(out.GetBuffer(), out.GetLength(), sig);
  582. out.Rewind(16);
  583. out.WriteBytes(sig, 16);
  584. if(srcPacket.type==PKT_STREAM_DATA || srcPacket.type==PKT_STREAM_DATA_X2 || srcPacket.type==PKT_STREAM_DATA_X3){
  585. PacketIdMapping mapping={srcPacket.seq, *reinterpret_cast<uint16_t*>(sig+14), 0};
  586. MutexGuard m(sentPacketsMutex);
  587. recentSentPackets.push_back(mapping);
  588. //LOGD("sent packet with id: %04X", mapping.id);
  589. while(recentSentPackets.size()>64)
  590. recentSentPackets.erase(recentSentPackets.begin());
  591. }
  592. lastSentSeq=srcPacket.seq;
  593. if(IS_MOBILE_NETWORK(networkType))
  594. stats.bytesSentMobile+=(uint64_t)out.GetLength();
  595. else
  596. stats.bytesSentWifi+=(uint64_t)out.GetLength();
  597. NetworkPacket pkt={0};
  598. pkt.address=(NetworkAddress*)&ep.address;
  599. pkt.port=ep.port;
  600. pkt.length=out.GetLength();
  601. pkt.data=out.GetBuffer();
  602. pkt.protocol=ep.type==Endpoint::Type::TCP_RELAY ? PROTO_TCP : PROTO_UDP;
  603. ActuallySendPacket(pkt, ep);
  604. }
  605. void VoIPGroupController::SetCallbacks(VoIPGroupController::Callbacks callbacks){
  606. VoIPController::SetCallbacks(callbacks);
  607. this->groupCallbacks=callbacks;
  608. }
  609. int32_t VoIPGroupController::GetCurrentUnixtime(){
  610. return time(NULL)+timeDifference;
  611. }
  612. float VoIPGroupController::GetParticipantAudioLevel(int32_t userID){
  613. if(userID==userSelfID)
  614. return selfLevelMeter.GetLevel();
  615. MutexGuard m(participantsMutex);
  616. for(vector<GroupCallParticipant>::iterator p=participants.begin(); p!=participants.end(); ++p){
  617. if(p->userID==userID){
  618. return p->levelMeter->GetLevel();
  619. }
  620. }
  621. return 0;
  622. }
  623. void VoIPGroupController::SetMicMute(bool mute){
  624. micMuted=mute;
  625. if(audioInput){
  626. if(mute)
  627. audioInput->Stop();
  628. else
  629. audioInput->Start();
  630. if(!audioInput->IsInitialized()){
  631. lastError=ERROR_AUDIO_IO;
  632. SetState(STATE_FAILED);
  633. return;
  634. }
  635. }
  636. outgoingStreams[0]->enabled=!mute;
  637. SerializeAndUpdateOutgoingStreams();
  638. }
  639. void VoIPGroupController::SetParticipantVolume(int32_t userID, float volume){
  640. MutexGuard m(participantsMutex);
  641. for(vector<GroupCallParticipant>::iterator p=participants.begin();p!=participants.end();++p){
  642. if(p->userID==userID){
  643. for(vector<shared_ptr<Stream>>::iterator s=p->streams.begin();s!=p->streams.end();++s){
  644. if((*s)->type==STREAM_TYPE_AUDIO){
  645. if((*s)->decoder){
  646. float db;
  647. if(volume==0.0f)
  648. db=-INFINITY;
  649. else if(volume<1.0f)
  650. db=-50.0f*(1.0f-volume);
  651. else if(volume>1.0f && volume<=2.0f)
  652. db=10.0f*(volume-1.0f);
  653. else
  654. db=0.0f;
  655. //LOGV("Setting user %u audio volume to %.2f dB", userID, db);
  656. audioMixer->SetInputVolume((*s)->callbackWrapper, db);
  657. }
  658. break;
  659. }
  660. }
  661. break;
  662. }
  663. }
  664. }
  665. void VoIPGroupController::SerializeAndUpdateOutgoingStreams(){
  666. BufferOutputStream out(1024);
  667. out.WriteByte((unsigned char) outgoingStreams.size());
  668. for(vector<shared_ptr<Stream>>::iterator s=outgoingStreams.begin(); s!=outgoingStreams.end(); ++s){
  669. BufferOutputStream o(128);
  670. o.WriteByte((*s)->id);
  671. o.WriteByte((*s)->type);
  672. o.WriteInt32((*s)->codec);
  673. o.WriteInt32((unsigned char) (((*s)->enabled ? STREAM_FLAG_ENABLED : 0) | STREAM_FLAG_DTX));
  674. o.WriteInt16((*s)->frameDuration);
  675. out.WriteInt16((int16_t) o.GetLength());
  676. out.WriteBytes(o.GetBuffer(), o.GetLength());
  677. }
  678. if(groupCallbacks.updateStreams)
  679. groupCallbacks.updateStreams(this, out.GetBuffer(), out.GetLength());
  680. }
  681. std::string VoIPGroupController::GetDebugString(){
  682. std::string r="Remote endpoints: \n";
  683. char buffer[2048];
  684. for(pair<const int64_t, Endpoint>& _endpoint:endpoints){
  685. Endpoint& endpoint=_endpoint.second;
  686. const char* type;
  687. switch(endpoint.type){
  688. case Endpoint::Type::UDP_P2P_INET:
  689. type="UDP_P2P_INET";
  690. break;
  691. case Endpoint::Type::UDP_P2P_LAN:
  692. type="UDP_P2P_LAN";
  693. break;
  694. case Endpoint::Type::UDP_RELAY:
  695. type="UDP_RELAY";
  696. break;
  697. case Endpoint::Type::TCP_RELAY:
  698. type="TCP_RELAY";
  699. break;
  700. default:
  701. type="UNKNOWN";
  702. break;
  703. }
  704. snprintf(buffer, sizeof(buffer), "%s:%u %dms [%s%s]\n", endpoint.address.ToString().c_str(), endpoint.port, (int)(endpoint.averageRTT*1000), type, currentEndpoint==endpoint.id ? ", IN_USE" : "");
  705. r+=buffer;
  706. }
  707. double avgLate[3];
  708. shared_ptr<JitterBuffer> jitterBuffer=incomingStreams.size()==1 ? incomingStreams[0]->jitterBuffer : NULL;
  709. if(jitterBuffer)
  710. jitterBuffer->GetAverageLateCount(avgLate);
  711. else
  712. memset(avgLate, 0, 3*sizeof(double));
  713. snprintf(buffer, sizeof(buffer),
  714. "RTT avg/min: %d/%d\n"
  715. "Congestion window: %d/%d bytes\n"
  716. "Key fingerprint: %02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n"
  717. "Last sent/ack'd seq: %u/%u\n"
  718. "Send/recv losses: %u/%u (%d%%)\n"
  719. "Audio bitrate: %d kbit\n"
  720. "Bytes sent/recvd: %llu/%llu\n\n",
  721. (int)(conctl->GetAverageRTT()*1000), (int)(conctl->GetMinimumRTT()*1000),
  722. int(conctl->GetInflightDataSize()), int(conctl->GetCongestionWindow()),
  723. keyFingerprint[0],keyFingerprint[1],keyFingerprint[2],keyFingerprint[3],keyFingerprint[4],keyFingerprint[5],keyFingerprint[6],keyFingerprint[7],
  724. lastSentSeq, lastRemoteAckSeq,
  725. conctl->GetSendLossCount(), recvLossCount, encoder ? encoder->GetPacketLoss() : 0,
  726. encoder ? (encoder->GetBitrate()/1000) : 0,
  727. (long long unsigned int)(stats.bytesSentMobile+stats.bytesSentWifi),
  728. (long long unsigned int)(stats.bytesRecvdMobile+stats.bytesRecvdWifi));
  729. MutexGuard m(participantsMutex);
  730. for(vector<GroupCallParticipant>::iterator p=participants.begin();p!=participants.end();++p){
  731. snprintf(buffer, sizeof(buffer), "Participant id: %d\n", p->userID);
  732. r+=buffer;
  733. for(vector<shared_ptr<Stream>>::iterator stm=p->streams.begin();stm!=p->streams.end();++stm){
  734. char* codec=reinterpret_cast<char*>(&(*stm)->codec);
  735. snprintf(buffer, sizeof(buffer), "Stream %d (type %d, codec '%c%c%c%c', %sabled)\n",
  736. (*stm)->id, (*stm)->type, codec[3], codec[2], codec[1], codec[0], (*stm)->enabled ? "en" : "dis");
  737. r+=buffer;
  738. if((*stm)->enabled){
  739. if((*stm)->jitterBuffer){
  740. snprintf(buffer, sizeof(buffer), "Jitter buffer: %d/%.2f\n",
  741. (*stm)->jitterBuffer->GetMinPacketCount(), (*stm)->jitterBuffer->GetAverageDelay());
  742. r+=buffer;
  743. }
  744. }
  745. }
  746. r+="\n";
  747. }
  748. return r;
  749. }