Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
packetizer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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_audio/packetizer.h
10 //! @brief Packetizer.
11 
12 #ifndef ROC_AUDIO_PACKETIZER_H_
13 #define ROC_AUDIO_PACKETIZER_H_
14 
16 #include "roc_audio/iwriter.h"
17 #include "roc_audio/units.h"
18 #include "roc_core/buffer_pool.h"
19 #include "roc_core/noncopyable.h"
20 #include "roc_core/time.h"
21 #include "roc_packet/icomposer.h"
22 #include "roc_packet/iwriter.h"
23 #include "roc_packet/packet_pool.h"
24 #include "roc_packet/units.h"
25 
26 namespace roc {
27 namespace audio {
28 
29 //! Packetizer.
30 //! @remarks
31 //! Gets an audio stream, encodes samples to packets using an encoder, and
32 //! writes packets to a packet writer.
33 class Packetizer : public IWriter, public core::NonCopyable<> {
34 public:
35  //! Initialization.
36  //!
37  //! @b Parameters
38  //! - @p writer is used to write generated packets
39  //! - @p composer is used to initialize new packets
40  //! - @p payload_encoder is used to write samples to packets
41  //! - @p packet_pool is used to allocate packets
42  //! - @p buffer_pool is used to allocate buffers for packets
43  //! - @p channels defines a set of channels in the input frames
44  //! - @p packet_length defines packet length in nanoseconds
45  //! - @p sample_rate defines number of samples per channel per second
46  //! - @p payload_type defines packet payload type
48  packet::IComposer& composer,
49  IFrameEncoder& payload_encoder,
50  packet::PacketPool& packet_pool,
51  core::BufferPool<uint8_t>& buffer_pool,
52  packet::channel_mask_t channels,
53  core::nanoseconds_t packet_length,
54  size_t sample_rate,
55  unsigned int payload_type);
56 
57  //! Write audio frame.
58  virtual void write(Frame& frame);
59 
60  //! Flush buffered packet, if any.
61  //! @remarks
62  //! Packet is padded to match fixed size.
63  void flush();
64 
65 private:
66  bool begin_packet_();
67  void end_packet_();
68 
69  void pad_packet_();
70 
71  packet::PacketPtr create_packet_();
72 
73  packet::IWriter& writer_;
74  packet::IComposer& composer_;
75  IFrameEncoder& payload_encoder_;
76 
77  packet::PacketPool& packet_pool_;
78  core::BufferPool<uint8_t>& buffer_pool_;
79 
80  const packet::channel_mask_t channels_;
81  const size_t num_channels_;
82  const size_t samples_per_packet_;
83  const unsigned int payload_type_;
84  const size_t payload_size_;
85 
86  packet::PacketPtr packet_;
87  size_t packet_pos_;
88 
89  const packet::source_t source_;
90  packet::seqnum_t seqnum_;
91  packet::timestamp_t timestamp_;
92 };
93 
94 } // namespace audio
95 } // namespace roc
96 
97 #endif // ROC_AUDIO_PACKETIZER_H_
Audio frame encoder interface.
virtual void write(Frame &frame)
Write audio frame.
Audio writer interface.
Definition: iwriter.h:21
Time definitions.
Various units used in audio processing.
Packet pool.
Root namespace.
Packet writer interface.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:21
uint32_t source_t
Packet source ID identifying packet stream.
Definition: units.h:22
uint32_t channel_mask_t
Bitmask of channels present in audio packet.
Definition: units.h:77
uint32_t timestamp_t
Audio packet timestamp.
Definition: units.h:46
Audio frame.
Definition: frame.h:22
void flush()
Flush buffered packet, if any.
Audio writer interface.
Packet composer interface.
Definition: icomposer.h:22
Packet composer interface.
Packet writer interface.
Definition: iwriter.h:21
Base class for non-copyable objects.
Definition: noncopyable.h:23
Audio frame encoder interface.
Packetizer(packet::IWriter &writer, packet::IComposer &composer, IFrameEncoder &payload_encoder, packet::PacketPool &packet_pool, core::BufferPool< uint8_t > &buffer_pool, packet::channel_mask_t channels, core::nanoseconds_t packet_length, size_t sample_rate, unsigned int payload_type)
Initialization.
Various units used in packets.
Buffer pool.
Non-copyable object.
uint16_t seqnum_t
Packet sequence number in packet stream.
Definition: units.h:25