Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
router.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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/router.h
10 //! @brief Route packets to writers.
11 
12 #ifndef ROC_PACKET_ROUTER_H_
13 #define ROC_PACKET_ROUTER_H_
14 
15 #include "roc_core/array.h"
16 #include "roc_core/attributes.h"
17 #include "roc_core/iarena.h"
18 #include "roc_core/noncopyable.h"
19 #include "roc_core/stddefs.h"
20 #include "roc_packet/iwriter.h"
21 #include "roc_packet/packet.h"
22 #include "roc_packet/units.h"
23 
24 namespace roc {
25 namespace packet {
26 
27 //! Route packets to packet writers.
28 //!
29 //! To create a route, user provides packet writer and packet flags.
30 //! Packets that include specified flags will be routed to given writer.
31 //!
32 //! When the very first packet is routed to a writer, router remembers
33 //! which source id (SSRC) that packet has, or that the packet doesn't
34 //! have any source id. Then router ensures that only packets with
35 //! that source id are passed to same writer.
36 //!
37 //! The user can query which source id were detected for which routes.
38 class Router : public IWriter, public core::NonCopyable<> {
39 public:
40  //! Initialize.
42 
43  //! Add route.
44  //! @remarks
45  //! Packets that has given @p flags set will be routed to @p writer.
46  ROC_ATTR_NODISCARD bool add_route(IWriter& writer, unsigned flags);
47 
48  //! Check if there is detected source id for given route.
49  //! @remarks
50  //! Returns true if there is route for given flags, and packets were
51  //! already written to that route, and those packets have source id.
52  bool has_source_id(unsigned flags);
53 
54  //! Get detected source id for given route.
55  //! @remarks
56  //! If has_source_id() returns true, this method returns source id
57  //! for the route.
58  stream_source_t get_source_id(unsigned flags);
59 
60  //! Write next packet.
61  //! @remarks
62  //! Route @p packet to a writer or drop it if no routes found.
64 
65 private:
66  struct Route {
67  IWriter* writer;
68  unsigned flags;
69  stream_source_t source;
70  bool has_source;
71  bool is_started;
72 
73  Route()
74  : writer(NULL)
75  , flags(0)
76  , source(0)
77  , has_source(false)
78  , is_started(false) {
79  }
80  };
81 
82  Route* find_route_(unsigned flags);
83  bool allow_route_(Route& route, const Packet& packet);
84 
85  core::Array<Route, 2> routes_;
86 };
87 
88 } // namespace packet
89 } // namespace roc
90 
91 #endif // ROC_PACKET_ROUTER_H_
Dynamic array.
Compiler attributes.
#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
Route packets to packet writers.
Definition: router.h:38
Router(core::IArena &arena)
Initialize.
virtual ROC_ATTR_NODISCARD status::StatusCode write(const PacketPtr &packet)
Write next packet.
stream_source_t get_source_id(unsigned flags)
Get detected source id for given route.
ROC_ATTR_NODISCARD bool add_route(IWriter &writer, unsigned flags)
Add route.
bool has_source_id(unsigned flags)
Check if there is detected source id for given route.
Memory arena interface.
Packet writer interface.
uint32_t stream_source_t
Packet stream identifier.
Definition: units.h:27
Root namespace.
Non-copyable object.
Packet.
StatusCode
Status code.
Definition: status_code.h:19
Commonly used types and functions.
Various units used in packets.