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