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 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/iallocator.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::IAllocator& allocator, size_t block_size);
32 
33  //! Check if object is successfully constructed.
34  bool 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.
40  virtual void write(const PacketPtr& packet);
41 
42  //! Send all buffered packets to output writer.
43  void flush();
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_
Memory allocator interface.
Definition: iallocator.h:23
Root namespace.
void flush()
Send all buffered packets to output writer.
Interleaver(IWriter &writer, core::IAllocator &allocator, size_t block_size)
Initialize.
Interleaves packets to transmit them in pseudo random order.
Definition: interleaver.h:25
Dynamic array.
Packet writer interface.
size_t block_size() const
Maximum delay between writing packet and moment we get it in output in terms of packets number...
Packet writer interface.
Definition: iwriter.h:21
Shared ownership intrusive pointer.
Definition: ownership.h:18
Base class for non-copyable objects.
Definition: noncopyable.h:23
bool valid() const
Check if object is successfully constructed.
virtual void write(const PacketPtr &packet)
Write next packet.
Packet.
Non-copyable object.
Memory allocator interface.