blob: 96710907dfd78aef9bcf50916c787893460e90f5 [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"
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020019#include "rtc_base/copy_on_write_buffer.h"
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000020
21namespace webrtc {
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000022namespace test {
23
24// Class for handling RTP packets in test applications.
25class Packet {
26 public:
27 // Creates a packet, with the packet payload (including header bytes) in
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020028 // `packet`. The `time_ms` is an extra time associated with this packet,
29 // typically used to denote arrival time.
30 // `virtual_packet_length_bytes` is typically used when reading RTP dump files
Tommi25eb47c2019-08-29 16:39:05 +020031 // that only contain the RTP headers, and no payload (a.k.a RTP dummy files or
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020032 // RTP light). The `virtual_packet_length_bytes` tells what size the packet
33 // had on wire, including the now discarded payload.
34 Packet(rtc::CopyOnWriteBuffer packet,
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000035 size_t virtual_packet_length_bytes,
36 double time_ms,
Tommi25eb47c2019-08-29 16:39:05 +020037 const RtpHeaderExtensionMap* extension_map = nullptr);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000038
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020039 Packet(rtc::CopyOnWriteBuffer packet,
40 double time_ms,
41 const RtpHeaderExtensionMap* extension_map = nullptr)
42 : Packet(packet, packet.size(), time_ms, extension_map) {}
43
Bjorn Terelius5350d1c2018-10-11 16:51:23 +020044 // Same as above, but creates the packet from an already parsed RTPHeader.
45 // This is typically used when reading RTP dump files that only contain the
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020046 // RTP headers, and no payload. The `virtual_packet_length_bytes` tells what
Bjorn Terelius5350d1c2018-10-11 16:51:23 +020047 // size the packet had on wire, including the now discarded payload,
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020048 // The `virtual_payload_length_bytes` tells the size of the payload.
Bjorn Terelius5350d1c2018-10-11 16:51:23 +020049 Packet(const RTPHeader& header,
50 size_t virtual_packet_length_bytes,
51 size_t virtual_payload_length_bytes,
52 double time_ms);
53
kwibergb8e56ee2016-08-29 06:37:33 -070054 virtual ~Packet();
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000055
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090056 Packet(const Packet&) = delete;
57 Packet& operator=(const Packet&) = delete;
58
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000059 // Parses the first bytes of the RTP payload, interpreting them as RED headers
Artem Titovd00ce742021-07-28 20:00:17 +020060 // according to RFC 2198. The headers will be inserted into `headers`. The
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000061 // caller of the method assumes ownership of the objects in the list, and
62 // must delete them properly.
63 bool ExtractRedHeaders(std::list<RTPHeader*>* headers) const;
64
Artem Titovd00ce742021-07-28 20:00:17 +020065 // Deletes all RTPHeader objects in `headers`, but does not delete `headers`
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000066 // itself.
67 static void DeleteRedHeaders(std::list<RTPHeader*>* headers);
68
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020069 const uint8_t* payload() const { return rtp_payload_.data(); }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000070
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020071 size_t packet_length_bytes() const { return packet_.size(); }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000072
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020073 size_t payload_length_bytes() const { return rtp_payload_.size(); }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000074
75 size_t virtual_packet_length_bytes() const {
76 return virtual_packet_length_bytes_;
77 }
78
79 size_t virtual_payload_length_bytes() const {
80 return virtual_payload_length_bytes_;
81 }
82
83 const RTPHeader& header() const { return header_; }
84
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000085 double time_ms() const { return time_ms_; }
86 bool valid_header() const { return valid_header_; }
87
88 private:
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020089 bool ParseHeader(const RtpHeaderExtensionMap* extension_map);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000090 void CopyToHeader(RTPHeader* destination) const;
91
92 RTPHeader header_;
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020093 const rtc::CopyOnWriteBuffer packet_;
94 rtc::ArrayView<const uint8_t> rtp_payload_; // Empty for dummy RTP packets.
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000095 // Virtual lengths are used when parsing RTP header files (dummy RTP files).
96 const size_t virtual_packet_length_bytes_;
Tommi25eb47c2019-08-29 16:39:05 +020097 size_t virtual_payload_length_bytes_ = 0;
98 const double time_ms_; // Used to denote a packet's arrival time.
Danil Chapovalovb4100ad2021-06-16 14:23:22 +020099 const bool valid_header_;
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_