Search in sources :

Example 51 with Extractor

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

the class AmrExtractorSeekTest method seeking_handlesSeekingToPositionInFile_extractsCorrectFrame_forNarrowBandAmr.

@Test
public void seeking_handlesSeekingToPositionInFile_extractsCorrectFrame_forNarrowBandAmr() throws IOException {
    String fileName = NARROW_BAND_AMR_FILE;
    Uri fileUri = TestUtil.buildAssetUri(fileName);
    expectedTrackOutput = TestUtil.extractAllSamplesFromFile(createAmrExtractor(), ApplicationProvider.getApplicationContext(), fileName).trackOutputs.get(0);
    AmrExtractor extractor = createAmrExtractor();
    FakeExtractorOutput extractorOutput = new FakeExtractorOutput();
    SeekMap seekMap = TestUtil.extractSeekMap(extractor, extractorOutput, dataSource, fileUri);
    FakeTrackOutput trackOutput = extractorOutput.trackOutputs.get(0);
    long targetSeekTimeUs = 980_000;
    int extractedFrameIndex = TestUtil.seekToTimeUs(extractor, seekMap, targetSeekTimeUs, dataSource, trackOutput, fileUri);
    assertThat(extractedFrameIndex).isNotEqualTo(-1);
    assertFirstFrameAfterSeekContainTargetSeekTime(trackOutput, targetSeekTimeUs, extractedFrameIndex);
}
Also used : FakeTrackOutput(com.google.android.exoplayer2.testutil.FakeTrackOutput) SeekMap(com.google.android.exoplayer2.extractor.SeekMap) Uri(android.net.Uri) FakeExtractorOutput(com.google.android.exoplayer2.testutil.FakeExtractorOutput) Test(org.junit.Test)

Example 52 with Extractor

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

the class AmrExtractorSeekTest method seeking_handlesSeekingBackward_extractsCorrectFrames_forNarrowBandAmr.

@Test
public void seeking_handlesSeekingBackward_extractsCorrectFrames_forNarrowBandAmr() throws IOException {
    String fileName = NARROW_BAND_AMR_FILE;
    Uri fileUri = TestUtil.buildAssetUri(fileName);
    expectedTrackOutput = TestUtil.extractAllSamplesFromFile(createAmrExtractor(), ApplicationProvider.getApplicationContext(), fileName).trackOutputs.get(0);
    AmrExtractor extractor = createAmrExtractor();
    FakeExtractorOutput extractorOutput = new FakeExtractorOutput();
    SeekMap seekMap = TestUtil.extractSeekMap(extractor, extractorOutput, dataSource, fileUri);
    FakeTrackOutput trackOutput = extractorOutput.trackOutputs.get(0);
    long firstSeekTimeUs = 980_000;
    TestUtil.seekToTimeUs(extractor, seekMap, firstSeekTimeUs, dataSource, trackOutput, fileUri);
    long targetSeekTimeUs = 0;
    int extractedFrameIndex = TestUtil.seekToTimeUs(extractor, seekMap, targetSeekTimeUs, dataSource, trackOutput, fileUri);
    assertThat(extractedFrameIndex).isNotEqualTo(-1);
    assertFirstFrameAfterSeekContainTargetSeekTime(trackOutput, targetSeekTimeUs, extractedFrameIndex);
}
Also used : FakeTrackOutput(com.google.android.exoplayer2.testutil.FakeTrackOutput) SeekMap(com.google.android.exoplayer2.extractor.SeekMap) Uri(android.net.Uri) FakeExtractorOutput(com.google.android.exoplayer2.testutil.FakeExtractorOutput) Test(org.junit.Test)

Example 53 with Extractor

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

the class TestUtil method seekToTimeUs.

/**
 * Seeks to the given seek time of the stream from the given input, and keeps reading from the
 * input until we can extract at least one sample following the seek position, or until
 * end-of-input is reached.
 *
 * @param extractor The {@link Extractor} to extract from input.
 * @param seekMap The {@link SeekMap} of the stream from the given input.
 * @param seekTimeUs The seek time, in micro-seconds.
 * @param trackOutput The {@link FakeTrackOutput} to store the extracted samples.
 * @param dataSource The {@link DataSource} that will be used to read from the input.
 * @param uri The Uri of the input.
 * @return The index of the first extracted sample written to the given {@code trackOutput} after
 *     the seek is completed, or {@link C#INDEX_UNSET} if the seek is completed without any
 *     extracted sample.
 */
public static int seekToTimeUs(Extractor extractor, SeekMap seekMap, long seekTimeUs, DataSource dataSource, FakeTrackOutput trackOutput, Uri uri) throws IOException {
    int numSampleBeforeSeek = trackOutput.getSampleCount();
    SeekMap.SeekPoints seekPoints = seekMap.getSeekPoints(seekTimeUs);
    long initialSeekLoadPosition = seekPoints.first.position;
    extractor.seek(initialSeekLoadPosition, seekTimeUs);
    PositionHolder positionHolder = new PositionHolder();
    positionHolder.position = C.POSITION_UNSET;
    ExtractorInput extractorInput = TestUtil.getExtractorInputFromPosition(dataSource, initialSeekLoadPosition, uri);
    int extractorReadResult = Extractor.RESULT_CONTINUE;
    while (true) {
        try {
            // Keep reading until we can read at least one sample after seek
            while (extractorReadResult == Extractor.RESULT_CONTINUE && trackOutput.getSampleCount() == numSampleBeforeSeek) {
                extractorReadResult = extractor.read(extractorInput, positionHolder);
            }
        } finally {
            DataSourceUtil.closeQuietly(dataSource);
        }
        if (extractorReadResult == Extractor.RESULT_SEEK) {
            extractorInput = TestUtil.getExtractorInputFromPosition(dataSource, positionHolder.position, uri);
            extractorReadResult = Extractor.RESULT_CONTINUE;
        } else if (extractorReadResult == Extractor.RESULT_END_OF_INPUT && trackOutput.getSampleCount() == numSampleBeforeSeek) {
            return C.INDEX_UNSET;
        } else if (trackOutput.getSampleCount() > numSampleBeforeSeek) {
            // First index after seek = num sample before seek.
            return numSampleBeforeSeek;
        }
    }
}
Also used : ExtractorInput(com.google.android.exoplayer2.extractor.ExtractorInput) DefaultExtractorInput(com.google.android.exoplayer2.extractor.DefaultExtractorInput) PositionHolder(com.google.android.exoplayer2.extractor.PositionHolder) SeekMap(com.google.android.exoplayer2.extractor.SeekMap)

Example 54 with Extractor

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

the class TestUtil method extractAllSamplesFromFile.

/**
 * Extracts all samples from the given file into a {@link FakeTrackOutput}.
 *
 * @param extractor The {@link Extractor} to extractor from input.
 * @param context A {@link Context}.
 * @param fileName The name of the input file.
 * @return The {@link FakeTrackOutput} containing the extracted samples.
 * @throws IOException If an error occurred reading from the input, or if the extractor finishes
 *     reading from input without extracting any {@link SeekMap}.
 */
public static FakeExtractorOutput extractAllSamplesFromFile(Extractor extractor, Context context, String fileName) throws IOException {
    byte[] data = TestUtil.getByteArray(context, fileName);
    FakeExtractorOutput expectedOutput = new FakeExtractorOutput();
    extractor.init(expectedOutput);
    FakeExtractorInput input = new FakeExtractorInput.Builder().setData(data).build();
    PositionHolder positionHolder = new PositionHolder();
    int readResult = Extractor.RESULT_CONTINUE;
    while (readResult != Extractor.RESULT_END_OF_INPUT) {
        while (readResult == Extractor.RESULT_CONTINUE) {
            readResult = extractor.read(input, positionHolder);
        }
        if (readResult == Extractor.RESULT_SEEK) {
            input.setPosition((int) positionHolder.position);
            readResult = Extractor.RESULT_CONTINUE;
        }
    }
    return expectedOutput;
}
Also used : PositionHolder(com.google.android.exoplayer2.extractor.PositionHolder)

Example 55 with Extractor

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

the class ExtractorAsserts method consumeTestData.

private static void consumeTestData(Extractor extractor, FakeExtractorInput input, long timeUs, FakeExtractorOutput output, boolean retryFromStartIfLive) throws IOException {
    extractor.seek(input.getPosition(), timeUs);
    PositionHolder seekPositionHolder = new PositionHolder();
    int readResult = Extractor.RESULT_CONTINUE;
    while (readResult != Extractor.RESULT_END_OF_INPUT) {
        try {
            // Extractor.read should not read seekPositionHolder.position. Set it to a value that's
            // likely to cause test failure if a read does occur.
            seekPositionHolder.position = Long.MIN_VALUE;
            readResult = extractor.read(input, seekPositionHolder);
            if (readResult == Extractor.RESULT_SEEK) {
                long seekPosition = seekPositionHolder.position;
                checkState(0 <= seekPosition && seekPosition <= Integer.MAX_VALUE);
                input.setPosition((int) seekPosition);
            }
        } catch (SimulatedIOException e) {
            if (!retryFromStartIfLive) {
                continue;
            }
            boolean isOnDemand = input.getLength() != C.LENGTH_UNSET || (output.seekMap != null && output.seekMap.getDurationUs() != C.TIME_UNSET);
            if (isOnDemand) {
                continue;
            }
            input.setPosition(0);
            for (int i = 0; i < output.numberOfTracks; i++) {
                output.trackOutputs.valueAt(i).clear();
            }
            extractor.seek(0, 0);
        }
    }
}
Also used : SimulatedIOException(com.google.android.exoplayer2.testutil.FakeExtractorInput.SimulatedIOException) PositionHolder(com.google.android.exoplayer2.extractor.PositionHolder)

Aggregations

Test (org.junit.Test)87 SeekMap (com.google.android.exoplayer2.extractor.SeekMap)74 Uri (android.net.Uri)66 FakeTrackOutput (com.google.android.exoplayer2.testutil.FakeTrackOutput)59 FakeExtractorOutput (com.google.android.exoplayer2.testutil.FakeExtractorOutput)44 ExtractorInput (com.google.android.exoplayer2.extractor.ExtractorInput)14 FakeExtractorInput (com.google.android.exoplayer2.testutil.FakeExtractorInput)12 DefaultExtractorInput (com.google.android.exoplayer2.extractor.DefaultExtractorInput)11 Extractor (com.google.android.exoplayer2.extractor.Extractor)11 Nullable (androidx.annotation.Nullable)9 FragmentedMp4Extractor (com.google.android.exoplayer2.extractor.mp4.FragmentedMp4Extractor)9 Mp3Extractor (com.google.android.exoplayer2.extractor.mp3.Mp3Extractor)8 WebvttDecoder (com.google.android.exoplayer2.text.webvtt.WebvttDecoder)8 Format (com.google.android.exoplayer2.Format)7 PositionHolder (com.google.android.exoplayer2.extractor.PositionHolder)7 Ac3Extractor (com.google.android.exoplayer2.extractor.ts.Ac3Extractor)7 AdtsExtractor (com.google.android.exoplayer2.extractor.ts.AdtsExtractor)7 TsExtractor (com.google.android.exoplayer2.extractor.ts.TsExtractor)7 ExtractorsFactory (com.google.android.exoplayer2.extractor.ExtractorsFactory)5 MatroskaExtractor (com.google.android.exoplayer2.extractor.mkv.MatroskaExtractor)5