Mjpeg streaming not working on google chrome

vsx06 picture vsx06 · Sep 1, 2013 · Viewed 7.9k times · Source

I am a newbie to mjpeg streaming. I am trying to build an mjpeg streaming server application that streams mjpeg video to clients running firefox or google chrome. At the moment, the streaming works fine on firefox, but refuses to run on google chrome. Does anyone know why this might be? (I have downloaded the latest versions of google chrome and firefox for windows) Following is the code snippet from my C# class that writes the http headers and image stream (memory stream) to the network stream:

 /* 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);
        }   

And following is the code snippet that makes the appropriate method calls to write headers and image data to the network stream :

/* 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);
            }
    }

Answer

Andy Carvell picture Andy Carvell · Sep 3, 2013

I have the same problem (bug in Chrome 29). I've found that if you embed your motion jpeg stream in an html file, example:

<html><body>
<img src="http://192.168.1.77:8081">
</body></html>

You can now view the stream in Chrome 29.