2020-01-25 20:45:43 +01:00
|
|
|
#include "../PrivateDefines.cpp"
|
2020-03-21 21:33:51 +01:00
|
|
|
#include "packets/PacketManager.h"
|
2020-01-25 20:45:43 +01:00
|
|
|
|
|
|
|
using namespace tgvoip;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#pragma mark - Networking & crypto
|
|
|
|
|
2020-01-28 23:45:47 +01:00
|
|
|
PacketManager &VoIPController::getBestPacketManager()
|
|
|
|
{
|
2020-03-22 20:09:44 +01:00
|
|
|
return outgoingStreams[ver.isNew() ? StreamId::Audio : StreamId::Signaling]->packetManager;
|
2020-01-28 23:45:47 +01:00
|
|
|
}
|
|
|
|
|
2020-03-22 20:09:44 +01:00
|
|
|
void VoIPController::ProcessIncomingPacket(NetworkPacket &npacket, Endpoint &srcEndpoint)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
ENFORCE_MSG_THREAD;
|
|
|
|
|
|
|
|
// Initial packet decryption and recognition
|
|
|
|
|
2020-03-24 17:46:50 +01:00
|
|
|
unsigned char *buffer = **npacket.data;
|
2020-03-19 17:05:49 +01:00
|
|
|
//size_t len = npacket.data->Length();
|
2020-03-24 17:46:50 +01:00
|
|
|
BufferInputStream in(*npacket.data);
|
2020-03-22 20:09:44 +01:00
|
|
|
if (ver.peerVersion < 9 || srcEndpoint.IsReflector())
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
if (in.Remaining() < 16)
|
|
|
|
{
|
|
|
|
LOGW("Received packet has wrong (no) peerTag");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (memcmp(buffer, srcEndpoint.IsReflector() ? (void *)srcEndpoint.peerTag : (void *)callID, 16) != 0)
|
|
|
|
{
|
|
|
|
LOGW("Received packet has wrong peerTag");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
in.Seek(16);
|
|
|
|
}
|
2020-03-16 16:07:13 +01:00
|
|
|
if (in.Remaining() >= 16 && srcEndpoint.IsReflector() && parseRelayPacket(in, srcEndpoint))
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (in.Remaining() < 40)
|
|
|
|
{
|
|
|
|
LOGV("Received packet is too small");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t innerLen = decryptPacket(buffer, in);
|
2020-03-16 16:07:13 +01:00
|
|
|
if (!innerLen) // Decryption failed
|
|
|
|
{
|
2020-01-25 20:45:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastRecvPacketTime = GetCurrentTime();
|
|
|
|
|
|
|
|
if (state == STATE_RECONNECTING)
|
|
|
|
{
|
|
|
|
LOGI("Received a valid packet while reconnecting - setting state to established");
|
|
|
|
SetState(STATE_ESTABLISHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srcEndpoint.type == Endpoint::Type::UDP_P2P_INET && !srcEndpoint.IsIPv6Only())
|
|
|
|
{
|
2020-03-22 20:09:44 +01:00
|
|
|
if (srcEndpoint.port != npacket.port || srcEndpoint.address != npacket.address)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-22 20:09:44 +01:00
|
|
|
if (!npacket.address.isIPv6)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-22 20:09:44 +01:00
|
|
|
LOGI("Incoming packet was decrypted successfully, changing P2P endpoint to %s:%u", npacket.address.ToString().c_str(), npacket.port);
|
|
|
|
srcEndpoint.address = npacket.address;
|
|
|
|
srcEndpoint.port = npacket.port;
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:09:44 +01:00
|
|
|
Packet packet;
|
2020-03-24 15:07:56 +01:00
|
|
|
if (!packet.parse(in, ver))
|
|
|
|
{
|
2020-03-22 20:09:44 +01:00
|
|
|
LOGW("Failure parsing incoming packet!");
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
2020-03-24 15:07:56 +01:00
|
|
|
packetsReceived++;
|
2020-03-22 20:09:44 +01:00
|
|
|
ProcessIncomingPacket(packet, srcEndpoint);
|
|
|
|
|
2020-03-24 15:07:56 +01:00
|
|
|
if (packet.otherPackets.size())
|
|
|
|
{ // Legacy for PKT_STREAM_X2-3
|
|
|
|
for (Packet &packet : packet.otherPackets)
|
|
|
|
{
|
2020-03-22 20:09:44 +01:00
|
|
|
ProcessIncomingPacket(packet, srcEndpoint);
|
|
|
|
}
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
2020-03-22 20:09:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoIPController::ProcessIncomingPacket(Packet &packet, Endpoint &srcEndpoint)
|
|
|
|
{
|
2020-03-24 15:07:56 +01:00
|
|
|
// Use packet manager of outgoing stream
|
|
|
|
PacketManager &manager = outgoingStreams[packet.legacy ? StreamId::Signaling : packet.streamId]->packetManager;
|
2020-01-25 20:45:43 +01:00
|
|
|
|
2020-03-24 17:10:40 +01:00
|
|
|
if (!manager.ackRemoteSeq(packet))
|
2020-01-28 23:45:47 +01:00
|
|
|
{
|
2020-01-25 20:45:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-24 17:10:40 +01:00
|
|
|
#ifdef LOG_PACKETSackId
|
2020-01-29 15:52:43 +01:00
|
|
|
LOGV("Received: from=%s:%u, seq=%u, length=%u, type=%s, transportId=%hhu", srcEndpoint.GetAddress().ToString().c_str(), srcEndpoint.port, pseq, (unsigned int)packet.data.Length(), GetPacketTypeString(type).c_str(), transportId);
|
2020-01-29 19:12:12 +01:00
|
|
|
#endif
|
2020-01-29 15:52:43 +01:00
|
|
|
|
2020-03-24 17:10:40 +01:00
|
|
|
for (auto &extra : packet.extraSignaling)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
ProcessExtraData(extra, srcEndpoint);
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
2020-01-26 10:28:30 +01:00
|
|
|
|
2020-03-24 17:10:40 +01:00
|
|
|
auto &sender = outgoingStreams[manager.transportId]->packetSender;
|
|
|
|
if (packet.ackSeq && seqgt(packet.ackSeq, manager.getLastAckedSeq()))
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 15:07:56 +01:00
|
|
|
if (waitingForAcks && manager.getLastAckedSeq() >= firstSentPing)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
rttHistory.Reset();
|
|
|
|
waitingForAcks = false;
|
|
|
|
dontSendPackets = 10;
|
|
|
|
messageThread.Post(
|
|
|
|
[this] {
|
|
|
|
dontSendPackets = 0;
|
|
|
|
},
|
|
|
|
1.0);
|
|
|
|
LOGI("resuming sending");
|
|
|
|
}
|
2020-03-24 17:10:40 +01:00
|
|
|
conctl.PacketAcknowledged(CongestionControlPacket(packet));
|
|
|
|
manager.ackLocal(packet.ackSeq, packet.ackMask);
|
2020-01-25 20:45:43 +01:00
|
|
|
|
2020-03-24 15:07:56 +01:00
|
|
|
for (auto &opkt : manager.getRecentOutgoingPackets())
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 17:10:40 +01:00
|
|
|
if (!opkt.ackTime && manager.wasLocalAcked(opkt.pkt.seq))
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
opkt.ackTime = GetCurrentTime();
|
2020-01-25 21:31:26 +01:00
|
|
|
opkt.rttTime = opkt.ackTime - opkt.sendTime;
|
2020-01-25 20:45:43 +01:00
|
|
|
if (opkt.lost)
|
|
|
|
{
|
2020-03-24 17:10:40 +01:00
|
|
|
LOGW("acknowledged lost packet %u (streamId %hhu)", opkt.pkt.seq, packet.streamId);
|
2020-01-25 20:45:43 +01:00
|
|
|
sendLosses--;
|
|
|
|
}
|
2020-03-24 17:10:40 +01:00
|
|
|
else // Don't report lost packets as acked?
|
2020-01-29 15:52:43 +01:00
|
|
|
{
|
2020-03-24 17:10:40 +01:00
|
|
|
sender->PacketAcknowledged(opkt);
|
2020-01-29 15:52:43 +01:00
|
|
|
}
|
2020-01-25 20:45:43 +01:00
|
|
|
|
2020-03-24 17:10:40 +01:00
|
|
|
conctl.PacketAcknowledged(opkt.pkt);
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-24 17:10:40 +01:00
|
|
|
|
|
|
|
HandleReliablePackets(manager);
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Endpoint &_currentEndpoint = endpoints.at(currentEndpoint);
|
|
|
|
if (srcEndpoint.id != currentEndpoint && srcEndpoint.IsReflector() && (_currentEndpoint.IsP2P() || _currentEndpoint.averageRTT == 0))
|
|
|
|
{
|
2020-03-24 15:07:56 +01:00
|
|
|
if (seqgt(manager.getLastSentSeq() - 32, manager.getLastAckedSeq()))
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
currentEndpoint = srcEndpoint.id;
|
|
|
|
_currentEndpoint = srcEndpoint;
|
|
|
|
LOGI("Peer network address probably changed, switching to relay");
|
|
|
|
if (allowP2p)
|
|
|
|
SendPublicEndpointsRequest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 10:28:30 +01:00
|
|
|
/*
|
2020-01-25 20:45:43 +01:00
|
|
|
if (config.logPacketStats)
|
|
|
|
{
|
|
|
|
DebugLoggedPacket dpkt = {
|
|
|
|
static_cast<int32_t>(pseq),
|
|
|
|
GetCurrentTime() - connectionInitTime,
|
|
|
|
static_cast<int32_t>(packet.data.Length())};
|
|
|
|
debugLoggedPackets.push_back(dpkt);
|
|
|
|
if (debugLoggedPackets.size() >= 2500)
|
|
|
|
{
|
|
|
|
debugLoggedPackets.erase(debugLoggedPackets.begin(), debugLoggedPackets.begin() + 500);
|
|
|
|
}
|
2020-01-26 10:28:30 +01:00
|
|
|
}*/
|
2020-01-25 20:45:43 +01:00
|
|
|
|
2020-01-29 15:52:43 +01:00
|
|
|
if (unacknowledgedIncomingPacketCount++ > unackNopThreshold)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
//LOGV("Sending nop packet as ack");
|
2020-03-24 17:10:40 +01:00
|
|
|
SendNopPacket();
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:45:47 +01:00
|
|
|
//LOGV("acks: %u -> %.2lf, %.2lf, %.2lf, %.2lf, %.2lf, %.2lf, %.2lf, %.2lf", manager.getLastAckedSeq()(), remoteAcks[0], remoteAcks[1], remoteAcks[2], remoteAcks[3], remoteAcks[4], remoteAcks[5], remoteAcks[6], remoteAcks[7]);
|
2020-01-28 21:45:56 +01:00
|
|
|
//LOGD("recv: %u -> %.2lf, %.2lf, %.2lf, %.2lf, %.2lf, %.2lf, %.2lf, %.2lf", getLastRemoteSeq(), recvPacketTimes[0], recvPacketTimes[1], recvPacketTimes[2], recvPacketTimes[3], recvPacketTimes[4], recvPacketTimes[5], recvPacketTimes[6], recvPacketTimes[7]);
|
2020-01-25 20:45:43 +01:00
|
|
|
//LOGI("RTT = %.3lf", GetAverageRTT());
|
|
|
|
//LOGV("Packet %u type is %d", pseq, type);
|
2020-03-24 17:46:50 +01:00
|
|
|
if (packet.data)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
if (!receivedFirstStreamPacket)
|
|
|
|
{
|
|
|
|
receivedFirstStreamPacket = true;
|
|
|
|
if (state != STATE_ESTABLISHED && receivedInitAck)
|
|
|
|
{
|
|
|
|
messageThread.Post(
|
|
|
|
[this]() {
|
|
|
|
SetState(STATE_ESTABLISHED);
|
|
|
|
},
|
|
|
|
.5);
|
|
|
|
LOGW("First audio packet - setting state to ESTABLISHED");
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 18:51:45 +01:00
|
|
|
if (srcEndpoint.type == Endpoint::Type::UDP_RELAY && srcEndpoint.id != peerPreferredRelay)
|
|
|
|
{
|
|
|
|
peerPreferredRelay = srcEndpoint.id;
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:59:49 +01:00
|
|
|
//LOGE("RECV: For pts %u = seq %u, got seq %u", pts, pts/60 + 1, pseq);
|
|
|
|
|
|
|
|
//LOGD("stream data, pts=%d, len=%d, rem=%d", pts, sdlen, in.Remaining());
|
|
|
|
auto &_stm = GetStreamByID<IncomingStream>(packet.streamId);
|
|
|
|
if (!_stm)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
LOGW("received packet for unknown stream %u", (unsigned int)packet.streamId);
|
|
|
|
return;
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
2020-03-24 21:59:49 +01:00
|
|
|
if (!audioOutStarted && audioOutput)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
MutexGuard m(audioIOMutex);
|
|
|
|
audioOutput->Start();
|
|
|
|
audioOutStarted = true;
|
|
|
|
}
|
|
|
|
if (_stm->type == StreamType::Audio)
|
|
|
|
{
|
|
|
|
auto stm = dynamic_pointer_cast<IncomingAudioStream>(_stm);
|
|
|
|
uint32_t pts = packet.seq * stm->frameDuration;
|
|
|
|
if (stm->jitterBuffer)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
stm->jitterBuffer->HandleInput(std::move(packet.data), pts, false);
|
|
|
|
/*if (peerVersion >= PROTOCOL_RELIABLE)
|
2020-01-29 15:52:43 +01:00
|
|
|
{
|
|
|
|
// Technically I should be using the specific packet manager's rtt history but will separate later
|
|
|
|
uint32_t tooOldSeq = stm->jitterBuffer->GetSeqTooLate(rttHistory[0]) - 1;
|
2020-03-24 15:07:56 +01:00
|
|
|
LOGW("Reverse acking seqs older than %u, newest acked seq %u (transportId %hhu)", tooOldSeq, manager.getLastRemoteSeq(), transportId);
|
|
|
|
manager.ackRemoteSeqsOlderThan(tooOldSeq);
|
2020-01-29 15:52:43 +01:00
|
|
|
}*/
|
2020-03-24 21:59:49 +01:00
|
|
|
if (packet.extraEC)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
for (uint8_t i = 0; i < 8; i++)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
if (packet.extraEC.v[i])
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
stm->jitterBuffer->HandleInput(std::move(packet.extraEC.v[i].get<Bytes>().data), pts - (8 - i) * stm->frameDuration, true);
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 21:59:49 +01:00
|
|
|
else if (_stm->type == StreamType::Video)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
auto stm = dynamic_pointer_cast<IncomingVideoStream>(_stm);
|
|
|
|
if (stm->packetReassembler)
|
|
|
|
{ /*
|
|
|
|
uint8_t frameSeq = in.ReadByte();
|
|
|
|
Buffer pdata(sdlen);
|
|
|
|
uint16_t rotation = 0;
|
|
|
|
if (fragmentIndex == 0)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
unsigned char _rotation = in.ReadByte() & (unsigned char)VIDEO_ROTATION_MASK;
|
|
|
|
switch (_rotation)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
case VIDEO_ROTATION_0:
|
|
|
|
rotation = 0;
|
|
|
|
break;
|
|
|
|
case VIDEO_ROTATION_90:
|
|
|
|
rotation = 90;
|
|
|
|
break;
|
|
|
|
case VIDEO_ROTATION_180:
|
|
|
|
rotation = 180;
|
|
|
|
break;
|
|
|
|
case VIDEO_ROTATION_270:
|
|
|
|
rotation = 270;
|
|
|
|
break;
|
|
|
|
default: // unreachable on sane CPUs
|
|
|
|
abort();
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
2020-03-24 21:59:49 +01:00
|
|
|
//if(rotation!=stm->rotation){
|
|
|
|
// stm->rotation=rotation;
|
|
|
|
// LOGI("Video rotation: %u", rotation);
|
|
|
|
//}
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
2020-03-24 21:59:49 +01:00
|
|
|
pdata.CopyFrom(buffer + in.GetOffset(), 0, sdlen);
|
|
|
|
stm->packetReassembler->AddFragment(std::move(pdata), fragmentIndex, fragmentCount, pts, frameSeq, keyframe, rotation);*/
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
2020-03-24 21:59:49 +01:00
|
|
|
//LOGV("Received video fragment %u of %u", fragmentIndex, fragmentCount);
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:59:49 +01:00
|
|
|
void VoIPController::ProcessExtraData(const Wrapped<Extra> &_data, Endpoint &srcEndpoint)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 17:10:40 +01:00
|
|
|
auto type = _data.getID();
|
|
|
|
if (lastReceivedExtrasByType[type] == _data.d->hash)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-03-24 17:10:40 +01:00
|
|
|
lastReceivedExtrasByType[type] = _data.d->hash;
|
|
|
|
LOGE("ProcessExtraData=%s", _data.print().c_str());
|
|
|
|
|
2020-03-24 17:46:50 +01:00
|
|
|
if (type == ExtraInit::ID)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
2020-03-24 17:46:50 +01:00
|
|
|
auto &data = _data.get<ExtraInit>();
|
2020-03-24 17:10:40 +01:00
|
|
|
|
|
|
|
LOGD("Received init");
|
|
|
|
if (!receivedInit)
|
|
|
|
ver.peerVersion = data.peerVersion;
|
2020-03-24 17:46:50 +01:00
|
|
|
LOGI("Peer version is %d", ver.peerVersion);
|
|
|
|
|
|
|
|
if (data.minVersion > PROTOCOL_VERSION || data.minVersion < MIN_PROTOCOL_VERSION)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
|
|
|
lastError = ERROR_INCOMPATIBLE;
|
|
|
|
|
|
|
|
SetState(STATE_FAILED);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-24 17:46:50 +01:00
|
|
|
|
2020-03-24 17:10:40 +01:00
|
|
|
if (!receivedInit)
|
|
|
|
{
|
2020-03-24 17:46:50 +01:00
|
|
|
if (data.flags & ExtraInit::Flags::DataSavingEnabled)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
|
|
|
dataSavingRequestedByPeer = true;
|
|
|
|
UpdateDataSavingState();
|
|
|
|
UpdateAudioBitrateLimit();
|
|
|
|
}
|
2020-03-24 17:46:50 +01:00
|
|
|
if (data.flags & ExtraInit::Flags::GroupCallSupported)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
|
|
|
peerCapabilities |= TGVOIP_PEER_CAP_GROUP_CALLS;
|
|
|
|
}
|
2020-03-24 17:46:50 +01:00
|
|
|
if (data.flags & ExtraInit::Flags::VideoRecvSupported)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
|
|
|
peerCapabilities |= TGVOIP_PEER_CAP_VIDEO_DISPLAY;
|
|
|
|
}
|
2020-03-24 17:46:50 +01:00
|
|
|
if (data.flags & ExtraInit::Flags::VideoSendSupported)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
|
|
|
peerCapabilities |= TGVOIP_PEER_CAP_VIDEO_CAPTURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 17:46:50 +01:00
|
|
|
if (!receivedInit && ((data.flags & ExtraInit::Flags::VideoSendSupported && config.enableVideoReceive) || (data.flags & ExtraInit::Flags::VideoRecvSupported && config.enableVideoSend)))
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
|
|
|
LOGD("Peer video decoders:");
|
2020-03-24 21:59:49 +01:00
|
|
|
for (const auto &decoder : data.decoders)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
{
|
|
|
|
peerVideoDecoders.push_back(decoder);
|
|
|
|
LOGD("%c%c%c%c", PRINT_FOURCC(decoder.data))
|
|
|
|
}
|
|
|
|
ver.maxVideoResolution = data.maxResolution;
|
|
|
|
|
|
|
|
SetupOutgoingVideoStream();
|
2020-03-24 17:10:40 +01:00
|
|
|
}
|
|
|
|
|
2020-03-24 21:59:49 +01:00
|
|
|
auto ack = std::make_shared<ExtraInitAck>();
|
|
|
|
ack->peerVersion = PROTOCOL_VERSION;
|
|
|
|
ack->minVersion = MIN_PROTOCOL_VERSION;
|
2020-03-24 17:10:40 +01:00
|
|
|
|
2020-03-24 21:59:49 +01:00
|
|
|
for (const auto &stream : outgoingStreams)
|
|
|
|
{
|
|
|
|
ack->streams.v.push_back(*dynamic_pointer_cast<MediaStreamInfo>(stream));
|
|
|
|
}
|
2020-03-24 17:10:40 +01:00
|
|
|
|
2020-03-24 21:59:49 +01:00
|
|
|
LOGI("Sending init ack");
|
|
|
|
SendExtra(ack);
|
|
|
|
SendNopPacket();
|
2020-03-24 17:10:40 +01:00
|
|
|
|
2020-03-24 21:59:49 +01:00
|
|
|
if (!receivedInit)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
receivedInit = true;
|
|
|
|
if ((srcEndpoint.type == Endpoint::Type::UDP_RELAY && udpConnectivityState != UDP_BAD && udpConnectivityState != UDP_NOT_AVAILABLE) || srcEndpoint.type == Endpoint::Type::TCP_RELAY)
|
|
|
|
{
|
|
|
|
currentEndpoint = srcEndpoint.id;
|
|
|
|
if (srcEndpoint.type == Endpoint::Type::UDP_RELAY || (useTCP && srcEndpoint.type == Endpoint::Type::TCP_RELAY))
|
|
|
|
preferredRelay = srcEndpoint.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!audioStarted && receivedInitAck)
|
|
|
|
{
|
|
|
|
StartAudio();
|
|
|
|
audioStarted = true;
|
2020-03-24 17:10:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 21:59:49 +01:00
|
|
|
else if (type == ExtraInitAck::ID)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
auto &data = _data.get<ExtraInitAck>();
|
2020-03-24 17:10:40 +01:00
|
|
|
LOGD("Received init ack");
|
|
|
|
|
|
|
|
if (!receivedInitAck)
|
|
|
|
{
|
|
|
|
receivedInitAck = true;
|
|
|
|
|
|
|
|
messageThread.Cancel(initTimeoutID);
|
|
|
|
initTimeoutID = MessageThread::INVALID_ID;
|
|
|
|
|
2020-03-24 21:59:49 +01:00
|
|
|
ver.peerVersion = data.peerVersion;
|
|
|
|
if (data.minVersion > PROTOCOL_VERSION || data.peerVersion < MIN_PROTOCOL_VERSION)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
lastError = ERROR_INCOMPATIBLE;
|
2020-03-24 17:10:40 +01:00
|
|
|
|
2020-03-24 21:59:49 +01:00
|
|
|
SetState(STATE_FAILED);
|
|
|
|
return;
|
2020-03-24 17:10:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LOGI("peer version from init ack %d", ver.peerVersion);
|
|
|
|
|
2020-03-24 21:59:49 +01:00
|
|
|
shared_ptr<IncomingAudioStream> incomingAudioStream;
|
|
|
|
for (const auto &stm : data.streams)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
if (stm.type == StreamType::Video)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
if (ver.peerVersion < 9)
|
|
|
|
{
|
|
|
|
LOGV("Skipping video stream for old protocol version");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto vStm = std::make_shared<IncomingVideoStream>();
|
|
|
|
vStm->id = stm.streamId;
|
|
|
|
vStm->type = stm.type;
|
|
|
|
vStm->codec = stm.codec;
|
|
|
|
vStm->enabled = stm.enabled;
|
|
|
|
|
|
|
|
vStm->packetReassembler = std::make_shared<PacketReassembler>();
|
|
|
|
vStm->packetReassembler->SetCallback(bind(&VoIPController::ProcessIncomingVideoFrame, this, placeholders::_1, placeholders::_2, placeholders::_3, placeholders::_4));
|
|
|
|
|
|
|
|
incomingStreams.push_back(dynamic_pointer_cast<IncomingStream>(vStm));
|
2020-03-24 17:10:40 +01:00
|
|
|
}
|
2020-03-24 21:59:49 +01:00
|
|
|
else if (stm.type == StreamType::Audio)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
auto aStm = std::make_shared<IncomingAudioStream>();
|
|
|
|
aStm->id = stm.streamId;
|
|
|
|
aStm->type = stm.type;
|
|
|
|
aStm->codec = stm.codec;
|
|
|
|
aStm->enabled = stm.enabled;
|
|
|
|
aStm->frameDuration = 60;
|
|
|
|
|
|
|
|
aStm->jitterBuffer = make_shared<JitterBuffer>(aStm->frameDuration);
|
|
|
|
if (aStm->frameDuration > 50)
|
|
|
|
aStm->jitterBuffer->SetMinPacketCount(ServerConfig::GetSharedInstance()->GetUInt("jitter_initial_delay_60", 2));
|
|
|
|
else if (aStm->frameDuration > 30)
|
|
|
|
aStm->jitterBuffer->SetMinPacketCount(ServerConfig::GetSharedInstance()->GetUInt("jitter_initial_delay_40", 4));
|
2020-03-24 17:10:40 +01:00
|
|
|
else
|
2020-03-24 21:59:49 +01:00
|
|
|
aStm->jitterBuffer->SetMinPacketCount(ServerConfig::GetSharedInstance()->GetUInt("jitter_initial_delay_20", 6));
|
|
|
|
aStm->decoder = nullptr;
|
|
|
|
|
|
|
|
if (!incomingAudioStream)
|
|
|
|
incomingAudioStream = aStm;
|
|
|
|
|
|
|
|
incomingStreams.push_back(dynamic_pointer_cast<IncomingStream>(aStm));
|
2020-03-24 17:10:40 +01:00
|
|
|
}
|
2020-03-24 21:59:49 +01:00
|
|
|
else if (stm.type == StreamType::Signaling)
|
2020-03-24 17:10:40 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
auto sStm = std::make_shared<IncomingStream>();
|
|
|
|
sStm->id = stm.streamId;
|
|
|
|
sStm->type = stm.type;
|
|
|
|
sStm->enabled = stm.enabled;
|
|
|
|
incomingStreams.push_back(std::move(sStm));
|
2020-03-24 17:10:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
LOGW("Unknown incoming stream type: %hhu", stm.type);
|
2020-03-24 17:10:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!incomingAudioStream)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ver.peerVersion >= 5 && !useMTProto2)
|
|
|
|
{
|
|
|
|
useMTProto2 = true;
|
|
|
|
LOGD("MTProto2 wasn't initially enabled for whatever reason but peer supports it; upgrading");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!audioStarted && receivedInit)
|
|
|
|
{
|
|
|
|
StartAudio();
|
|
|
|
audioStarted = true;
|
|
|
|
}
|
|
|
|
messageThread.Post(
|
|
|
|
[this] {
|
|
|
|
if (state == STATE_WAIT_INIT_ACK)
|
|
|
|
{
|
|
|
|
SetState(STATE_ESTABLISHED);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ServerConfig::GetSharedInstance()->GetDouble("established_delay_if_no_stream_data", 1.5));
|
|
|
|
if (allowP2p)
|
|
|
|
SendPublicEndpointsRequest();
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 21:59:49 +01:00
|
|
|
else if (type == ExtraStreamFlags::ID)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
auto &data = _data.get<ExtraStreamFlags>();
|
|
|
|
LOGV("Peer stream state: id %u flags %u", (unsigned int)data.streamId, (unsigned int)data.flags);
|
|
|
|
for (auto &s : incomingStreams)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
if (s->id == data.streamId)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
bool prevEnabled = s->enabled;
|
|
|
|
bool prevPaused = s->paused;
|
2020-03-24 21:59:49 +01:00
|
|
|
s->enabled = data.flags & ExtraStreamFlags::Flags::Enabled;
|
|
|
|
s->paused = data.flags & ExtraStreamFlags::Flags::Paused;
|
|
|
|
if (s->type == StreamType::Audio)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
auto astm = dynamic_pointer_cast<IncomingAudioStream>(s);
|
|
|
|
if (data.flags & ExtraStreamFlags::Flags::ExtraEC)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
if (!astm->extraECEnabled)
|
|
|
|
{
|
|
|
|
astm->extraECEnabled = true;
|
|
|
|
if (astm->jitterBuffer)
|
|
|
|
astm->jitterBuffer->SetMinPacketCount(4);
|
|
|
|
}
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
2020-03-24 21:59:49 +01:00
|
|
|
else
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
if (astm->extraECEnabled)
|
|
|
|
{
|
|
|
|
astm->extraECEnabled = false;
|
|
|
|
if (astm->jitterBuffer)
|
|
|
|
astm->jitterBuffer->SetMinPacketCount(2);
|
|
|
|
}
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-24 15:07:56 +01:00
|
|
|
if (prevEnabled != s->enabled && s->type == StreamType::Video && videoRenderer)
|
2020-01-25 20:45:43 +01:00
|
|
|
videoRenderer->SetStreamEnabled(s->enabled);
|
2020-03-24 15:07:56 +01:00
|
|
|
if (prevPaused != s->paused && s->type == StreamType::Video && videoRenderer)
|
2020-01-25 20:45:43 +01:00
|
|
|
videoRenderer->SetStreamPaused(s->paused);
|
|
|
|
UpdateAudioOutputState();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-21 15:08:03 +01:00
|
|
|
else if (type == ExtraStreamCsd::ID)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
auto &data = _data.get<ExtraStreamCsd>();
|
2020-01-25 20:45:43 +01:00
|
|
|
LOGI("Received codec specific data");
|
2020-03-24 21:59:49 +01:00
|
|
|
auto &stm = GetStreamByID<IncomingVideoStream>(data.streamId);
|
|
|
|
if (stm)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
stm->codecSpecificData.clear();
|
|
|
|
stm->csdIsValid = false;
|
|
|
|
stm->width = data.width;
|
|
|
|
stm->height = data.height;
|
|
|
|
for (auto &v : data.data)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
stm->codecSpecificData.push_back(std::move(v.data));
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-21 15:08:03 +01:00
|
|
|
else if (type == ExtraLanEndpoint::ID)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
if (!allowP2p)
|
|
|
|
return;
|
2020-03-24 21:59:49 +01:00
|
|
|
auto &data = _data.get<ExtraLanEndpoint>();
|
2020-01-25 20:45:43 +01:00
|
|
|
LOGV("received lan endpoint (extra)");
|
2020-03-24 12:15:04 +01:00
|
|
|
if (currentEndpoint == Endpoint::ID::LANv4)
|
2020-01-25 20:45:43 +01:00
|
|
|
currentEndpoint = preferredRelay;
|
|
|
|
|
|
|
|
unsigned char peerTag[16];
|
2020-03-24 21:59:49 +01:00
|
|
|
Endpoint lan(Endpoint::ID::LANv4, data.port, data.address, NetworkAddress::Empty(), Endpoint::Type::UDP_P2P_LAN, peerTag);
|
2020-01-25 20:45:43 +01:00
|
|
|
MutexGuard m(endpointsMutex);
|
2020-03-24 12:15:04 +01:00
|
|
|
endpoints[Endpoint::ID::LANv4] = lan;
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
2020-03-21 15:08:03 +01:00
|
|
|
else if (type == ExtraNetworkChanged::ID)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
LOGI("Peer network changed");
|
2020-03-24 21:59:49 +01:00
|
|
|
auto &data = _data.get<ExtraNetworkChanged>();
|
2020-01-25 20:45:43 +01:00
|
|
|
wasNetworkHandover = true;
|
|
|
|
const Endpoint &_currentEndpoint = endpoints.at(currentEndpoint);
|
|
|
|
if (_currentEndpoint.type != Endpoint::Type::UDP_RELAY && _currentEndpoint.type != Endpoint::Type::TCP_RELAY)
|
|
|
|
currentEndpoint = preferredRelay;
|
|
|
|
if (allowP2p)
|
|
|
|
SendPublicEndpointsRequest();
|
2020-03-24 21:59:49 +01:00
|
|
|
|
|
|
|
if (ver.peerVersion < 2)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dataSavingRequestedByPeer = data.flags & ExtraInit::Flags::DataSavingEnabled;
|
2020-01-25 20:45:43 +01:00
|
|
|
UpdateDataSavingState();
|
|
|
|
UpdateAudioBitrateLimit();
|
|
|
|
ResetEndpointPingStats();
|
|
|
|
}
|
2020-03-21 15:08:03 +01:00
|
|
|
else if (type == ExtraGroupCallKey::ID)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
if (!didReceiveGroupCallKey && !didSendGroupCallKey)
|
|
|
|
{
|
2020-03-24 21:59:49 +01:00
|
|
|
auto &data = _data.get<ExtraGroupCallKey>();
|
|
|
|
messageThread.Post([this, &data] {
|
2020-01-25 20:45:43 +01:00
|
|
|
if (callbacks.groupCallKeyReceived)
|
2020-03-24 21:59:49 +01:00
|
|
|
callbacks.groupCallKeyReceived(this, *data.key);
|
2020-01-25 20:45:43 +01:00
|
|
|
});
|
|
|
|
didReceiveGroupCallKey = true;
|
|
|
|
}
|
|
|
|
}
|
2020-03-21 15:08:03 +01:00
|
|
|
else if (type == ExtraGroupCallUpgrade::ID)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
if (!didInvokeUpgradeCallback)
|
|
|
|
{
|
|
|
|
messageThread.Post([this] {
|
|
|
|
if (callbacks.upgradeToGroupCallRequested)
|
|
|
|
callbacks.upgradeToGroupCallRequested(this);
|
|
|
|
});
|
|
|
|
didInvokeUpgradeCallback = true;
|
|
|
|
}
|
|
|
|
}
|
2020-03-21 15:08:03 +01:00
|
|
|
else if (type == ExtraIpv6Endpoint::ID)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
if (!allowP2p)
|
|
|
|
return;
|
2020-03-24 21:59:49 +01:00
|
|
|
|
|
|
|
auto &data = _data.get<ExtraIpv6Endpoint>();
|
2020-01-25 20:45:43 +01:00
|
|
|
peerIPv6Available = true;
|
2020-03-24 21:59:49 +01:00
|
|
|
LOGV("Received peer IPv6 endpoint [%s]:%u", data.address.ToString().c_str(), data.port);
|
2020-01-25 20:45:43 +01:00
|
|
|
|
|
|
|
Endpoint ep;
|
|
|
|
ep.type = Endpoint::Type::UDP_P2P_INET;
|
2020-03-24 21:59:49 +01:00
|
|
|
ep.port = data.port;
|
|
|
|
ep.v6address = data.address;
|
2020-03-24 12:15:04 +01:00
|
|
|
ep.id = Endpoint::ID::P2Pv6;
|
|
|
|
endpoints[Endpoint::ID::P2Pv6] = ep;
|
2020-01-25 20:45:43 +01:00
|
|
|
if (!myIPv6.IsEmpty())
|
2020-03-24 12:15:04 +01:00
|
|
|
currentEndpoint = Endpoint::ID::P2Pv6;
|
2020-01-25 20:45:43 +01:00
|
|
|
}
|
2020-03-24 21:59:49 +01:00
|
|
|
else if (type == ExtraPing::ID)
|
|
|
|
{
|
|
|
|
//LOGD("Received ping from %s:%d", srcEndpoint.address.ToString().c_str(), srcEndpoint.port);
|
|
|
|
if (srcEndpoint.type != Endpoint::Type::UDP_RELAY && srcEndpoint.type != Endpoint::Type::TCP_RELAY && !allowP2p)
|
|
|
|
{
|
|
|
|
LOGW("Received p2p ping but p2p is disabled by manual override");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto pong = std::make_shared<ExtraPong>();
|
|
|
|
SendExtra(pong);
|
|
|
|
SendNopPacket();
|
|
|
|
}
|
|
|
|
else if (type == ExtraPong::ID)
|
|
|
|
{
|
|
|
|
auto &data = _data.get<ExtraPong>();
|
|
|
|
if (data.seq)
|
|
|
|
{
|
|
|
|
#ifdef LOG_PACKETS
|
|
|
|
LOGD("Received pong for ping in seq %u", data.seq);
|
|
|
|
#endif
|
|
|
|
if (data.seq == srcEndpoint.lastPingSeq)
|
|
|
|
{
|
|
|
|
srcEndpoint.rtts.Add(GetCurrentTime() - srcEndpoint.lastPingTime);
|
|
|
|
srcEndpoint.averageRTT = srcEndpoint.rtts.NonZeroAverage();
|
|
|
|
LOGD("Current RTT via %s: %.3f, average: %.3f", srcEndpoint.address.ToString().c_str(), srcEndpoint.rtts[0], srcEndpoint.averageRTT);
|
|
|
|
if (srcEndpoint.averageRTT > rateMaxAcceptableRTT)
|
|
|
|
needRate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
else if (type == PKT_STREAM_EC)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
shared_ptr<Stream> stm = GetStreamByID(streamID, false);
|
|
|
|
if (!stm)
|
|
|
|
{
|
|
|
|
LOGW("Received FEC packet for unknown stream %u", streamID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (stm->type != StreamType::Video)
|
|
|
|
{
|
|
|
|
LOGW("Received FEC packet for non-video stream %u", streamID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!stm->packetReassembler)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint8_t fseq = in.ReadByte();
|
|
|
|
unsigned char fecScheme = in.ReadByte();
|
|
|
|
unsigned char prevFrameCount = in.ReadByte();
|
|
|
|
uint16_t fecLen = in.ReadUInt16();
|
|
|
|
if (fecLen > in.Remaining())
|
|
|
|
return;
|
2020-01-25 20:45:43 +01:00
|
|
|
|
2020-03-24 21:59:49 +01:00
|
|
|
Buffer fecData(fecLen);
|
|
|
|
in.ReadBytes(fecData);
|
|
|
|
|
|
|
|
stm->packetReassembler->AddFEC(std::move(fecData), fseq, prevFrameCount, fecScheme);
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
}
|
2020-01-25 20:45:43 +01:00
|
|
|
void VoIPController::ProcessAcknowledgedOutgoingExtra(UnacknowledgedExtraData &extra)
|
|
|
|
{
|
2020-03-24 17:10:40 +01:00
|
|
|
if (extra.data.getID() == ExtraGroupCallKey::ID)
|
2020-01-25 20:45:43 +01:00
|
|
|
{
|
|
|
|
if (!didReceiveGroupCallKeyAck)
|
|
|
|
{
|
|
|
|
didReceiveGroupCallKeyAck = true;
|
|
|
|
messageThread.Post([this] {
|
|
|
|
if (callbacks.groupCallKeySent)
|
|
|
|
callbacks.groupCallKeySent(this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-03-19 17:05:49 +01:00
|
|
|
}
|