我是一个对mjpeg流媒体新手。我正在尝试构建一个mjpeg流媒体服务器应用程序,向运行firefox或google chrome的客户端流式传输mjpeg视频。目前,在firefox上流媒体工作正常,但在google chrome上却无法运行。有人知道这可能是为什么吗?(我已经下载了最新版本的Windows版google chrome和firefox)以下是来自我的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);
}
}