Search in sources :

Example 51 with ParserException

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

the class VorbisUtilTest method verifyVorbisHeaderCapturePattern_withInvalidPattern_returnsFalse.

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

Example 52 with ParserException

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

the class AmrExtractorNonParameterizedTest method read_amrNb_returnParserException_forInvalidFrameHeader.

@Test
public void read_amrNb_returnParserException_forInvalidFrameHeader() throws IOException {
    AmrExtractor amrExtractor = setupAmrExtractorWithOutput();
    byte[] invalidHeaderFrame = newNarrowBandAmrFrameWithType(4);
    // The padding bits are at bit-1 positions in the following pattern: 1000 0011
    // Padding bits must be 0.
    invalidHeaderFrame[0] = (byte) (invalidHeaderFrame[0] | 0b01111101);
    byte[] data = joinData(amrSignatureNb(), invalidHeaderFrame);
    FakeExtractorInput input = fakeExtractorInputWithData(data);
    try {
        amrExtractor.read(input, new PositionHolder());
        fail();
    } catch (ParserException e) {
    // expected
    }
}
Also used : FakeExtractorInput(com.google.android.exoplayer2.testutil.FakeExtractorInput) ParserException(com.google.android.exoplayer2.ParserException) PositionHolder(com.google.android.exoplayer2.extractor.PositionHolder) Test(org.junit.Test)

Example 53 with ParserException

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

the class AmrExtractorNonParameterizedTest method read_amrWb_returnParserException_forInvalidFrameHeader.

@Test
public void read_amrWb_returnParserException_forInvalidFrameHeader() throws IOException {
    AmrExtractor amrExtractor = setupAmrExtractorWithOutput();
    byte[] invalidHeaderFrame = newWideBandAmrFrameWithType(6);
    // The padding bits are at bit-1 positions in the following pattern: 1000 0011
    // Padding bits must be 0.
    invalidHeaderFrame[0] = (byte) (invalidHeaderFrame[0] | 0b01111110);
    byte[] data = joinData(amrSignatureWb(), invalidHeaderFrame);
    FakeExtractorInput input = fakeExtractorInputWithData(data);
    try {
        amrExtractor.read(input, new PositionHolder());
        fail();
    } catch (ParserException e) {
    // expected
    }
}
Also used : FakeExtractorInput(com.google.android.exoplayer2.testutil.FakeExtractorInput) ParserException(com.google.android.exoplayer2.ParserException) PositionHolder(com.google.android.exoplayer2.extractor.PositionHolder) Test(org.junit.Test)

Example 54 with ParserException

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

the class WebvttExtractor method processSample.

@RequiresNonNull("output")
private void processSample() throws ParserException {
    ParsableByteArray webvttData = new ParsableByteArray(sampleData);
    // Validate the first line of the header.
    WebvttParserUtil.validateWebvttHeaderLine(webvttData);
    // Defaults to use if the header doesn't contain an X-TIMESTAMP-MAP header.
    long vttTimestampUs = 0;
    long tsTimestampUs = 0;
    // Parse the remainder of the header looking for X-TIMESTAMP-MAP.
    for (String line = webvttData.readLine(); !TextUtils.isEmpty(line); line = webvttData.readLine()) {
        if (line.startsWith("X-TIMESTAMP-MAP")) {
            Matcher localTimestampMatcher = LOCAL_TIMESTAMP.matcher(line);
            if (!localTimestampMatcher.find()) {
                throw ParserException.createForMalformedContainer("X-TIMESTAMP-MAP doesn't contain local timestamp: " + line, /* cause= */
                null);
            }
            Matcher mediaTimestampMatcher = MEDIA_TIMESTAMP.matcher(line);
            if (!mediaTimestampMatcher.find()) {
                throw ParserException.createForMalformedContainer("X-TIMESTAMP-MAP doesn't contain media timestamp: " + line, /* cause= */
                null);
            }
            vttTimestampUs = WebvttParserUtil.parseTimestampUs(Assertions.checkNotNull(localTimestampMatcher.group(1)));
            tsTimestampUs = TimestampAdjuster.ptsToUs(Long.parseLong(Assertions.checkNotNull(mediaTimestampMatcher.group(1))));
        }
    }
    // Find the first cue header and parse the start time.
    Matcher cueHeaderMatcher = WebvttParserUtil.findNextCueHeader(webvttData);
    if (cueHeaderMatcher == null) {
        // No cues found. Don't output a sample, but still output a corresponding track.
        buildTrackOutput(0);
        return;
    }
    long firstCueTimeUs = WebvttParserUtil.parseTimestampUs(Assertions.checkNotNull(cueHeaderMatcher.group(1)));
    long sampleTimeUs = timestampAdjuster.adjustTsTimestamp(TimestampAdjuster.usToWrappedPts(firstCueTimeUs + tsTimestampUs - vttTimestampUs));
    long subsampleOffsetUs = sampleTimeUs - firstCueTimeUs;
    // Output the track.
    TrackOutput trackOutput = buildTrackOutput(subsampleOffsetUs);
    // Output the sample.
    sampleDataWrapper.reset(sampleData, sampleSize);
    trackOutput.sampleData(sampleDataWrapper, sampleSize);
    trackOutput.sampleMetadata(sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
}
Also used : ParsableByteArray(com.google.android.exoplayer2.util.ParsableByteArray) Matcher(java.util.regex.Matcher) TrackOutput(com.google.android.exoplayer2.extractor.TrackOutput) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 55 with ParserException

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

the class WebvttDecoder method decode.

@Override
protected Subtitle decode(byte[] bytes, int length, boolean reset) throws SubtitleDecoderException {
    parsableWebvttData.reset(bytes, length);
    List<WebvttCssStyle> definedStyles = new ArrayList<>();
    // Validate the first line of the header, and skip the remainder.
    try {
        WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
    } catch (ParserException e) {
        throw new SubtitleDecoderException(e);
    }
    while (!TextUtils.isEmpty(parsableWebvttData.readLine())) {
    }
    int event;
    List<WebvttCueInfo> cueInfos = new ArrayList<>();
    while ((event = getNextEvent(parsableWebvttData)) != EVENT_END_OF_FILE) {
        if (event == EVENT_COMMENT) {
            skipComment(parsableWebvttData);
        } else if (event == EVENT_STYLE_BLOCK) {
            if (!cueInfos.isEmpty()) {
                throw new SubtitleDecoderException("A style block was found after the first cue.");
            }
            // Consume the "STYLE" header.
            parsableWebvttData.readLine();
            definedStyles.addAll(cssParser.parseBlock(parsableWebvttData));
        } else if (event == EVENT_CUE) {
            @Nullable WebvttCueInfo cueInfo = WebvttCueParser.parseCue(parsableWebvttData, definedStyles);
            if (cueInfo != null) {
                cueInfos.add(cueInfo);
            }
        }
    }
    return new WebvttSubtitle(cueInfos);
}
Also used : ParserException(com.google.android.exoplayer2.ParserException) ArrayList(java.util.ArrayList) Nullable(androidx.annotation.Nullable) SubtitleDecoderException(com.google.android.exoplayer2.text.SubtitleDecoderException)

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