1 /**
2  * Copyright (c) 2020 Filip Klembara (in2core)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef RTC_MEDIA_HANDLER_ELEMENT_H
20 #define RTC_MEDIA_HANDLER_ELEMENT_H
21 
22 #if RTC_ENABLE_MEDIA
23 
24 #include "common.hpp"
25 #include "message.hpp"
26 #include "rtp.hpp"
27 
28 namespace rtc {
29 
30 using ChainedMessagesProduct = shared_ptr<std::vector<binary_ptr>>;
31 
32 RTC_CPP_EXPORT ChainedMessagesProduct make_chained_messages_product();
33 RTC_CPP_EXPORT ChainedMessagesProduct make_chained_messages_product(message_ptr msg);
34 
35 /// Ougoing messages
36 struct RTC_CPP_EXPORT ChainedOutgoingProduct {
37 	ChainedOutgoingProduct(ChainedMessagesProduct messages = nullptr,
38 	                       message_ptr control = nullptr);
39 	const ChainedMessagesProduct messages;
40 	const message_ptr control;
41 };
42 
43 /// Incoming messages with response
44 struct RTC_CPP_EXPORT ChainedIncomingProduct {
45 	ChainedIncomingProduct(ChainedMessagesProduct incoming = nullptr,
46 	                       ChainedMessagesProduct outgoing = nullptr);
47 	const ChainedMessagesProduct incoming;
48 	const ChainedOutgoingProduct outgoing;
49 };
50 
51 /// Incoming control messages with response
52 struct RTC_CPP_EXPORT ChainedIncomingControlProduct {
53 	ChainedIncomingControlProduct(message_ptr incoming,
54 	                              optional<ChainedOutgoingProduct> outgoing = nullopt);
55 	const message_ptr incoming;
56 	const optional<ChainedOutgoingProduct> outgoing;
57 };
58 
59 /// Chainable handler
60 class RTC_CPP_EXPORT MediaHandlerElement
61     : public std::enable_shared_from_this<MediaHandlerElement> {
62 	shared_ptr<MediaHandlerElement> upstream = nullptr;
63 	shared_ptr<MediaHandlerElement> downstream = nullptr;
64 
65 	void prepareAndSendResponse(optional<ChainedOutgoingProduct> outgoing,
66 	                            std::function<bool(ChainedOutgoingProduct)> send);
67 
68 	void removeFromChain();
69 
70 public:
71 	MediaHandlerElement();
72 
73 	/// Creates response to incoming message
74 	/// @param messages Current repsonse
75 	/// @returns New response
76 	optional<ChainedOutgoingProduct> processOutgoingResponse(ChainedOutgoingProduct messages);
77 
78 	// Process incoming and ougoing messages
79 	message_ptr formIncomingControlMessage(message_ptr message,
80 	                                       std::function<bool(ChainedOutgoingProduct)> send);
81 	ChainedMessagesProduct
82 	formIncomingBinaryMessage(ChainedMessagesProduct messages,
83 	                          std::function<bool(ChainedOutgoingProduct)> send);
84 	message_ptr formOutgoingControlMessage(message_ptr message);
85 	optional<ChainedOutgoingProduct> formOutgoingBinaryMessage(ChainedOutgoingProduct product);
86 
87 	/// Process current control message
88 	/// @param messages current message
89 	/// @returns Modified message and response
90 	virtual ChainedIncomingControlProduct processIncomingControlMessage(message_ptr messages);
91 
92 	/// Process current control message
93 	/// @param messages current message
94 	/// @returns Modified message
95 	virtual message_ptr processOutgoingControlMessage(message_ptr messages);
96 
97 	/// Process current binary message
98 	/// @param messages current message
99 	/// @returns Modified message and response
100 	virtual ChainedIncomingProduct processIncomingBinaryMessage(ChainedMessagesProduct messages);
101 
102 	/// Process current binary message
103 	/// @param messages current message
104 	/// @param control current control message
105 	/// @returns Modified binary message and control message
106 	virtual ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages,
107 	                                                            message_ptr control);
108 
109 	/// Set given element as upstream to this
110 	/// @param upstream Upstream element
111 	/// @returns Upstream element
112 	shared_ptr<MediaHandlerElement> chainWith(shared_ptr<MediaHandlerElement> upstream);
113 
114 	/// Remove all downstream elements from chain
115 	void recursiveRemoveChain();
116 };
117 
118 } // namespace rtc
119 
120 #endif // RTC_ENABLE_MEDIA
121 
122 #endif // RTC_MEDIA_HANDLER_ELEMENT_H
123