| | | 1 | | // author: https://github.com/pabloFuente |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Runtime.InteropServices; |
| | | 5 | | using System.Security; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | namespace 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 | | ) |
| | 1 | 54 | | { |
| | | 55 | | // Convert strings to null-terminated UTF-8 byte arrays |
| | 1 | 56 | | var sdkBytes = StringToUtf8Null(sdk); |
| | 1 | 57 | | var sdkVersionBytes = StringToUtf8Null(sdkVersion); |
| | | 58 | | |
| | 1 | 59 | | fixed (byte* sdkPtr = sdkBytes) |
| | 1 | 60 | | fixed (byte* sdkVersionPtr = sdkVersionBytes) |
| | 1 | 61 | | { |
| | 1 | 62 | | InitializeNative(callback, captureLogs, sdkPtr, sdkVersionPtr); |
| | 1 | 63 | | } |
| | 1 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Converts a string to a null-terminated UTF-8 byte array. |
| | | 68 | | /// </summary> |
| | | 69 | | private static byte[] StringToUtf8Null(string str) |
| | 2 | 70 | | { |
| | 2 | 71 | | if (str == null) |
| | 0 | 72 | | { |
| | 0 | 73 | | return new byte[] { 0 }; |
| | | 74 | | } |
| | | 75 | | |
| | 2 | 76 | | var utf8 = Encoding.UTF8.GetBytes(str); |
| | 2 | 77 | | var result = new byte[utf8.Length + 1]; |
| | 2 | 78 | | Buffer.BlockCopy(utf8, 0, result, 0, utf8.Length); |
| | 2 | 79 | | result[utf8.Length] = 0; // Null terminator |
| | 2 | 80 | | return result; |
| | 2 | 81 | | } |
| | | 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 | | } |