Пример #1
0
        private void RecordScreen()
        {
            var  stopwatch         = new Stopwatch();
            var  buffer            = new byte[screenWidth * screenHeight * 4];
            Task videoWriteTask    = null;
            var  isFirstFrame      = true;
            var  shotsTaken        = 0;
            var  timeTillNextFrame = TimeSpan.Zero;

            stopwatch.Start();

            while (!stopThread.WaitOne(timeTillNextFrame))
            {
                GetScreenshot(buffer);
                shotsTaken++;

                // Wait for the previous frame is written
                if (!isFirstFrame)
                {
                    videoWriteTask.Wait();
                    videoFrameWritten.Set();
                }

                if (audioStream != null)
                {
                    var signalled = WaitHandle.WaitAny(new WaitHandle[] { audioBlockWritten, stopThread });
                    if (signalled == 1)
                    {
                        break;
                    }
                }

                // Start asynchronous (encoding and) writing of the new frame
                // Overloads with Memory parameters are available on .NET 5+
#if NET5_0_OR_GREATER
                videoWriteTask = videoStream.WriteFrameAsync(true, buffer.AsMemory(0, buffer.Length));
#else
                videoWriteTask = videoStream.WriteFrameAsync(true, buffer, 0, buffer.Length);
#endif

                timeTillNextFrame = TimeSpan.FromSeconds(shotsTaken / (double)writer.FramesPerSecond - stopwatch.Elapsed.TotalSeconds);
                if (timeTillNextFrame < TimeSpan.Zero)
                {
                    timeTillNextFrame = TimeSpan.Zero;
                }

                isFirstFrame = false;
            }

            stopwatch.Stop();

            // Wait for the last frame is written
            if (!isFirstFrame)
            {
                videoWriteTask.Wait();
            }
        }
 void RecordScreen()
 {
     var frameInterval = TimeSpan.FromSeconds(1 / (double)writer.FramesPerSecond);
     var buffer = new byte[Params.Width * Params.Height * 4];
     Task videoWriteTask = null;
     var isFirstFrame = true;
     var timeTillNextFrame = TimeSpan.Zero;
     while (!stopThread.WaitOne(timeTillNextFrame))
     {
         var timestamp = DateTime.Now;
         Screenshot(buffer);
         // Wait for the previous frame is written
         if (!isFirstFrame)
         {
             videoWriteTask.Wait();
             videoFrameWritten.Set();
         }
         if (audioStream != null)
         {
             var signalled = WaitHandle.WaitAny(new WaitHandle[] { audioBlockWritten, stopThread });
             if (signalled == 1) break;
         }
         // Start asynchronous (encoding and) writing of the new frame
         videoWriteTask = videoStream.WriteFrameAsync(true, buffer, 0, buffer.Length);
         timeTillNextFrame = timestamp + frameInterval - DateTime.Now;
         if (timeTillNextFrame < TimeSpan.Zero) timeTillNextFrame = TimeSpan.Zero;
         isFirstFrame = false;
     }
     // Wait for the last frame is written
     if (!isFirstFrame) videoWriteTask.Wait();
 }
Пример #3
0
        void RecordScreen()
        {
            var  frameInterval     = TimeSpan.FromSeconds(1 / (double)writer.FramesPerSecond);
            var  buffer            = new byte[Params.Width * Params.Height * 4];
            Task videoWriteTask    = null;
            var  timeTillNextFrame = TimeSpan.Zero;

            while (!stopThread.WaitOne(timeTillNextFrame))
            {
                var timestamp = DateTime.Now;

                Screenshot(buffer);

                // Wait for the previous frame is written
                videoWriteTask?.Wait();

                // Start asynchronous (encoding and) writing of the new frame
                videoWriteTask = videoStream.WriteFrameAsync(true, buffer, 0, buffer.Length);

                timeTillNextFrame = timestamp + frameInterval - DateTime.Now;
                if (timeTillNextFrame < TimeSpan.Zero)
                {
                    timeTillNextFrame = TimeSpan.Zero;
                }
            }

            // Wait for the last frame is written
            videoWriteTask?.Wait();
        }
Пример #4
0
        private void RecordScreen()
        {
            try
            {
                var stopwatch = new Stopwatch();
                var buffer    = new byte[screenWidth * screenHeight * 4];

                Task videoWriteTask = null;

                var isFirstFrame      = true;
                var shotsTaken        = 0;
                var timeTillNextFrame = TimeSpan.Zero;
                stopwatch.Start();

                while (!stopThread.WaitOne(timeTillNextFrame))
                {
                    try
                    {
                        //buffer = GetScreenshotNew();
                        GetScreenshot(buffer);
                    }
                    catch (Exception ex)
                    {
                        Exceptions.Add(ex);
                        Interlocked.Increment(ref CountErrors);
                        GetScreenshotEmpty(buffer);
                    }
                    shotsTaken++;

                    // Wait for the previous frame is written
                    if (!isFirstFrame)
                    {
                        videoWriteTask.Wait();
                        videoFrameWritten.Set();
                    }
                    videoWriteTask = videoStream.WriteFrameAsync(true, buffer, 0, buffer.Length);

                    timeTillNextFrame = TimeSpan.FromSeconds(shotsTaken / (double)writer.FramesPerSecond - stopwatch.Elapsed.TotalSeconds);
                    if (timeTillNextFrame < TimeSpan.Zero)
                    {
                        timeTillNextFrame = TimeSpan.Zero;
                    }

                    isFirstFrame = false;
                }

                stopwatch.Stop();

                // Wait for the last frame is written
                if (!isFirstFrame)
                {
                    videoWriteTask.Wait();
                }
            }
            catch (Exception ex)
            {
                Interlocked.Increment(ref CountErrors);
                Exceptions.Add(ex);
            }
        }
Пример #5
0
        /// <summary>
        /// Asynchronously writes an Image frame.
        /// </summary>
        /// <param name="Image">The Image frame to write.</param>
        /// <returns>The Task Object.</returns>
        public Task WriteFrameAsync(Bitmap Image)
        {
            var bits = Image.LockBits(new Rectangle(Point.Empty, Image.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);

            Marshal.Copy(bits.Scan0, _videoBuffer, 0, _videoBuffer.Length);
            Image.UnlockBits(bits);

            return(_videoStream.WriteFrameAsync(true, _videoBuffer, 0, _videoBuffer.Length));
        }
Пример #6
0
        private void RecordScreen()
        {
            Stopwatch stopwatch = new Stopwatch();

            byte[] buffer = new byte[mScreenWidth * mScreenHeight * 4];

            Task     videoWriteTask    = null;
            bool     isFirstFrame      = true;
            int      shotsTaken        = 0;
            TimeSpan timeTillNextFrame = TimeSpan.Zero;



            stopwatch.Start();



            while (!stopThread.WaitOne(timeTillNextFrame))
            {
                GetScreenshot(buffer);
                shotsTaken++;

                if (!isFirstFrame)
                {
                    videoWriteTask.Wait();



                    videoFrameWritten.Set();
                }
                if (mAudioSource != null)
                {
                    var signalled = WaitHandle.WaitAny(new WaitHandle[] { audioBlockWritten, stopThread });
                    if (signalled == 1)
                    {
                        break;
                    }
                }

                videoWriteTask = mVideoStream.WriteFrameAsync(true, buffer, 0, buffer.Length);

                timeTillNextFrame = TimeSpan.FromSeconds(shotsTaken / (double)mWriter.FramesPerSecond - stopwatch.Elapsed.TotalSeconds);
                if (timeTillNextFrame < TimeSpan.Zero)
                {
                    timeTillNextFrame = TimeSpan.Zero;
                }

                isFirstFrame = false;
            }
            stopwatch.Stop();
            // Wait for the last frame is written
            if (!isFirstFrame)
            {
                videoWriteTask.Wait();
            }
        }
Пример #7
0
 private void RecordScreen()
 {
     while (!stop)
     {
         var buffer = GetScreenshot();
         // 把图片写入视频流
         videoStream.WriteFrameAsync(true, buffer, 0, buffer.Length).Wait();
     }
     writer.Close();
 }
Пример #8
0
        private void RecordScreen()
        {
            var stopwatch = new Stopwatch();
            var buffer    = new byte[width * height * 4];

            Task videoWriteTask = null;

            var isFirstFrame      = true;
            var shotsTaken        = 0;
            var timeTillNextFrame = TimeSpan.Zero;

            stopwatch.Start();

            while (!stopThread.WaitOne(timeTillNextFrame))
            {
                Screenshot(buffer);
                shotsTaken++;

                if (!isFirstFrame)
                {
                    videoWriteTask.Wait();

                    videoFrameWritten.Set();
                }

                if (audioStream != null)
                {
                    var signalled = WaitHandle.WaitAny(new WaitHandle[] { audioBlockWritten, stopThread });
                    if (signalled == 1)
                    {
                        break;
                    }
                }

                videoWriteTask = videoStream.WriteFrameAsync(true, buffer, 0, buffer.Length);

                timeTillNextFrame = TimeSpan.FromSeconds(shotsTaken / (double)writer.FramesPerSecond - stopwatch.Elapsed.TotalSeconds);
                if (timeTillNextFrame < TimeSpan.Zero)
                {
                    timeTillNextFrame = TimeSpan.Zero;
                }

                isFirstFrame = false;
            }

            stopwatch.Stop();

            if (!isFirstFrame)
            {
                videoWriteTask.Wait();
            }
        }
Пример #9
0
        private void RecordScreen()
        {
            var stopwatch = new Stopwatch();
            var buffer    = new byte[screenWidth * screenHeight * 4];

            Task videoWriteTask = null;

            var isFirstFrame      = true;
            var shotsTaken        = 0;
            var timeTillNextFrame = TimeSpan.Zero;

            stopwatch.Start();

            while (!stopThread.SafeWaitHandle.IsClosed && !stopThread.WaitOne(timeTillNextFrame))
            {
                ScreenshotCaptureWithMouse.ScreenCapture.CaptureScreen.GetScreenshotAll(buffer, screenWidth, screenHeight);

                shotsTaken++;

                // Wait for the previous frame is written
                if (!isFirstFrame)
                {
                    videoWriteTask.Wait();
                }
                videoWriteTask = videoStream.WriteFrameAsync(true, buffer, 0, buffer.Length);

                timeTillNextFrame = TimeSpan.FromSeconds(shotsTaken / (double)writer.FramesPerSecond - stopwatch.Elapsed.TotalSeconds);
                if (timeTillNextFrame < TimeSpan.Zero)
                {
                    timeTillNextFrame = TimeSpan.Zero;
                }

                isFirstFrame = false;

                if (stopwatchGlobal.Elapsed.TotalSeconds > 10)
                {
                    ///если превышаем размер, то запускаем процесс создания видео
                    ///запускаем новый процес записи аудио
                    Break?.Invoke();
                }
            }

            stopwatch.Stop();

            // Wait for the last frame is written
            if (!isFirstFrame)
            {
                videoWriteTask.Wait();
            }
        }
Пример #10
0
        public void Record()
        {
            _tempName = "Temp-" + DateTime.Now.Ticks + ".avi";
            var writer = new AviWriter(_tempName)
            {
                FramesPerSecond = 30,
            };

            IAviVideoStream stream    = writer.AddMotionJpegVideoStream(ScreenWidth, ScreenHeight);
            var             frameData = new byte[stream.Width * stream.Height * 4];

            var stopwatch = new Stopwatch();
            var buffer    = new byte[ScreenWidth * ScreenHeight * 4];

            var shotsTaken        = 0;
            var timeTillNextFrame = TimeSpan.Zero;

            Task videoWriteTask = null;
            var  isFirstFrame   = true;

            stopwatch.Start();
            while (!WaitHandler.WaitOne(timeTillNextFrame))
            {
                GetScreenshot(buffer);
                if (!isFirstFrame)
                {
                    videoWriteTask.Wait();
                }

                videoWriteTask    = stream.WriteFrameAsync(true, buffer, 0, buffer.Length);
                timeTillNextFrame = TimeSpan.FromSeconds(shotsTaken / (double)writer.FramesPerSecond - stopwatch.Elapsed.TotalSeconds);
                if (timeTillNextFrame < TimeSpan.Zero)
                {
                    timeTillNextFrame = TimeSpan.Zero;
                }

                isFirstFrame = false;
            }

            stopwatch.Stop();
            if (!isFirstFrame)
            {
                videoWriteTask.Wait();
            }

            writer.Close();
        }
Пример #11
0
        public void Start()
        {
            _fileWriter = new AviWriter(_outputFilePath)
            {
                FramesPerSecond = _configurator.FramePerSecond,
                EmitIndex1      = true
            };
            _videoStream = _fileWriter.AddEncodingVideoStream(_selectedEncoder, true, _configurator.Width, _configurator.Height);
            _frameData   = new byte[_videoStream.Width * _videoStream.Height * 4];
            _cts         = new CancellationTokenSource();

            _workTask = Task.Run(async() =>
            {
                Task writeTask = Task.FromResult(true);
                while (!_cts.IsCancellationRequested)
                {
                    GetSnapshot(_frameData);
                    await writeTask;
                    writeTask = _videoStream.WriteFrameAsync(true, _frameData, 0, _frameData.Length);
                }
                await writeTask;
            });
        }
Пример #12
0
        private void RecordScreen()
        {
            var frameInterval = TimeSpan.FromSeconds(1 / (double)writer.FramesPerSecond);
            var buffer        = new byte[screenWidth * screenHeight * 4];

#if FX45
            Task videoWriteTask = null;
#else
            IAsyncResult videoWriteResult = null;
#endif
            var isFirstFrame      = true;
            var timeTillNextFrame = TimeSpan.Zero;
            while (!stopThread.WaitOne(timeTillNextFrame))
            {
                var timestamp = DateTime.Now;

                GetScreenshot(buffer);

                // Wait for the previous frame is written
                if (!isFirstFrame)
                {
#if FX45
                    videoWriteTask.Wait();
#else
                    videoStream.EndWriteFrame(videoWriteResult);
#endif
                    videoFrameWritten.Set();
                }

                if (audioStream != null)
                {
                    var signalled = WaitHandle.WaitAny(new WaitHandle[] { audioBlockWritten, stopThread });
                    if (signalled == 1)
                    {
                        break;
                    }
                }

                // Start asynchronous (encoding and) writing of the new frame
#if FX45
                videoWriteTask = videoStream.WriteFrameAsync(true, buffer, 0, buffer.Length);
#else
                videoWriteResult = videoStream.BeginWriteFrame(true, buffer, 0, buffer.Length, null, null);
#endif

                timeTillNextFrame = timestamp + frameInterval - DateTime.Now;
                if (timeTillNextFrame < TimeSpan.Zero)
                {
                    timeTillNextFrame = TimeSpan.Zero;
                }

                isFirstFrame = false;
            }

            // Wait for the last frame is written
            if (!isFirstFrame)
            {
#if FX45
                videoWriteTask.Wait();
#else
                videoStream.EndWriteFrame(videoWriteResult);
#endif
            }
        }