I want to ask how could I stream images using C# to clients which are not local.
The idea is to let users from other computers see the images coming from the webcam connected to my PC, by typing the IP of my computer to their webbrowser.
I have succeeded in making it locally – when I access the 127.0.0.1 address on my computer, it works.
I have tried using WCF, and also TcpListener:
listener = new HttpListener();
listener.Start();
HttpListenerContext context = listener.GetContext();
HttpListenerResponse response = context.Response;
System.IO.Stream output = response.OutputStream;
...
But, when I try to make it work from outside my local network – I fail to.
My question is like this:
-Do you know any library in C# which will easily let me listen to requests and answer them by a Stream? (I am trying to let browsers access the images stream which I send to them. it works nicely locally…)
-Maybe you know of a different approach which will ease the task?
-Besides, to which address should I listen when trying to broadcast to the outside world? 127.0.0.1? the address which I see on WhatsMyIp sites?
Thank you Very Much!
Webcams with built in networking typically stream images back using MJPEG/MIME multipart.
If you have a camera with built-in networking, then it most likely already supports streaming an MJPEG stream over the network. Being able to access the camera from outside your local network is a matter of configuring your home router to pass external requests (probably on port 80) to the camera’s IP address (this is known as NAT configuration). The exact process for doing this will depend on your router, but it should be fairly easy to configure.
If you have a camera without networking built-in you can build a ‘proxy’ on your home computer that will listen for network requests from external clients, fetch images from the camera, and send the images back as parts within the response stream. Once you have the proxy written, it will be just like the network camera in terms of external access–you will need to configure your router to allow external access.
HttpListener would be a good choice for implementing the proxy. The main work here is formatting the response.OutputStream according to the MJPEG/MIME multipart convention. Here I would recommend using StreamWriter (for the textual parts) and Image.Save for the images. Keep in mind that the MJPEG response contains a combination of textual data (for the MIME/multipart headers and boundaries) and binary data (for the actual JPEG images) contained within the MIME/multipart body. If you need to support streaming to multiple clients concurrently you will also need to use threading.
As far as the IP address to use HttpListener supports a + or * notation that avoids the need to specify any IP address. For example: http://+/Stream/ See MSDN for more information.