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 pathReStreamAgent.cpp
More file actions
114 lines (95 loc) · 3.2 KB
/
ReStreamAgent.cpp
File metadata and controls
114 lines (95 loc) · 3.2 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
#include "ReStreamAgent.h"
#include <CxxPtr/GlibPtr.h>
#include "Client/WsClient.h"
#include "GstStreaming/GstTestStreamer.h"
#include "GstStreaming/GstReStreamer.h"
#include "Log.h"
#include "Session.h"
enum {
DEFAULT_RECONNECT_TIMEOUT = 5,
};
static const auto Log = AgentLog;
static std::unique_ptr<WebRTCPeer>
CreatePeer(
const Config* config,
const std::string& uri)
{
auto streamerIt = config->streamers.end();
if(uri == config->name) {
streamerIt = config->streamers.find(std::string());
} else if(uri.size() > config->name.size() + 1 &&
0 == uri.compare(0, config->name.size(), config->name) &&
uri[config->name.size()] == '/')
{
const std::string streamerName = uri.substr(config->name.size() + 1);
streamerIt = config->streamers.find(streamerName);
}
if(config->streamers.end() == streamerIt) {
Log()->error("Unknown URI \"{}\"", uri);
return std::unique_ptr<WebRTCPeer>();
}
const StreamerConfig& streamer = streamerIt->second;
switch(streamer.type) {
case StreamerConfig::Type::Test:
return std::make_unique<GstTestStreamer>(streamer.uri);
case StreamerConfig::Type::ReStreamer:
return std::make_unique<GstReStreamer>(streamer.uri);
default:
return std::unique_ptr<WebRTCPeer>();
}
}
static std::unique_ptr<rtsp::ServerSession> CreateSession (
const Config* config,
Session::Cache* cache,
const std::function<void (const rtsp::Request*) noexcept>& sendRequest,
const std::function<void (const rtsp::Response*) noexcept>& sendResponse) noexcept
{
return
std::make_unique<Session>(
config,
cache,
std::bind(CreatePeer, config, std::placeholders::_1),
sendRequest, sendResponse);
}
static void ClientDisconnected(
const Config* config,
client::WsClient* client) noexcept
{
const unsigned reconnectTimeout =
config->reconnectTimeout > 0 ?
config->reconnectTimeout :
DEFAULT_RECONNECT_TIMEOUT;
GSourcePtr timeoutSourcePtr(g_timeout_source_new_seconds(reconnectTimeout));
GSource* timeoutSource = timeoutSourcePtr.get();
g_source_set_callback(timeoutSource,
[] (gpointer userData) -> gboolean {
static_cast<client::WsClient*>(userData)->connect();
return false;
}, client, nullptr);
g_source_attach(timeoutSource, g_main_context_get_thread_default());
}
int ReStreamAgentMain(const Config& config)
{
GMainContextPtr clientContextPtr(g_main_context_new());
GMainContext* clientContext = clientContextPtr.get();
g_main_context_push_thread_default(clientContext);
GMainLoopPtr loopPtr(g_main_loop_new(clientContext, FALSE));
GMainLoop* loop = loopPtr.get();
Session::Cache sessionsCache;
client::WsClient client(
config.clientConfig,
loop,
std::bind(
CreateSession,
&config,
&sessionsCache,
std::placeholders::_1,
std::placeholders::_2),
std::bind(ClientDisconnected, &config, &client));
if(client.init()) {
client.connect();
g_main_loop_run(loop);
} else
return -1;
return 0;
}