Files
plezy/lib/services/jellyfin_sequential_launcher.dart
edde746 05fd622968 feat(emby): add Emby as a MediaBrowser backend alongside Jellyfin
Emby is Jellyfin's upstream ancestor and speaks a near-identical MediaBrowser
API, so the existing Jellyfin stack is parameterised by a `MediaBrowserDialect`
rather than forked. `JellyfinClient`, its auth service, endpoint discovery, LAN
discovery, and the add/edit connection screens all take the dialect and keep one
implementation; `MediaBackend.emby` and `ConnectionKind.emby` carry it through
the neutral models, the Drift `kind` discriminator, downloads, and caches.

Every divergence below was measured against a live Emby 4.9.5 server, not
inferred from documentation, and each is documented at its capability getter.
Jellyfin's request strings stay byte-identical so nothing about its behaviour
changes.

Routes and auth
- Emby only accepts the pre-10.9 user-scoped item routes (`/Users/{id}/Items/…`,
  `/Users/{id}/PlayedItems/…`, `/Users/{id}/FavoriteItems/…`); the unprefixed
  forms Jellyfin 10.11 added return 404.
- The API is also served under a legacy `/emby` prefix, and both dialects accept
  the token as `X-Emby-Token` or `api_key=`.
- Emby answers only its own LAN discovery datagram ("who is EmbyServer?") and
  ignores Jellyfin's; its default HTTPS port is 8920.
- No `/QuickConnect` route exists, so Quick Connect stays Jellyfin-only.

Row fields Emby withholds
- `ProductionYear`, `OfficialRating`, `PremiereDate` and `DateCreated` are absent
  from list rows unless named in `Fields`, which would otherwise strip the year
  and age-rating badge from every card in the app.
- `UserData.LastPlayedDate` never appears on a list row under `Fields=UserData`,
  `EnableUserData=true` or the user-scoped `Ids=` form — only on the single-item
  detail route, or when the Emby-specific `UserDataLastPlayedDate` token is
  requested. Without it every recency-ordered surface silently degrades to
  library-add time, and `JellyfinApiCache.applyWatchState` stamps
  `DateTime.now()` on watched rows, so an offline watch-state pull would rewrite
  the cached play time of everything it walked.

Continue Watching and Next Up
- Emby computes Next Up per series only: the library-wide `/Shows/NextUp` query
  returns nothing under every parameter combination tried. The shelf is
  therefore reconstructed from a played-episode recency scan plus one
  `/Shows/NextUp?SeriesId=` per distinct series, bounded by a shared wall clock
  that covers the scan as well — per-request timeouts cannot bound the pass
  because `MediaServerHttpClient` times the connect and receive phases
  independently. Rows are stamped with their series' newest play from the same
  response that ordered them, so no per-series enrichment request is needed.
- `/Shows/NextUp` ignores `NextUpDateCutoff`, and no server-side played-date
  filter exists to delegate to (`MinDatePlayed` and `MinDateLastPlayed` are
  ignored; `MinDateLastSaved`, `MinDateCreated` and `MinPremiereDate` filter
  unrelated dates), so the 365-day window is applied to the scanned dates.
- The resume route returns items with no saved position, including plain next
  episodes, so the Emby resume leg reads from `/Items?Filters=IsResumable`.
- Emby is ahead of Jellyfin in one place: `/Users/{id}/Items/{id}/HideFromResume`
  makes Continue Watching removal a real capability.

Everything else
- `/Sessions/Playing` and `/Sessions/Playing/Progress` reject a body with no
  `PlaySessionId` (HTTP 400), so playback reporting always sends one.
- Passing any `MediaTypes` value to the playlist query returns an empty list.
- There is no aggregate `/Items/Filters` route; the four filter facets are
  reassembled from `/Genres`, `/OfficialRatings`, `/Studios` and `/Tags`.
- Metadata writes take name-pair lists (`Genres: [{'Name': 'Action'}]`); the
  plain string array is accepted and then silently discarded.
- Custom artwork uploads must be base64 text, not raw bytes — which was broken
  for Jellyfin too and is fixed for both.
- Trickplay, media segments and lyrics 404 on Emby, so scrub previews are absent
  and intro/credit markers fall back to chapter names.

Verified against a local Emby 4.9.5 and a Jellyfin 10.11.11 control server:
onboarding, browse, detail, playable stream URLs serving real bytes, subtitle
sidecars, watch-state write and restore, hubs, cross-server aggregation and
search across both backends simultaneously.
2026-08-05 06:09:26 +02:00

259 lines
9.6 KiB
Dart

import 'dart:math';
import '../media/ids.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../i18n/strings.g.dart';
import '../media/media_item.dart';
import '../media/media_kind.dart';
import '../media/media_server_client.dart';
import '../media/play_queue.dart';
import '../providers/multi_server_provider.dart';
import '../providers/playback_state_provider.dart';
import '../utils/snackbar_helper.dart';
import '../utils/media_server_http_client.dart';
import 'jellyfin_client.dart';
import 'media_list_playback_launcher.dart';
import 'playlist_items_loader.dart';
/// Backend-neutral launcher for MediaBrowser collections, playlists, and
/// folders.
///
/// Jellyfin and Emby have no server-side queue resource — the client fetches
/// children (collection) or playlist items, applies shuffle locally, and hands
/// the flat list to [PlaybackStateProvider] via
/// [PlaybackStateProvider.setPlaybackFromLocalQueue], which the player already
/// consumes (mirrors the path [EpisodeNavigationService] uses for episode
/// windows).
///
/// The persisted `jellyfin:` queue-id prefix predates Emby support and covers
/// both dialects. It must remain stable so existing saved queues keep working.
class JellyfinSequentialLauncher extends MediaListPlaybackLauncher {
final BuildContext context;
/// Hook for tests — bypasses [Provider.of] so callers can inject a
/// fake [MediaServerClient]. Production callers leave this null and
/// the launcher resolves the client through [MultiServerProvider].
final MediaServerClient? clientForTesting;
/// Hook for tests — bypasses [Provider.of] so callers can inject a
/// fake [PlaybackStateProvider]. Production callers leave this null.
final PlaybackStateProvider? playbackStateForTesting;
/// Hook for tests — replaces the real player navigation so the unit
/// test doesn't need a Navigator/route stack.
final Future<void> Function(MediaItem item)? navigateForTesting;
JellyfinSequentialLauncher({
required this.context,
this.clientForTesting,
this.playbackStateForTesting,
this.navigateForTesting,
});
@override
Future<PlayQueueResult> launchFromCollectionOrPlaylist({
required Object item,
required bool shuffle,
MediaItem? startItem,
bool showLoadingIndicator = true,
}) async {
final facts = MediaListPlaybackLauncher.classifyItem(item);
if (facts == null) {
return PlayQueueError(Exception('Item must be a collection or playlist'));
}
final serverId = facts.serverId;
if (serverId == null) {
return PlayQueueError(Exception('Item is missing serverId'));
}
return _launchLocalQueue(
serverId: serverId,
queueId: 'jellyfin:${facts.id}',
contextKey: facts.id,
shuffle: shuffle,
showLoadingIndicator: showLoadingIndicator,
fetchItems: (client, abort) async {
// Playlists go through the dedicated `/Playlists/{id}/Items` endpoint
// so playlist-defined order is preserved; collections fall back to
// recursive descendant expansion (which skips unplayable Series
// containers and surfaces Movies + Episodes flat).
final List<MediaItem> items;
if (facts.isPlaylist) {
items = await fetchAllPlaylistItems(client, facts.id, abort: abort);
} else if (client is JellyfinClient) {
items = await client.fetchPlayableDescendants(facts.id, abort: abort);
} else {
// Test/back-end compatibility: the neutral interface intentionally
// does not bind unrelated complete-list callers to launch lifetime.
items = await client.fetchPlayableDescendants(facts.id);
}
abort.throwIfAborted();
return items;
},
// When a startItem is given (and we're not shuffling), keep the full
// original order and move the local queue cursor to that item.
resolveStartIndex: (items) {
final start = startItem;
if (shuffle || start == null) return 0;
final index = items.indexWhere((it) => it.id == start.id);
return index < 0 ? 0 : index;
},
);
}
/// Launch playback from a MediaBrowser folder row. Neither dialect has a
/// server-side queue resource, so folders use the same local queue path as
/// collections. The client query is video-only; music-only folders return
/// [PlayQueueEmpty].
@override
Future<PlayQueueResult> launchFromFolder({
required MediaItem folder,
required bool shuffle,
bool showLoadingIndicator = true,
}) async {
final serverId = folder.serverId;
if (serverId == null) {
return PlayQueueError(Exception('Item is missing serverId'));
}
return _launchLocalQueue(
serverId: serverId,
queueId: 'jellyfin:folder:${folder.id}',
contextKey: folder.id,
shuffle: shuffle,
showLoadingIndicator: showLoadingIndicator,
fetchItems: (client, abort) async {
final fetched = client is JellyfinClient
? await client.fetchPlayableFolderDescendants(folder.id, abort: abort)
: await client.fetchPlayableDescendants(folder.id);
abort.throwIfAborted();
return fetched.where((item) => item.kind.isVideo).map((item) {
return item.copyWith(
serverId: item.serverId ?? serverId,
serverName: item.serverName ?? folder.serverName,
libraryId: item.libraryId ?? folder.libraryId,
libraryTitle: item.libraryTitle ?? folder.libraryTitle,
);
}).toList();
},
);
}
@override
Future<PlayQueueResult> launchShuffledShow({required MediaItem metadata, bool showLoadingIndicator = true}) async {
final kind = metadata.kind;
if (kind != MediaKind.show && kind != MediaKind.season) {
return PlayQueueError(Exception('Shuffle play only works for shows and seasons'));
}
final serverId = metadata.serverId;
if (serverId == null) {
return PlayQueueError(Exception('Item is missing serverId'));
}
final String seriesId;
if (kind == MediaKind.show) {
seriesId = metadata.id;
} else {
final parent = metadata.parentId;
if (parent == null) {
return PlayQueueError(Exception('Season is missing parentId'));
}
seriesId = parent;
}
return _launchLocalQueue(
serverId: serverId,
queueId: 'jellyfin:$seriesId',
contextKey: seriesId,
shuffle: true,
showLoadingIndicator: showLoadingIndicator,
fetchItems: (client, abort) async {
final raw = client is JellyfinClient
? await client.fetchClientSideEpisodeQueue(seriesId, abort: abort)
: await client.fetchClientSideEpisodeQueue(seriesId);
abort.throwIfAborted();
if (raw == null) return const <MediaItem>[];
return raw.map((e) => e.copyWith(serverId: serverId, serverName: metadata.serverName ?? e.serverName)).toList();
},
);
}
/// Fetch, shuffle, and publish a local queue behind the cancellable loading
/// dialog. [fetchItems] carries the only per-entry-point difference: which
/// client call produces the items and how they're normalized.
Future<PlayQueueResult> _launchLocalQueue({
required String serverId,
required String queueId,
required String contextKey,
required bool shuffle,
required bool showLoadingIndicator,
required Future<List<MediaItem>> Function(MediaServerClient client, AbortController abort) fetchItems,
int Function(List<MediaItem> items)? resolveStartIndex,
}) async {
final abort = AbortController();
return executeWithLoading(
context: context,
showLoading: showLoadingIndicator,
actionLabel: shuffle ? t.common.shuffle : t.common.play,
abort: abort,
execute: (dismissLoading) async {
final client = clientForTesting ?? _resolveClient(ServerId(serverId));
if (client == null) {
return _missingClientError(serverId, dismissLoading);
}
var items = await fetchItems(client, abort);
if (items.isEmpty) return const PlayQueueEmpty();
abort.throwIfAborted();
if (shuffle) {
items = List.of(items)..shuffle(Random());
}
abort.throwIfAborted();
final startIndex = resolveStartIndex?.call(items) ?? 0;
await dismissLoading();
abort.throwIfAborted();
if (!context.mounted && navigateForTesting == null) {
return const PlayQueueError('Context not mounted');
}
abort.throwIfAborted();
final playbackState = playbackStateForTesting ?? context.read<PlaybackStateProvider>();
return launchLocalQueuePlayback(
context: context,
playbackState: playbackState,
queue: LocalPlayQueue(
id: queueId,
items: items,
currentIndex: startIndex,
shuffled: shuffle,
backendId: client.backend.id,
),
contextKey: contextKey,
navigateForTesting: navigateForTesting,
);
},
);
}
/// Resolve the [MediaServerClient] for [serverId] through
/// [MultiServerProvider]. Returns null when the server isn't online or
/// the provider isn't in scope.
MediaServerClient? _resolveClient(ServerId serverId) {
final provider = Provider.of<MultiServerProvider>(context, listen: false);
return provider.serverManager.getClient(serverId);
}
Future<PlayQueueError> _missingClientError(String serverId, Future<void> Function() dismissLoading) async {
await dismissLoading();
if (context.mounted) {
showErrorSnackBar(context, t.errors.noClientAvailable);
}
return PlayQueueError(Exception('No client for server $serverId'));
}
}