C ++ pistache和MJPEG服务器

时间:2019-02-25 15:30:34

标签: c++ mjpeg

我正在使用REST C ++框架Pistache(pistache.2019.02.01.zip,http://pistache.io/)。

我的可执行文件提供了图像,我想创建一个MJPEG流。

到目前为止,我的实现如下所示:

void PistMJPEGServer::serveMJPEG(const Rest::Request& request, Http::ResponseWriter response)
{

std::shared_ptr<Http::ResponseWriter> shared_response = std::make_shared<Http::ResponseWriter>(std::move(response));
// the "HandleMJPEG" function is declared as part of my class
handleMJPEG = [shared_response, this](ssize_t bytes)
{
    shared_response->headers()
        .add<Http::Header::Connection>(Http::ConnectionControl::Close)
        .add<Http::Header::CacheControl>(Http::CacheDirective::NoCache)
        ;
        shared_response->send(
            Http::Code::Ok,
            jpegBoundary
       ).then(
            [shared_response, this](ssize_t bytes)
            {
            shared_response->headers()
                .add<Http::Header::Connection>(Http::ConnectionControl::Close)
                .add<Http::Header::CacheControl>(Http::CacheDirective::NoCache)
            ;   
                // this method is mine, it returns an image as a buffer
                auto buf = this->getImage();
                shared_response->send(
                    Http::Code::Ok,
                    std::string{buf.begin(), buf.end()},
                    MIME(Image, Jpeg)
                ).then(
                    this->handleMJPEG,
                    Async::Throw
                )
                ;
            },
            Async::Throw
        );
    };

    shared_response->headers()
        .add<Http::Header::Connection>(Http::ConnectionControl::Close)
        .add<Http::Header::CacheControl>(Http::CacheDirective::NoCache)
    ;
    shared_response->send(
    Http::Code::Ok,
    "",
    Http::Mime::MediaType::fromString(
        boost::str(boost::format("multipart/x-mixed-replace; boundary=%1%") % jpegBoundary)
        )
    )
    .then(
        handleMJPEG,
        Async::Throw
    )
    ;

}

递归循环效果很好,但是我在浏览器中看不到任何图像。实际上,我没有得到我期望的Http :: ConnectionControl :: Close和Http :: CacheDirective :: NoCache ...如果尝试添加更多标头,则看不到它们,否则它们会混淆(如果需要,我可以提供示例。)

我的状态很好,还是Pistache无法完成我想实现的目标?我以前使用boost :: asio实现,但是我更愿意使用Pistache,而且使用起来更简单。

1 个答案:

答案 0 :(得分:1)

上周,我处理了标头,并尝试纠正错误的Connection HTTP标头中的默认行为。您可以再次检查并尝试使用更早的提交来实现。记住此库为pre-alpha版本。您不应该在生产中使用它。