PacketReassembler.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // Created by Grishka on 19.03.2018.
  3. //
  4. #ifndef TGVOIP_PACKETREASSEMBLER_H
  5. #define TGVOIP_PACKETREASSEMBLER_H
  6. #include <vector>
  7. #include <functional>
  8. #include <unordered_map>
  9. #include "Buffers.h"
  10. namespace tgvoip {
  11. class PacketReassembler{
  12. public:
  13. PacketReassembler();
  14. virtual ~PacketReassembler();
  15. void Reset();
  16. void AddFragment(Buffer pkt, unsigned int fragmentIndex, unsigned int fragmentCount, uint32_t pts, bool keyframe);
  17. void SetCallback(std::function<void(Buffer packet, uint32_t pts, bool keyframe)> callback);
  18. private:
  19. struct Packet{
  20. uint32_t timestamp;
  21. uint32_t partCount;
  22. uint32_t receivedPartCount;
  23. bool isKeyframe;
  24. Buffer* parts;
  25. TGVOIP_DISALLOW_COPY_AND_ASSIGN(Packet);
  26. Packet(Packet&& other) : timestamp(other.timestamp), partCount(other.partCount), receivedPartCount(other.receivedPartCount), isKeyframe(other.isKeyframe){
  27. parts=other.parts;
  28. other.parts=NULL;
  29. }
  30. Packet& operator=(Packet&& other){
  31. if(&other!=this){
  32. if(parts)
  33. delete[] parts;
  34. parts=other.parts;
  35. other.parts=NULL;
  36. timestamp=other.timestamp;
  37. partCount=other.partCount;
  38. receivedPartCount=other.receivedPartCount;
  39. isKeyframe=other.isKeyframe;
  40. }
  41. return *this;
  42. }
  43. Packet(uint32_t partCount) : partCount(partCount){
  44. parts=new Buffer[partCount];
  45. }
  46. ~Packet(){
  47. if(parts)
  48. delete[] parts;
  49. }
  50. void AddFragment(Buffer pkt, uint32_t fragmentIndex);
  51. Buffer Reassemble();
  52. };
  53. std::function<void(Buffer, uint32_t, bool)> callback;
  54. std::vector<Packet> packets;
  55. uint32_t maxTimestamp=0;
  56. };
  57. }
  58. #endif //TGVOIP_PACKETREASSEMBLER_H