1
votes

I'm trying to stream my live web cam video to other computers in my network over Ethernet using .NET/C#.

For showing web cam video I use AForge.Controls.VideoSourcePlayer.

For streaming I use the following simplified code on the server side:

private void OnTcpClientConnected(TcpClient tcpClient)
{
    using (tcpClient)
    {
        using (NetworkStream stream = tcpClient.GetStream())
        {
            BinaryFormatter formatter = new BinaryFormatter();
            while ( isTcpServerRunning )
            {
                Bitmap frame = videoSourcePlayer.GetCurrentVideoFrame();
                formatter.Serialize(stream, frame );
            }
        }
    }
}

For reading the stream I use the following simplified code on the client side:

using( TcpClient client = new TcpClient() )
{
    client.Connect( ipAddress, port );

    using( NetworkStream stream = client.GetStream() )
    {
        BinaryFormatter formatter = new BinaryFormatter();
        while( isConnected )
        {
            DisplayPicture((Bitmap) formatter.Deserialize(stream));
        }
    }
}

Well, so far it works and runs kind of smoothly.

Now I have some questions:

  • Should I use UdpClient or TcpClient for video streaming? If Udp, how to get the Networkstream? (For TcpClient its GetStream() method.)

  • One single web cam frame (bitmap) provided by AForge has resolution of 640x480 and is ~540KB. That means for frame rate 20fps in one single second ~10,5MB are transferred over the network which seems quite much to me. How could I reduce the image size or compress the transferred data?

  • Generally: Is there some better way to stream a "video" over the network? Are there best practices or good libraries?

Thanks in advance.

1

1 Answers

1
votes

Obviously streaming jpegs is lighter than streaming bitmaps.
It makes sense to compress bitmaps to jpeg before send it over the wire.
This solves all of your problems. Don't force yourself into udp or tcp.

AForge also provides a way to stream jpegs over HTTP. Take a look at MJPEG here