This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForwarder.cpp
More file actions
265 lines (209 loc) · 6.31 KB
/
Forwarder.cpp
File metadata and controls
265 lines (209 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "Forwarder.h"
#include <cassert>
#include <map>
#include <glib.h>
#include <CxxPtr/CPtr.h>
#include "RtspParser/Common.h"
#include "Config.h"
#include "FrontSession.h"
#include "BackSession.h"
#include "Log.h"
static const auto Log = SignalingServerLog;
namespace {
struct BackSessionData {
BackSession *const backSession;
rtsp::Parameters list;
};
}
// FIXME! need some protection from different session on the same address
struct Forwarder::Private
{
Private(const Config&);
const Config& config;
std::map<std::string, BackSessionData> backSessions; // uri -> BackSessionData
std::string cachedAllSourcesList;
};
Forwarder::Private::Private(const Config& config) :
config(config)
{
}
Forwarder::Forwarder(const Config& config) :
_p(std::make_unique<Private>(config))
{
}
Forwarder::~Forwarder()
{
}
const Config& Forwarder::config() const
{
return _p->config;
}
std::unique_ptr<FrontSession>
Forwarder::createFrontSession(
const std::function<void (const rtsp::Request*) noexcept>& sendRequest,
const std::function<void (const rtsp::Response*) noexcept>& sendResponse) noexcept
{
return
std::make_unique<FrontSession>(
this, sendRequest, sendResponse);
}
std::unique_ptr<BackSession>
Forwarder::createBackSession(
const std::function<void (const rtsp::Request*) noexcept>& sendRequest,
const std::function<void (const rtsp::Response*) noexcept>& sendResponse) noexcept
{
return
std::make_unique<BackSession>(
this, sendRequest, sendResponse);
}
bool Forwarder::registerBackSession(
const std::string& name,
const std::string& token,
BackSession* session)
{
const AuthTokens& backAuthTokens = config().backAuthTokens;
const auto it = backAuthTokens.find(name);
if(backAuthTokens.end() == it) {
Log()->error("[Forwarder] Unknown streaming source \"{}\".", name);
return false;
}
if(it->second != token) {
Log()->error("[Forwarder] Invalid token for streaming source \"{}\".", name);
return false;
}
const auto pair = _p->backSessions.emplace(name, BackSessionData{ session });
if(!pair.second) {
Log()->error("[Forwarder] Streaming source \"{}\" already registered.", name);
return false;
}
Log()->info("[Forwarder] Streaming source \"{}\" registered.", name);
return true;
}
bool Forwarder::swapBackSessionSourcesList(
const std::string& name,
rtsp::Parameters* sourcesList)
{
assert(sourcesList);
const auto backSessionIt = _p->backSessions.find(name);
if(backSessionIt == _p->backSessions.end())
return false;
BackSessionData& data = backSessionIt->second;
if(!data.list.empty())
return false;
data.list.swap(*sourcesList);
clearCachedSourcesList();
return true;
}
void Forwarder::clearCachedSourcesList()
{
_p->cachedAllSourcesList.clear();
}
const std::string& Forwarder::allSourcesList()
{
if(!_p->cachedAllSourcesList.empty())
return _p->cachedAllSourcesList;
for(const auto& sessionPair: _p->backSessions) {
const std::string& name = sessionPair.first;
const BackSessionData& data = sessionPair.second;
for(const auto& sourcePair: data.list) {
const std::string& sourceName = name + "/" + sourcePair.first;
const std::string& sourceDescription = sourcePair.first;
CharPtr escapedNamePtr(
g_uri_escape_string(sourceName.data(), "/", false));
if(!escapedNamePtr) {
static std::string emptyList;
return emptyList; // insufficient memory?
}
_p->cachedAllSourcesList += escapedNamePtr.get();
_p->cachedAllSourcesList += ": " + sourceDescription + "\r\n";
}
}
if(_p->cachedAllSourcesList.empty())
_p->cachedAllSourcesList = "\r\n";
return _p->cachedAllSourcesList;
}
void Forwarder::removeBackSession(
const std::string& name,
BackSession* session)
{
_p->backSessions.erase(name);
clearCachedSourcesList();
}
void Forwarder::registerMediaSession(
FrontSession* frontSession,
const rtsp::SessionId& frontMediaSession,
BackSession* backSession,
const rtsp::SessionId& backMediaSession)
{
backSession->registerMediaSession(frontSession, frontMediaSession, backMediaSession);
}
void Forwarder::unregisterMediaSession(
FrontSession* frontSession,
const rtsp::SessionId& frontMediaSession,
BackSession* backSession,
const rtsp::SessionId& backMediaSession)
{
backSession->unregisterMediaSession(frontSession, frontMediaSession, backMediaSession);
}
bool Forwarder::forwardToBackSession(
FrontSession* source,
BackSession* target,
std::unique_ptr<rtsp::Request>& requestPtr)
{
std::string sourceName = requestPtr->uri;
const std::string::size_type spliterPos = sourceName.find('/');
if(spliterPos != std::string::npos)
sourceName.resize(spliterPos);
const auto backSessionIt = _p->backSessions.find(sourceName);
if(backSessionIt == _p->backSessions.end())
return false;
BackSession* target2 = backSessionIt->second.backSession;
if(target && target != target2)
return false;
return target2->forward(source, requestPtr);
}
bool Forwarder::forwardToBackSession(
BackSession* target,
const rtsp::Response& response)
{
return target->forward(response);
}
bool Forwarder::forwardToFrontSession(
BackSession* source,
FrontSession* target,
std::unique_ptr<rtsp::Request>& requestPtr)
{
return target->forward(source, requestPtr);
}
bool Forwarder::forwardToFrontSession(
BackSession* source,
FrontSession* target,
const rtsp::Request& request,
std::unique_ptr<rtsp::Response>& response)
{
return target->forward(source, request, response);
}
void Forwarder::cancelRequest(
BackSession* session,
const rtsp::CSeq& cseq)
{
session->cancelRequest(cseq);
}
void Forwarder::forceTeardown(
BackSession* session,
const rtsp::SessionId& mediaSession)
{
session->forceTeardown(mediaSession);
}
void Forwarder::cancelRequest(
FrontSession* session,
const rtsp::CSeq& cseq)
{
session->cancelRequest(cseq);
}
void Forwarder::forceTeardown(
FrontSession* session,
const rtsp::SessionId& mediaSession)
{
session->forceTeardown(mediaSession);
}