Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C++
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.

CSS
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(); //this is where I get the picture
                               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) // The remote host closed the connection.
                           {
                               return;
                           }
                       }
                       finally
                       {
                           // Close output stream as we are done
                           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.
Posted
Comments
Foothill 16-Jul-13 17:59pm    
Have you looked into adding a Silverlight plugin? It's designed to handle streaming JPEGS.
Aleksandar.S 16-Jul-13 19:08pm    
No, not really. Are you referencing on any specific plugin?
I've imagined this as a REST service that could be used from what ever. The camera itself could serve MJPEG the way I explained, so the only thing I should actually do is to reproduce the camera behavior with my service. So far, I'm not doing that with Web Api. I can't reproduce that kind of reply I am getting from it and that is explained in any document explaining MJPEG.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900