Question

I will try to explain clearly the context of my problem.

I have a server grabbing frames from an IP camera videostreaming. I'm able to process and edit these images but now I have to deliver these images again as a videostream(mjpeg). It is not mandatory that I need to deliver it as streaming to everyone, it would be enought by now if I can send the streaming through TCP to another client.
I don't have any idea what the best way to go about it would be, as I don't think sending the images one by one is a good practice...

Can anyone help me somehow? I've never dealt with this situation and I'm not able to find any solution on Google (probably I'm not doing the right query...)

How would you handle it?

Was it helpful?

Solution

I have found a simple CodeProject which performs a very similar task, streaming your desktop. I hope it can help you as well!

OTHER TIPS

Here is the simplest way to send Image as video stream over IP using Tcp This method send and receive video stream. This is based on Windows form.

Send Stream(Server):

    private void Start_Sending_Video_Conference(string remote_IP, int port_number)
    {
        try
        {

            ms = new MemoryStream();// Store it in Binary Array as Stream


            IDataObject data;
            Image bmap;

            //  Copy image to clipboard
            SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0);

            //  Get image from clipboard and convert it to a bitmap
            data = Clipboard.GetDataObject();

            if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
            {
                bmap = ((Image)(data.GetData(typeof(System.Drawing.Bitmap))));
                bmap.Save(ms, ImageFormat.Bmp);
            }


            picCapture.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arrImage = ms.GetBuffer();
            myclient = new TcpClient(remote_IP, 5000);//Connecting with server
            myns = myclient.GetStream();
            mysw = new BinaryWriter(myns);
            mysw.Write(arrImage);//send the stream to above address
            ms.Flush();
            mysw.Flush();
            myns.Flush();
            ms.Close();
            mysw.Close();
            myns.Close();
            myclient.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Video Conference Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

Receive Stream(Client):

  private void Start_Receiving_Video_Conference()
    {
        try
        {

            // Open The Port
            mytcpl = new TcpListener(5000);
            mytcpl.Start();                      // Start Listening on That Port
            mysocket = mytcpl.AcceptSocket();        // Accept Any Request From Client and Start a Session
            ns = new NetworkStream(mysocket);    // Receives The Binary Data From Port

            picture_comming.Image = Image.FromStream(ns);
            mytcpl.Stop();                           // Close TCP Session

            if (mysocket.Connected == true)          // Looping While Connected to Receive Another Message 
            {
                while (true)
                {
                    Start_Receiving_Video_Conference();              // Back to First Method
                }
            }
            myns.Flush();

        }
        catch (Exception) { button1.Enabled = true; myth.Abort(); }
    }

Unmanaged Imports:

[System.Runtime.InteropServices.DllImport("user32", EntryPoint="SendMessageA")] 
    static extern int SendMessage(int hwnd, int wMsg, int wParam,  [MarshalAs(UnmanagedType.AsAny)] 
        object lParam); 
    [System.Runtime.InteropServices.DllImport("user32", EntryPoint="SetWindowPos")] 
    static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); 
    [System.Runtime.InteropServices.DllImport("user32")] 
    static extern bool DestroyWindow(int hndw); 
    [System.Runtime.InteropServices.DllImport("avicap32.dll")] 
    static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID); 
    [System.Runtime.InteropServices.DllImport("avicap32.dll")] 
    static extern bool capGetDriverDescriptionA(short wDriver, string lpszName, int cbName, string lpszVer, int cbVer);

Method To Preview Webcam Image on Server

This method should start first to initialize preview of webcam (On server side)

    private void OpenPreviewWindow() 
    {
        int iHeight = 259;
        int iWidth = 369;
        // 
        //  Open Preview window in picturebox
        // 
        hHwnd = capCreateCaptureWindowA(iDevice.ToString (), (WS_VISIBLE | WS_CHILD), 0, 0, 640, 480, picCapture.Handle.ToInt32(), 0);
        // 
        //  Connect to device
        // 
        if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) == 1) 
        {
            // 
            // Set the preview scale
            // 
            SendMessage(hHwnd, WM_CAP_SET_SCALE, 1, 0);
            // 
            // Set the preview rate in milliseconds
            // 
            SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);
            // 
            // Start previewing the image from the camera
            // 
            SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, 0);
            // 
            //  Resize window to fit in picturebox
            // 
            SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, iWidth,iHeight, (SWP_NOMOVE | SWP_NOZORDER));
        }
        else 
        {
            // 
            //  Error connecting to device close window
            //  
            DestroyWindow(hHwnd);
        }
    }

To Close Preview:

private void ClosePreviewWindow() 
    {
        // 
        //  Disconnect from device
        // 
        SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0);
        // 
        //  close window
        // 
        DestroyWindow(hHwnd);
    }

WebCam Api

    const short WM_CAP = 1024; 
    const int WM_CAP_DRIVER_CONNECT = WM_CAP + 10; 
    const int WM_CAP_DRIVER_DISCONNECT = WM_CAP + 11; 
    const int WM_CAP_EDIT_COPY = WM_CAP + 30; 
    const int WM_CAP_SET_PREVIEW = WM_CAP + 50; 
    const int WM_CAP_SET_PREVIEWRATE = WM_CAP + 52; 
    const int WM_CAP_SET_SCALE = WM_CAP + 53; 
    const int WS_CHILD = 1073741824; 
    const int WS_VISIBLE = 268435456; 
    const short SWP_NOMOVE = 2; 
    const short SWP_NOSIZE = 1; 
    const short SWP_NOZORDER = 4; 
    const short HWND_BOTTOM = 1; 

Please ask if any query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top