locked
screen share video streaming RRS feed

  • Question

  • i am currently trying to make an video streaming with UDP

    i have already a code the makes is on clint and one server but there are so much lags and elay when i run it there are any else code or way to do it?

    code that i am using now (server code):

    public int port = 443;
            private TcpClient client;
            private TcpListener server;
            private NetworkStream mainStream;
    
            private readonly Thread Listening;
            private readonly Thread GetImage;
    
    
            public Form2(int Port)
            {
                port = Port;
                client = new TcpClient();
                Listening = new Thread(StartListening);
                GetImage = new Thread(ReciveImage);
    
                InitializeComponent();
    
                backgroundWorker1.WorkerReportsProgress = true;
                backgroundWorker1.WorkerSupportsCancellation = true;
    
                backgroundWorker2.WorkerReportsProgress = true;
                backgroundWorker2.WorkerSupportsCancellation = true;
            }
    
            private void StartListening()
            {
                backgroundWorker2.RunWorkerAsync();
            }
    
            private void StopListening()
            {
                server.Stop();
                client = null;
                if (Listening.IsAlive) Listening.Abort();
                if (GetImage.IsAlive) GetImage.Abort();
            }
            private void ReciveImage()
            {
                backgroundWorker1.RunWorkerAsync();
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                server = new TcpListener(IPAddress.Any, port);
                Listening.Start();
            }
            protected override void OnFormClosing(FormClosingEventArgs e)
            {
                base.OnFormClosing(e);
                StopListening();
            }
    
    
    
            private void Form2_Load(object sender, EventArgs e)
            {
    
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
    
            }
    
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                while (client.Connected)
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    mainStream = client.GetStream();
                    pictureBox1.Image = (Image)binaryFormatter.Deserialize(mainStream);
                }
            }
    
            private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
            {
                while (!client.Connected)
                {
                    server.Start();
                    client = server.AcceptTcpClient();
                }
                GetImage.Start();
            }

    client code:

    private readonly TcpClient client = new TcpClient();
            private NetworkStream mainStream;
            private int portNumber;
    
            private static Image GrabDesktop()
            {
                Rectangle bounds = Screen.PrimaryScreen.Bounds;
                Bitmap screen = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);//Format32bppArgb
                Graphics graphic = Graphics.FromImage(screen);
                graphic.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size , CopyPixelOperation.SourceCopy);
                return screen;
            }
            public static BinaryFormatter binaryFormatter = new BinaryFormatter();
            private void SendDesktopImage()
            {
                mainStream = client.GetStream();
                binaryFormatter.Serialize(mainStream, GrabDesktop());
            }
    
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void connect_btn_Click(object sender, EventArgs e)
            {
                int port = 443;
                try
                {
                    client.Connect(textBox.Text, port);
                }
                catch { }
            }
    
            private void share_btn_Click(object sender, EventArgs e)
            {
                timer1.Start();
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                SendDesktopImage();
            }
    
            private void disconnect_btn_Click(object sender, EventArgs e)
            {
                timer1.Stop();
            }

    Thursday, April 23, 2020 7:54 AM