1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-11-29 20:29:01 +01:00
This commit is contained in:
Daniil Gentili 2020-03-27 19:13:37 +01:00
parent 457576e93f
commit b8da2077ba
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
10 changed files with 112 additions and 26 deletions

View File

@ -36,6 +36,22 @@ void AudioPacketSender::SendFrame(unsigned char *data, size_t len, unsigned char
{
ecAudioPackets.pop_front();
}
if (shittyInternetMode)
{
uint8_t maxEC = std::min(
std::min(
static_cast<uint8_t>(ecAudioPackets.size()),
static_cast<uint8_t>(pkt->seq - 1)),
extraEcLevel);
uint8_t offset = 8 - maxEC;
for (uint8_t i = 0; i < maxEC; i++)
{
if (!packetManager.wasLocalAcked(pkt->seq - (i + 1)))
{
pkt->extraEC.v[offset + i].d = std::make_shared<InputBytes>(ecAudioPackets[i]);
}
}
}
/*
uint8_t fecCount = std::min(static_cast<uint8_t>(ecAudioPackets.size()), extraEcLevel);
pkt.WriteByte(fecCount);

View File

@ -49,6 +49,7 @@ VoIPController::VoIPController() : rawSendQueue(64)
void VoIPController::InitializeTimers()
{
LOGE("Init timeout is %lf", config.initTimeout);
initTimeoutID = messageThread.Post(
[this] {
LOGW("Init timeout, disconnecting");

View File

@ -78,17 +78,24 @@ void VoIPController::ProcessIncomingPacket(NetworkPacket &npacket, Endpoint &src
LOGW("Failure parsing incoming packet! %s", ver.isNew() ? "(new mode)" : ver.isLegacy() ? "(legacy mode)" : "(legacylegacy mode)");
packet.clear();
in.Seek(offset);
VersionInfo ver(9, 92);
VersionInfo ver(10, 110);
if (!packet.parse(in, ver))
{
LOGW("Failure parsing incoming packet! %s", ver.isNew() ? "(new mode)" : ver.isLegacy() ? "(legacy mode)" : "(legacylegacy mode)");
packet.clear();
in.Seek(offset);
VersionInfo ver(7, 91);
VersionInfo ver(9, 92);
if (!packet.parse(in, ver))
{
LOGW("Failure parsing incoming packet! %s", ver.isNew() ? "(new mode)" : ver.isLegacy() ? "(legacy mode)" : "(legacylegacy mode)");
return;
packet.clear();
in.Seek(offset);
VersionInfo ver(7, 91);
if (!packet.parse(in, ver))
{
LOGW("Failure parsing incoming packet! %s", ver.isNew() ? "(new mode)" : ver.isLegacy() ? "(legacy mode)" : "(legacylegacy mode)");
return;
}
}
}
}
@ -201,7 +208,7 @@ void VoIPController::ProcessIncomingPacket(Packet &packet, Endpoint &srcEndpoint
//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]);
//LOGI("RTT = %.3lf", GetAverageRTT());
//LOGV("Packet %u type is %d", pseq, type);
if (packet.data && packet.data->Length())
if (packet.data)
{
if (!receivedFirstStreamPacket)
{
@ -249,7 +256,7 @@ void VoIPController::ProcessIncomingPacket(Packet &packet, Endpoint &srcEndpoint
{
if (packet.extraEC.v[i])
{
stm->jitterBuffer->HandleInput(std::move(packet.extraEC.v[i].get<Bytes>().data), pts - (8 - i) * stm->frameDuration, true);
stm->jitterBuffer->HandleInput(std::move(packet.extraEC.v[i].get<OutputBytes>().data), pts - (8 - i) * stm->frameDuration, true);
}
}
}
@ -400,8 +407,10 @@ void VoIPController::ProcessExtraData(const Wrapped<Extra> &_data, Endpoint &src
{
receivedInitAck = true;
LOGE("Canceling init timeout");
messageThread.Cancel(initTimeoutID);
initTimeoutID = MessageThread::INVALID_ID;
LOGE("Canceled init timeout");
ver.peerVersion = data.peerVersion;
if (data.minVersion > PROTOCOL_VERSION || data.peerVersion < MIN_PROTOCOL_VERSION)
@ -547,7 +556,7 @@ void VoIPController::ProcessExtraData(const Wrapped<Extra> &_data, Endpoint &src
stm->height = data.height;
for (auto &v : data.data)
{
stm->codecSpecificData.push_back(std::move(*v.data));
stm->codecSpecificData.push_back(std::move(*v.get<OutputBytes>().data));
}
}
}

View File

@ -142,6 +142,9 @@ bool Packet::parseLegacy(const BufferInputStream &in, const VersionInfo &ver)
}
}
}
} else {
LOGW("Got unknown legacy packet type %hhu (probably new packet)", type);
return false;
}
return true;
}

View File

@ -11,7 +11,7 @@ bool Packet::parse(const BufferInputStream &in, const VersionInfo &ver)
return parseLegacy(in, ver);
}
uint16_t length;
uint16_t length = 0;
uint8_t flags;
bool res = in.TryRead(seq) &&
in.TryRead(ackSeq) &&
@ -23,6 +23,11 @@ bool Packet::parse(const BufferInputStream &in, const VersionInfo &ver)
streamId = flags & 3;
flags >>= 2;
if (!(flags & Flags::Reserved))
{
LOGW("No reserved flag in new flag, probably legacy packet");
return false;
}
if (streamId == StreamId::Extended && !in.TryRead(streamId))
return false;
if (!(flags & Flags::Len16 ? in.TryRead(length) : in.TryReadCompat<uint8_t>(length)))
@ -45,7 +50,7 @@ bool Packet::parse(const BufferInputStream &in, const VersionInfo &ver)
if (!in.TryRead(*data))
return false;
}
if ((flags & Flags::RecvTS) && !in.TryRead(recvTS))
return false;
if ((flags & Flags::ExtraEC) && !in.TryRead(extraEC, ver))
@ -59,7 +64,7 @@ bool Packet::parse(const BufferInputStream &in, const VersionInfo &ver)
void Packet::serialize(BufferOutputStream &out, const VersionInfo &ver) const
{
uint8_t shortStreamId = streamId > StreamId::Extended ? StreamId::Extended : streamId;
uint8_t flags = 0;
uint8_t flags = Flags::Reserved; // Always true, allows us to distinguish from legacy packets if the maxLayer is wrong.
size_t length = data ? data->Length() : 0;
if (length > 0xFF || eFlags)

View File

@ -92,7 +92,8 @@ public:
Len16 = 1,
RecvTS = 2,
ExtraEC = 4,
ExtraSignaling = 8
ExtraSignaling = 8,
Reserved = 16
};
enum EFlags : uint8_t
{

View File

@ -33,7 +33,7 @@ std::shared_ptr<Extra> Extra::choose(const BufferInputStream &in, const VersionI
return nullptr;
}
unsigned char fullHash[SHA1_LENGTH];
VoIPController::crypto.sha1(const_cast<uint8_t *>(in.GetRawBuffer()), in.Remaining(), fullHash);
VoIPController::crypto.sha1(const_cast<uint8_t *>(in.GetRawBuffer() - in.GetOffset()), in.GetLength(), fullHash);
#ifdef LOG_PACKETS
LOGE("Got extra ID %hhu", id);

View File

@ -146,7 +146,7 @@ public:
uint16_t width = 0;
uint16_t height = 0;
Array<Bytes> data;
Array<Wrapped<Bytes>> data;
uint8_t getID() const
{

View File

@ -96,7 +96,7 @@ struct Mask : public Serializable, SingleChoice<Mask<T>>
}
for (uint8_t i = 0; i < 8; i++)
{
if (!(i & (1 << i)))
if (!(mask & (1 << i)))
continue;
if (!in.TryRead(v.at(i), ver))
@ -186,9 +186,7 @@ struct Array : public Serializable, SingleChoice<Array<T>>
{
auto data = T::choose(in, ver);
if (!data || !data->parse(in, ver))
{
return false;
}
v.push_back(std::move(*data));
}
return true;
@ -258,13 +256,17 @@ struct Wrapped : public Serializable, SingleChoice<Wrapped<T>>
uint8_t len;
if (!in.TryRead(len))
return false;
if (in.Remaining() < len)
return false;
#ifdef LOG_PACKETS
LOGW("Got buffer of length %hhu", len);
#endif
auto buf = in.GetPartBuffer(len);
d = T::choose(buf, ver);
if (!d)
{
return false;
}
return d->parse(buf, ver);
}
void serialize(BufferOutputStream &out, const VersionInfo &ver) const override
@ -326,25 +328,25 @@ struct Wrapped : public Serializable, SingleChoice<Wrapped<T>>
std::shared_ptr<T> d;
};
class OutputBytes;
struct Bytes : public Serializable,
SingleChoice<Bytes>
SingleChoice<OutputBytes>
{
virtual ~Bytes() = default;
Bytes() = default;
Bytes(Buffer &&_data) : data(std::make_unique<Buffer>(std::move(_data))){};
Bytes(Bytes &&_data) : data(std::move(_data.data)){};
bool parse(const BufferInputStream &in, const VersionInfo &ver)
{
data = std::make_unique<Buffer>(in.GetLength());
return in.TryRead(*data);
setData(std::make_unique<Buffer>(in.GetLength()));
return in.TryRead(*getData());
}
void serialize(BufferOutputStream &out, const VersionInfo &ver) const override
{
out.WriteBytes(*data);
out.WriteBytes(*getData());
}
operator bool()
{
return data && !data->IsEmpty();
return getData() && !getData()->IsEmpty();
}
std::string print() const override
{
@ -352,12 +354,60 @@ struct Bytes : public Serializable,
}
size_t getSize(const VersionInfo &ver) const override
{
return data->Length();
return getData()->Length();
}
std::unique_ptr<Buffer> data;
virtual Buffer *getData() = 0;
virtual const Buffer *getData() const = 0;
virtual void setData(std::unique_ptr<Buffer> &&) = 0;
};
struct UInt32 : public Serializable, SingleChoice<UInt32>
struct OutputBytes : public Bytes
{
virtual ~OutputBytes() = default;
OutputBytes() = default;
OutputBytes(Buffer &&_data) : data(std::make_unique<Buffer>(std::move(_data))){};
virtual Buffer *getData()
{
return data.get();
}
virtual const Buffer *getData() const
{
return data.get();
}
virtual void setData(std::unique_ptr<Buffer> &&buf)
{
data = std::move(buf);
}
std::unique_ptr<Buffer> data;
};
struct InputBytes : public Bytes
{
virtual ~InputBytes() = default;
InputBytes() = default;
InputBytes(std::shared_ptr<Buffer> _data) : data(_data){};
virtual Buffer *getData()
{
return data.get();
}
virtual const Buffer *getData() const
{
return data.get();
}
virtual void setData(std::unique_ptr<Buffer> &&buf)
{
data = std::move(buf);
}
std::shared_ptr<Buffer> data;
};
struct UInt32 : public Serializable,
SingleChoice<UInt32>
{
virtual ~UInt32() = default;
UInt32() = default;

View File

@ -25,7 +25,8 @@ packet$_
recvTS:flags.1?int
extraFEC:flags.2?ExtraFEC
extraSignaling:flags.3?Extras<ExtraSignaling>
reserved:flags.4?true:true // Always true, allows us to distinguish from legacy packets if the maxLayer is wrong.
= Packet;
// ExtraFEC