Search in sources :

Example 11 with SeekPoints

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

the class XingSeekerTest method getSeekPointsAtStartOfStream.

@Test
public void getSeekPointsAtStartOfStream() {
    SeekPoints seekPoints = seeker.getSeekPoints(0);
    SeekPoint seekPoint = seekPoints.first;
    assertThat(seekPoint).isEqualTo(seekPoints.second);
    assertThat(seekPoint.timeUs).isEqualTo(0);
    assertThat(seekPoint.position).isEqualTo(XING_FRAME_POSITION + xingFrameSize);
}
Also used : SeekPoint(com.google.android.exoplayer2.extractor.SeekPoint) SeekPoints(com.google.android.exoplayer2.extractor.SeekMap.SeekPoints) Test(org.junit.Test)

Example 12 with SeekPoints

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

the class XingSeekerTest method getSeekPointsAtEndOfStream.

@Test
public void getSeekPointsAtEndOfStream() {
    SeekPoints seekPoints = seeker.getSeekPoints(STREAM_DURATION_US);
    SeekPoint seekPoint = seekPoints.first;
    assertThat(seekPoint).isEqualTo(seekPoints.second);
    assertThat(seekPoint.timeUs).isEqualTo(STREAM_DURATION_US);
    assertThat(seekPoint.position).isEqualTo(STREAM_LENGTH - 1);
}
Also used : SeekPoint(com.google.android.exoplayer2.extractor.SeekPoint) SeekPoints(com.google.android.exoplayer2.extractor.SeekMap.SeekPoints) Test(org.junit.Test)

Example 13 with SeekPoints

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

the class XingSeekerTest method getTimeForAllPositions.

@Test
public void getTimeForAllPositions() {
    for (int offset = xingFrameSize; offset < DATA_SIZE_BYTES; offset++) {
        int position = XING_FRAME_POSITION + offset;
        // Test seeker.
        long timeUs = seeker.getTimeUs(position);
        SeekPoints seekPoints = seeker.getSeekPoints(timeUs);
        SeekPoint seekPoint = seekPoints.first;
        assertThat(seekPoint).isEqualTo(seekPoints.second);
        assertThat(seekPoint.position).isEqualTo(position);
        // Test seekerWithInputLength.
        timeUs = seekerWithInputLength.getTimeUs(position);
        seekPoints = seekerWithInputLength.getSeekPoints(timeUs);
        seekPoint = seekPoints.first;
        assertThat(seekPoint).isEqualTo(seekPoints.second);
        assertThat(seekPoint.position).isEqualTo(position);
    }
}
Also used : SeekPoint(com.google.android.exoplayer2.extractor.SeekPoint) SeekPoints(com.google.android.exoplayer2.extractor.SeekMap.SeekPoints) SeekPoint(com.google.android.exoplayer2.extractor.SeekPoint) Test(org.junit.Test)

Example 14 with SeekPoints

use of com.google.android.exoplayer2.extractor.SeekMap.SeekPoints 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 15 with SeekPoints

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

the class Mp4Extractor method getSeekPoints.

@Override
public SeekPoints getSeekPoints(long timeUs) {
    if (checkNotNull(tracks).length == 0) {
        return new SeekPoints(SeekPoint.START);
    }
    long firstTimeUs;
    long firstOffset;
    long secondTimeUs = C.TIME_UNSET;
    long secondOffset = C.POSITION_UNSET;
    // If we have a video track, use it to establish one or two seek points.
    if (firstVideoTrackIndex != C.INDEX_UNSET) {
        TrackSampleTable sampleTable = tracks[firstVideoTrackIndex].sampleTable;
        int sampleIndex = getSynchronizationSampleIndex(sampleTable, timeUs);
        if (sampleIndex == C.INDEX_UNSET) {
            return new SeekPoints(SeekPoint.START);
        }
        long sampleTimeUs = sampleTable.timestampsUs[sampleIndex];
        firstTimeUs = sampleTimeUs;
        firstOffset = sampleTable.offsets[sampleIndex];
        if (sampleTimeUs < timeUs && sampleIndex < sampleTable.sampleCount - 1) {
            int secondSampleIndex = sampleTable.getIndexOfLaterOrEqualSynchronizationSample(timeUs);
            if (secondSampleIndex != C.INDEX_UNSET && secondSampleIndex != sampleIndex) {
                secondTimeUs = sampleTable.timestampsUs[secondSampleIndex];
                secondOffset = sampleTable.offsets[secondSampleIndex];
            }
        }
    } else {
        firstTimeUs = timeUs;
        firstOffset = Long.MAX_VALUE;
    }
    // Take into account other tracks.
    for (int i = 0; i < tracks.length; i++) {
        if (i != firstVideoTrackIndex) {
            TrackSampleTable sampleTable = tracks[i].sampleTable;
            firstOffset = maybeAdjustSeekOffset(sampleTable, firstTimeUs, firstOffset);
            if (secondTimeUs != C.TIME_UNSET) {
                secondOffset = maybeAdjustSeekOffset(sampleTable, secondTimeUs, secondOffset);
            }
        }
    }
    SeekPoint firstSeekPoint = new SeekPoint(firstTimeUs, firstOffset);
    if (secondTimeUs == C.TIME_UNSET) {
        return new SeekPoints(firstSeekPoint);
    } else {
        SeekPoint secondSeekPoint = new SeekPoint(secondTimeUs, secondOffset);
        return new SeekPoints(firstSeekPoint, secondSeekPoint);
    }
}
Also used : SeekPoint(com.google.android.exoplayer2.extractor.SeekPoint) SeekPoint(com.google.android.exoplayer2.extractor.SeekPoint)

Aggregations

SeekPoint (com.google.android.exoplayer2.extractor.SeekPoint)17 Test (org.junit.Test)6 SeekPoints (com.google.android.exoplayer2.extractor.SeekMap.SeekPoints)5 SeekPoints (androidx.media3.extractor.SeekMap.SeekPoints)4 Nullable (androidx.annotation.Nullable)3 SeekPoint (androidx.media3.extractor.SeekPoint)3 SeekMap (com.google.android.exoplayer2.extractor.SeekMap)3 DefaultExtractorInput (com.google.android.exoplayer2.extractor.DefaultExtractorInput)2 ExtractorInput (com.google.android.exoplayer2.extractor.ExtractorInput)2 PositionHolder (com.google.android.exoplayer2.extractor.PositionHolder)1 FakeExtractorInput (com.google.android.exoplayer2.testutil.FakeExtractorInput)1