如何使用QTcp服务器套接字创建HTTP MJPEG流服务器? [英] How to Create a HTTP MJPEG Streaming Server With QTcp-Server Sockets?

查看:438
本文介绍了如何使用QTcp服务器套接字创建HTTP MJPEG流服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Http服务器来发送MJPEG流。我已经能够发送图片,但没有实时流。

I want to create a Http Server to send an MJPEG Stream. I'm Already able to send an Image but no Live-Stream.

我做了什么:创建了一个TCP服务器。当客户端连接TCP-Socket时创建。然后我实现了一个ReadyRead SLOT,当浏览器发送GET请求到服务器时,它执行。

What I did: Created an TCP-Server. When a client Connects a TCP-Socket is created. Then I implemented a ReadyRead SLOT which gots executed when the Browser sends the "GET" Request to the Server.

GET / HTTP/1.1
Host: 127.0.0.1:8889
User-Agent: Mozilla/5.0...

然后我运行下面的代码

QString inbound = m_Client->readAll();

QByteArray header = "HTTP/1.1 200 OK\r\n";
m_Client->write(header);

QByteArray ContentType = "Content-Type: image/jpeg\r\n\n";
m_Client->write(ContentType);

Mat first_img; //OPENCV Material
m_Stream->read(first_img); // Read Image from Webcam
QImage img = Mat2QImage(first_img); // Convert to QImage
QByteArray ba; // QByteArray as Buffer for JPG Envoded QImage
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "JPG");
m_Client->write(ba); // Write The Encoded Image

m_Client->close();

我想过创建一个循环来重复图像流部分
,但这不是工作。浏览器只是保持加载,什么也不发生....

I thought about creating a loop to repeat the Image Streaming Part but this doesn't work. The Browser Just keeps loading and nothing happens....

while(1){
    Mat first_img; //OPENCV Material
    m_Stream->read(first_img); // Read Image from Webcam

    QImage img = Mat2QImage(first_img); // Convert to QImage
    QByteArray ba; // QByteArray as Buffer for JPG Envoded QImage
    QBuffer buffer(&ba);
    buffer.open(QIODevice::WriteOnly);
    img.save(&buffer, "JPG");

    m_Client->write(ba); // Write The Encoded Image

    QThread::usleep(500);
}

我缺少什么?是编码错误还是我处理请求的方式?也许mime类型?

What am I missing? Is the Coding wrong or the way i Handle The Request? Perhaps mime-types?

更新
我看看

http://www.damonkohler.com/2010/ 10 / mjpeg-streaming-protocol.html

https:/ /en.wikipedia.org/wiki/Motion_JPEG
,并尝试执行这些方法,但没有任何结果....

Update I had a look at
http://www.damonkohler.com/2010/10/mjpeg-streaming-protocol.html and https://en.wikipedia.org/wiki/Motion_JPEG and tried to implement theses Methods but without any Results....

QString inbound = m_Client->readAll();

QByteArray ContentType = ("HTTP/1.0 200 OK\r\n" \
        "Server: YourServerName\r\n" \
        "Connection: close\r\n" \
        "Max-Age: 0\r\n" \
        "Expires: 0\r\n" \
        "Cache-Control: no-cache, private\r\n" \
        "Pragma: no-cache\r\n" \
        "Content-Type: multipart/x-mixed-replace; " \
        "boundary=--BoundaryString\r\n\r\n");
m_Client->write(ContentType);



while(1){
    Mat first_img; //OPENCV Material
    m_Stream->read(first_img); // Read Image from Webcam

    QImage img = Mat2QImage(first_img); // Convert to QImage
    QByteArray ba; // QByteArray as Buffer for JPG Envoded QImage
    QBuffer buffer(&ba);
    buffer.open(QIODevice::WriteOnly);
    img.save(&buffer, "JPG");

    QByteArray BoundaryString = ("--BoundaryString\r\n" \
                                 "Content-type: image/jpg\r\n\r\n");

    m_Client->write(BoundaryString);
    m_Client->write(ba); // Write The Encoded Image

    QThread::usleep(500);
}

m_Client->close();


推荐答案

b我只是调整一些协议相关的东西....

I solved it myself.... I just had to adjust some Protocol releated things....

m_TcpHttpClient->readAll(); // Discard "Get Request String"

QByteArray ContentType = ("HTTP/1.0 200 OK\r\n" \
                          "Server: en.code-bude.net example server\r\n" \
                          "Cache-Control: no-cache\r\n" \
                          "Cache-Control: private\r\n" \
                          "Content-Type: multipart/x-mixed-replace;boundary=--boundary\r\n\r\n");

m_TcpHttpClient->write(ContentType);


while(1){

    // Image to Byte Array via OPENCV Method
    std::vector<uchar> buff;
    imencode(".jpg",m_VisualEngine->GetActualFrame(),buff);
    std::string content(buff.begin(), buff.end());
    QByteArray CurrentImg(QByteArray::fromStdString(content));


    QByteArray BoundaryString = ("--boundary\r\n" \
                                 "Content-Type: image/jpeg\r\n" \
                                 "Content-Length: ");

    BoundaryString.append(QString::number(CurrentImg.length()));
    BoundaryString.append("\r\n\r\n");

    m_TcpHttpClient->write(BoundaryString);
    m_TcpHttpClient->write(CurrentImg); // Write The Encoded Image

    m_TcpHttpClient->flush();
}

这篇关于如何使用QTcp服务器套接字创建HTTP MJPEG流服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆