gpt4 book ai didi

c++ - boost 野兽中的多部分边界

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:06 24 4
gpt4 key购买 nike

我正在尝试在本地托管的服务器上将视频中的帧作为 mjpeg 流式传输。以下是我的代码:

#include <iostream>
#include <string>
#include <memory>
#include <thread>
#include <cstdlib>

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/config.hpp>

using boost::asio::ip::tcp;
namespace http = boost::beast::http;

cv::Mat frame;
std::vector<uchar> buffer;

int main(){
try{
boost::asio::io_context io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));
boost::system::error_code err;
tcp::socket socket(io_service);
acceptor.accept(socket);

boost::beast::flat_buffer request_buffer;

cv::VideoCapture cap("zz.mp4");

// http::request<http::string_body> req;
// http::read(socket, request_buffer, req, err);
// if(err){
// std::cerr << "read: " << err.message() << "\n";
// }

while(cap.isOpened()){
cap >> frame;
cv::imencode(".jpg", frame, buffer, std::vector<int> {cv::IMWRITE_JPEG_QUALITY, 95});

auto const size = buffer.size();

http::request<http::string_body> req;
http::read(socket, request_buffer, req, err);
if(err){
std::cerr << "read: " << err.message() << "\n";
break;
}

http::response<http::empty_body> res{http::status::ok, req.version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
// res.set(http::field::content_type, "multipart/x-mixed-replace; boundary=frame\r\n\r\n--frame\n");
res.set(http::field::content_type, "multipart/x-mixed-replace; boundary=frame");
res.content_length(size);
http::response_serializer<http::empty_body> sr{res};
http::write_header(socket, sr);
while(true){
http::response<http::vector_body<unsigned char>> res{std::piecewise_construct,
std::make_tuple(std::move(buffer)),
std::make_tuple(http::status::ok, req.version())};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "image/jpeg");
res.content_length(size);
res.keep_alive(req.keep_alive());
http::write(socket, res, err);
}
}
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return 0;
}

我想做的是让每一帧都出现,而不必每次都刷新页面发送新的请求。我开始意识到,在发送每个具有 MIME 类型 image/jpeg 的图像缓冲区之前,我需要发送一个内容类型为 multipart/x-mixed-replace 的 header 。但是我无法弄清楚如何在我对 boost-beast 的响应中设置结束边界(显然是多部分所必需的)。或者也许我的问题在别处。任何帮助/建议将不胜感激。干杯!

最佳答案

答案在这里。我们需要使用 boost::beast::http::field::body

设置边界
#include <iostream>
#include <string>
#include <memory>
#include <thread>
#include <cstdlib>

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/config.hpp>

using boost::asio::ip::tcp;
namespace http = boost::beast::http;

cv::Mat frame;
std::vector<uchar> buffer;
cv::Mat grayframe;

int main(){
try{
boost::asio::io_context io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));
boost::system::error_code err;
tcp::socket socket(io_service);
acceptor.accept(socket);

boost::beast::flat_buffer request_buffer;

cv::VideoCapture cap("zz.mp4");

http::request<http::string_body> req;
http::read(socket, request_buffer, req, err);
if(err){
std::cerr << "read: " << err.message() << "\n";
}

http::response<http::empty_body> res{http::status::ok, req.version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "multipart/x-mixed-replace; boundary=frame");
res.keep_alive();
http::response_serializer<http::empty_body> sr{res};
http::write_header(socket, sr);

while(cap.isOpened()){
cap >> frame;
cv::cvtColor(frame, grayframe, CV_BGR2GRAY);
cv::imencode(".jpg", grayframe, buffer, std::vector<int> {cv::IMWRITE_JPEG_QUALITY, 95});

auto const size = buffer.size();

http::response<http::vector_body<unsigned char>> res{std::piecewise_construct,
std::make_tuple(std::move(buffer)),
std::make_tuple(http::status::ok, req.version())};
res.set(http::field::body, "--frame");
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "image/jpeg");
res.content_length(size);
res.keep_alive(req.keep_alive());
http::write(socket, res, err);
}
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return 0;
}

关于c++ - boost 野兽中的多部分边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58534425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com