androidx / media

Jetpack Media3 support libraries for media use cases, including ExoPlayer, an extensible media player for Android
https://developer.android.com/media/media3
Apache License 2.0
1.51k stars 359 forks source link

DefaultDrmSession should handle when key is already available (getKeyRequest() -> REQUEST_TYPE_NONE) #1404

Open boom1 opened 3 months ago

boom1 commented 3 months ago

Use case description

When a DRM-protected (e.g. PlayReady) media file is played back for the second time, the DRM key could still be available in the DRM. Then the DRM might return REQUEST_TYPE_NONE on getKeyRequest() (see https://developer.android.com/reference/android/media/MediaDrm.KeyRequest#REQUEST_TYPE_NONE ). DefaultDrmSession should handle this case.

Proposed solution

The proposed solution is to check the return value of getKeyRequest() and to start playback immediately, when the key is already available. See this patch:

diff --git a/exoplayer/library/core/src/main/java/com/google/android/exoplayer2/drm/DefaultDrmSession.java b/exoplayer/library/core/src/main/java/com/google/android/exoplayer2/drm/DefaultDrmSession.java
index b357a2ed06..2c93597bdf 100644
--- a/exoplayer/library/core/src/main/java/com/google/android/exoplayer2/drm/DefaultDrmSession.java
+++ b/exoplayer/library/core/src/main/java/com/google/android/exoplayer2/drm/DefaultDrmSession.java
@@ -20,6 +20,7 @@ import static com.google.android.exoplayer2.util.Assertions.checkStateNotNull;
 import static java.lang.Math.min;

 import android.annotation.SuppressLint;
+import android.media.MediaDrm;
 import android.media.NotProvisionedException;
 import android.os.Handler;
 import android.os.HandlerThread;
@@ -500,6 +501,11 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
   private void postKeyRequest(byte[] scope, int type, boolean allowRetry) {
     try {
       currentKeyRequest = mediaDrm.getKeyRequest(scope, schemeDatas, type, keyRequestParameters);
+      if (currentKeyRequest.getRequestType() == KeyRequest.REQUEST_TYPE_NONE) {
+        state = STATE_OPENED_WITH_KEYS;
+        dispatchEvent(DrmSessionEventListener.EventDispatcher::drmKeysLoaded);
+        return;
+      }
       Util.castNonNull(requestHandler)
           .post(MSG_KEYS, Assertions.checkNotNull(currentKeyRequest), allowRetry);
     } catch (Exception e) {

Alternatives considered

The check for REQUEST_TYPE_NONE could be done in requestHandler.post() or below, but then additional changes are necessary; especially some kind of null or empty keyResponse would need to be considered as OK/key-already-available. (IMHO not better.)

icbaker commented 2 months ago

Thanks for the request - do you have some content which triggers this key request type that you could share with us for testing/debugging? Please either post media and license server links here or send them to android-media-github@google.com with the subject Issue #1404. Please also update this issue to indicate you’ve done this.

boom1 commented 2 months ago

Hello Ian, A test would be to play the same DRM-protected media file twice. On the second run, getKeyRequest() might return REQUEST_TYPE_NONE. I added some more info to android-media-github@google.com.

icbaker commented 2 months ago

Thanks for the email, unfortunately it's not really detailed enough for us to take action (we need literal media & license URLs we can use to repro).

A test would be to play the same DRM-protected media file twice. On the second run, getKeyRequest() might return REQUEST_TYPE_NONE.

I tried this in the demo app with the Widevine DASH (MP4, H264) -> HD (cenc) sample, but every time I played it I saw REQUEST_TYPE_INITIAL (0) emitted from the MediaDrm instance, so I'm afraid it didn't help to repro the behaviour.

boom1 commented 2 months ago

I had hoped that it is sufficient that REQUEST_TYPE_NONE is part of the official Google API for MediaDrm. IMHO ExoPlayer should handle all allowed return codes correctly. Doing so wouldn't harm for any current DRM (unless it wrongly returns REQUEST_TYPE_NONE although it means REQUEST_TYPE_INITIAL, which at least Widevine doesn't do). What else can I do; would a +1 from the platform provider help?