Search in sources :

Example 21 with DrmSession

use of androidx.media3.exoplayer.drm.DrmSession in project media by androidx.

the class SampleQueueTest method allowPlaceholderSessionPopulatesDrmSession.

@Test
public void allowPlaceholderSessionPopulatesDrmSession() {
    when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS);
    DrmSession mockPlaceholderDrmSession = Mockito.mock(DrmSession.class);
    when(mockPlaceholderDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS);
    mockDrmSessionManager.mockPlaceholderDrmSession = mockPlaceholderDrmSession;
    writeTestDataWithEncryptedSections();
    int result = sampleQueue.read(formatHolder, inputBuffer, /* readFlags= */
    0, /* loadingFinished= */
    false);
    assertThat(result).isEqualTo(RESULT_FORMAT_READ);
    assertThat(formatHolder.drmSession).isSameInstanceAs(mockDrmSession);
    assertReadEncryptedSample(/* sampleIndex= */
    0);
    assertReadEncryptedSample(/* sampleIndex= */
    1);
    formatHolder.clear();
    assertThat(formatHolder.drmSession).isNull();
    result = sampleQueue.read(formatHolder, inputBuffer, /* readFlags= */
    0, /* loadingFinished= */
    false);
    assertThat(result).isEqualTo(RESULT_FORMAT_READ);
    assertThat(formatHolder.drmSession).isSameInstanceAs(mockPlaceholderDrmSession);
    assertReadEncryptedSample(/* sampleIndex= */
    2);
    result = sampleQueue.read(formatHolder, inputBuffer, /* readFlags= */
    0, /* loadingFinished= */
    false);
    assertThat(result).isEqualTo(RESULT_FORMAT_READ);
    assertThat(formatHolder.drmSession).isSameInstanceAs(mockDrmSession);
    assertReadEncryptedSample(/* sampleIndex= */
    3);
}
Also used : DrmSession(androidx.media3.exoplayer.drm.DrmSession) Test(org.junit.Test)

Example 22 with DrmSession

use of androidx.media3.exoplayer.drm.DrmSession in project media by androidx.

the class MediaCodecRenderer method render.

@Override
public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
    if (pendingOutputEndOfStream) {
        pendingOutputEndOfStream = false;
        processEndOfStream();
    }
    if (pendingPlaybackException != null) {
        ExoPlaybackException playbackException = pendingPlaybackException;
        pendingPlaybackException = null;
        throw playbackException;
    }
    try {
        if (outputStreamEnded) {
            renderToEndOfStream();
            return;
        }
        if (inputFormat == null && !readSourceOmittingSampleData(FLAG_REQUIRE_FORMAT)) {
            // We still don't have a format and can't make progress without one.
            return;
        }
        // We have a format.
        maybeInitCodecOrBypass();
        if (bypassEnabled) {
            TraceUtil.beginSection("bypassRender");
            while (bypassRender(positionUs, elapsedRealtimeUs)) {
            }
            TraceUtil.endSection();
        } else if (codec != null) {
            long renderStartTimeMs = SystemClock.elapsedRealtime();
            TraceUtil.beginSection("drainAndFeed");
            while (drainOutputBuffer(positionUs, elapsedRealtimeUs) && shouldContinueRendering(renderStartTimeMs)) {
            }
            while (feedInputBuffer() && shouldContinueRendering(renderStartTimeMs)) {
            }
            TraceUtil.endSection();
        } else {
            decoderCounters.skippedInputBufferCount += skipSource(positionUs);
            // We need to read any format changes despite not having a codec so that drmSession can be
            // updated, and so that we have the most recent format should the codec be initialized. We
            // may also reach the end of the stream. FLAG_PEEK is used because we don't want to advance
            // the source further than skipSource has already done.
            readSourceOmittingSampleData(FLAG_PEEK);
        }
        decoderCounters.ensureUpdated();
    } catch (IllegalStateException e) {
        if (isMediaCodecException(e)) {
            onCodecError(e);
            boolean isRecoverable = Util.SDK_INT >= 21 && isRecoverableMediaCodecExceptionV21(e);
            if (isRecoverable) {
                releaseCodec();
            }
            throw createRendererException(createDecoderException(e, getCodecInfo()), inputFormat, isRecoverable, PlaybackException.ERROR_CODE_DECODING_FAILED);
        }
        throw e;
    }
}
Also used : ExoPlaybackException(androidx.media3.exoplayer.ExoPlaybackException)

Example 23 with DrmSession

use of androidx.media3.exoplayer.drm.DrmSession in project media by androidx.

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(androidx.media3.exoplayer.drm.DrmSession.DrmSessionException)

Example 24 with DrmSession

use of androidx.media3.exoplayer.drm.DrmSession in project media by androidx.

the class SampleQueue method onFormatResult.

/**
 * Sets the downstream format, performs DRM resource management, and populates the {@code
 * outputFormatHolder}.
 *
 * @param newFormat The new downstream format.
 * @param outputFormatHolder The output {@link FormatHolder}.
 */
private void onFormatResult(Format newFormat, FormatHolder outputFormatHolder) {
    boolean isFirstFormat = downstreamFormat == null;
    @Nullable DrmInitData oldDrmInitData = isFirstFormat ? null : downstreamFormat.drmInitData;
    downstreamFormat = newFormat;
    @Nullable DrmInitData newDrmInitData = newFormat.drmInitData;
    outputFormatHolder.format = drmSessionManager != null ? newFormat.copyWithCryptoType(drmSessionManager.getCryptoType(newFormat)) : newFormat;
    outputFormatHolder.drmSession = currentDrmSession;
    if (drmSessionManager == null) {
        // This sample queue is not expected to handle DRM. Nothing to do.
        return;
    }
    if (!isFirstFormat && Util.areEqual(oldDrmInitData, newDrmInitData)) {
        // Nothing to do.
        return;
    }
    // Ensure we acquire the new session before releasing the previous one in case the same session
    // is being used for both DrmInitData.
    @Nullable DrmSession previousSession = currentDrmSession;
    currentDrmSession = drmSessionManager.acquireSession(drmEventDispatcher, newFormat);
    outputFormatHolder.drmSession = currentDrmSession;
    if (previousSession != null) {
        previousSession.release(drmEventDispatcher);
    }
}
Also used : DrmInitData(androidx.media3.common.DrmInitData) DrmSession(androidx.media3.exoplayer.drm.DrmSession) Nullable(androidx.annotation.Nullable)

Aggregations

Test (org.junit.Test)18 FakeExoMediaDrm (androidx.media3.test.utils.FakeExoMediaDrm)16 AppManagedProvider (androidx.media3.exoplayer.drm.ExoMediaDrm.AppManagedProvider)7 Nullable (androidx.annotation.Nullable)5 DrmSessionReference (androidx.media3.exoplayer.drm.DrmSessionManager.DrmSessionReference)5 MediaSource (androidx.media3.exoplayer.source.MediaSource)4 DrmInitData (androidx.media3.common.DrmInitData)3 DrmSession (androidx.media3.exoplayer.drm.DrmSession)3 DrmSessionException (androidx.media3.exoplayer.drm.DrmSession.DrmSessionException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Looper (android.os.Looper)2 C (androidx.media3.common.C)2 Format (androidx.media3.common.Format)2 MimeTypes (androidx.media3.common.MimeTypes)2 Assertions.checkNotNull (androidx.media3.common.util.Assertions.checkNotNull)2 Util (androidx.media3.common.util.Util)2 PlayerId (androidx.media3.exoplayer.analytics.PlayerId)2 TestUtil (androidx.media3.test.utils.TestUtil)2 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)2 ImmutableList (com.google.common.collect.ImmutableList)2