packet.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_
  11. #define MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_
  12. #include <list>
  13. #include <memory>
  14. #include "api/rtp_headers.h" // NOLINT(build/include)
  15. #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
  16. #include "rtc_base/constructor_magic.h"
  17. namespace webrtc {
  18. namespace RtpUtility {
  19. class RtpHeaderParser;
  20. } // namespace RtpUtility
  21. namespace test {
  22. // Class for handling RTP packets in test applications.
  23. class Packet {
  24. public:
  25. // Creates a packet, with the packet payload (including header bytes) in
  26. // |packet_memory|. The length of |packet_memory| is |allocated_bytes|.
  27. // The new object assumes ownership of |packet_memory| and will delete it
  28. // when the Packet object is deleted. The |time_ms| is an extra time
  29. // associated with this packet, typically used to denote arrival time.
  30. // The first bytes in |packet_memory| will be parsed using |parser|.
  31. // |virtual_packet_length_bytes| is typically used when reading RTP dump files
  32. // that only contain the RTP headers, and no payload (a.k.a RTP dummy files or
  33. // RTP light). The |virtual_packet_length_bytes| tells what size the packet
  34. // had on wire, including the now discarded payload, whereas |allocated_bytes|
  35. // is the length of the remaining payload (typically only the RTP header).
  36. Packet(uint8_t* packet_memory,
  37. size_t allocated_bytes,
  38. size_t virtual_packet_length_bytes,
  39. double time_ms,
  40. const RtpUtility::RtpHeaderParser& parser,
  41. const RtpHeaderExtensionMap* extension_map = nullptr);
  42. // Same as above, but creates the packet from an already parsed RTPHeader.
  43. // This is typically used when reading RTP dump files that only contain the
  44. // RTP headers, and no payload. The |virtual_packet_length_bytes| tells what
  45. // size the packet had on wire, including the now discarded payload,
  46. // The |virtual_payload_length_bytes| tells the size of the payload.
  47. Packet(const RTPHeader& header,
  48. size_t virtual_packet_length_bytes,
  49. size_t virtual_payload_length_bytes,
  50. double time_ms);
  51. // The following constructors are the same as the first two, but without a
  52. // parser. Note that when the object is constructed using any of these
  53. // methods, the header will be parsed using a default RtpHeaderParser object.
  54. // In particular, RTP header extensions won't be parsed.
  55. Packet(uint8_t* packet_memory, size_t allocated_bytes, double time_ms);
  56. Packet(uint8_t* packet_memory,
  57. size_t allocated_bytes,
  58. size_t virtual_packet_length_bytes,
  59. double time_ms);
  60. virtual ~Packet();
  61. // Parses the first bytes of the RTP payload, interpreting them as RED headers
  62. // according to RFC 2198. The headers will be inserted into |headers|. The
  63. // caller of the method assumes ownership of the objects in the list, and
  64. // must delete them properly.
  65. bool ExtractRedHeaders(std::list<RTPHeader*>* headers) const;
  66. // Deletes all RTPHeader objects in |headers|, but does not delete |headers|
  67. // itself.
  68. static void DeleteRedHeaders(std::list<RTPHeader*>* headers);
  69. const uint8_t* payload() const { return payload_; }
  70. size_t packet_length_bytes() const { return packet_length_bytes_; }
  71. size_t payload_length_bytes() const { return payload_length_bytes_; }
  72. size_t virtual_packet_length_bytes() const {
  73. return virtual_packet_length_bytes_;
  74. }
  75. size_t virtual_payload_length_bytes() const {
  76. return virtual_payload_length_bytes_;
  77. }
  78. const RTPHeader& header() const { return header_; }
  79. double time_ms() const { return time_ms_; }
  80. bool valid_header() const { return valid_header_; }
  81. private:
  82. bool ParseHeader(const webrtc::RtpUtility::RtpHeaderParser& parser,
  83. const RtpHeaderExtensionMap* extension_map);
  84. void CopyToHeader(RTPHeader* destination) const;
  85. RTPHeader header_;
  86. const std::unique_ptr<uint8_t[]> payload_memory_;
  87. const uint8_t* payload_ = nullptr; // First byte after header.
  88. const size_t packet_length_bytes_ = 0; // Total length of packet.
  89. size_t payload_length_bytes_ = 0; // Length of the payload, after RTP header.
  90. // Zero for dummy RTP packets.
  91. // Virtual lengths are used when parsing RTP header files (dummy RTP files).
  92. const size_t virtual_packet_length_bytes_;
  93. size_t virtual_payload_length_bytes_ = 0;
  94. const double time_ms_; // Used to denote a packet's arrival time.
  95. const bool valid_header_; // Set by the RtpHeaderParser.
  96. RTC_DISALLOW_COPY_AND_ASSIGN(Packet);
  97. };
  98. } // namespace test
  99. } // namespace webrtc
  100. #endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_