libtins  4.6
rtp.h
1 #ifndef TINS_RTP_H
2 #define TINS_RTP_H
3 
4 #include <tins/endianness.h>
5 #include <tins/pdu.h>
6 #include <tins/pdu_option.h>
7 #include <tins/small_uint.h>
8 
9 namespace Tins {
10 
19 class TINS_API RTP : public PDU {
20 public:
24  static const PDU::PDUType pdu_flag = PDU::RTP;
25 
29  typedef std::vector<uint32_t> csrc_ids_type;
30 
34  typedef std::vector<uint32_t> extension_header_data_type;
35 
39  RTP();
40 
47  RTP(const uint8_t* data, uint32_t size);
48 
52  small_uint<2> version() const { return header_.version; }
53 
57  small_uint<1> padding_bit() const { return header_.padding; }
58 
62  small_uint<1> extension_bit() const { return header_.extension; }
63 
67  small_uint<4> csrc_count() const { return header_.csrc_count; }
68 
72  small_uint<1> marker_bit() const { return header_.marker; }
73 
77  small_uint<7> payload_type() const { return header_.payload_type; }
78 
82  uint16_t sequence_number() const { return Endian::be_to_host(header_.seq_num); }
83 
87  uint32_t timestamp() const { return Endian::be_to_host(header_.timestamp); }
88 
92  uint32_t ssrc_id() const { return Endian::be_to_host(header_.ssrc_id); }
93 
97  const csrc_ids_type& csrc_ids() const {
98  return csrc_ids_;
99  }
100 
104  uint8_t padding_size() const { return padding_size_; }
105 
109  uint16_t extension_profile() const { return Endian::be_to_host(ext_header_.profile); }
110 
114  uint16_t extension_length() const { return Endian::be_to_host(ext_header_.length); }
115 
120  return ext_data_;
121  }
122 
127  void version(small_uint<2> version) { header_.version = version; }
128 
133  void extension_bit(small_uint<1> extension) { header_.extension = extension; }
134 
139  void marker_bit(small_uint<1> marker) { header_.marker = marker; }
140 
145  void payload_type(small_uint<7> payload_type) { header_.payload_type = payload_type; }
146 
151  void sequence_number(uint16_t seq_num) { header_.seq_num = Endian::host_to_be(seq_num); }
152 
157  void timestamp(uint32_t timestamp) { header_.timestamp = Endian::host_to_be(timestamp); }
158 
163  void ssrc_id(uint32_t ssrc_id) { header_.ssrc_id = Endian::host_to_be(ssrc_id); }
164 
169  void padding_size(uint8_t size) {
170  padding_bit(size > 0);
171  padding_size_ = size;
172  }
173 
178  void extension_profile(uint16_t profile) { ext_header_.profile = Endian::host_to_be(profile); }
179 
187  void add_extension_data(const uint32_t value);
188 
198  bool remove_extension_data(const uint32_t value);
199 
205  bool search_extension_data(const uint32_t value);
206 
215  void add_csrc_id(const uint32_t csrc_id);
216 
226  bool remove_csrc_id(const uint32_t value);
227 
233  bool search_csrc_id(const uint32_t value);
234 
243  uint32_t header_size() const;
244 
253  uint32_t trailer_size() const { return static_cast<uint32_t>(padding_size_); }
254 
259  PDUType pdu_type() const { return pdu_flag; }
260 
264  RTP *clone() const { return new RTP(*this); }
265 
266 private:
267  TINS_BEGIN_PACK
268  struct rtp_header {
269  #if TINS_IS_BIG_ENDIAN
270  uint16_t version:2,
271  padding:1,
272  extension:1,
273  csrc_count:4,
274  marker:1,
275  payload_type:7;
276  #elif TINS_IS_LITTLE_ENDIAN
277  uint16_t csrc_count:4,
278  extension:1,
279  padding:1,
280  version:2,
281  payload_type:7,
282  marker:1;
283  #endif
284  uint16_t seq_num;
285  uint32_t timestamp;
286  uint32_t ssrc_id;
287  } TINS_END_PACK;
288 
289  TINS_BEGIN_PACK
290  struct rtp_extension_header {
291  uint16_t profile;
292  uint16_t length;
293  } TINS_END_PACK;
294 
295  void write_serialization(uint8_t* buffer, uint32_t size);
296  csrc_ids_type::const_iterator search_csrc_id_iterator(const uint32_t csrc_id) const;
297  csrc_ids_type::iterator search_csrc_id_iterator(const uint32_t csrc_id);
298  extension_header_data_type::const_iterator search_extension_data_iterator(const uint32_t data) const;
299  extension_header_data_type::iterator search_extension_data_iterator(const uint32_t data);
300 
305  void padding_bit(small_uint<1> padding) { header_.padding = padding; }
306 
311  void csrc_count(small_uint<4> csrc_count) { header_.csrc_count = csrc_count; }
312 
317  void extension_length(uint16_t length) { ext_header_.length = Endian::host_to_be(length); }
318 
319  rtp_header header_;
320  csrc_ids_type csrc_ids_;
321  rtp_extension_header ext_header_;
322  extension_header_data_type ext_data_;
323  uint8_t padding_size_;
324 };
325 
326 } // Tins
327 
328 #endif // TINS_RTP_H
Base class for protocol data units.
Definition: pdu.h:107
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
Represents a RTP PDU.
Definition: rtp.h:19
const csrc_ids_type & csrc_ids() const
Getter for the CSRC identifiers.
Definition: rtp.h:97
std::vector< uint32_t > extension_header_data_type
Definition: rtp.h:34
void sequence_number(uint16_t seq_num)
Setter for the sequence number.
Definition: rtp.h:151
small_uint< 1 > padding_bit() const
Getter for the padding bit.
Definition: rtp.h:57
small_uint< 4 > csrc_count() const
Getter for the CSRC count.
Definition: rtp.h:67
void ssrc_id(uint32_t ssrc_id)
Setter for the SSRC identifier.
Definition: rtp.h:163
uint32_t ssrc_id() const
Getter for the SSRC identifier.
Definition: rtp.h:92
std::vector< uint32_t > csrc_ids_type
Definition: rtp.h:29
uint16_t extension_length() const
Getter for the extension header length.
Definition: rtp.h:114
uint32_t trailer_size() const
Returns the RTP packet's trailer length.
Definition: rtp.h:253
void payload_type(small_uint< 7 > payload_type)
Setter for the payload type.
Definition: rtp.h:145
small_uint< 7 > payload_type() const
Getter for the payload type.
Definition: rtp.h:77
uint8_t padding_size() const
Getter for the padding size.
Definition: rtp.h:104
RTP * clone() const
Definition: rtp.h:264
small_uint< 1 > extension_bit() const
Getter for the extension bit.
Definition: rtp.h:62
void extension_bit(small_uint< 1 > extension)
Setter for the extension bit.
Definition: rtp.h:133
void extension_profile(uint16_t profile)
Setter for the extension header profile.
Definition: rtp.h:178
small_uint< 1 > marker_bit() const
Getter for the marker bit.
Definition: rtp.h:72
const extension_header_data_type & extension_data() const
Getter for the extension header data.
Definition: rtp.h:119
void version(small_uint< 2 > version)
Setter for the version.
Definition: rtp.h:127
uint32_t timestamp() const
Getter for the timestamp.
Definition: rtp.h:87
uint16_t sequence_number() const
Getter for the sequence number.
Definition: rtp.h:82
uint16_t extension_profile() const
Getter for the extension header profile.
Definition: rtp.h:109
PDUType pdu_type() const
Getter for the PDU's type.
Definition: rtp.h:259
void timestamp(uint32_t timestamp)
Setter for the timestamp.
Definition: rtp.h:157
void padding_size(uint8_t size)
Setter for the padding size.
Definition: rtp.h:169
small_uint< 2 > version() const
Getter for the version.
Definition: rtp.h:52
void marker_bit(small_uint< 1 > marker)
Setter for the marker bit.
Definition: rtp.h:139
The Tins namespace.
Definition: address_range.h:38