Kanishka Rakhecha
Kanishka Rakhecha

Reputation: 51

Multipart boundary in boost beast

I'm trying to stream frames from a video as mjpegs on a locally hosted server. The following is my code:

#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;
}

What I want to be able to do is make each frame appear without having to refresh the page to send a new request each time. I've come to realize I need to send a header with content type multipart/x-mixed-replace before sending each image buffer with mime type image/jpeg. But I can't figure out how to set end boundary(necessary for multipart, apparently) in my response in boost-beast. Or maybe my problems lie elsewhere. Any help/advice would be appreciated. Cheers!

Upvotes: 1

Views: 1721

Answers (1)

Kanishka Rakhecha
Kanishka Rakhecha

Reputation: 51

Here's the answer. We need to set boundary using 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;
}

Upvotes: 1

Related Questions