Files
pyramid/lib/features/voice_channel/livekit_call_manager.dart
T
Bernd SteckmeisterandClaude Opus 5.5 441cc6ef54 fix(calls): Bildschirmübertragung ohne Absturz, Lautsprecher, Mikrofon im Hintergrund, zurück in den Anruf
Bernds Anruftest Handy↔PC (2026-10-07):
- Teilen am Handy: App "verkleinerte sich", Anruf danach weg. Ursache:
  Android 14+ verlangt vor MediaProjection einen Vordergrunddienst vom Typ
  mediaProjection; flutter_webrtc startet keinen, der Manifest-Eintrag
  verwies auf eine nicht mehr existierende Klasse. Neu: CallService
  (microphone|mediaProjection) + CallForeground: Zustimmung → Dienst
  (wartet auf echten startForeground) → Mitschnitt. Emulator-Test (Android
  16): mit Fix 1080x2400@30 läuft; Gegenprobe ohne Dienst wirft genau
  'Media projections require a foreground service ...'.
- Dienst läuft während jedes Anrufs: Mikrofon bleibt im Hintergrund aktiv,
  Benachrichtigung "Anruf läuft" führt zurück. Mikrofon-Typ nur mit
  Berechtigung, sonst schmalere Variante statt Dienst-Abbruch.
- Handy hörte nichts: SDK schaltet Sprachanrufe auf die Hörmuschel. Jetzt
  Lautsprecher als Standard (wie Discord) + Umschalter in der Anrufleiste.
- LiveKit-Teilen am Handy öffnete den PC-Quellen-Auswähler → System-Dialog.
- Im offenen Chat (Handy) Leiste "Anruf läuft · Zurück".
- Einstellungen → Sprache & Video: Mikrofontest mit Pegel (PC-Diagnose:
  das Standardgerät lieferte nur Stille – Voicemeeter/virtuelle Geräte).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-08 00:01:29 +02:00

742 lines
25 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;
import 'package:livekit_client/livekit_client.dart';
import 'package:matrix/matrix.dart' hide Room;
import 'package:pyramid/core/call_foreground.dart';
import 'package:pyramid/core/ice_servers.dart';
import 'package:pyramid/core/livekit_token.dart';
import 'package:pyramid/core/settings_prefs.dart';
import 'package:pyramid/features/voice_channel/voice_channel_service.dart';
const _kVoicePresenceType = 'io.pyramid.voice.presence';
/// Implementierung von [VoiceChannelService] (Modul `voice_channel`).
/// Andere Module gehen ausschließlich über `voiceChannelProvider`.
class LiveKitCallManager extends VoiceChannelService {
static final LiveKitCallManager instance = LiveKitCallManager._();
LiveKitCallManager._();
// ── Publish quality presets (Streamer → Server) ──────────────────────────
static final Map<String, VideoParameters> qualityPresets = {
'sd': VideoParametersPresets.h360_169,
'hd': VideoParametersPresets.h720_169,
'hd60': VideoParametersPresets.h720_169,
'fhd': VideoParametersPresets.h1080_169,
'fhd60': VideoParametersPresets.h1080_169,
'4k': VideoParametersPresets.h2160_169,
};
/// FPS + Bitrate pro Screenshare-Preset. Die 60-FPS-Presets (M4,
/// `docs/STREAMING_60FPS.md`) brauchen deutlich mehr Bitrate, sonst
/// verwirft der Encoder Frames und es ruckelt trotz hoher Framerate.
@visibleForTesting
static const Map<String, VideoEncoding> screenShareEncodings = {
'sd': VideoEncoding(maxBitrate: 500_000, maxFramerate: 10),
'hd': VideoEncoding(maxBitrate: 1_500_000, maxFramerate: 15),
'hd60': VideoEncoding(maxBitrate: 5_000_000, maxFramerate: 60),
'fhd': VideoEncoding(maxBitrate: 2_500_000, maxFramerate: 20),
'fhd60': VideoEncoding(maxBitrate: 8_000_000, maxFramerate: 60),
'4k': VideoEncoding(maxBitrate: 4_000_000, maxFramerate: 30),
};
/// 60-FPS-Presets bekommen beim Publish eine andere Strategie
/// (kein Simulcast, Framerate vor Auflösung halten).
@visibleForTesting
static bool isHighFpsPreset(String key) => key.endsWith('60');
Room? _room;
EventsListener<RoomEvent>? _roomEventListener;
String _displayName = '';
@override
bool isConnecting = false;
@override
String? error;
@override
bool isMuted = false;
@override
bool isCameraOff = false;
@override
bool isScreenSharing = false;
String _localQualityKey = 'hd';
String _subscribeQualityKey = 'auto'; // 'auto' | 'low' | 'medium' | 'high'
String? _currentScreenShareSourceId;
LocalVideoTrack? _screenShareTrack;
@override
bool isDeafened = false;
bool _mutedBeforeDeafen = false;
@override
bool isVoiceChannel = false;
String? _matrixRoomId;
Client? _matrixClient;
Timer? _presenceHeartbeat;
Timer? _statsTimer;
/// Entwertet laufende `startCall`-Verbindungsaufbauten: Wer während des
/// (mehrere Sekunden dauernden) `connect` auflegt oder den Kanal wechselt,
/// darf KEINE Geisterverbindung zurücklassen – vorher blieb in dem Fall
/// das Mikro im alten Kanal offen, während die UI „nicht verbunden" zeigte.
int _callGeneration = 0;
@override
bool get isActive => _room != null || isConnecting;
@override
Room? get room => _room;
@override
String? get currentRoomId => _matrixRoomId;
@override
String get displayName => _displayName;
bool get noiseSuppression => false;
@override
String get currentQualityKey => _localQualityKey;
@override
String get subscribeQualityKey => _subscribeQualityKey;
VideoParameters get _currentQuality =>
qualityPresets[_localQualityKey] ?? VideoParametersPresets.h720_169;
void _onRoomChanged() => notifyListeners();
// Handy: Lautsprecher statt Hörmuschel (wie Discord).
bool _speakerOn = true;
bool get _isMobile =>
!kIsWeb &&
(defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS);
@override
bool get isSpeakerOn => _speakerOn;
@override
Future<void> toggleSpeaker() async {
_speakerOn = !_speakerOn;
notifyListeners();
await _applySpeaker();
}
Future<void> _applySpeaker() async {
if (!_isMobile) return;
try {
await rtc.Helper.setSpeakerphoneOn(_speakerOn);
} catch (e) {
debugPrint('[LiveKit] Lautsprecher: $e');
}
}
void _cleanUpAfterDisconnect() {
_callGeneration++;
CallForeground.callEnded('livekit');
_stopStatsLogging();
_presenceHeartbeat?.cancel();
_presenceHeartbeat = null;
if (_matrixClient != null && _matrixRoomId != null) {
_writePresence(false).catchError((_) {});
}
_roomEventListener?.dispose();
_roomEventListener = null;
_room?.removeListener(_onRoomChanged);
_room = null;
_displayName = '';
isConnecting = false;
// `error` bewusst NICHT löschen: Ein unerwarteter Abbruch/Join-Fehler
// muss für die UI sichtbar bleiben (kein stiller Fehlerpfad). Manuelles
// Auflegen (hangUp) setzt error selbst zurück.
isMuted = false;
isCameraOff = false;
isScreenSharing = false;
isDeafened = false;
isVoiceChannel = false;
_mutedBeforeDeafen = false;
_screenShareTrack = null;
_currentScreenShareSourceId = null;
_matrixRoomId = null;
_matrixClient = null;
notifyListeners();
}
Future<void> _writePresence(bool active) async {
final client = _matrixClient;
final roomId = _matrixRoomId;
final userId = client?.userID;
if (client == null || roomId == null || userId == null) return;
try {
Map<String, dynamic> content;
if (active) {
// Resolve display name + avatar from any joined room's member state
User? member;
for (final r in client.rooms) {
final participants = r.getParticipants();
try {
member = participants.firstWhere((u) => u.id == userId);
if (member.displayName != null || member.avatarUrl != null) break;
} catch (_) {}
}
content = {
'active': true,
'joined_at': DateTime.now().millisecondsSinceEpoch,
'display_name': member?.displayName ?? userId,
if (member?.avatarUrl != null) 'avatar_url': member!.avatarUrl.toString(),
};
} else {
content = {'active': false};
}
await client.setRoomStateWithKey(roomId, _kVoicePresenceType, userId, content);
} catch (e) {
debugPrint('[VoicePresence] $e');
}
}
@override
Future<void> startCall({
required String roomName,
required String roomDisplayName,
required String identity,
bool audioOnly = false,
bool voiceChannel = false,
Client? matrixClient,
String? matrixRoomId,
}) async {
if (isActive) return;
final gen = ++_callGeneration;
isConnecting = true;
isVoiceChannel = voiceChannel;
error = null;
_displayName = roomDisplayName;
isCameraOff = audioOnly;
isMuted = false;
isScreenSharing = false;
isDeafened = false;
_mutedBeforeDeafen = false;
_localQualityKey = 'hd';
_subscribeQualityKey = 'auto';
_matrixClient = matrixClient;
_matrixRoomId = matrixRoomId;
notifyListeners();
try {
// Token holt der Server (prüft den Matrix-Login, Identität = eigene
// Matrix-ID); das LiveKit-Secret liegt nicht mehr in der App.
// [identity] wird dafür nicht mehr gebraucht.
final accessToken = matrixClient?.accessToken;
if (accessToken == null || accessToken.isEmpty) {
throw const LiveKitTokenException(
'Nicht angemeldet – Beitritt nicht möglich.');
}
final lk = await LiveKitTokenService.fetch(
room: roomName,
matrixToken: accessToken,
);
if (gen != _callGeneration) return; // inzwischen aufgelegt/gewechselt
final token = lk.token;
final url = lk.url;
final room = Room(
roomOptions: const RoomOptions(
adaptiveStream: false, // Matches FluffyChat fix
dynacast: true,
),
);
room.addListener(_onRoomChanged);
// Eigene ICE-Server (STUN/TURN aus ice.json) mitgeben. Client-seitige
// iceServers ERSETZEN die vom LiveKit-Server gelieferten — die zeigten
// bisher auf einen hinter CGNAT unerreichbaren coturn.
RTCConfiguration rtcConfig = const RTCConfiguration();
try {
final ice = await IceServers.get();
rtcConfig = RTCConfiguration(
iceServers: ice
.map((m) => RTCIceServer(
urls: m['urls'] is List
? List<String>.from(m['urls'] as List)
: [m['urls'] as String],
username: m['username'] as String?,
credential: m['credential'] as String?,
))
.toList(),
);
} catch (e) {
debugPrint('[LiveKit] ICE server fetch error: $e');
}
await room.connect(
url,
token,
connectOptions: ConnectOptions(rtcConfiguration: rtcConfig),
);
if (gen != _callGeneration) {
// Während des Verbindens wurde aufgelegt oder der Kanal gewechselt –
// diese Verbindung gehört niemandem mehr: sofort wieder trennen,
// sonst bleibt das Mikro unsichtbar im alten Kanal offen.
room.removeListener(_onRoomChanged);
try {
await room.disconnect();
} catch (_) {}
return;
}
_room = room;
isConnecting = false;
notifyListeners();
_speakerOn = true;
unawaited(_applySpeaker());
// Android: Mikrofon im Hintergrund + „Anruf läuft“-Benachrichtigung.
unawaited(CallForeground.callStarted('livekit', title: roomDisplayName));
if (voiceChannel && matrixClient != null && matrixRoomId != null) {
await _writePresence(true);
_presenceHeartbeat = Timer.periodic(const Duration(seconds: 30), (_) {
_writePresence(true).catchError((_) {});
});
}
_roomEventListener = room.createListener()
..on<LocalTrackPublishedEvent>((_) => notifyListeners())
..on<LocalTrackUnpublishedEvent>((event) {
if (event.publication.source == TrackSource.screenShareVideo) {
isScreenSharing = false;
_currentScreenShareSourceId = null;
_screenShareTrack = null;
CallForeground.screenCaptureStopped('livekit');
}
notifyListeners();
})
..on<TrackSubscribedEvent>((event) {
final pub = event.publication;
if (_subscribeQualityKey != 'auto' && pub.kind == TrackType.VIDEO) {
pub.setVideoQuality(_videoQualityFromKey(_subscribeQualityKey));
}
// Ausgabelautstärke aus den Einstellungen auf neue Audio-Tracks.
if (pub.kind == TrackType.AUDIO) {
try {
rtc.Helper.setVolume(
_outputVolume.clamp(0.0, 2.0), event.track.mediaStreamTrack);
} catch (_) {}
}
notifyListeners();
})
..on<TrackUnsubscribedEvent>((_) => notifyListeners())
..on<TrackPublishedEvent>((_) => notifyListeners())
..on<TrackUnpublishedEvent>((_) => notifyListeners())
..on<ParticipantConnectedEvent>((_) => notifyListeners())
..on<ParticipantDisconnectedEvent>((_) => notifyListeners())
..on<RoomDisconnectedEvent>((event) {
// Kommt nur bei UNERWARTETEN Abbrüchen an (manuelles hangUp
// entsorgt den Listener vorher) – Grund sichtbar machen statt
// still aus dem Kanal zu verschwinden.
error = 'Verbindung getrennt (${event.reason?.name ?? 'unbekannt'})';
_cleanUpAfterDisconnect();
});
// In den Einstellungen gewählte Geräte anwenden (voice_*_device Prefs).
// Stale-IDs (abgestecktes Headset etc.) werden gegen die aktuell
// vorhandenen Geräte validiert und sonst ignoriert.
String? prefMic, prefSpeaker, prefCam;
var deviceIds = <String>{};
try {
_outputVolume = await VoicePrefs.loadOutputVolume();
final devicePrefs = await VoicePrefs.loadDevices();
prefMic = devicePrefs.mic;
prefSpeaker = devicePrefs.speaker;
prefCam = devicePrefs.camera;
final devices = await rtc.navigator.mediaDevices.enumerateDevices();
deviceIds = devices.map((d) => d.deviceId).toSet();
} catch (e) {
debugPrint('[LiveKit] Device prefs/enumerate error: $e');
}
if (prefMic != null && !deviceIds.contains(prefMic)) prefMic = null;
if (prefSpeaker != null && !deviceIds.contains(prefSpeaker)) prefSpeaker = null;
if (prefCam != null && !deviceIds.contains(prefCam)) prefCam = null;
await room.localParticipant?.setMicrophoneEnabled(
true,
audioCaptureOptions:
prefMic != null ? AudioCaptureOptions(deviceId: prefMic) : null,
);
if (prefSpeaker != null) {
try {
await rtc.Helper.selectAudioOutput(prefSpeaker);
} catch (e) {
debugPrint('[LiveKit] selectAudioOutput error: $e');
}
}
if (!audioOnly) {
try {
await room.localParticipant?.setCameraEnabled(
true,
cameraCaptureOptions: CameraCaptureOptions(
deviceId: prefCam,
params: _currentQuality,
),
);
} catch (e) {
debugPrint('[LiveKit] Failed to enable camera: $e');
}
}
_startStatsLogging();
} catch (e) {
debugPrint('[VOIP] Start call error: $e');
if (gen != _callGeneration) {
// Inzwischen wurde aufgelegt/neu verbunden – der Zustand gehört
// schon dem neuen Aufbau, hier nichts mehr anfassen.
return;
}
isConnecting = false;
_cleanUpAfterDisconnect();
// NACH dem Aufräumen setzen, damit der Join-Fehler sichtbar bleibt.
error = 'Beitritt fehlgeschlagen: $e';
notifyListeners();
}
}
VideoQuality _videoQualityFromKey(String key) {
switch (key) {
case 'low': return VideoQuality.LOW;
case 'medium': return VideoQuality.MEDIUM;
case 'high': return VideoQuality.HIGH;
default: return VideoQuality.HIGH; // auto defaults to high
}
}
// ── Ausgabelautstärke (Settings-Slider "voice_output_volume") ─────────────
double _outputVolume = 1.0;
/// Live-Anwendung des Lautstärke-Sliders auf alle Remote-Audio-Tracks.
@override
Future<void> setOutputVolume(double v) async {
_outputVolume = v;
final room = _room;
if (room == null) return;
for (final p in room.remoteParticipants.values) {
for (final pub in p.audioTrackPublications) {
final t = pub.track;
if (t != null) {
try {
await rtc.Helper.setVolume(v.clamp(0.0, 2.0), t.mediaStreamTrack);
} catch (_) {}
}
}
}
}
@override
Future<void> setSubscribeQuality(String key) async {
debugPrint('[LiveKit] Viewer changing subscribe quality to: $key');
_subscribeQualityKey = key;
if (_room == null) { notifyListeners(); return; }
final quality = _videoQualityFromKey(key);
for (final p in _room!.remoteParticipants.values) {
for (final pub in p.videoTrackPublications) {
if (pub.kind == TrackType.VIDEO) {
debugPrint('[LiveKit] Setting subscription quality for ${p.identity} to $quality');
pub.setVideoQuality(quality);
}
}
}
notifyListeners();
}
@override
Future<void> toggleMute() async {
isMuted = !isMuted;
notifyListeners();
await _room?.localParticipant?.setMicrophoneEnabled(!isMuted);
}
@override
Future<void> toggleDeafen() async {
if (!isDeafened) {
_mutedBeforeDeafen = isMuted;
isDeafened = true;
if (!isMuted) {
await _room?.localParticipant?.setMicrophoneEnabled(false);
isMuted = true;
}
} else {
isDeafened = false;
if (!_mutedBeforeDeafen) {
await _room?.localParticipant?.setMicrophoneEnabled(true);
isMuted = false;
}
}
_applyDeafen();
notifyListeners();
}
void _applyDeafen() {
if (_room == null) return;
for (final p in _room!.remoteParticipants.values) {
for (final trackPub in p.audioTrackPublications) {
final track = trackPub.track;
if (track != null) {
track.mediaStreamTrack.enabled = !isDeafened;
}
}
}
}
@override
Future<void> toggleCamera() async {
if (isCameraOff) {
try {
await _room?.localParticipant?.setCameraEnabled(
true,
cameraCaptureOptions: CameraCaptureOptions(params: _currentQuality),
);
isCameraOff = false;
} catch (e) {
debugPrint('[LiveKit] toggleCamera ON error: $e');
}
} else {
try {
await _room?.localParticipant?.setCameraEnabled(false);
} catch (e) {
debugPrint('[LiveKit] toggleCamera OFF error: $e');
}
isCameraOff = true;
}
notifyListeners();
}
@override
Future<void> toggleScreenShare() async {
if (isScreenSharing) {
await stopScreenShare();
} else {
try {
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.windows) {
final sources = await rtc.desktopCapturer.getSources(types: [rtc.SourceType.Screen, rtc.SourceType.Window]);
if (sources.isNotEmpty) {
await startScreenShare(sources.first.id);
return;
}
}
// Android 14+: erst Zustimmung + mediaProjection-Dienst, sonst
// stürzt die App beim Mitschnitt ab.
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
if (!await CallForeground.prepareScreenCapture('livekit')) return;
}
await _room?.localParticipant?.setScreenShareEnabled(true);
isScreenSharing = true;
notifyListeners();
} catch (e) {
debugPrint('[ScreenShare] Toggle error: $e');
}
}
}
VideoEncoding _screenShareEncoding() {
return screenShareEncodings[_localQualityKey] ??
const VideoEncoding(maxBitrate: 1_500_000, maxFramerate: 15);
}
@override
Future<void> startScreenShare(String sourceId) async {
if (isScreenSharing) return;
try {
final encoding = _screenShareEncoding();
final highFps = isHighFpsPreset(_localQualityKey);
final track = await LocalVideoTrack.createScreenShareTrack(
ScreenShareCaptureOptions(
sourceId: sourceId,
maxFrameRate: encoding.maxFramerate.toDouble(),
// Ohne explizite params bleibt die Capture-Auflösung beim
// LiveKit-Default (1080p/15) hängen – das Preset (Auflösung UND
// FPS) muss bis in die Capture-Constraints durchgereicht werden.
params: VideoParameters(
dimensions: _currentQuality.dimensions,
encoding: encoding,
),
),
);
if (_room == null || _room!.localParticipant == null) return;
await _room!.localParticipant!.publishVideoTrack(
track,
publishOptions: VideoPublishOptions(
// 60-FPS-Presets: EIN Layer mit voller Bitrate statt drei
// Simulcast-Encodes (Software-Encoder!), und unter Last lieber
// Auflösung senken als Frames verlieren. contentHint 'motion'
// wäre ein weiterer Hebel, ist aber in flutter_webrtc 1.4.1 aus
// Dart nicht setzbar (Pub-Cache geprüft, 2026-07-06).
simulcast: !highFps,
degradationPreference: highFps
? DegradationPreference.maintainFramerate
: DegradationPreference.balanced,
screenShareEncoding: encoding,
),
);
isScreenSharing = true;
_currentScreenShareSourceId = sourceId;
_screenShareTrack = track;
notifyListeners();
unawaited(_applyFpsToAllSimulcastLayers(track));
} catch (e) {
debugPrint('[ScreenShare] ERROR: $e');
}
}
Future<void> _applyFpsToAllSimulcastLayers(LocalVideoTrack track) async {
final targetFps = _screenShareEncoding().maxFramerate;
for (var attempt = 1; attempt <= 5; attempt++) {
await Future.delayed(const Duration(milliseconds: 600));
if (!isScreenSharing || _screenShareTrack != track) return;
final sender = track.transceiver?.sender;
if (sender == null) continue;
final params = sender.parameters;
final encodings = params.encodings;
if (encodings == null || encodings.isEmpty) continue;
var anyUpdated = false;
for (final enc in encodings) {
if ((enc.maxFramerate ?? 0) < targetFps) {
enc.maxFramerate = targetFps;
anyUpdated = true;
}
}
if (anyUpdated) await sender.setParameters(params);
return;
}
}
@override
Future<void> stopScreenShare() async {
final local = _room?.localParticipant;
if (local != null) {
final toStop = <String>[];
for (final pub in local.videoTrackPublications) {
if (pub.source == TrackSource.screenShareVideo) {
toStop.add(pub.sid);
await pub.track?.stop();
}
}
for (final sid in toStop) {
try { await local.removePublishedTrack(sid); } catch (_) {}
}
}
try { await _room?.localParticipant?.setScreenShareEnabled(false); } catch (_) {}
await CallForeground.screenCaptureStopped('livekit');
isScreenSharing = false;
_currentScreenShareSourceId = null;
_screenShareTrack = null;
notifyListeners();
}
@override
Future<void> hangUp() async {
// Entwertet einen evtl. noch laufenden Verbindungsaufbau (s. _callGeneration).
_callGeneration++;
unawaited(CallForeground.callEnded('livekit'));
_stopStatsLogging();
_presenceHeartbeat?.cancel();
_presenceHeartbeat = null;
if (_matrixClient != null && _matrixRoomId != null) {
await _writePresence(false).catchError((_) {});
}
_roomEventListener?.dispose();
_roomEventListener = null;
_room?.removeListener(_onRoomChanged);
await _room?.disconnect();
_room = null;
_displayName = '';
isConnecting = false;
error = null;
isMuted = false;
isCameraOff = false;
isScreenSharing = false;
isDeafened = false;
isVoiceChannel = false;
_mutedBeforeDeafen = false;
_screenShareTrack = null;
_currentScreenShareSourceId = null;
_matrixRoomId = null;
_matrixClient = null;
notifyListeners();
}
@override
Future<void> changeQuality(String qualityKey) async {
debugPrint('[LiveKit] Streamer changing publish quality to: $qualityKey');
_localQualityKey = qualityKey;
final params = qualityPresets[qualityKey];
if (params == null) { notifyListeners(); return; }
if (_room == null) { notifyListeners(); return; }
final local = _room!.localParticipant;
if (local == null) { notifyListeners(); return; }
if (!isCameraOff) {
await local.setCameraEnabled(false);
await Future.delayed(const Duration(milliseconds: 400));
try {
await local.setCameraEnabled(
true,
cameraCaptureOptions: CameraCaptureOptions(params: params),
);
} catch (e) {
debugPrint('[LiveKit] Camera restart error: $e');
isCameraOff = true;
}
}
if (isScreenSharing && _currentScreenShareSourceId != null) {
final sid = _currentScreenShareSourceId!;
await stopScreenShare();
await Future.delayed(const Duration(milliseconds: 400));
await startScreenShare(sid);
}
notifyListeners();
}
@override
Future<void> changeAudioInput(String deviceId) async {
final participant = _room?.localParticipant;
if (participant == null) return;
try {
final track = await LocalAudioTrack.create(AudioCaptureOptions(deviceId: deviceId));
for (final pub in List.of(participant.audioTrackPublications)) {
await participant.removePublishedTrack(pub.sid);
}
await participant.publishAudioTrack(track);
if (isMuted) await participant.setMicrophoneEnabled(false);
} catch (e) { debugPrint('[Audio] error: $e'); }
}
@override
void setRemoteVolume(double volume) {
if (_room == null) return;
final mute = volume < 0.05;
for (final p in _room!.remoteParticipants.values) {
for (final pub in p.audioTrackPublications) {
pub.track?.mediaStreamTrack.enabled = !mute;
}
}
}
void _startStatsLogging() {
_stopStatsLogging();
_statsTimer = Timer.periodic(const Duration(seconds: 5), (_) => _logStats());
}
void _stopStatsLogging() {
_statsTimer?.cancel();
_statsTimer = null;
}
Future<void> _logStats() async {
if (_room == null) return;
// Basic stats logging...
debugPrint('[Stats] SubQuality: $_subscribeQualityKey | PubQuality: $_localQualityKey');
}
}