blob: ef118d9f0b63b3b8103939acb20664ca08e5c1c7 [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>
15
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020016#include "api/array_view.h"
17#include "api/rtp_headers.h"
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"
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020020#include "rtc_base/copy_on_write_buffer.h"
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000021
22namespace webrtc {
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000023namespace test {
24
25// Class for handling RTP packets in test applications.
26class Packet {
27 public:
28 // Creates a packet, with the packet payload (including header bytes) in
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020029 // `packet`. The `time_ms` is an extra time associated with this packet,
30 // typically used to denote arrival time.
31 // `virtual_packet_length_bytes` is typically used when reading RTP dump files
Tommi25eb47c2019-08-29 16:39:05 +020032 // that only contain the RTP headers, and no payload (a.k.a RTP dummy files or
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020033 // RTP light). The `virtual_packet_length_bytes` tells what size the packet
34 // had on wire, including the now discarded payload.
35 Packet(rtc::CopyOnWriteBuffer packet,
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000036 size_t virtual_packet_length_bytes,
37 double time_ms,
Tommi25eb47c2019-08-29 16:39:05 +020038 const RtpHeaderExtensionMap* extension_map = nullptr);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000039
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020040 Packet(rtc::CopyOnWriteBuffer packet,
41 double time_ms,
42 const RtpHeaderExtensionMap* extension_map = nullptr)
43 : Packet(packet, packet.size(), time_ms, extension_map) {}
44
Bjorn Terelius5350d1c2018-10-11 16:51:23 +020045 // Same as above, but creates the packet from an already parsed RTPHeader.
46 // This is typically used when reading RTP dump files that only contain the
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020047 // RTP headers, and no payload. The `virtual_packet_length_bytes` tells what
Bjorn Terelius5350d1c2018-10-11 16:51:23 +020048 // size the packet had on wire, including the now discarded payload,
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020049 // The `virtual_payload_length_bytes` tells the size of the payload.
Bjorn Terelius5350d1c2018-10-11 16:51:23 +020050 Packet(const RTPHeader& header,
51 size_t virtual_packet_length_bytes,
52 size_t virtual_payload_length_bytes,
53 double time_ms);
54
kwibergb8e56ee2016-08-29 06:37:33 -070055 virtual ~Packet();
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000056
57 // Parses the first bytes of the RTP payload, interpreting them as RED headers
58 // according to RFC 2198. The headers will be inserted into |headers|. The
59 // caller of the method assumes ownership of the objects in the list, and
60 // must delete them properly.
61 bool ExtractRedHeaders(std::list<RTPHeader*>* headers) const;
62
63 // Deletes all RTPHeader objects in |headers|, but does not delete |headers|
64 // itself.
65 static void DeleteRedHeaders(std::list<RTPHeader*>* headers);
66
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020067 const uint8_t* payload() const { return rtp_payload_.data(); }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000068
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020069 size_t packet_length_bytes() const { return packet_.size(); }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000070
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020071 size_t payload_length_bytes() const { return rtp_payload_.size(); }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000072
73 size_t virtual_packet_length_bytes() const {
74 return virtual_packet_length_bytes_;
75 }
76
77 size_t virtual_payload_length_bytes() const {
78 return virtual_payload_length_bytes_;
79 }
80
81 const RTPHeader& header() const { return header_; }
82
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000083 double time_ms() const { return time_ms_; }
84 bool valid_header() const { return valid_header_; }
85
86 private:
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020087 bool ParseHeader(const RtpHeaderExtensionMap* extension_map);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000088 void CopyToHeader(RTPHeader* destination) const;
89
90 RTPHeader header_;
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020091 const rtc::CopyOnWriteBuffer packet_;
92 rtc::ArrayView<const uint8_t> rtp_payload_; // Empty for dummy RTP packets.
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000093 // Virtual lengths are used when parsing RTP header files (dummy RTP files).
94 const size_t virtual_packet_length_bytes_;
Tommi25eb47c2019-08-29 16:39:05 +020095 size_t virtual_payload_length_bytes_ = 0;
96 const double time_ms_; // Used to denote a packet's arrival time.
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020097 const bool valid_header_;
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000098
henrikg3c089d72015-09-16 05:37:44 -070099 RTC_DISALLOW_COPY_AND_ASSIGN(Packet);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000100};
101
102} // namespace test
103} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200104#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_