| | | 1 | | // author: https://github.com/pabloFuente |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Concurrent; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Reflection; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using Google.Protobuf; |
| | | 12 | | using LiveKit.Proto; |
| | | 13 | | |
| | | 14 | | namespace LiveKit.Rtc.Internal |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Process-wide client of the native <c>livekit_ffi</c> library: sends the requests and delivers the native |
| | | 18 | | /// events. Public for the tests; applications use <see cref="Room"/> and the track, source and stream classes. |
| | | 19 | | /// </summary> |
| | | 20 | | public sealed class FfiClient : IDisposable |
| | | 21 | | { |
| | 1 | 22 | | private static readonly Lazy<FfiClient> _instance = new Lazy<FfiClient>( |
| | 1 | 23 | | () => new FfiClient(), |
| | 1 | 24 | | LazyThreadSafetyMode.ExecutionAndPublication |
| | 1 | 25 | | ); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The singleton instance. |
| | | 29 | | /// </summary> |
| | 76020 | 30 | | public static FfiClient Instance => _instance.Value; |
| | | 31 | | |
| | | 32 | | private readonly FfiCallbackDelegate _callbackDelegate; |
| | | 33 | | |
| | | 34 | | // Synchronization |
| | 1 | 35 | | private readonly object _lock = new object(); |
| | 1 | 36 | | private readonly object _initLock = new object(); |
| | | 37 | | private volatile bool _initialized; |
| | | 38 | | private volatile bool _disposed; |
| | | 39 | | |
| | | 40 | | // Event Storage |
| | 1 | 41 | | private readonly List<FfiEvent> _eventQueue = new(); |
| | 1 | 42 | | private readonly List<( |
| | 1 | 43 | | Func<FfiEvent, bool> Predicate, |
| | 1 | 44 | | TaskCompletionSource<FfiEvent> Tcs |
| | 1 | 45 | | )> _waiters = new List<(Func<FfiEvent, bool>, TaskCompletionSource<FfiEvent>)>(); |
| | | 46 | | |
| | | 47 | | // Audio, video and data track stream events go straight to their stream, by handle, so their |
| | | 48 | | // cost does not grow with the number of open streams |
| | 1 | 49 | | private readonly ConcurrentDictionary<ulong, Action<FfiEvent>> _streamHandlers = new(); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Global event for all FFI messages. Invoked outside the internal lock. |
| | | 53 | | /// </summary> |
| | | 54 | | public event EventHandler<FfiEvent>? EventReceived; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// The SDK name reported to the native library, and by it to the LiveKit server. |
| | | 58 | | /// </summary> |
| | | 59 | | public const string SdkName = "dotnet"; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// SDK version reported to the FFI and sent to the LiveKit server as the client SDK |
| | | 63 | | /// version. Read from the assembly, which the build stamps with the |
| | | 64 | | /// <c><Version></c> property of LivekitRtc.csproj, so it never has to be updated |
| | | 65 | | /// by hand on release. |
| | | 66 | | /// </summary> |
| | 1 | 67 | | public static readonly string SdkVersion = ReadSdkVersion(); |
| | | 68 | | |
| | 1 | 69 | | private FfiClient() |
| | 1 | 70 | | { |
| | 1 | 71 | | _callbackDelegate = OnFfiCallback; |
| | 1 | 72 | | } |
| | | 73 | | |
| | | 74 | | private static string ReadSdkVersion() |
| | 1 | 75 | | { |
| | 1 | 76 | | var assembly = typeof(FfiClient).Assembly; |
| | | 77 | | |
| | | 78 | | // The informational version is the csproj <Version> as written (e.g. "0.1.4" or |
| | | 79 | | // "0.2.0-beta.1"), plus "+<commit sha>" build metadata appended by SourceLink. |
| | 1 | 80 | | var informational = assembly |
| | 1 | 81 | | .GetCustomAttribute<AssemblyInformationalVersionAttribute>() |
| | 1 | 82 | | ?.InformationalVersion; |
| | 1 | 83 | | if (informational != null && informational.Length > 0) |
| | 1 | 84 | | { |
| | 1 | 85 | | var metadataStart = informational.IndexOf('+'); |
| | 1 | 86 | | return metadataStart >= 0 |
| | 1 | 87 | | ? informational.Substring(0, metadataStart) |
| | 1 | 88 | | : informational; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | // Fallback: the four-part assembly version, trimmed to major.minor.patch |
| | 0 | 92 | | var version = assembly.GetName().Version; |
| | 0 | 93 | | return version == null ? "0.0.0" : $"{version.Major}.{version.Minor}.{version.Build}"; |
| | 1 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Loads the native library and registers the event callback, once per process. |
| | | 98 | | /// <paramref name="captureLogs"/> forwards the native logs to the console. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="captureLogs">Forwards the native logs to the console when true.</param> |
| | | 101 | | public void Initialize(bool captureLogs = false) |
| | 67 | 102 | | { |
| | 67 | 103 | | if (_initialized) |
| | 66 | 104 | | return; |
| | | 105 | | |
| | 1 | 106 | | lock (_initLock) |
| | 1 | 107 | | { |
| | 1 | 108 | | if (_initialized || _disposed) |
| | 0 | 109 | | return; |
| | 1 | 110 | | NativeMethods.Initialize(_callbackDelegate, captureLogs, SdkName, SdkVersion); |
| | 1 | 111 | | _initialized = true; |
| | 1 | 112 | | } |
| | 67 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Sends a synchronous request to the Rust SDK. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="request">The request to send.</param> |
| | | 119 | | /// <returns>The native response.</returns> |
| | | 120 | | public unsafe FfiResponse SendRequest(FfiRequest request) |
| | 39214 | 121 | | { |
| | 39214 | 122 | | EnsureInitialized(); |
| | | 123 | | |
| | 39214 | 124 | | byte[] requestData = request.ToByteArray(); |
| | 39214 | 125 | | fixed (byte* dataPtr = requestData) |
| | 39214 | 126 | | { |
| | 39214 | 127 | | ulong handleId = NativeMethods.Request( |
| | 39214 | 128 | | dataPtr, |
| | 39214 | 129 | | (UIntPtr)requestData.Length, |
| | 39214 | 130 | | out byte* responsePtr, |
| | 39214 | 131 | | out UIntPtr responseLen |
| | 39214 | 132 | | ); |
| | | 133 | | |
| | 39214 | 134 | | if (handleId == FfiHandle.InvalidHandle) |
| | 10 | 135 | | throw new InvalidOperationException("FFI request failed: invalid handle"); |
| | | 136 | | |
| | | 137 | | try |
| | 39204 | 138 | | { |
| | | 139 | | // Direct parse from unmanaged memory to reduce allocations |
| | 39204 | 140 | | using var stream = new UnmanagedMemoryStream(responsePtr, (long)responseLen); |
| | 39204 | 141 | | return FfiResponse.Parser.ParseFrom(stream); |
| | | 142 | | } |
| | | 143 | | finally |
| | 39204 | 144 | | { |
| | 39204 | 145 | | NativeMethods.DropHandle(handleId); |
| | 39204 | 146 | | } |
| | | 147 | | } |
| | 39204 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Async waits for a specific event. Uses the Waiter Pattern to ensure no events |
| | | 152 | | /// are missed between a Request and a Wait call. |
| | | 153 | | /// </summary> |
| | | 154 | | /// <param name="predicate">Selects the awaited event.</param> |
| | | 155 | | /// <param name="timeout">Maximum time to wait; the default when null.</param> |
| | | 156 | | /// <param name="cancellationToken">Cancels the wait.</param> |
| | | 157 | | /// <returns>The first event that satisfies the predicate.</returns> |
| | | 158 | | public async Task<FfiEvent> WaitForEventAsync( |
| | | 159 | | Func<FfiEvent, bool> predicate, |
| | | 160 | | TimeSpan? timeout = null, |
| | | 161 | | CancellationToken cancellationToken = default |
| | | 162 | | ) |
| | 27354 | 163 | | { |
| | 27354 | 164 | | TaskCompletionSource<FfiEvent> tcs = new( |
| | 27354 | 165 | | TaskCreationOptions.RunContinuationsAsynchronously |
| | 27354 | 166 | | ); |
| | | 167 | | |
| | 27354 | 168 | | lock (_lock) |
| | 27354 | 169 | | { |
| | | 170 | | // 1. Check if the event is already in the queue |
| | 54713374 | 171 | | for (int i = 0; i < _eventQueue.Count; i++) |
| | 27329971 | 172 | | { |
| | 27329971 | 173 | | if (predicate(_eventQueue[i])) |
| | 638 | 174 | | { |
| | 638 | 175 | | var evt = _eventQueue[i]; |
| | 638 | 176 | | _eventQueue.RemoveAt(i); // Preserves order of remaining events |
| | 638 | 177 | | return evt; |
| | | 178 | | } |
| | 27329333 | 179 | | } |
| | | 180 | | |
| | | 181 | | // 2. Not found in queue; register a waiter while holding the lock |
| | 26716 | 182 | | _waiters.Add((predicate, tcs)); |
| | 26716 | 183 | | } |
| | | 184 | | |
| | 26716 | 185 | | using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 26716 | 186 | | if (timeout.HasValue) |
| | 26715 | 187 | | cts.CancelAfter(timeout.Value); |
| | | 188 | | |
| | 26716 | 189 | | using var registration = cts.Token.Register(() => |
| | 472 | 190 | | { |
| | 472 | 191 | | lock (_lock) |
| | 11595 | 192 | | _waiters.RemoveAll(w => w.Tcs == tcs); |
| | 472 | 193 | | tcs.TrySetCanceled(); |
| | 27188 | 194 | | }); |
| | | 195 | | |
| | | 196 | | try |
| | 26716 | 197 | | { |
| | 26716 | 198 | | return await tcs.Task.ConfigureAwait(false); |
| | | 199 | | } |
| | 472 | 200 | | catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) |
| | 327 | 201 | | { |
| | 327 | 202 | | throw new TimeoutException( |
| | 327 | 203 | | $"FFI event wait timed out after {timeout?.TotalSeconds}s" |
| | 327 | 204 | | ); |
| | | 205 | | } |
| | 26882 | 206 | | } |
| | | 207 | | |
| | | 208 | | private void OnFfiCallback(IntPtr dataPtr, UIntPtr dataLen) |
| | 42881 | 209 | | { |
| | 42881 | 210 | | if (_disposed) |
| | 0 | 211 | | return; |
| | | 212 | | |
| | | 213 | | try |
| | 42881 | 214 | | { |
| | | 215 | | FfiEvent ffiEvent; |
| | | 216 | | unsafe |
| | 42881 | 217 | | { |
| | 42881 | 218 | | using var stream = new UnmanagedMemoryStream((byte*)dataPtr, (long)dataLen); |
| | 42881 | 219 | | ffiEvent = FfiEvent.Parser.ParseFrom(stream); |
| | 42881 | 220 | | } |
| | | 221 | | |
| | 42881 | 222 | | if (ffiEvent.MessageCase == FfiEvent.MessageOneofCase.Logs) |
| | 0 | 223 | | { |
| | 0 | 224 | | foreach (var record in ffiEvent.Logs.Records) |
| | 0 | 225 | | LogRecord(record); |
| | 0 | 226 | | return; |
| | | 227 | | } |
| | | 228 | | |
| | 42881 | 229 | | if (ffiEvent.MessageCase == FfiEvent.MessageOneofCase.Panic) |
| | 0 | 230 | | { |
| | 0 | 231 | | Console.Error.WriteLine($"FFI Panic: {ffiEvent.Panic.Message}"); |
| | 0 | 232 | | Environment.Exit(1); |
| | 0 | 233 | | return; |
| | | 234 | | } |
| | | 235 | | |
| | 42881 | 236 | | List<TaskCompletionSource<FfiEvent>> toTrigger = new(); |
| | | 237 | | |
| | 42881 | 238 | | lock (_lock) |
| | 42881 | 239 | | { |
| | 42881 | 240 | | if (_disposed) |
| | 0 | 241 | | return; |
| | | 242 | | |
| | | 243 | | // Match against active waiters |
| | 1523508 | 244 | | for (int i = _waiters.Count - 1; i >= 0; i--) |
| | 718873 | 245 | | { |
| | 718873 | 246 | | if (_waiters[i].Predicate(ffiEvent)) |
| | 26244 | 247 | | { |
| | 26244 | 248 | | toTrigger.Add(_waiters[i].Tcs); |
| | 26244 | 249 | | _waiters.RemoveAt(i); |
| | 26244 | 250 | | } |
| | 718873 | 251 | | } |
| | | 252 | | |
| | | 253 | | // If no one is waiting for this specifically, queue it, unless it is a kind |
| | | 254 | | // that is only ever consumed through EventReceived (room, track and stream |
| | | 255 | | // fan-out events): those would fill the queue, evict pending callbacks and |
| | | 256 | | // make every wait scan a thousand stale frames. |
| | 42881 | 257 | | if (toTrigger.Count == 0 && IsClaimable(ffiEvent)) |
| | 3760 | 258 | | { |
| | 3760 | 259 | | Enqueue(ffiEvent); |
| | 3760 | 260 | | } |
| | 42881 | 261 | | } |
| | | 262 | | |
| | | 263 | | // Trigger TaskCompletionSources and Events OUTSIDE the lock to prevent deadlocks |
| | 181131 | 264 | | foreach (var tcs in toTrigger) |
| | 26244 | 265 | | { |
| | | 266 | | // The waiter may have been cancelled or timed out meanwhile: keep the event |
| | | 267 | | // for a later wait on the same predicate instead of dropping it. |
| | 26244 | 268 | | if (!tcs.TrySetResult(ffiEvent)) |
| | 0 | 269 | | { |
| | 0 | 270 | | lock (_lock) |
| | 0 | 271 | | { |
| | 0 | 272 | | Enqueue(ffiEvent); |
| | 0 | 273 | | } |
| | 0 | 274 | | } |
| | 26244 | 275 | | } |
| | 42881 | 276 | | DispatchToStream(ffiEvent); |
| | 42881 | 277 | | RaiseEventReceived(ffiEvent); |
| | 42881 | 278 | | } |
| | 0 | 279 | | catch (Exception ex) |
| | 0 | 280 | | { |
| | 0 | 281 | | Console.Error.WriteLine($"Error in FFI Callback: {ex}"); |
| | 0 | 282 | | } |
| | 42881 | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Delivers the audio, video, data track or data stream reader events of |
| | | 287 | | /// <paramref name="streamHandle"/> to <paramref name="handler"/>, on the native thread that |
| | | 288 | | /// raised them. They still reach <see cref="EventReceived"/> too. |
| | | 289 | | /// </summary> |
| | | 290 | | /// <param name="streamHandle">Native handle of the stream or reader.</param> |
| | | 291 | | /// <param name="handler">Receives the events; it must not block.</param> |
| | | 292 | | internal void AddStreamHandler(ulong streamHandle, Action<FfiEvent> handler) => |
| | 165 | 293 | | _streamHandlers[streamHandle] = handler; |
| | | 294 | | |
| | | 295 | | /// <summary> |
| | | 296 | | /// Stops delivering the events of <paramref name="streamHandle"/>. |
| | | 297 | | /// </summary> |
| | | 298 | | /// <param name="streamHandle">Native handle of the stream.</param> |
| | | 299 | | internal void RemoveStreamHandler(ulong streamHandle) => |
| | 257 | 300 | | _streamHandlers.TryRemove(streamHandle, out _); |
| | | 301 | | |
| | | 302 | | private void DispatchToStream(FfiEvent ffiEvent) |
| | 42881 | 303 | | { |
| | | 304 | | ulong streamHandle; |
| | 42881 | 305 | | switch (ffiEvent.MessageCase) |
| | | 306 | | { |
| | | 307 | | case FfiEvent.MessageOneofCase.AudioStreamEvent: |
| | 985 | 308 | | streamHandle = ffiEvent.AudioStreamEvent.StreamHandle; |
| | 985 | 309 | | break; |
| | | 310 | | case FfiEvent.MessageOneofCase.VideoStreamEvent: |
| | 114 | 311 | | streamHandle = ffiEvent.VideoStreamEvent.StreamHandle; |
| | 114 | 312 | | break; |
| | | 313 | | case FfiEvent.MessageOneofCase.DataTrackStreamEvent: |
| | 9 | 314 | | streamHandle = ffiEvent.DataTrackStreamEvent.StreamHandle; |
| | 9 | 315 | | break; |
| | | 316 | | case FfiEvent.MessageOneofCase.TextStreamReaderEvent: |
| | 746 | 317 | | streamHandle = ffiEvent.TextStreamReaderEvent.ReaderHandle; |
| | 746 | 318 | | break; |
| | | 319 | | case FfiEvent.MessageOneofCase.ByteStreamReaderEvent: |
| | 668 | 320 | | streamHandle = ffiEvent.ByteStreamReaderEvent.ReaderHandle; |
| | 668 | 321 | | break; |
| | | 322 | | default: |
| | 40359 | 323 | | return; |
| | | 324 | | } |
| | | 325 | | |
| | 2522 | 326 | | if (!_streamHandlers.TryGetValue(streamHandle, out var handler)) |
| | 10 | 327 | | return; |
| | | 328 | | try |
| | 2512 | 329 | | { |
| | 2512 | 330 | | handler(ffiEvent); |
| | 2512 | 331 | | } |
| | 0 | 332 | | catch (Exception ex) |
| | 0 | 333 | | { |
| | 0 | 334 | | Console.Error.WriteLine($"[ERROR] FFI stream event handler threw: {ex}"); |
| | 0 | 335 | | } |
| | 42881 | 336 | | } |
| | | 337 | | |
| | | 338 | | // Caller holds _lock |
| | | 339 | | private void Enqueue(FfiEvent ffiEvent) |
| | 3760 | 340 | | { |
| | 3760 | 341 | | _eventQueue.Add(ffiEvent); |
| | 3760 | 342 | | if (_eventQueue.Count > 1000) |
| | 2123 | 343 | | _eventQueue.RemoveAt(0); // Circular buffer: remove oldest |
| | 3760 | 344 | | } |
| | | 345 | | |
| | | 346 | | private static bool IsClaimable(FfiEvent ffiEvent) |
| | 16646 | 347 | | { |
| | 16646 | 348 | | switch (ffiEvent.MessageCase) |
| | | 349 | | { |
| | | 350 | | case FfiEvent.MessageOneofCase.RoomEvent: |
| | | 351 | | case FfiEvent.MessageOneofCase.TrackEvent: |
| | | 352 | | case FfiEvent.MessageOneofCase.AudioStreamEvent: |
| | | 353 | | case FfiEvent.MessageOneofCase.VideoStreamEvent: |
| | | 354 | | case FfiEvent.MessageOneofCase.DataTrackStreamEvent: |
| | | 355 | | case FfiEvent.MessageOneofCase.TextStreamReaderEvent: |
| | | 356 | | case FfiEvent.MessageOneofCase.ByteStreamReaderEvent: |
| | | 357 | | case FfiEvent.MessageOneofCase.RpcMethodInvocation: |
| | 12886 | 358 | | return false; |
| | | 359 | | default: |
| | 3760 | 360 | | return true; |
| | | 361 | | } |
| | 16646 | 362 | | } |
| | | 363 | | |
| | | 364 | | // One throwing subscriber must not hide the event from the others (a second Room, the |
| | | 365 | | // audio and video streams...) |
| | | 366 | | private void RaiseEventReceived(FfiEvent ffiEvent) |
| | 42881 | 367 | | { |
| | 42881 | 368 | | var handlers = EventReceived; |
| | 42881 | 369 | | if (handlers == null) |
| | 5103 | 370 | | return; |
| | | 371 | | |
| | 252194 | 372 | | foreach (var handler in handlers.GetInvocationList()) |
| | 69430 | 373 | | { |
| | | 374 | | try |
| | 69430 | 375 | | { |
| | 69430 | 376 | | ((EventHandler<FfiEvent>)handler)(this, ffiEvent); |
| | 69430 | 377 | | } |
| | 0 | 378 | | catch (Exception ex) |
| | 0 | 379 | | { |
| | 0 | 380 | | Console.Error.WriteLine($"[ERROR] FFI event subscriber threw: {ex}"); |
| | 0 | 381 | | } |
| | 69430 | 382 | | } |
| | 42881 | 383 | | } |
| | | 384 | | |
| | | 385 | | private void LogRecord(LogRecord record) |
| | 0 | 386 | | { |
| | 0 | 387 | | var level = record.Level switch |
| | 0 | 388 | | { |
| | 0 | 389 | | LogLevel.LogError => "ERROR", |
| | 0 | 390 | | LogLevel.LogWarn => "WARN", |
| | 0 | 391 | | LogLevel.LogInfo => "INFO", |
| | 0 | 392 | | _ => "DEBUG", |
| | 0 | 393 | | }; |
| | 0 | 394 | | Console.WriteLine($"[{level}] {record.Target} - {record.Message}"); |
| | 0 | 395 | | } |
| | | 396 | | |
| | | 397 | | /// <summary> |
| | | 398 | | /// Copies <paramref name="len"/> bytes from native memory. Implemented in managed code: |
| | | 399 | | /// the native library has no copy entry point (calling the old binding threw |
| | | 400 | | /// <see cref="EntryPointNotFoundException"/>). |
| | | 401 | | /// </summary> |
| | | 402 | | /// <param name="ptr">Address of the native buffer.</param> |
| | | 403 | | /// <param name="len">Number of bytes to copy.</param> |
| | | 404 | | /// <returns>A managed copy of the bytes.</returns> |
| | | 405 | | public byte[] CopyBuffer(ulong ptr, int len) |
| | 0 | 406 | | { |
| | 0 | 407 | | byte[] buffer = new byte[len]; |
| | 0 | 408 | | if (len > 0) |
| | 0 | 409 | | { |
| | 0 | 410 | | Marshal.Copy((IntPtr)ptr, buffer, 0, len); |
| | 0 | 411 | | } |
| | 0 | 412 | | return buffer; |
| | 0 | 413 | | } |
| | | 414 | | |
| | | 415 | | /// <summary> |
| | | 416 | | /// Never worked: the native library has no such entry point, and a pointer to a managed |
| | | 417 | | /// array is only valid while the array is pinned. Pin the buffer around the request |
| | | 418 | | /// instead (see <c>AudioFrame.WithPinnedBuffer</c>). |
| | | 419 | | /// </summary> |
| | | 420 | | /// <param name="data">The managed array.</param> |
| | | 421 | | /// <returns>A pointer that is only valid while the array stays pinned.</returns> |
| | | 422 | | [Obsolete( |
| | | 423 | | "The native entry point does not exist and the returned pointer would not be pinned. Pin the buffer with 'fi |
| | | 424 | | )] |
| | | 425 | | public unsafe ulong RetrievePtr(byte[] data) |
| | 0 | 426 | | { |
| | 0 | 427 | | fixed (byte* ptr = data) |
| | 0 | 428 | | return NativeMethods.RetrievePtr(ptr, (UIntPtr)data.Length); |
| | 0 | 429 | | } |
| | | 430 | | |
| | | 431 | | // Whether Dispose released the native library: handle releases skip the requests that |
| | | 432 | | // precede them, since the library dropped every handle then |
| | 1273 | 433 | | internal bool IsDisposed => _disposed; |
| | | 434 | | |
| | | 435 | | private void EnsureInitialized() |
| | 39214 | 436 | | { |
| | 39214 | 437 | | if (!_initialized) |
| | 1 | 438 | | Initialize(); |
| | 39214 | 439 | | } |
| | | 440 | | |
| | | 441 | | /// <summary> |
| | | 442 | | /// Releases the native library. The SDK cannot be used in this process afterwards. |
| | | 443 | | /// </summary> |
| | | 444 | | public void Dispose() |
| | 0 | 445 | | { |
| | 0 | 446 | | if (_disposed) |
| | 0 | 447 | | return; |
| | 0 | 448 | | lock (_initLock) |
| | 0 | 449 | | { |
| | 0 | 450 | | if (_disposed) |
| | 0 | 451 | | return; |
| | 0 | 452 | | _disposed = true; |
| | | 453 | | |
| | 0 | 454 | | lock (_lock) |
| | 0 | 455 | | { |
| | | 456 | | // Cancel all pending async waiters |
| | 0 | 457 | | foreach (var waiter in _waiters) |
| | 0 | 458 | | waiter.Tcs.TrySetCanceled(); |
| | 0 | 459 | | _waiters.Clear(); |
| | 0 | 460 | | _eventQueue.Clear(); |
| | 0 | 461 | | } |
| | | 462 | | |
| | 0 | 463 | | if (_initialized) |
| | 0 | 464 | | NativeMethods.Dispose(); |
| | 0 | 465 | | } |
| | 0 | 466 | | } |
| | | 467 | | } |
| | | 468 | | } |