【发布时间】:2017-02-25 14:23:39
【问题描述】:
在我当前的 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 Network 库在 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);
}
【问题讨论】:
-
反对者请详细说明
标签: c++ qt http networking http-headers