Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix audio/subtitle index reset when switching episodes #6112

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions src/components/playback/playbackmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2407,20 +2407,20 @@ class PlaybackManager {
});
}

function rankStreamType(prevIndex, prevSource, mediaSource, streamType, isSecondarySubtitle) {
function rankStreamType(prevIndex, prevSource, mediaStreams, trackOptions, streamType, isSecondarySubtitle) {
if (prevIndex == -1) {
console.debug(`AutoSet ${streamType} - No Stream Set`);
if (streamType == 'Subtitle') {
if (isSecondarySubtitle) {
mediaSource.DefaultSecondarySubtitleStreamIndex = -1;
trackOptions.DefaultSecondarySubtitleStreamIndex = -1;
} else {
mediaSource.DefaultSubtitleStreamIndex = -1;
trackOptions.DefaultSubtitleStreamIndex = -1;
}
}
return;
}

if (!prevSource.MediaStreams || !mediaSource.MediaStreams) {
if (!prevSource.MediaStreams || !mediaStreams) {
console.debug(`AutoSet ${streamType} - No MediaStreams`);
return;
}
Expand All @@ -2446,7 +2446,7 @@ class PlaybackManager {
}

let newRelIndex = 0;
for (const stream of mediaSource.MediaStreams) {
for (const stream of mediaStreams) {
if (stream.Type != streamType) continue;

let score = 0;
Expand All @@ -2469,38 +2469,38 @@ class PlaybackManager {
console.debug(`AutoSet ${streamType} - Using ${bestStreamIndex} score ${bestStreamScore}.`);
if (streamType == 'Subtitle') {
if (isSecondarySubtitle) {
mediaSource.DefaultSecondarySubtitleStreamIndex = bestStreamIndex;
trackOptions.DefaultSecondarySubtitleStreamIndex = bestStreamIndex;
} else {
mediaSource.DefaultSubtitleStreamIndex = bestStreamIndex;
trackOptions.DefaultSubtitleStreamIndex = bestStreamIndex;
}
}
if (streamType == 'Audio') {
mediaSource.DefaultAudioStreamIndex = bestStreamIndex;
trackOptions.DefaultAudioStreamIndex = bestStreamIndex;
}
} else {
console.debug(`AutoSet ${streamType} - Threshold not met. Using default.`);
}
}

function autoSetNextTracks(prevSource, mediaSource, audio, subtitle) {
function autoSetNextTracks(prevSource, mediaStreams, trackOptions, audio, subtitle) {
try {
if (!prevSource) return;

if (!mediaSource) {
console.warn('AutoSet - No mediaSource');
if (!mediaStreams) {
console.warn('AutoSet - No mediaStreams');
return;
}

if (audio && typeof prevSource.DefaultAudioStreamIndex == 'number') {
rankStreamType(prevSource.DefaultAudioStreamIndex, prevSource, mediaSource, 'Audio');
rankStreamType(prevSource.DefaultAudioStreamIndex, prevSource, mediaStreams, trackOptions, 'Audio');
}

if (subtitle && typeof prevSource.DefaultSubtitleStreamIndex == 'number') {
rankStreamType(prevSource.DefaultSubtitleStreamIndex, prevSource, mediaSource, 'Subtitle');
rankStreamType(prevSource.DefaultSubtitleStreamIndex, prevSource, mediaStreams, trackOptions, 'Subtitle');
}

if (subtitle && typeof prevSource.DefaultSecondarySubtitleStreamIndex == 'number') {
rankStreamType(prevSource.DefaultSecondarySubtitleStreamIndex, prevSource, mediaSource, 'Subtitle', true);
rankStreamType(prevSource.DefaultSecondarySubtitleStreamIndex, prevSource, mediaStreams, trackOptions, 'Subtitle', true);
}
} catch (e) {
console.error(`AutoSet - Caught unexpected error: ${e}`);
Expand Down Expand Up @@ -2572,12 +2572,18 @@ class PlaybackManager {
});
}

return Promise.all([promise, player.getDeviceProfile(item)]).then(function (responses) {
const deviceProfile = responses[1];
const apiClient = ServerConnections.getApiClient(item.ServerId);
viown marked this conversation as resolved.
Show resolved Hide resolved
const mediaSourceId = playOptions.mediaSourceId || item.Id;
const getMediaStreams = apiClient.getItem(apiClient.getCurrentUserId(), mediaSourceId)
viown marked this conversation as resolved.
Show resolved Hide resolved
.then(fullItem => {
return fullItem.MediaStreams;
});

const apiClient = ServerConnections.getApiClient(item.ServerId);
return Promise.all([promise, player.getDeviceProfile(item), apiClient.getCurrentUser(), getMediaStreams]).then(function (responses) {
const deviceProfile = responses[1];
const user = responses[2];
const mediaStreams = responses[3];

const mediaSourceId = playOptions.mediaSourceId;
const audioStreamIndex = playOptions.audioStreamIndex;
const subtitleStreamIndex = playOptions.subtitleStreamIndex;
const options = {
Expand All @@ -2600,9 +2606,20 @@ class PlaybackManager {
// this reference was only needed by sendPlaybackListToPlayer
playOptions.items = null;

const trackOptions = {};

autoSetNextTracks(prevSource, mediaStreams, trackOptions, user.Configuration.RememberAudioSelections, user.Configuration.RememberSubtitleSelections);
dmitrylyzo marked this conversation as resolved.
Show resolved Hide resolved
if (trackOptions.DefaultAudioStreamIndex != null) {
options.audioStreamIndex = trackOptions.DefaultAudioStreamIndex;
}
if (trackOptions.DefaultSubtitleStreamIndex != null) {
options.subtitleStreamIndex = trackOptions.DefaultSubtitleStreamIndex;
}

return getPlaybackMediaSource(player, apiClient, deviceProfile, item, mediaSourceId, options).then(async (mediaSource) => {
const user = await apiClient.getCurrentUser();
autoSetNextTracks(prevSource, mediaSource, user.Configuration.RememberAudioSelections, user.Configuration.RememberSubtitleSelections);
if (trackOptions.DefaultSecondarySubtitleStreamIndex != null) {
mediaSource.DefaultSecondarySubtitleStreamIndex = trackOptions.DefaultSecondarySubtitleStreamIndex;
}

if (mediaSource.DefaultSubtitleStreamIndex == null || mediaSource.DefaultSubtitleStreamIndex < 0) {
mediaSource.DefaultSubtitleStreamIndex = mediaSource.DefaultSecondarySubtitleStreamIndex;
Expand Down
Loading