Skip to content

Commit

Permalink
Merge pull request TeamNewPipe#10195 from AudricV/player_refactor-ren…
Browse files Browse the repository at this point in the history
…derers-activation-or-deactivation

Refactor Player.useVideoSource logic and improve its comments
  • Loading branch information
TobiGr committed Jul 22, 2023
2 parents 3243f97 + 5ab3a4a commit 3edd4c0
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions app/src/main/java/org/schabi/newpipe/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -2065,43 +2065,36 @@ private void notifyAudioTrackUpdateToListeners() {
}

public void useVideoSource(final boolean videoEnabled) {
if (playQueue == null || isAudioOnly == !videoEnabled || audioPlayerSelected()) {
if (playQueue == null || audioPlayerSelected()) {
return;
}

isAudioOnly = !videoEnabled;

// The current metadata may be null sometimes (for e.g. when using an unstable connection
// in livestreams) so we will be not able to execute the block below.
// Reload the play queue manager in this case, which is the behavior when we don't know the
// index of the video renderer or playQueueManagerReloadingNeeded returns true.
getCurrentStreamInfo().ifPresentOrElse(info -> {
// In the case we don't know the source type, fallback to the one with video with audio
// or audio-only source.
// In case we don't know the source type, fall back to either video-with-audio, or
// audio-only source type
final SourceType sourceType = videoResolver.getStreamSourceType()
.orElse(SourceType.VIDEO_WITH_AUDIO_OR_AUDIO_ONLY);

if (playQueueManagerReloadingNeeded(sourceType, info, getVideoRendererIndex())) {
reloadPlayQueueManager();
} else {
if (StreamTypeUtil.isAudio(info.getStreamType())) {
// Nothing to do more than setting the recovery position
setRecovery();
return;
}

final var parametersBuilder = trackSelector.buildUponParameters();

// Enable/disable the video track and the ability to select subtitles
parametersBuilder.setTrackTypeDisabled(C.TRACK_TYPE_TEXT, !videoEnabled);
parametersBuilder.setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, !videoEnabled);

trackSelector.setParameters(parametersBuilder);
}

setRecovery();

// Disable or enable video and subtitles renderers depending of the videoEnabled value
trackSelector.setParameters(trackSelector.buildUponParameters()
.setTrackTypeDisabled(C.TRACK_TYPE_TEXT, !videoEnabled)
.setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, !videoEnabled));
}, () -> {
// This is executed when the current stream info is not available.
/*
The current metadata may be null sometimes (for e.g. when using an unstable connection
in livestreams) so we will be not able to execute the block below
Reload the play queue manager in this case, which is the behavior when we don't know the
index of the video renderer or playQueueManagerReloadingNeeded returns true
*/
reloadPlayQueueManager();
setRecovery();
});
Expand Down

0 comments on commit 3edd4c0

Please sign in to comment.