Create a SampleDataReader interface for TrackOutput.sampleData().

Modify TrackOutput.sampleData() to accept SampleDataReader instead of ExtractorInput. SampleDataReader supports only read and skip calls, which all sampleData() implementations already restrict themselves to.

PiperOrigin-RevId: 294905155
This commit is contained in:
olly 2020-02-13 14:52:41 +00:00 committed by Oliver Woodman
parent f5025bfce4
commit e606893ff8
10 changed files with 65 additions and 17 deletions

View File

@ -18,7 +18,7 @@ package com.google.android.exoplayer2.source;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer; import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.extractor.ExtractorInput; import com.google.android.exoplayer2.extractor.SampleDataReader;
import com.google.android.exoplayer2.extractor.TrackOutput.CryptoData; import com.google.android.exoplayer2.extractor.TrackOutput.CryptoData;
import com.google.android.exoplayer2.source.SampleQueue.SampleExtrasHolder; import com.google.android.exoplayer2.source.SampleQueue.SampleExtrasHolder;
import com.google.android.exoplayer2.upstream.Allocation; import com.google.android.exoplayer2.upstream.Allocation;
@ -174,7 +174,7 @@ import java.nio.ByteBuffer;
return totalBytesWritten; return totalBytesWritten;
} }
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput) public int sampleData(SampleDataReader input, int length, boolean allowEndOfInput)
throws IOException, InterruptedException { throws IOException, InterruptedException {
length = preAppend(length); length = preAppend(length);
int bytesAppended = int bytesAppended =

View File

@ -26,7 +26,7 @@ import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.drm.DrmInitData; import com.google.android.exoplayer2.drm.DrmInitData;
import com.google.android.exoplayer2.drm.DrmSession; import com.google.android.exoplayer2.drm.DrmSession;
import com.google.android.exoplayer2.drm.DrmSessionManager; import com.google.android.exoplayer2.drm.DrmSessionManager;
import com.google.android.exoplayer2.extractor.ExtractorInput; import com.google.android.exoplayer2.extractor.SampleDataReader;
import com.google.android.exoplayer2.extractor.TrackOutput; import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.upstream.Allocator; import com.google.android.exoplayer2.upstream.Allocator;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
@ -469,7 +469,7 @@ public class SampleQueue implements TrackOutput {
} }
@Override @Override
public final int sampleData(ExtractorInput input, int length, boolean allowEndOfInput) public final int sampleData(SampleDataReader input, int length, boolean allowEndOfInput)
throws IOException, InterruptedException { throws IOException, InterruptedException {
return sampleDataQueue.sampleData(input, length, allowEndOfInput); return sampleDataQueue.sampleData(input, length, allowEndOfInput);
} }

View File

@ -23,8 +23,8 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.extractor.DummyTrackOutput; import com.google.android.exoplayer2.extractor.DummyTrackOutput;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.ExtractorOutput; import com.google.android.exoplayer2.extractor.ExtractorOutput;
import com.google.android.exoplayer2.extractor.SampleDataReader;
import com.google.android.exoplayer2.extractor.SeekMap; import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.extractor.TrackOutput; import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
@ -203,7 +203,7 @@ public final class ChunkExtractorWrapper implements ExtractorOutput {
} }
@Override @Override
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput) public int sampleData(SampleDataReader input, int length, boolean allowEndOfInput)
throws IOException, InterruptedException { throws IOException, InterruptedException {
return castNonNull(trackOutput).sampleData(input, length, allowEndOfInput); return castNonNull(trackOutput).sampleData(input, length, allowEndOfInput);
} }

View File

@ -25,7 +25,7 @@ import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.FormatHolder; import com.google.android.exoplayer2.FormatHolder;
import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.drm.DrmSessionManager; import com.google.android.exoplayer2.drm.DrmSessionManager;
import com.google.android.exoplayer2.extractor.ExtractorInput; import com.google.android.exoplayer2.extractor.SampleDataReader;
import com.google.android.exoplayer2.extractor.TrackOutput; import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.metadata.Metadata; import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.MetadataInputBuffer; import com.google.android.exoplayer2.metadata.MetadataInputBuffer;
@ -295,7 +295,7 @@ public final class PlayerEmsgHandler implements Handler.Callback {
} }
@Override @Override
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput) public int sampleData(SampleDataReader input, int length, boolean allowEndOfInput)
throws IOException, InterruptedException { throws IOException, InterruptedException {
return sampleQueue.sampleData(input, length, allowEndOfInput); return sampleQueue.sampleData(input, length, allowEndOfInput);
} }

View File

@ -33,7 +33,7 @@ public final class DummyTrackOutput implements TrackOutput {
} }
@Override @Override
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput) public int sampleData(SampleDataReader input, int length, boolean allowEndOfInput)
throws IOException, InterruptedException { throws IOException, InterruptedException {
int bytesSkipped = input.skip(length); int bytesSkipped = input.skip(length);
if (bytesSkipped == C.RESULT_END_OF_INPUT) { if (bytesSkipped == C.RESULT_END_OF_INPUT) {

View File

@ -63,7 +63,7 @@ import java.io.InputStream;
* (regardless of {@code allowEndOfInput}). * (regardless of {@code allowEndOfInput}).
* </ul> * </ul>
*/ */
public interface ExtractorInput { public interface ExtractorInput extends SampleDataReader {
/** /**
* Reads up to {@code length} bytes from the input and resets the peek position. * Reads up to {@code length} bytes from the input and resets the peek position.

View File

@ -0,0 +1,48 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.extractor;
import com.google.android.exoplayer2.C;
import java.io.IOException;
/** Provides sample data to be consumed by a {@link TrackOutput}. */
public interface SampleDataReader {
/**
* Reads up to {@code length} bytes of sample data from the input.
*
* @param target A target array into which data should be written.
* @param offset The offset into the target array at which to write.
* @param length The maximum number of bytes to read from the input.
* @return The number of bytes read, or {@link C#RESULT_END_OF_INPUT} if the input has ended. This
* may be less than {@code length} because the end of the input (or available data) was
* reached, the method was interrupted, or the operation was aborted early for another reason.
* @throws IOException If an error occurs reading from the input.
* @throws InterruptedException If the thread has been interrupted.
*/
int read(byte[] target, int offset, int length) throws IOException, InterruptedException;
/**
* Like {@link #read(byte[], int, int)}, except the sample data is skipped instead of read.
*
* @param length The maximum number of bytes to skip from the input.
* @return The number of bytes skipped, or {@link C#RESULT_END_OF_INPUT} if the input has ended.
* This may be less than {@code length} because the end of the input (or available data) was
* reached, the method was interrupted, or the operation was aborted early for another reason.
* @throws IOException If an error occurs reading from the input.
* @throws InterruptedException If the thread has been interrupted.
*/
int skip(int length) throws IOException, InterruptedException;
}

View File

@ -112,7 +112,7 @@ public interface TrackOutput {
* @throws IOException If an error occurred reading from the input. * @throws IOException If an error occurred reading from the input.
* @throws InterruptedException If the thread was interrupted. * @throws InterruptedException If the thread was interrupted.
*/ */
int sampleData(ExtractorInput input, int length, boolean allowEndOfInput) int sampleData(SampleDataReader input, int length, boolean allowEndOfInput)
throws IOException, InterruptedException; throws IOException, InterruptedException;
/** /**
@ -127,13 +127,13 @@ public interface TrackOutput {
* Called when metadata associated with a sample has been extracted from the stream. * Called when metadata associated with a sample has been extracted from the stream.
* *
* <p>The corresponding sample data will have already been passed to the output via calls to * <p>The corresponding sample data will have already been passed to the output via calls to
* {@link #sampleData(ExtractorInput, int, boolean)} or {@link #sampleData(ParsableByteArray, * {@link #sampleData(SampleDataReader, int, boolean)} or {@link #sampleData(ParsableByteArray,
* int)}. * int)}.
* *
* @param timeUs The media timestamp associated with the sample, in microseconds. * @param timeUs The media timestamp associated with the sample, in microseconds.
* @param flags Flags associated with the sample. See {@code C.BUFFER_FLAG_*}. * @param flags Flags associated with the sample. See {@code C.BUFFER_FLAG_*}.
* @param size The size of the sample data, in bytes. * @param size The size of the sample data, in bytes.
* @param offset The number of bytes that have been passed to {@link #sampleData(ExtractorInput, * @param offset The number of bytes that have been passed to {@link #sampleData(SampleDataReader,
* int, boolean)} or {@link #sampleData(ParsableByteArray, int)} since the last byte belonging * int, boolean)} or {@link #sampleData(ParsableByteArray, int)} since the last byte belonging
* to the sample whose metadata is being passed. * to the sample whose metadata is being passed.
* @param encryptionData The encryption data required to decrypt the sample. May be null. * @param encryptionData The encryption data required to decrypt the sample. May be null.

View File

@ -29,8 +29,8 @@ import com.google.android.exoplayer2.drm.DrmSession;
import com.google.android.exoplayer2.drm.DrmSessionManager; import com.google.android.exoplayer2.drm.DrmSessionManager;
import com.google.android.exoplayer2.extractor.DummyTrackOutput; import com.google.android.exoplayer2.extractor.DummyTrackOutput;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.ExtractorOutput; import com.google.android.exoplayer2.extractor.ExtractorOutput;
import com.google.android.exoplayer2.extractor.SampleDataReader;
import com.google.android.exoplayer2.extractor.SeekMap; import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.extractor.TrackOutput; import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.metadata.Metadata; import com.google.android.exoplayer2.metadata.Metadata;
@ -1443,7 +1443,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
} }
@Override @Override
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput) public int sampleData(SampleDataReader input, int length, boolean allowEndOfInput)
throws IOException, InterruptedException { throws IOException, InterruptedException {
ensureBufferCapacity(bufferPosition + length); ensureBufferCapacity(bufferPosition + length);
int numBytesRead = input.read(buffer, bufferPosition, length); int numBytesRead = input.read(buffer, bufferPosition, length);

View File

@ -20,7 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.extractor.ExtractorInput; import com.google.android.exoplayer2.extractor.SampleDataReader;
import com.google.android.exoplayer2.extractor.TrackOutput; import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.testutil.Dumper.Dumpable; import com.google.android.exoplayer2.testutil.Dumper.Dumpable;
import com.google.android.exoplayer2.util.ParsableByteArray; import com.google.android.exoplayer2.util.ParsableByteArray;
@ -65,7 +65,7 @@ public final class FakeTrackOutput implements TrackOutput, Dumper.Dumpable {
} }
@Override @Override
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput) public int sampleData(SampleDataReader input, int length, boolean allowEndOfInput)
throws IOException, InterruptedException { throws IOException, InterruptedException {
byte[] newData = new byte[length]; byte[] newData = new byte[length];
int bytesAppended = input.read(newData, 0, length); int bytesAppended = input.read(newData, 0, length);