disconnectLocalSendTransportScreen function

Future<void> disconnectLocalSendTransportScreen(
  1. DisconnectSendTransportScreenOptions options
)

Disconnects the local send transport for screen sharing by closing the local screen producer and notifying the server.

Parameters:

  • options (DisconnectSendTransportScreenOptions): Contains the parameters required for disconnecting the local screen transport.

Workflow:

  1. Close Local Screen Producer:
    • If an active local screen producer exists, it is closed.
    • The local state is updated to reflect the closed producer.
  2. Notify Server:
    • Emits closeScreenProducer and pauseProducerMedia events to the server to notify about the paused local screen producer.

Returns:

  • A Future<void> that completes when the local screen 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 = DisconnectSendTransportScreenOptions(
  parameters: myDisconnectSendTransportScreenParameters,
);

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

Implementation

Future<void> disconnectLocalSendTransportScreen(
    DisconnectSendTransportScreenOptions options) async {
  try {
    final parameters = options.parameters;

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

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

    // Close the local screen producer and update the state
    if (localScreenProducer != null) {
      localScreenProducer.close();
      updateLocalScreenProducer?.call(null); // Set to null after closing
    }

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