A very simple, fast, multithreaded, platform independent HTTP and HTTPS server and client library implemented using C++11 and Boost.Asio. Created to be an easy way to make REST resources available from C++ applications.
This was based on code found at https://github.com/aanon4/RobotVision -- they just pulled in Simple-Web-Server and modified it locally rather than pushing it back as a pull request.
and image update for openCV:
class MJPEGServer {
SimpleWeb::Server server;
std::mutex streamMutex;
std::condition_variable streamCv;
std::vector vJPEG;
unsigned int active = 0;
void setStreamJPEG(cv::Mat &streamMat) {
// only do the work if we are actively streaming
if (active > 0 && streamMutex.try_lock())
{
cv::imencode(".jpg", streamMat, vJPEG, std::vector());
streamCv.notify_all();
streamMutex.unlock();
}
}
}
This was based on code found at https://github.com/aanon4/RobotVision -- they just pulled in Simple-Web-Server and modified it locally rather than pushing it back as a pull request.
MJPEG resource: // mjpeg stream resource server.resource["^/stream"]["GET"] = [this](std::shared_ptr<SimpleWeb::Server::Response> response,
std::shared_ptr<SimpleWeb::Server::Request> request) {
response->set_nodelay();
std::thread streamThread([this, response] {
class MJPEG {
public:
static void send_image(RestServer server, const std::shared_ptr<SimpleWeb::Server::Response> &response) {
std::unique_lock lk(server->streamMutex);
server->streamCv.wait(lk);
int size = (int)server->vJPEG.size();
response->write_more("--mjpegstream\r\n");
response->write_more("Content-Type: image/jpeg\r\n");
response->write_more(str(boost::format("Content-Length: %1%\r\n\r\n") % size));
response->write((const char )server->vJPEG.data(), size);
lk.unlock();
response->write_more("\r\n");
response->send([server, response](const SimpleWeb::error_code &ec) {
if (!ec) {
send_image(server, response);
}
else {
// Done
if (server->active > 0) {
server->active--;
}
}
});
}
};
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "multipart/x-mixed-replace;boundary=mjpegstream");
headers.emplace("Connection", "close");
headers.emplace("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
headers.emplace("Content-Length", "-1");
response->write(SimpleWeb::StatusCode::success_ok, headers);
active++;
MJPEG::send_image(this, response);
});
streamThread.detach();
};
and image update for openCV: class MJPEGServer { SimpleWeb::Server server;
std::mutex streamMutex;
std::condition_variable streamCv;
std::vector vJPEG;
unsigned int active = 0;
void setStreamJPEG(cv::Mat &streamMat) { // only do the work if we are actively streaming if (active > 0 && streamMutex.try_lock()) { cv::imencode(".jpg", streamMat, vJPEG, std::vector());
streamCv.notify_all();
streamMutex.unlock();
}
}
}