Пример #1
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
            }
        }