|
|
@@ -18,6 +18,7 @@ public:
|
|
|
string host;
|
|
|
int port;
|
|
|
std::atomic<bool> stopping{false}; // 退出标记: 让MJPEG推流循环及时结束
|
|
|
+ std::atomic<bool> mjpeg_enabled{true}; // MJPEG推流开关
|
|
|
unordered_map<string, function<string()>> get_handlers;
|
|
|
unordered_map<string, function<string(const map<string,string>&)>> getq_handlers;
|
|
|
unordered_map<string, function<string(const string&)>> post_handlers;
|
|
|
@@ -227,6 +228,10 @@ public:
|
|
|
|
|
|
svr.Get("/mjpeg/:ch", [this](const httplib::Request& req, httplib::Response& res) {
|
|
|
if (!authed(req)) { denyJson(res); return; }
|
|
|
+ if (!mjpeg_enabled.load()) {
|
|
|
+ res.set_content("stream disabled", "text/plain");
|
|
|
+ return;
|
|
|
+ }
|
|
|
int ch = 0;
|
|
|
try { ch = stoi(req.path_params.at("ch")); } catch (...) {}
|
|
|
auto fp = frame_providers.find(ch);
|
|
|
@@ -241,7 +246,7 @@ public:
|
|
|
"multipart/x-mixed-replace; boundary=mjpegboundary",
|
|
|
[this, fp, seqfn](size_t, httplib::DataSink& sink) -> bool {
|
|
|
uint64_t last_seq = 0;
|
|
|
- while (!stopping.load() && sink.is_writable()) {
|
|
|
+ while (!stopping.load() && mjpeg_enabled.load() && sink.is_writable()) {
|
|
|
uint64_t seq = seqfn ? seqfn() : 0;
|
|
|
if (seq != 0 && seq == last_seq) {
|
|
|
// 帧未更新, 跳过重复JPEG编码
|
|
|
@@ -509,6 +514,20 @@ public:
|
|
|
if (it != post_handlers.end())
|
|
|
res.set_content(it->second(req.body), "application/json");
|
|
|
});
|
|
|
+
|
|
|
+ svr.Get("/api/mjpeg/status", [this](const httplib::Request& req, httplib::Response& res) {
|
|
|
+ if (!authed(req)) { denyJson(res); return; }
|
|
|
+ auto it = get_handlers.find("/api/mjpeg/status");
|
|
|
+ if (it != get_handlers.end())
|
|
|
+ res.set_content(it->second(), "application/json");
|
|
|
+ });
|
|
|
+
|
|
|
+ svr.Post("/api/mjpeg/toggle", [this](const httplib::Request& req, httplib::Response& res) {
|
|
|
+ if (!authed(req)) { denyJson(res); return; }
|
|
|
+ auto it = post_handlers.find("/api/mjpeg/toggle");
|
|
|
+ if (it != post_handlers.end())
|
|
|
+ res.set_content(it->second(req.body), "application/json");
|
|
|
+ });
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -536,6 +555,14 @@ void WebServer::stop() {
|
|
|
running_ = false;
|
|
|
}
|
|
|
|
|
|
+void WebServer::setMjpegEnabled(bool en) {
|
|
|
+ if (impl_) impl_->mjpeg_enabled.store(en);
|
|
|
+}
|
|
|
+
|
|
|
+bool WebServer::mjpegEnabled() const {
|
|
|
+ return impl_ ? impl_->mjpeg_enabled.load() : true;
|
|
|
+}
|
|
|
+
|
|
|
void WebServer::setGetHandler(const string& path, function<string()> handler) {
|
|
|
impl_->get_handlers[path] = handler;
|
|
|
}
|