在我目前的项目C ++ / Qt中,我正在尝试制作一个文件服务器来传输视频。我可以将视频从应用流式传输到Chrome,但我无法将其流式传输到其他播放器(例如 - VLC)。我尝试过其他库,但是我的工作正常。 我想这是标题的问题。这是标题 -
HTTP/1.0 200 OK\r\n
Content-Length: VIDEO-SIZE\r\n
Content-Type: video/mp4\r\n\r\n
我也支持部分视频支持(使用搜索功能),这里是标题 -
HTTP/1.0 200 OK\r\n
Content-Length: VIDEO-SIZE\r\n
Content-Type: video/mp4\r\n
Content-Range: bytes RANGE/VIDEO-SIZE\r\n\r\n
我正在使用Qt网络库在C ++中开发应用程序,代码非常简单。基本上它的作用是发送上面的标题,然后发送视频。 CPP代码如下(不完全是一个好的代码,只是一个基本的草案)
QTcpSocket *clientConnection = server->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
clientConnection->waitForReadyRead();
QMap<QString, QString> requestMap;
while (!clientConnection->atEnd()) {
QString line(clientConnection->readLine());
qDebug()<<line;
if (line.indexOf(":") <= 0 || line.isEmpty())
continue;
line.replace("\r\n", "");
QString key(line.left(line.indexOf(":"))),
value(line.mid(line.indexOf(":") + 2, line.length()));
requestMap.insert(key, value);
qDebug() << "KEY: " << key << " VALUE: " << value << "\n";
}
img->open(QFile::ReadOnly);
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_8);
QString header, imgsize(QString::number(img->size()));
if(requestMap.contains("Range")){
QString range = requestMap["Range"];
range = range.mid(6, range.length()); // 'bytes=' is 6 chars
qint64 seek = range.left(range.indexOf("-")).toInt();
if (range.endsWith("-"))
range.append(QString::number(img->size() - 1));
header = "HTTP/1.0 206 PARTIAL CONTENT\r\n"
"Content-Length: "+imgsize+"\r\n"
"Content-Range: bytes "+range+"/"+imgsize + "\r\n"
"Content-Type: "+db.mimeTypeForFile(fileinfo).name()+"\r\n\r\n";
img->seek(seek);
} else header = "HTTP/1.0 200 OK\r\n"
"Content-Length: "+imgsize+"\r\n"
"Content-Type: "+db.mimeTypeForFile(fileinfo).name()+"\r\n\r\n";
out << header.toLatin1();
clientConnection->write(block);
clientConnection->waitForBytesWritten();
block.resize(65536);
while(!img->atEnd())
{
qint64 read = img->read(block.data(), 65536);
clientConnection->write(block, read);
}
答案 0 :(得分:1)
请勿在代码中使用QDataStream
。只需创建QByteArray
,附加标题,然后通过clientConnection->write()