henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_ |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 15 | #include <memory> |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 16 | |
Patrik Höglund | 3e11343 | 2017-12-15 14:40:10 +0100 | [diff] [blame] | 17 | #include "api/rtp_headers.h" // NOLINT(build/include) |
Tommi | 25eb47c | 2019-08-29 16:39:05 +0200 | [diff] [blame] | 18 | #include "modules/rtp_rtcp/include/rtp_header_extension_map.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/constructor_magic.h" |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
Tommi | 25eb47c | 2019-08-29 16:39:05 +0200 | [diff] [blame] | 23 | namespace RtpUtility { |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 24 | class RtpHeaderParser; |
Tommi | 25eb47c | 2019-08-29 16:39:05 +0200 | [diff] [blame] | 25 | } // namespace RtpUtility |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 26 | |
| 27 | namespace test { |
| 28 | |
| 29 | // Class for handling RTP packets in test applications. |
| 30 | class Packet { |
| 31 | public: |
| 32 | // Creates a packet, with the packet payload (including header bytes) in |
| 33 | // |packet_memory|. The length of |packet_memory| is |allocated_bytes|. |
| 34 | // The new object assumes ownership of |packet_memory| and will delete it |
| 35 | // when the Packet object is deleted. The |time_ms| is an extra time |
| 36 | // associated with this packet, typically used to denote arrival time. |
| 37 | // The first bytes in |packet_memory| will be parsed using |parser|. |
Tommi | 25eb47c | 2019-08-29 16:39:05 +0200 | [diff] [blame] | 38 | // |virtual_packet_length_bytes| is typically used when reading RTP dump files |
| 39 | // that only contain the RTP headers, and no payload (a.k.a RTP dummy files or |
| 40 | // RTP light). The |virtual_packet_length_bytes| tells what size the packet |
| 41 | // had on wire, including the now discarded payload, whereas |allocated_bytes| |
| 42 | // is the length of the remaining payload (typically only the RTP header). |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 43 | Packet(uint8_t* packet_memory, |
| 44 | size_t allocated_bytes, |
| 45 | size_t virtual_packet_length_bytes, |
| 46 | double time_ms, |
Tommi | 25eb47c | 2019-08-29 16:39:05 +0200 | [diff] [blame] | 47 | const RtpUtility::RtpHeaderParser& parser, |
| 48 | const RtpHeaderExtensionMap* extension_map = nullptr); |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 49 | |
Bjorn Terelius | 5350d1c | 2018-10-11 16:51:23 +0200 | [diff] [blame] | 50 | // Same as above, but creates the packet from an already parsed RTPHeader. |
| 51 | // This is typically used when reading RTP dump files that only contain the |
| 52 | // RTP headers, and no payload. The |virtual_packet_length_bytes| tells what |
| 53 | // size the packet had on wire, including the now discarded payload, |
| 54 | // The |virtual_payload_length_bytes| tells the size of the payload. |
| 55 | Packet(const RTPHeader& header, |
| 56 | size_t virtual_packet_length_bytes, |
| 57 | size_t virtual_payload_length_bytes, |
| 58 | double time_ms); |
| 59 | |
| 60 | // The following constructors are the same as the first two, but without a |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 61 | // parser. Note that when the object is constructed using any of these |
| 62 | // methods, the header will be parsed using a default RtpHeaderParser object. |
| 63 | // In particular, RTP header extensions won't be parsed. |
| 64 | Packet(uint8_t* packet_memory, size_t allocated_bytes, double time_ms); |
| 65 | |
| 66 | Packet(uint8_t* packet_memory, |
| 67 | size_t allocated_bytes, |
| 68 | size_t virtual_packet_length_bytes, |
| 69 | double time_ms); |
| 70 | |
kwiberg | b8e56ee | 2016-08-29 06:37:33 -0700 | [diff] [blame] | 71 | virtual ~Packet(); |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 72 | |
| 73 | // Parses the first bytes of the RTP payload, interpreting them as RED headers |
| 74 | // according to RFC 2198. The headers will be inserted into |headers|. The |
| 75 | // caller of the method assumes ownership of the objects in the list, and |
| 76 | // must delete them properly. |
| 77 | bool ExtractRedHeaders(std::list<RTPHeader*>* headers) const; |
| 78 | |
| 79 | // Deletes all RTPHeader objects in |headers|, but does not delete |headers| |
| 80 | // itself. |
| 81 | static void DeleteRedHeaders(std::list<RTPHeader*>* headers); |
| 82 | |
| 83 | const uint8_t* payload() const { return payload_; } |
| 84 | |
| 85 | size_t packet_length_bytes() const { return packet_length_bytes_; } |
| 86 | |
| 87 | size_t payload_length_bytes() const { return payload_length_bytes_; } |
| 88 | |
| 89 | size_t virtual_packet_length_bytes() const { |
| 90 | return virtual_packet_length_bytes_; |
| 91 | } |
| 92 | |
| 93 | size_t virtual_payload_length_bytes() const { |
| 94 | return virtual_payload_length_bytes_; |
| 95 | } |
| 96 | |
| 97 | const RTPHeader& header() const { return header_; } |
| 98 | |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 99 | double time_ms() const { return time_ms_; } |
| 100 | bool valid_header() const { return valid_header_; } |
| 101 | |
| 102 | private: |
Tommi | 25eb47c | 2019-08-29 16:39:05 +0200 | [diff] [blame] | 103 | bool ParseHeader(const webrtc::RtpUtility::RtpHeaderParser& parser, |
| 104 | const RtpHeaderExtensionMap* extension_map); |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 105 | void CopyToHeader(RTPHeader* destination) const; |
| 106 | |
| 107 | RTPHeader header_; |
Tommi | 25eb47c | 2019-08-29 16:39:05 +0200 | [diff] [blame] | 108 | const std::unique_ptr<uint8_t[]> payload_memory_; |
| 109 | const uint8_t* payload_ = nullptr; // First byte after header. |
| 110 | const size_t packet_length_bytes_ = 0; // Total length of packet. |
| 111 | size_t payload_length_bytes_ = 0; // Length of the payload, after RTP header. |
| 112 | // Zero for dummy RTP packets. |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 113 | // Virtual lengths are used when parsing RTP header files (dummy RTP files). |
| 114 | const size_t virtual_packet_length_bytes_; |
Tommi | 25eb47c | 2019-08-29 16:39:05 +0200 | [diff] [blame] | 115 | size_t virtual_payload_length_bytes_ = 0; |
| 116 | const double time_ms_; // Used to denote a packet's arrival time. |
| 117 | const bool valid_header_; // Set by the RtpHeaderParser. |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 118 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 119 | RTC_DISALLOW_COPY_AND_ASSIGN(Packet); |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace test |
| 123 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 124 | #endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_ |