Exemple #1
0
        /// <summary>
        /// Writes the given bitmap image as a frame to the stream.  If this is the first image or more than half of the pixels have
        /// changed then a raw frame will be written.   Otherwise, a diff frame will be written.
        ///
        /// This method uses the system's wall clock to determine the timestamp for this frame. The timestamp will be
        /// relative to the wall clock time when the first frame was written.
        /// </summary>
        /// <param name="bitmap">the image to write</param>
        /// <param name="desiredFrameTime">if provided, sstamp the frame with this time, otherwise stamp it with the wall clock delta from the first frame time</param>
        /// <param name="force">if true, writes the frame even if there are no changes</param>
        /// <returns>the same bitmap that was passed in</returns>
        public ConsoleBitmap WriteFrame(ConsoleBitmap bitmap, bool force = false, TimeSpan?desiredFrameTime = null)
        {
            if (pausedAt.HasValue)
            {
                return(bitmap);
            }

            var rawFrame = GetRawFrame(bitmap);

            var now = DateTime.UtcNow - TotalPauseTime;

            if (firstFrameTime.HasValue == false)
            {
                rawFrame.Timestamp = TimeSpan.Zero;
                firstFrameTime     = now;
            }
            else
            {
                rawFrame.Timestamp = desiredFrameTime.HasValue ? desiredFrameTime.Value : now - firstFrameTime.Value;
            }

            if (lastFrame == null)
            {
                StreamHeader(bitmap);
                writer.Write(serializer.SerializeFrame(rawFrame));
                FramesWritten++;
            }
            else
            {
                if (GetEffectiveWidth(bitmap) != lastFrame.Pixels.Length || GetEffectiveHeight(bitmap) != lastFrame.Pixels[0].Length)
                {
                    throw new InvalidOperationException("Video frame has changed size");
                }

                var diff = PrepareDiffFrame(lastFrame, bitmap);
                diff.Timestamp = rawFrame.Timestamp;

                var numPixels = GetEffectiveWidth(bitmap) * GetEffectiveHeight(bitmap);
                if (force || diff.Diffs.Count > numPixels / 2)
                {
                    writer.Write(serializer.SerializeFrame(rawFrame));
                    FramesWritten++;
                }
                else if (diff.Diffs.Count > 0)
                {
                    writer.Write(serializer.SerializeFrame(diff));
                    FramesWritten++;
                }
            }

            lastFrame = rawFrame;
            return(bitmap);
        }
        /// <summary>
        /// Writes the given bitmap image as a frame to the stream.  If this is the first image or more than half of the pixels have
        /// changed then a raw frame will be written.   Otherwise, a diff frame will be written.
        ///
        /// This method uses the system's wall clock to determine the timestamp for this frame. The timestamp will be
        /// relative to the wall clock time when the first frame was written.
        /// </summary>
        /// <param name="bitmap">the image to write</param>
        /// <returns>the same bitmap that was passed in</returns>
        public ConsoleBitmap WriteFrame(ConsoleBitmap bitmap)
        {
            var rawFrame = GetRawFrame(bitmap);

            var now = DateTime.UtcNow;

            if (firstFrameTime.HasValue == false)
            {
                rawFrame.Timestamp = TimeSpan.Zero;
                firstFrameTime     = now;
            }
            else
            {
                rawFrame.Timestamp = now - firstFrameTime.Value;
            }

            var frameTime = firstFrameTime.HasValue == false ? TimeSpan.Zero : now - firstFrameTime.Value;

            if (lastFrame == null)
            {
                rawFrame.Timestamp = frameTime;
                StreamHeader(bitmap);
                writer.Write(serializer.SerializeFrame(rawFrame));
            }
            else
            {
                var diff = PrepareDiffFrame(bitmap);
                diff.Timestamp = frameTime;
                if (diff.Diffs.Count > bitmap.Width * bitmap.Height / 2)
                {
                    writer.Write(serializer.SerializeFrame(rawFrame));
                }
                else if (diff.Diffs.Count > 0)
                {
                    writer.Write(serializer.SerializeFrame(diff));
                }
            }

            lastFrame = rawFrame;
            return(bitmap);
        }
Exemple #3
0
        /// <summary>
        /// Writes the given bitmap image as a frame to the stream.  If this is the first image or more than half of the pixels have
        /// changed then a raw frame will be written.   Otherwise, a diff frame will be written.
        ///
        /// This method uses the system's wall clock to determine the timestamp for this frame. The timestamp will be
        /// relative to the wall clock time when the first frame was written.
        /// </summary>
        /// <param name="bitmap">the image to write</param>
        /// <param name="desiredFrameTime">if provided, sstamp the frame with this time, otherwise stamp it with the wall clock delta from the first frame time</param>
        /// <param name="force">if true, writes the frame even if there are no changes</param>
        /// <returns>the same bitmap that was passed in</returns>
        public ConsoleBitmap WriteFrame(ConsoleBitmap bitmap, bool force = false, TimeSpan?desiredFrameTime = null)
        {
            var rawFrame = GetRawFrame(bitmap);

            var now = DateTime.UtcNow;

            if (firstFrameTime.HasValue == false)
            {
                rawFrame.Timestamp = TimeSpan.Zero;
                firstFrameTime     = now;
            }
            else
            {
                rawFrame.Timestamp = desiredFrameTime.HasValue ? desiredFrameTime.Value : now - firstFrameTime.Value;
            }

            if (lastFrame == null)
            {
                StreamHeader(bitmap);
                writer.Write(serializer.SerializeFrame(rawFrame));
                FramesWritten++;
            }
            else
            {
                var diff = PrepareDiffFrame(bitmap);
                diff.Timestamp = rawFrame.Timestamp;
                if (force || diff.Diffs.Count > bitmap.Width * bitmap.Height / 2)
                {
                    writer.Write(serializer.SerializeFrame(rawFrame));
                    FramesWritten++;
                }
                else if (diff.Diffs.Count > 0)
                {
                    writer.Write(serializer.SerializeFrame(diff));
                    FramesWritten++;
                }
            }

            lastFrame = rawFrame;
            return(bitmap);
        }