< Summary

Information
Class: LiveKit.Rtc.Internal.NativeMethods
Assembly: LivekitRtc
File(s): /home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitRtc/Internal/NativeMethods.cs
Line coverage
89%
Covered lines: 17
Uncovered lines: 2
Coverable lines: 19
Total lines: 155
Line coverage: 89.4%
Branch coverage
50%
Covered branches: 5
Total branches: 10
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Initialize(...)50%88100%
StringToUtf8Null(...)50%2280%

File(s)

/home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitRtc/Internal/NativeMethods.cs

#LineLine coverage
 1// author: https://github.com/pabloFuente
 2
 3using System;
 4using System.Runtime.InteropServices;
 5using System.Security;
 6using System.Text;
 7
 8namespace LiveKit.Rtc.Internal
 9{
 10    /// <summary>
 11    /// Delegate for receiving FFI events from the native library.
 12    /// </summary>
 13    /// <param name="dataPtr">Pointer to the event data buffer.</param>
 14    /// <param name="dataLen">Length of the event data.</param>
 15    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
 16    internal delegate void FfiCallbackDelegate(IntPtr dataPtr, UIntPtr dataLen);
 17
 18    /// <summary>
 19    /// P/Invoke declarations for the LiveKit FFI native library.
 20    /// </summary>
 21    [SuppressUnmanagedCodeSecurity]
 22    internal static class NativeMethods
 23    {
 24        private const string LibName = "livekit_ffi";
 25
 26        /// <summary>
 27        /// Initialize the LiveKit FFI library (internal - use the wrapper method).
 28        /// </summary>
 29        [DllImport(
 30            LibName,
 31            CallingConvention = CallingConvention.Cdecl,
 32            EntryPoint = "livekit_ffi_initialize"
 33        )]
 34        private static extern unsafe void InitializeNative(
 35            FfiCallbackDelegate callback,
 36            [MarshalAs(UnmanagedType.I1)] bool captureLogs,
 37            byte* sdk,
 38            byte* sdkVersion
 39        );
 40
 41        /// <summary>
 42        /// Initialize the LiveKit FFI library.
 43        /// </summary>
 44        /// <param name="callback">Callback function for receiving events.</param>
 45        /// <param name="captureLogs">Whether to capture and forward logs.</param>
 46        /// <param name="sdk">SDK identifier string.</param>
 47        /// <param name="sdkVersion">SDK version string.</param>
 48        internal static unsafe void Initialize(
 49            FfiCallbackDelegate callback,
 50            bool captureLogs,
 51            string sdk,
 52            string sdkVersion
 53        )
 154        {
 55            // Convert strings to null-terminated UTF-8 byte arrays
 156            var sdkBytes = StringToUtf8Null(sdk);
 157            var sdkVersionBytes = StringToUtf8Null(sdkVersion);
 58
 159            fixed (byte* sdkPtr = sdkBytes)
 160            fixed (byte* sdkVersionPtr = sdkVersionBytes)
 161            {
 162                InitializeNative(callback, captureLogs, sdkPtr, sdkVersionPtr);
 163            }
 164        }
 65
 66        /// <summary>
 67        /// Converts a string to a null-terminated UTF-8 byte array.
 68        /// </summary>
 69        private static byte[] StringToUtf8Null(string str)
 270        {
 271            if (str == null)
 072            {
 073                return new byte[] { 0 };
 74            }
 75
 276            var utf8 = Encoding.UTF8.GetBytes(str);
 277            var result = new byte[utf8.Length + 1];
 278            Buffer.BlockCopy(utf8, 0, result, 0, utf8.Length);
 279            result[utf8.Length] = 0; // Null terminator
 280            return result;
 281        }
 82
 83        /// <summary>
 84        /// Send an FFI request to the native library. Every length is a Rust <c>usize</c>, so
 85        /// <see cref="UIntPtr"/>: an <c>int</c> leaves the upper half of the register undefined.
 86        /// </summary>
 87        /// <param name="data">Pointer to the request data buffer.</param>
 88        /// <param name="len">Length of the request data.</param>
 89        /// <param name="dataPtr">Output pointer to the response data buffer.</param>
 90        /// <param name="dataLen">Output length of the response data.</param>
 91        /// <returns>Handle ID for the request (used for cleanup).</returns>
 92        [DllImport(
 93            LibName,
 94            CallingConvention = CallingConvention.Cdecl,
 95            EntryPoint = "livekit_ffi_request"
 96        )]
 97        internal static extern unsafe ulong Request(
 98            byte* data,
 99            UIntPtr len,
 100            out byte* dataPtr,
 101            out UIntPtr dataLen
 102        );
 103
 104        /// <summary>
 105        /// Drop (release) a handle returned by the FFI library.
 106        /// </summary>
 107        /// <param name="handleId">The handle ID to release.</param>
 108        /// <returns>True if the handle was successfully dropped.</returns>
 109        [DllImport(
 110            LibName,
 111            CallingConvention = CallingConvention.Cdecl,
 112            EntryPoint = "livekit_ffi_drop_handle"
 113        )]
 114        [return: MarshalAs(UnmanagedType.I1)]
 115        internal static extern bool DropHandle(ulong handleId);
 116
 117        /// <summary>
 118        /// Dispose and cleanup the FFI library.
 119        /// </summary>
 120        [DllImport(
 121            LibName,
 122            CallingConvention = CallingConvention.Cdecl,
 123            EntryPoint = "livekit_ffi_dispose"
 124        )]
 125        internal static extern void Dispose();
 126
 127        /// <summary>
 128        /// Copy a buffer from native memory to managed memory.
 129        /// </summary>
 130        /// <param name="ptr">Pointer to the native buffer.</param>
 131        /// <param name="len">Length of the buffer.</param>
 132        /// <param name="dst">Destination managed buffer.</param>
 133        [DllImport(
 134            LibName,
 135            CallingConvention = CallingConvention.Cdecl,
 136            EntryPoint = "livekit_copy_buffer"
 137        )]
 138        // Not exported by livekit-ffi (only initialize, dispose, request and drop_handle are):
 139        // FfiClient.CopyBuffer is implemented in managed code
 140        internal static extern unsafe void CopyBuffer(ulong ptr, UIntPtr len, byte* dst);
 141
 142        /// <summary>
 143        /// Retrieve a pointer from data.
 144        /// </summary>
 145        /// <param name="data">Pointer to the data.</param>
 146        /// <param name="len">Length of the data.</param>
 147        /// <returns>The pointer value.</returns>
 148        [DllImport(
 149            LibName,
 150            CallingConvention = CallingConvention.Cdecl,
 151            EntryPoint = "livekit_retrieve_ptr"
 152        )]
 153        internal static extern unsafe ulong RetrievePtr(byte* data, UIntPtr len);
 154    }
 155}