Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
packet.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Roc authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_packet/packet.h
10 //! @brief Packet.
11 
12 #ifndef ROC_PACKET_PACKET_H_
13 #define ROC_PACKET_PACKET_H_
14 
15 #include "roc_core/helpers.h"
16 #include "roc_core/list_node.h"
17 #include "roc_core/pool.h"
18 #include "roc_core/refcnt.h"
19 #include "roc_core/shared_ptr.h"
20 #include "roc_packet/fec.h"
21 #include "roc_packet/print.h"
22 #include "roc_packet/rtp.h"
23 #include "roc_packet/udp.h"
24 
25 namespace roc {
26 namespace packet {
27 
28 class PacketPool;
29 class Packet;
30 
31 //! Packet smart pointer.
33 
34 //! Packet.
35 class Packet : public core::RefCnt<Packet>, public core::ListNode {
36 public:
37  //! Constructor.
38  explicit Packet(PacketPool&);
39 
40  //! Packet flags.
41  enum {
42  FlagUDP = (1 << 0), //!< Packet contains UDP header.
43  FlagRTP = (1 << 1), //!< Packet contains RTP header.
44  FlagFEC = (1 << 2), //!< Packet contains FEC header.
45  FlagAudio = (1 << 3), //!< Packet contains audio samples.
46  FlagRepair = (1 << 4), //!< Packet contains repair FEC symbols.
47  FlagComposed = (1 << 5), //!< Packet is already composed.
48  FlagRestored = (1 << 6) //!< Packet was restored using FEC decoder.
49  };
50 
51  //! Add flags.
52  void add_flags(unsigned flags);
53 
54  //! Get flags.
55  unsigned flags() const;
56 
57  //! UDP packet.
58  const UDP* udp() const;
59 
60  //! UDP packet.
61  UDP* udp();
62 
63  //! RTP packet.
64  const RTP* rtp() const;
65 
66  //! RTP packet.
67  RTP* rtp();
68 
69  //! FEC packet.
70  const FEC* fec() const;
71 
72  //! FEC packet.
73  FEC* fec();
74 
75  //! Get packet data.
76  const core::Slice<uint8_t>& data() const;
77 
78  //! Set packet data.
79  void set_data(const core::Slice<uint8_t>& data);
80 
81  //! Return packet stream identifier.
82  //! @remarks
83  //! The returning value depends on packet type. For some packet types, may
84  //! be always zero.
85  source_t source() const;
86 
87  //! Get the timestamp of the first sample in packet.
88  //! @remarks
89  //! Timestamp units depend on packet type. For some packet types, may
90  //! be always zero.
91  timestamp_t begin() const;
92 
93  //! Get the timestamp of the last sample in packet plus one.
94  //! @remarks
95  //! Timestamp units depend on packet type. For some packet types, may
96  //! be always zero.
97  timestamp_t end() const;
98 
99  //! Determine packet order.
100  //! @returns
101  //! * -1 if this packet precedes @p other packet
102  //! * 0 if this packet has the same position as @p other packet
103  //! * +1 if this packet succeeds @p other packet
104  int compare(const Packet& other) const;
105 
106  //! Print packet to stderr.
107  void print(int flags) const {
108  packet::print(*this, flags);
109  }
110 
111  //! Get pointer to packet from a pointer to its UDP part.
113  return ROC_CONTAINER_OF(udp, Packet, udp_);
114  }
115 
116 private:
117  friend class core::RefCnt<Packet>;
118 
119  void destroy();
120 
121  PacketPool& pool_;
122 
123  unsigned flags_;
124 
125  UDP udp_;
126  RTP rtp_;
127  FEC fec_;
128 
129  core::Slice<uint8_t> data_;
130 };
131 
132 } // namespace packet
133 } // namespace roc
134 
135 #endif // ROC_PACKET_PACKET_H_
Packet contains RTP header.
Definition: packet.h:43
void print(int flags) const
Print packet to stderr.
Definition: packet.h:107
Packet contains repair FEC symbols.
Definition: packet.h:46
Packet contains FEC header.
Definition: packet.h:44
#define ROC_CONTAINER_OF(ptr, type, member)
Cast a member of a structure out to the containing structure.
Definition: helpers.h:19
Packet contains UDP header.
Definition: packet.h:42
const RTP * rtp() const
RTP packet.
Pool.
Packet contains audio samples.
Definition: packet.h:45
int compare(const Packet &other) const
Determine packet order.
Base class for list element.
Definition: list_node.h:26
Root namespace.
Linked list node.
unsigned flags() const
Get flags.
source_t source() const
Return packet stream identifier.
Shared ownership intrusive pointer.
void set_data(const core::Slice< uint8_t > &data)
Set packet data.
timestamp_t begin() const
Get the timestamp of the first sample in packet.
const FEC * fec() const
FEC packet.
FECFRAME packet.
Definition: fec.h:35
uint32_t source_t
Packet source ID identifying packet stream.
Definition: units.h:22
uint32_t timestamp_t
Audio packet timestamp.
Definition: units.h:46
void add_flags(unsigned flags)
Add flags.
RTP packet.
Definition: rtp.h:23
UDP packet.
Definition: udp.h:25
const UDP * udp() const
UDP packet.
Packet is already composed.
Definition: packet.h:47
Packet.
Definition: packet.h:35
timestamp_t end() const
Get the timestamp of the last sample in packet plus one.
Packet(PacketPool &)
Constructor.
const core::Slice< uint8_t > & data() const
Get packet data.
FEC packet.
Base class for reference countable objects.
Definition: refcnt.h:25
Base class for reference countable objects.
Packet was restored using FEC decoder.
Definition: packet.h:48
Compile time helpers.
core::SharedPtr< Packet > PacketPtr
Packet smart pointer.
Definition: packet.h:29
RTP packet.
static Packet * container_of(UDP *udp)
Get pointer to packet from a pointer to its UDP part.
Definition: packet.h:112
void print(const Packet &packet, int flags)
Print packet to stderr.
UDP packet.