What Methods Would Work in Streaming Live Video Using AForge.NET and Windows Forms?

1k Views Asked by At

I am trying to send a live video feed from a client to another client and vice-versa. I am using TCP sockets. So, AForge.NET helps out with actually getting the video from a webcam and displaying it as a set of images in a PictureBox. The AForge.NET works just as expected; it is just getting the images quickly to the other client that is the problem. I have tried many solutions to this problem.

One of them was to send the number of bytes as an Int across the network first and then send over the image (frame) to the client. For some reason though, after so many frames (it varied each time) it would receive a number that is not a valid representation of the bytes about to be sent over. I am using asynchronous callbacks so maybe there was some delay or maybe it was sent too fast.

Then another solution I tried was setting the bytes received to the maximum amount that can be sent over TCP/IP and then just filled the empty bytes with spaces or some other filler character. Then to check how big it is you could just run through (starting from the end) the byte array and find the first non-space character or a character especially included to denote the end of the frame in bytes. This would have been more dynamic in that the size is not needed each time but the efficiency goes down though due to the constant size being so large. And there were issues getting the special byte to work. Since it is a 64-bit application, a single character is 2 bytes. So I added the character to the frame (at the end) to denote where the frame stops. But it could not detect it when I ran back through it. So maybe I made a mistake working with bytes but I felt like that one should have worked.

Basically, I want to know, using AForge.NET, how to get the video images, to stream live video from one application to the next over the network using TCP sockets.

1

There are 1 best solutions below

0
On

Your first approach was the correct one. The thing you transmit between machines must start with the number of bytes transmitted, followed by the actual payload. You should try to fix the issues you had in this approach.

Using special values, zeroes or others, is not going to work with binary data. Your image data may validly contain a series of bytes that just so happen to be your sentinel value, and your stream will be broken.

There are a number of things to watch for when transmitting data over TCP sockets. An important one is that you receive a stream. The receive handler may get less bytes than what was sent from the other side at application level. Possibly even not enough bytes to rebuild the image length variable. It has to consume more of the stream to finally get the whole object that was sent.

In C# you can use the TcpClient, get its stream and call Read() on it, it will block until enough bytes are consumed from the pipe.

On the sending side double check the pixel format (8bpp, 24bpp, 32bpp?) and possible row padding.

Also note that AForge will dispose the Bitmap when your event handler returns. Especially you must not transfer the Bitmap reference to another thread and send it from there, make your own copy.