blob: 5748ba259049e75d914e9e9845d648a95eb38efc [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)
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/constructor_magic.h"
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000019
20namespace webrtc {
21
22class RtpHeaderParser;
23
24namespace test {
25
26// Class for handling RTP packets in test applications.
27class Packet {
28 public:
29 // Creates a packet, with the packet payload (including header bytes) in
30 // |packet_memory|. The length of |packet_memory| is |allocated_bytes|.
31 // The new object assumes ownership of |packet_memory| and will delete it
32 // when the Packet object is deleted. The |time_ms| is an extra time
33 // associated with this packet, typically used to denote arrival time.
34 // The first bytes in |packet_memory| will be parsed using |parser|.
35 Packet(uint8_t* packet_memory,
36 size_t allocated_bytes,
37 double time_ms,
38 const RtpHeaderParser& parser);
39
40 // Same as above, but with the extra argument |virtual_packet_length_bytes|.
41 // This is typically used when reading RTP dump files that only contain the
42 // RTP headers, and no payload (a.k.a RTP dummy files or RTP light). The
43 // |virtual_packet_length_bytes| tells what size the packet had on wire,
44 // including the now discarded payload, whereas |allocated_bytes| is the
45 // length of the remaining payload (typically only the RTP header).
46 Packet(uint8_t* packet_memory,
47 size_t allocated_bytes,
48 size_t virtual_packet_length_bytes,
49 double time_ms,
50 const RtpHeaderParser& parser);
51
Bjorn Terelius5350d1c2018-10-11 16:51:23 +020052 // Same as above, but creates the packet from an already parsed RTPHeader.
53 // This is typically used when reading RTP dump files that only contain the
54 // RTP headers, and no payload. The |virtual_packet_length_bytes| tells what
55 // size the packet had on wire, including the now discarded payload,
56 // The |virtual_payload_length_bytes| tells the size of the payload.
57 Packet(const RTPHeader& header,
58 size_t virtual_packet_length_bytes,
59 size_t virtual_payload_length_bytes,
60 double time_ms);
61
62 // The following constructors are the same as the first two, but without a
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000063 // parser. Note that when the object is constructed using any of these
64 // methods, the header will be parsed using a default RtpHeaderParser object.
65 // In particular, RTP header extensions won't be parsed.
66 Packet(uint8_t* packet_memory, size_t allocated_bytes, double time_ms);
67
68 Packet(uint8_t* packet_memory,
69 size_t allocated_bytes,
70 size_t virtual_packet_length_bytes,
71 double time_ms);
72
kwibergb8e56ee2016-08-29 06:37:33 -070073 virtual ~Packet();
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000074
75 // Parses the first bytes of the RTP payload, interpreting them as RED headers
76 // according to RFC 2198. The headers will be inserted into |headers|. The
77 // caller of the method assumes ownership of the objects in the list, and
78 // must delete them properly.
79 bool ExtractRedHeaders(std::list<RTPHeader*>* headers) const;
80
81 // Deletes all RTPHeader objects in |headers|, but does not delete |headers|
82 // itself.
83 static void DeleteRedHeaders(std::list<RTPHeader*>* headers);
84
85 const uint8_t* payload() const { return payload_; }
86
87 size_t packet_length_bytes() const { return packet_length_bytes_; }
88
89 size_t payload_length_bytes() const { return payload_length_bytes_; }
90
91 size_t virtual_packet_length_bytes() const {
92 return virtual_packet_length_bytes_;
93 }
94
95 size_t virtual_payload_length_bytes() const {
96 return virtual_payload_length_bytes_;
97 }
98
99 const RTPHeader& header() const { return header_; }
100
101 void set_time_ms(double time) { time_ms_ = time; }
102 double time_ms() const { return time_ms_; }
103 bool valid_header() const { return valid_header_; }
104
105 private:
106 bool ParseHeader(const RtpHeaderParser& parser);
107 void CopyToHeader(RTPHeader* destination) const;
108
109 RTPHeader header_;
kwiberg2d0c3322016-02-14 09:28:33 -0800110 std::unique_ptr<uint8_t[]> payload_memory_;
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000111 const uint8_t* payload_; // First byte after header.
112 const size_t packet_length_bytes_; // Total length of packet.
113 size_t payload_length_bytes_; // Length of the payload, after RTP header.
114 // Zero for dummy RTP packets.
115 // Virtual lengths are used when parsing RTP header files (dummy RTP files).
116 const size_t virtual_packet_length_bytes_;
117 size_t virtual_payload_length_bytes_;
118 double time_ms_; // Used to denote a packet's arrival time.
119 bool valid_header_; // Set by the RtpHeaderParser.
120
henrikg3c089d72015-09-16 05:37:44 -0700121 RTC_DISALLOW_COPY_AND_ASSIGN(Packet);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000122};
123
124} // namespace test
125} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200126#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_