Search code examples
c#.netxnajpegmjpeg

How to Read a Local MJPEG File


I'm writing an App for Windows Phone in XNA... and I'd like to read a MJPEG stream stored in the App's resources. I've found many examples of how to get a MJPEG from a website via WebHttpRequest like this:

        // get the response
        HttpWebRequest request = (HttpWebRequest) asyncResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(asyncResult);

        try {
            // find boundary value
            string contentType = response.Headers["Content-Type"];

            if (!String.IsNullOrEmpty(contentType) && !contentType.Contains("=")) {
                throw new FormatException("Invalid content-type header.  The source is likely not returning a proper MJPEG stream.");
            }

            string boundary = response.Headers["Content-Type"].Split('=')[1].Replace("\"", "");
            byte[] boundaryBytes = Encoding.UTF8.GetBytes(boundary.StartsWith("--") ? boundary : "--" + boundary);

            using (Stream stream = response.GetResponseStream()) {
                using (BinaryReader binaryReader = new BinaryReader(stream)) {

                   // code to parse the MJPEG stream
                }
            }
        } finally {
            response.Close();
        }

... but this is not exactly what I'm looking for. Here below is my code for reading the MJPEG as a binary stream from the App's resources:

    private void ParseMjpeg(object uri)
    {
        // what the corresponding code for determining the boundary bytes in my local MJPEG?
        byte[] boundaryBytes = ???

        using (Stream stream = TitleContainer.OpenStream(uri.ToString())) {
            using (BinaryReader binaryReader = new BinaryReader(stream)) {

                // code to parse the MJPEG stream as before: OK
            }
        }
    }

How do I determine the boundary bytes in my code here above? Any help would be REALLY appreciated.

Thanks, j3d


Solution

  • this should make your life a lot easier. As far as I understand the DLL should provide you with most of what you need.

    http://channel9.msdn.com/coding4fun/articles/MJPEG-Decoder