1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-25 04:22:30 +01:00

Refactored `PlaybackResolver`

* fixes the deprecation of ``setTag``
* makes the code more consistent
* de-duplicates some code
This commit is contained in:
litetex 2021-11-12 20:14:39 +01:00
parent dd2cde3c1a
commit 48a1ab64b0

View File

@ -9,6 +9,7 @@ import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MediaSourceFactory;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.StreamInfo;
@ -41,25 +42,28 @@ public interface PlaybackResolver extends Resolver<StreamInfo, MediaSource> {
@NonNull final String sourceUrl, @NonNull final String sourceUrl,
@C.ContentType final int type, @C.ContentType final int type,
@NonNull final MediaSourceTag metadata) { @NonNull final MediaSourceTag metadata) {
final Uri uri = Uri.parse(sourceUrl); final MediaSourceFactory factory;
switch (type) { switch (type) {
case C.TYPE_SS: case C.TYPE_SS:
return dataSource.getLiveSsMediaSourceFactory().setTag(metadata) factory = dataSource.getLiveSsMediaSourceFactory();
.createMediaSource(MediaItem.fromUri(uri)); break;
case C.TYPE_DASH: case C.TYPE_DASH:
return dataSource.getLiveDashMediaSourceFactory().setTag(metadata) factory = dataSource.getLiveDashMediaSourceFactory();
.createMediaSource( break;
new MediaItem.Builder()
.setUri(uri)
.setLiveTargetOffsetMs(
PlayerDataSource.LIVE_STREAM_EDGE_GAP_MILLIS)
.build());
case C.TYPE_HLS: case C.TYPE_HLS:
return dataSource.getLiveHlsMediaSourceFactory().setTag(metadata) factory = dataSource.getLiveHlsMediaSourceFactory();
.createMediaSource(MediaItem.fromUri(uri)); break;
default: default:
throw new IllegalStateException("Unsupported type: " + type); throw new IllegalStateException("Unsupported type: " + type);
} }
return factory.createMediaSource(
new MediaItem.Builder()
.setTag(metadata)
.setUri(Uri.parse(sourceUrl))
.setLiveTargetOffsetMs(PlayerDataSource.LIVE_STREAM_EDGE_GAP_MILLIS)
.build()
);
} }
@NonNull @NonNull
@ -72,25 +76,30 @@ public interface PlaybackResolver extends Resolver<StreamInfo, MediaSource> {
@C.ContentType final int type = TextUtils.isEmpty(overrideExtension) @C.ContentType final int type = TextUtils.isEmpty(overrideExtension)
? Util.inferContentType(uri) : Util.inferContentType("." + overrideExtension); ? Util.inferContentType(uri) : Util.inferContentType("." + overrideExtension);
final MediaSourceFactory factory;
switch (type) { switch (type) {
case C.TYPE_SS: case C.TYPE_SS:
return dataSource.getLiveSsMediaSourceFactory().setTag(metadata) factory = dataSource.getLiveSsMediaSourceFactory();
.createMediaSource(MediaItem.fromUri(uri)); break;
case C.TYPE_DASH: case C.TYPE_DASH:
return dataSource.getDashMediaSourceFactory().setTag(metadata) factory = dataSource.getDashMediaSourceFactory();
.createMediaSource(MediaItem.fromUri(uri)); break;
case C.TYPE_HLS: case C.TYPE_HLS:
return dataSource.getHlsMediaSourceFactory().setTag(metadata) factory = dataSource.getHlsMediaSourceFactory();
.createMediaSource(MediaItem.fromUri(uri)); break;
case C.TYPE_OTHER: case C.TYPE_OTHER:
return dataSource.getExtractorMediaSourceFactory().setTag(metadata) factory = dataSource.getExtractorMediaSourceFactory();
.createMediaSource( break;
new MediaItem.Builder()
.setUri(uri)
.setCustomCacheKey(cacheKey)
.build());
default: default:
throw new IllegalStateException("Unsupported type: " + type); throw new IllegalStateException("Unsupported type: " + type);
} }
return factory.createMediaSource(
new MediaItem.Builder()
.setTag(metadata)
.setUri(uri)
.setCustomCacheKey(cacheKey)
.build()
);
} }
} }