Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send video frame via socket

Tags:

c#

sockets

I captured video stream with Kinect device. I need to Send this streams over network. For this purpose I used sockets. Her is piece of code in server and client:

private System.Drawing.Bitmap _CurrentBitmap;
    public ManWindow()
    {
        InitializeComponent();

        this.Loaded += delegate
        {
            BackgroundWorker bw1 = new BackgroundWorker();
            bw1.RunWorkerCompleted += (a, b) => bw1.RunWorkerAsync();
            bw1.DoWork += delegate { SendImage(); };
            bw1.RunWorkerAsync();
        };
    }
    public void SendImage()
    {
        Socket sListen = new Socket(AddressFamily.InterNetwork,
                                    SocketType.Stream,
                                    ProtocolType.Tcp);

        IPAddress IP = IPAddress.Parse("127.0.0.1");
        IPEndPoint IPE = new IPEndPoint(IP, 4321);

        sListen.Bind(IPE);
        sListen.Listen(2);

        while (true)
        {
            Socket clientSocket;
            clientSocket = sListen.Accept();

            var converter = new System.Drawing.ImageConverter();
            byte[] buffer = (byte[])converter.ConvertTo(_CurrentBitmap, typeof(byte[]));
            clientSocket.Send(buffer, buffer.Length, SocketFlags.None);
        }
    }

Client:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        BackgroundWorker bw11111 = new BackgroundWorker();
        bw11111.RunWorkerCompleted += (a, b) => bw11111.RunWorkerAsync();
        bw11111.DoWork += delegate { Recive(); };
        bw11111.RunWorkerAsync();
        Recive();
    }

    public void Recive()
    {
        Socket s = new Socket(AddressFamily.InterNetwork,
                            SocketType.Stream,
                            ProtocolType.Tcp);

        IPAddress IP = IPAddress.Parse("127.0.0.1");
        IPEndPoint IPE = new IPEndPoint(IP, 4321);
        s.Connect(IPE);

        while (true)
        {

            byte[] buffer = new byte[1000000];

            s.Receive(buffer, buffer.Length, SocketFlags.None);

            MemoryStream ms = new MemoryStream(buffer);

            ms.Write(buffer, 0, buffer.Length);
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ms);

            Dispatcher.BeginInvoke(new Action(() =>
            {
                rgbImage11.Source = bitmap.ToBitmapSource();
            }));

        }
    }

_CurrentBitmap is frame that readed from Kinect and every 1 second updated. When Client starts, call Socket Receive method, it receives frame and after convert to bitmap asign it to Image WPF control. My problem is this action occurs ones and loop in client not repeated.

like image 802
Artin Falahi Avatar asked Aug 07 '12 11:08

Artin Falahi


1 Answers

I think you are doing it right, although depending on your needs UDP sockets might make more sense. Try changing your Server code to something like this:

while (true)
{
     Socket clientSocket;
     clientSocket = sListen.Accept();

     var converter = new System.Drawing.ImageConverter();
     while(true) // find a better way to determine that the picture is still updating?
    {
        byte[] buffer = (byte[])converter.ConvertTo(_CurrentBitmap, typeof(byte[]));
        clientSocket.Send(buffer, buffer.Length, SocketFlags.None);
    }
}

You were sending one bitmap to your client, then falling out of the loop. The socket will not stay open, so your client runs it's loop once.

like image 103
Chris Avatar answered Nov 07 '22 15:11

Chris