feat(player): keep the session's explicit track choices across episodes

Episode advance carried live player state, so the viewer's choice only
survived while every episode could serve it: one episode without the
picked audio or subtitle fell back, and the fallback became the carry for
the rest of the session. The screen now keeps the last explicit audio,
subtitle, and secondary-subtitle choices for its lifetime; automatic
outcomes never overwrite them, so the choice retries on every following
episode and reattaches as soon as a catalog can serve it again.

Audio catches up with the subtitle carry from #1785. The old matcher
required raw language equality (a 'sv' pick never found a 'swe' row) and
otherwise took the first same-language track, flipping a commentary or
alternate-mix pick back to the main mix on every episode. Audio now uses
the same evidence bands as subtitles: bridged language parity is
authoritative, a unique title match vouches for untagged tracks, codec
and channel-count parity only break ties, and an ambiguous catalog
declines to the server's own choice instead of guessing. The synthesized
source descriptor also prefers the row's own title over the display title
that collapses to the bare language.

Episode advance previously sent no audio hint to negotiation at all, so a
transcode baked in the server's default audio no matter what was playing.
Both backends now resolve the carried semantics against the new episode's
streams: Jellyfin sends the resolved AudioStreamIndex, Plex feeds the
transcode decision, an explicit per-part stream id always wins, and a
failed match falls back to the server's pick.

close #1785
This commit is contained in:
edde746
2026-08-04 16:10:02 +02:00
parent 61ae314c94
commit 4872adcde3
13 changed files with 721 additions and 112 deletions
@@ -64,6 +64,56 @@ JellyfinClient _clientWithPlaybackInfo(
);
}
Future<({PlaybackInitializationResult result, Uri playbackInfoUri, Map<String, dynamic> playbackInfoBody})>
_initializeJellyfinAudioCarry({int? selectedAudioStreamId, AudioTrack? preferredAudioTrack}) async {
late Uri playbackInfoUri;
late String playbackInfoBody;
final client = _clientWithPlaybackInfo(
(request) async {
playbackInfoUri = request.url;
playbackInfoBody = request.body;
return jsonResponse({
'MediaSources': [
{'Id': 'src-1'},
],
});
},
itemSources: [
{
'Id': 'src-1',
'Container': 'mkv',
'MediaStreams': [
{'Index': 0, 'Type': 'Video'},
{'Index': 1, 'Type': 'Audio', 'Codec': 'aac', 'Language': 'eng', 'IsDefault': true},
{'Index': 4, 'Type': 'Audio', 'Codec': 'flac', 'Language': 'jpn', 'Title': 'Main'},
],
},
],
);
try {
final result = await client.getPlaybackInitialization(
PlaybackInitializationOptions(
metadata: testMediaItem(
id: 'item-1',
backend: MediaBackend.jellyfin,
kind: MediaKind.episode,
serverId: 'srv-1',
),
selectedMediaIndex: 0,
selectedAudioStreamId: selectedAudioStreamId,
preferredAudioTrack: preferredAudioTrack,
),
);
return (
result: result,
playbackInfoUri: playbackInfoUri,
playbackInfoBody: jsonDecode(playbackInfoBody) as Map<String, dynamic>,
);
} finally {
client.close();
}
}
/// Serves [routes] as JSON keyed by request path and records the last URL seen
/// for each path; every other path answers 404.
({JellyfinClient client, Map<String, Uri> requests}) _routedClient(Map<String, Object> routes) {
@@ -1342,6 +1392,38 @@ void main() {
expect(uri.queryParameters['Container'], 'mkv');
});
test('semantic carried audio resolves against the selected source before PlaybackInfo negotiation', () async {
final initialized = await _initializeJellyfinAudioCarry(
preferredAudioTrack: const AudioTrack(id: 'source:99', language: 'jpn', title: 'Main', codec: 'flac'),
);
expect(initialized.playbackInfoUri.queryParameters['AudioStreamIndex'], '4');
expect(initialized.playbackInfoBody['AudioStreamIndex'], 4);
expect(initialized.result.activeAudioStreamId, 4);
expect(initialized.result.mediaInfo!.audioTracks.singleWhere((track) => track.id == 4).selected, isTrue);
});
test('explicit Jellyfin audio stream wins over a conflicting semantic carry', () async {
final initialized = await _initializeJellyfinAudioCarry(
selectedAudioStreamId: 1,
preferredAudioTrack: const AudioTrack(id: 'source:99', language: 'jpn', title: 'Main', codec: 'flac'),
);
expect(initialized.playbackInfoUri.queryParameters['AudioStreamIndex'], '1');
expect(initialized.playbackInfoBody['AudioStreamIndex'], 1);
expect(initialized.result.activeAudioStreamId, 1);
});
test('unresolvable semantic audio carry lets Jellyfin choose the stream', () async {
final initialized = await _initializeJellyfinAudioCarry(
preferredAudioTrack: const AudioTrack(id: 'source:99', language: 'swe'),
);
expect(initialized.playbackInfoUri.queryParameters.containsKey('AudioStreamIndex'), isFalse);
expect(initialized.playbackInfoBody.containsKey('AudioStreamIndex'), isFalse);
expect(initialized.result.activeAudioStreamId, isNull);
});
test('stale selected audio stream is not sent for a source without that stream', () async {
Uri? playbackInfoUri;
String? playbackInfoBody;