start method
Starts the session by fetching credentials and connecting to the room.
Implementation
Future<void> start() async {
if (room.connectionState != ConnectionState.disconnected) {
logger.info('Session.start() ignored: room already connecting or connected.');
return;
}
_setError(null);
_agentTimeoutTimer?.cancel();
final Duration timeout = _options.agentConnectTimeout;
Future<bool> connect() async {
final response = await _tokenSourceConfiguration.fetch();
await room.connect(
response.serverUrl,
response.participantToken,
);
return response.dispatchesAgent();
}
try {
final bool dispatchesAgent;
if (_options.preConnectAudio) {
dispatchesAgent = await room.withPreConnectAudio(
() async {
_setConnectionState(ConnectionState.connecting);
_agent.connecting(buffering: true);
return connect();
},
timeout: timeout,
);
} else {
_setConnectionState(ConnectionState.connecting);
_agent.connecting(buffering: false);
dispatchesAgent = await connect();
await room.localParticipant?.setMicrophoneEnabled(true);
}
if (dispatchesAgent) {
_agentTimeoutTimer = Timer(timeout, () {
if (isConnected && !_agent.isConnected) {
_agent.failed(AgentFailure.timeout);
}
});
} else {
_agentTimeoutTimer?.cancel();
_agentTimeoutTimer = null;
}
} catch (error, stackTrace) {
logger.warning('Session.start() failed: $error', error, stackTrace);
_setError(SessionError.connection(error));
_setConnectionState(ConnectionState.disconnected);
_agent.disconnected();
}
}