Esempio n. 1
0
        private void StartTcpClient()
        {
            try
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(this.HostName, this.Port);

                NetworkStream stream = tcpClient.GetStream();


                var binary = new BinaryFormatter();
                binary.Serialize(stream, (uint)ClientType.Receiver);
                binary.Serialize(stream, GuidTransmitters.ToByteArray());

                //Reader
                Task.Factory.StartNew(() =>
                {
                    var decoder = new OpenH264Lib.Decoder("openh264-2.0.0-win32.dll");


                    while (tcpClient?.Connected == true)
                    {
                        try
                        {
                            var bin  = new BinaryFormatter();
                            var data = (byte[])bin.Deserialize(stream);

                            var bmp = decoder.Decode(data, data.Length);
                            if (bmp != null)
                            {
                                System.Windows.Application.Current?.Dispatcher?.Invoke(() =>
                                {
                                    SetBmp(bmp);
                                    this.InvalidateVisual();
                                });
                            }
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine($"Failed read data. Ex: {ex.Message}");
                        }
                    }
                });

                //Writer
                Task.Factory.StartNew(() =>
                {
                    while (tcpClient?.Connected == true)
                    {
                        var data = sendData.Dequeue();
                        if (stream.CanWrite)
                        {
                            var bin = new BinaryFormatter();
                            bin.Serialize(stream, data);
                        }
                        else
                        {
                            sendData.Enqueue(data);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Failed connection. Ex: {ex.Message}");
            }
        }
 private void OnEncode(byte[] data, int length, OpenH264Lib.Encoder.FrameType keyFrame)
 {
     datas.Clear(q => q.Count > 10);
     datas.Enqueue(data);
 }