Search in sources :

Example 1 with PsExtractor

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

the class PsExtractorSeekTest method handlePendingSeek_handlesSeekingForward_extractsCorrectFrame.

@Test
public void handlePendingSeek_handlesSeekingForward_extractsCorrectFrame() throws IOException {
    PsExtractor extractor = new PsExtractor();
    FakeExtractorOutput extractorOutput = new FakeExtractorOutput();
    SeekMap seekMap = extractSeekMapAndTracks(extractor, extractorOutput);
    FakeTrackOutput trackOutput = extractorOutput.trackOutputs.get(VIDEO_TRACK_ID);
    long firstSeekTimeUs = 987_000;
    seekToTimeUs(extractor, seekMap, firstSeekTimeUs, trackOutput);
    long targetSeekTimeUs = 1_234_000;
    int extractedFrameIndex = seekToTimeUs(extractor, seekMap, targetSeekTimeUs, trackOutput);
    assertThat(extractedFrameIndex).isNotEqualTo(-1);
    assertFirstFrameAfterSeekContainsTargetSeekTime(trackOutput, targetSeekTimeUs, extractedFrameIndex);
}
Also used : FakeTrackOutput(com.google.android.exoplayer2.testutil.FakeTrackOutput) SeekMap(com.google.android.exoplayer2.extractor.SeekMap) FakeExtractorOutput(com.google.android.exoplayer2.testutil.FakeExtractorOutput) Test(org.junit.Test)

Example 2 with PsExtractor

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

the class PsExtractorSeekTest method extractAllSamplesFromFileToExpectedOutput.

private void extractAllSamplesFromFileToExpectedOutput(Context context, String fileName) throws IOException {
    byte[] data = TestUtil.getByteArray(context, fileName);
    PsExtractor extractor = new PsExtractor();
    extractor.init(expectedOutput);
    FakeExtractorInput input = new FakeExtractorInput.Builder().setData(data).build();
    int readResult = Extractor.RESULT_CONTINUE;
    while (readResult != Extractor.RESULT_END_OF_INPUT) {
        readResult = extractor.read(input, positionHolder);
        if (readResult == Extractor.RESULT_SEEK) {
            input.setPosition((int) positionHolder.position);
        }
    }
}
Also used : FakeExtractorInput(com.google.android.exoplayer2.testutil.FakeExtractorInput)

Example 3 with PsExtractor

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

the class PsExtractorSeekTest method handlePendingSeek_handlesRandomSeeks_extractsCorrectFrame.

@Test
public void handlePendingSeek_handlesRandomSeeks_extractsCorrectFrame() throws IOException {
    PsExtractor extractor = new PsExtractor();
    FakeExtractorOutput extractorOutput = new FakeExtractorOutput();
    SeekMap seekMap = extractSeekMapAndTracks(extractor, extractorOutput);
    FakeTrackOutput trackOutput = extractorOutput.trackOutputs.get(VIDEO_TRACK_ID);
    long numSeek = 100;
    for (long i = 0; i < numSeek; i++) {
        long targetSeekTimeUs = random.nextInt(DURATION_US + 1);
        int extractedFrameIndex = seekToTimeUs(extractor, seekMap, targetSeekTimeUs, trackOutput);
        assertThat(extractedFrameIndex).isNotEqualTo(-1);
        assertFirstFrameAfterSeekContainsTargetSeekTime(trackOutput, targetSeekTimeUs, extractedFrameIndex);
    }
}
Also used : FakeTrackOutput(com.google.android.exoplayer2.testutil.FakeTrackOutput) SeekMap(com.google.android.exoplayer2.extractor.SeekMap) FakeExtractorOutput(com.google.android.exoplayer2.testutil.FakeExtractorOutput) Test(org.junit.Test)

Example 4 with PsExtractor

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

the class PsExtractorSeekTest method handlePendingSeek_handlesSeekingToPositionInFile_extractsCorrectFrame.

@Test
public void handlePendingSeek_handlesSeekingToPositionInFile_extractsCorrectFrame() throws IOException {
    PsExtractor extractor = new PsExtractor();
    FakeExtractorOutput extractorOutput = new FakeExtractorOutput();
    SeekMap seekMap = extractSeekMapAndTracks(extractor, extractorOutput);
    FakeTrackOutput trackOutput = extractorOutput.trackOutputs.get(VIDEO_TRACK_ID);
    long targetSeekTimeUs = 987_000;
    int extractedFrameIndex = seekToTimeUs(extractor, seekMap, targetSeekTimeUs, trackOutput);
    assertThat(extractedFrameIndex).isNotEqualTo(-1);
    assertFirstFrameAfterSeekContainsTargetSeekTime(trackOutput, targetSeekTimeUs, extractedFrameIndex);
}
Also used : FakeTrackOutput(com.google.android.exoplayer2.testutil.FakeTrackOutput) SeekMap(com.google.android.exoplayer2.extractor.SeekMap) FakeExtractorOutput(com.google.android.exoplayer2.testutil.FakeExtractorOutput) Test(org.junit.Test)

Example 5 with PsExtractor

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

the class PsExtractorSeekTest method seekToTimeUs.

/**
 * Seeks to the given seek time and keeps reading from input until we can extract at least one
 * frame from the seek position, or until end-of-input is reached.
 *
 * @return The index of the first extracted frame written to the given {@code trackOutput} after
 *     the seek is completed, or -1 if the seek is completed without any extracted frame.
 */
private int seekToTimeUs(PsExtractor psExtractor, SeekMap seekMap, long seekTimeUs, FakeTrackOutput trackOutput) throws IOException {
    int numSampleBeforeSeek = trackOutput.getSampleCount();
    SeekMap.SeekPoints seekPoints = seekMap.getSeekPoints(seekTimeUs);
    long initialSeekLoadPosition = seekPoints.first.position;
    psExtractor.seek(initialSeekLoadPosition, seekTimeUs);
    positionHolder.position = C.POSITION_UNSET;
    ExtractorInput extractorInput = getExtractorInputFromPosition(initialSeekLoadPosition);
    int extractorReadResult = Extractor.RESULT_CONTINUE;
    while (true) {
        try {
            // Keep reading until we can read at least one frame after seek
            while (extractorReadResult == Extractor.RESULT_CONTINUE && trackOutput.getSampleCount() == numSampleBeforeSeek) {
                extractorReadResult = psExtractor.read(extractorInput, positionHolder);
            }
        } finally {
            DataSourceUtil.closeQuietly(dataSource);
        }
        if (extractorReadResult == Extractor.RESULT_SEEK) {
            extractorInput = getExtractorInputFromPosition(positionHolder.position);
            extractorReadResult = Extractor.RESULT_CONTINUE;
        } else if (extractorReadResult == Extractor.RESULT_END_OF_INPUT) {
            return -1;
        } else if (trackOutput.getSampleCount() > numSampleBeforeSeek) {
            // First index after seek = num sample before seek.
            return numSampleBeforeSeek;
        }
    }
}
Also used : ExtractorInput(com.google.android.exoplayer2.extractor.ExtractorInput) FakeExtractorInput(com.google.android.exoplayer2.testutil.FakeExtractorInput) DefaultExtractorInput(com.google.android.exoplayer2.extractor.DefaultExtractorInput) SeekMap(com.google.android.exoplayer2.extractor.SeekMap)

Aggregations

SeekMap (com.google.android.exoplayer2.extractor.SeekMap)8 FakeExtractorOutput (com.google.android.exoplayer2.testutil.FakeExtractorOutput)7 Test (org.junit.Test)7 FakeTrackOutput (com.google.android.exoplayer2.testutil.FakeTrackOutput)6 FakeExtractorInput (com.google.android.exoplayer2.testutil.FakeExtractorInput)4 DefaultExtractorInput (com.google.android.exoplayer2.extractor.DefaultExtractorInput)3 ExtractorInput (com.google.android.exoplayer2.extractor.ExtractorInput)3 Nullable (androidx.annotation.Nullable)1 AmrExtractor (com.google.android.exoplayer2.extractor.amr.AmrExtractor)1 FlacExtractor (com.google.android.exoplayer2.extractor.flac.FlacExtractor)1 FlvExtractor (com.google.android.exoplayer2.extractor.flv.FlvExtractor)1 JpegExtractor (com.google.android.exoplayer2.extractor.jpeg.JpegExtractor)1 MatroskaExtractor (com.google.android.exoplayer2.extractor.mkv.MatroskaExtractor)1 Mp3Extractor (com.google.android.exoplayer2.extractor.mp3.Mp3Extractor)1 FragmentedMp4Extractor (com.google.android.exoplayer2.extractor.mp4.FragmentedMp4Extractor)1 Mp4Extractor (com.google.android.exoplayer2.extractor.mp4.Mp4Extractor)1 OggExtractor (com.google.android.exoplayer2.extractor.ogg.OggExtractor)1 Ac3Extractor (com.google.android.exoplayer2.extractor.ts.Ac3Extractor)1 Ac4Extractor (com.google.android.exoplayer2.extractor.ts.Ac4Extractor)1 AdtsExtractor (com.google.android.exoplayer2.extractor.ts.AdtsExtractor)1