Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new Flash screen video.
        /// </summary>
        /// <param name="parameters">The video parameters.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="parameters"/> is null.</exception>
        public FlashScreenVideo(FlashScreenVideoParameters parameters)
            : base(parameters)
        {
            width = parameters.Width;
            height = parameters.Height;
            framesPerSecond = parameters.FramesPerSecond;
            nominalBlockWidth = parameters.BlockWidth;
            nominalBlockHeight = parameters.BlockHeight;
            keyFramePeriodInFrames = Math.Max(1, (int) Math.Ceiling(parameters.KeyFramePeriod.TotalSeconds * framesPerSecond));

            cols = (width + nominalBlockWidth - 1) / nominalBlockWidth;
            rows = (height + nominalBlockHeight - 1) / nominalBlockHeight;

            int framePixelsLength = width * height;

            deflater = new Deflater(parameters.CompressionLevel, false);
            reserveBytesPerFrame = (nominalBlockHeight * nominalBlockWidth * 3 + 2 /*block header size*/ + ZLibWorstCaseInflation) * rows * cols + 4 /*frame header size*/;

            previousFramePixels = new int[framePixelsLength];
            currentFramePixels = new int[framePixelsLength];
            blockBuffer = new byte[nominalBlockHeight * nominalBlockWidth * 3];

            flvWriter = new FlvWriter(FlvWriter.FlvFlags.Video);
            var flvMetadata = new FlvWriter.FlvMetadata()
            {
                { "duration", 0.0 },
                { "width", width },
                { "height", height },
                { "framerate", framesPerSecond },
                { "videocodecid", 3 /*screen video*/ },
                { "canSeekToEnd", true }
            };
            flvMetadataUpdater = flvWriter.WriteFlvMetaFrame(flvMetadata, 0);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new Flash screen video.
        /// </summary>
        /// <param name="parameters">The video parameters.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="parameters"/> is null.</exception>
        public FlashScreenVideo(FlashScreenVideoParameters parameters)
            : base(parameters)
        {
            width                  = parameters.Width;
            height                 = parameters.Height;
            framesPerSecond        = parameters.FramesPerSecond;
            nominalBlockWidth      = parameters.BlockWidth;
            nominalBlockHeight     = parameters.BlockHeight;
            keyFramePeriodInFrames = Math.Max(1, (int)Math.Ceiling(parameters.KeyFramePeriod.TotalSeconds * framesPerSecond));

            cols = (width + nominalBlockWidth - 1) / nominalBlockWidth;
            rows = (height + nominalBlockHeight - 1) / nominalBlockHeight;

            int framePixelsLength = width * height;

            deflater             = new Deflater(parameters.CompressionLevel, false);
            reserveBytesPerFrame = (nominalBlockHeight * nominalBlockWidth * 3 + 2 /*block header size*/ + ZLibWorstCaseInflation) * rows * cols + 4 /*frame header size*/;

            previousFramePixels = new int[framePixelsLength];
            currentFramePixels  = new int[framePixelsLength];
            blockBuffer         = new byte[nominalBlockHeight * nominalBlockWidth * 3];

            flvWriter = new FlvWriter(FlvWriter.FlvFlags.Video);
            var flvMetadata = new FlvWriter.FlvMetadata()
            {
                { "duration", 0.0 },
                { "width", width },
                { "height", height },
                { "framerate", framesPerSecond },
                { "videocodecid", 3 /*screen video*/ },
                { "canSeekToEnd", true }
            };

            flvMetadataUpdater = flvWriter.WriteFlvMetaFrame(flvMetadata, 0);
        }
        public void CreateVideo()
        {
            const int width = 640;
            const int height = 480;
            const int frames = 30;

            Stopwatch stopwatch = Stopwatch.StartNew();
            var parameters = new FlashScreenVideoParameters(width, height, 5)
            {
                BlockWidth = 256,
                BlockHeight = 256
            };
            video = new FlashScreenVideo(parameters);
            stopwatch.Stop();

            Bitmap bitmap = new Bitmap(width, height);
            BitmapVideoFrame bitmapVideoFrame = new BitmapVideoFrame(bitmap);
            for (int frame = 0; frame < frames; frame++)
            {
                BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height),
                    ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                try
                {
                    int offset = 0;
                    for (int y = 0; y < height; y++)
                    {
                        int scanOffset = offset;

                        for (int x = 0; x < width; x++)
                        {
                            int color = (x * 255 / width) << 16 | (y * 255 / height) << 8 | frame * 255 / frames;
                            Marshal.WriteInt32(bitmapData.Scan0, scanOffset, color);

                            scanOffset += 4;
                        }

                        offset += bitmapData.Stride;
                    }
                }
                finally
                {
                    bitmap.UnlockBits(bitmapData);
                }

                stopwatch.Start();
                video.AddFrame(bitmapVideoFrame);
                stopwatch.Stop();
            }

            TestLog.WriteLine("Video encoding {2} frames at {0}x{1} took {3}s", width, height, frames, stopwatch.Elapsed.TotalSeconds);
        }
        public void FramesPerSecond_ReturnsFramesPerSecond()
        {
            var parameters = new FlashScreenVideoParameters(32, 16, 30);

            Assert.AreEqual(30, parameters.FramesPerSecond);
        }
        public void Height_ReturnsHeight()
        {
            var parameters = new FlashScreenVideoParameters(32, 16, 30);

            Assert.AreEqual(16, parameters.Height);
        }
        public void Width_ReturnsWidth()
        {
            var parameters = new FlashScreenVideoParameters(32, 16, 30);

            Assert.AreEqual(32, parameters.Width);
        }
        public void KeyFramePeriod_CanGetAndSetValue()
        {
            var parameters = new FlashScreenVideoParameters(32, 16, 30);

            Assert.AreEqual(TimeSpan.FromSeconds(2), parameters.KeyFramePeriod);

            parameters.KeyFramePeriod = TimeSpan.FromSeconds(0);
            Assert.AreEqual(TimeSpan.FromSeconds(0), parameters.KeyFramePeriod);

            var ex = Assert.Throws<ArgumentOutOfRangeException>(() => parameters.KeyFramePeriod = TimeSpan.FromSeconds(-1));
            Assert.Contains(ex.Message, "Key frame period must not be negative.");
        }
        public void CompressionLevel_CanGetAndSetValue()
        {
            var parameters = new FlashScreenVideoParameters(32, 16, 30);

            Assert.AreEqual(6, parameters.CompressionLevel);

            parameters.CompressionLevel = 9;
            Assert.AreEqual(9, parameters.CompressionLevel);

            parameters.CompressionLevel = 0;
            Assert.AreEqual(0, parameters.CompressionLevel);

            var ex = Assert.Throws<ArgumentOutOfRangeException>(() => parameters.CompressionLevel = -1);
            Assert.Contains(ex.Message, "Compression level must be between 0 and 9.");

            ex = Assert.Throws<ArgumentOutOfRangeException>(() => parameters.CompressionLevel = 10);
            Assert.Contains(ex.Message, "Compression level must be between 0 and 9.");
        }
        public void BlockHeight_CanGetAndSetValue()
        {
            var parameters = new FlashScreenVideoParameters(32, 16, 30);

            Assert.AreEqual(64, parameters.BlockHeight);

            parameters.BlockHeight = 256;
            Assert.AreEqual(256, parameters.BlockHeight);

            parameters.BlockHeight = 16;
            Assert.AreEqual(16, parameters.BlockHeight);

            var ex = Assert.Throws<ArgumentOutOfRangeException>(() => parameters.BlockHeight = 0);
            Assert.Contains(ex.Message, "Block height must be a multiple of 16 between 16 and 256.");

            ex = Assert.Throws<ArgumentOutOfRangeException>(() => parameters.BlockHeight = 256 + 16);
            Assert.Contains(ex.Message, "Block height must be a multiple of 16 between 16 and 256.");

            ex = Assert.Throws<ArgumentOutOfRangeException>(() => parameters.BlockHeight = 17);
            Assert.Contains(ex.Message, "Block height must be a multiple of 16 between 16 and 256.");
        }