disconnectLocalSendTransportAudio function

Future<void> disconnectLocalSendTransportAudio(
  1. DisconnectSendTransportAudioOptions options
)

Disconnects the local send transport for audio by pausing the local audio producer and notifying the server.

Parameters:

  • options (DisconnectSendTransportAudioOptions): Contains the parameters required for disconnecting the local audio transport.

Workflow:

  1. Pause Local Audio Producer:
    • If an active local audio producer exists, it is paused, and the local state is updated.
  2. Notify Server:
    • Emits a pauseProducerMedia event to the server to notify about the paused local audio producer.

Returns:

  • A Future<void> that completes when the local audio transport is successfully disconnected.

Error Handling:

  • Logs errors to the console in debug mode and rethrows them for higher-level handling.

Example Usage:

final options = DisconnectSendTransportAudioOptions(
  parameters: myDisconnectSendTransportAudioParameters,
);

disconnectLocalSendTransportAudio(options)
  .then(() => print('Local audio send transport disconnected successfully'))
  .catchError((error) => print('Error disconnecting local audio send transport: $error'));

Implementation

Future<void> disconnectLocalSendTransportAudio(
    DisconnectSendTransportAudioOptions options) async {
  try {
    final parameters = options.parameters;

    final Producer? localAudioProducer = parameters.localAudioProducer;
    final io.Socket? localSocket = parameters.localSocket;
    final String roomName = parameters.roomName;
    final void Function(Producer? localAudioProducer)?
        updateLocalAudioProducer = parameters.updateLocalAudioProducer;

    if (localSocket == null || localSocket.id == null) {
      // Local socket is not connected; nothing to disconnect
      return;
    }

    // Pause the local audio producer
    if (localAudioProducer != null) {
      localAudioProducer
          .pause(); // MediaSFU prefers pause instead of close for recording
      updateLocalAudioProducer?.call(null); // Set to null after pausing
    }

    // Notify the server about pausing the local audio producer
    localSocket.emit('pauseProducerMedia', {
      'mediaTag': 'audio',
      'roomName': roomName,
    });
  } catch (error) {
    if (kDebugMode) {
      print('Error disconnecting local audio send transport: $error');
    }
    rethrow; // Re-throw to propagate the error
  }
}