disconnectSendTransportVideo function

Future<void> disconnectSendTransportVideo(
  1. DisconnectSendTransportVideoOptions options
)

Disconnects the send transport for video by closing the video producer(s), updating the UI, and notifying the server.

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

Parameters:

  • options (DisconnectSendTransportVideoOptions): Contains the parameters required for disconnecting the video send transport.

Workflow:

  1. Close Primary Video Producer:
    • If an active primary video producer exists, it is closed.
    • The primary state is updated to reflect the closed producer.
  2. Notify Server:
    • Emits pauseProducerMedia event with mediaTag as video to notify about the paused primary video producer.
  3. Update UI:
    • Based on islevel and lockScreen status, updates the main window state.
  4. Reorder Streams:
    • Calls reorderStreams to adjust the stream layout based on the current state.
  5. Handle Local Video Transport Disconnection:
    • Invokes disconnectLocalSendTransportVideo to handle the local video transport disconnection.

Returns:

  • A Future<void> that completes when the video send transport(s) are successfully disconnected.

Error Handling:

  • Catches and logs any errors encountered during the disconnection process.
  • Errors are printed to the console in debug mode to aid troubleshooting.

Example Usage:

final options = DisconnectSendTransportVideoOptions(
  parameters: MyDisconnectSendTransportVideoParameters(
    videoProducer: myVideoProducer,
    localVideoProducer: myLocalVideoProducer,
    socket: mySocket,
    localSocket: myLocalSocket,
    islevel: '2',
    roomName: 'myRoom',
    lockScreen: false,
    updateMainWindow: true,
    updateVideoProducer: (producer) => print('Video producer updated: $producer'),
    updateLocalVideoProducer: (producer) => print('Local video producer updated: $producer'),
    updateUpdateMainWindow: (state) => print('Main window updated: $state'),
    reorderStreams: reorderStreamsFunction,
    getUpdatedAllParams: () => myUpdatedParameters,
  ),
);

disconnectSendTransportVideo(options).then((_) {
  print('Video send transport disconnected successfully');
}).catchError((error) {
  print('Error disconnecting video send transport: $error');
});

Notes:

  • Assumes server processing of pauseProducerMedia and reorderStreams events to update video sharing status.
  • Relies on updateVideoProducer and updateUpdateMainWindow callbacks to manage the state after video disconnection.

Additional Considerations:

  • Ensures ReorderStreamsOptions are accurately set to reorder the stream layout on the UI based on lockScreen status.

Implementation

Future<void> disconnectSendTransportVideo(
    DisconnectSendTransportVideoOptions options) async {
  try {
    // Retrieve updated parameters
    final parameters = options.parameters.getUpdatedAllParams();

    final Producer? videoProducer = parameters.videoProducer;
    final io.Socket? socket = parameters.socket;
    final String islevel = parameters.islevel;
    final String roomName = parameters.roomName;
    bool updateMainWindow = parameters.updateMainWindow;
    final bool lockScreen = parameters.lockScreen;

    // Callbacks
    final void Function(Producer? videoProducer) updateVideoProducer =
        parameters.updateVideoProducer;
    final void Function(bool updateMainWindow) updateUpdateMainWindow =
        parameters.updateUpdateMainWindow;
    final ReorderStreamsType reorderStreams = parameters.reorderStreams;

    // Close the video producer and update the state
    if (videoProducer != null) {
      videoProducer.close();
      updateVideoProducer(null);
    }

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

    // Update the UI based on level and lock status
    if (islevel == '2') {
      updateMainWindow = true;
      updateUpdateMainWindow(updateMainWindow);
    }

    final optionsReorder = ReorderStreamsOptions(
      add: lockScreen,
      screenChanged: true,
      parameters: parameters as ReorderStreamsParameters,
    );
    await reorderStreams(
      optionsReorder,
    );

    // Handle local video transport disconnection
    try {
      await disconnectLocalSendTransportVideo(options);
    } catch (localError) {
      if (kDebugMode) {
        print('Error disconnecting local video send transport: $localError');
      }
      // Optionally, handle the local error (e.g., show a notification)
    }
  } catch (error) {
    if (kDebugMode) {
      print('Error disconnecting send transport for video: $error');
    }
  }
}