Search in sources :

Example 86 with ExtractorInput

use of com.google.android.exoplayer2.extractor.ExtractorInput in project ExoPlayer by google.

the class FlacDecoderJni method readFromExtractorInput.

private int readFromExtractorInput(ExtractorInput extractorInput, byte[] tempBuffer, int offset, int length) throws IOException {
    int read = extractorInput.read(tempBuffer, offset, length);
    if (read == C.RESULT_END_OF_INPUT) {
        endOfExtractorInput = true;
        read = 0;
    }
    return read;
}
Also used : SeekPoint(com.google.android.exoplayer2.extractor.SeekPoint)

Example 87 with ExtractorInput

use of com.google.android.exoplayer2.extractor.ExtractorInput in project ExoPlayer by google.

the class FlacDecoderJni method read.

/**
 * Reads up to {@code length} bytes from the data source.
 *
 * <p>This method blocks until at least one byte of data can be read, the end of the input is
 * detected or an exception is thrown.
 *
 * @param target A target {@link ByteBuffer} into which data should be written.
 * @return Returns the number of bytes read, or -1 on failure. If all of the data has already been
 *     read from the source, then 0 is returned.
 */
// Called from native code.
@SuppressWarnings("unused")
public int read(ByteBuffer target) throws IOException {
    int byteCount = target.remaining();
    if (byteBufferData != null) {
        byteCount = min(byteCount, byteBufferData.remaining());
        int originalLimit = byteBufferData.limit();
        byteBufferData.limit(byteBufferData.position() + byteCount);
        target.put(byteBufferData);
        byteBufferData.limit(originalLimit);
    } else if (extractorInput != null) {
        ExtractorInput extractorInput = this.extractorInput;
        byte[] tempBuffer = Util.castNonNull(this.tempBuffer);
        byteCount = min(byteCount, TEMP_BUFFER_SIZE);
        int read = readFromExtractorInput(extractorInput, tempBuffer, /* offset= */
        0, byteCount);
        if (read < 4) {
            // Reading less than 4 bytes, most of the time, happens because of getting the bytes left in
            // the buffer of the input. Do another read to reduce the number of calls to this method
            // from the native code.
            read += readFromExtractorInput(extractorInput, tempBuffer, read, /* length= */
            byteCount - read);
        }
        byteCount = read;
        target.put(tempBuffer, 0, byteCount);
    } else {
        return -1;
    }
    return byteCount;
}
Also used : ExtractorInput(com.google.android.exoplayer2.extractor.ExtractorInput) SeekPoint(com.google.android.exoplayer2.extractor.SeekPoint)

Example 88 with ExtractorInput

use of com.google.android.exoplayer2.extractor.ExtractorInput in project ExoPlayer by google.

the class FlacExtractor method decodeStreamMetadata.

// Requires initialized.
@RequiresNonNull({ "decoderJni", "extractorOutput", "trackOutput" })
// Ensures stream metadata decoded.
@EnsuresNonNull({ "streamMetadata", "outputFrameHolder" })
@SuppressWarnings("nullness:contracts.postcondition")
private void decodeStreamMetadata(ExtractorInput input) throws IOException {
    if (streamMetadataDecoded) {
        return;
    }
    FlacDecoderJni flacDecoderJni = decoderJni;
    FlacStreamMetadata streamMetadata;
    try {
        streamMetadata = flacDecoderJni.decodeStreamMetadata();
    } catch (IOException e) {
        flacDecoderJni.reset(/* newPosition= */
        0);
        input.setRetryPosition(/* position= */
        0, e);
        throw e;
    }
    streamMetadataDecoded = true;
    if (this.streamMetadata == null) {
        this.streamMetadata = streamMetadata;
        outputBuffer.reset(streamMetadata.getMaxDecodedFrameSize());
        outputFrameHolder = new OutputFrameHolder(ByteBuffer.wrap(outputBuffer.getData()));
        binarySearchSeeker = outputSeekMap(flacDecoderJni, streamMetadata, input.getLength(), extractorOutput, outputFrameHolder);
        @Nullable Metadata metadata = streamMetadata.getMetadataCopyWithAppendedEntriesFrom(id3Metadata);
        outputFormat(streamMetadata, metadata, trackOutput);
    }
}
Also used : FlacStreamMetadata(com.google.android.exoplayer2.extractor.FlacStreamMetadata) Metadata(com.google.android.exoplayer2.metadata.Metadata) IOException(java.io.IOException) FlacStreamMetadata(com.google.android.exoplayer2.extractor.FlacStreamMetadata) Nullable(androidx.annotation.Nullable) OutputFrameHolder(com.google.android.exoplayer2.ext.flac.FlacBinarySearchSeeker.OutputFrameHolder) EnsuresNonNull(org.checkerframework.checker.nullness.qual.EnsuresNonNull) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 89 with ExtractorInput

use of com.google.android.exoplayer2.extractor.ExtractorInput in project ExoPlayer by google.

the class FlacExtractor method read.

@Override
public int read(final ExtractorInput input, PositionHolder seekPosition) throws IOException {
    if (input.getPosition() == 0 && !id3MetadataDisabled && id3Metadata == null) {
        id3Metadata = FlacMetadataReader.peekId3Metadata(input, /* parseData= */
        true);
    }
    FlacDecoderJni decoderJni = initDecoderJni(input);
    try {
        decodeStreamMetadata(input);
        if (binarySearchSeeker != null && binarySearchSeeker.isSeeking()) {
            return handlePendingSeek(input, seekPosition, outputBuffer, outputFrameHolder, trackOutput);
        }
        ByteBuffer outputByteBuffer = outputFrameHolder.byteBuffer;
        long lastDecodePosition = decoderJni.getDecodePosition();
        try {
            decoderJni.decodeSampleWithBacktrackPosition(outputByteBuffer, lastDecodePosition);
        } catch (FlacDecoderJni.FlacFrameDecodeException e) {
            throw new IOException("Cannot read frame at position " + lastDecodePosition, e);
        }
        int outputSize = outputByteBuffer.limit();
        if (outputSize == 0) {
            return RESULT_END_OF_INPUT;
        }
        outputSample(outputBuffer, outputSize, decoderJni.getLastFrameTimestamp(), trackOutput);
        return decoderJni.isEndOfData() ? RESULT_END_OF_INPUT : RESULT_CONTINUE;
    } finally {
        decoderJni.clearData();
    }
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SeekPoint(com.google.android.exoplayer2.extractor.SeekPoint)

Example 90 with ExtractorInput

use of com.google.android.exoplayer2.extractor.ExtractorInput in project ExoPlayer by google.

the class JpegExtractor method sniffMotionPhotoVideo.

private void sniffMotionPhotoVideo(ExtractorInput input) throws IOException {
    // Check if the file is truncated.
    boolean peekedData = input.peekFully(scratch.getData(), /* offset= */
    0, /* length= */
    1, /* allowEndOfInput= */
    true);
    if (!peekedData) {
        endReadingWithImageTrack();
    } else {
        input.resetPeekPosition();
        if (mp4Extractor == null) {
            mp4Extractor = new Mp4Extractor();
        }
        mp4ExtractorStartOffsetExtractorInput = new StartOffsetExtractorInput(input, mp4StartPosition);
        if (mp4Extractor.sniff(mp4ExtractorStartOffsetExtractorInput)) {
            mp4Extractor.init(new StartOffsetExtractorOutput(mp4StartPosition, checkNotNull(extractorOutput)));
            startReadingMotionPhoto();
        } else {
            endReadingWithImageTrack();
        }
    }
}
Also used : Mp4Extractor(com.google.android.exoplayer2.extractor.mp4.Mp4Extractor)

Aggregations

FakeExtractorInput (com.google.android.exoplayer2.testutil.FakeExtractorInput)85 Test (org.junit.Test)71 ExtractorInput (com.google.android.exoplayer2.extractor.ExtractorInput)53 ParsableByteArray (com.google.android.exoplayer2.util.ParsableByteArray)39 FlacStreamMetadataHolder (com.google.android.exoplayer2.extractor.FlacMetadataReader.FlacStreamMetadataHolder)20 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)17 DefaultExtractorInput (com.google.android.exoplayer2.extractor.DefaultExtractorInput)16 Nullable (androidx.annotation.Nullable)15 Metadata (com.google.android.exoplayer2.metadata.Metadata)13 SampleNumberHolder (com.google.android.exoplayer2.extractor.FlacFrameReader.SampleNumberHolder)9 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)9 EOFException (java.io.EOFException)8 SeekPoint (com.google.android.exoplayer2.extractor.SeekPoint)7 RequiresNonNull (org.checkerframework.checker.nullness.qual.RequiresNonNull)7 ParserException (com.google.android.exoplayer2.ParserException)5 SeekMap (com.google.android.exoplayer2.extractor.SeekMap)5 TrackOutput (com.google.android.exoplayer2.extractor.TrackOutput)5 IOException (java.io.IOException)5 PositionHolder (com.google.android.exoplayer2.extractor.PositionHolder)3 Id3Decoder (com.google.android.exoplayer2.metadata.id3.Id3Decoder)3