Esempio n. 1
0
        public void Close_Closes()
        {
            var ffmpegMock = new Mock <FFmpegClient>();

            ffmpegMock
            .Setup(c => c.CloseFormatContext(It.IsAny <AVFormatContext>()))
            .Verifiable();
            var ffmpeg = ffmpegMock.Object;

            var nativeAVFormatContext = new NativeAVFormatContext
            {
                nb_streams = 1,
            };

            var nativeIOContext = new NativeAVIOContext
            {
            };

            using (var handle = new AVFormatContextHandle(ffmpeg, &nativeAVFormatContext))
                using (var ioContext = new AVIOContext(ffmpeg, new AVIOContextHandle(ffmpeg, &nativeIOContext)))
                    using (var formatContext = new AVFormatContext(ffmpeg, ioContext, handle))
                    {
                        formatContext.Close();
                    }

            ffmpegMock.Verify();
        }
Esempio n. 2
0
        public void Constructor_InitializesInstance()
        {
            var ffmpegMock = new Mock <FFmpegClient>();

            var bytes = new byte[] { (byte)'t', (byte)'e', (byte)'s', (byte)'t' };

            var bytesHandle = Marshal.AllocHGlobal(bytes.Length);

            Marshal.Copy(bytes, 0, bytesHandle, bytes.Length);

            var nativeIOContext = new NativeAVIOContext
            {
                buffer = (byte *)bytesHandle,
                pos    = 5,
            };

            ffmpegMock
            .Setup(c => c.FreeAVHandle(It.IsAny <SafeHandle>()))
            .Verifiable();

            var ffmpeg = ffmpegMock.Object;

            using (var handle = new AVIOContextHandle(ffmpeg, &nativeIOContext))
                using (var context = new AVIOContext(ffmpeg, handle))
                {
                    Assert.Equal((int)&nativeIOContext, (int)context.NativeObject);
                    var result = new byte[4];
                    Marshal.Copy((IntPtr)context.Buffer.NativeObject, result, 0, bytes.Length);
                    Assert.Equal(bytes, result);
                }

            ffmpegMock.Verify();

            Marshal.FreeHGlobal(bytesHandle);
        }
Esempio n. 3
0
        public void Disposes_Twice()
        {
            var ffmpegMock = new Mock <FFmpegClient>();

            ffmpegMock
            .Setup(c => c.CloseFormatContext(It.IsAny <AVFormatContext>()))
            .Verifiable();
            var ffmpeg = ffmpegMock.Object;

            var nativeAVFormatContext = new NativeAVFormatContext
            {
                nb_streams = 1,
            };

            var nativeIOContext = new NativeAVIOContext
            {
            };

            using (var handle = new AVFormatContextHandle(ffmpeg, &nativeAVFormatContext))
            {
                var ioContextMock = new Mock <AVIOContext>(ffmpeg, new AVIOContextHandle(ffmpeg, &nativeIOContext))
                {
                    CallBase = true,
                };

                var formatContext = new AVFormatContext(ffmpeg, ioContextMock.Object, handle);

                formatContext.Dispose();
                formatContext.Dispose();

                ioContextMock.Verify(c => c.Dispose(), Times.Once);
            }
        }
Esempio n. 4
0
        public void GetVideoStream_ThrowsOnMultipleStreams()
        {
            var ffmpegMock = new Mock <FFmpegClient>();

            ffmpegMock
            .Setup(c => c.FreeAVFormatContext(It.IsAny <IntPtr>()))
            .Verifiable();
            var ffmpeg = ffmpegMock.Object;

            var nativeAVFormatContext = new NativeAVFormatContext
            {
                nb_streams = 2,
            };

            var nativeIOContext = new NativeAVIOContext
            {
            };

            using (var handle = new AVFormatContextHandle(ffmpeg, &nativeAVFormatContext))
                using (var ioContext = new AVIOContext(ffmpeg, new AVIOContextHandle(ffmpeg, &nativeIOContext)))
                    using (var formatContext = new AVFormatContext(ffmpeg, ioContext, handle))
                    {
                        Assert.Throws <InvalidOperationException>(() => formatContext.GetVideoStream());
                    }

            ffmpegMock.Verify();
        }
Esempio n. 5
0
        public void GetStream_ThrowsOnIllegalIndex()
        {
            var ffmpegMock = new Mock <FFmpegClient>();

            ffmpegMock
            .Setup(c => c.FreeAVFormatContext(It.IsAny <IntPtr>()))
            .Verifiable();
            var ffmpeg = ffmpegMock.Object;

            var nativeAVFormatContext = new NativeAVFormatContext
            {
                nb_streams = 1,
            };

            var nativeIOContext = new NativeAVIOContext
            {
            };

            using (var handle = new AVFormatContextHandle(ffmpeg, &nativeAVFormatContext))
                using (var ioContext = new AVIOContext(ffmpeg, new AVIOContextHandle(ffmpeg, &nativeIOContext)))
                    using (var formatContext = new AVFormatContext(ffmpeg, ioContext, handle))
                    {
                        Assert.Throws <ArgumentOutOfRangeException>("index", () => formatContext.GetStream(20));
                        Assert.Throws <ArgumentOutOfRangeException>("index", () => formatContext.GetStream(-1));
                    }

            ffmpegMock.Verify();
        }
Esempio n. 6
0
        public void Constructor_InitializesInstance()
        {
            var ffmpegMock = new Mock <FFmpegClient>();

            var codecParameters = new NativeAVCodecParameters
            {
                codec_type = NativeAVMediaType.AVMEDIA_TYPE_VIDEO,
            };

            var nativeStream = new NativeAVStream
            {
                codecpar = &codecParameters,
            };

            var streamPtr = new IntPtr(&nativeStream);

            var nativeAVFormatContext = new NativeAVFormatContext
            {
                duration    = 10,
                nb_streams  = 1,
                event_flags = (int)AVFormatContextEventFlags.MetadataUpdated,
                ctx_flags   = (int)AVFormatContextFlags.NoHeader,
                streams     = (NativeAVStream **)streamPtr,
            };

            var nativeIOContext = new NativeAVIOContext
            {
            };

            ffmpegMock
            .Setup(c => c.FreeAVFormatContext(It.IsAny <IntPtr>()))
            .Verifiable();
            var ffmpeg = ffmpegMock.Object;

            using (var handle = new AVFormatContextHandle(ffmpeg, &nativeAVFormatContext))
                using (var ioContext = new AVIOContext(ffmpeg, new AVIOContextHandle(ffmpeg, &nativeIOContext)))
                    using (var formatContext = new AVFormatContext(ffmpeg, ioContext, handle))
                    {
                        Assert.Equal(handle, formatContext.Handle);
                        Assert.Equal <uint>(1, formatContext.StreamCount);
                        Assert.False(formatContext.IsClosed);
                        Assert.False(handle.IsClosed);
                        Assert.Equal((int)AVFormatContextEventFlags.MetadataUpdated, (int)formatContext.EventFlags);
                        Assert.Equal((int)AVFormatContextFlags.NoHeader, (int)formatContext.Flags);
                    }

            ffmpegMock.Verify();
        }
Esempio n. 7
0
        public void Constuctor_InitializesInstance()
        {
            var ffmpegMock = new Mock <FFmpegClient>();

            var nativeIOContext = new NativeAVIOContext
            {
                pos = 5,
            };

            ffmpegMock
            .Setup(c => c.FreeAVHandle(It.IsAny <SafeHandle>()))
            .Verifiable();

            using (var handle = new AVIOContextHandle(ffmpegMock.Object, &nativeIOContext))
            {
                Assert.Equal((int)&nativeIOContext, (int)handle.DangerousGetHandle().ToPointer());
            }

            ffmpegMock.Verify();
        }
Esempio n. 8
0
        public void GetVideoStream_ThrowsOnWrongType()
        {
            var ffmpegMock = new Mock <FFmpegClient>();

            ffmpegMock
            .Setup(c => c.FreeAVFormatContext(It.IsAny <IntPtr>()))
            .Verifiable();
            var ffmpeg = ffmpegMock.Object;

            var codecParameters = new NativeAVCodecParameters
            {
                format     = 12346,
                codec_type = NativeAVMediaType.AVMEDIA_TYPE_AUDIO,
            };

            var nativeStream = new NativeAVStream
            {
                codecpar = &codecParameters,
            };
            var streamPtr = new IntPtr(&nativeStream);

            var nativeAVFormatContext = new NativeAVFormatContext
            {
                nb_streams = 1,
                streams    = (NativeAVStream **)&streamPtr,
            };

            var nativeIOContext = new NativeAVIOContext
            {
            };

            using (var handle = new AVFormatContextHandle(ffmpeg, &nativeAVFormatContext))
                using (var ioContext = new AVIOContext(ffmpeg, new AVIOContextHandle(ffmpeg, &nativeIOContext)))
                    using (var formatContext = new AVFormatContext(ffmpeg, ioContext, handle))
                    {
                        Assert.Throws <InvalidOperationException>(() => formatContext.GetVideoStream());
                    }

            ffmpegMock.Verify();
        }
Esempio n. 9
0
        public void GetStream_ReturnsStream()
        {
            var ffmpegMock = new Mock <FFmpegClient>();

            ffmpegMock
            .Setup(c => c.FreeAVFormatContext(It.IsAny <IntPtr>()))
            .Verifiable();
            var ffmpeg = ffmpegMock.Object;

            var codecParameters = new NativeAVCodecParameters
            {
                format = 12346,
            };

            var nativeStream = new NativeAVStream
            {
                codecpar = &codecParameters,
            };
            var streamPtr = new IntPtr(&nativeStream);

            var nativeAVFormatContext = new NativeAVFormatContext
            {
                nb_streams = 1,
                streams    = (NativeAVStream **)&streamPtr,
            };

            var nativeIOContext = new NativeAVIOContext
            {
            };

            using (var handle = new AVFormatContextHandle(ffmpeg, &nativeAVFormatContext))
                using (var ioContext = new AVIOContext(ffmpeg, new AVIOContextHandle(ffmpeg, &nativeIOContext)))
                    using (var formatContext = new AVFormatContext(ffmpeg, ioContext, handle))
                    {
                        var stream = formatContext.GetStream(0);
                        Assert.Equal(12346, stream.CodecParameters.Format);
                    }

            ffmpegMock.Verify();
        }
Esempio n. 10
0
        public void IOContext_Get()
        {
            var ffmpegMock = new Mock <FFmpegClient>();

            ffmpegMock
            .Setup(c => c.FreeAVFormatContext(It.IsAny <IntPtr>()))
            .Verifiable();
            var ffmpeg = ffmpegMock.Object;

            var bytes = new byte[] { (byte)'t', (byte)'e', (byte)'s', (byte)'t' };

            var bytesHandle = Marshal.AllocHGlobal(bytes.Length);

            Marshal.Copy(bytes, 0, bytesHandle, bytes.Length);

            var nativeIOContext = new NativeAVIOContext
            {
                buffer = (byte *)bytesHandle,
                pos    = 5,
            };

            var nativeAVFormatContext = new NativeAVFormatContext
            {
                pb = &nativeIOContext,
            };

            using (var handle = new AVFormatContextHandle(ffmpeg, &nativeAVFormatContext))
                using (var ioContext = new AVIOContext(ffmpeg, new AVIOContextHandle(ffmpeg, &nativeIOContext)))
                    using (var formatContext = new AVFormatContext(ffmpeg, ioContext, handle))
                    {
                        var result = new byte[4];
                        Marshal.Copy((IntPtr)formatContext.IOContext.Buffer.NativeObject, result, 0, bytes.Length);
                        Assert.Equal(bytes, result);
                    }

            ffmpegMock.Verify();

            Marshal.FreeHGlobal(bytesHandle);
        }