From d6c721ffc51f17dea22e7b9beedb1e3ecbe51fba Mon Sep 17 00:00:00 2001 From: aquilescanta Date: Thu, 22 Jul 2021 15:17:10 +0100 Subject: [PATCH] Move DRM exception classification method to public API PiperOrigin-RevId: 386232697 --- .../exoplayer2/drm/DefaultDrmSession.java | 79 +----------- .../android/exoplayer2/drm/DrmUtil.java | 120 ++++++++++++++++++ 2 files changed, 122 insertions(+), 77 deletions(-) create mode 100644 library/core/src/main/java/com/google/android/exoplayer2/drm/DrmUtil.java diff --git a/library/core/src/main/java/com/google/android/exoplayer2/drm/DefaultDrmSession.java b/library/core/src/main/java/com/google/android/exoplayer2/drm/DefaultDrmSession.java index cf667055d1..96389b018f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/drm/DefaultDrmSession.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/drm/DefaultDrmSession.java @@ -19,9 +19,6 @@ import static com.google.android.exoplayer2.util.Assertions.checkState; import static java.lang.Math.min; import android.annotation.SuppressLint; -import android.media.DeniedByServerException; -import android.media.MediaDrm; -import android.media.MediaDrmResetException; import android.media.NotProvisionedException; import android.os.Handler; import android.os.HandlerThread; @@ -29,12 +26,10 @@ import android.os.Looper; import android.os.Message; import android.os.SystemClock; import android.util.Pair; -import androidx.annotation.DoNotInline; import androidx.annotation.GuardedBy; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; -import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.drm.DrmInitData.SchemeData; import com.google.android.exoplayer2.drm.ExoMediaDrm.KeyRequest; import com.google.android.exoplayer2.drm.ExoMediaDrm.ProvisionRequest; @@ -519,7 +514,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; private void onError(Exception e, boolean thrownByExoMediaDrm) { lastException = - new DrmSessionException(e, getErrorCodeForMediaDrmException(e, thrownByExoMediaDrm)); + new DrmSessionException( + e, DrmUtil.getErrorCodeForMediaDrmException(e, thrownByExoMediaDrm)); Log.e(TAG, "DRM session error", e); dispatchEvent(eventDispatcher -> eventDispatcher.drmSessionManagerError(e)); if (state != STATE_OPENED_WITH_KEYS) { @@ -539,36 +535,6 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; } } - @PlaybackException.ErrorCode - private static int getErrorCodeForMediaDrmException( - Exception exception, boolean thrownByExoMediaDrm) { - if (Util.SDK_INT >= 21 && PlatformOperationsWrapperV21.isMediaDrmStateException(exception)) { - return PlatformOperationsWrapperV21.mediaDrmStateExceptionToErrorCode(exception); - } else if (Util.SDK_INT >= 23 - && PlatformOperationsWrapperV23.isMediaDrmResetException(exception)) { - return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; - } else if (Util.SDK_INT >= 18 - && PlatformOperationsWrapperV18.isNotProvisionedException(exception)) { - return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; - } else if (Util.SDK_INT >= 18 - && PlatformOperationsWrapperV18.isDeniedByServerException(exception)) { - return PlaybackException.ERROR_CODE_DRM_DEVICE_REVOKED; - } else if (exception instanceof UnsupportedDrmException) { - return PlaybackException.ERROR_CODE_DRM_SCHEME_UNSUPPORTED; - } else if (exception instanceof DefaultDrmSessionManager.MissingSchemeDataException) { - return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; - } else if (exception instanceof KeysExpiredException) { - return PlaybackException.ERROR_CODE_DRM_LICENSE_EXPIRED; - } else if (thrownByExoMediaDrm) { - // A MediaDrm exception was thrown but it was impossible to determine the cause. Because no - // better diagnosis tools were provided, we treat this as a system error. - return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; - } else { - // The error happened during the license request. - return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; - } - } - // Internal classes. @SuppressLint("HandlerLeak") @@ -714,45 +680,4 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; this.request = request; } } - - @RequiresApi(18) - private static final class PlatformOperationsWrapperV18 { - - @DoNotInline - public static boolean isNotProvisionedException(@Nullable Throwable throwable) { - return throwable instanceof NotProvisionedException; - } - - @DoNotInline - public static boolean isDeniedByServerException(@Nullable Throwable throwable) { - return throwable instanceof DeniedByServerException; - } - } - - @RequiresApi(21) - private static final class PlatformOperationsWrapperV21 { - - @DoNotInline - public static boolean isMediaDrmStateException(@Nullable Throwable throwable) { - return throwable instanceof MediaDrm.MediaDrmStateException; - } - - @DoNotInline - @PlaybackException.ErrorCode - public static int mediaDrmStateExceptionToErrorCode(Throwable throwable) { - @Nullable - String diagnosticsInfo = ((MediaDrm.MediaDrmStateException) throwable).getDiagnosticInfo(); - int drmErrorCode = Util.getErrorCodeFromPlatformDiagnosticsInfo(diagnosticsInfo); - return C.getErrorCodeForMediaDrmErrorCode(drmErrorCode); - } - } - - @RequiresApi(23) - private static final class PlatformOperationsWrapperV23 { - - @DoNotInline - public static boolean isMediaDrmResetException(@Nullable Throwable throwable) { - return throwable instanceof MediaDrmResetException; - } - } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/drm/DrmUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/drm/DrmUtil.java new file mode 100644 index 0000000000..19e5aa844e --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/drm/DrmUtil.java @@ -0,0 +1,120 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.drm; + +import android.media.DeniedByServerException; +import android.media.MediaDrm; +import android.media.MediaDrmResetException; +import android.media.NotProvisionedException; +import androidx.annotation.DoNotInline; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; +import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; +import com.google.android.exoplayer2.util.Util; + +/** DRM-related utility methods. */ +public final class DrmUtil { + + /** + * Returns the {@link PlaybackException.ErrorCode} that corresponds to the given DRM-related + * exception. + * + * @param exception The DRM-related exception for which to obtain a corresponding {@link + * PlaybackException.ErrorCode}. + * @param thrownByExoMediaDrm Whether the given exception originated in a {@link ExoMediaDrm} + * method. Exceptions that did not originate in {@link ExoMediaDrm} are assumed to originate + * in the license request. + * @return The {@link PlaybackException.ErrorCode} that corresponds to the given DRM-related + * exception. + */ + @PlaybackException.ErrorCode + public static int getErrorCodeForMediaDrmException( + Exception exception, boolean thrownByExoMediaDrm) { + if (Util.SDK_INT >= 21 && PlatformOperationsWrapperV21.isMediaDrmStateException(exception)) { + return PlatformOperationsWrapperV21.mediaDrmStateExceptionToErrorCode(exception); + } else if (Util.SDK_INT >= 23 + && PlatformOperationsWrapperV23.isMediaDrmResetException(exception)) { + return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; + } else if (Util.SDK_INT >= 18 + && PlatformOperationsWrapperV18.isNotProvisionedException(exception)) { + return PlaybackException.ERROR_CODE_DRM_PROVISIONING_FAILED; + } else if (Util.SDK_INT >= 18 + && PlatformOperationsWrapperV18.isDeniedByServerException(exception)) { + return PlaybackException.ERROR_CODE_DRM_DEVICE_REVOKED; + } else if (exception instanceof UnsupportedDrmException) { + return PlaybackException.ERROR_CODE_DRM_SCHEME_UNSUPPORTED; + } else if (exception instanceof DefaultDrmSessionManager.MissingSchemeDataException) { + return PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR; + } else if (exception instanceof KeysExpiredException) { + return PlaybackException.ERROR_CODE_DRM_LICENSE_EXPIRED; + } else if (thrownByExoMediaDrm) { + // A MediaDrm exception was thrown but it was impossible to determine the cause. Because no + // better diagnosis tools were provided, we treat this as a system error. + return PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR; + } else { + // The error happened during the license request. + return PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED; + } + } + + // Internal classes. + + @RequiresApi(18) + private static final class PlatformOperationsWrapperV18 { + + @DoNotInline + public static boolean isNotProvisionedException(@Nullable Throwable throwable) { + return throwable instanceof NotProvisionedException; + } + + @DoNotInline + public static boolean isDeniedByServerException(@Nullable Throwable throwable) { + return throwable instanceof DeniedByServerException; + } + } + + @RequiresApi(21) + private static final class PlatformOperationsWrapperV21 { + + @DoNotInline + public static boolean isMediaDrmStateException(@Nullable Throwable throwable) { + return throwable instanceof MediaDrm.MediaDrmStateException; + } + + @DoNotInline + @PlaybackException.ErrorCode + public static int mediaDrmStateExceptionToErrorCode(Throwable throwable) { + @Nullable + String diagnosticsInfo = ((MediaDrm.MediaDrmStateException) throwable).getDiagnosticInfo(); + int drmErrorCode = Util.getErrorCodeFromPlatformDiagnosticsInfo(diagnosticsInfo); + return C.getErrorCodeForMediaDrmErrorCode(drmErrorCode); + } + } + + @RequiresApi(23) + private static final class PlatformOperationsWrapperV23 { + + @DoNotInline + public static boolean isMediaDrmResetException(@Nullable Throwable throwable) { + return throwable instanceof MediaDrmResetException; + } + } + + // Prevent instantiation. + + private DrmUtil() {} +}