|
1 | 1 | package org.schabi.newpipe.player.resolver; |
2 | 2 |
|
| 3 | +import static org.schabi.newpipe.extractor.stream.AudioStream.UNKNOWN_BITRATE; |
| 4 | +import static org.schabi.newpipe.extractor.stream.VideoStream.RESOLUTION_UNKNOWN; |
3 | 5 | import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; |
4 | 6 | import static org.schabi.newpipe.player.helper.PlayerDataSource.LIVE_STREAM_EDGE_GAP_MILLIS; |
5 | 7 |
|
|
20 | 22 | import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifest; |
21 | 23 | import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifestParser; |
22 | 24 |
|
| 25 | +import org.schabi.newpipe.extractor.MediaFormat; |
23 | 26 | import org.schabi.newpipe.extractor.ServiceList; |
24 | 27 | import org.schabi.newpipe.extractor.services.youtube.ItagItem; |
25 | 28 | import org.schabi.newpipe.extractor.services.youtube.dashmanifestcreators.CreationException; |
|
49 | 52 | public interface PlaybackResolver extends Resolver<StreamInfo, MediaSource> { |
50 | 53 | String TAG = PlaybackResolver.class.getSimpleName(); |
51 | 54 |
|
| 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 | + |
52 | 128 | @Nullable |
53 | 129 | static MediaSource maybeBuildLiveMediaSource(@NonNull final PlayerDataSource dataSource, |
54 | 130 | @NonNull final StreamInfo info) { |
|
0 commit comments