我是 mjpeg 流媒体的新手。我正在尝试构建一个 mjpeg 流服务器应用程序,将 mjpeg 视频流式传输到运行 firefox 或 google chrome 的客户端。目前,流媒体在 Firefox 上运行良好,但拒绝在 google chrome 上运行。有谁知道这可能是为什么?(我已经下载了最新版本的 google chrome 和 firefox for windows)以下是我的 C# 类中的代码片段,它将 http 标头和图像流(内存流)写入网络流:
/* Write the HTTP header to the network stream */
public void WriteHeader()
{
Write("HTTP/1.1 200 OK\r\n" +
"Content-Type: multipart/x-mixed-replace; boundary=" +
this.Boundary +
"\r\n"
);
this.Stream.Flush();
}
/* To write text to the stream */
private void Write(string text)
{
byte[] data = BytesOf(text);
this.Stream.Write(data, 0, data.Length);
}
/* Write header followed by the provided memory stream*/
public void Write(MemoryStream imageStream)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine(this.Boundary);
sb.AppendLine("Content-Type: image/jpeg");
sb.AppendLine("Content-Length: " + imageStream.Length.ToString());
sb.AppendLine();
Write(sb.ToString());
imageStream.WriteTo(this.Stream);
Write("\r\n");
this.Stream.Flush();
}
/* To get bytes from the from the specified string */
private static byte[] BytesOf(string text)
{
return Encoding.ASCII.GetBytes(text);
}
以下是进行适当方法调用以将标头和图像数据写入网络流的代码片段:
/* Sends data to the specified client */
private void SendData(Socket client)
{
MjpegWriter jw = new MjpegWriter(new NetworkStream(client, true));
jw.WriteHeader();
foreach (var memstream in this.ProcessImages())
{
Thread.Sleep(50);
jw.Write(memstream);
}
}