I have the following:
public sealed class ServerModule : NancyModule
{
public ServerModule()
{
Get["/video"] = o =>
{
byte[] img = GetImage("whatever.jpeg");
return new MjpegResponse(firstImg);
};
}
private sealed class MjpegResponse : Response
{
public MjpegResponse(byte[] data)
{
this.Headers.Clear();
this.Headers.Add("Server", "IP Webcam Server 1.5");
this.Headers.Add("Connection", "close");
this.Headers.Add("Cache-Control", "no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0");
this.Headers.Add("Pragma", "no-cache");
this.Headers.Add("Expires", "-1");
this.Headers.Add("Access-Control-Allow-Origin", "*");
this.ContentType = "multipart/x-mixed-replace;boundary=Ba4oTvQMY8ew04N8dcnM";
var footer = Encoding.ASCII.GetBytes("\r\n");
this.Contents = stream =>
{
using(var writer = new BinaryWriter(stream))
{
writer.Write(data);
writer.Write(footer);
}
};
}
}
}
The headers match the headers that I am getting from an IPCamera which has a built-in MJPEG server even though Fiddler is showing the correct headers:
HTTP/1.1 200 OK
Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: multipart/x-mixed-replace;boundary=Ba4oTvQMY8ew04N8dcnM
Expires: -1
Server: IP Webcam Server 1.5 Microsoft-HTTPAPI/2.0
Access-Control-Allow-Origin: *
Date: Tue, 24 Nov 2015 21:23:29 GMT
Connection: close
When trying in an MJPEG enabled browser (Firefox) it is unable to stream or show anything. Another thing I have noticed is the Transfer-Encoding: chunked
which should not be appearing.
Any ideas?
I think the issue here is quite simple to resolve. After writing to a stream, it point to the end of it. You need to rewind that pointer to the start of the stream before returning with a response:
stream.Seek(0, SeekOrigin.Begin);