Commit afb1a930 by Oliver Woodman

Refine ExtractorSampleSource retry.

I was hoping not to avoid this, but in the case of a parsing
failure during preparation we keep retrying forever!
parent 1a01dcc5
...@@ -27,7 +27,7 @@ import java.io.IOException; ...@@ -27,7 +27,7 @@ import java.io.IOException;
* A {@link TrackOutput} that buffers extracted samples in a queue, and allows for consumption from * A {@link TrackOutput} that buffers extracted samples in a queue, and allows for consumption from
* that queue. * that queue.
*/ */
public final class DefaultTrackOutput implements TrackOutput { public class DefaultTrackOutput implements TrackOutput {
private final RollingSampleBuffer rollingBuffer; private final RollingSampleBuffer rollingBuffer;
private final SampleHolder sampleInfoHolder; private final SampleHolder sampleInfoHolder;
......
...@@ -23,6 +23,7 @@ import com.google.android.exoplayer.SampleSource; ...@@ -23,6 +23,7 @@ import com.google.android.exoplayer.SampleSource;
import com.google.android.exoplayer.TrackInfo; import com.google.android.exoplayer.TrackInfo;
import com.google.android.exoplayer.TrackRenderer; import com.google.android.exoplayer.TrackRenderer;
import com.google.android.exoplayer.drm.DrmInitData; import com.google.android.exoplayer.drm.DrmInitData;
import com.google.android.exoplayer.upstream.Allocator;
import com.google.android.exoplayer.upstream.BufferPool; import com.google.android.exoplayer.upstream.BufferPool;
import com.google.android.exoplayer.upstream.DataSource; import com.google.android.exoplayer.upstream.DataSource;
import com.google.android.exoplayer.upstream.DataSpec; import com.google.android.exoplayer.upstream.DataSpec;
...@@ -58,7 +59,7 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -58,7 +59,7 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
private final Extractor extractor; private final Extractor extractor;
private final BufferPool bufferPool; private final BufferPool bufferPool;
private final int requestedBufferSize; private final int requestedBufferSize;
private final SparseArray<DefaultTrackOutput> sampleQueues; private final SparseArray<InternalTrackOutput> sampleQueues;
private final int minLoadableRetryCount; private final int minLoadableRetryCount;
private final boolean frameAccurateSeeking; private final boolean frameAccurateSeeking;
private final Uri uri; private final Uri uri;
...@@ -93,6 +94,9 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -93,6 +94,9 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
private long currentLoadableExceptionTimestamp; private long currentLoadableExceptionTimestamp;
private boolean loadingFinished; private boolean loadingFinished;
private int extractedSampleCount;
private int extractedSampleCountAtStartOfLoad;
/** /**
* @param uri The {@link Uri} of the media stream. * @param uri The {@link Uri} of the media stream.
* @param dataSource A data source to read the media stream. * @param dataSource A data source to read the media stream.
...@@ -125,7 +129,7 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -125,7 +129,7 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
this.remainingReleaseCount = downstreamRendererCount; this.remainingReleaseCount = downstreamRendererCount;
this.requestedBufferSize = requestedBufferSize; this.requestedBufferSize = requestedBufferSize;
this.minLoadableRetryCount = minLoadableRetryCount; this.minLoadableRetryCount = minLoadableRetryCount;
sampleQueues = new SparseArray<DefaultTrackOutput>(); sampleQueues = new SparseArray<InternalTrackOutput>();
bufferPool = new BufferPool(BUFFER_FRAGMENT_LENGTH); bufferPool = new BufferPool(BUFFER_FRAGMENT_LENGTH);
pendingResetPositionUs = NO_RESET_PENDING; pendingResetPositionUs = NO_RESET_PENDING;
frameAccurateSeeking = true; frameAccurateSeeking = true;
...@@ -228,7 +232,7 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -228,7 +232,7 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
return NOTHING_READ; return NOTHING_READ;
} }
DefaultTrackOutput sampleQueue = sampleQueues.valueAt(track); InternalTrackOutput sampleQueue = sampleQueues.valueAt(track);
if (pendingMediaFormat[track]) { if (pendingMediaFormat[track]) {
formatHolder.format = sampleQueue.getFormat(); formatHolder.format = sampleQueue.getFormat();
formatHolder.drmInitData = drmInitData; formatHolder.drmInitData = drmInitData;
...@@ -335,7 +339,8 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -335,7 +339,8 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
@Override @Override
public void onLoadError(Loadable ignored, IOException e) { public void onLoadError(Loadable ignored, IOException e) {
currentLoadableException = e; currentLoadableException = e;
currentLoadableExceptionCount = loadable.madeProgress() ? 1 : currentLoadableExceptionCount + 1; currentLoadableExceptionCount = extractedSampleCount > extractedSampleCountAtStartOfLoad ? 1
: currentLoadableExceptionCount + 1;
currentLoadableExceptionTimestamp = SystemClock.elapsedRealtime(); currentLoadableExceptionTimestamp = SystemClock.elapsedRealtime();
maybeStartLoading(); maybeStartLoading();
} }
...@@ -344,9 +349,9 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -344,9 +349,9 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
@Override @Override
public TrackOutput track(int id) { public TrackOutput track(int id) {
DefaultTrackOutput sampleQueue = sampleQueues.get(id); InternalTrackOutput sampleQueue = sampleQueues.get(id);
if (sampleQueue == null) { if (sampleQueue == null) {
sampleQueue = new DefaultTrackOutput(bufferPool); sampleQueue = new InternalTrackOutput(bufferPool);
sampleQueues.put(id, sampleQueue); sampleQueues.put(id, sampleQueue);
} }
return sampleQueue; return sampleQueue;
...@@ -427,6 +432,7 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -427,6 +432,7 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
// We're playing a seekable on-demand stream. Resume the current loadable, which will // We're playing a seekable on-demand stream. Resume the current loadable, which will
// request data starting from the point it left off. // request data starting from the point it left off.
} }
extractedSampleCountAtStartOfLoad = extractedSampleCount;
loader.startLoading(loadable, this); loader.startLoading(loadable, this);
} }
return; return;
...@@ -445,6 +451,7 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -445,6 +451,7 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
loadable = createLoadableFromPositionUs(pendingResetPositionUs); loadable = createLoadableFromPositionUs(pendingResetPositionUs);
pendingResetPositionUs = NO_RESET_PENDING; pendingResetPositionUs = NO_RESET_PENDING;
} }
extractedSampleCountAtStartOfLoad = extractedSampleCount;
loader.startLoading(loadable, this); loader.startLoading(loadable, this);
} }
...@@ -522,6 +529,24 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -522,6 +529,24 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
} }
/** /**
* Extension of {@link DefaultTrackOutput} that increments a shared counter of the total number
* of extracted samples.
*/
private class InternalTrackOutput extends DefaultTrackOutput {
public InternalTrackOutput(Allocator allocator) {
super(allocator);
}
@Override
public void sampleMetadata(long timeUs, int flags, int size, int offset, byte[] encryptionKey) {
super.sampleMetadata(timeUs, flags, size, offset, encryptionKey);
extractedSampleCount++;
}
}
/**
* Loads the media stream and extracts sample data from it. * Loads the media stream and extracts sample data from it.
*/ */
private static class ExtractingLoadable implements Loadable { private static class ExtractingLoadable implements Loadable {
...@@ -536,7 +561,6 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -536,7 +561,6 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
private volatile boolean loadCanceled; private volatile boolean loadCanceled;
private boolean pendingExtractorSeek; private boolean pendingExtractorSeek;
private boolean madeProgress;
public ExtractingLoadable(Uri uri, DataSource dataSource, Extractor extractor, public ExtractingLoadable(Uri uri, DataSource dataSource, Extractor extractor,
BufferPool bufferPool, int bufferPoolSizeLimit, long position) { BufferPool bufferPool, int bufferPoolSizeLimit, long position) {
...@@ -550,10 +574,6 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -550,10 +574,6 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
pendingExtractorSeek = true; pendingExtractorSeek = true;
} }
public boolean madeProgress() {
return madeProgress;
}
@Override @Override
public void cancelLoad() { public void cancelLoad() {
loadCanceled = true; loadCanceled = true;
...@@ -587,12 +607,9 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa ...@@ -587,12 +607,9 @@ public class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loa
} }
} finally { } finally {
if (result == Extractor.RESULT_SEEK) { if (result == Extractor.RESULT_SEEK) {
madeProgress |= true;
result = Extractor.RESULT_CONTINUE; result = Extractor.RESULT_CONTINUE;
} else if (input != null) { } else if (input != null) {
long newPosition = input.getPosition(); positionHolder.position = input.getPosition();
madeProgress |= newPosition > positionHolder.position;
positionHolder.position = newPosition;
} }
dataSource.close(); dataSource.close();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment