Skip to content

Commit ef20d9b

Browse files
StypoxAudricV
authored andcommitted
Move stream's cache key generation in PlaybackResolver and improve PlaybackResolver's code
1 parent fbee310 commit ef20d9b

4 files changed

Lines changed: 79 additions & 54 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/helper/PlayerHelper.java

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ALL;
44
import static com.google.android.exoplayer2.Player.REPEAT_MODE_OFF;
55
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ONE;
6-
import static org.schabi.newpipe.extractor.stream.AudioStream.UNKNOWN_BITRATE;
7-
import static org.schabi.newpipe.extractor.stream.VideoStream.RESOLUTION_UNKNOWN;
86
import static org.schabi.newpipe.player.Player.IDLE_WINDOW_FLAGS;
97
import static org.schabi.newpipe.player.Player.PLAYER_TYPE;
108
import static org.schabi.newpipe.player.helper.PlayerHelper.AutoplayType.AUTOPLAY_TYPE_ALWAYS;
@@ -47,11 +45,9 @@
4745
import org.schabi.newpipe.R;
4846
import org.schabi.newpipe.extractor.InfoItem;
4947
import org.schabi.newpipe.extractor.MediaFormat;
50-
import org.schabi.newpipe.extractor.stream.AudioStream;
5148
import org.schabi.newpipe.extractor.stream.StreamInfo;
5249
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
5350
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
54-
import org.schabi.newpipe.extractor.stream.VideoStream;
5551
import org.schabi.newpipe.extractor.utils.Utils;
5652
import org.schabi.newpipe.player.MainPlayer;
5753
import org.schabi.newpipe.player.Player;
@@ -197,52 +193,6 @@ public static String resizeTypeOf(@NonNull final Context context,
197193
}
198194
}
199195

200-
@NonNull
201-
public static String cacheKeyOf(@NonNull final StreamInfo info,
202-
@NonNull final VideoStream videoStream) {
203-
String cacheKey = info.getUrl() + " " + videoStream.getId();
204-
205-
final String resolution = videoStream.getResolution();
206-
final MediaFormat mediaFormat = videoStream.getFormat();
207-
if (resolution.equals(RESOLUTION_UNKNOWN) && mediaFormat == null) {
208-
// The hash code is only used in the cache key in the case when the resolution and the
209-
// media format are unknown
210-
cacheKey += " " + videoStream.hashCode();
211-
} else {
212-
if (mediaFormat != null) {
213-
cacheKey += " " + videoStream.getFormat().getName();
214-
}
215-
if (!resolution.equals(RESOLUTION_UNKNOWN)) {
216-
cacheKey += " " + resolution;
217-
}
218-
}
219-
220-
return cacheKey;
221-
}
222-
223-
@NonNull
224-
public static String cacheKeyOf(@NonNull final StreamInfo info,
225-
@NonNull final AudioStream audioStream) {
226-
String cacheKey = info.getUrl() + " " + audioStream.getId();
227-
228-
final int averageBitrate = audioStream.getAverageBitrate();
229-
final MediaFormat mediaFormat = audioStream.getFormat();
230-
if (averageBitrate == UNKNOWN_BITRATE && mediaFormat == null) {
231-
// The hash code is only used in the cache key in the case when the resolution and the
232-
// media format are unknown
233-
cacheKey += " " + audioStream.hashCode();
234-
} else {
235-
if (mediaFormat != null) {
236-
cacheKey += " " + audioStream.getFormat().getName();
237-
}
238-
if (averageBitrate != UNKNOWN_BITRATE) {
239-
cacheKey += " " + averageBitrate;
240-
}
241-
}
242-
243-
return cacheKey;
244-
}
245-
246196
/**
247197
* Given a {@link StreamInfo} and the existing queue items,
248198
* provide the {@link SinglePlayQueue} consisting of the next video for auto queueing.

app/src/main/java/org/schabi/newpipe/player/resolver/AudioPlaybackResolver.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.schabi.newpipe.extractor.stream.AudioStream;
1414
import org.schabi.newpipe.extractor.stream.StreamInfo;
1515
import org.schabi.newpipe.player.helper.PlayerDataSource;
16-
import org.schabi.newpipe.player.helper.PlayerHelper;
1716
import org.schabi.newpipe.player.mediaitem.MediaItemTag;
1817
import org.schabi.newpipe.player.mediaitem.StreamInfoTag;
1918
import org.schabi.newpipe.util.ListHelper;
@@ -57,7 +56,7 @@ public MediaSource resolve(@NonNull final StreamInfo info) {
5756

5857
try {
5958
return PlaybackResolver.buildMediaSource(
60-
dataSource, audio, info, PlayerHelper.cacheKeyOf(info, audio), tag);
59+
dataSource, audio, info, PlaybackResolver.cacheKeyOf(info, audio), tag);
6160
} catch (final IOException e) {
6261
Log.e(TAG, "Unable to create audio source:", e);
6362
return null;

app/src/main/java/org/schabi/newpipe/player/resolver/PlaybackResolver.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.schabi.newpipe.player.resolver;
22

3+
import static org.schabi.newpipe.extractor.stream.AudioStream.UNKNOWN_BITRATE;
4+
import static org.schabi.newpipe.extractor.stream.VideoStream.RESOLUTION_UNKNOWN;
35
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
46
import static org.schabi.newpipe.player.helper.PlayerDataSource.LIVE_STREAM_EDGE_GAP_MILLIS;
57

@@ -20,6 +22,7 @@
2022
import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifest;
2123
import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifestParser;
2224

25+
import org.schabi.newpipe.extractor.MediaFormat;
2326
import org.schabi.newpipe.extractor.ServiceList;
2427
import org.schabi.newpipe.extractor.services.youtube.ItagItem;
2528
import org.schabi.newpipe.extractor.services.youtube.dashmanifestcreators.CreationException;
@@ -49,6 +52,79 @@
4952
public interface PlaybackResolver extends Resolver<StreamInfo, MediaSource> {
5053
String TAG = PlaybackResolver.class.getSimpleName();
5154

55+
@NonNull
56+
private static StringBuilder commonCacheKeyOf(@NonNull final StreamInfo info,
57+
@NonNull final Stream stream,
58+
final boolean resolutionOrBitrateUnknown) {
59+
// stream info service id
60+
final StringBuilder cacheKey = new StringBuilder(info.getServiceId());
61+
62+
// stream info id
63+
cacheKey.append(" ");
64+
cacheKey.append(info.getId());
65+
66+
// stream id (even if unknown)
67+
cacheKey.append(" ");
68+
cacheKey.append(stream.getId());
69+
70+
// mediaFormat (if not null)
71+
final MediaFormat mediaFormat = stream.getFormat();
72+
if (mediaFormat != null) {
73+
cacheKey.append(" ");
74+
cacheKey.append(mediaFormat.getName());
75+
}
76+
77+
// content (only if other information is missing)
78+
// If the media format and the resolution/bitrate are both missing, then we don't have
79+
// enough information to distinguish this stream from other streams.
80+
// So, only in that case, we use the content (i.e. url or manifest) to differentiate
81+
// between streams.
82+
// Note that if the content were used even when other information is present, then two
83+
// streams with the same stats but with different contents (e.g. because the url was
84+
// refreshed) will be considered different (i.e. with a different cacheKey), making the
85+
// cache useless.
86+
if (resolutionOrBitrateUnknown && mediaFormat == null) {
87+
cacheKey.append(" ");
88+
Objects.hash(stream.getContent(), stream.getManifestUrl());
89+
}
90+
91+
return cacheKey;
92+
}
93+
94+
@NonNull
95+
static String cacheKeyOf(@NonNull final StreamInfo info,
96+
@NonNull final VideoStream videoStream) {
97+
final boolean resolutionUnknown = videoStream.getResolution().equals(RESOLUTION_UNKNOWN);
98+
final StringBuilder cacheKey = commonCacheKeyOf(info, videoStream, resolutionUnknown);
99+
100+
// resolution (if known)
101+
if (!resolutionUnknown) {
102+
cacheKey.append(" ");
103+
cacheKey.append(videoStream.getResolution());
104+
}
105+
106+
// isVideoOnly
107+
cacheKey.append(" ");
108+
cacheKey.append(videoStream.isVideoOnly());
109+
110+
return cacheKey.toString();
111+
}
112+
113+
@NonNull
114+
static String cacheKeyOf(@NonNull final StreamInfo info,
115+
@NonNull final AudioStream audioStream) {
116+
final boolean averageBitrateUnknown = audioStream.getAverageBitrate() == UNKNOWN_BITRATE;
117+
final StringBuilder cacheKey = commonCacheKeyOf(info, audioStream, averageBitrateUnknown);
118+
119+
// averageBitrate (if known)
120+
if (!averageBitrateUnknown) {
121+
cacheKey.append(" ");
122+
cacheKey.append(audioStream.getAverageBitrate());
123+
}
124+
125+
return cacheKey.toString();
126+
}
127+
52128
@Nullable
53129
static MediaSource maybeBuildLiveMediaSource(@NonNull final PlayerDataSource dataSource,
54130
@NonNull final StreamInfo info) {

app/src/main/java/org/schabi/newpipe/player/resolver/VideoPlaybackResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public MediaSource resolve(@NonNull final StreamInfo info) {
9595
if (video != null) {
9696
try {
9797
final MediaSource streamSource = PlaybackResolver.buildMediaSource(
98-
dataSource, video, info, PlayerHelper.cacheKeyOf(info, video), tag);
98+
dataSource, video, info, PlaybackResolver.cacheKeyOf(info, video), tag);
9999
mediaSources.add(streamSource);
100100
} catch (final IOException e) {
101101
Log.e(TAG, "Unable to create video source:", e);
@@ -114,7 +114,7 @@ public MediaSource resolve(@NonNull final StreamInfo info) {
114114
if (audio != null && (video == null || video.isVideoOnly())) {
115115
try {
116116
final MediaSource audioSource = PlaybackResolver.buildMediaSource(
117-
dataSource, audio, info, PlayerHelper.cacheKeyOf(info, audio), tag);
117+
dataSource, audio, info, PlaybackResolver.cacheKeyOf(info, audio), tag);
118118
mediaSources.add(audioSource);
119119
streamSourceType = SourceType.VIDEO_WITH_SEPARATED_AUDIO;
120120
} catch (final IOException e) {

0 commit comments

Comments
 (0)