-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamer.cpp
More file actions
107 lines (90 loc) · 3.08 KB
/
Streamer.cpp
File metadata and controls
107 lines (90 loc) · 3.08 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
#include "Streamer.h"
#include <thread>
#include <memory>
#include <functional>
#include <CxxPtr/GlibPtr.h>
#include "RtStreaming/GstRtStreaming/LibGst.h"
#include "RtStreaming/GstRtStreaming/GstClient.h"
#include "WebRTSP/Client/Log.h"
#include "WebRTSP/Client/WsClient.h"
#include "RtStreaming/GstRtStreaming/GstTestStreamer.h"
#include "RtStreaming/GstRtStreaming/GstReStreamer.h"
#include "Session.h"
#include "OnvifSession.h"
enum {
RECONNECT_TIMEOUT = 10,
};
static std::unique_ptr<WebRTCPeer> CreateClientPeer(const Config& config, const std::string& uri)
{
switch(config.streamer.type) {
case StreamerConfig::Type::Test:
return std::make_unique<GstTestStreamer>(config.streamer.source);
case StreamerConfig::Type::ReStreamer:
return std::make_unique<GstReStreamer>(uri, std::string());
case StreamerConfig::Type::OnvifReStreamer:
return std::make_unique<GstReStreamer>(uri, std::string());
default:
return nullptr;
}
}
static std::unique_ptr<rtsp::Session> CreateClientSession (
const Config& config,
const rtsp::Session::SendRequest& sendRequest,
const rtsp::Session::SendResponse& sendResponse) noexcept
{
if(config.streamer.type == StreamerConfig::Type::OnvifReStreamer) {
return
std::make_unique<OnvifSession>(
config,
std::bind(CreateClientPeer, std::ref(config), std::placeholders::_1),
sendRequest,
sendResponse);
} else {
return
std::make_unique<Session>(
config,
std::bind(CreateClientPeer, std::ref(config), std::placeholders::_1),
sendRequest,
sendResponse);
}
}
static void ClientDisconnected(client::WsClient& client) noexcept
{
GSourcePtr timeoutSourcePtr(g_timeout_source_new_seconds(RECONNECT_TIMEOUT));
GSource* timeoutSource = timeoutSourcePtr.get();
g_source_set_callback(timeoutSource,
[] (gpointer userData) -> gboolean {
static_cast<client::WsClient*>(userData)->connect();
return false;
}, &client, nullptr);
GMainContext* threadContext = g_main_context_get_thread_default();
g_source_attach(timeoutSource, threadContext ? threadContext : g_main_context_default());
}
int StreamerMain(const Config& config, bool useGlobalDefaultContext)
{
LibGst libGst;
GMainContextPtr contextPtr(
useGlobalDefaultContext ?
g_main_context_ref(g_main_context_default()) :
g_main_context_new());
GMainContext* context = contextPtr.get();
if(!useGlobalDefaultContext) {
g_main_context_push_thread_default(context);
}
GMainLoopPtr loopPtr(g_main_loop_new(context, FALSE));
GMainLoop* loop = loopPtr.get();
client::WsClient client(
config,
loop,
std::bind(
CreateClientSession,
std::ref(config),
std::placeholders::_1,
std::placeholders::_2),
ClientDisconnected);
if(client.init()) {
client.connect();
g_main_loop_run(loop);
}
return 0;
}