Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
interleaver.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_packet/interleaver.h
10 //! @brief Interleaves packets before transmit.
11 
12 #ifndef ROC_PACKET_INTERLEAVER_H_
13 #define ROC_PACKET_INTERLEAVER_H_
14 
15 #include "roc_core/array.h"
16 #include "roc_core/iarena.h"
17 #include "roc_core/noncopyable.h"
18 #include "roc_packet/iwriter.h"
19 #include "roc_packet/packet.h"
20 
21 namespace roc {
22 namespace packet {
23 
24 //! Interleaves packets to transmit them in pseudo random order.
25 class Interleaver : public IWriter, public core::NonCopyable<> {
26 public:
27  //! Initialize.
28  //! @remarks
29  //! Interleaver reorders packets passed to write() and writes
30  //! them to @p output.
31  Interleaver(IWriter& writer, core::IArena& arena, size_t block_size);
32 
33  //! Check if object is successfully constructed.
34  bool is_valid() const;
35 
36  //! Write next packet.
37  //! @remarks
38  //! Packets are written to internal buffer. Buffered packets are
39  //! then reordered and sent to output writer.
41 
42  //! Send all buffered packets to output writer.
44 
45  //! Maximum delay between writing packet and moment we get it in output
46  //! in terms of packets number.
47  size_t block_size() const;
48 
49 private:
50  //! Initialize tx_seq_ to a new randomized sequence.
51  void reinit_seq_();
52 
53  // Output writer.
54  IWriter& writer_;
55 
56  // Number of packets in block.
57  size_t block_size_;
58 
59  // Output sequence.
60  core::Array<size_t> send_seq_;
61 
62  // Delay line.
63  core::Array<PacketPtr> packets_;
64 
65  size_t next_2_put_;
66  size_t next_2_send_;
67 
68  bool valid_;
69 };
70 
71 } // namespace packet
72 } // namespace roc
73 
74 #endif // ROC_PACKET_INTERLEAVER_H_
Dynamic array.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Memory arena interface.
Definition: iarena.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Packet writer interface.
Definition: iwriter.h:23
Interleaves packets to transmit them in pseudo random order.
Definition: interleaver.h:25
virtual ROC_ATTR_NODISCARD status::StatusCode write(const PacketPtr &packet)
Write next packet.
Interleaver(IWriter &writer, core::IArena &arena, size_t block_size)
Initialize.
size_t block_size() const
Maximum delay between writing packet and moment we get it in output in terms of packets number.
bool is_valid() const
Check if object is successfully constructed.
ROC_ATTR_NODISCARD status::StatusCode flush()
Send all buffered packets to output writer.
Memory arena interface.
Packet writer interface.
Root namespace.
Non-copyable object.
Packet.
StatusCode
Status code.
Definition: status_code.h:19