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 Streaming 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/list_node.h"
16 #include "roc_core/macro_helpers.h"
18 #include "roc_core/ref_counted.h"
19 #include "roc_core/shared_ptr.h"
20 #include "roc_core/slab_pool.h"
21 #include "roc_core/time.h"
22 #include "roc_packet/fec.h"
24 #include "roc_packet/rtcp.h"
25 #include "roc_packet/rtp.h"
26 #include "roc_packet/udp.h"
27 
28 namespace roc {
29 namespace packet {
30 
31 class Packet;
32 
33 //! Packet smart pointer.
35 
36 //! Packet.
37 class Packet : public core::RefCounted<Packet, core::PoolAllocation>,
38  public core::ListNode<>,
39  public core::MpscQueueNode<> {
40 public:
41  //! Constructor.
42  explicit Packet(core::IPool& packet_pool);
43 
44  //! Packet flags.
45  enum Flag {
46  FlagUDP = (1 << 0), //!< Packet contains UDP header.
47  FlagRTP = (1 << 1), //!< Packet contains RTP header.
48  FlagFEC = (1 << 2), //!< Packet contains FEC header.
49  FlagRTCP = (1 << 3), //!< Packet contains RTCP compound packet.
50  FlagAudio = (1 << 4), //!< Packet contains audio samples.
51  FlagRepair = (1 << 5), //!< Packet contains repair FEC symbols.
52  FlagControl = (1 << 6), //!< Packet contains control message.
53  FlagPrepared = (1 << 7), //!< Packet was prepared for composing.
54  FlagComposed = (1 << 8), //!< Packet was composed.
55  FlagRestored = (1 << 9) //!< Packet was restored using FEC decoder.
56  };
57 
58  //! Add flags.
59  void add_flags(unsigned flags);
60 
61  //! Check specific flag.
62  bool has_flags(unsigned flags) const;
63 
64  //! Get flags.
65  unsigned flags() const;
66 
67  //! UDP packet.
68  const UDP* udp() const;
69 
70  //! UDP packet.
71  UDP* udp();
72 
73  //! RTP packet.
74  const RTP* rtp() const;
75 
76  //! RTP packet.
77  RTP* rtp();
78 
79  //! FEC packet.
80  const FEC* fec() const;
81 
82  //! FEC packet.
83  FEC* fec();
84 
85  //! RTCP packet.
86  const RTCP* rtcp() const;
87 
88  //! RTCP packet.
89  RTCP* rtcp();
90 
91  //! Get packet buffer.
92  //! @remarks
93  //! Returns slice with entire packet with all headers and footers.
94  const core::Slice<uint8_t>& buffer() const;
95 
96  //! Set packet buffer.
97  void set_buffer(const core::Slice<uint8_t>& data);
98 
99  //! Get protocol-dependent packet payload.
100  //! @remarks
101  //! Returns sub-slice with inner-most packet data.
102  //! E.g. for RTP nested into FECFRAME, returns payload
103  //! of RTP packet (where samples are stored).
105 
106  //! Check if packet has stream identifier.
107  //! @remarks
108  //! The returning value depends on packet type. If this method returns
109  //! true, then source_id() returns stream identifier.
110  bool has_source_id() const;
111 
112  //! Get packet stream identifier.
113  //! @remarks
114  //! The returning value depends on packet type. For some packet types, may
115  //! be always zero.
117 
118  //! Get stream timestamp (STS) of the packet.
119  //! @remarks
120  //! Timestamp units depend on packet type. For some packet types, may
121  //! be always zero.
123 
124  //! Get duration of the packet.
125  //! @remarks
126  //! Units are the same as for stream_timestamp().
128 
129  //! Get capture timestamp (CTS) of the packet.
130  //! @remarks
131  //! Returns number of nanoseconds since Unix epoch.
133 
134  //! Get receive timestamp (RTS) of the packet.
135  //! @remarks
136  //! Returns number of nanoseconds since Unix epoch.
138 
139  //! Determine packet ordering.
140  //! @returns
141  //! * -1 if this packet precedes @p other packet
142  //! * 0 if this packet has the same position as @p other packet
143  //! * +1 if this packet succeeds @p other packet
144  int compare(const Packet& other) const;
145 
146  //! Print packet to stderr.
147  void print(int flags) const {
148  packet::print_packet(*this, flags);
149  }
150 
151  //! Get pointer to packet from a pointer to its UDP part.
153  return ROC_CONTAINER_OF(udp, Packet, udp_);
154  }
155 
156  //! Estimate number of bytes per packet for given number of samples.
157  //! This is only an approximation, don't rely on it.
158  static size_t approx_size(size_t n_samples);
159 
160 private:
161  unsigned flags_;
162 
163  UDP udp_;
164  RTP rtp_;
165  FEC fec_;
166  RTCP rtcp_;
167 
168  core::Slice<uint8_t> buffer_;
169 };
170 
171 } // namespace packet
172 } // namespace roc
173 
174 #endif // ROC_PACKET_PACKET_H_
Memory pool interface.
Definition: ipool.h:23
Base class for List element.
Definition: list_node.h:48
Base class for MpscQueue element.
Base class for object with reference counter.
Definition: ref_counted.h:40
const FEC * fec() const
FEC packet.
UDP * udp()
UDP packet.
void print(int flags) const
Print packet to stderr.
Definition: packet.h:147
stream_source_t source_id() const
Get packet stream identifier.
const UDP * udp() const
UDP packet.
static size_t approx_size(size_t n_samples)
Estimate number of bytes per packet for given number of samples. This is only an approximation,...
bool has_source_id() const
Check if packet has stream identifier.
static Packet * container_of(UDP *udp)
Get pointer to packet from a pointer to its UDP part.
Definition: packet.h:152
RTP * rtp()
RTP packet.
int compare(const Packet &other) const
Determine packet ordering.
const core::Slice< uint8_t > & payload() const
Get protocol-dependent packet payload.
FEC * fec()
FEC packet.
stream_timestamp_t duration() const
Get duration of the packet.
unsigned flags() const
Get flags.
Packet(core::IPool &packet_pool)
Constructor.
const RTP * rtp() const
RTP packet.
RTCP * rtcp()
RTCP packet.
core::nanoseconds_t receive_timestamp() const
Get receive timestamp (RTS) of the packet.
const RTCP * rtcp() const
RTCP packet.
const core::Slice< uint8_t > & buffer() const
Get packet buffer.
Flag
Packet flags.
Definition: packet.h:45
@ FlagRestored
Packet was restored using FEC decoder.
Definition: packet.h:55
@ FlagPrepared
Packet was prepared for composing.
Definition: packet.h:53
@ FlagRTCP
Packet contains RTCP compound packet.
Definition: packet.h:49
@ FlagRepair
Packet contains repair FEC symbols.
Definition: packet.h:51
@ FlagControl
Packet contains control message.
Definition: packet.h:52
@ FlagRTP
Packet contains RTP header.
Definition: packet.h:47
@ FlagAudio
Packet contains audio samples.
Definition: packet.h:50
@ FlagComposed
Packet was composed.
Definition: packet.h:54
@ FlagUDP
Packet contains UDP header.
Definition: packet.h:46
@ FlagFEC
Packet contains FEC header.
Definition: packet.h:48
void set_buffer(const core::Slice< uint8_t > &data)
Set packet buffer.
core::nanoseconds_t capture_timestamp() const
Get capture timestamp (CTS) of the packet.
stream_timestamp_t stream_timestamp() const
Get stream timestamp (STS) of the packet.
bool has_flags(unsigned flags) const
Check specific flag.
void add_flags(unsigned flags)
Add flags.
FEC packet.
Linked list node.
Helper macros.
#define ROC_CONTAINER_OF(ptr, type, member)
Cast a member of a structure out to the containing structure.
Definition: macro_helpers.h:37
MpscQueue node.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
uint32_t stream_source_t
Packet stream identifier.
Definition: units.h:27
core::SharedPtr< Packet > PacketPtr
Packet smart pointer.
Definition: packet.h:31
uint32_t stream_timestamp_t
Packet stream timestamp.
Definition: units.h:36
void print_packet(const Packet &packet, int flags)
Print packet to stderr.
Root namespace.
Base class for object with reference counter.
Print packet to console.
RTCP compound packet.
RTP packet.
Shared ownership intrusive pointer.
Memory pool.
FECFRAME packet.
Definition: fec.h:35
RTCP compound packet.
Definition: rtcp.h:22
RTP packet.
Definition: rtp.h:24
UDP packet.
Definition: udp.h:26
Time definitions.
UDP packet.