From 255523b4323b94b2e9fa0873f80b2f5873192cc5 Mon Sep 17 00:00:00 2001 From: olly Date: Thu, 23 Mar 2017 08:01:41 -0700 Subject: [PATCH] Call DrmSession methods on the session not the manager The way it was before worked, but it's not really documented that DrmSession methods implemented by DefaultDrmSessionManager can be called after the session has been released, and it feels wrong to do this. At some point we should probably consider moving the DrmSession part of DefaultDrmSessionManager into an inner class, to force usage in the "normal" way. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=151003855 --- .../exoplayer2/drm/OfflineLicenseHelper.java | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/drm/OfflineLicenseHelper.java b/library/core/src/main/java/com/google/android/exoplayer2/drm/OfflineLicenseHelper.java index 93f3b396c8..040ca50c76 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/drm/OfflineLicenseHelper.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/drm/OfflineLicenseHelper.java @@ -133,8 +133,7 @@ public final class OfflineLicenseHelper { public synchronized byte[] downloadLicense(DrmInitData drmInitData) throws IOException, InterruptedException, DrmSessionException { Assertions.checkArgument(drmInitData != null); - blockingKeyRequest(DefaultDrmSessionManager.MODE_DOWNLOAD, null, drmInitData); - return drmSessionManager.getOfflineLicenseKeySetId(); + return blockingKeyRequest(DefaultDrmSessionManager.MODE_DOWNLOAD, null, drmInitData); } /** @@ -147,8 +146,7 @@ public final class OfflineLicenseHelper { public synchronized byte[] renewLicense(byte[] offlineLicenseKeySetId) throws DrmSessionException { Assertions.checkNotNull(offlineLicenseKeySetId); - blockingKeyRequest(DefaultDrmSessionManager.MODE_DOWNLOAD, offlineLicenseKeySetId, null); - return drmSessionManager.getOfflineLicenseKeySetId(); + return blockingKeyRequest(DefaultDrmSessionManager.MODE_DOWNLOAD, offlineLicenseKeySetId, null); } /** @@ -173,34 +171,43 @@ public final class OfflineLicenseHelper { public synchronized Pair getLicenseDurationRemainingSec(byte[] offlineLicenseKeySetId) throws DrmSessionException { Assertions.checkNotNull(offlineLicenseKeySetId); - DrmSession session = openBlockingKeyRequest(DefaultDrmSessionManager.MODE_QUERY, + DrmSession drmSession = openBlockingKeyRequest(DefaultDrmSessionManager.MODE_QUERY, offlineLicenseKeySetId, null); + DrmSessionException error = drmSession.getError(); Pair licenseDurationRemainingSec = - WidevineUtil.getLicenseDurationRemainingSec(drmSessionManager); - drmSessionManager.releaseSession(session); + WidevineUtil.getLicenseDurationRemainingSec(drmSession); + drmSessionManager.releaseSession(drmSession); + if (error != null) { + if (error.getCause() instanceof KeysExpiredException) { + return Pair.create(0L, 0L); + } + throw error; + } return licenseDurationRemainingSec; } - private void blockingKeyRequest(@Mode int licenseMode, byte[] offlineLicenseKeySetId, + private byte[] blockingKeyRequest(@Mode int licenseMode, byte[] offlineLicenseKeySetId, DrmInitData drmInitData) throws DrmSessionException { - DrmSession session = openBlockingKeyRequest(licenseMode, offlineLicenseKeySetId, + DrmSession drmSession = openBlockingKeyRequest(licenseMode, offlineLicenseKeySetId, drmInitData); - DrmSessionException error = session.getError(); + DrmSessionException error = drmSession.getError(); + byte[] keySetId = drmSession.getOfflineLicenseKeySetId(); + drmSessionManager.releaseSession(drmSession); if (error != null) { throw error; } - drmSessionManager.releaseSession(session); + return keySetId; } private DrmSession openBlockingKeyRequest(@Mode int licenseMode, byte[] offlineLicenseKeySetId, DrmInitData drmInitData) { drmSessionManager.setMode(licenseMode, offlineLicenseKeySetId); conditionVariable.close(); - DrmSession session = drmSessionManager.acquireSession(handlerThread.getLooper(), + DrmSession drmSession = drmSessionManager.acquireSession(handlerThread.getLooper(), drmInitData); // Block current thread until key loading is finished conditionVariable.block(); - return session; + return drmSession; } }