Search in sources :

Example 1 with FlacStreamInfo

use of com.google.android.exoplayer2.util.FlacStreamInfo in project ExoPlayer by google.

the class FlacReader method readHeaders.

@Override
protected boolean readHeaders(ParsableByteArray packet, long position, SetupData setupData) throws IOException, InterruptedException {
    byte[] data = packet.data;
    if (streamInfo == null) {
        streamInfo = new FlacStreamInfo(data, 17);
        byte[] metadata = Arrays.copyOfRange(data, 9, packet.limit());
        // Set the last metadata block flag, ignore the other blocks
        metadata[4] = (byte) 0x80;
        List<byte[]> initializationData = Collections.singletonList(metadata);
        setupData.format = Format.createAudioSampleFormat(null, MimeTypes.AUDIO_FLAC, null, Format.NO_VALUE, streamInfo.bitRate(), streamInfo.channels, streamInfo.sampleRate, initializationData, null, 0, null);
    } else if ((data[0] & 0x7F) == SEEKTABLE_PACKET_TYPE) {
        flacOggSeeker = new FlacOggSeeker();
        flacOggSeeker.parseSeekTable(packet);
    } else if (isAudioPacket(data)) {
        if (flacOggSeeker != null) {
            flacOggSeeker.setFirstFrameOffset(position);
            setupData.oggSeeker = flacOggSeeker;
        }
        return false;
    }
    return true;
}
Also used : FlacStreamInfo(com.google.android.exoplayer2.util.FlacStreamInfo)

Example 2 with FlacStreamInfo

use of com.google.android.exoplayer2.util.FlacStreamInfo in project ExoPlayer by google.

the class FlacExtractor method read.

@Override
public int read(final ExtractorInput input, PositionHolder seekPosition) throws IOException, InterruptedException {
    decoderJni.setData(input);
    if (!metadataParsed) {
        final FlacStreamInfo streamInfo;
        try {
            streamInfo = decoderJni.decodeMetadata();
            if (streamInfo == null) {
                throw new IOException("Metadata decoding failed");
            }
        } catch (IOException e) {
            decoderJni.reset(0);
            input.setRetryPosition(0, e);
            // never executes
            throw e;
        }
        metadataParsed = true;
        extractorOutput.seekMap(new SeekMap() {

            final boolean isSeekable = decoderJni.getSeekPosition(0) != -1;

            final long durationUs = streamInfo.durationUs();

            @Override
            public boolean isSeekable() {
                return isSeekable;
            }

            @Override
            public long getPosition(long timeUs) {
                return isSeekable ? decoderJni.getSeekPosition(timeUs) : 0;
            }

            @Override
            public long getDurationUs() {
                return durationUs;
            }
        });
        Format mediaFormat = Format.createAudioSampleFormat(null, MimeTypes.AUDIO_RAW, null, streamInfo.bitRate(), Format.NO_VALUE, streamInfo.channels, streamInfo.sampleRate, C.ENCODING_PCM_16BIT, null, null, 0, null);
        trackOutput.format(mediaFormat);
        outputBuffer = new ParsableByteArray(streamInfo.maxDecodedFrameSize());
        outputByteBuffer = ByteBuffer.wrap(outputBuffer.data);
    }
    outputBuffer.reset();
    long lastDecodePosition = decoderJni.getDecodePosition();
    int size;
    try {
        size = decoderJni.decodeSample(outputByteBuffer);
    } catch (IOException e) {
        if (lastDecodePosition >= 0) {
            decoderJni.reset(lastDecodePosition);
            input.setRetryPosition(lastDecodePosition, e);
        }
        throw e;
    }
    if (size <= 0) {
        return RESULT_END_OF_INPUT;
    }
    trackOutput.sampleData(outputBuffer, size);
    trackOutput.sampleMetadata(decoderJni.getLastSampleTimestamp(), C.BUFFER_FLAG_KEY_FRAME, size, 0, null);
    return decoderJni.isEndOfData() ? RESULT_END_OF_INPUT : RESULT_CONTINUE;
}
Also used : ParsableByteArray(com.google.android.exoplayer2.util.ParsableByteArray) Format(com.google.android.exoplayer2.Format) FlacStreamInfo(com.google.android.exoplayer2.util.FlacStreamInfo) IOException(java.io.IOException) SeekMap(com.google.android.exoplayer2.extractor.SeekMap)

Aggregations

FlacStreamInfo (com.google.android.exoplayer2.util.FlacStreamInfo)2 Format (com.google.android.exoplayer2.Format)1 SeekMap (com.google.android.exoplayer2.extractor.SeekMap)1 ParsableByteArray (com.google.android.exoplayer2.util.ParsableByteArray)1 IOException (java.io.IOException)1