feat(video_calls): added system android pip for calls

This commit is contained in:
misha
2026-06-10 09:32:43 +03:00
parent 27fe4afd81
commit de795c5274
15 changed files with 436 additions and 33 deletions

View File

@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'package:eatvio_app/core/app_router.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
@@ -46,6 +47,9 @@ class VideoCallManager {
static const Duration _mutedMicOverlayMinInterval = Duration(
milliseconds: 2600,
);
static const Duration _localMicActivationInterruptionGrace = Duration(
milliseconds: 1500,
);
static const double _mutedMicAudioLevelThreshold = 0.008;
static const double _mutedMicEnergyDelta = 1e-6;
@@ -69,6 +73,7 @@ class VideoCallManager {
bool _videoPausedForBackground = false;
bool? _micEnabledBeforeSystemInterruption;
int _audioInterruptionHoldCount = 0;
DateTime? _ignoreAudioInterruptionUntil;
OverlayEntry? _systemInterruptionEntry;
VideoCallManagerSnapshot _snapshot = VideoCallManagerSnapshot.initial;
@@ -367,6 +372,14 @@ class VideoCallManager {
/// [currentUiEnabled] reflects what the user sees on the mic control
/// ([VideoCallSessionState.isMicActiveForUi]), not only stored intent.
Future<void> toggleMic(bool currentUiEnabled) async {
if (!currentUiEnabled &&
_snapshot.systemAudioInterrupted &&
_snapshot.micEnabled &&
_isLocalMicrophonePublishing()) {
_clearSystemInterruptionState();
_lastMutedMicTotalEnergy = null;
return;
}
final next = !currentUiEnabled;
if (_snapshot.systemAudioInterrupted || _audioInterruptionHoldCount > 0) {
if (next) {
@@ -397,6 +410,9 @@ class VideoCallManager {
}
_emitSnapshot(_snapshot.copyWith(micEnabled: next));
_lastMutedMicTotalEnergy = null;
if (next) {
_suppressOwnMicActivationInterruptions();
}
await _room!.localParticipant?.setMicrophoneEnabled(
next,
audioCaptureOptions: _micCaptureOptions,
@@ -608,6 +624,7 @@ class VideoCallManager {
_recordingOverlayScheduled = false;
_micEnabledBeforeSystemInterruption = null;
_audioInterruptionHoldCount = 0;
_ignoreAudioInterruptionUntil = null;
_joinInProgress = false;
_clearPreparedConnectionCredentials();
_syncSystemInterruptionOverlay(show: false);
@@ -1056,6 +1073,9 @@ class VideoCallManager {
_snapshot.connectPhase != VideoCallConnectPhase.connected) {
return;
}
if (_shouldIgnoreAudioInterruption(event)) {
return;
}
if (event.begin) {
_audioInterruptionHoldCount++;
if (_audioInterruptionHoldCount == 1) {
@@ -1115,6 +1135,7 @@ class VideoCallManager {
if (lp == null) return;
try {
_suppressOwnMicActivationInterruptions();
await lp.setMicrophoneEnabled(
true,
audioCaptureOptions: _micCaptureOptions,
@@ -1131,6 +1152,24 @@ class VideoCallManager {
}
}
void _suppressOwnMicActivationInterruptions() {
if (!Platform.isAndroid) return;
_ignoreAudioInterruptionUntil = DateTime.now().add(
_localMicActivationInterruptionGrace,
);
}
bool _shouldIgnoreAudioInterruption(AudioInterruptionEvent event) {
if (!Platform.isAndroid || !event.begin) return false;
final until = _ignoreAudioInterruptionUntil;
if (until == null) return false;
if (DateTime.now().isBefore(until)) {
return true;
}
_ignoreAudioInterruptionUntil = null;
return false;
}
Future<void> _restartLocalAudioTrack() async {
if (!_canHandleCallAudio || !_snapshot.micEnabled) return;
final lp = _room!.localParticipant;
@@ -1139,6 +1178,7 @@ class VideoCallManager {
final track = pub?.track;
if (track is lk.LocalAudioTrack) {
try {
_suppressOwnMicActivationInterruptions();
await track.restartTrack();
return;
} catch (_) {}
@@ -1148,6 +1188,7 @@ class VideoCallManager {
false,
audioCaptureOptions: _micCaptureOptions,
);
_suppressOwnMicActivationInterruptions();
await lp.setMicrophoneEnabled(
true,
audioCaptureOptions: _micCaptureOptions,