1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-11-30 04:39:03 +01:00
libtgvoip/PacketReassembler.cpp

75 lines
1.7 KiB
C++
Raw Normal View History

2018-06-04 21:37:43 +02:00
//
// Created by Grishka on 19.03.2018.
//
#include "PacketReassembler.h"
#include "logging.h"
#include <assert.h>
2018-06-04 21:37:43 +02:00
using namespace tgvoip;
PacketReassembler::PacketReassembler(){
}
PacketReassembler::~PacketReassembler(){
}
void PacketReassembler::Reset(){
}
2019-02-05 12:41:00 +01:00
void PacketReassembler::AddFragment(Buffer pkt, unsigned int fragmentIndex, unsigned int fragmentCount, uint32_t pts, bool keyframe){
//LOGD("add fragment: ts=%u, index=%u of %u", pts, fragmentIndex, fragmentCount);
if(pts!=currentTimestamp){
assert(fragmentCount<=255);
currentTimestamp=pts;
2019-02-05 12:41:00 +01:00
/*for(Buffer& b:parts){
if(!b.IsEmpty())
b=Buffer();
}*/
currentPacketPartCount=fragmentCount;
2019-02-05 12:41:00 +01:00
currentIsKeyframe=keyframe;
receivedPartCount=0;
}
if(fragmentCount==0 || fragmentCount==1){
callback(std::move(pkt), pts, keyframe);
return;
}
if(fragmentIndex<currentPacketPartCount){
parts[fragmentIndex]=std::move(pkt);
}
if(parts.size()<currentPacketPartCount)
return;
2019-02-05 12:41:00 +01:00
receivedPartCount++;
bool anyAreEmpty=false;
2019-02-05 12:41:00 +01:00
for(int i=0;i<currentPacketPartCount;i++){
if(parts[i].IsEmpty()){
anyAreEmpty=true;
break;
}
}
2019-02-05 12:41:00 +01:00
if(!anyAreEmpty && receivedPartCount==currentPacketPartCount){
//LOGI("part count %u", (unsigned int)parts.size());
BufferOutputStream out(10240);
2019-02-05 12:41:00 +01:00
for(int i=0;i<currentPacketPartCount;i++){
out.WriteBytes(parts[i]);
parts[i]=Buffer();
}
2019-02-05 12:41:00 +01:00
//LOGI("reassembled %u parts, %u bytes", (unsigned int)receivedPartCount, out.GetLength());
callback(Buffer(std::move(out)), currentTimestamp, currentIsKeyframe);
currentPacketPartCount=0;
currentTimestamp=0;
}
}
2018-06-04 21:37:43 +02:00
2019-02-05 12:41:00 +01:00
void PacketReassembler::SetCallback(std::function<void(Buffer packet, uint32_t pts, bool keyframe)> callback){
this->callback=callback;
2018-06-04 21:37:43 +02:00
}