Search in sources :

Example 16 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class ServerSideAdInsertionUtilTest method addAdGroupToAdPlaybackState_emptyLeadingAds_markedAsSkipped.

@Test
public void addAdGroupToAdPlaybackState_emptyLeadingAds_markedAsSkipped() {
    AdPlaybackState state = new AdPlaybackState(ADS_ID);
    state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
    0, /* contentResumeOffsetUs= */
    50_000, /* adDurationsUs...= */
    0, 0, 10_000, 40_000, 0);
    AdPlaybackState.AdGroup adGroup = state.getAdGroup(/* adGroupIndex= */
    0);
    assertThat(adGroup.durationsUs[0]).isEqualTo(0);
    assertThat(adGroup.states[0]).isEqualTo(AdPlaybackState.AD_STATE_SKIPPED);
    assertThat(adGroup.durationsUs[1]).isEqualTo(0);
    assertThat(adGroup.states[1]).isEqualTo(AdPlaybackState.AD_STATE_SKIPPED);
    assertThat(adGroup.durationsUs[2]).isEqualTo(10_000);
    assertThat(adGroup.states[2]).isEqualTo(AdPlaybackState.AD_STATE_UNAVAILABLE);
    assertThat(adGroup.durationsUs[4]).isEqualTo(0);
    assertThat(adGroup.states[4]).isEqualTo(AdPlaybackState.AD_STATE_UNAVAILABLE);
    assertThat(stream(adGroup.durationsUs).sum()).isEqualTo(50_000);
}
Also used : ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) Test(org.junit.Test)

Example 17 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class ServerSideAdInsertionUtilTest method getMediaPeriodPositionUsForContent_returnsCorrectPositions.

@Test
public void getMediaPeriodPositionUsForContent_returnsCorrectPositions() {
    // stream: 0-- ad1 --200-- content --2100-- ad2 --2300-- content --4300-- ad3 --4500-- content
    // content timeline: 0 - [ad1] - 100-2000 -[ad2] - 2000-4000 - [ad3] - 4400-end
    AdPlaybackState state = new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
    0, 0, 0, 2000, 4000).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
    2, /* adCount= */
    2).withAdCount(/* adGroupIndex= */
    3, /* adCount= */
    1).withAdCount(/* adGroupIndex= */
    4, /* adCount= */
    3).withContentResumeOffsetUs(/* adGroupIndex= */
    2, /* contentResumeOffsetUs= */
    100).withContentResumeOffsetUs(/* adGroupIndex= */
    3, /* contentResumeOffsetUs= */
    0).withContentResumeOffsetUs(/* adGroupIndex= */
    4, /* contentResumeOffsetUs= */
    400).withAdDurationsUs(/* adGroupIndex= */
    2, /* adDurationsUs...= */
    150, 50).withAdDurationsUs(/* adGroupIndex= */
    3, /* adDurationsUs...= */
    200).withAdDurationsUs(/* adGroupIndex= */
    4, /* adDurationsUs...= */
    50, 50, 100);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    0, /* nextAdGroupIndex= */
    2, state)).isEqualTo(0);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    0, /* nextAdGroupIndex= */
    3, state)).isEqualTo(0);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    0, /* nextAdGroupIndex= */
    C.INDEX_UNSET, state)).isEqualTo(0);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    100, /* nextAdGroupIndex= */
    2, state)).isEqualTo(100);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    100, /* nextAdGroupIndex= */
    3, state)).isEqualTo(0);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    100, /* nextAdGroupIndex= */
    C.INDEX_UNSET, state)).isEqualTo(0);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    200, /* nextAdGroupIndex= */
    3, state)).isEqualTo(100);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    200, /* nextAdGroupIndex= */
    C.INDEX_UNSET, state)).isEqualTo(100);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    2099, /* nextAdGroupIndex= */
    3, state)).isEqualTo(1999);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    2099, /* nextAdGroupIndex= */
    C.INDEX_UNSET, state)).isEqualTo(1999);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    2100, /* nextAdGroupIndex= */
    3, state)).isEqualTo(2000);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    2100, /* nextAdGroupIndex= */
    4, state)).isEqualTo(2000);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    2100, /* nextAdGroupIndex= */
    C.INDEX_UNSET, state)).isEqualTo(2000);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    2300, /* nextAdGroupIndex= */
    3, state)).isEqualTo(2200);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    2300, /* nextAdGroupIndex= */
    4, state)).isEqualTo(2000);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    2300, /* nextAdGroupIndex= */
    C.INDEX_UNSET, state)).isEqualTo(2000);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    4299, /* nextAdGroupIndex= */
    4, state)).isEqualTo(3999);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    4299, /* nextAdGroupIndex= */
    C.INDEX_UNSET, state)).isEqualTo(3999);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    4300, /* nextAdGroupIndex= */
    4, state)).isEqualTo(4000);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    4300, /* nextAdGroupIndex= */
    C.INDEX_UNSET, state)).isEqualTo(4200);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    4500, /* nextAdGroupIndex= */
    4, state)).isEqualTo(4200);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    4500, /* nextAdGroupIndex= */
    C.INDEX_UNSET, state)).isEqualTo(4400);
    assertThat(getMediaPeriodPositionUsForContent(/* positionUs= */
    4700, /* nextAdGroupIndex= */
    C.INDEX_UNSET, state)).isEqualTo(4600);
}
Also used : ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) Test(org.junit.Test)

Example 18 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class ServerSideAdInsertionUtilTest method getMediaPeriodPositionUsForAd_returnsCorrectPositions.

@Test
public void getMediaPeriodPositionUsForAd_returnsCorrectPositions() {
    // stream: 0-- ad1 --200-- content --2100-- ad2 --2300-- content --4300-- ad3 --4500-- content
    // content timeline: 0 - [ad1] - 100-2000 -[ad2] - 2000-4000 - [ad3] - 4400-end
    AdPlaybackState state = new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
    0, 0, 0, 2000, 4000).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
    2, /* adCount= */
    2).withAdCount(/* adGroupIndex= */
    3, /* adCount= */
    1).withAdCount(/* adGroupIndex= */
    4, /* adCount= */
    3).withContentResumeOffsetUs(/* adGroupIndex= */
    2, /* contentResumeOffsetUs= */
    100).withContentResumeOffsetUs(/* adGroupIndex= */
    3, /* contentResumeOffsetUs= */
    0).withContentResumeOffsetUs(/* adGroupIndex= */
    4, /* contentResumeOffsetUs= */
    400).withAdDurationsUs(/* adGroupIndex= */
    2, /* adDurationsUs...= */
    150, 50).withAdDurationsUs(/* adGroupIndex= */
    3, /* adDurationsUs...= */
    200).withAdDurationsUs(/* adGroupIndex= */
    4, /* adDurationsUs...= */
    50, 50, 100);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    0, /* adGroupIndex= */
    2, /* adIndexInAdGroup= */
    0, state)).isEqualTo(0);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    100, /* adGroupIndex= */
    2, /* adIndexInAdGroup= */
    0, state)).isEqualTo(100);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    100, /* adGroupIndex= */
    2, /* adIndexInAdGroup= */
    1, state)).isEqualTo(-50);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    200, /* adGroupIndex= */
    2, /* adIndexInAdGroup= */
    0, state)).isEqualTo(200);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    200, /* adGroupIndex= */
    2, /* adIndexInAdGroup= */
    1, state)).isEqualTo(50);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    300, /* adGroupIndex= */
    2, /* adIndexInAdGroup= */
    1, state)).isEqualTo(150);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    2000, /* adGroupIndex= */
    3, /* adIndexInAdGroup= */
    0, state)).isEqualTo(-100);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    2100, /* adGroupIndex= */
    3, /* adIndexInAdGroup= */
    0, state)).isEqualTo(0);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    2300, /* adGroupIndex= */
    3, /* adIndexInAdGroup= */
    0, state)).isEqualTo(200);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    4300, /* adGroupIndex= */
    4, /* adIndexInAdGroup= */
    0, state)).isEqualTo(0);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    4300, /* adGroupIndex= */
    4, /* adIndexInAdGroup= */
    1, state)).isEqualTo(-50);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    4300, /* adGroupIndex= */
    4, /* adIndexInAdGroup= */
    2, state)).isEqualTo(-100);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    4400, /* adGroupIndex= */
    4, /* adIndexInAdGroup= */
    0, state)).isEqualTo(100);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    4400, /* adGroupIndex= */
    4, /* adIndexInAdGroup= */
    1, state)).isEqualTo(50);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    4400, /* adGroupIndex= */
    4, /* adIndexInAdGroup= */
    2, state)).isEqualTo(0);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    4500, /* adGroupIndex= */
    4, /* adIndexInAdGroup= */
    1, state)).isEqualTo(150);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    4500, /* adGroupIndex= */
    4, /* adIndexInAdGroup= */
    2, state)).isEqualTo(100);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    4700, /* adGroupIndex= */
    4, /* adIndexInAdGroup= */
    1, state)).isEqualTo(350);
    assertThat(getMediaPeriodPositionUsForAd(/* positionUs= */
    4700, /* adGroupIndex= */
    4, /* adIndexInAdGroup= */
    2, state)).isEqualTo(300);
}
Also used : ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) Test(org.junit.Test)

Example 19 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class MpegAudioReader method readHeaderRemainder.

/**
 * Attempts to read the remaining two bytes of the frame header.
 *
 * <p>If a frame header is read in full then the state is changed to {@link #STATE_READING_FRAME},
 * the media format is output if this has not previously occurred, the four header bytes are
 * output as sample data, and the position of the source is advanced to the byte that immediately
 * follows the header.
 *
 * <p>If a frame header is read in full but cannot be parsed then the state is changed to {@link
 * #STATE_READING_HEADER}.
 *
 * <p>If a frame header is not read in full then the position of the source is advanced to the
 * limit, and the method should be called again with the next source to continue the read.
 *
 * @param source The source from which to read.
 */
@RequiresNonNull("output")
private void readHeaderRemainder(ParsableByteArray source) {
    int bytesToRead = min(source.bytesLeft(), HEADER_SIZE - frameBytesRead);
    source.readBytes(headerScratch.getData(), frameBytesRead, bytesToRead);
    frameBytesRead += bytesToRead;
    if (frameBytesRead < HEADER_SIZE) {
        // We haven't read the whole header yet.
        return;
    }
    headerScratch.setPosition(0);
    boolean parsedHeader = header.setForHeaderData(headerScratch.readInt());
    if (!parsedHeader) {
        // We thought we'd located a frame header, but we hadn't.
        frameBytesRead = 0;
        state = STATE_READING_HEADER;
        return;
    }
    frameSize = header.frameSize;
    if (!hasOutputFormat) {
        frameDurationUs = (C.MICROS_PER_SECOND * header.samplesPerFrame) / header.sampleRate;
        Format format = new Format.Builder().setId(formatId).setSampleMimeType(header.mimeType).setMaxInputSize(MpegAudioUtil.MAX_FRAME_SIZE_BYTES).setChannelCount(header.channels).setSampleRate(header.sampleRate).setLanguage(language).build();
        output.format(format);
        hasOutputFormat = true;
    }
    headerScratch.setPosition(0);
    output.sampleData(headerScratch, HEADER_SIZE);
    state = STATE_READING_FRAME;
}
Also used : Format(com.google.android.exoplayer2.Format) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 20 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class StreamReader method readPayload.

@RequiresNonNull({ "trackOutput", "oggSeeker", "extractorOutput" })
private int readPayload(ExtractorInput input, PositionHolder seekPosition) throws IOException {
    long position = oggSeeker.read(input);
    if (position >= 0) {
        seekPosition.position = position;
        return Extractor.RESULT_SEEK;
    } else if (position < -1) {
        onSeekEnd(-(position + 2));
    }
    if (!seekMapSet) {
        SeekMap seekMap = checkStateNotNull(oggSeeker.createSeekMap());
        extractorOutput.seekMap(seekMap);
        seekMapSet = true;
    }
    if (lengthOfReadPacket > 0 || oggPacket.populate(input)) {
        lengthOfReadPacket = 0;
        ParsableByteArray payload = oggPacket.getPayload();
        long granulesInPacket = preparePayload(payload);
        if (granulesInPacket >= 0 && currentGranule + granulesInPacket >= targetGranule) {
            // calculate time and send payload data to codec
            long timeUs = convertGranuleToTime(currentGranule);
            trackOutput.sampleData(payload, payload.limit());
            trackOutput.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, payload.limit(), 0, null);
            targetGranule = -1;
        }
        currentGranule += granulesInPacket;
    } else {
        state = STATE_END_OF_INPUT;
        return Extractor.RESULT_END_OF_INPUT;
    }
    return Extractor.RESULT_CONTINUE;
}
Also used : ParsableByteArray(com.google.android.exoplayer2.util.ParsableByteArray) SeekMap(com.google.android.exoplayer2.extractor.SeekMap) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Aggregations

Test (org.junit.Test)26 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)10 Nullable (androidx.annotation.Nullable)9 ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState)9 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)9 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)8 PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)8 SuppressLint (android.annotation.SuppressLint)7 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)7 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)7 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)7 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)6 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)6 ArrayList (java.util.ArrayList)6 AnalyticsListener (com.google.android.exoplayer2.analytics.AnalyticsListener)5 Listener (com.google.android.exoplayer2.Player.Listener)4 MediaSource (com.google.android.exoplayer2.source.MediaSource)4 TrackSelection (com.google.android.exoplayer2.trackselection.TrackSelection)4 IOException (java.io.IOException)4 Pair (android.util.Pair)3