xref: /aosp_15_r20/external/webrtc/modules/audio_coding/neteq/tools/packet.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_
12 #define MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_
13 
14 #include <list>
15 
16 #include "api/array_view.h"
17 #include "api/rtp_headers.h"
18 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
19 #include "rtc_base/copy_on_write_buffer.h"
20 
21 namespace webrtc {
22 namespace test {
23 
24 // Class for handling RTP packets in test applications.
25 class Packet {
26  public:
27   // Creates a packet, with the packet payload (including header bytes) in
28   // `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
31   // that only contain the RTP headers, and no payload (a.k.a RTP dummy files or
32   // 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,
35          size_t virtual_packet_length_bytes,
36          double time_ms,
37          const RtpHeaderExtensionMap* extension_map = nullptr);
38 
39   Packet(rtc::CopyOnWriteBuffer packet,
40          double time_ms,
41          const RtpHeaderExtensionMap* extension_map = nullptr)
42       : Packet(packet, packet.size(), time_ms, extension_map) {}
43 
44   // 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
46   // RTP headers, and no payload. The `virtual_packet_length_bytes` tells what
47   // size the packet had on wire, including the now discarded payload,
48   // The `virtual_payload_length_bytes` tells the size of the payload.
49   Packet(const RTPHeader& header,
50          size_t virtual_packet_length_bytes,
51          size_t virtual_payload_length_bytes,
52          double time_ms);
53 
54   virtual ~Packet();
55 
56   Packet(const Packet&) = delete;
57   Packet& operator=(const Packet&) = delete;
58 
59   // Parses the first bytes of the RTP payload, interpreting them as RED headers
60   // according to RFC 2198. The headers will be inserted into `headers`. The
61   // 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 
65   // Deletes all RTPHeader objects in `headers`, but does not delete `headers`
66   // itself.
67   static void DeleteRedHeaders(std::list<RTPHeader*>* headers);
68 
payload()69   const uint8_t* payload() const { return rtp_payload_.data(); }
70 
packet_length_bytes()71   size_t packet_length_bytes() const { return packet_.size(); }
72 
payload_length_bytes()73   size_t payload_length_bytes() const { return rtp_payload_.size(); }
74 
virtual_packet_length_bytes()75   size_t virtual_packet_length_bytes() const {
76     return virtual_packet_length_bytes_;
77   }
78 
virtual_payload_length_bytes()79   size_t virtual_payload_length_bytes() const {
80     return virtual_payload_length_bytes_;
81   }
82 
header()83   const RTPHeader& header() const { return header_; }
84 
time_ms()85   double time_ms() const { return time_ms_; }
valid_header()86   bool valid_header() const { return valid_header_; }
87 
88  private:
89   bool ParseHeader(const RtpHeaderExtensionMap* extension_map);
90   void CopyToHeader(RTPHeader* destination) const;
91 
92   RTPHeader header_;
93   const rtc::CopyOnWriteBuffer packet_;
94   rtc::ArrayView<const uint8_t> rtp_payload_;  // Empty for dummy RTP packets.
95   // Virtual lengths are used when parsing RTP header files (dummy RTP files).
96   const size_t virtual_packet_length_bytes_;
97   size_t virtual_payload_length_bytes_ = 0;
98   const double time_ms_;     // Used to denote a packet's arrival time.
99   const bool valid_header_;
100 };
101 
102 }  // namespace test
103 }  // namespace webrtc
104 #endif  // MODULES_AUDIO_CODING_NETEQ_TOOLS_PACKET_H_
105