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; } }