disconnectLocalSendTransportScreen function
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:
- Close Local Screen Producer:
- If an active local screen producer exists, it is closed.
- The local state is updated to reflect the closed producer.
- Notify Server:
- Emits
closeScreenProducer
andpauseProducerMedia
events to the server to notify about the paused local screen producer.
- Emits
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
}
}