-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiveRTSPServer.hh
130 lines (111 loc) · 3.57 KB
/
LiveRTSPServer.hh
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
#pragma once
#include <vector>
#include <memory>
#include <thread>
#include <unistd.h>
#include <fcntl.h>
#include <mutex>
#include <atomic>
#include <map>
#include <functional>
#include <condition_variable>
#include <iostream>
#include "mcore/unique_fd.h"
#include "easyloggingpp/easylogging++.h"
#include "RTSPServer.hh"
#include "BasicUsageEnvironment.hh"
#include "GroupsockHelper.hh"
#include "Media.hh"
#include "LiveMediaFactory.hh"
namespace LiveRTSP {
class LiveRTSPServer {
public:
static std::unique_ptr<LiveRTSPServer> MakeLiveRTSPServer(Port ourPort, unsigned reclamationSeconds, bool log_debug = false);
~LiveRTSPServer();
LiveRTSPServer(const LiveRTSPServer &) = delete;
LiveRTSPServer& operator=(const LiveRTSPServer &) = delete;
bool Start();
bool Stop();
// TODO:
// message string builder
// reliable control, control sync
// result notify callback
// cancelable
bool Control(const std::string &msg);
protected:
LiveRTSPServer(bool log_debug);
private:
class RTSPServerImpl : public RTSPServer {
public:
static RTSPServerImpl *MakeRTSPServerImpl(UsageEnvironment &env,
Port ourPort, UserAuthenticationDatabase *authDatabase, unsigned reclamationSeconds);
void Close();
protected:
RTSPServerImpl(UsageEnvironment &env, int ourSocketIPv4, int ourSocketIPv6, Port ourPort,
UserAuthenticationDatabase *authDatabase, unsigned reclamationSeconds);
~RTSPServerImpl();
};
struct RTSPServerImplDeleter {
void operator()(RTSPServerImpl *impl) {
LOG(INFO) << "Destroy RTSPServer";
// Media::close will finally delete this
impl->Close();
}
};
struct UsageEnvironmentDeleter {
void operator()(UsageEnvironment *env) {
if (!env->reclaim())
LOG(ERROR) << "-->reclaiming fail\n";
}
};
using RTSPServerImplUnique = std::unique_ptr<RTSPServerImpl, RTSPServerImplDeleter>;
using UsageEnvUnique = std::unique_ptr<UsageEnvironment, UsageEnvironmentDeleter>;
private:
static void LiveTask(LiveRTSPServer *livertsp);
static void ControlMethodDispatch(LiveRTSPServer *livertsp, int mask);
bool Initialize(Port ourPort, unsigned reclamationSeconds);
bool Poking(std::vector<uint8_t> &messageBuf);
/**
* info
*
* @param kv ignore
*/
bool ControlMethodInfo(const std::map<std::string, std::string> &kv);
/**
* stop
*
* @param kv ignore
*/
bool ControlMethodStop(const std::map<std::string, std::string> &kv);
/**
* add_session
*
* @param kv name= input=ffmpeg video= audio=
*/
bool ControlMethodAddSession(const std::map<std::string, std::string> &kv);
/**
* del_session
*
* @param kv name=
*/
bool ControlMethodDelSession(const std::map<std::string, std::string> &kv);
// RAII
static uint32_t genid;
bool start;
char stoppedFlag;
mcore::unique_fd pipe_r;
mcore::unique_fd pipe_w;
std::thread liveThread;
std::mutex startMutex;
std::condition_variable startCondVar;
std::unique_ptr<TaskScheduler> scheduler;
UsageEnvUnique env;
RTSPServerImplUnique rtspServer;
std::vector<uint8_t> messageBuf;
using ControlHandlerKVMap = std::map<std::string, std::string>;
using ControlHandler = std::function<bool (const std::map<std::string,std::string> &keyval)>;
std::map<std::string, ControlHandler> controlMethodHandlerMap;
bool debug;
LiveMediaFactory mediaFactory;
};
}