blob: f4189aae102d03c0f7b39ce9c8c89c5c4c9318a9 [file] [log] [blame]
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_
12#define MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000013
14#include <list>
kwiberg2d0c3322016-02-14 09:28:33 -080015#include <memory>
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000016
Patrik Höglund3e113432017-12-15 14:40:10 +010017#include "api/rtp_headers.h" // NOLINT(build/include)
Tommi25eb47c2019-08-29 16:39:05 +020018#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/constructor_magic.h"
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000020
21namespace webrtc {
22
Tommi25eb47c2019-08-29 16:39:05 +020023namespace RtpUtility {
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000024class RtpHeaderParser;
Tommi25eb47c2019-08-29 16:39:05 +020025} // namespace RtpUtility
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000026
27namespace test {
28
29// Class for handling RTP packets in test applications.
30class 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|.
Tommi25eb47c2019-08-29 16:39:05 +020038 // |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.org810acbc2014-04-14 18:42:23 +000043 Packet(uint8_t* packet_memory,
44 size_t allocated_bytes,
45 size_t virtual_packet_length_bytes,
46 double time_ms,
Tommi25eb47c2019-08-29 16:39:05 +020047 const RtpUtility::RtpHeaderParser& parser,
48 const RtpHeaderExtensionMap* extension_map = nullptr);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000049
Bjorn Terelius5350d1c2018-10-11 16:51:23 +020050 // 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.org810acbc2014-04-14 18:42:23 +000061 // 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
kwibergb8e56ee2016-08-29 06:37:33 -070071 virtual ~Packet();
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000072
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.org810acbc2014-04-14 18:42:23 +000099 double time_ms() const { return time_ms_; }
100 bool valid_header() const { return valid_header_; }
101
102 private:
Tommi25eb47c2019-08-29 16:39:05 +0200103 bool ParseHeader(const webrtc::RtpUtility::RtpHeaderParser& parser,
104 const RtpHeaderExtensionMap* extension_map);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000105 void CopyToHeader(RTPHeader* destination) const;
106
107 RTPHeader header_;
Tommi25eb47c2019-08-29 16:39:05 +0200108 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.org810acbc2014-04-14 18:42:23 +0000113 // Virtual lengths are used when parsing RTP header files (dummy RTP files).
114 const size_t virtual_packet_length_bytes_;
Tommi25eb47c2019-08-29 16:39:05 +0200115 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.org810acbc2014-04-14 18:42:23 +0000118
henrikg3c089d72015-09-16 05:37:44 -0700119 RTC_DISALLOW_COPY_AND_ASSIGN(Packet);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000120};
121
122} // namespace test
123} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200124#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_