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