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