Hello everyone,
I'm trying to make an IP camera proxy/relay. To clear what that is, a camera can hold up only up to 15 connections. Since this should be used with a larger number of users, my idea is to make a REST web service with Web API and serve an MJPEG stream to every user. The source would be buffered somehow on the server.
I'm using an Axis M1101 ip camera.
My solution is to generate an MJPEG stream using static images which I pull and cache of the camera every 1/25 secounds (the camera should keep up with that). The problem is how can I generate and send a MJPEG "live". Just to be fair, I could also do with a H.264 stream, but an MJPEG seams easier.
I've tried using MultipartContent, something like this, just for testing.
HttpResponseMessage response = Request.CreateResponse();
MultipartContent multipartContent = new MultipartContent("x-mixed-replace", "myboundary");
response.Content = multipartContent;
byte[] array = ImageStream();
HttpContent content1 = new ByteArrayContent(array);
content1.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
content1.Headers.ContentLength = array.Length;
multipartContent.Add(content1);
array = ImageStream();
HttpContent content2 = new ByteArrayContent(array);
content2.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
multipartContent.Add(content2);
content2.Headers.ContentLength = array.Length;
array = ImageStream();
HttpContent content3 = new ByteArrayContent(array);
content3.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
multipartContent.Add(content3);
content3.Headers.ContentLength = array.Length;
array = ImageStream();
HttpContent content4 = new ByteArrayContent(array);
content4.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
multipartContent.Add(content4);
content4.Headers.ContentLength = array.Length;
return response;
Expected, this returns the series of images, but in one blow, if I am clear. How can I make a stream of of this, so the images could keep on coming?
I've also tried using PushStreamContent, and that just shows series of characters to the browser, which are unrecognizable as an MJPEG.
response.Content = new PushStreamContent(
async (outputStream, httpContent, transportContext) =>
{
try
{
String boundaryConstant = "myboundary";
var boundary = System.Text.Encoding.Unicode.GetBytes(boundaryConstant);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
while (true)
{
byte[] imageStream = ImageStream();
response.Content.Headers.ContentLength = imageStream.Length;
await outputStream.WriteAsync(imageStream, 0, imageStream.Length);
await outputStream.WriteAsync(boundary, 0, boundary.Length);
}
}
catch (HttpException ex)
{
if (ex.ErrorCode == -2147023667)
{
return;
}
}
finally
{
outputStream.Close();
}
});
Every other advice regarding the whole idea itself are welcome. For example, how to build a h.264 stream, because I've only found how to build them statically, not live.