Search in sources :

Example 46 with ParserException

use of com.google.android.exoplayer2.ParserException in project ExoPlayer by google.

the class AudioTagPayloadReader method parsePayload.

@Override
protected boolean parsePayload(ParsableByteArray data, long timeUs) throws ParserException {
    if (audioFormat == AUDIO_FORMAT_MP3) {
        int sampleSize = data.bytesLeft();
        output.sampleData(data, sampleSize);
        output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
        return true;
    } else {
        int packetType = data.readUnsignedByte();
        if (packetType == AAC_PACKET_TYPE_SEQUENCE_HEADER && !hasOutputFormat) {
            // Parse the sequence header.
            byte[] audioSpecificConfig = new byte[data.bytesLeft()];
            data.readBytes(audioSpecificConfig, 0, audioSpecificConfig.length);
            AacUtil.Config aacConfig = AacUtil.parseAudioSpecificConfig(audioSpecificConfig);
            Format format = new Format.Builder().setSampleMimeType(MimeTypes.AUDIO_AAC).setCodecs(aacConfig.codecs).setChannelCount(aacConfig.channelCount).setSampleRate(aacConfig.sampleRateHz).setInitializationData(Collections.singletonList(audioSpecificConfig)).build();
            output.format(format);
            hasOutputFormat = true;
            return false;
        } else if (audioFormat != AUDIO_FORMAT_AAC || packetType == AAC_PACKET_TYPE_AAC_RAW) {
            int sampleSize = data.bytesLeft();
            output.sampleData(data, sampleSize);
            output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
            return true;
        } else {
            return false;
        }
    }
}
Also used : AacUtil(com.google.android.exoplayer2.audio.AacUtil) Format(com.google.android.exoplayer2.Format)

Example 47 with ParserException

use of com.google.android.exoplayer2.ParserException in project ExoPlayer by google.

the class FlacMetadataReader method readStreamMarker.

/**
 * Reads the FLAC stream marker.
 *
 * @param input Input stream to read the stream marker from.
 * @throws ParserException If an error occurs parsing the stream marker. In this case, the
 *     position of {@code input} is advanced by {@link FlacConstants#STREAM_MARKER_SIZE} bytes.
 * @throws IOException If reading from the input fails. In this case, the position is left
 *     unchanged.
 */
public static void readStreamMarker(ExtractorInput input) throws IOException {
    ParsableByteArray scratch = new ParsableByteArray(FlacConstants.STREAM_MARKER_SIZE);
    input.readFully(scratch.getData(), 0, FlacConstants.STREAM_MARKER_SIZE);
    if (scratch.readUnsignedInt() != STREAM_MARKER) {
        throw ParserException.createForMalformedContainer("Failed to read FLAC stream marker.", /* cause= */
        null);
    }
}
Also used : ParsableByteArray(com.google.android.exoplayer2.util.ParsableByteArray)

Example 48 with ParserException

use of com.google.android.exoplayer2.ParserException in project ExoPlayer by google.

the class FlacMetadataReader method getFrameStartMarker.

/**
 * Returns the frame start marker, consisting of the 2 first bytes of the first frame.
 *
 * <p>The read position of {@code input} is left unchanged and the peek position is aligned with
 * the read position.
 *
 * @param input Input stream to get the start marker from (starting from the read position).
 * @return The frame start marker (which must be the same for all the frames in the stream).
 * @throws ParserException If an error occurs parsing the frame start marker.
 * @throws IOException If peeking from the input fails.
 */
public static int getFrameStartMarker(ExtractorInput input) throws IOException {
    input.resetPeekPosition();
    ParsableByteArray scratch = new ParsableByteArray(2);
    input.peekFully(scratch.getData(), 0, 2);
    int frameStartMarker = scratch.readUnsignedShort();
    int syncCode = frameStartMarker >> 2;
    if (syncCode != SYNC_CODE) {
        input.resetPeekPosition();
        throw ParserException.createForMalformedContainer("First frame does not start with sync code.", /* cause= */
        null);
    }
    input.resetPeekPosition();
    return frameStartMarker;
}
Also used : ParsableByteArray(com.google.android.exoplayer2.util.ParsableByteArray)

Example 49 with ParserException

use of com.google.android.exoplayer2.ParserException in project ExoPlayer by google.

the class VorbisUtilTest method verifyVorbisHeaderCapturePattern_withValidHeader_returnsFalse.

@Test
public void verifyVorbisHeaderCapturePattern_withValidHeader_returnsFalse() {
    ParsableByteArray header = new ParsableByteArray(new byte[] { 0x01, 'v', 'o', 'r', 'b', 'i', 's' });
    try {
        VorbisUtil.verifyVorbisHeaderCapturePattern(0x99, header, false);
        fail();
    } catch (ParserException e) {
        assertThat(e.getMessage()).isEqualTo("expected header type 99");
    }
}
Also used : ParsableByteArray(com.google.android.exoplayer2.util.ParsableByteArray) ParserException(com.google.android.exoplayer2.ParserException) Test(org.junit.Test)

Example 50 with ParserException

use of com.google.android.exoplayer2.ParserException in project ExoPlayer by google.

the class VorbisUtilTest method verifyVorbisHeaderCapturePattern_withInvalidHeaderQuite_returnsFalse.

@Test
public void verifyVorbisHeaderCapturePattern_withInvalidHeaderQuite_returnsFalse() throws ParserException {
    ParsableByteArray header = new ParsableByteArray(new byte[] { 0x01, 'v', 'o', 'r', 'b', 'i', 's' });
    assertThat(verifyVorbisHeaderCapturePattern(0x99, header, true)).isFalse();
}
Also used : ParsableByteArray(com.google.android.exoplayer2.util.ParsableByteArray) Test(org.junit.Test)

Aggregations

ParsableByteArray (com.google.android.exoplayer2.util.ParsableByteArray)25 Test (org.junit.Test)25 ParserException (com.google.android.exoplayer2.ParserException)21 MediaItem (com.google.android.exoplayer2.MediaItem)13 Timeline (com.google.android.exoplayer2.Timeline)13 Nullable (androidx.annotation.Nullable)12 Format (com.google.android.exoplayer2.Format)7 PositionHolder (com.google.android.exoplayer2.extractor.PositionHolder)5 LeafAtom (com.google.android.exoplayer2.extractor.mp4.Atom.LeafAtom)5 FakeExtractorInput (com.google.android.exoplayer2.testutil.FakeExtractorInput)5 ArrayList (java.util.ArrayList)5 Uri (android.net.Uri)3 DrmInitData (com.google.android.exoplayer2.drm.DrmInitData)3 ContainerAtom (com.google.android.exoplayer2.extractor.mp4.Atom.ContainerAtom)3 RequiresNonNull (org.checkerframework.checker.nullness.qual.RequiresNonNull)3 CallSuper (androidx.annotation.CallSuper)2 AacUtil (com.google.android.exoplayer2.audio.AacUtil)2 SchemeData (com.google.android.exoplayer2.drm.DrmInitData.SchemeData)2 GaplessInfoHolder (com.google.android.exoplayer2.extractor.GaplessInfoHolder)2 AvcConfig (com.google.android.exoplayer2.video.AvcConfig)2