I want to create a Http Server to send an MJPEG Stream. I'm Already able to send an Image but no Live-Stream.
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...
Then I run following Code
QString inbound = m_Client->readAll();
QByteArray header = "HTTP/1.1 200 OK
";
m_Client->write(header);
QByteArray ContentType = "Content-Type: image/jpeg
";
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);
}
What am I missing? Is the Coding wrong or the way i Handle The Request? Perhaps mime-types?
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
"
"Server: YourServerName
"
"Connection: close
"
"Max-Age: 0
"
"Expires: 0
"
"Cache-Control: no-cache, private
"
"Pragma: no-cache
"
"Content-Type: multipart/x-mixed-replace; "
"boundary=--BoundaryString
");
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
"
"Content-type: image/jpg
");
m_Client->write(BoundaryString);
m_Client->write(ba); // Write The Encoded Image
QThread::usleep(500);
}
m_Client->close();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…