Search in sources :

Example 21 with FakeExtractorOutput

use of androidx.media3.test.utils.FakeExtractorOutput in project media by androidx.

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(androidx.media3.extractor.PositionHolder)

Example 22 with FakeExtractorOutput

use of androidx.media3.test.utils.FakeExtractorOutput in project media by androidx.

the class TestUtil method extractSeekMap.

/**
 * Reads from the given input using the given {@link Extractor}, until it can produce the {@link
 * SeekMap} and all of the track formats have been identified, or until the extractor encounters
 * EOF.
 *
 * @param extractor The {@link Extractor} to extractor from input.
 * @param output The {@link FakeTrackOutput} to store the extracted {@link SeekMap} and track.
 * @param dataSource The {@link DataSource} that will be used to read from the input.
 * @param uri The Uri of the input.
 * @return The extracted {@link SeekMap}.
 * @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 SeekMap extractSeekMap(Extractor extractor, FakeExtractorOutput output, DataSource dataSource, Uri uri) throws IOException {
    ExtractorInput input = getExtractorInputFromPosition(dataSource, /* position= */
    0, uri);
    extractor.init(output);
    PositionHolder positionHolder = new PositionHolder();
    int readResult = Extractor.RESULT_CONTINUE;
    while (true) {
        try {
            // Keep reading until we get the seek map and the track information.
            while (readResult == Extractor.RESULT_CONTINUE && (output.seekMap == null || !output.tracksEnded)) {
                readResult = extractor.read(input, positionHolder);
            }
            for (int i = 0; i < output.trackOutputs.size(); i++) {
                int trackId = output.trackOutputs.keyAt(i);
                while (readResult == Extractor.RESULT_CONTINUE && output.trackOutputs.get(trackId).lastFormat == null) {
                    readResult = extractor.read(input, positionHolder);
                }
            }
        } finally {
            DataSourceUtil.closeQuietly(dataSource);
        }
        if (readResult == Extractor.RESULT_SEEK) {
            input = getExtractorInputFromPosition(dataSource, positionHolder.position, uri);
            readResult = Extractor.RESULT_CONTINUE;
        } else if (readResult == Extractor.RESULT_END_OF_INPUT) {
            throw new IOException("EOF encountered without seekmap");
        }
        if (output.seekMap != null) {
            return output.seekMap;
        }
    }
}
Also used : ExtractorInput(androidx.media3.extractor.ExtractorInput) DefaultExtractorInput(androidx.media3.extractor.DefaultExtractorInput) PositionHolder(androidx.media3.extractor.PositionHolder) IOException(java.io.IOException)

Example 23 with FakeExtractorOutput

use of androidx.media3.test.utils.FakeExtractorOutput in project media by androidx.

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(androidx.media3.test.utils.FakeExtractorInput.SimulatedIOException) PositionHolder(androidx.media3.extractor.PositionHolder)

Example 24 with FakeExtractorOutput

use of androidx.media3.test.utils.FakeExtractorOutput in project media by androidx.

the class AmrExtractorNonParameterizedTest method setupAmrExtractorWithOutput.

private static AmrExtractor setupAmrExtractorWithOutput() {
    AmrExtractor amrExtractor = new AmrExtractor();
    FakeExtractorOutput output = new FakeExtractorOutput();
    amrExtractor.init(output);
    return amrExtractor;
}
Also used : FakeExtractorOutput(androidx.media3.test.utils.FakeExtractorOutput)

Example 25 with FakeExtractorOutput

use of androidx.media3.test.utils.FakeExtractorOutput in project media by androidx.

the class AmrExtractorSeekTest method seeking_handlesRandomSeeks_extractsCorrectFrames_forWideBandAmr.

@Test
public void seeking_handlesRandomSeeks_extractsCorrectFrames_forWideBandAmr() throws IOException, InterruptedException {
    String fileName = WIDE_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 numSeek = 100;
    for (long i = 0; i < numSeek; i++) {
        long targetSeekTimeUs = random.nextInt(NARROW_BAND_FILE_DURATION_US + 1);
        int extractedFrameIndex = TestUtil.seekToTimeUs(extractor, seekMap, targetSeekTimeUs, dataSource, trackOutput, fileUri);
        assertThat(extractedFrameIndex).isNotEqualTo(-1);
        assertFirstFrameAfterSeekContainTargetSeekTime(trackOutput, targetSeekTimeUs, extractedFrameIndex);
    }
}
Also used : FakeTrackOutput(androidx.media3.test.utils.FakeTrackOutput) SeekMap(androidx.media3.extractor.SeekMap) Uri(android.net.Uri) FakeExtractorOutput(androidx.media3.test.utils.FakeExtractorOutput) Test(org.junit.Test)

Aggregations

FakeExtractorOutput (androidx.media3.test.utils.FakeExtractorOutput)50 Test (org.junit.Test)43 SeekMap (androidx.media3.extractor.SeekMap)34 FakeTrackOutput (androidx.media3.test.utils.FakeTrackOutput)31 Uri (android.net.Uri)25 FakeExtractorInput (androidx.media3.test.utils.FakeExtractorInput)9 PositionHolder (androidx.media3.extractor.PositionHolder)7 WebvttDecoder (androidx.media3.extractor.text.webvtt.WebvttDecoder)6 Before (org.junit.Before)6 Format (androidx.media3.common.Format)4 TimestampAdjuster (androidx.media3.common.util.TimestampAdjuster)4 ExtractorInput (androidx.media3.extractor.ExtractorInput)4 Cue (androidx.media3.common.text.Cue)3 DefaultExtractorInput (androidx.media3.extractor.DefaultExtractorInput)3 IOException (java.io.IOException)2 ParsableByteArray (androidx.media3.common.util.ParsableByteArray)1 TrackOutput (androidx.media3.extractor.TrackOutput)1 TrackIdGenerator (androidx.media3.extractor.ts.TsPayloadReader.TrackIdGenerator)1 SimulatedIOException (androidx.media3.test.utils.FakeExtractorInput.SimulatedIOException)1