Beispiel #1
0
        internal static ArraySegment <byte> OpenSslRentEncode <THandle>(
            GetEncodedSizeFunc <THandle> getSize,
            EncodeFunc <THandle> encode,
            THandle handle)
            where THandle : SafeHandle
        {
            int size = getSize(handle);

            if (size < 1)
            {
                throw CreateOpenSslCryptographicException();
            }

            byte[] data = ArrayPool <byte> .Shared.Rent(size);

            int size2 = encode(handle, data);

            if (size2 < 1)
            {
                Debug.Fail(
                    $"{nameof(OpenSslEncode)}: {nameof(getSize)} succeeded ({size}) and {nameof(encode)} failed ({size2})");

                // Since we don't know what was written, assume it was secret and clear the value.
                // (It doesn't matter much, since we're behind Debug.Fail)
                ArrayPool <byte> .Shared.Return(data, clearArray : true);

                // If it ever happens, ensure the error queue gets cleared.
                // And since it didn't write the data, reporting an exception is good too.
                throw CreateOpenSslCryptographicException();
            }

            Debug.Assert(size == size2);

            return(new ArraySegment <byte>(data, 0, size2));
        }
Beispiel #2
0
        internal static byte[] OpenSslEncode <THandle>(
            GetEncodedSizeFunc <THandle> getSize,
            EncodeFunc <THandle> encode,
            THandle handle)
            where THandle : SafeHandle
        {
            int size = getSize(handle);

            if (size < 1)
            {
                throw CreateOpenSslCryptographicException();
            }

            byte[] data = new byte[size];

            int size2 = encode(handle, data);

            if (size2 < 1)
            {
                Debug.Fail(
                    $"{nameof(OpenSslEncode)}: {nameof(getSize)} succeeded ({size}) and {nameof(encode)} failed ({size2})");

                // If it ever happens, ensure the error queue gets cleared.
                // And since it didn't write the data, reporting an exception is good too.
                throw CreateOpenSslCryptographicException();
            }

            Debug.Assert(size == size2);

            return(data);
        }
Beispiel #3
0
        public OpenH264Encoder(string dllName, int width, int height, int bps, int fps, int keyframeInterval)
        {
            this.Width            = width;
            this.Height           = height;
            this.KeyframeInterval = keyframeInterval;
            bps = bps / 2;

            unsafe
            {
                EncodeFunc encodeFunc = (byte *data, int *sizes, int sizeCount, int layerSize, FrameType frameType, uint frameNum) =>
                {
                    var keyFrame = (frameType == FrameType.IDR) || (frameType == FrameType.I);
                    var d        = new byte[layerSize];
                    Marshal.Copy((IntPtr)data, d, 0, layerSize);
                    var now = DateTime.Now;
                    if (dateTimeQueue.TryDequeue(out var dt))
                    {
                        now = dt;
                    }
                    var df = new DataFrame()
                    {
                        StartTime  = now,
                        Data       = d,
                        DataLength = layerSize,
                        FrameNum   = EncodeNum++,
                        KeyFrame   = keyFrame
                    };
                    EncodeResult?.Invoke(df);
                };

                delegateRefs.Add(encodeFunc);

                this.encodeFuncContext = new EncodeFuncContext {
                    Pointer = Marshal.GetFunctionPointerForDelegate(encodeFunc)
                };

                InitEncoder(dllName, width, height, bps, fps, keyframeInterval, encodeFunc);
            }
        }
Beispiel #4
0
        internal static byte[] OpenSslEncode <THandle>(GetEncodedSizeFunc <THandle> getSize, EncodeFunc <THandle> encode, THandle handle)
            where THandle : SafeHandle
        {
            int size = getSize(handle);

            if (size < 1)
            {
                throw Crypto.CreateOpenSslCryptographicException();
            }

            byte[] data = new byte[size];

            int size2 = encode(handle, data);

            Debug.Assert(size == size2);

            return(data);
        }
Beispiel #5
0
 public static extern void SetEncodeFunc(EncodeFunc func);
Beispiel #6
0
 static extern void InitEncoder(string dllName, int width, int height, int bps, int fps, int keyframeInterval, EncodeFunc encodeFunc);