Search in sources :

Example 41 with State

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

the class CastPlayer method updateTracksAndSelections.

/**
 * Updates the internal tracks and selection and returns whether they have changed.
 */
private boolean updateTracksAndSelections() {
    if (remoteMediaClient == null) {
        // There is no session. We leave the state of the player as it is now.
        return false;
    }
    MediaStatus mediaStatus = getMediaStatus();
    MediaInfo mediaInfo = mediaStatus != null ? mediaStatus.getMediaInfo() : null;
    List<MediaTrack> castMediaTracks = mediaInfo != null ? mediaInfo.getMediaTracks() : null;
    if (castMediaTracks == null || castMediaTracks.isEmpty()) {
        boolean hasChanged = !currentTrackGroups.isEmpty();
        currentTrackGroups = TrackGroupArray.EMPTY;
        currentTrackSelection = EMPTY_TRACK_SELECTION_ARRAY;
        return hasChanged;
    }
    long[] activeTrackIds = mediaStatus.getActiveTrackIds();
    if (activeTrackIds == null) {
        activeTrackIds = EMPTY_TRACK_ID_ARRAY;
    }
    TrackGroup[] trackGroups = new TrackGroup[castMediaTracks.size()];
    TrackSelection[] trackSelections = new TrackSelection[RENDERER_COUNT];
    for (int i = 0; i < castMediaTracks.size(); i++) {
        MediaTrack mediaTrack = castMediaTracks.get(i);
        trackGroups[i] = new TrackGroup(CastUtils.mediaTrackToFormat(mediaTrack));
        long id = mediaTrack.getId();
        int trackType = MimeTypes.getTrackType(mediaTrack.getContentType());
        int rendererIndex = getRendererIndexForTrackType(trackType);
        if (isTrackActive(id, activeTrackIds) && rendererIndex != C.INDEX_UNSET && trackSelections[rendererIndex] == null) {
            trackSelections[rendererIndex] = new FixedTrackSelection(trackGroups[i], 0);
        }
    }
    TrackGroupArray newTrackGroups = new TrackGroupArray(trackGroups);
    TrackSelectionArray newTrackSelections = new TrackSelectionArray(trackSelections);
    if (!newTrackGroups.equals(currentTrackGroups) || !newTrackSelections.equals(currentTrackSelection)) {
        currentTrackSelection = new TrackSelectionArray(trackSelections);
        currentTrackGroups = new TrackGroupArray(trackGroups);
        return true;
    }
    return false;
}
Also used : TrackGroupArray(com.google.android.exoplayer2.source.TrackGroupArray) TrackSelectionArray(com.google.android.exoplayer2.trackselection.TrackSelectionArray) MediaTrack(com.google.android.gms.cast.MediaTrack) MediaInfo(com.google.android.gms.cast.MediaInfo) TrackGroup(com.google.android.exoplayer2.source.TrackGroup) TrackSelection(com.google.android.exoplayer2.trackselection.TrackSelection) FixedTrackSelection(com.google.android.exoplayer2.trackselection.FixedTrackSelection) FixedTrackSelection(com.google.android.exoplayer2.trackselection.FixedTrackSelection) MediaStatus(com.google.android.gms.cast.MediaStatus)

Example 42 with State

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

the class ImaUtil method splitAdGroupForPeriod.

private static AdPlaybackState splitAdGroupForPeriod(Object adsId, AdPlaybackState.AdGroup adGroup, long periodStartUs, long periodDurationUs) {
    AdPlaybackState adPlaybackState = new AdPlaybackState(checkNotNull(adsId), /* adGroupTimesUs...= */
    0).withAdCount(/* adGroupIndex= */
    0, /* adCount= */
    1).withAdDurationsUs(/* adGroupIndex= */
    0, periodDurationUs).withIsServerSideInserted(/* adGroupIndex= */
    0, true).withContentResumeOffsetUs(/* adGroupIndex= */
    0, adGroup.contentResumeOffsetUs);
    long periodEndUs = periodStartUs + periodDurationUs;
    long adDurationsUs = 0;
    for (int i = 0; i < adGroup.count; i++) {
        adDurationsUs += adGroup.durationsUs[i];
        if (periodEndUs <= adGroup.timeUs + adDurationsUs + 10_000) {
            // Map the state of the global ad state to the period specific ad state.
            switch(adGroup.states[i]) {
                case AdPlaybackState.AD_STATE_PLAYED:
                    adPlaybackState = adPlaybackState.withPlayedAd(/* adGroupIndex= */
                    0, /* adIndexInAdGroup= */
                    0);
                    break;
                case AdPlaybackState.AD_STATE_SKIPPED:
                    adPlaybackState = adPlaybackState.withSkippedAd(/* adGroupIndex= */
                    0, /* adIndexInAdGroup= */
                    0);
                    break;
                case AdPlaybackState.AD_STATE_ERROR:
                    adPlaybackState = adPlaybackState.withAdLoadError(/* adGroupIndex= */
                    0, /* adIndexInAdGroup= */
                    0);
                    break;
                default:
                    // Do nothing.
                    break;
            }
            break;
        }
    }
    return adPlaybackState;
}
Also used : AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState)

Example 43 with State

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

the class AdTagLoader method addListenerWithAdView.

/**
 * Starts passing events from this instance (including any pending ad playback state) and
 * registers obstructions.
 */
public void addListenerWithAdView(EventListener eventListener, AdViewProvider adViewProvider) {
    boolean isStarted = !eventListeners.isEmpty();
    eventListeners.add(eventListener);
    if (isStarted) {
        if (!AdPlaybackState.NONE.equals(adPlaybackState)) {
            // Pass the existing ad playback state to the new listener.
            eventListener.onAdPlaybackState(adPlaybackState);
        }
        return;
    }
    lastVolumePercent = 0;
    lastAdProgress = VideoProgressUpdate.VIDEO_TIME_NOT_READY;
    lastContentProgress = VideoProgressUpdate.VIDEO_TIME_NOT_READY;
    maybeNotifyPendingAdLoadError();
    if (!AdPlaybackState.NONE.equals(adPlaybackState)) {
        // Pass the ad playback state to the player, and resume ads if necessary.
        eventListener.onAdPlaybackState(adPlaybackState);
    } else if (adsManager != null) {
        adPlaybackState = new AdPlaybackState(adsId, getAdGroupTimesUsForCuePoints(adsManager.getAdCuePoints()));
        updateAdPlaybackState();
    }
    for (AdOverlayInfo overlayInfo : adViewProvider.getAdOverlayInfos()) {
        adDisplayContainer.registerFriendlyObstruction(imaFactory.createFriendlyObstruction(overlayInfo.view, ImaUtil.getFriendlyObstructionPurpose(overlayInfo.purpose), overlayInfo.reasonDetail));
    }
}
Also used : AdOverlayInfo(com.google.android.exoplayer2.ui.AdOverlayInfo) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState)

Example 44 with State

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

the class FlvExtractor method readTagData.

/**
 * Reads the body of a tag from the provided {@link ExtractorInput}.
 *
 * @param input The {@link ExtractorInput} from which to read.
 * @return True if the data was consumed by a reader. False if it was skipped.
 * @throws IOException If an error occurred reading or parsing data from the source.
 */
@RequiresNonNull("extractorOutput")
private boolean readTagData(ExtractorInput input) throws IOException {
    boolean wasConsumed = true;
    boolean wasSampleOutput = false;
    long timestampUs = getCurrentTimestampUs();
    if (tagType == TAG_TYPE_AUDIO && audioReader != null) {
        ensureReadyForMediaOutput();
        wasSampleOutput = audioReader.consume(prepareTagData(input), timestampUs);
    } else if (tagType == TAG_TYPE_VIDEO && videoReader != null) {
        ensureReadyForMediaOutput();
        wasSampleOutput = videoReader.consume(prepareTagData(input), timestampUs);
    } else if (tagType == TAG_TYPE_SCRIPT_DATA && !outputSeekMap) {
        wasSampleOutput = metadataReader.consume(prepareTagData(input), timestampUs);
        long durationUs = metadataReader.getDurationUs();
        if (durationUs != C.TIME_UNSET) {
            extractorOutput.seekMap(new IndexSeekMap(metadataReader.getKeyFrameTagPositions(), metadataReader.getKeyFrameTimesUs(), durationUs));
            outputSeekMap = true;
        }
    } else {
        input.skipFully(tagDataSize);
        wasConsumed = false;
    }
    if (!outputFirstSample && wasSampleOutput) {
        outputFirstSample = true;
        mediaTagTimestampOffsetUs = metadataReader.getDurationUs() == C.TIME_UNSET ? -tagTimestampUs : 0;
    }
    // There's a 4 byte previous tag size before the next header.
    bytesToNextTagHeader = 4;
    state = STATE_SKIPPING_TO_TAG_HEADER;
    return wasConsumed;
}
Also used : IndexSeekMap(com.google.android.exoplayer2.extractor.IndexSeekMap) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 45 with State

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

the class FakeTimeline method createAdPlaybackState.

/**
 * Returns an ad playback state with the specified number of ads in each of the specified ad
 * groups, each ten seconds long.
 *
 * @param adsPerAdGroup The number of ads per ad group.
 * @param adGroupTimesUs The times of ad groups, in microseconds.
 * @return The ad playback state.
 */
public static AdPlaybackState createAdPlaybackState(int adsPerAdGroup, long... adGroupTimesUs) {
    int adGroupCount = adGroupTimesUs.length;
    AdPlaybackState adPlaybackState = new AdPlaybackState(/* adsId= */
    new Object(), adGroupTimesUs);
    long[][] adDurationsUs = new long[adGroupCount][];
    for (int i = 0; i < adGroupCount; i++) {
        adPlaybackState = adPlaybackState.withAdCount(/* adGroupIndex= */
        i, adsPerAdGroup);
        for (int j = 0; j < adsPerAdGroup; j++) {
            adPlaybackState = adPlaybackState.withAdUri(/* adGroupIndex= */
            i, /* adIndexInAdGroup= */
            j, Uri.parse("https://example.com/ad/" + i + "/" + j));
        }
        adDurationsUs[i] = new long[adsPerAdGroup];
        Arrays.fill(adDurationsUs[i], AD_DURATION_US);
    }
    adPlaybackState = adPlaybackState.withAdDurationsUs(adDurationsUs);
    return adPlaybackState;
}
Also used : AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState)

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