Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stream narrated EPUBs on Audiobookshelf without downloading them first, play narrated books with commas in their audio file names, stop cars from restarting finished books, keep big downloads from knocking Absorb offline and let you pick what to remove when a book and its ebook are both downloaded

Barnabasf2e2301c3471
diff --git a/lib/providers/_lp_absorbing.dart b/lib/providers/_lp_absorbing.dart
index 31cbbbab..64fead00 100644
--- a/lib/providers/_lp_absorbing.dart
+++ b/lib/providers/_lp_absorbing.dart
@@ -959,7 +959,7 @@ mixin _AbsorbingMixin on ChangeNotifier, _StateMixin, _CoreMixin {
final metadata = media['metadata'] as Map<String, dynamic>? ?? {};
final title = metadata['title'] as String? ?? '';
final author = metadata['authorName'] as String? ?? '';
- if (UpNextNarration.instance.needsDownload(key)) {
+ if (UpNextNarration.instance.needsDownload(key, offline: isOffline)) {
unawaited(UpNextNarration.instance.stopped(key, title));
return;
}
@@ -1145,7 +1145,7 @@ mixin _AbsorbingMixin on ChangeNotifier, _StateMixin, _CoreMixin {
final nextData = next.value;
final media = nextData['media'] as Map<String, dynamic>? ?? {};
final metadata = media['metadata'] as Map<String, dynamic>? ?? {};
- if (UpNextNarration.instance.needsDownload(nextKey)) {
+ if (UpNextNarration.instance.needsDownload(nextKey, offline: isOffline)) {
unawaited(UpNextNarration.instance.stopped(nextKey, metadata['title'] as String? ?? ''));
return;
}
@@ -1521,7 +1521,7 @@ mixin _AbsorbingMixin on ChangeNotifier, _StateMixin, _CoreMixin {
final key = _playlistItemKey(m);
if (key.isEmpty) continue;
if (self.isItemFinishedByKey(key)) continue;
- if (UpNextNarration.instance.needsDownload(key)) {
+ if (UpNextNarration.instance.needsDownload(key, offline: isOffline)) {
unawaited(UpNextNarration.instance.stopped(key, _playlistItemTitle(m) ?? ''));
return;
}
@@ -1967,7 +1967,7 @@ mixin _AbsorbingMixin on ChangeNotifier, _StateMixin, _CoreMixin {
final key = _playlistItemKey(items[i]);
if (key.isEmpty) continue;
if (self.isItemFinishedByKey(key)) continue;
- if (UpNextNarration.instance.needsDownload(key)) {
+ if (UpNextNarration.instance.needsDownload(key, offline: isOffline)) {
unawaited(UpNextNarration.instance.stopped(key, _playlistItemTitle(items[i]) ?? ''));
return;
}
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 9caa9750..b785a813 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -11,6 +11,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'api_service.dart';
import 'download_service.dart';
import 'narrated_books.dart';
+import 'narration_stream.dart';
import 'offline_source.dart';
import 'playback_history_service.dart' hide PlaybackEvent;
import 'progress_sync_service.dart';
@@ -1522,6 +1523,7 @@ class AudioPlayerService extends ChangeNotifier {
double duration,
String title,
String author,
+ bool streamed,
})? _pendingLoadOnlySession;
/// Externally-pushed signal that the server is currently unreachable.
@@ -3845,11 +3847,42 @@ class AudioPlayerService extends ChangeNotifier {
String? result;
if (episodeId == null && !api.isBookOrbit && NarratedBooks.contains(itemId) &&
!_downloadService.isDownloaded(progressKey)) {
- // An Audiobookshelf EPUB that reads itself aloud plays once downloaded.
- debugPrint('[Player] $itemId is narration that is not downloaded - not starting');
- _endAdvanceBuffering();
- _clearState();
- return 'Download this book to play its narration';
+ // An Audiobookshelf EPUB that reads itself aloud: its audio files play
+ // straight out of the EPUB on the server, and otherwise like a download.
+ final prefs = await SharedPreferences.getInstance();
+ final offline = (prefs.getBool('manual_offline_mode') ?? false) || _knownOffline;
+ final stream = offline ? null : await NarrationStream.instance.open(api, itemId, startTime: startTime);
+ if (_startSuperseded(playbackGeneration)) {
+ return _abandonStart(null, null, 'narration, after opening the stream');
+ }
+ if (stream == null) {
+ debugPrint('[Player] $itemId is narration that is not downloaded and '
+ '${offline ? 'this phone is offline' : 'could not be streamed'} - not starting');
+ _endAdvanceBuffering();
+ _clearState();
+ return 'Download this book to play its narration';
+ }
+ // The stream's timeline is laid on the audio's real lengths.
+ totalDuration = stream.duration;
+ _totalDuration = stream.duration;
+ if (stream.chapters.isNotEmpty) {
+ chapters = stream.chapters;
+ _chapters = stream.chapters;
+ _handler?.updateChaptersQueue(stream.chapters);
+ }
+ result = await _playFromLocal(
+ progressKey,
+ title,
+ author,
+ coverUrl,
+ totalDuration,
+ chapters,
+ startTime,
+ forceStartTime,
+ loadOnly,
+ playbackGeneration,
+ stream,
+ );
} else if (_downloadService.isDownloaded(progressKey)) {
result = await _playFromLocal(
progressKey,
@@ -4281,6 +4314,9 @@ class AudioPlayerService extends ChangeNotifier {
bool forceStartTime = false,
bool loadOnly = false,
int? generation,
+ // A narration streamed out of its EPUB on the server: its files and
+ // timeline stand in for a download's.
+ NarrationStreamSource? stream,
]) async {
final gen = generation ?? _playbackGeneration;
if (_startSuperseded(gen)) return null;
@@ -4288,7 +4324,9 @@ class AudioPlayerService extends ChangeNotifier {
// to a newer start as soon as one begins.
final rawItemId = _currentItemId!;
final episodeId = _currentEpisodeId;
- debugPrint('[Player] Playing from local files: $title');
+ debugPrint(stream != null
+ ? '[Player] Streaming the narration out of the EPUB: $title'
+ : '[Player] Playing from local files: $title');
// Alpha [PodDur]: trace podcast-episode duration loading. Symptom:
// Android Auto progress bar missing for ~60s on cold-start podcast play
// because the first MediaItem push carries dur=0. We want to know what
@@ -4325,6 +4363,10 @@ class AudioPlayerService extends ChangeNotifier {
debugPrint(
'[Player] Forced start time: ${startTime}s — skipping server/local position comparison',
);
+ } else if (_downloadService.isFromOtherAccount(rawItemId)) {
+ // The server signed in now doesn't hold this book's place, and could
+ // hold a different book's under the same id.
+ debugPrint('[Player] Another account\'s download - playing its spot on this phone, ${startTime.toStringAsFixed(1)}s');
} else if (!_playFromUi && startTime > 0) {
debugPrint(
'[Player] Skipping progress reconcile - not started from the app screen, playing local position ${startTime.toStringAsFixed(1)}s now',
@@ -4398,7 +4440,7 @@ class AudioPlayerService extends ChangeNotifier {
if (_startSuperseded(gen)) {
return _abandonStart(null, null, 'local, after the position check');
}
- final localPaths = _downloadService.getLocalPaths(itemId);
+ final localPaths = stream?.urls ?? _downloadService.getLocalPaths(itemId);
if (localPaths == null || localPaths.isEmpty) {
debugPrint('[Player] No local files found');
_clearState();
@@ -4406,7 +4448,7 @@ class AudioPlayerService extends ChangeNotifier {
}
// Get cached session data for track durations (and chapters if needed)
- final cachedJson = _downloadService.getCachedSessionData(itemId);
+ final cachedJson = stream != null ? jsonEncode(stream.session) : _downloadService.getCachedSessionData(itemId);
List<dynamic>? audioTracks;
if (cachedJson != null) {
try {
@@ -4552,6 +4594,7 @@ class AudioPlayerService extends ChangeNotifier {
duration: totalDuration,
title: title,
author: author,
+ streamed: stream != null,
);
_handler?.refreshPlaybackState();
notifyListeners();
@@ -4579,6 +4622,7 @@ class AudioPlayerService extends ChangeNotifier {
startTime: startTime,
displayTitle: title,
displayAuthor: author,
+ streamed: stream != null,
);
Future.delayed(const Duration(milliseconds: 500), () {
_handler?.refreshPlaybackState();
@@ -7348,6 +7392,7 @@ class AudioPlayerService extends ChangeNotifier {
startTime: position.inMilliseconds / 1000.0,
displayTitle: pendingSession.title,
displayAuthor: pendingSession.author,
+ streamed: pendingSession.streamed,
);
// The loadOnly playItem returned before _setupSync, so this session
// has no completion listener, no position sync, no EQ attach and no
diff --git a/lib/services/download_service.dart b/lib/services/download_service.dart
index 0b38af55..b373fd58 100644
--- a/lib/services/download_service.dart
+++ b/lib/services/download_service.dart
@@ -19,6 +19,8 @@ import 'ebook_cache.dart';
import 'narrated_books.dart';
import 'narration_service.dart';
import 'offline_source.dart';
+import 'server_backend.dart';
+import '../utils/percent_escapes.dart';
import 'user_account_service.dart';
enum DownloadStatus { none, downloading, downloaded, error }
@@ -168,6 +170,10 @@ class _PendingBook {
/// chosen, so a burst of terminal updates can't finalize the book twice.
bool finalizing = false;
+ /// Times a narrated EPUB went on with a fresh sign-in after the server
+ /// turned it away.
+ int signInRenewals = 0;
+
/// A hard track failure aborts the whole book; remember why for the message.
bool failing = false;
TaskException? failException;
@@ -318,7 +324,9 @@ class DownloadService extends ChangeNotifier {
DownloadService._()
: _initializeOverride = null,
_cancelTasksOverride = null,
- _deleteTaskRecordOverride = null;
+ _deleteTaskRecordOverride = null {
+ UserAccountService.onScopeMoved = _moveOrigins;
+ }
@visibleForTesting
DownloadService.forTesting({
@@ -356,12 +364,63 @@ class DownloadService extends ChangeNotifier {
}
/// Whether [itemId] is a download made on a different account than the one
- /// signed in now. Downloads from before origins were kept count as this
- /// account's.
+ /// signed in now. Downloads from before origins were kept give themselves
+ /// away by their id: a BookOrbit book's is a number and an Audiobookshelf
+ /// one's never is.
bool isFromOtherAccount(String itemId) {
+ final accounts = UserAccountService();
+ final current = accounts.activeScopeKey;
+ if (current.isEmpty) return false;
final origin = _origins[itemId];
- final current = UserAccountService().activeScopeKey;
- return origin != null && current.isNotEmpty && origin != current;
+ if (origin == current) return false;
+ final bookOrbitId = int.tryParse(itemId) != null;
+ if (origin == null) return bookOrbitId != ServerBackend.active.isBookOrbit;
+ final me = accounts.accounts.where((a) => a.scopeKey == current).firstOrNull;
+ if (me == null) return true;
+ // Another user on the same server has the same books under the same
+ // ids, so their download is this account's to read and sync too.
+ if (origin.startsWith('${_serverOf(me)}_')) return false;
+ final from = accounts.accounts.where((a) => a.scopeKey == origin).firstOrNull ??
+ _movedAccount(origin, accounts.accounts, bookOrbit: bookOrbitId);
+ return from == null || _serverOf(from) != _serverOf(me);
+ }
+
+ static String _serverOf(SavedAccount a) =>
+ a.scopeKey.substring(0, a.scopeKey.length - a.username.length - 1);
+
+ /// The saved account an origin no account has any more stands for: its
+ /// address was changed before origins followed it, so it is the one
+ /// account with that user on the same kind of server.
+ static SavedAccount? _movedAccount(String origin, List<SavedAccount> accounts, {required bool bookOrbit}) {
+ final candidates = accounts
+ .where((a) => origin.endsWith('_${a.username}') && a.backend.isBookOrbit == bookOrbit)
+ .toList();
+ return candidates.length == 1 ? candidates.single : null;
+ }
+
+ @visibleForTesting
+ void debugSetOrigin(String itemId, String scope) => _origins[itemId] = scope;
+
+ /// An account's address changed: its downloads are still its own, and so
+ /// are the places kept for another account's books.
+ Future<void> _moveOrigins(String from, String to) async {
+ var moved = 0;
+ _origins.updateAll((_, scope) {
+ if (scope != from) return scope;
+ moved++;
+ return to;
+ });
+ try {
+ final prefs = await SharedPreferences.getInstance();
+ final spots = jsonDecode(prefs.getString(_otherAccountSpotsKey) ?? '{}') as Map<String, dynamic>;
+ final renamed = {
+ for (final e in spots.entries)
+ e.key.startsWith('$from|') ? '$to|${e.key.substring(from.length + 1)}' : e.key: e.value,
+ };
+ await prefs.setString(_otherAccountSpotsKey, jsonEncode(renamed));
+ } catch (_) {}
+ if (moved > 0) await _save();
+ debugPrint('[Download] $moved download(s) moved with their account: $from -> $to');
}
static const _otherAccountSpotsKey = 'other_account_reading';
@@ -422,6 +481,11 @@ class DownloadService extends ChangeNotifier {
/// Play sessions kept open for downloads that use their temporary links.
final Map<String, ({ApiService api, String sessionId})> _linkSessions = {};
+ /// The API each narrated EPUB download started with, so one the server
+ /// turns away for a lapsed sign-in can go on with a fresh one. Empty after
+ /// a restart, when such a download fails the way it always did.
+ final Map<String, ApiService> _narrationApis = {};
+
StreamSubscription<TaskUpdate>? _updatesSub;
bool _downloaderConfigured = false;
@@ -504,7 +568,7 @@ class DownloadService extends ChangeNotifier {
final uri = Uri.parse(uriString);
var docId =
uri.pathSegments.isNotEmpty ? uri.pathSegments.last : uriString;
- docId = Uri.decodeComponent(docId);
+ docId = undoPercentEscapes(docId);
final colon = docId.indexOf(':');
final rel = colon >= 0 ? docId.substring(colon + 1) : docId;
return rel.isEmpty ? docId : rel;
@@ -678,6 +742,114 @@ class DownloadService extends ChangeNotifier {
debugPrint('[Download] ebook kept for offline: $itemId');
}
+ /// What a downloaded book has on the phone, for choosing what to remove:
+ /// whether its audio is here and how big it is (null when that can't be
+ /// told, as in a custom folder), the ebook copy's size, or null when there
+ /// is no copy, and whether the audio was taken out of that ebook (a
+ /// narrated EPUB, one file on the server and one book to remove).
+ Future<({bool audio, int? audioBytes, int? ebookBytes, bool fromEbook})> onPhone(String itemId) async {
+ final info = _downloads[itemId];
+ final audio = info != null && info.localPaths.isNotEmpty;
+ var fromEbook = false;
+ if (audio) {
+ try {
+ final media = ((jsonDecode(info.sessionData ?? '') as Map)['libraryItem'] as Map?)?['media'] as Map?;
+ final audioFiles = (media?['audioFiles'] as List?) ?? const [];
+ fromEbook = audioFiles.isEmpty && (media?['narration'] != null || NarratedBooks.contains(itemId));
+ } catch (_) {}
+ }
+ int? audioBytes;
+ if (audio) {
+ var total = 0;
+ var known = true;
+ for (final path in info.localPaths) {
+ if (isContentUri(path)) {
+ known = false;
+ continue;
+ }
+ try {
+ final f = File(path);
+ if (f.existsSync()) total += f.lengthSync();
+ } catch (_) {}
+ }
+ // Files in a custom folder can't be measured here; the server's sizes
+ // for the book's audio stand in.
+ audioBytes = known ? total : _serverAudioBytes(info.sessionData);
+ }
+ int? ebookBytes;
+ try {
+ final entry = await cachedEbookFileFor(itemId);
+ if (entry != null) {
+ final f = await ebookCacheFileFor(itemId, entry);
+ if (f.existsSync()) ebookBytes = f.lengthSync();
+ }
+ } catch (_) {}
+ return (audio: audio, audioBytes: audioBytes, ebookBytes: ebookBytes, fromEbook: fromEbook);
+ }
+
+ static int? _serverAudioBytes(String? sessionData) {
+ try {
+ final session = jsonDecode(sessionData ?? '') as Map<String, dynamic>;
+ final media = (session['libraryItem'] as Map?)?['media'] as Map?;
+ final files = (media?['audioFiles'] as List?) ?? (session['audioTracks'] as List?) ?? const [];
+ var total = 0;
+ for (final f in files.whereType<Map>()) {
+ total += ((f['metadata'] as Map?)?['size'] as num?)?.toInt() ?? 0;
+ }
+ return total > 0 ? total : null;
+ } catch (_) {
+ return null;
+ }
+ }
+
+ /// Takes a downloaded book's ebook copy off the phone and leaves its audio.
+ Future<void> deleteEbookCopy(String itemId) async {
+ _keptEbooks.remove(itemId);
+ await deleteCachedEbook(itemId);
+ if (!_downloads.containsKey(itemId)) _origins.remove(itemId);
+ await _save();
+ notifyListeners();
+ }
+
+ /// Takes a downloaded book's audio off the phone and keeps its ebook copy,
+ /// listed on its own the way one the reader kept is.
+ Future<void> deleteAudioKeepEbook(String itemId) async {
+ final info = _downloads[itemId];
+ if (info != null && !_keptEbooks.containsKey(itemId)) {
+ Map<String, dynamic>? stored;
+ try {
+ stored = (jsonDecode(info.sessionData ?? '') as Map<String, dynamic>)['libraryItem'] as Map<String, dynamic>?;
+ } catch (_) {}
+ String? cover;
+ try {
+ final from = info.localCoverPath == null ? null : File(info.localCoverPath!);
+ if (from != null && from.existsSync()) {
+ final to = await ebookCoverCacheFile(itemId);
+ await from.copy(to.path);
+ cover = to.path;
+ }
+ } catch (_) {}
+ _keptEbooks[itemId] = DownloadInfo(
+ itemId: itemId,
+ status: DownloadStatus.downloaded,
+ sessionData: stored == null
+ ? null
+ : jsonEncode({
+ 'libraryItem': stored,
+ 'mediaMetadata': (stored['media'] as Map?)?['metadata'],
+ 'duration': 0,
+ 'chapters': const <dynamic>[],
+ }),
+ title: info.title,
+ author: info.author,
+ coverUrl: info.coverUrl,
+ localCoverPath: cover,
+ libraryId: info.libraryId,
+ );
+ }
+ await deleteDownload(itemId, byUser: true);
+ }
+
Future<void> deleteKeptEbook(String itemId) async {
if (_keptEbooks.remove(itemId) == null) return;
if (!_downloads.containsKey(itemId)) _origins.remove(itemId);
@@ -1077,7 +1249,13 @@ class DownloadService extends ChangeNotifier {
// gone, so they hold a slot with e.g. 40% forever). A healthy transfer
// emits updates constantly, so real downloads never sit silent this long
// while the app is running.
- final silence = DateTime.now().difference(p.lastUpdate);
+ // A book waiting in the downloader's queue behind another one's files
+ // hears nothing while the other book moves, and isn't stuck.
+ var heardAt = p.lastUpdate;
+ for (final other in _pending.values) {
+ if (other.lastUpdate.isAfter(heardAt)) heardAt = other.lastUpdate;
+ }
+ final silence = DateTime.now().difference(heardAt);
if ((p.overallProgress == 0 && silence > const Duration(minutes: 3)) ||
silence > const Duration(minutes: 10)) {
debugPrint('[Download] Reconciler failing stale $itemId '
@@ -1102,12 +1280,20 @@ class DownloadService extends ChangeNotifier {
// real start reconfigures back to always.
if (Platform.isAndroid) {
try {
- await FileDownloader().configure(androidConfig: [
- (
- Config.runInForeground,
- backgroundIsolateMode ? Config.never : Config.always,
- ),
- ]);
+ await FileDownloader().configure(
+ // A book in 93 files started all 93 at once, which swamped the
+ // server and the connection until Absorb's own requests timed out
+ // and it went offline. The queue is native, so it keeps feeding
+ // files with the app in the background.
+ globalConfig: [(Config.holdingQueue, (6, 4, null))],
+ androidConfig: [
+ (
+ Config.runInForeground,
+ backgroundIsolateMode ? Config.never : Config.always,
+ ),
+ ],
+ );
+ debugPrint('[Download] at most 4 files at a time per server, 6 in all');
} catch (e) {
debugPrint('[Download] androidConfig failed: $e');
}
@@ -1806,7 +1992,12 @@ class DownloadService extends ChangeNotifier {
Future<void> _cacheEbookForOffline(
ApiService api, String itemId, Map<String, dynamic> ebookFile, String title) async {
try {
- final f = await NarrationService.instance.readerFile(api, itemId, ebookFile, title);
+ // A narrated EPUB beside the audiobook comes as the reader's copy
+ // without its audio. Should that fail, the whole file (hundreds of MB
+ // of narration the audiobook already has) isn't fetched in its place.
+ final size = ((ebookFile['metadata'] as Map?)?['size'] as num?)?.toInt() ?? 0;
+ final f = await NarrationService.instance
+ .readerFile(api, itemId, ebookFile, title, wholeFallback: size < 50 * 1024 * 1024);
await _excludeFromBackup(f.path);
debugPrint('[Download] cached ebook for offline: $itemId');
} catch (e) {
@@ -2253,6 +2444,7 @@ class DownloadService extends ChangeNotifier {
final filename = _sanitizePath(fileMeta?['filename'] as String? ?? 'book.epub');
final size = (fileMeta?['size'] as num?)?.toInt();
final target = await api.narratedEpubDownload(itemId, ino);
+ _narrationApis[itemId] = api;
final url = target.url;
final wifiOnly = await PlayerSettings.getWifiOnlyDownloads();
// BookOrbit streams and keeps places by its own timings, so the
@@ -2523,7 +2715,7 @@ class DownloadService extends ChangeNotifier {
var originalName = trackMeta?['filename'] as String? ?? '';
if (originalName.isEmpty) {
final contentPath = Uri.tryParse(contentUrl)?.path ?? contentUrl;
- originalName = Uri.decodeComponent(contentPath.split('/').last);
+ originalName = undoPercentEscapes(contentPath.split('/').last);
if (originalName.contains('?')) originalName = originalName.split('?').first;
}
if (originalName.isNotEmpty && originalName.contains('.')) {
@@ -2616,6 +2808,11 @@ class DownloadService extends ChangeNotifier {
_emitBookProgress(itemId, p);
}
} else if (update is TaskStatusUpdate) {
+ if (_signInRanOut(itemId, p, update)) {
+ p.signInRenewals++;
+ unawaited(_carryOnWithFreshSignIn(itemId, i, update));
+ return;
+ }
unawaited(
_handleTaskStatus(
itemId: itemId,
@@ -2628,6 +2825,44 @@ class DownloadService extends ChangeNotifier {
}
}
+ /// A narrated EPUB downloads whole as one long request, signed in once.
+ /// When it has to start a new request (a dropped connection, Android's
+ /// nine-minute work limit) after BookOrbit's 15-minute sign-in has lapsed,
+ /// the server says 401 and the plugin retries with the same headers.
+ bool _signInRanOut(String itemId, _PendingBook p, TaskStatusUpdate update) {
+ if (update.status != TaskStatus.failed || !p.narration) return false;
+ if (p.cancelled || p.failing || p.signInRenewals >= 2) return false;
+ if (!_narrationApis.containsKey(itemId)) return false;
+ final ex = update.exception;
+ final code = update.responseStatusCode ?? (ex is TaskHttpException ? ex.httpResponseCode : null);
+ return code == 401;
+ }
+
+ Future<void> _carryOnWithFreshSignIn(String itemId, int trackIndex, TaskStatusUpdate update) async {
+ final task = update.task;
+ final api = _narrationApis[itemId];
+ if (task is DownloadTask && api != null) {
+ try {
+ final headers = await api.renewedMediaHeaders();
+ final renewed = task.copyWith(headers: {...task.headers, ...headers});
+ final resumed = await FileDownloader().resume(renewed);
+ final ok = resumed || await FileDownloader().enqueue(renewed);
+ debugPrint('[Download] $itemId: the sign-in ran out mid-download, '
+ '${ok ? (resumed ? 'resuming' : 'starting over') : 'could not go on'} with a fresh one');
+ if (ok) return;
+ } catch (e) {
+ debugPrint('[Download] $itemId: could not renew the download sign-in: $e');
+ }
+ }
+ await _handleTaskStatus(
+ itemId: itemId,
+ trackIndex: trackIndex,
+ status: update.status,
+ exception: update.exception,
+ responseCode: update.responseStatusCode,
+ );
+ }
+
Future<void> _handleTaskStatus({
required String itemId,
required int trackIndex,
@@ -2681,6 +2916,7 @@ class DownloadService extends ChangeNotifier {
final s = p.trackStatus[i];
if (s == null || !_terminal.contains(s)) return;
}
+ _narrationApis.remove(itemId);
if (p.trackStatus.values.every((s) => s == TaskStatus.complete)) {
p.finalizing = true;
await _finalizeSuccess(itemId);
@@ -3100,11 +3336,7 @@ class DownloadService extends ChangeNotifier {
bool _folderTakenByOther(String nestedName, String itemId) {
bool matches(String? dir) {
if (dir == null) return false;
- var d = dir;
- try {
- d = Uri.decodeComponent(dir);
- } catch (_) {}
- d = d.replaceAll(r'\', '/');
+ final d = undoPercentEscapes(dir).replaceAll(r'\', '/');
return d.endsWith('/$nestedName');
}
diff --git a/lib/services/ebook_cache.dart b/lib/services/ebook_cache.dart
index bad72da2..6fbac278 100644
--- a/lib/services/ebook_cache.dart
+++ b/lib/services/ebook_cache.dart
@@ -193,6 +193,54 @@ Future<void> saveCachedLocations(File epub, String locations) async {
} catch (_) {}
}
+/// Drops the location index kept beside [epub], for a copy that changed.
+Future<void> dropCachedLocations(File epub) async {
+ try {
+ final f = _locationsFileFor(epub);
+ if (f.existsSync()) await f.delete();
+ } catch (_) {}
+}
+
+/// What identifies the server's copy of an ebook, so a cached copy can tell
+/// when the server has rewritten it (BookOrbit writing metadata back, a file
+/// replaced on disk). Null when the item says nothing about it.
+String? ebookFileVersion(Map<String, dynamic> ebookFile) {
+ final v = ebookFile['version'];
+ if (v is String && v.isNotEmpty) return v;
+ final meta = ebookFile['metadata'] as Map?;
+ final mtime = meta?['mtimeMs'];
+ if (mtime is num && mtime > 0) return '${mtime.toInt()}:${meta?['size'] ?? ''}';
+ return null;
+}
+
+File _versionFileFor(File cached) => File('${cached.path}.version');
+
+/// Whether [cached] is still the server's current copy. One cached before
+/// versions were kept takes the current version as its own.
+Future<bool> cachedEbookIsCurrent(File cached, Map<String, dynamic> ebookFile) async {
+ final version = ebookFileVersion(ebookFile);
+ if (version == null) return true;
+ try {
+ final f = _versionFileFor(cached);
+ if (!f.existsSync()) {
+ await f.writeAsString(version);
+ return true;
+ }
+ return await f.readAsString() == version;
+ } catch (_) {
+ return true;
+ }
+}
+
+/// Notes which server copy [cached] is, once it has been fetched.
+Future<void> rememberEbookVersion(File cached, Map<String, dynamic> ebookFile) async {
+ final version = ebookFileVersion(ebookFile);
+ if (version == null) return;
+ try {
+ await _versionFileFor(cached).writeAsString(version);
+ } catch (_) {}
+}
+
/// True if the item's ebook is already cached on disk (readable offline).
Future<bool> isEbookCached(String itemId, Map<String, dynamic> ebookFile) async {
final f = await ebookCacheFileFor(itemId, ebookFile);
@@ -211,10 +259,29 @@ Future<File> fetchEbookToCache(
) async {
final cachedFile = await ebookCacheFileFor(itemId, ebookFile);
if (cachedFile.existsSync() && await cachedFile.length() > 0) {
- debugPrint('[EbookCache] cache hit item=$itemId bytes=${await cachedFile.length()}');
- return cachedFile;
+ if (await cachedEbookIsCurrent(cachedFile, ebookFile)) {
+ debugPrint('[EbookCache] cache hit item=$itemId bytes=${await cachedFile.length()}');
+ return cachedFile;
+ }
+ debugPrint('[EbookCache] item=$itemId changed on the server - fetching the new copy');
+ try {
+ final fresh = await _downloadEbook(api, itemId, ebookFile, cachedFile);
+ await dropCachedLocations(fresh);
+ return fresh;
+ } catch (e) {
+ debugPrint('[EbookCache] item=$itemId new copy failed, keeping the old one: $e');
+ return cachedFile;
+ }
}
+ return _downloadEbook(api, itemId, ebookFile, cachedFile);
+}
+Future<File> _downloadEbook(
+ ApiService api,
+ String itemId,
+ Map<String, dynamic> ebookFile,
+ File cachedFile,
+) async {
final ino = ebookFile['ino'] as String?;
if (ino == null) throw Exception('No ebook file found');
@@ -277,6 +344,7 @@ Future<File> fetchEbookToCache(
}
if (cachedFile.existsSync()) await cachedFile.delete();
await partFile.rename(cachedFile.path);
+ await rememberEbookVersion(cachedFile, ebookFile);
final secs = DateTime.now().difference(startedAt).inMilliseconds / 1000.0;
debugPrint('[EbookCache] downloaded item=$itemId bytes=${await cachedFile.length()} in ${secs}s');
} finally {
diff --git a/lib/services/epub_book_text.dart b/lib/services/epub_book_text.dart
index 4a0c73d6..354e435a 100644
--- a/lib/services/epub_book_text.dart
+++ b/lib/services/epub_book_text.dart
@@ -6,6 +6,7 @@ import 'package:html/parser.dart' as html_parser;
import 'package:xml/xml.dart';
import 'media_overlay.dart';
+import '../utils/percent_escapes.dart';
/// A spot found in a book's text: the spine section, where the words start
/// and end in its text, and how well they matched (0 to 1).
@@ -161,10 +162,7 @@ class EpubBookText {
}
static String _resolve(String base, String href) {
- var clean = href.split('#').first.split('?').first;
- try {
- clean = Uri.decodeFull(clean);
- } catch (_) {}
+ final clean = undoPathEscapes(href.split('#').first.split('?').first);
final parts = <String>[];
for (final p in '${_dir(base)}$clean'.split('/')) {
if (p.isEmpty || p == '.') continue;
@@ -310,17 +308,26 @@ class EpubBookText {
return el == null ? null : _flat(el.text);
}
+ /// Where each element with an id starts in section [si]'s text: the
+ /// offset of its first text. Elements with no text are left out.
+ Map<String, int> elementOffsets(int si) {
+ final s = _section(si);
+ if (s == null) return const {};
+ final out = <String, int>{};
+ for (var k = 0; k < s.nodes.length; k++) {
+ for (html.Node? up = s.nodes[k].parent; up != null; up = up.parent) {
+ if (up is html.Element && up.id.isNotEmpty) out.putIfAbsent(up.id, () => s.starts[k]);
+ if (identical(up, s.root)) break;
+ }
+ }
+ return out;
+ }
+
/// The TOC title for a spine href, matched the way the reader matches it:
/// by file name first, then by the end of the path.
String? chapterForHref(String href) {
if (href.isEmpty || toc.isEmpty) return null;
- String base(String s) {
- var p = s.split('#').first;
- try {
- p = Uri.decodeFull(p);
- } catch (_) {}
- return p.split('/').last.toLowerCase();
- }
+ String base(String s) => undoPercentEscapes(s.split('#').first.split('/').last).toLowerCase();
String? label(String t) => t.trim().isEmpty ? null : t.trim();
final b = base(href);
@@ -449,3 +456,68 @@ class EpubBookText {
return null;
}
}
+
+/// Book time of the narrated sentence a reading spot [cfi] falls in: the
+/// last one in its section to start at or before it, or the section's first
+/// when the spot comes before them all. Null when the spot isn't in [book] or
+/// its section isn't narrated.
+double? narrationTimeAtCfi(MediaOverlay overlay, EpubBookText book, String cfi) =>
+ placeCfiOnNarration(overlay, book, cfi).seconds;
+
+/// [narrationTimeAtCfi], with how the spot was found or why it wasn't.
+/// Another reader's CFI can count a chapter's elements differently (an XML
+/// parse against this HTML one), so the ids it names are tried first: a
+/// narrated sentence it names is the answer outright, and any other named
+/// element gives a place in the text when the steps don't resolve here.
+({double? seconds, String how}) placeCfiOnNarration(MediaOverlay overlay, EpubBookText book, String cfi) {
+ final m = RegExp(r'^epubcfi\((.*)\)$').firstMatch(cfi.trim());
+ final bang = m?.group(1)?.indexOf('!') ?? -1;
+ if (m == null || bang < 0) return (seconds: null, how: 'not a CFI');
+ final inner = m.group(1)!;
+ final pkg = inner.substring(0, bang).split('/').where((p) => p.isNotEmpty).toList();
+ final spineStep = pkg.isEmpty ? null : int.tryParse(pkg.last.split('[').first);
+ if (spineStep == null || spineStep < 2) return (seconds: null, how: 'no chapter in the CFI');
+ final si = spineStep ~/ 2 - 1;
+ final sectionOfHref = <String, int>{};
+ final clips = [
+ for (final c in overlay.clips)
+ if (c.fragment != null && sectionOfHref.putIfAbsent(c.textHref, () => book.sectionOf(c.textHref)) == si) c,
+ ];
+ if (clips.isEmpty) return (seconds: null, how: 'chapter $si is not narrated');
+ // The ids the CFI names, deepest first, leaving out text assertions.
+ final ids = [
+ for (final a in RegExp(r'\[([^\]]*)\]').allMatches(inner.substring(bang + 1)))
+ if (a.group(1)!.isNotEmpty && !a.group(1)!.startsWith(';')) a.group(1)!.split(';').first,
+ ].reversed.toList();
+ for (final id in ids) {
+ for (final c in clips) {
+ if (c.fragment == id) return (seconds: overlay.startOf(c), how: 'it names sentence $id');
+ }
+ }
+ final offsets = book.elementOffsets(si);
+ var target = book.locate(cfi)?.offset;
+ var how = 'its place in chapter $si';
+ if (target == null) {
+ for (final id in ids) {
+ final off = offsets[id];
+ if (off != null) {
+ target = off;
+ how = 'the element $id it names';
+ break;
+ }
+ }
+ }
+ if (target == null) return (seconds: null, how: 'its steps and ids are not in chapter $si here');
+ OverlayClip? best;
+ var bestOffset = -1;
+ for (final c in clips) {
+ final off = offsets[c.fragment];
+ if (off == null || off > target) continue;
+ if (off >= bestOffset) {
+ best = c;
+ bestOffset = off;
+ }
+ }
+ final pick = best ?? clips.first;
+ return (seconds: overlay.startOf(pick), how: how);
+}
diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dart
index 251e5728..939bb6b1 100644
--- a/lib/services/home_widget_service.dart
+++ b/lib/services/home_widget_service.dart
@@ -18,6 +18,7 @@ import 'progress_sync_service.dart';
import 'scoped_prefs.dart';
import 'user_account_service.dart';
import 'wear_player_service.dart';
+import 'sleep_timer_service.dart';
const String _androidWidgetName = 'NowPlayingWidget';
const String _androidWidgetCompactName = 'NowPlayingWidgetCompact';
@@ -134,9 +135,11 @@ class HomeWidgetService {
final player = AudioPlayerService();
player.addListener(_onPlayerChanged);
+ SleepTimerService().addListener(_onSleepChanged);
// Listen for widget click actions (e.g. play/pause button)
_clickSub = HomeWidget.widgetClicked.listen(_onWidgetClicked);
+ WearPlayerService.instance.listen();
// Check if the app was cold-started from a widget tap
final launchUri = await HomeWidget.initiallyLaunchedFromHomeWidget();
@@ -206,6 +209,22 @@ class HomeWidgetService {
return duration > 0 && currentTime >= duration - 1.0;
}
+ /// Whether the server holds the last-played item as finished. The phone
+ /// only knows a book is done when it finished here, so one finished on
+ /// another device or marked finished on the server still read as something
+ /// to resume. Offline, slow or no record reads as not finished and leaves
+ /// the local answer standing.
+ static Future<bool> finishedOnServer(
+ Future<Map<String, dynamic>?> Function() fetch, {
+ Duration wait = const Duration(seconds: 3),
+ }) async {
+ try {
+ return lastPlayedIsFinished(await fetch().timeout(wait));
+ } catch (_) {
+ return false;
+ }
+ }
+
/// The stash readers/writers below can run before [init] (the boot-time
/// engine adopt fires right after the player service starts), and the
/// HomeWidget plugin throws on iOS until it has been told the app group.
@@ -283,6 +302,7 @@ class HomeWidgetService {
_pendingUpdate?.cancel();
_clickSub?.cancel();
AudioPlayerService().removeListener(_onPlayerChanged);
+ SleepTimerService().removeListener(_onSleepChanged);
}
void _onWidgetClicked(Uri? uri) {
@@ -312,6 +332,20 @@ class HomeWidgetService {
_handlePlayItem(itemId, episodeId);
}
break;
+ case '/next_chapter':
+ case '/prev_chapter':
+ case '/bookmark':
+ case '/stop':
+ case '/sleep':
+ case '/seek':
+ case '/speed':
+ // The watch asked while the app was down, so it was started with
+ // the request in its launch URI.
+ WearCommands.handle(
+ uri.path.substring(1),
+ Map<String, dynamic>.from(uri.queryParameters),
+ );
+ break;
}
}
}
@@ -493,6 +527,16 @@ class HomeWidgetService {
);
if (api == null) return;
+ // A car that sends play as it connects then started a book finished
+ // elsewhere from the top, and the first sync un-finished it.
+ if (await finishedOnServer(() => api.getItemProgress(progressKey))) {
+ debugPrint(
+ '[HomeWidget] play_pause: last played $progressKey is finished on '
+ 'the server - nothing to resume',
+ );
+ return;
+ }
+
try {
// A downloaded item needs nothing from the server to start: the download
// record already carries title/author/duration/chapters, so play it now
@@ -656,6 +700,23 @@ class HomeWidgetService {
_scheduleUpdate();
}
+ // The timer ticks every second and the watch counts down by itself, so
+ // only a timer set, added to, cancelled or down another minute goes out.
+ String? _sleepSig;
+
+ void _onSleepChanged() {
+ final sleep = SleepTimerService();
+ final sig = switch (sleep.mode) {
+ SleepTimerMode.time => 'time:${sleep.timeRemaining.inMinutes}',
+ SleepTimerMode.chapters => 'chapters:${sleep.chaptersRemaining}',
+ SleepTimerMode.episodes => 'episodes',
+ SleepTimerMode.off => null,
+ };
+ if (sig == _sleepSig) return;
+ _sleepSig = sig;
+ _onPlayerChanged();
+ }
+
/// Schedule an update on the next microtask so we never do async work
/// inside the synchronous ChangeNotifier callback.
void _scheduleUpdate() {
@@ -786,7 +847,17 @@ class HomeWidgetService {
}
await _updateAllWidgets();
- _pushToWear(player, hasBook, skipBack, skipForward);
+ final rewind = await AutoRewindSettings.load();
+ final skipBarrier = await PlayerSettings.getSkipChapterBarrier();
+ final speedAdjusted = await PlayerSettings.getSpeedAdjustedTime();
+ final sleepAddMinutes = await PlayerSettings.getShakeAddMinutes();
+ final speedPresets = await PlayerSettings.getSpeedPresets();
+ // The app's colour, picked or taken from the playing cover, so the
+ // watch can wear it too.
+ final themeColor = await PlayerSettings.getColorSource() == 'manual'
+ ? await PlayerSettings.getManualSeedColor()
+ : (await PlayerSettings.getCoverSeedColor() ?? 0xFF7C6FBF);
+ _pushToWear(player, hasBook, skipBack, skipForward, rewind, skipBarrier, speedAdjusted, sleepAddMinutes, speedPresets, themeColor);
}
/// Mirror the same snapshot we just wrote to the home widget to the
@@ -797,9 +868,35 @@ class HomeWidgetService {
bool hasBook,
int skipBack,
int skipForward,
+ AutoRewindSettings rewind,
+ bool skipChapterBarrier,
+ bool speedAdjustedTime,
+ int sleepAddMinutes,
+ List<double> speedPresets,
+ int themeColor,
) {
if (!Platform.isAndroid) return;
final chapterTitle = player.currentChapter?['title'] as String?;
+ final chapters = hasBook ? player.chapters : const <dynamic>[];
+ var chapterIndex = -1;
+ if (hasBook && chapters.isNotEmpty) {
+ final posS = player.position.inMilliseconds / 1000.0;
+ for (var i = chapters.length - 1; i >= 0; i--) {
+ final start = ((chapters[i] as Map)['start'] as num?)?.toDouble() ?? 0;
+ if (posS >= start) {
+ chapterIndex = i;
+ break;
+ }
+ }
+ }
+ final sleep = SleepTimerService();
+ final sleepMode = switch (sleep.mode) {
+ SleepTimerMode.time => 'time',
+ SleepTimerMode.chapters => 'chapters',
+ SleepTimerMode.episodes => 'episodes',
+ SleepTimerMode.off => null,
+ };
+ WearPlayerService.instance.publishChapters(hasBook ? player.currentItemId : null, chapters);
WearPlayerService.instance.publish(
hasBook: hasBook,
itemId: hasBook ? player.currentItemId : null,
@@ -812,6 +909,26 @@ class HomeWidgetService {
speed: player.speed.toDouble(),
skipBackSec: skipBack,
skipForwardSec: skipForward,
+ chapterStartMs: hasBook ? (player.currentChapterStart * 1000).round() : 0,
+ chapterEndMs: hasBook ? (player.currentChapterEnd * 1000).round() : 0,
+ autoRewindEnabled: rewind.enabled,
+ autoRewindMinSec: rewind.minRewind,
+ autoRewindMaxSec: rewind.maxRewind,
+ autoRewindDelaySec: rewind.activationDelay,
+ autoRewindChapterBarrier: rewind.chapterBarrier,
+ autoRewindSessionStart: rewind.sessionStartRewind,
+ skipChapterBarrier: skipChapterBarrier,
+ speedAdjustedTime: speedAdjustedTime,
+ chapterIndex: chapterIndex,
+ chapterCount: chapters.length,
+ sleepMode: sleepMode,
+ sleepAddMinutes: sleepAddMinutes,
+ speedPresets: speedPresets,
+ sleepEndsAtMs: sleep.mode == SleepTimerMode.time
+ ? DateTime.now().millisecondsSinceEpoch + sleep.timeRemaining.inMilliseconds
+ : 0,
+ sleepChaptersLeft: sleep.mode == SleepTimerMode.chapters ? sleep.chaptersRemaining : 0,
+ themeColor: themeColor,
);
}
diff --git a/lib/services/image_epub.dart b/lib/services/image_epub.dart
index 01a6025b..c89a01be 100644
--- a/lib/services/image_epub.dart
+++ b/lib/services/image_epub.dart
@@ -4,6 +4,8 @@ import 'dart:io';
import 'package:archive/archive_io.dart';
import 'package:flutter/foundation.dart' show compute, debugPrint;
+import '../utils/percent_escapes.dart';
+
/// EPUBs whose pages are pictures (scanned or comic-style books) carry no
/// text for the EPUB reader to lay out or index, so they read better in the
/// comic reader. This finds their page images in reading order.
@@ -42,7 +44,7 @@ String _dir(String path) {
/// [href] relative to [base]'s folder, as a path inside the zip.
String _resolve(String base, String href) {
- final clean = Uri.decodeFull(href.split('#').first.split('?').first);
+ final clean = undoPathEscapes(href.split('#').first.split('?').first);
final parts = <String>[];
for (final p in '${_dir(base)}$clean'.split('/')) {
if (p.isEmpty || p == '.') continue;
diff --git a/lib/services/local_session_service.dart b/lib/services/local_session_service.dart
index d914c5f4..6676bd0e 100644
--- a/lib/services/local_session_service.dart
+++ b/lib/services/local_session_service.dart
@@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'api_service.dart';
+import 'download_service.dart';
import 'progress_sync_service.dart';
import 'scoped_prefs.dart';
@@ -107,6 +108,7 @@ class LocalSessionService {
'date': s['date'],
'dayOfWeek': s['dayOfWeek'],
if (s['keepProgress'] == true) 'keepProgress': true,
+ if (s['playMethod'] != null) 'playMethod': s['playMethod'],
};
/// Swap the payload's duration for the item's server-side duration when it
@@ -163,15 +165,27 @@ class LocalSessionService {
required double startTime,
String? displayTitle,
String? displayAuthor,
+ bool streamed = false,
}) async {
await _loadActive();
+ // Narration streamed from the EPUB on the server is still logged through
+ // a local session, but the server should show it as Direct Play.
+ final playMethod = streamed ? 0 : null;
+ // Listening to another account's download is that account's, not the
+ // one signed in now, so it isn't logged here.
+ if (DownloadService().isFromOtherAccount(progressKey)) {
+ if (_active != null) await finalizeActive();
+ debugPrint('[LocalSession] $progressKey is another account\'s download - not logged');
+ return;
+ }
final now = clock();
final nowMs = now.millisecondsSinceEpoch;
final today = _dateKey(now);
// Resume the existing session only if it's the same item on the same day.
if (_active != null &&
_active!['progressKey'] == progressKey &&
- _active!['date'] == today) {
+ _active!['date'] == today &&
+ _active!['playMethod'] == playMethod) {
return;
}
// Any other lingering session — a different item, or the same item from a
@@ -199,6 +213,7 @@ class LocalSessionService {
// defaulting to the library item title (the show). Books send their title.
'displayTitle': displayTitle,
'displayAuthor': displayAuthor,
+ if (playMethod != null) 'playMethod': playMethod,
};
await _persistActive();
debugPrint('[LocalSession] Began session ${_active!['id']} for $progressKey ($today)');
@@ -214,6 +229,7 @@ class LocalSessionService {
ApiService? api,
}) async {
if (seconds <= 0) return;
+ if (DownloadService().isFromOtherAccount(progressKey)) return;
await _loadActive();
if (_active == null) {
debugPrint('[LocalSession] accrue with no active session — skipping');
@@ -256,6 +272,7 @@ class LocalSessionService {
'dayOfWeek': _dayOfWeek(effective),
'displayTitle': old['displayTitle'],
'displayAuthor': old['displayAuthor'],
+ if (old['playMethod'] != null) 'playMethod': old['playMethod'],
};
// Ship the just-closed day promptly when online.
if (api != null) await flushPending(api: api);
diff --git a/lib/services/media_overlay.dart b/lib/services/media_overlay.dart
index 281beacb..4c7069e0 100644
--- a/lib/services/media_overlay.dart
+++ b/lib/services/media_overlay.dart
@@ -3,6 +3,8 @@ import 'dart:math';
import 'package:archive/archive_io.dart';
+import '../utils/percent_escapes.dart';
+
/// One narrated sentence: where its text is and which stretch of which audio
/// file reads it.
class OverlayClip {
@@ -21,6 +23,10 @@ class OverlayClip {
required this.begin,
required this.end,
});
+
+ /// The sentence the way the SMIL file names it, document and id. BookOrbit's
+ /// read along keeps its place in this form and only matches it exactly.
+ String get textSrc => fragment == null ? textHref : '$textHref#$fragment';
}
/// An audio file inside the EPUB, placed on the book's timeline.
@@ -184,10 +190,35 @@ class MediaOverlay {
static bool sameDoc(String a, String b) {
if (a == b) return true;
- final x = a.split('#').first, y = b.split('#').first;
+ final x = _docPath(a), y = _docPath(b);
return x == y || x.endsWith('/$y') || y.endsWith('/$x');
}
+ // BookOrbit's timings can keep a name escaped (%2C) where the EPUB's own
+ // path has it plain, so both are compared with every escape undone.
+ static String _docPath(String href) {
+ final path = href.split('#').first;
+ return undoPathEscapes(path);
+ }
+
+ /// The sentence whose element is [fragment], in [section] when given (an
+ /// id can repeat across sections). The id can come bare or with its
+ /// document in front (`text/ch1.xhtml#s12`), the way BookOrbit keeps it.
+ /// Null when nothing narrated has it.
+ OverlayClip? clipByFragment(String fragment, int? section) {
+ final hash = fragment.indexOf('#');
+ final id = hash < 0 ? fragment : fragment.substring(hash + 1);
+ final doc = hash > 0 ? fragment.substring(0, hash) : null;
+ OverlayClip? any;
+ for (final c in clips) {
+ if (c.fragment != id) continue;
+ if (doc != null && !sameDoc(c.textHref, doc)) continue;
+ if (section == null || c.section == section) return c;
+ any ??= c;
+ }
+ return any;
+ }
+
/// The first sentence whose element is [fragment] in [href], or the first
/// one in [href] when the fragment isn't narrated.
OverlayClip? clipFor(String href, String? fragment) {
@@ -470,7 +501,7 @@ class MediaOverlay {
raw.add(_RawClip(
spineIndex,
_resolve(smilPath!, src),
- hash < 0 ? null : Uri.decodeComponent(src.substring(hash + 1)),
+ hash < 0 ? null : undoPercentEscapes(src.substring(hash + 1)),
_resolve(smilPath, audioSrc),
begin,
parseClock(au['clipend']),
@@ -577,7 +608,10 @@ String _dir(String path) {
/// [href] relative to [base]'s folder, as a path inside the zip.
String _resolve(String base, String href) {
- final clean = Uri.decodeFull(href.split('#').first.split('?').first);
+ // Every escape undone, segment by segment: Uri.decodeFull leaves reserved
+ // characters like %2C (a comma) encoded, and a file with one in its name
+ // was then looked for under a name the zip doesn't have.
+ final clean = undoPathEscapes(href.split('#').first.split('?').first);
final parts = <String>[];
for (final p in '${_dir(base)}$clean'.split('/')) {
if (p.isEmpty || p == '.') continue;
@@ -590,6 +624,8 @@ String _resolve(String base, String href) {
return parts.join('/');
}
+
+
String? _text(Archive archive, String name) {
final f = archive.findFile(name);
if (f == null) return null;
diff --git a/lib/services/narration_service.dart b/lib/services/narration_service.dart
index 67fe734c..2bf302a3 100644
--- a/lib/services/narration_service.dart
+++ b/lib/services/narration_service.dart
@@ -1,6 +1,8 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
+import 'dart:isolate';
+import 'dart:math';
import 'package:archive/archive_io.dart';
import 'package:flutter/foundation.dart';
@@ -14,8 +16,10 @@ import 'ebook_cache.dart';
import 'epub_book_text.dart';
import 'media_overlay.dart';
import 'narrated_books.dart';
+import 'progress_sync_service.dart';
import 'remote_zip.dart';
import 'scoped_prefs.dart';
+import '../utils/percent_escapes.dart';
Future<MediaOverlay?> _parseEpub(String path) async {
final input = InputFileStream(path);
@@ -29,6 +33,26 @@ Future<MediaOverlay?> _parseEpub(String path) async {
}
}
+/// A zip path with every escape undone, so a name stored escaped and one
+/// stored plain compare equal.
+String _plainPath(String path) => undoPathEscapes(path);
+
+ArchiveFile? _entryFor(Archive archive, String href) {
+ final exact = archive.findFile(href);
+ if (exact != null) return exact;
+ final want = _plainPath(href);
+ for (final f in archive.files) {
+ final name = _plainPath(f.name);
+ if (name == want || name.endsWith('/$want')) return f;
+ }
+ // Last, the file name alone, when only one file in the EPUB has it.
+ final base = want.split('/').last;
+ final named = archive.files.where((f) => _plainPath(f.name).split('/').last == base).toList();
+ return named.length == 1 ? named.single : null;
+}
+
+const _audioExts = {'.mp3', '.m4a', '.m4b', '.mp4', '.aac', '.ogg', '.opus', '.wav', '.flac'};
+
List<String> _extract(({String epub, String dir, List<String> hrefs, List<String> names}) job) {
final input = InputFileStream(job.epub);
try {
@@ -36,8 +60,15 @@ List<String> _extract(({String epub, String dir, List<String> hrefs, List<String
Directory(job.dir).createSync(recursive: true);
final out = <String>[];
for (var i = 0; i < job.hrefs.length; i++) {
- final entry = archive.findFile(job.hrefs[i]);
- if (entry == null) throw StateError('${job.hrefs[i]} is not in the EPUB');
+ final entry = _entryFor(archive, job.hrefs[i]);
+ if (entry == null) {
+ final audio = archive.files
+ .map((f) => f.name)
+ .where((n) => _audioExts.any(n.toLowerCase().endsWith))
+ .take(5)
+ .join(' | ');
+ throw StateError('${job.hrefs[i]} is not in the EPUB (its audio: $audio)');
+ }
final path = '${job.dir}/${job.names[i]}';
final file = File(path);
if (!(file.existsSync() && file.lengthSync() == entry.size)) {
@@ -64,11 +95,11 @@ void _copyWithout(({String from, String to, List<String> skip}) job) {
try {
File(job.to).parent.createSync(recursive: true);
final archive = ZipDecoder().decodeStream(input);
- final skip = job.skip.toSet();
+ final skip = job.skip.map(_plainPath).toSet();
final out = ZipFileEncoder()..create(part);
try {
for (final f in archive.files) {
- if (!skip.contains(f.name)) out.addArchiveFile(f);
+ if (!skip.contains(_plainPath(f.name))) out.addArchiveFile(f);
}
} finally {
out.closeSync();
@@ -325,13 +356,35 @@ class NarrationService {
/// without its audio, pulled from the server a piece at a time, so a
/// several hundred MB file opens in seconds. Anything else is the usual
/// cached download.
+ /// [wholeFallback] false keeps a copy without the audio from falling back
+ /// to the whole file, for a download riding along with an audiobook.
Future<File> readerFile(
ApiService api,
String itemId,
Map<String, dynamic> ebookFile,
- String title,
- ) async {
+ String title, {
+ bool wholeFallback = true,
+ }) async {
final cached = await ebookCacheFileFor(itemId, ebookFile);
+ if (cached.existsSync() && cached.lengthSync() > 0 && !await cachedEbookIsCurrent(cached, ebookFile)) {
+ // The server rewrote the book (BookOrbit writing metadata back, a file
+ // replaced). Fetch the new one, keeping the old copy if that fails.
+ final old = File('${cached.path}.old');
+ debugPrint('[EbookCache] $itemId changed on the server - getting the new copy');
+ await cached.rename(old.path);
+ try {
+ final fresh = await _freshReaderFile(api, itemId, ebookFile, title, cached, wholeFallback: wholeFallback);
+ await rememberEbookVersion(fresh, ebookFile);
+ await dropCachedLocations(fresh);
+ if (old.existsSync()) await old.delete();
+ return fresh;
+ } catch (e) {
+ debugPrint('[EbookCache] new copy of $itemId failed ($e) - keeping the old one');
+ if (cached.existsSync()) await cached.delete();
+ await old.rename(cached.path);
+ return cached;
+ }
+ }
if (cached.existsSync() && cached.lengthSync() > 0) {
if (_slimFailed.contains(itemId) || !await _cachedWithAudio(api, itemId, ebookFile, cached)) {
debugPrint('[EbookCache] cache hit item=$itemId bytes=${cached.lengthSync()}');
@@ -345,6 +398,7 @@ class NarrationService {
await cached.rename(aside.path);
try {
final slim = await _freshReaderFile(api, itemId, ebookFile, title, cached, wholeFallback: false);
+ await rememberEbookVersion(slim, ebookFile);
if (aside.existsSync()) await aside.delete();
return slim;
} catch (e) {
@@ -355,7 +409,9 @@ class NarrationService {
return cached;
}
}
- return _freshReaderFile(api, itemId, ebookFile, title, cached);
+ final fresh = await _freshReaderFile(api, itemId, ebookFile, title, cached, wholeFallback: wholeFallback);
+ await rememberEbookVersion(fresh, ebookFile);
+ return fresh;
}
/// A cached EPUB that still holds the narration: big, the book reads
@@ -589,6 +645,105 @@ class NarrationService {
return true;
}
+ /// The narration time of a reading spot [cfi] in a BookOrbit EPUB, found
+ /// in the copy the reader keeps on this phone. Null when there is none.
+ static Future<double?> readingSpotSeconds(String bookId, MediaOverlay overlay, String cfi) async {
+ final file = await ebookCacheFileFor(bookId, const {'ebookFormat': 'epub'});
+ if (!file.existsSync()) {
+ debugPrint('[Narration] $bookId: no copy of the book here to place $cfi');
+ return null;
+ }
+ final path = file.path;
+ final placed = await Isolate.run(() {
+ final book = EpubBookText.open(path);
+ if (book == null) return (seconds: null, how: 'the copy here is not an EPUB it can read');
+ try {
+ return placeCfiOnNarration(overlay, book, cfi);
+ } finally {
+ book.close();
+ }
+ });
+ debugPrint('[Narration] $bookId: $cfi ${placed.seconds == null ? 'not placed' : 'is ${placed.seconds!.toStringAsFixed(0)}s'}'
+ ' - ${placed.how}');
+ return placed.seconds;
+ }
+
+ static final Map<String, double?> _pagesPlaced = {};
+
+ /// How far past the words heard a page has to be to move the audio: a page
+ /// with them on it is no reason to jump.
+ static const _pageAheadOfAudio = 30.0;
+
+ /// The server stamps a save a moment after this phone stamps its own, so a
+ /// record only this much newer is still this phone's.
+ static const _ownSaveStamp = Duration(minutes: 1);
+
+ /// The narration time of the page [itemId] was read to on another device
+ /// (the Audiobookshelf web reader, another app), found in the copy the
+ /// reader keeps here. Audiobookshelf stamps the page and the audio with one
+ /// time, so the page counts only when the record was saved after this phone
+ /// last saved its own place, and only ahead of the audio. A page this phone
+ /// saved, or one behind the words heard, never moves it.
+ static Future<double?> pageReadElsewhere(String itemId, Map<String, dynamic> progress) async {
+ final cfi = progress['ebookLocation'];
+ if (progress['isFinished'] == true || cfi is! String || !cfi.startsWith('epubcfi(')) return null;
+ final sync = ProgressSyncService();
+ final saved = (progress['lastUpdate'] as num?)?.toInt() ?? 0;
+ final mine = await sync.getSavedTimestamp(itemId);
+ if (mine > 0 && saved <= mine + _ownSaveStamp.inMilliseconds) return null;
+ // A page read here that hasn't reached the server yet is newer than the
+ // one it holds.
+ if ((await sync.getPendingEbookProgress()).containsKey(itemId)) return null;
+ final heard = (progress['currentTime'] as num?)?.toDouble() ?? 0;
+ final key = '$itemId|$cfi';
+ final double? at;
+ if (_pagesPlaced.containsKey(key)) {
+ at = _pagesPlaced[key];
+ } else {
+ final overlay = await instance.localOverlay(itemId);
+ if (overlay == null) return null;
+ at = _pagesPlaced[key] = await readingSpotSeconds(itemId, overlay, cfi);
+ }
+ if (at == null || at < heard + _pageAheadOfAudio) return null;
+ debugPrint('[Narration] $itemId: read to a page ${at.toStringAsFixed(0)}s in on another device, '
+ 'past the ${heard.toStringAsFixed(0)}s heard - listening picks up there');
+ return at;
+ }
+
+ @visibleForTesting
+ static void debugPlacePage(String itemId, String cfi, double? at) => _pagesPlaced['$itemId|$cfi'] = at;
+
+ /// [o] laid on its audio files' real lengths, read over the network once
+ /// per book through [measure] (null for a file it can't read that way), so
+ /// a streamed book keeps the same timeline as a downloaded one. Kept, and
+ /// the book's length brought up to date.
+ Future<MediaOverlay> timedForStream(
+ String itemId,
+ MediaOverlay o,
+ Future<double?> Function(int track) measure,
+ ) async {
+ if (await ScopedPrefs.getBool(_timedKey(itemId)) == true) return o;
+ final started = DateTime.now();
+ final measured = List<double?>.filled(o.tracks.length, null);
+ for (var i = 0; i < o.tracks.length; i += 8) {
+ await Future.wait([
+ for (var j = i; j < min(i + 8, o.tracks.length); j++)
+ measure(j).then((d) => measured[j] = d).catchError((Object e) {
+ debugPrint('[Narration] $itemId file $j not measured: $e');
+ return null;
+ }),
+ ]);
+ }
+ final timed = o.withTrackDurations(measured);
+ debugPrint('[Narration] $itemId measured ${measured.where((m) => m != null).length} of '
+ '${measured.length} files over the network in ${DateTime.now().difference(started).inMilliseconds}ms: '
+ '${o.duration.toStringAsFixed(0)}s from the timings, ${timed.duration.toStringAsFixed(0)}s from the audio');
+ await keepTimings(itemId, timed);
+ if (measured.every((m) => m != null)) await ScopedPrefs.setBool(_timedKey(itemId), true);
+ if (NarratedBooks.contains(itemId)) await markAudiobook(itemId, timed);
+ return timed;
+ }
+
/// Keeps [o] as [itemId]'s narration on this phone (memory and disk).
Future<void> keepTimings(String itemId, MediaOverlay o) async {
_overlays[itemId] = o;
@@ -597,7 +752,7 @@ class NarrationService {
/// Plays the narration, from the sentence [from] when given (read along
/// starting at the page in view, "play from here"). An Audiobookshelf book
- /// plays it once downloaded: false until then.
+ /// that isn't downloaded streams it out of the EPUB on the server.
Future<bool> play(
ApiService api,
Map<String, dynamic> item, {
@@ -606,7 +761,6 @@ class NarrationService {
}) async {
final itemId = item['id'] as String?;
if (itemId == null) return false;
- if (!api.isBookOrbit && !hasLocalAudio(itemId)) return false;
final o = await localOverlay(itemId);
double? startTime;
if (from != null && o != null) {
diff --git a/lib/services/narration_stream.dart b/lib/services/narration_stream.dart
new file mode 100644
index 00000000..23dabd85
--- /dev/null
+++ b/lib/services/narration_stream.dart
@@ -0,0 +1,502 @@
+import 'dart:async';
+import 'dart:io';
+import 'dart:math';
+import 'dart:typed_data';
+
+import 'package:flutter/foundation.dart';
+import 'package:path_provider/path_provider.dart';
+import 'package:shared_preferences/shared_preferences.dart';
+
+import 'api_service.dart';
+import 'audio_file_duration.dart';
+import 'ebook_cache.dart';
+import 'media_overlay.dart';
+import 'narration_service.dart';
+import 'remote_zip.dart';
+import '../utils/percent_escapes.dart';
+
+/// What the player needs to stream a narration: one loopback URL per audio
+/// file, and the book laid out the way a download's is.
+typedef NarrationStreamSource = ({
+ List<String> urls,
+ double duration,
+ List<Map<String, dynamic>> chapters,
+ Map<String, dynamic> session,
+});
+
+class _Track {
+ final RemoteZipEntry entry;
+ final String mime;
+ int? dataOffset;
+
+ _Track(this.entry, this.mime);
+
+ int get size => entry.size;
+}
+
+class _Book {
+ final String itemId;
+ final Uri url;
+ final List<_Track> tracks;
+ final Directory dir;
+
+ _Book(this.itemId, this.url, this.tracks, this.dir);
+}
+
+/// Plays an Audiobookshelf narrated EPUB without downloading it first, the
+/// way the watch does. Audiobookshelf only sees an ebook, so there is no
+/// audio to stream; instead each audio file inside the EPUB is played as a
+/// byte range of the EPUB on the server. The player gets a 127.0.0.1 URL per
+/// file, and this moves each range it asks for to where that file sits in
+/// the EPUB, with a fresh token. A file the EPUB keeps compressed can't be
+/// read in pieces, so it is pulled out whole the first time it plays.
+///
+/// BookOrbit streams narration itself, but can't find a file whose name the
+/// EPUB writes with certain escapes (a comma as %2C); those books come
+/// through here too, from the EPUB BookOrbit serves.
+///
+/// The port and path secret are kept across launches like the BookOrbit
+/// proxy's; the secret keeps other apps on the device off the session.
+class NarrationStream {
+ NarrationStream._();
+ static final NarrationStream instance = NarrationStream._();
+
+ static const _portKey = 'narration_stream_port';
+ static const _secretKey = 'narration_stream_secret';
+
+ HttpServer? _server;
+ Future<void>? _starting;
+ int _port = 0;
+ String _secret = '';
+ ApiService? _api;
+ Map<String, String>? _headers;
+ DateTime _headersAt = DateTime(0);
+ HttpClient? _client;
+ final Map<String, _Book> _books = {};
+ // By book and file rather than on the book, so opening the book again
+ // while a file is still coming down waits on that pull instead of
+ // starting a second one into the same file.
+ final Map<String, Future<File>> _pulls = {};
+
+ String get _prefix => 'http://127.0.0.1:$_port/$_secret';
+
+ bool owns(String url) => _port > 0 && url.startsWith(_prefix);
+
+ /// Gets [itemId]'s narration ready to stream from the start of the track
+ /// that [startTime] falls in. Null when it can't: not an Audiobookshelf
+ /// narrated EPUB, or the server couldn't be read.
+ Future<NarrationStreamSource?> open(ApiService api, String itemId, {double startTime = 0}) async {
+ if (api.isBookOrbit || kIsWeb) return null;
+ final started = DateTime.now();
+ RemoteZip? zip;
+ try {
+ await ensureRunning();
+ _api = api;
+ _headers = null;
+ final item = await api.getLibraryItem(itemId);
+ final ino = resolveEbookFile(item)?['ino'] as String?;
+ if (item == null || ino == null) {
+ debugPrint('[NarrationStream] $itemId has no ebook file to stream from');
+ return null;
+ }
+ final file = Uri.parse(api.buildFileUrl(itemId, ino));
+ final url = Uri(scheme: file.scheme, userInfo: file.userInfo, host: file.host, port: file.port, path: file.path);
+ zip = await RemoteZip.open(url, headers: await _freshHeaders());
+ final narration = NarrationService.instance;
+ var overlay = await narration.localOverlay(itemId) ?? await narration.probe(api, item);
+ if (overlay == null || overlay.tracks.isEmpty) {
+ debugPrint('[NarrationStream] $itemId holds no narration');
+ return null;
+ }
+ final tracks = _tracksIn(zip, overlay.tracks);
+ final opened = zip;
+ overlay = await narration.timedForStream(itemId, overlay, (i) async {
+ final t = tracks[i];
+ if (!t.entry.stored) return null;
+ final at = t.dataOffset ??= await opened.dataOffset(t.entry);
+ return audioDuration((o, n) => opened.bytes(at + o, n), t.size);
+ });
+
+ final urls = await _serve(itemId, url, tracks, overlay.tracks, _trackAt(overlay.tracks, startTime));
+ final chapters = overlay.chapters();
+ debugPrint('[NarrationStream] $itemId ready in ${DateTime.now().difference(started).inMilliseconds}ms: '
+ '${tracks.length} file(s)${_packed(tracks)}, ${overlay.duration.toStringAsFixed(0)}s');
+ return (
+ urls: urls,
+ duration: overlay.duration,
+ chapters: chapters,
+ session: {
+ 'duration': overlay.duration,
+ 'chapters': chapters,
+ 'audioTracks': overlay.audioTracks((t) => t.href),
+ },
+ );
+ } catch (e) {
+ debugPrint('[NarrationStream] $itemId could not be streamed: $e');
+ return null;
+ } finally {
+ zip?.close();
+ }
+ }
+
+ /// Serves [tracks], the narration's audio files inside the EPUB at [epub],
+ /// from that EPUB's bytes, for a server that has the EPUB but can't hand
+ /// out a file inside it. One loopback URL per track, in order; null when
+ /// the EPUB can't be read in ranges or a file isn't in it.
+ Future<List<String>?> serveFromEpub(ApiService api, String itemId, Uri epub, List<OverlayTrack> tracks,
+ {double startTime = 0}) async {
+ if (kIsWeb || tracks.isEmpty) return null;
+ final started = DateTime.now();
+ RemoteZip? zip;
+ try {
+ await ensureRunning();
+ _api = api;
+ _headers = null;
+ zip = await RemoteZip.open(epub, headers: await _freshHeaders());
+ final found = _tracksIn(zip, tracks);
+ final urls = await _serve(itemId, epub, found, tracks, _trackAt(tracks, startTime));
+ debugPrint('[NarrationStream] $itemId ready from its EPUB in ${DateTime.now().difference(started).inMilliseconds}ms: '
+ '${found.length} file(s)${_packed(found)}');
+ return urls;
+ } catch (e) {
+ debugPrint('[NarrationStream] $itemId could not be streamed from its EPUB: $e');
+ return null;
+ } finally {
+ zip?.close();
+ }
+ }
+
+ List<_Track> _tracksIn(RemoteZip zip, List<OverlayTrack> tracks) => [
+ for (final t in tracks)
+ _Track(
+ _entryFor(zip, t.href) ?? (throw RemoteZipException('${t.href} is not in the EPUB')),
+ t.mediaType.isNotEmpty ? t.mediaType : _mimeFor(t.href),
+ ),
+ ];
+
+ static String _packed(List<_Track> tracks) {
+ final packed = tracks.where((t) => !t.entry.stored).length;
+ return packed > 0 ? ', $packed compressed' : '';
+ }
+
+ /// Makes [itemId] the book being served and hands back its URLs.
+ Future<List<String>> _serve(String itemId, Uri url, List<_Track> tracks, List<OverlayTrack> overlay, int first) async {
+ final base = await getTemporaryDirectory();
+ final root = Directory('${base.path}/narration_stream');
+ // Pulled files are only kept for the book that is streaming.
+ if (root.existsSync()) {
+ for (final d in root.listSync().whereType<Directory>()) {
+ if (d.path.split(Platform.pathSeparator).last != _safe(itemId)) {
+ try {
+ d.deleteSync(recursive: true);
+ } catch (_) {}
+ }
+ }
+ }
+ final dir = Directory('${root.path}/${_safe(itemId)}')..createSync(recursive: true);
+ final book = _Book(itemId, url, tracks, dir);
+ _books
+ ..clear()
+ ..[itemId] = book;
+ // A compressed file has to come down whole before it plays, so the one
+ // playback starts in comes down now rather than on the player's clock.
+ if (!tracks[first].entry.stored) await _pull(book, first);
+ return [
+ for (var i = 0; i < tracks.length; i++)
+ '$_prefix/${Uri.encodeComponent(itemId)}/$i/${Uri.encodeComponent(_fileName(overlay[i].href))}',
+ ];
+ }
+
+ static int _trackAt(List<OverlayTrack> tracks, double seconds) {
+ for (var i = tracks.length - 1; i > 0; i--) {
+ if (seconds >= tracks[i].start) return i;
+ }
+ return 0;
+ }
+
+ static RemoteZipEntry? _entryFor(RemoteZip zip, String href) {
+ final plain = undoPathEscapes(href);
+ return zip.entry(href) ??
+ zip.entry(plain) ??
+ zip.entries.where((e) => MediaOverlay.sameDoc(e.name, plain)).firstOrNull;
+ }
+
+ static String _fileName(String href) => undoPercentEscapes(href.split('/').last);
+
+ static String _safe(String s) => s.replaceAll(RegExp(r'[^A-Za-z0-9._-]'), '_');
+
+ static String _mimeFor(String href) {
+ final name = href.toLowerCase();
+ if (name.endsWith('.mp3')) return 'audio/mpeg';
+ if (name.endsWith('.ogg') || name.endsWith('.oga') || name.endsWith('.opus')) return 'audio/ogg';
+ if (name.endsWith('.wav')) return 'audio/wav';
+ if (name.endsWith('.flac')) return 'audio/flac';
+ return 'audio/mp4';
+ }
+
+ Future<Map<String, String>> _freshHeaders({bool renew = false}) async {
+ final api = _api;
+ if (api == null) return const {};
+ final cached = _headers;
+ if (!renew && cached != null && DateTime.now().difference(_headersAt) < const Duration(minutes: 5)) {
+ return cached;
+ }
+ final headers = await api.renewedMediaHeaders();
+ _headers = headers;
+ _headersAt = DateTime.now();
+ return headers;
+ }
+
+ HttpClient get _http => _client ??= HttpClient()
+ ..autoUncompress = false
+ ..connectionTimeout = const Duration(seconds: 15)
+ ..idleTimeout = const Duration(seconds: 30);
+
+ /// A GET of bytes [start] to [end] of the EPUB, asked again with a renewed
+ /// token when the server turns the first one away.
+ Future<(HttpClientRequest, HttpClientResponse)> _upstream(Uri url, int start, int end) async {
+ Future<(HttpClientRequest, HttpClientResponse)> send(Map<String, String> headers) async {
+ final req = await _http.getUrl(url);
+ req.followRedirects = true;
+ headers.forEach(req.headers.set);
+ req.headers.set(HttpHeaders.acceptEncodingHeader, 'identity');
+ req.headers.set(HttpHeaders.rangeHeader, 'bytes=$start-$end');
+ return (req, await req.close());
+ }
+
+ var (req, res) = await send(await _freshHeaders());
+ if (res.statusCode == HttpStatus.unauthorized) {
+ await res.drain<void>().catchError((_) {});
+ (req, res) = await send(await _freshHeaders(renew: true));
+ }
+ if (res.statusCode != HttpStatus.partialContent) {
+ req.abort();
+ throw RemoteZipException('server answered ${res.statusCode} to a range request');
+ }
+ return (req, res);
+ }
+
+ Future<Uint8List> _bytes(Uri url, int start, int count) async {
+ final (_, res) = await _upstream(url, start, start + count - 1);
+ final out = BytesBuilder(copy: false);
+ await for (final chunk in res) {
+ out.add(chunk);
+ }
+ return out.takeBytes();
+ }
+
+ /// Where a file's data starts, read from its local header the first time.
+ Future<int> _dataOffset(_Book book, _Track t) async {
+ final known = t.dataOffset;
+ if (known != null) return known;
+ final h = ByteData.sublistView(await _bytes(book.url, t.entry.headerOffset, 30));
+ if (h.getUint32(0, Endian.little) != 0x04034b50) {
+ throw RemoteZipException('bad local header for ${t.entry.name}');
+ }
+ return t.dataOffset = t.entry.headerOffset + 30 + h.getUint16(26, Endian.little) + h.getUint16(28, Endian.little);
+ }
+
+ /// A compressed file pulled out of the EPUB whole, once; later asks share
+ /// the first pull.
+ Future<File> _pull(_Book book, int index) {
+ final key = '${book.itemId}/$index';
+ final running = _pulls[key];
+ if (running != null) return running;
+ final pull = () async {
+ final t = book.tracks[index];
+ final out = File('${book.dir.path}/track-$index');
+ if (out.existsSync() && out.lengthSync() == t.size) return out;
+ final started = DateTime.now();
+ final at = await _dataOffset(book, t);
+ final (_, res) = await _upstream(book.url, at, at + t.entry.compressedSize - 1);
+ final part = File('${out.path}.part');
+ final sink = part.openWrite();
+ try {
+ Stream<List<int>> body = res.timeout(const Duration(seconds: 60));
+ if (t.entry.method == 8) body = body.transform(ZLibDecoder(raw: true));
+ await sink.addStream(body);
+ } finally {
+ await sink.close();
+ }
+ final got = part.lengthSync();
+ if (got != t.size) {
+ part.deleteSync();
+ throw RemoteZipException('${t.entry.name} came out $got bytes, not ${t.size}');
+ }
+ await part.rename(out.path);
+ debugPrint('[NarrationStream] pulled ${t.entry.name} (${t.size} bytes) in '
+ '${DateTime.now().difference(started).inMilliseconds}ms');
+ return out;
+ }();
+ _pulls[key] = pull;
+ pull.then<void>((_) {}, onError: (Object _) => _pulls.remove(key));
+ return pull;
+ }
+
+ /// Load the saved port and secret and start listening. Safe to call again;
+ /// it rebinds when the platform took the socket away while suspended.
+ Future<void> ensureRunning() => _starting ??= _ensure().whenComplete(() => _starting = null);
+
+ Future<void> _ensure() async {
+ final server = _server;
+ if (server != null) {
+ if (await _portAnswers(_port, _secret)) return;
+ debugPrint('[NarrationStream] Not answering on $_port, rebinding');
+ _server = null;
+ try {
+ await server.close(force: true);
+ } catch (_) {}
+ }
+ final prefs = await SharedPreferences.getInstance();
+ var secret = prefs.getString(_secretKey);
+ if (secret == null || secret.length < 16) {
+ final rng = Random.secure();
+ secret = List.generate(24, (_) => rng.nextInt(256)).map((b) => b.toRadixString(16).padLeft(2, '0')).join();
+ await prefs.setString(_secretKey, secret);
+ }
+ _secret = secret;
+ final savedPort = prefs.getInt(_portKey) ?? 0;
+ HttpServer? bound;
+ if (savedPort > 0) {
+ try {
+ bound = await HttpServer.bind(InternetAddress.loopbackIPv4, savedPort);
+ } on SocketException catch (e) {
+ debugPrint('[NarrationStream] Port $savedPort busy ($e), picking another');
+ }
+ }
+ bound ??= await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
+ if (bound.port != savedPort) await prefs.setInt(_portKey, bound.port);
+ _port = bound.port;
+ _server = bound;
+ bound.listen(
+ _handle,
+ onError: (Object e) => debugPrint('[NarrationStream] Server error: $e'),
+ onDone: () {
+ if (identical(_server, bound)) _server = null;
+ },
+ );
+ debugPrint('[NarrationStream] Listening on 127.0.0.1:$_port');
+ }
+
+ Future<bool> _portAnswers(int port, String secret) async {
+ final client = HttpClient()..connectionTimeout = const Duration(seconds: 1);
+ try {
+ final req = await client.get('127.0.0.1', port, '/$secret/__ping');
+ final res = await req.close().timeout(const Duration(seconds: 1));
+ await res.drain<void>();
+ return res.statusCode == HttpStatus.noContent;
+ } catch (_) {
+ return false;
+ } finally {
+ client.close(force: true);
+ }
+ }
+
+ @visibleForTesting
+ Future<void> stop() async {
+ final server = _server;
+ _server = null;
+ _books.clear();
+ _pulls.clear();
+ await server?.close(force: true);
+ }
+
+ /// `bytes=a-b`, `bytes=a-` or `bytes=-n` as an inclusive span of a file
+ /// [size] long. Null for no range; (-1, -1) for one that can't be met.
+ static (int, int)? parseRange(String? header, int size) {
+ if (header == null) return null;
+ final m = RegExp(r'^bytes=(\d*)-(\d*)$').firstMatch(header.trim());
+ if (m == null) return null;
+ final a = m.group(1)!, b = m.group(2)!;
+ if (a.isEmpty && b.isEmpty) return null;
+ if (a.isEmpty) {
+ final n = int.parse(b);
+ if (n <= 0) return (-1, -1);
+ return (max(0, size - n), size - 1);
+ }
+ final start = int.parse(a);
+ final end = b.isEmpty ? size - 1 : min(int.parse(b), size - 1);
+ if (start >= size || end < start) return (-1, -1);
+ return (start, end);
+ }
+
+ Future<void> _handle(HttpRequest req) async {
+ final res = req.response;
+ final parts = req.uri.pathSegments;
+ if (parts.isEmpty || parts.first != _secret) {
+ res.statusCode = HttpStatus.notFound;
+ await res.close();
+ return;
+ }
+ if (parts.length == 2 && parts[1] == '__ping') {
+ res.statusCode = HttpStatus.noContent;
+ await res.close();
+ return;
+ }
+ final book = parts.length >= 3 ? _books[parts[1]] : null;
+ final index = parts.length >= 3 ? int.tryParse(parts[2]) : null;
+ if (book == null || index == null || index < 0 || index >= book.tracks.length) {
+ res.statusCode = HttpStatus.notFound;
+ await res.close();
+ return;
+ }
+ if (req.method != 'GET' && req.method != 'HEAD') {
+ res.statusCode = HttpStatus.methodNotAllowed;
+ await res.close();
+ return;
+ }
+ final t = book.tracks[index];
+ final range = parseRange(req.headers.value(HttpHeaders.rangeHeader), t.size);
+ if (range != null && range.$1 < 0) {
+ res.statusCode = HttpStatus.requestedRangeNotSatisfiable;
+ res.headers.set(HttpHeaders.contentRangeHeader, 'bytes */${t.size}');
+ await res.close();
+ return;
+ }
+ final start = range?.$1 ?? 0;
+ final end = range?.$2 ?? t.size - 1;
+
+ HttpClientRequest? upstreamReq;
+ try {
+ // Opened before the reply starts, so a failure can still say so.
+ Stream<List<int>> body;
+ if (t.entry.stored) {
+ final at = await _dataOffset(book, t);
+ final (r, upstreamRes) = await _upstream(book.url, at + start, at + end);
+ upstreamReq = r;
+ body = upstreamRes;
+ } else {
+ body = (await _pull(book, index)).openRead(start, end + 1);
+ }
+ res.statusCode = range == null ? HttpStatus.ok : HttpStatus.partialContent;
+ res.headers.set(HttpHeaders.contentTypeHeader, t.mime);
+ res.headers.set(HttpHeaders.acceptRangesHeader, 'bytes');
+ res.headers.contentLength = end - start + 1;
+ if (range != null) res.headers.set(HttpHeaders.contentRangeHeader, 'bytes $start-$end/${t.size}');
+ if (req.method == 'HEAD') {
+ upstreamReq?.abort();
+ await res.close();
+ return;
+ }
+ // The next file is likely to be asked for soon; a compressed one gets a
+ // head start on its pull.
+ if (index + 1 < book.tracks.length && !book.tracks[index + 1].entry.stored) {
+ unawaited(_pull(book, index + 1).then<void>((_) {}, onError: (Object _) {}));
+ }
+ try {
+ await res.addStream(body);
+ await res.close();
+ } catch (_) {
+ // The player hung up (seek, skip, stop). Drop the upstream body too.
+ upstreamReq?.abort();
+ }
+ } catch (e) {
+ debugPrint('[NarrationStream] ${book.itemId} file $index failed: $e');
+ upstreamReq?.abort();
+ try {
+ res.statusCode = HttpStatus.badGateway;
+ await res.close();
+ } catch (_) {}
+ }
+ }
+}
diff --git a/lib/services/offline_source.dart b/lib/services/offline_source.dart
index 84306c47..aa040638 100644
--- a/lib/services/offline_source.dart
+++ b/lib/services/offline_source.dart
@@ -22,7 +22,8 @@ ProgressiveAudioSourceOptions? mp3ExtractorOptions() =>
/// Build the right AudioSource for a downloaded track. Content URIs play via
/// AudioSource.uri (media3's ContentDataSource opens them through the persisted
-/// SAF permission); filesystem paths play via AudioSource.file.
-AudioSource localAudioSource(String pathOrUri) => isContentUri(pathOrUri)
+/// SAF permission); filesystem paths play via AudioSource.file. A narration
+/// streamed out of its EPUB comes as a loopback http URL.
+AudioSource localAudioSource(String pathOrUri) => isContentUri(pathOrUri) || pathOrUri.startsWith('http://')
? AudioSource.uri(Uri.parse(pathOrUri), options: mp3ExtractorOptions())
: AudioSource.file(pathOrUri, options: mp3ExtractorOptions());
diff --git a/lib/services/progress_sync_service.dart b/lib/services/progress_sync_service.dart
index b6f41b35..0ecfe219 100644
--- a/lib/services/progress_sync_service.dart
+++ b/lib/services/progress_sync_service.dart
@@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'api_service.dart';
+import 'download_service.dart';
import 'scoped_prefs.dart';
import 'sync_logic.dart';
@@ -69,6 +70,7 @@ class ProgressSyncService {
'timestamp': DateTime.now().millisecondsSinceEpoch,
};
await ScopedPrefs.setString('progress_$itemId', jsonEncode(data));
+ if (_staysOnPhone(itemId)) return;
final pendingList = await ScopedPrefs.getStringList('pending_syncs');
if (!pendingList.contains(itemId)) {
@@ -187,6 +189,10 @@ class ProgressSyncService {
return localDuration;
}
+ /// Another account's download keeps its place on this phone only: the
+ /// server signed in now can have a different book under the same id.
+ bool _staysOnPhone(String itemId) => DownloadService().isFromOtherAccount(itemId);
+
/// Sync a single item to the server. Returns true if synced.
Future<bool> syncToServer({
required ApiService api,
@@ -197,6 +203,7 @@ class ProgressSyncService {
debugPrint('[Sync] Skipped — offline');
return false;
}
+ if (_staysOnPhone(itemId)) return true;
final data = await getLocal(itemId);
if (data == null) {
@@ -276,7 +283,7 @@ class ProgressSyncService {
for (final itemId in batch) {
final data = await getLocal(itemId);
- if (data == null) {
+ if (data == null || _staysOnPhone(itemId)) {
final updated = await ScopedPrefs.getStringList('pending_syncs');
updated.remove(itemId);
await ScopedPrefs.setStringList('pending_syncs', updated);
diff --git a/lib/services/up_next_narration.dart b/lib/services/up_next_narration.dart
index 364c55da..0c2731fd 100644
--- a/lib/services/up_next_narration.dart
+++ b/lib/services/up_next_narration.dart
@@ -14,11 +14,12 @@ import 'audio_player_service.dart';
import 'download_service.dart';
import 'narrated_books.dart';
-/// An Audiobookshelf EPUB that reads itself aloud plays only once its
-/// narration is downloaded, so a queue must never reach one unready. Starting
-/// a book from the app when it is three quarters done offers the download of
-/// an EPUB that is next in the queue; a queue that gets there anyway stops
-/// (never skipping it) and says why.
+/// An Audiobookshelf EPUB that reads itself aloud streams from the server,
+/// so offline it plays only once its narration is downloaded, and a queue
+/// must never reach one unready. Starting a book from the app offline when it
+/// is three quarters done offers the download of an EPUB that is next in the
+/// queue; a queue that gets there anyway stops (never skipping it) and says
+/// why.
class UpNextNarration {
UpNextNarration._();
static final UpNextNarration instance = UpNextNarration._();
@@ -28,9 +29,10 @@ class UpNextNarration {
// Asked once a run for each up-next book, so "Not now" sticks.
final Set<String> _asked = {};
- /// Whether [key] is a narrated EPUB on Audiobookshelf that can't play yet.
- bool needsDownload(String key) {
- if (key.length > 36 || !NarratedBooks.contains(key)) return false;
+ /// Whether [key] is a narrated EPUB on Audiobookshelf that can't play yet:
+ /// [offline], so it can't stream, and not on the phone.
+ bool needsDownload(String key, {required bool offline}) {
+ if (!offline || key.length > 36 || !NarratedBooks.contains(key)) return false;
final dl = DownloadService();
return !dl.isDownloaded(key) && !dl.isDownloading(key);
}
@@ -45,7 +47,7 @@ class UpNextNarration {
final done = player.position.inMilliseconds / 1000.0 / total;
if (done < _nearTheEnd) return;
final next = await lib.upNextEntry(currentItemId: itemId);
- if (next == null || !needsDownload(next.key) || !_asked.add(next.key)) return;
+ if (next == null || !needsDownload(next.key, offline: lib.isOffline) || !_asked.add(next.key)) return;
final ctx = rootNavigatorKey.currentContext;
if (ctx == null || !ctx.mounted) return;
final api = ctx.read<AuthProvider>().apiService;
diff --git a/lib/utils/percent_escapes.dart b/lib/utils/percent_escapes.dart
new file mode 100644
index 00000000..9144e75a
--- /dev/null
+++ b/lib/utils/percent_escapes.dart
@@ -0,0 +1,27 @@
+import 'dart:convert';
+
+/// [text] with its `%XX` escapes turned back into the characters they stand
+/// for and everything else left as it is. Uri.decodeComponent throws on any
+/// character outside ASCII and Uri.decodeFull leaves reserved ones like %2C
+/// escaped, so a name like `Bob)꞉ Bobiverse%2C Book 1` came back unchanged.
+String undoPercentEscapes(String text) {
+ if (!text.contains('%')) return text;
+ bool hex(int c) => (c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x46) || (c >= 0x61 && c <= 0x66);
+ final bytes = utf8.encode(text);
+ final out = <int>[];
+ for (var i = 0; i < bytes.length; i++) {
+ final b = bytes[i];
+ if (b == 0x25 && i + 2 < bytes.length && hex(bytes[i + 1]) && hex(bytes[i + 2])) {
+ out.add(int.parse(String.fromCharCodes([bytes[i + 1], bytes[i + 2]]), radix: 16));
+ i += 2;
+ } else {
+ out.add(b);
+ }
+ }
+ return utf8.decode(out, allowMalformed: true);
+}
+
+/// A path with each segment's escapes undone, so an escaped slash can't
+/// split a name into folders.
+String undoPathEscapes(String path) =>
+ path.contains('%') ? path.split('/').map(undoPercentEscapes).join('/') : path;
diff --git a/lib/widgets/absorbing_shared.dart b/lib/widgets/absorbing_shared.dart
index 27ba240f..fb27b14b 100644
--- a/lib/widgets/absorbing_shared.dart
+++ b/lib/widgets/absorbing_shared.dart
@@ -294,14 +294,23 @@ class _DownloadWideButtonState extends State<DownloadWideButton> {
if (api == null) return;
final l = AppLocalizations.of(context)!;
if (_dl.isDownloaded(widget.itemId)) {
+ final onPhone = await _dl.onPhone(widget.itemId);
+ if (!context.mounted) return;
+ if (onPhone.audio && onPhone.ebookBytes != null && !onPhone.fromEbook) {
+ await _pickWhatToRemove(context, onPhone);
+ return;
+ }
showDialog(context: context, builder: (ctx) => AlertDialog(
title: Text(l.removeDownloadQuestion),
content: Text(l.removeDownloadContent),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx), child: Text(l.cancel)),
- TextButton(onPressed: () {
- _dl.deleteDownload(widget.itemId, byUser: true);
+ TextButton(onPressed: () async {
Navigator.pop(ctx);
+ // A narrated EPUB's text and the audio taken out of it go together.
+ if (onPhone.fromEbook) await _dl.deleteEbookCopy(widget.itemId);
+ await _dl.deleteDownload(widget.itemId, byUser: true);
+ if (!context.mounted) return;
showOverlayToast(context, l.downloadRemoved, icon: Icons.delete_outline_rounded);
},
child: Text(l.remove, style: const TextStyle(color: Colors.redAccent))),
@@ -316,6 +325,66 @@ class _DownloadWideButtonState extends State<DownloadWideButton> {
}
}
}
+
+ /// Both the audiobook and its ebook are on the phone: remove either or
+ /// both, everything picked to start with.
+ Future<void> _pickWhatToRemove(
+ BuildContext context, ({bool audio, int? audioBytes, int? ebookBytes, bool fromEbook}) onPhone) async {
+ final l = AppLocalizations.of(context)!;
+ String size(int? bytes) {
+ if (bytes == null) return '';
+ if (bytes < 1048576) return '${(bytes / 1024).ceil()} KB';
+ if (bytes < 1073741824) return '${(bytes / 1048576).toStringAsFixed(1)} MB';
+ return '${(bytes / 1073741824).toStringAsFixed(2)} GB';
+ }
+
+ var audio = true, ebook = true;
+ final remove = await showDialog<bool>(
+ context: context,
+ builder: (ctx) => StatefulBuilder(
+ builder: (ctx, setDialog) => AlertDialog(
+ title: Text(l.removeDownloadQuestion),
+ content: Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [
+ Text(l.removeWhichDownload),
+ const SizedBox(height: 8),
+ CheckboxListTile(
+ contentPadding: EdgeInsets.zero,
+ value: audio,
+ onChanged: (v) => setDialog(() => audio = v ?? false),
+ title: Text(l.fileInfoAudio),
+ subtitle: onPhone.audioBytes == null ? null : Text(size(onPhone.audioBytes)),
+ ),
+ CheckboxListTile(
+ contentPadding: EdgeInsets.zero,
+ value: ebook,
+ onChanged: (v) => setDialog(() => ebook = v ?? false),
+ title: Text(l.fileInfoEbook),
+ subtitle: Text(size(onPhone.ebookBytes)),
+ ),
+ ]),
+ actions: [
+ TextButton(onPressed: () => Navigator.pop(ctx, false), child: Text(l.cancel)),
+ TextButton(
+ onPressed: audio || ebook ? () => Navigator.pop(ctx, true) : null,
+ child: Text(l.remove, style: TextStyle(color: audio || ebook ? Colors.redAccent : null)),
+ ),
+ ],
+ ),
+ ),
+ );
+ if (remove != true || !context.mounted) return;
+ if (audio && ebook) {
+ await _dl.deleteEbookCopy(widget.itemId);
+ await _dl.deleteDownload(widget.itemId, byUser: true);
+ } else if (audio) {
+ await _dl.deleteAudioKeepEbook(widget.itemId);
+ } else {
+ await _dl.deleteEbookCopy(widget.itemId);
+ }
+ if (!context.mounted) return;
+ showOverlayToast(context, audio ? l.downloadRemoved : l.ebookRemovedOffline,
+ icon: Icons.delete_outline_rounded);
+ }
}
// ─── ABSORBING WAVE ANIMATION ────────────────────────────────