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"
19 #include "roc_core/noncopyable.h"
20 #include "roc_core/time.h"
21 #include "roc_packet/icomposer.h"
22 #include "roc_packet/isequencer.h"
23 #include "roc_packet/iwriter.h"
25 #include "roc_packet/units.h"
26 
27 namespace roc {
28 namespace audio {
29 
30 //! Metrics of packetizer.
32  //! Cumulative count of produced packets.
33  uint64_t packet_count;
34 
35  //! Cumulative count of produced payload bytes.
36  //! This excludes packet headers and padding.
37  uint64_t payload_count;
38 
40  : packet_count(0)
41  , payload_count(0) {
42  }
43 };
44 
45 //! Packetizer.
46 //! @remarks
47 //! Gets an audio stream, encodes samples to packets using an encoder, and
48 //! writes packets to a packet writer.
49 class Packetizer : public IFrameWriter, public core::NonCopyable<> {
50 public:
51  //! Initialization.
52  //!
53  //! @b Parameters
54  //! - @p writer is used to write generated packets
55  //! - @p composer is used to initialize new packets
56  //! - @p sequencer is used to put packets in sequence
57  //! - @p payload_encoder is used to write samples to packets
58  //! - @p packet_factory is used to allocate packets
59  //! - @p buffer_factory is used to allocate buffers for packets
60  //! - @p packet_length defines packet length in nanoseconds
61  //! - @p sample_spec describes input frames
63  packet::IComposer& composer,
64  packet::ISequencer& sequencer,
65  IFrameEncoder& payload_encoder,
66  packet::PacketFactory& packet_factory,
67  core::nanoseconds_t packet_length,
68  const SampleSpec& sample_spec);
69 
70  //! Check if object is successfully constructed.
71  bool is_valid() const;
72 
73  //! Get sample rate.
74  size_t sample_rate() const;
75 
76  //! Get metrics.
77  const PacketizerMetrics& metrics() const;
78 
79  //! Write audio frame.
80  virtual void write(Frame& frame);
81 
82  //! Flush buffered packet, if any.
83  //! @remarks
84  //! Packet is padded to match fixed size.
85  void flush();
86 
87 private:
88  bool begin_packet_();
89  void end_packet_();
90 
91  void pad_packet_(size_t written_payload_size);
92 
93  packet::PacketPtr create_packet_();
94 
95  packet::IWriter& writer_;
96  packet::IComposer& composer_;
97  packet::ISequencer& sequencer_;
98  IFrameEncoder& payload_encoder_;
99 
100  packet::PacketFactory& packet_factory_;
101 
102  const SampleSpec sample_spec_;
103  size_t samples_per_packet_;
104  size_t payload_size_;
105 
106  packet::PacketPtr packet_;
107  size_t packet_pos_;
108  core::nanoseconds_t packet_cts_;
109 
110  core::nanoseconds_t capture_ts_;
111 
112  PacketizerMetrics metrics_;
113 
114  bool valid_;
115 };
116 
117 } // namespace audio
118 } // namespace roc
119 
120 #endif // ROC_AUDIO_PACKETIZER_H_
Audio frame.
Definition: frame.h:25
Audio frame encoder interface.
Frame writer interface.
Definition: iframe_writer.h:22
size_t sample_rate() const
Get sample rate.
bool is_valid() const
Check if object is successfully constructed.
Packetizer(packet::IWriter &writer, packet::IComposer &composer, packet::ISequencer &sequencer, IFrameEncoder &payload_encoder, packet::PacketFactory &packet_factory, core::nanoseconds_t packet_length, const SampleSpec &sample_spec)
Initialization.
virtual void write(Frame &frame)
Write audio frame.
void flush()
Flush buffered packet, if any.
const PacketizerMetrics & metrics() const
Get metrics.
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:30
Base class for non-copyable objects.
Definition: noncopyable.h:23
Packet composer interface.
Definition: icomposer.h:22
Packet sequencer. Fills protocol-specific packet headers to form a continous sequence....
Definition: isequencer.h:24
Packet writer interface.
Definition: iwriter.h:23
Packet composer interface.
Audio frame encoder interface.
Frame writer interface.
Packet sequencer.
Packet writer interface.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
Root namespace.
Non-copyable object.
Packet factory.
Audio sample.
Sample specifications.
Metrics of packetizer.
Definition: packetizer.h:31
uint64_t payload_count
Cumulative count of produced payload bytes. This excludes packet headers and padding.
Definition: packetizer.h:37
uint64_t packet_count
Cumulative count of produced packets.
Definition: packetizer.h:33
Time definitions.
Various units used in packets.