Skip to content

Commit

Permalink
Use relative subtitle/audio indexes. (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwalton3 committed Jun 26, 2022
1 parent f7fed8a commit b670715
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
63 changes: 59 additions & 4 deletions native/mpvVideoPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,26 @@
return this.appSettings.get('volume') || 1;
}

/**
* @private
*/
getRelativeIndexByType(mediaStreams, jellyIndex, streamType) {
let relIndex = 1;
for (const source of mediaStreams) {
if (source.Type != streamType || source.IsExternal) {
continue;
}

if (source.Index == jellyIndex) {
return relIndex;
}

relIndex += 1;
}

return null;
}

/**
* @private
*/
Expand All @@ -262,7 +282,43 @@
return '';
}

return '#' + this._subtitleTrackIndexToSetOnPlaying;
const subtitleRelIndex = this.getRelativeIndexByType(
options.mediaSource.MediaStreams,
this._subtitleTrackIndexToSetOnPlaying,
'Subtitle'
);

return subtitleRelIndex != null
? '#' + subtitleRelIndex
: '';
}

/**
* @private
*/
getAudioParam() {
const options = this._currentPlayOptions;

if (this._audioTrackIndexToSetOnPlaying != null && this._audioTrackIndexToSetOnPlaying >= 0) {
const initialAudioStream = options.mediaSource.MediaStreams[this._audioTrackIndexToSetOnPlaying];
if (!initialAudioStream) {
return '#1';
}
}

if (this._audioTrackIndexToSetOnPlaying == -1 || this._audioTrackIndexToSetOnPlaying == null) {
return '#1';
}

const audioRelIndex = this.getRelativeIndexByType(
options.mediaSource.MediaStreams,
this._audioTrackIndexToSetOnPlaying,
'Audio'
);

return audioRelIndex != null
? '#' + audioRelIndex
: '#1';
}

tryGetFramerate(options) {
Expand Down Expand Up @@ -300,8 +356,7 @@
player.load(val,
{ startMilliseconds: ms, autoplay: true },
streamdata,
(this._audioTrackIndexToSetOnPlaying != null)
? '#' + this._audioTrackIndexToSetOnPlaying : '#1',
this.getAudioParam(),
this.getSubtitleParam(),
resolve);
});
Expand Down Expand Up @@ -367,7 +422,7 @@
return;
}

window.api.player.setAudioStream(index != -1 ? '#' + index : '');
window.api.player.setAudioStream(this.getAudioParam());
}

onEndedInternal() {
Expand Down
25 changes: 13 additions & 12 deletions src/player/PlayerComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,20 +876,21 @@ void PlayerComponent::reselectStream(const QString &streamSelection, MediaType t

QString selection = "no";

for (auto stream : findStreamsForURL(streamName))
if (!streamID.isEmpty())
{
auto map = stream.toMap();

if (map["type"].toString() != mpvStreamTypeName)
continue;

if (!streamID.isEmpty() && map["ff-index"].toString() == streamID)
selection = streamID;
} else {
for (auto stream : findStreamsForURL(streamName))
{
selection = map["id"].toString();
break;
} else if (streamID.isEmpty() && map["external-filename"].toString() == streamName) {
selection = map["id"].toString();
break;
auto map = stream.toMap();

if (map["type"].toString() != mpvStreamTypeName)
{
continue;
} else if (map["external-filename"].toString() == streamName) {
selection = map["id"].toString();
break;
}
}
}

Expand Down

0 comments on commit b670715

Please sign in to comment.