mirror of
https://github.com/samsonjs/media.git
synced 2026-04-19 13:35:47 +00:00
More SampleSource simplification.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=118775144
This commit is contained in:
parent
a4d17282f1
commit
292b2402e6
11 changed files with 38 additions and 100 deletions
|
|
@ -67,6 +67,7 @@ public class DashSourceBuilder implements SourceBuilder {
|
|||
public SampleSource buildRenderers(DemoPlayer player) {
|
||||
MediaPresentationDescriptionParser parser = new MediaPresentationDescriptionParser();
|
||||
DataSource manifestDataSource = dataSourceFactory.createDataSource();
|
||||
// TODO[REFACTOR]: This needs releasing.
|
||||
ManifestFetcher<MediaPresentationDescription> manifestFetcher = new ManifestFetcher<>(
|
||||
Uri.parse(url), manifestDataSource, parser);
|
||||
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ public class HlsSourceBuilder implements SourceBuilder {
|
|||
public SampleSource buildRenderers(DemoPlayer player) {
|
||||
HlsPlaylistParser parser = new HlsPlaylistParser();
|
||||
DataSource manifestDataSource = dataSourceFactory.createDataSource();
|
||||
// TODO[REFACTOR]: This needs releasing.
|
||||
ManifestFetcher<HlsPlaylist> manifestFetcher = new ManifestFetcher<>(Uri.parse(url),
|
||||
manifestDataSource, parser);
|
||||
|
||||
|
|
|
|||
|
|
@ -62,6 +62,7 @@ public class SmoothStreamingSourceBuilder implements SourceBuilder {
|
|||
@Override
|
||||
public SampleSource buildRenderers(DemoPlayer player) {
|
||||
SmoothStreamingManifestParser parser = new SmoothStreamingManifestParser();
|
||||
// TODO[REFACTOR]: This needs releasing.
|
||||
DataSource manifestDataSource = dataSourceFactory.createDataSource();
|
||||
ManifestFetcher<SmoothStreamingManifest> manifestFetcher = new ManifestFetcher<>(Uri.parse(url),
|
||||
manifestDataSource, parser);
|
||||
|
|
|
|||
|
|
@ -65,18 +65,16 @@ public final class SingleSampleSource implements SampleSource, TrackStream, Load
|
|||
|
||||
private final Uri uri;
|
||||
private final DataSource dataSource;
|
||||
private final Loader loader;
|
||||
private final Format format;
|
||||
private final long durationUs;
|
||||
private final int minLoadableRetryCount;
|
||||
private final TrackGroupArray tracks;
|
||||
private final Handler eventHandler;
|
||||
private final EventListener eventListener;
|
||||
private final int eventSourceId;
|
||||
|
||||
private boolean prepared;
|
||||
private long pendingResetPositionUs;
|
||||
private boolean loadingFinished;
|
||||
private Loader loader;
|
||||
|
||||
private int streamState;
|
||||
private byte[] sampleData;
|
||||
|
|
@ -98,10 +96,10 @@ public final class SingleSampleSource implements SampleSource, TrackStream, Load
|
|||
this.dataSource = dataSource;
|
||||
this.format = format;
|
||||
this.durationUs = durationUs;
|
||||
this.minLoadableRetryCount = minLoadableRetryCount;
|
||||
this.eventHandler = eventHandler;
|
||||
this.eventListener = eventListener;
|
||||
this.eventSourceId = eventSourceId;
|
||||
loader = new Loader("Loader:SingleSampleSource", minLoadableRetryCount);
|
||||
tracks = new TrackGroupArray(new TrackGroup(format));
|
||||
sampleData = new byte[INITIAL_SAMPLE_SIZE];
|
||||
}
|
||||
|
|
@ -113,11 +111,6 @@ public final class SingleSampleSource implements SampleSource, TrackStream, Load
|
|||
|
||||
@Override
|
||||
public boolean prepare(long positionUs) {
|
||||
if (prepared) {
|
||||
return true;
|
||||
}
|
||||
loader = new Loader("Loader:" + format.sampleMimeType, minLoadableRetryCount);
|
||||
prepared = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -134,7 +127,6 @@ public final class SingleSampleSource implements SampleSource, TrackStream, Load
|
|||
@Override
|
||||
public TrackStream[] selectTracks(List<TrackStream> oldStreams,
|
||||
List<TrackSelection> newSelections, long positionUs) {
|
||||
Assertions.checkState(prepared);
|
||||
Assertions.checkState(oldStreams.size() <= 1);
|
||||
Assertions.checkState(newSelections.size() <= 1);
|
||||
// Unselect old tracks.
|
||||
|
|
@ -213,10 +205,7 @@ public final class SingleSampleSource implements SampleSource, TrackStream, Load
|
|||
@Override
|
||||
public void release() {
|
||||
streamState = STREAM_STATE_END_OF_STREAM;
|
||||
if (loader != null) {
|
||||
loader.release();
|
||||
loader = null;
|
||||
}
|
||||
loader.release();
|
||||
}
|
||||
|
||||
// Loader.Callback implementation.
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||
|
||||
private static final long NO_RESET_PENDING = Long.MIN_VALUE;
|
||||
|
||||
private final Loader loader;
|
||||
private final int eventSourceId;
|
||||
private final LoadControl loadControl;
|
||||
private final ChunkSource chunkSource;
|
||||
|
|
@ -66,7 +67,6 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||
private final int bufferSizeContribution;
|
||||
private final Handler eventHandler;
|
||||
private final EventListener eventListener;
|
||||
private final int minLoadableRetryCount;
|
||||
|
||||
private boolean prepared;
|
||||
private long downstreamPositionUs;
|
||||
|
|
@ -78,7 +78,6 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||
|
||||
private TrackGroupArray trackGroups;
|
||||
private long durationUs;
|
||||
private Loader loader;
|
||||
private boolean loadingFinished;
|
||||
private boolean trackEnabled;
|
||||
private long currentLoadStartTimeMs;
|
||||
|
|
@ -132,7 +131,7 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||
this.eventHandler = eventHandler;
|
||||
this.eventListener = eventListener;
|
||||
this.eventSourceId = eventSourceId;
|
||||
this.minLoadableRetryCount = minLoadableRetryCount;
|
||||
loader = new Loader("Loader:ChunkSampleSource", minLoadableRetryCount);
|
||||
currentLoadableHolder = new ChunkOperationHolder();
|
||||
mediaChunks = new LinkedList<>();
|
||||
readOnlyMediaChunks = Collections.unmodifiableList(mediaChunks);
|
||||
|
|
@ -151,7 +150,6 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||
durationUs = chunkSource.getDurationUs();
|
||||
TrackGroup tracks = chunkSource.getTracks();
|
||||
if (tracks != null) {
|
||||
loader = new Loader("Loader:" + tracks.getFormat(0).containerMimeType, minLoadableRetryCount);
|
||||
trackGroups = new TrackGroupArray(tracks);
|
||||
} else {
|
||||
trackGroups = new TrackGroupArray();
|
||||
|
|
@ -191,7 +189,7 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||
newStreams[0] = this;
|
||||
}
|
||||
// Cancel or start requests as necessary.
|
||||
if (!trackEnabled && loader != null) {
|
||||
if (!trackEnabled) {
|
||||
if (loadControlRegistered) {
|
||||
loadControl.unregister(this);
|
||||
loadControlRegistered = false;
|
||||
|
|
@ -335,10 +333,7 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||
public void release() {
|
||||
prepared = false;
|
||||
trackEnabled = false;
|
||||
if (loader != null) {
|
||||
loader.release();
|
||||
loader = null;
|
||||
}
|
||||
loader.release();
|
||||
}
|
||||
|
||||
// Loadable.Callback implementation.
|
||||
|
|
|
|||
|
|
@ -114,7 +114,6 @@ public class DashChunkSource implements ChunkSource {
|
|||
private final long[] availableRangeValues;
|
||||
private final int eventSourceId;
|
||||
|
||||
private boolean manifestFetcherEnabled;
|
||||
private boolean live;
|
||||
private long durationUs;
|
||||
private MediaPresentationDescription currentManifest;
|
||||
|
|
@ -229,11 +228,6 @@ public class DashChunkSource implements ChunkSource {
|
|||
|
||||
@Override
|
||||
public boolean prepare() throws IOException {
|
||||
if (!manifestFetcherEnabled) {
|
||||
// TODO[REFACTOR]: We need to disable this at some point.
|
||||
manifestFetcher.enable();
|
||||
manifestFetcherEnabled = true;
|
||||
}
|
||||
if (currentManifest == null) {
|
||||
currentManifest = manifestFetcher.getManifest();
|
||||
if (currentManifest == null) {
|
||||
|
|
|
|||
|
|
@ -192,6 +192,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
|
|||
}
|
||||
}
|
||||
|
||||
private final Loader loader;
|
||||
private final ExtractorHolder extractorHolder;
|
||||
private final Allocator allocator;
|
||||
private final int requestedBufferSize;
|
||||
|
|
@ -224,7 +225,6 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
|
|||
private long pendingNextSampleUs;
|
||||
private long sampleTimeOffsetUs;
|
||||
|
||||
private Loader loader;
|
||||
private ExtractingLoadable loadable;
|
||||
private IOException fatalException;
|
||||
private boolean currentLoadExtractedSamples;
|
||||
|
|
@ -308,6 +308,10 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
|
|||
this.allocator = allocator;
|
||||
this.requestedBufferSize = requestedBufferSize;
|
||||
this.minLoadableRetryCount = minLoadableRetryCount;
|
||||
// Assume on-demand until we know otherwise.
|
||||
int initialMinRetryCount = minLoadableRetryCount == MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA
|
||||
? DEFAULT_MIN_LOADABLE_RETRY_COUNT_ON_DEMAND : minLoadableRetryCount;
|
||||
loader = new Loader("Loader:ExtractorSampleSource", initialMinRetryCount);
|
||||
if (extractors == null || extractors.length == 0) {
|
||||
extractors = new Extractor[DEFAULT_EXTRACTOR_CLASSES.size()];
|
||||
for (int i = 0; i < extractors.length; i++) {
|
||||
|
|
@ -330,12 +334,6 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
|
|||
if (prepared) {
|
||||
return true;
|
||||
}
|
||||
if (loader == null) {
|
||||
// Assume on-demand until we know otherwise.
|
||||
int initialMinLoadableRetryCount = minLoadableRetryCount == MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA
|
||||
? DEFAULT_MIN_LOADABLE_RETRY_COUNT_ON_DEMAND : minLoadableRetryCount;
|
||||
loader = new Loader("Loader:ExtractorSampleSource", initialMinLoadableRetryCount);
|
||||
}
|
||||
maybeStartLoading();
|
||||
if (seekMap == null || !tracksBuilt || !haveFormatsForAllTracks()) {
|
||||
maybeThrowError();
|
||||
|
|
@ -517,10 +515,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
|
|||
@Override
|
||||
public void release() {
|
||||
enabledTrackCount = 0;
|
||||
if (loader != null) {
|
||||
loader.release();
|
||||
loader = null;
|
||||
}
|
||||
loader.release();
|
||||
}
|
||||
|
||||
// Loader.Callback implementation.
|
||||
|
|
|
|||
|
|
@ -85,7 +85,6 @@ public class HlsChunkSource {
|
|||
private final HlsPlaylistParser playlistParser;
|
||||
private final PtsTimestampAdjusterProvider timestampAdjusterProvider;
|
||||
|
||||
private boolean manifestFetcherEnabled;
|
||||
private byte[] scratchSpace;
|
||||
private boolean live;
|
||||
private long durationUs;
|
||||
|
|
@ -157,11 +156,6 @@ public class HlsChunkSource {
|
|||
* @return True if the source was prepared, false otherwise.
|
||||
*/
|
||||
public boolean prepare() throws IOException {
|
||||
if (!manifestFetcherEnabled) {
|
||||
// TODO[REFACTOR]: We need to disable this at some point.
|
||||
manifestFetcher.enable();
|
||||
manifestFetcherEnabled = true;
|
||||
}
|
||||
if (masterPlaylist == null) {
|
||||
HlsPlaylist playlist = manifestFetcher.getManifest();
|
||||
if (playlist == null) {
|
||||
|
|
|
|||
|
|
@ -63,9 +63,9 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||
private static final int PRIMARY_TYPE_AUDIO = 2;
|
||||
private static final int PRIMARY_TYPE_VIDEO = 3;
|
||||
|
||||
private final Loader loader;
|
||||
private final HlsChunkSource chunkSource;
|
||||
private final LinkedList<HlsExtractorWrapper> extractors;
|
||||
private final int minLoadableRetryCount;
|
||||
private final int bufferSizeContribution;
|
||||
private final ChunkOperationHolder chunkOperationHolder;
|
||||
|
||||
|
|
@ -99,7 +99,6 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||
private TsChunk currentTsLoadable;
|
||||
private TsChunk previousTsLoadable;
|
||||
|
||||
private Loader loader;
|
||||
private long currentLoadStartTimeMs;
|
||||
|
||||
public HlsSampleSource(HlsChunkSource chunkSource, LoadControl loadControl,
|
||||
|
|
@ -120,11 +119,11 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||
this.chunkSource = chunkSource;
|
||||
this.loadControl = loadControl;
|
||||
this.bufferSizeContribution = bufferSizeContribution;
|
||||
this.minLoadableRetryCount = minLoadableRetryCount;
|
||||
this.eventHandler = eventHandler;
|
||||
this.eventListener = eventListener;
|
||||
this.eventSourceId = eventSourceId;
|
||||
this.pendingResetPositionUs = NO_RESET_PENDING;
|
||||
loader = new Loader("Loader:HLS", minLoadableRetryCount);
|
||||
extractors = new LinkedList<>();
|
||||
chunkOperationHolder = new ChunkOperationHolder();
|
||||
}
|
||||
|
|
@ -159,8 +158,7 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||
}
|
||||
}
|
||||
// We're not prepared and we haven't loaded what we need.
|
||||
if (loader == null) {
|
||||
loader = new Loader("Loader:HLS", minLoadableRetryCount);
|
||||
if (!loadControlRegistered) {
|
||||
loadControl.register(this, bufferSizeContribution);
|
||||
loadControlRegistered = true;
|
||||
}
|
||||
|
|
@ -368,14 +366,11 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||
@Override
|
||||
public void release() {
|
||||
enabledTrackCount = 0;
|
||||
if (loader != null) {
|
||||
if (loadControlRegistered) {
|
||||
loadControl.unregister(this);
|
||||
loadControlRegistered = false;
|
||||
}
|
||||
loader.release();
|
||||
loader = null;
|
||||
if (loadControlRegistered) {
|
||||
loadControl.unregister(this);
|
||||
loadControlRegistered = false;
|
||||
}
|
||||
loader.release();
|
||||
}
|
||||
|
||||
// Loader.Callback implementation.
|
||||
|
|
|
|||
|
|
@ -65,7 +65,6 @@ public class SmoothStreamingChunkSource implements ChunkSource {
|
|||
private final ManifestFetcher<SmoothStreamingManifest> manifestFetcher;
|
||||
private final FormatEvaluator adaptiveFormatEvaluator;
|
||||
|
||||
private boolean manifestFetcherEnabled;
|
||||
private boolean live;
|
||||
private long durationUs;
|
||||
private TrackEncryptionBox[] trackEncryptionBoxes;
|
||||
|
|
@ -122,11 +121,6 @@ public class SmoothStreamingChunkSource implements ChunkSource {
|
|||
|
||||
@Override
|
||||
public boolean prepare() throws IOException {
|
||||
if (!manifestFetcherEnabled) {
|
||||
// TODO[REFACTOR]: We need to disable this at some point.
|
||||
manifestFetcher.enable();
|
||||
manifestFetcherEnabled = true;
|
||||
}
|
||||
if (currentManifest == null) {
|
||||
currentManifest = manifestFetcher.getManifest();
|
||||
if (currentManifest == null) {
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ public class ManifestFetcher<T> implements Loader.Callback {
|
|||
|
||||
}
|
||||
|
||||
private final Loader loader;
|
||||
private final UriLoadable.Parser<T> parser;
|
||||
private final DataSource dataSource;
|
||||
private final Handler eventHandler;
|
||||
|
|
@ -76,8 +77,6 @@ public class ManifestFetcher<T> implements Loader.Callback {
|
|||
|
||||
private volatile Uri manifestUri;
|
||||
|
||||
private int enabledCount;
|
||||
private Loader loader;
|
||||
private UriLoadable<T> currentLoadable;
|
||||
private long currentLoadStartTimestamp;
|
||||
|
||||
|
|
@ -109,6 +108,7 @@ public class ManifestFetcher<T> implements Loader.Callback {
|
|||
this.dataSource = dataSource;
|
||||
this.eventHandler = eventHandler;
|
||||
this.eventListener = eventListener;
|
||||
loader = new Loader("Loader:ManifestFetcher", 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -167,32 +167,10 @@ public class ManifestFetcher<T> implements Loader.Callback {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables refresh functionality.
|
||||
*/
|
||||
public void enable() {
|
||||
enabledCount++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables refresh functionality.
|
||||
*/
|
||||
public void disable() {
|
||||
if (--enabledCount == 0) {
|
||||
if (loader != null) {
|
||||
loader.release();
|
||||
loader = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be invoked repeatedly by callers who require an updated manifest.
|
||||
*/
|
||||
public void requestRefresh() {
|
||||
if (loader == null) {
|
||||
loader = new Loader("manifestLoader", 1);
|
||||
}
|
||||
if (loader.isLoading()) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -202,17 +180,22 @@ public class ManifestFetcher<T> implements Loader.Callback {
|
|||
notifyManifestRefreshStarted();
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases the fetcher.
|
||||
* <p>
|
||||
* This method should be called when the fetcher is no longer required.
|
||||
*/
|
||||
public void release() {
|
||||
loader.release();
|
||||
}
|
||||
|
||||
// Loadable.Callback implementation.
|
||||
|
||||
@Override
|
||||
public void onLoadCompleted(Loadable loadable) {
|
||||
if (currentLoadable != loadable) {
|
||||
// Stale event.
|
||||
return;
|
||||
}
|
||||
|
||||
manifest = currentLoadable.getResult();
|
||||
manifestLoadStartTimestamp = currentLoadStartTimestamp;
|
||||
manifestLoadCompleteTimestamp = SystemClock.elapsedRealtime();
|
||||
|
||||
if (manifest instanceof RedirectingManifest) {
|
||||
RedirectingManifest redirectingManifest = (RedirectingManifest) manifest;
|
||||
Uri nextUri = redirectingManifest.getNextManifestUri();
|
||||
|
|
@ -220,7 +203,6 @@ public class ManifestFetcher<T> implements Loader.Callback {
|
|||
manifestUri = nextUri;
|
||||
}
|
||||
}
|
||||
|
||||
notifyManifestRefreshed();
|
||||
}
|
||||
|
||||
|
|
@ -231,15 +213,12 @@ public class ManifestFetcher<T> implements Loader.Callback {
|
|||
|
||||
@Override
|
||||
public int onLoadError(Loadable loadable, IOException exception) {
|
||||
if (currentLoadable != loadable) {
|
||||
// Stale event.
|
||||
return Loader.DONT_RETRY;
|
||||
}
|
||||
|
||||
notifyManifestError(new ManifestIOException(exception));
|
||||
return Loader.RETRY;
|
||||
}
|
||||
|
||||
// Private methods.
|
||||
|
||||
private void notifyManifestRefreshStarted() {
|
||||
if (eventHandler != null && eventListener != null) {
|
||||
eventHandler.post(new Runnable() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue