A cross platform Audiobookshelf client for Android and iOS
Dart
Debounce transient cast idle/disconnect events to keep UI in sync
Nathan Poulsonc8a38edaca04
diff --git a/lib/services/chromecast_service.dart b/lib/services/chromecast_service.dartindex 41e31d91..1185f318 100644--- a/lib/services/chromecast_service.dart+++ b/lib/services/chromecast_service.dart@@ -65,10 +65,20 @@ class ChromecastService extends ChangeNotifier {StreamSubscription? _sessionSub, _mediaStatusSub, _positionSub;Timer? _syncTimer;+ Timer? _idleDebounceTimer;+ Timer? _disconnectDebounceTimer;final _progressSync = ProgressSyncService();bool _initialized = false;bool _isCompletingBook = false;+ // Grace periods for transient cast hiccups. Idle/disconnect events during+ // active casting can be spurious (network blips, track transitions, stream+ // re-subscribe races) - we wait briefly before actually tearing down UI+ // state so the user doesn't see the card vanish while the Chromecast keeps+ // playing on its own.+ static const Duration _idleGrace = Duration(seconds: 5);+ static const Duration _disconnectGrace = Duration(seconds: 5);+// ── Init ──Future<void> init() async {@@ -113,15 +123,36 @@ class ChromecastService extends ChangeNotifier {final state = GoogleCastSessionManager.instance.connectionState;debugPrint('[Cast] SESSION EVENT — connectionState: $state, session: ${session?.device?.friendlyName ?? "null"}');if (state == GoogleCastConnectState.connected) {+ // Cancel any pending disconnect wipe - we're back before the grace expired.+ if (_disconnectDebounceTimer?.isActive ?? false) {+ debugPrint('[Cast] Reconnected within grace period - cancelling disconnect wipe');+ _disconnectDebounceTimer?.cancel();+ _disconnectDebounceTimer = null;+ }+ final wasConnected = _connectionState == CastConnectionState.connected;_connectionState = CastConnectionState.connected;_connectedDeviceName = session?.device?.friendlyName;debugPrint('[Cast] ✓ CONNECTED to: $_connectedDeviceName');_listenToMediaStatus();_listenToPosition();_updateVolumeFromSession();+ // Rehydrate casting metadata if we lost it (e.g. after a disconnect+ // wipe, or if app was restarted while cast was active).+ if (!wasConnected || _castingItemId == null) {+ _rehydrateFromRemoteMedia();+ }} else if (state == GoogleCastConnectState.disconnected) {- debugPrint('[Cast] ✗ DISCONNECTED');- _onDisconnected();+ debugPrint('[Cast] ✗ DISCONNECTED (scheduling wipe in ${_disconnectGrace.inSeconds}s)');+ // Debounce: transient wifi/session blips can emit disconnected then+ // reconnect shortly after. Only wipe state if it sticks.+ _disconnectDebounceTimer?.cancel();+ _disconnectDebounceTimer = Timer(_disconnectGrace, () {+ debugPrint('[Cast] Disconnect grace expired - wiping state');+ _onDisconnected();+ });+ // Reflect "connecting" in the UI so controls aren't fully dead but+ // also aren't claiming a live connection.+ _connectionState = CastConnectionState.connecting;} else {debugPrint('[Cast] ~ CONNECTING...');_connectionState = CastConnectionState.connecting;@@ -148,36 +179,125 @@ class ChromecastService extends ChangeNotifier {_mediaStatusSub?.cancel();_positionSub?.cancel();_syncTimer?.cancel();+ _idleDebounceTimer?.cancel();+ _disconnectDebounceTimer?.cancel();_updateWakeLock(false);_onPlaybackStateChangedCallback?.call(false);notifyListeners();}+ /// After a reconnect where we lost our local casting state, try to read the+ /// current remote media status and repopulate enough metadata for the UI to+ /// show the now-playing card again. Best-effort: if nothing is playing+ /// remotely, this is a no-op.+ void _rehydrateFromRemoteMedia() {+ try {+ final status = GoogleCastRemoteMediaClient.instance.mediaStatus;+ if (status == null) {+ debugPrint('[Cast] Rehydrate: no remote media status available');+ return;+ }+ final info = status.mediaInformation;+ if (info == null) {+ debugPrint('[Cast] Rehydrate: no mediaInformation on status');+ return;+ }+ debugPrint('[Cast] Rehydrate: remote playerState=${status.playerState}, contentId=${info.contentId}');+ // We can't fully reconstruct itemId / chapters from the cast payload+ // alone, but we can at least surface title/author/duration so the UI+ // doesn't look dead. The sync timer and completion logic stay dormant+ // until castItem is invoked again for a known item.+ final meta = info.metadata;+ if (meta is GoogleCastGenericMediaMetadata) {+ _castingTitle ??= meta.title;+ _castingAuthor ??= meta.subtitle;+ }+ if (_castingDuration <= 0) {+ _castingDuration = info.duration?.inSeconds.toDouble() ?? 0;+ }+ switch (status.playerState) {+ case CastMediaPlayerState.playing: _playbackState = CastPlaybackState.playing; break;+ case CastMediaPlayerState.paused: _playbackState = CastPlaybackState.paused; break;+ case CastMediaPlayerState.buffering: _playbackState = CastPlaybackState.buffering; break;+ case CastMediaPlayerState.loading: _playbackState = CastPlaybackState.loading; break;+ default: break;+ }+ notifyListeners();+ } catch (e) {+ debugPrint('[Cast] Rehydrate error: $e');+ }+ }+void _listenToMediaStatus() {_mediaStatusSub?.cancel();_mediaStatusSub = GoogleCastRemoteMediaClient.instance.mediaStatusStream.listen((status) {final prev = _playbackState;++ // Determine the raw target state this event implies.+ CastPlaybackState target;if (status == null) {- _playbackState = CastPlaybackState.idle;+ target = CastPlaybackState.idle;} else {debugPrint('[Cast] Media status: ${status.playerState}');switch (status.playerState) {- case CastMediaPlayerState.playing: _playbackState = CastPlaybackState.playing; break;- case CastMediaPlayerState.paused: _playbackState = CastPlaybackState.paused; break;- case CastMediaPlayerState.buffering: _playbackState = CastPlaybackState.buffering; break;- case CastMediaPlayerState.loading: _playbackState = CastPlaybackState.loading; break;- default: _playbackState = CastPlaybackState.idle;+ case CastMediaPlayerState.playing: target = CastPlaybackState.playing; break;+ case CastMediaPlayerState.paused: target = CastPlaybackState.paused; break;+ case CastMediaPlayerState.buffering: target = CastPlaybackState.buffering; break;+ case CastMediaPlayerState.loading: target = CastPlaybackState.loading; break;+ default: target = CastPlaybackState.idle;}}+ // Any non-idle event means we're still live - cancel any pending idle wipe.+ if (target != CastPlaybackState.idle && (_idleDebounceTimer?.isActive ?? false)) {+ debugPrint('[Cast] Idle grace cancelled - received $target');+ _idleDebounceTimer?.cancel();+ _idleDebounceTimer = null;+ }++ // If the cast reports idle while we still have an active item that isn't+ // near the end, treat it as a transient blip and wait before actually+ // flipping to idle. This prevents the UI card from vanishing on brief+ // stream hiccups / track transitions while the Chromecast keeps playing.+ if (target == CastPlaybackState.idle &&+ _castingItemId != null &&+ _castingDuration > 0 &&+ !_isAdvancingTrack) {+ final pos = _castPosition.inMilliseconds / 1000.0;+ final nearEnd = pos >= _castingDuration - 5;+ final atTrackBoundary = _fallbackTracks != null && _fallbackTrackIdx < _fallbackTracks!.length - 1;++ if (nearEnd || atTrackBoundary) {+ // Real end-of-track/book - apply immediately, existing completion+ // logic below will handle it.+ _playbackState = CastPlaybackState.idle;+ } else if (!(_idleDebounceTimer?.isActive ?? false)) {+ debugPrint('[Cast] Idle during active cast (pos=${pos.toStringAsFixed(1)}s/${_castingDuration.toStringAsFixed(1)}s) - debouncing ${_idleGrace.inSeconds}s');+ _idleDebounceTimer = Timer(_idleGrace, () {+ debugPrint('[Cast] Idle grace expired - applying idle state');+ _playbackState = CastPlaybackState.idle;+ _updateWakeLock(false);+ _onPlaybackStateChangedCallback?.call(false);+ notifyListeners();+ });+ // Leave _playbackState as-is (typically playing/buffering) so the UI+ // keeps showing the cast card during the grace window.+ return;+ } else {+ // Grace already running - keep prior state visible.+ return;+ }+ } else {+ _playbackState = target;+ }+// Notify listeners & manage wake lock on playback state transitionsfinal wasPlaying = prev == CastPlaybackState.playing || prev == CastPlaybackState.buffering;final nowPlaying = _playbackState == CastPlaybackState.playing || _playbackState == CastPlaybackState.buffering;if (wasPlaying != nowPlaying) {_onPlaybackStateChangedCallback?.call(nowPlaying);_updateWakeLock(nowPlaying);-}// Detect playback completion: was playing/buffering → now idle@@ -656,6 +776,7 @@ class ChromecastService extends ChangeNotifier {_castingItemId = _castingEpisodeId = _castingTitle = _castingAuthor = _castingCoverUrl = null;_castingDuration = 0; _castingChapters = [];_fallbackTracks = null; _fallbackOffsets = null; _fallbackTrackIdx = -1;+ _idleDebounceTimer?.cancel();_updateWakeLock(false);_onPlaybackStateChangedCallback?.call(false);notifyListeners();@@ -806,5 +927,13 @@ class ChromecastService extends ChangeNotifier {}@override- void dispose() { _sessionSub?.cancel(); _mediaStatusSub?.cancel(); _positionSub?.cancel(); _syncTimer?.cancel(); super.dispose(); }+ void dispose() {+ _sessionSub?.cancel();+ _mediaStatusSub?.cancel();+ _positionSub?.cancel();+ _syncTimer?.cancel();+ _idleDebounceTimer?.cancel();+ _disconnectDebounceTimer?.cancel();+ super.dispose();+ }}