public static SubmitStatus SubmitPacketExt(this ESPlayer player, ESPacket packet)
        {
            Dictionary <string, IntPtr> unmanagedMemories = null;

            try
            {
                unmanagedMemories = GetNativePacket(packet);

                var nativePlayerHandle = _nativePlayerFieldGetter.Invoke(player);
                var result             =
                    _submitPacketMethodCaller.Invoke(nativePlayerHandle, unmanagedMemories["unmanagedNativePacket"]);
                return(result);
            }
            catch (Exception)
            {
                throw new InvalidOperationException("Submitting espacket to native pipeline has failed.");
            }
            finally
            {
                if (unmanagedMemories != null)
                {
                    foreach (var kv in unmanagedMemories)
                    {
                        Marshal.FreeHGlobal(kv.Value);
                    }
                }
            }
        }
        private static Dictionary <string, IntPtr> GetNativePacket(ESPacket packet)
        {
            var dictionary = new Dictionary <string, IntPtr>();

            try
            {
                if (packet.buffer == null)
                {
                    return(null);
                }
                var length             = packet.buffer.Length;
                var unmanagedBufferPtr = Marshal.AllocHGlobal(length * Marshal.SizeOf(packet.buffer[0]));
                dictionary.Add("unmanagedBuffer", unmanagedBufferPtr);
                Marshal.Copy(packet.buffer, 0, unmanagedBufferPtr, length);
                var nativePacket = new NativePacket
                {
                    type               = packet.type,
                    buffer             = unmanagedBufferPtr,
                    bufferSize         = (uint)packet.buffer.Length,
                    pts                = packet.pts / 1000000UL,
                    duration           = packet.duration,
                    matroskaColorInfo  = IntPtr.Zero,
                    hdr10pMetadataSize = 0,
                    hdr10pMetadata     = IntPtr.Zero
                };
                var unmanagedNativePacketPtr = Marshal.AllocHGlobal(Marshal.SizeOf(nativePacket));
                dictionary.Add("unmanagedNativePacket", unmanagedNativePacketPtr);
                Marshal.StructureToPtr(nativePacket, unmanagedNativePacketPtr, false);
                return(dictionary);
            }
            catch (Exception)
            {
                foreach (var kv in dictionary)
                {
                    Marshal.FreeHGlobal(kv.Value);
                }
                throw;
            }
        }