Example #1
0
        public void Streaming()
        {
            byte[] cursorData = null;

            Program.WriteLine("Enter streaming method");

            using (_connection)
                using (_screenCaptureService)
                    using (_cursorStreamCodec)
                        while (_isStreaming)
                        {
                            //locks are quite expensive so we capture 10 frames and then lock again
                            lock (_streamComponentsLock)
                                for (int i = 0; i < 10; i++)
                                {
                                    if (!_isStreaming)
                                    {
                                        Program.WriteLine("Leave streaming method (_isStreaming check failed)");
                                        return;
                                    }

                                    Program.WriteLine("Begin capture screen...");
                                    var data = _screenCaptureService.CaptureScreen(_unsafeCodec, _cursorStreamCodec, _drawCursor);
                                    Program.WriteLine("Screen captured...");
                                    if (data == null)
                                    {
                                        Program.WriteLine("Screen data was null");
                                        continue;
                                    }

                                    var bytesToSend = 2 + data.Length;
                                    var flags       = ScreenResponseFlags.Frame;

                                    if (_drawCursor && _cursorStreamCodec != null)
                                    {
                                        Program.WriteLine("Get cursor");
                                        cursorData   = _cursorStreamCodec.CodeCursor();
                                        bytesToSend += cursorData.Length + 4;
                                        flags       |= ScreenResponseFlags.Cursor;
                                        Program.WriteLine("Cursor captured");
                                    }

                                    try
                                    {
                                        Program.WriteLine("Send data");
                                        if (_connection.SupportsStream)
                                        {
                                            _connection.SendStream(new WriterCall(bytesToSend,
                                                                                  writer =>
                                            {
                                                writer.Write((byte)flags);
                                                if (_drawCursor && cursorData != null)
                                                {
                                                    writer.Write(cursorData.Length);
                                                    writer.Write(cursorData);
                                                }

                                                writer.Write((byte)_currentMonitor);
                                                data.WriteIntoStream(writer.BaseStream);
                                            }));
                                        }
                                        else
                                        {
                                            var buffer = new byte[bytesToSend];
                                            buffer[0] = (byte)flags;

                                            var index = 1;
                                            if (_drawCursor && cursorData != null)
                                            {
                                                Buffer.BlockCopy(BitConverter.GetBytes(cursorData.Length), 0, buffer, index, 4);
                                                Buffer.BlockCopy(cursorData, 0, buffer, index + 4, cursorData.Length);

                                                index += cursorData.Length + 4;
                                            }

                                            buffer[index] = (byte)_currentMonitor;
                                            data.WriteToBuffer(buffer, index + 1);

                                            _connection.SendData(buffer, 0, buffer.Length);
                                        }
                                        Program.WriteLine("Data sent");
                                    }
                                    catch (Exception ex)
                                    {
                                        if (!_isStreaming)
                                        {
                                            Program.WriteLine("Leave streaming method (_isStreaming check failed and exception): " + ex);
                                            return;
                                        }

                                        ResponseByte((byte)RemoteDesktopCommunication.ResponseCaptureCancelled, _connectionInfo);
                                    }
                                }
                        }

            Program.WriteLine("Leave streaming method");
        }
Example #2
0
        private void button_Click(object sender, RoutedEventArgs e)
        {
            _dispatcherTimer = new DispatcherTimer {
                Interval = TimeSpan.FromSeconds(1)
            };
            _dispatcherTimer.Tick += DispatcherTimerOnTick;
            _dispatcherTimer.Start();

            new Thread(() =>
            {
                using (var streamCodec = new UnsafeStreamCodec(new JpgCompression(70),
                                                               UnsafeStreamCodecParameters.None))
                    using (var decoderCodec = new UnsafeStreamCodec(new JpgCompression(70),
                                                                    UnsafeStreamCodecParameters.None))
                        using (var cursorCodec = new CursorStreamCodec())
                            using (var decodeCursorCodec = new CursorStreamCodec())
                                using (var screenService = new FrontBufferService())
                                {
                                    streamCodec.ImageQuality = 70;
                                    screenService.Initialize(0);
                                    WriteableBitmap currentWriteableBitmap = null;

                                    while (!_isClosed)
                                    {
                                        var sw   = Stopwatch.StartNew();
                                        var data = screenService.CaptureScreen(streamCodec, cursorCodec, true);
                                        if (data == null)
                                        {
                                            continue;
                                        }
                                        //Debug.Print($"Screen captured ({sw.ElapsedMilliseconds})");

                                        unsafe
                                        {
                                            byte[] bytes;
                                            using (var ms = new MemoryStream())
                                            {
                                                data.WriteIntoStream(ms);
                                                bytes = ms.ToArray();
                                            }

                                            var cursorData = cursorCodec.CodeCursor();

                                            fixed(byte *bytePtr = bytes)
                                            {
                                                sw.Reset();
                                                //Debug.Print("Decode " + bytes.Length);
                                                var newBitmap =
                                                    decoderCodec.AppendModifier(decodeCursorCodec.CreateModifierTask(cursorData, 0,
                                                                                                                     cursorData.Length)).DecodeData(bytePtr, (uint)bytes.Length, Dispatcher);
                                                //Debug.Print($"Screen decoded ({sw.ElapsedMilliseconds})");
                                                if (newBitmap != currentWriteableBitmap)
                                                {
                                                    currentWriteableBitmap = newBitmap;
                                                    Dispatcher.Invoke(() => ImageAsd.Source = currentWriteableBitmap);
                                                }
                                                Interlocked.Increment(ref _currentFps);
                                            }
                                        }
                                    }
                                }
            }).Start();
        }