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 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_audio/packetizer.h
10 //! @brief Packetizer.
11 
12 #ifndef ROC_AUDIO_PACKETIZER_H_
13 #define ROC_AUDIO_PACKETIZER_H_
14 
17 #include "roc_audio/sample.h"
18 #include "roc_audio/sample_spec.h"
20 #include "roc_core/noncopyable.h"
21 #include "roc_core/time.h"
22 #include "roc_packet/icomposer.h"
23 #include "roc_packet/iwriter.h"
25 #include "roc_packet/units.h"
26 
27 namespace roc {
28 namespace audio {
29 
30 //! Packetizer.
31 //! @remarks
32 //! Gets an audio stream, encodes samples to packets using an encoder, and
33 //! writes packets to a packet writer.
34 class Packetizer : public IFrameWriter, public core::NonCopyable<> {
35 public:
36  //! Initialization.
37  //!
38  //! @b Parameters
39  //! - @p writer is used to write generated packets
40  //! - @p composer is used to initialize new packets
41  //! - @p payload_encoder is used to write samples to packets
42  //! - @p packet_factory is used to allocate packets
43  //! - @p buffer_factory is used to allocate buffers for packets
44  //! - @p packet_length defines packet length in nanoseconds
45  //! - @p sample_spec defines the sample spec
46  //! - @p payload_type defines packet payload type
48  packet::IComposer& composer,
49  IFrameEncoder& payload_encoder,
50  packet::PacketFactory& packet_factory,
51  core::BufferFactory<uint8_t>& buffer_factory,
52  core::nanoseconds_t packet_length,
53  const audio::SampleSpec& sample_spec,
54  unsigned int payload_type);
55 
56  //! Check if object is successfully constructed.
57  bool is_valid() const;
58 
59  //! Write audio frame.
60  virtual void write(Frame& frame);
61 
62  //! Flush buffered packet, if any.
63  //! @remarks
64  //! Packet is padded to match fixed size.
65  void flush();
66 
67 private:
68  bool begin_packet_();
69  void end_packet_();
70 
71  void pad_packet_();
72 
73  packet::PacketPtr create_packet_();
74 
75  packet::IWriter& writer_;
76  packet::IComposer& composer_;
77  IFrameEncoder& payload_encoder_;
78 
79  packet::PacketFactory& packet_factory_;
80  core::BufferFactory<uint8_t>& buffer_factory_;
81 
82  const audio::SampleSpec sample_spec_;
83  const size_t samples_per_packet_;
84  const unsigned int payload_type_;
85  const size_t payload_size_;
86 
87  packet::PacketPtr packet_;
88  size_t packet_pos_;
89 
91  packet::seqnum_t seqnum_;
92  packet::stream_timestamp_t stream_ts_;
93  core::nanoseconds_t capture_ts_;
94 
95  bool valid_;
96 };
97 
98 } // namespace audio
99 } // namespace roc
100 
101 #endif // ROC_AUDIO_PACKETIZER_H_
Buffer factory.
Audio frame.
Definition: frame.h:25
Audio frame encoder interface.
Frame writer interface.
Definition: iframe_writer.h:22
bool is_valid() const
Check if object is successfully constructed.
virtual void write(Frame &frame)
Write audio frame.
void flush()
Flush buffered packet, if any.
Packetizer(packet::IWriter &writer, packet::IComposer &composer, IFrameEncoder &payload_encoder, packet::PacketFactory &packet_factory, core::BufferFactory< uint8_t > &buffer_factory, core::nanoseconds_t packet_length, const audio::SampleSpec &sample_spec, unsigned int payload_type)
Initialization.
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:26
Base class for non-copyable objects.
Definition: noncopyable.h:23
Packet composer interface.
Definition: icomposer.h:22
Packet writer interface.
Definition: iwriter.h:23
Packet composer interface.
Audio frame encoder interface.
Frame writer interface.
Packet writer interface.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
uint32_t stream_source_t
Packet stream identifier.
Definition: units.h:27
uint16_t seqnum_t
Packet sequence number.
Definition: units.h:64
uint32_t stream_timestamp_t
Packet stream timestamp.
Definition: units.h:36
Root namespace.
Non-copyable object.
Packet factory.
Audio sample.
Sample specifications.
Time definitions.
Various units used in packets.