How to Create a HTTP MJPEG Streaming Server With QTcp-Server Sockets?
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();
}
How to Create a Http Server with TCP Server Sockets in C++/QT
You have an extra space between the /
and 1.1
, and you are missing an empty line between the headers block and the response body.
Browser Connection Reset when using TCPServerwith Qt
After LOTS of trial and error (and luck), I found the solution. The code should end like this -
...
socket->flush();
socket->waitForBytesWritten(10000);
socket->close();
EDIT -
After trying again, I found out this - There should be an extra \r\n
after the headers. So this will work -
void TcpServer::newConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
QByteArray header = "HTTP/1.1 200 OK\r\n";
socket->write(header);
QByteArray ContentType = "Content-Type: text/html\r\n\r\n\*Here is the edit*\";
socket->write(ContentType);
QByteArray Body = "Test";
socket->write(Body);
socket->flush();
socket->close();
}
Error while using QTcpSocket
Here Is the Code I used to open the MJPEG Streaming Server in my original Project. Perhaps It can help you finding out your Problem.
void MjpegStreamingEngine::StartServer(){
m_TcpHttpServer = new QTcpServer();
m_TcpHttpServer->connect(m_TcpHttpServer,
SIGNAL(newConnection()),
this,
SLOT(TcpHttpconnected()));
m_TcpHttpServer->listen(QHostAddress::Any,
8889);
}
void MjpegStreamingEngine::TcpHttpconnected(){
m_TcpHttpClient = m_TcpHttpServer->nextPendingConnection();
m_TcpHttpClient->connect(m_TcpHttpClient,
SIGNAL(readyRead()),
this,
SLOT(TcpHttpreadyRead()));
}
void MjpegStreamingEngine::TcpHttpreadyRead(){
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){
if (m_TcpHttpClient->isOpen())
{
// Send Image
QThread::msleep(10);
}else{
return;
}
}
m_TcpHttpClient->disconnect(); //Should never be Reached
}
OpenCV create Output Stream
In the End I figured out that OpenCV is not capable of creating an Output-Live Video Stream. Its only possible to render a Video to File.
Instead i created my own little Http Motion-JPEG Stream
How to Create a HTTP MJPEG Streaming Server With QTcp-Server Sockets?
I also want to Mention that VLC provides a very powerful Backend / libs.
C++ LibVLC Create Stream from Frames/Images
Another Soloution could be to use GStreamer if you are on Linux
Related Topics
Array of Size Defined by Not Constant Variable
What Is the Size of Sizeof(Vector)? C++
Sigkill While Allocating Memory in C++
G++ Linker: Force Static Linking If Static Library Exists
Should I Use a C++ Reinterpret_Cast Over a C-Style Cast
String Plus Char - What Is Happening
What Does "-Wall" in "G++ -Wall Test.Cpp -O Test" Do
Get List of Static Libraries Used in an Executable
Is There a Safe Navigation Operator for C++
Breaking Out from Socket Select
C++ If Else If Not Working Properly
Why Can't I Use Inheritance to Implement an Interface in C++
Trying to Use a While Statement to Validate User Input C++
What's the Difference Between Sockaddr, Sockaddr_In, and Sockaddr_In6