disconnectSendTransportAudio function

Future<void> disconnectSendTransportAudio(
  1. DisconnectSendTransportAudioOptions options
)

Disconnects the send transport for audio by pausing the audio producer(s) and updating the UI accordingly.

This function supports both a primary and a local audio producer, delegating local handling to a separate function.

Parameters:

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

Workflow:

  1. Pause Primary Audio Producer:
    • If an active primary audio producer exists, it is paused, and the primary state is updated.
  2. Update UI:
    • Based on conditions (such as video status, user level, and screen lock status), the main window UI is updated.
    • If no video is active and certain conditions are met, user media may be prepopulated.
  3. Notify Server:
    • Emits a pauseProducerMedia event to the server to notify about the paused primary audio producer.
  4. Handle Local Audio Transport:
    • Invokes disconnectLocalSendTransportAudio to handle the local audio transport disconnection.

Returns:

  • A Future<void> that completes when the audio send transport(s) are 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(
    audioProducer: myAudioProducer,
    localAudioProducer: myLocalAudioProducer,
    socket: mySocket,
    localSocket: myLocalSocket,
    videoAlreadyOn: false,
    islevel: '1',
    lockScreen: false,
    shared: false,
    updateMainWindow: true,
    hostLabel: 'Host',
    roomName: 'Room 1',
    updateAudioProducer: (producer) => print('Updated audio producer: $producer'),
    updateLocalAudioProducer: (producer) => print('Updated local audio producer: $producer'),
    updateUpdateMainWindow: (state) => print('Main window state updated: $state'),
    prepopulateUserMedia: myPrepopulateUserMediaFunction,
  ),
);

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

Notes:

  • This function integrates with prepopulateUserMedia to manage the state of the main window if video is inactive and specific conditions are met.
  • It assumes that the server listens to pauseProducerMedia events and takes appropriate action upon receiving it.

Implementation

Future<void> disconnectSendTransportAudio(
    DisconnectSendTransportAudioOptions options) async {
  try {
    // Destructure parameters using getters
    final parameters = options.parameters;
    final Producer? audioProducer = parameters.audioProducer;
    final io.Socket? socket = parameters.socket;
    final bool videoAlreadyOn = parameters.videoAlreadyOn;
    final String islevel = parameters.islevel;
    final bool lockScreen = parameters.lockScreen;
    final bool shared = parameters.shared;
    bool updateMainWindow = parameters.updateMainWindow;
    final String hostLabel = parameters.hostLabel;
    final String roomName = parameters.roomName;

    // Callback functions
    final void Function(Producer? audioProducer) updateAudioProducer =
        parameters.updateAudioProducer;
    final void Function(bool updateMainWindow) updateUpdateMainWindow =
        parameters.updateUpdateMainWindow;

    // mediasfu function
    final PrepopulateUserMediaType prepopulateUserMedia =
        parameters.prepopulateUserMedia;

    // Pause the audio producer
    if (audioProducer != null) {
      audioProducer.pause();
      updateAudioProducer(audioProducer);
    }

    // Update the UI based on conditions
    if (!videoAlreadyOn && islevel == '2') {
      if (!lockScreen && !shared) {
        updateMainWindow = true;
        updateUpdateMainWindow(updateMainWindow);

        // Prepopulate user media
        final optionsPrepopulate = PrepopulateUserMediaOptions(
          name: hostLabel,
          parameters: parameters,
        );
        await prepopulateUserMedia(
          optionsPrepopulate,
        );

        updateMainWindow = false;
        updateUpdateMainWindow(updateMainWindow);
      }
    }

    // Notify the server about pausing the audio producer
    socket!.emit('pauseProducerMedia', {
      'mediaTag': 'audio',
      'roomName': roomName,
    });

    // Handle local audio transport disconnection
    try {
      await disconnectLocalSendTransportAudio(options);
    } catch (localError) {
      if (kDebugMode) {
        print('Error disconnecting local audio send transport: $localError');
      }
      // Optionally, handle the local error (e.g., show a notification)
    }
  } catch (error) {
    if (kDebugMode) {
      print('MediaSFU - disconnectSendTransportAudio error: $error');
    }
    // Handle errors as needed (e.g., show alert, retry logic)
  }
}