2017-02-25 35 views
-1

在我當前的項目中C++/Qt,我正在試圖製作一個文件服務器來流式傳輸視頻。我可以將視頻從應用流式傳輸到Chrome,但我無法將其流式傳輸給其他玩家(例如VLC)。我試過其他圖書館,他們工作,但我的工作不起作用。 我想這是頭問題。這裏是頭 -視頻可以在Chrome上流式傳輸,但不能在其他播放器中播放。

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 

我開發的應用程序在C++中使用Qt網絡庫和代碼非常簡單。基本上它是發送上述標題,然後是視頻。 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

下載者請詳細說明 – FadedCoder

回答

1

不要在代碼中使用QDataStream。只需創建QByteArray,附加您的標題,並通過發送clientConnection->write()

相關問題