-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainCtrl.cpp
More file actions
321 lines (291 loc) · 9.64 KB
/
MainCtrl.cpp
File metadata and controls
321 lines (291 loc) · 9.64 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "MainCtrl.h"
MainCtrl::MainCtrl()
: is_talking(false)
, msg_cb_thread(nullptr) {
}
MainCtrl::~MainCtrl() {
rpc->call({
{ "method", "logout" },
{ "token", token }
});
msg_cb_thread->join();
}
QString MainCtrl::serverIp() const {
return server_ip.c_str();
}
void MainCtrl::setServerIp(const QString & ip) {
server_ip = ip.toStdString();
if(!server_ip.empty() && !server_port.empty()) {
debug("server_init", server_ip, server_port);
rpc = make_unique<IPCClient>(server_ip, server_port);
}
}
QString MainCtrl::serverPort() const {
return server_port.c_str();
}
void MainCtrl::setServerPort(const QString & port) {
server_port = port.toStdString();
if(!server_ip.empty() && !server_port.empty()) {
debug("server_init", server_ip, server_port);
rpc = make_unique<IPCClient>(server_ip, server_port);
}
}
QString MainCtrl::getDeviceName(int idx) {
if(idx < 0 || idx >= devices.size()) return "";
return devices[idx].name.c_str();
}
bool MainCtrl::getDeviceIsPlaying(int idx) {
if(idx < 0 || idx >= devices.size()) return false;
return devices[idx].is_playing;
}
bool MainCtrl::getWindowIsPlayingAudio(int idx) {
if(idx < 0 || idx >= windows.size()) return false;
int window_idx = windows[idx].device_idx;
if(window_idx < 0 || window_idx >= devices.size()) return false;
return devices[window_idx].is_playing_audio;
}
int MainCtrl::getDeivceIdxByWindowIdx(int idx) {
if(idx < 0 || idx >= windows.size()) return -1;
return windows[idx].device_idx;
}
bool MainCtrl::getDeviceIsActive(int idx) {
if(idx < 0 || idx >= devices.size()) return false;
return devices[idx].is_active;
}
QString MainCtrl::getAllDeviceKey() {
return init_device_list.c_str();
}
int MainCtrl::addDevice(const QString & key) {
auto [ctx, ret] = rpc->call({
{ "method", "get_device_info" },
{ "user", token },
{ "key", key.toStdString() }
});
if(!ret) {
debug("添加设备失败");
return -1;
}
DeviceInfo info;
info.name = ctx.value("name", "未命名设备");
info.key = key.toStdString();
info.rtmp_url = ctx.value("rtmp_url", "");
info.encryption = ctx.value("encryption", "");
info.is_active = false;
info.is_playing = false;
info.is_playing_audio = false;
info.window_idx = -1;
devices.push_back(info);
return devices.size() - 1;
}
int MainCtrl::addWindow(VideoCtrl* videoCtrl) {
WindowInfo info;
info.is_playing = false;
info.device_idx = -1;
info.video_ctrl = videoCtrl;
windows.push_back(info);
return windows.size() - 1;
}
int MainCtrl::playVideo(int device_idx) {
debug("playVideo", device_idx);
if(device_idx < 0 || device_idx >= devices.size()) return -1;
// 获取设备
auto device = devices[device_idx];
// 设备不在线或设备正在播放
debug("device info:", device.is_active, device.is_playing, device.rtmp_url);
if(!device.is_active || device.is_playing) return -1;
// 获取窗口索引
int window_idx = findWindow();
if(window_idx == -1) return -1;
// 视频在播放则停止覆盖
if(windows[window_idx].is_playing) {
stopVideo(windows[window_idx].device_idx);
}
// 播放视频
bool ret = windows[window_idx].video_ctrl->play(device.rtmp_url.c_str(), device.encryption.c_str());
if(!ret) return -1;
// 更新信息
windows[window_idx].device_idx = device_idx;
windows[window_idx].is_playing = true;
devices[device_idx].is_playing = true;
devices[device_idx].window_idx = window_idx;
return window_idx;
}
void MainCtrl::playAudio(int window_idx) {
debug("playAudio", window_idx);
if(window_idx < 0 || window_idx >= windows.size()) return;
// 获取窗口
auto & window = windows[window_idx];
if(window.device_idx < 0 || window.device_idx >= devices.size()) return;
// 获取设备
auto & device = devices[window.device_idx];
// 开启音频
if(window.is_playing) {
window.video_ctrl->playAudio();
device.is_playing_audio = true;
}
}
void MainCtrl::stopVideo(int device_idx) {
debug("stopVideo", device_idx);
if(device_idx < 0 || device_idx >= devices.size()) return;
// 获取设备
auto & device = devices[device_idx];
if(device.window_idx < 0 || device.window_idx >= windows.size()) return;
// 获取窗口
auto & window = windows[device.window_idx];
// 关闭视频音频
window.video_ctrl->stop();
// 更新信息
window.is_playing = false;
window.device_idx = -1;
device.is_playing = false;
device.is_playing_audio = false;
device.window_idx = -1;
}
void MainCtrl::stopAudio(int window_idx) {
if(window_idx < 0 || window_idx >= windows.size()) return;
// 获取窗口
auto & window = windows[window_idx];
if(window.device_idx < 0 || window.device_idx >= devices.size()) return;
// 获取设备
auto & device = devices[window.device_idx];
// 关闭音频
if(window.is_playing) {
window.video_ctrl->stopAudio();
}
device.is_playing_audio = false;
}
int MainCtrl::findWindow() {
int idx = -1;
for(int i=0; i < windows.size(); ++i) {
idx = 0;
if(windows[i].is_playing) continue;
idx = i;
break;
}
debug("窗口idx:", idx);
return idx;
}
bool MainCtrl::startTalk(int window_idx) {
if(is_talking) return false;
if(window_idx < 0 || window_idx >= windows.size()) return false;
// 获取窗口
auto & window = windows[window_idx];
if(window.device_idx < 0 || window.device_idx >= devices.size()) return false;
// 获取设备
auto & device = devices[window.device_idx];
// 通知连接设备
auto [ctx, ret] = rpc->call({
{ "method", "talk_ctrl" },
{ "ctrl_type", "start" },
{ "device_key", device.key }
});
if(!ret) return false;
string req_talk_ret = ctx.value("result", "");
if(req_talk_ret != "success") {
debug("对讲开启失败, err_msg:", ctx.value("err_msg", ""));
return false;
}
talk_rtmp_push_url = ctx.value("rtmp_push_url", "");
is_talking = true;
return true;
}
void MainCtrl::stopTalk(int window_idx) {
if(!is_talking) return;
if(window_idx < 0 || window_idx >= windows.size()) return;
// 获取窗口
auto & window = windows[window_idx];
if(window.device_idx < 0 || window.device_idx >= devices.size()) return;
// 获取设备
auto & device = devices[window.device_idx];
// 通知连接设备
rpc->call({
{ "method", "talk_ctrl" },
{ "ctrl_type", "stop" },
{ "device_key", device.key }
});
is_talking = false;
}
QString MainCtrl::getTalkRtmpUrl() {
return talk_rtmp_push_url.c_str();
}
void MainCtrl::startMessageCallBack() {
msg_cb_thread = make_unique<std::thread>([this]() {
this->rpc->streamCall({
{ "method", "message_callback" },
{ "id", this->token }
},
[this](const string & msg) {
this->MessageHandle(msg);
});
});
}
void MainCtrl::MessageHandle(const string & msg) {
debug("receive msg:", msg);
json js_msg = json::parse(msg);
if(js_msg.value("reply", json()) != json()) {
json reply = js_msg.value("reply", json());
if(reply.value("operation", "") == "device status change") {
string device_id = reply.value("device_id", "");
string device_status = reply.value("device_status", "");
if(device_id != "" && device_status != "") {
for(auto & device : devices) {
if(device.key == device_id) {
device.is_active = device_status == "active" ? true : false;
debug("changed", device_id, device_status);
break;
}
}
}
}
}
}
QString MainCtrl::getRecordFile(int device_idx, QString begin_time, QString end_time) {
if(device_idx < 0 || device_idx >= devices.size()) return "";
// 获取设备
auto & device = devices[device_idx];
auto ret = rpc->recordDownload({
{ "key", device.key },
{ "begin_time", begin_time.toStdString() },
{ "end_time", end_time.toStdString() }
});
if(ret == nullopt) {
debug("录像文件获取失败");
return "";
}
return (*ret).c_str();
}
QString MainCtrl::getEncryption(int device_idx) {
if(device_idx < 0 || device_idx >= devices.size()) return "";
// 获取设备
auto & device = devices[device_idx];
return device.encryption.c_str();
}
QString MainCtrl::register_user(const QString & username, const QString & password) {
auto [reply,ret] = rpc->call({
{ "method", "register" },
{ "username", username.toStdString() },
{ "password", password.toStdString() }
});
if(ret && reply.value("msg", "failure") == "success") {
return "注册成功";
}
return reply.value("reason", "注册失败").c_str();
}
QString MainCtrl::login_user(const QString & username, const QString & password) {
auto [reply,ret] = rpc->call({
{ "method", "login" },
{ "username", username.toStdString() },
{ "password", password.toStdString() }
});
debug("login reply:", reply.dump());
if(ret && reply.value("msg", "failure") == "success") {
token = reply.value("token", "");
if(!token.empty()) {
startMessageCallBack();
auto device_list = reply.value("device_list", json::array());
init_device_list = device_list.dump();
}
return token == "" ? "登录失败" : "登录成功";
}
return "登录失败";
}