startCapture method

  1. @override
Future<void> startCapture(
  1. void onData(
    1. MiniAVBuffer buffer,
    2. Object? userData
    ), {
  2. Object? userData,
})
override

Start loopback capture. onData is called for each audio buffer received.

Implementation

@override
Future<void> startCapture(
  void Function(MiniAVBuffer buffer, Object? userData) onData, {
  Object? userData,
}) async {
  _ensureNotDestroyed();

  await stopCapture(); // Clean up any previous callback

  void ffiCallback(
    ffi.Pointer<bindings.MiniAVBuffer> buffer,
    ffi.Pointer<ffi.Void> cbUserData,
  ) {
    // Check if context was destroyed during callback
    if (_isDestroyed) {
      return; // Silently ignore if destroyed
    }

    final platformBuffer = MiniAVBufferFFI.fromPointer(buffer);
    try {
      onData(platformBuffer, userData);
    } catch (e, s) {
      print('Error in loopback user callback: $e\n$s');
    }
  }

  _callbackHandle =
      ffi.NativeCallable<bindings.MiniAVBufferCallbackFunction>.listener(
        ffiCallback,
      );

  final res = bindings.MiniAV_Loopback_StartCapture(
    _contextHandle!,
    _callbackHandle!.nativeFunction,
    ffi.nullptr,
  );

  if (res != bindings.MiniAVResultCode.MINIAV_SUCCESS) {
    await _cleanupCallback();
    throw Exception('Failed to start loopback capture: ${res.name}');
  }
}