Search in sources :

Example 6 with Assertions.checkNotNull

use of com.google.android.exoplayer2.util.Assertions.checkNotNull in project ExoPlayer by google.

the class DownloadService method onStartCommand.

@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
    lastStartId = startId;
    taskRemoved = false;
    @Nullable String intentAction = null;
    @Nullable String contentId = null;
    if (intent != null) {
        intentAction = intent.getAction();
        contentId = intent.getStringExtra(KEY_CONTENT_ID);
        startedInForeground |= intent.getBooleanExtra(KEY_FOREGROUND, false) || ACTION_RESTART.equals(intentAction);
    }
    // intentAction is null if the service is restarted or no action is specified.
    if (intentAction == null) {
        intentAction = ACTION_INIT;
    }
    DownloadManager downloadManager = Assertions.checkNotNull(downloadManagerHelper).downloadManager;
    switch(intentAction) {
        case ACTION_INIT:
        case ACTION_RESTART:
            // Do nothing.
            break;
        case ACTION_ADD_DOWNLOAD:
            @Nullable DownloadRequest downloadRequest = Assertions.checkNotNull(intent).getParcelableExtra(KEY_DOWNLOAD_REQUEST);
            if (downloadRequest == null) {
                Log.e(TAG, "Ignored ADD_DOWNLOAD: Missing " + KEY_DOWNLOAD_REQUEST + " extra");
            } else {
                int stopReason = intent.getIntExtra(KEY_STOP_REASON, Download.STOP_REASON_NONE);
                downloadManager.addDownload(downloadRequest, stopReason);
            }
            break;
        case ACTION_REMOVE_DOWNLOAD:
            if (contentId == null) {
                Log.e(TAG, "Ignored REMOVE_DOWNLOAD: Missing " + KEY_CONTENT_ID + " extra");
            } else {
                downloadManager.removeDownload(contentId);
            }
            break;
        case ACTION_REMOVE_ALL_DOWNLOADS:
            downloadManager.removeAllDownloads();
            break;
        case ACTION_RESUME_DOWNLOADS:
            downloadManager.resumeDownloads();
            break;
        case ACTION_PAUSE_DOWNLOADS:
            downloadManager.pauseDownloads();
            break;
        case ACTION_SET_STOP_REASON:
            if (!Assertions.checkNotNull(intent).hasExtra(KEY_STOP_REASON)) {
                Log.e(TAG, "Ignored SET_STOP_REASON: Missing " + KEY_STOP_REASON + " extra");
            } else {
                int stopReason = intent.getIntExtra(KEY_STOP_REASON, /* defaultValue= */
                0);
                downloadManager.setStopReason(contentId, stopReason);
            }
            break;
        case ACTION_SET_REQUIREMENTS:
            @Nullable Requirements requirements = Assertions.checkNotNull(intent).getParcelableExtra(KEY_REQUIREMENTS);
            if (requirements == null) {
                Log.e(TAG, "Ignored SET_REQUIREMENTS: Missing " + KEY_REQUIREMENTS + " extra");
            } else {
                downloadManager.setRequirements(requirements);
            }
            break;
        default:
            Log.e(TAG, "Ignored unrecognized action: " + intentAction);
            break;
    }
    if (Util.SDK_INT >= 26 && startedInForeground && foregroundNotificationUpdater != null) {
        // From API level 26, services started in the foreground are required to show a notification.
        foregroundNotificationUpdater.showNotificationIfNotAlready();
    }
    isStopped = false;
    if (downloadManager.isIdle()) {
        onIdle();
    }
    return START_STICKY;
}
Also used : Nullable(androidx.annotation.Nullable) Requirements(com.google.android.exoplayer2.scheduler.Requirements)

Example 7 with Assertions.checkNotNull

use of com.google.android.exoplayer2.util.Assertions.checkNotNull in project ExoPlayer by google.

the class SegmentDownloader method mergeSegments.

private static void mergeSegments(List<Segment> segments, CacheKeyFactory keyFactory) {
    HashMap<String, Integer> lastIndexByCacheKey = new HashMap<>();
    int nextOutIndex = 0;
    for (int i = 0; i < segments.size(); i++) {
        Segment segment = segments.get(i);
        String cacheKey = keyFactory.buildCacheKey(segment.dataSpec);
        @Nullable Integer lastIndex = lastIndexByCacheKey.get(cacheKey);
        @Nullable Segment lastSegment = lastIndex == null ? null : segments.get(lastIndex);
        if (lastSegment == null || segment.startTimeUs > lastSegment.startTimeUs + MAX_MERGED_SEGMENT_START_TIME_DIFF_US || !canMergeSegments(lastSegment.dataSpec, segment.dataSpec)) {
            lastIndexByCacheKey.put(cacheKey, nextOutIndex);
            segments.set(nextOutIndex, segment);
            nextOutIndex++;
        } else {
            long mergedLength = segment.dataSpec.length == C.LENGTH_UNSET ? C.LENGTH_UNSET : lastSegment.dataSpec.length + segment.dataSpec.length;
            DataSpec mergedDataSpec = lastSegment.dataSpec.subrange(/* offset= */
            0, mergedLength);
            segments.set(Assertions.checkNotNull(lastIndex), new Segment(lastSegment.startTimeUs, mergedDataSpec));
        }
    }
    Util.removeRange(segments, /* fromIndex= */
    nextOutIndex, /* toIndex= */
    segments.size());
}
Also used : HashMap(java.util.HashMap) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Nullable(androidx.annotation.Nullable)

Example 8 with Assertions.checkNotNull

use of com.google.android.exoplayer2.util.Assertions.checkNotNull in project ExoPlayer by google.

the class DecoderAudioRenderer method onInputFormatChanged.

private void onInputFormatChanged(FormatHolder formatHolder) throws ExoPlaybackException {
    Format newFormat = Assertions.checkNotNull(formatHolder.format);
    setSourceDrmSession(formatHolder.drmSession);
    Format oldFormat = inputFormat;
    inputFormat = newFormat;
    encoderDelay = newFormat.encoderDelay;
    encoderPadding = newFormat.encoderPadding;
    if (decoder == null) {
        maybeInitDecoder();
        eventDispatcher.inputFormatChanged(inputFormat, /* decoderReuseEvaluation= */
        null);
        return;
    }
    DecoderReuseEvaluation evaluation;
    if (sourceDrmSession != decoderDrmSession) {
        evaluation = new DecoderReuseEvaluation(decoder.getName(), oldFormat, newFormat, REUSE_RESULT_NO, DISCARD_REASON_DRM_SESSION_CHANGED);
    } else {
        evaluation = canReuseDecoder(decoder.getName(), oldFormat, newFormat);
    }
    if (evaluation.result == REUSE_RESULT_NO) {
        if (decoderReceivedBuffers) {
            // Signal end of stream and wait for any final output buffers before re-initialization.
            decoderReinitializationState = REINITIALIZATION_STATE_SIGNAL_END_OF_STREAM;
        } else {
            // There aren't any final output buffers, so release the decoder immediately.
            releaseDecoder();
            maybeInitDecoder();
            audioTrackNeedsConfigure = true;
        }
    }
    eventDispatcher.inputFormatChanged(inputFormat, evaluation);
}
Also used : Format(com.google.android.exoplayer2.Format) DecoderReuseEvaluation(com.google.android.exoplayer2.decoder.DecoderReuseEvaluation)

Example 9 with Assertions.checkNotNull

use of com.google.android.exoplayer2.util.Assertions.checkNotNull in project ExoPlayer by google.

the class DefaultPlaybackSessionManager method updateSessionsWithTimelineChange.

@Override
public synchronized void updateSessionsWithTimelineChange(EventTime eventTime) {
    Assertions.checkNotNull(listener);
    Timeline previousTimeline = currentTimeline;
    currentTimeline = eventTime.timeline;
    Iterator<SessionDescriptor> iterator = sessions.values().iterator();
    while (iterator.hasNext()) {
        SessionDescriptor session = iterator.next();
        if (!session.tryResolvingToNewTimeline(previousTimeline, currentTimeline) || session.isFinishedAtEventTime(eventTime)) {
            iterator.remove();
            if (session.isCreated) {
                if (session.sessionId.equals(currentSessionId)) {
                    currentSessionId = null;
                }
                listener.onSessionFinished(eventTime, session.sessionId, /* automaticTransitionToNextPlayback= */
                false);
            }
        }
    }
    updateCurrentSession(eventTime);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline)

Example 10 with Assertions.checkNotNull

use of com.google.android.exoplayer2.util.Assertions.checkNotNull in project ExoPlayer by google.

the class OfflineLicenseHelper method getLicenseDurationRemainingSec.

/**
 * Returns the remaining license and playback durations in seconds, for an offline license.
 *
 * @param offlineLicenseKeySetId The key set id of the license.
 * @return The remaining license and playback durations, in seconds.
 * @throws DrmSessionException Thrown when a DRM session error occurs.
 */
public synchronized Pair<Long, Long> getLicenseDurationRemainingSec(byte[] offlineLicenseKeySetId) throws DrmSessionException {
    Assertions.checkNotNull(offlineLicenseKeySetId);
    drmSessionManager.setPlayer(handlerThread.getLooper(), PlayerId.UNSET);
    drmSessionManager.prepare();
    DrmSession drmSession = openBlockingKeyRequest(DefaultDrmSessionManager.MODE_QUERY, offlineLicenseKeySetId, FORMAT_WITH_EMPTY_DRM_INIT_DATA);
    DrmSessionException error = drmSession.getError();
    Pair<Long, Long> licenseDurationRemainingSec = WidevineUtil.getLicenseDurationRemainingSec(drmSession);
    drmSession.release(eventDispatcher);
    drmSessionManager.release();
    if (error != null) {
        if (error.getCause() instanceof KeysExpiredException) {
            return Pair.create(0L, 0L);
        }
        throw error;
    }
    return Assertions.checkNotNull(licenseDurationRemainingSec);
}
Also used : DrmSessionException(com.google.android.exoplayer2.drm.DrmSession.DrmSessionException)

Aggregations

Nullable (androidx.annotation.Nullable)28 Format (com.google.android.exoplayer2.Format)10 ArrayList (java.util.ArrayList)9 MediaItem (com.google.android.exoplayer2.MediaItem)8 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)5 Matcher (java.util.regex.Matcher)5 SuppressLint (android.annotation.SuppressLint)4 Context (android.content.Context)4 Uri (android.net.Uri)4 MediaSource (com.google.android.exoplayer2.source.MediaSource)4 DataSource (com.google.android.exoplayer2.upstream.DataSource)4 StatsDataSource (com.google.android.exoplayer2.upstream.StatsDataSource)4 Activity (android.app.Activity)3 Intent (android.content.Intent)3 SQLException (android.database.SQLException)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 Bundle (android.os.Bundle)3 CallbackMediaItem (androidx.media2.common.CallbackMediaItem)3 C (com.google.android.exoplayer2.C)3 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)3