NewPipe-app-android/app/src/main/java/org/schabi/newpipe/player/helper/PlayerDataSource.java

118 lines
5.7 KiB
Java
Raw Normal View History

package org.schabi.newpipe.player.helper;
import android.content.Context;
2021-10-22 12:07:25 +02:00
import android.os.Build;
2019-10-07 09:41:44 +02:00
2019-10-04 14:59:08 +02:00
import androidx.annotation.NonNull;
2021-10-22 12:07:25 +02:00
import com.google.android.exoplayer2.source.MediaParserExtractorAdapter;
2019-10-07 09:41:44 +02:00
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.source.SingleSampleMediaSource;
2021-10-22 12:07:25 +02:00
import com.google.android.exoplayer2.source.chunk.MediaParserChunkExtractor;
import com.google.android.exoplayer2.source.dash.DashMediaSource;
import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
2021-10-22 12:07:25 +02:00
import com.google.android.exoplayer2.source.hls.MediaParserHlsMediaChunkExtractor;
import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource;
import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy;
import com.google.android.exoplayer2.upstream.TransferListener;
public class PlayerDataSource {
private static final int MANIFEST_MINIMUM_RETRY = 5;
private static final int EXTRACTOR_MINIMUM_RETRY = Integer.MAX_VALUE;
2021-08-28 19:41:58 +02:00
public static final int LIVE_STREAM_EDGE_GAP_MILLIS = 10000;
private final DataSource.Factory cacheDataSourceFactory;
private final DataSource.Factory cachelessDataSourceFactory;
public PlayerDataSource(@NonNull final Context context, @NonNull final String userAgent,
@NonNull final TransferListener transferListener) {
cacheDataSourceFactory = new CacheFactory(context, userAgent, transferListener);
cachelessDataSourceFactory
= new DefaultDataSourceFactory(context, userAgent, transferListener);
}
public SsMediaSource.Factory getLiveSsMediaSourceFactory() {
return new SsMediaSource.Factory(new DefaultSsChunkSource.Factory(
cachelessDataSourceFactory), cachelessDataSourceFactory)
.setLoadErrorHandlingPolicy(
new DefaultLoadErrorHandlingPolicy(MANIFEST_MINIMUM_RETRY))
.setLivePresentationDelayMs(LIVE_STREAM_EDGE_GAP_MILLIS);
}
public HlsMediaSource.Factory getLiveHlsMediaSourceFactory() {
2021-10-22 12:07:25 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return new HlsMediaSource.Factory(cachelessDataSourceFactory)
.setExtractorFactory(MediaParserHlsMediaChunkExtractor.FACTORY)
.setAllowChunklessPreparation(true)
.setLoadErrorHandlingPolicy(
new DefaultLoadErrorHandlingPolicy(MANIFEST_MINIMUM_RETRY));
} else {
return new HlsMediaSource.Factory(cachelessDataSourceFactory)
.setAllowChunklessPreparation(true)
.setLoadErrorHandlingPolicy(
new DefaultLoadErrorHandlingPolicy(MANIFEST_MINIMUM_RETRY));
}
}
public DashMediaSource.Factory getLiveDashMediaSourceFactory() {
2021-10-22 12:07:25 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(
MediaParserChunkExtractor.FACTORY,
cachelessDataSourceFactory, 1), cachelessDataSourceFactory)
.setLoadErrorHandlingPolicy(
new DefaultLoadErrorHandlingPolicy(MANIFEST_MINIMUM_RETRY));
} else {
return new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(
cachelessDataSourceFactory), cachelessDataSourceFactory)
.setLoadErrorHandlingPolicy(
new DefaultLoadErrorHandlingPolicy(MANIFEST_MINIMUM_RETRY));
}
}
public SsMediaSource.Factory getSsMediaSourceFactory() {
return new SsMediaSource.Factory(new DefaultSsChunkSource.Factory(
cacheDataSourceFactory), cacheDataSourceFactory);
}
public HlsMediaSource.Factory getHlsMediaSourceFactory() {
2021-10-22 12:07:25 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return new HlsMediaSource.Factory(cacheDataSourceFactory)
.setExtractorFactory(MediaParserHlsMediaChunkExtractor.FACTORY);
} else {
return new HlsMediaSource.Factory(cacheDataSourceFactory);
}
}
public DashMediaSource.Factory getDashMediaSourceFactory() {
2021-10-22 12:07:25 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(
MediaParserChunkExtractor.FACTORY,
cacheDataSourceFactory, 1), cacheDataSourceFactory);
} else {
return new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(
cacheDataSourceFactory), cacheDataSourceFactory);
}
}
2019-10-07 09:41:44 +02:00
public ProgressiveMediaSource.Factory getExtractorMediaSourceFactory() {
2021-10-22 12:07:25 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return new ProgressiveMediaSource.Factory(cacheDataSourceFactory,
MediaParserExtractorAdapter.FACTORY)
.setLoadErrorHandlingPolicy(
new DefaultLoadErrorHandlingPolicy(EXTRACTOR_MINIMUM_RETRY));
} else {
return new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.setLoadErrorHandlingPolicy(
new DefaultLoadErrorHandlingPolicy(EXTRACTOR_MINIMUM_RETRY));
}
}
public SingleSampleMediaSource.Factory getSampleMediaSourceFactory() {
return new SingleSampleMediaSource.Factory(cacheDataSourceFactory);
}
}