Widevine license expiry on Android: four ways it shows up, and how to recover without a black screen

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Widevine license expiry on Android: four ways it shows up, and how to recover without a black screen

短效 DRM 许可证是一个好主意。一个仅存活几分钟的许可证可以限制密钥泄露带来的损害,让你能够切断停止付费用户的访问权限,这也是许多内容所有者目前的要求。问题在于播放器端:许可证会在播放过程中过期,而你的应用必须在观众毫无察觉的情况下获取新的许可证。在 Android 上使用 Media3 (ExoPlayer) 和 Widevine 时,我们原以为这只是一个需要捕获的错误。结果发现它有四种表现形式,而且其中只有一个明确提到了“DRM”。这篇文章记录了我们在运行许可证每 90 秒过期一次的长会话播放时所学到的经验。

The easy face: a DRM error

When things go by the book, an expired license surfaces as a PlaybackException with one of the DRM error codes (ERROR_CODE_DRM_*), usually caused by a MediaCodec.CryptoException. You catch it, fetch a fresh license, call prepare() again at the current position, and playback continues. If that were the whole story, this post would end here.

表现形式 2:“已释放状态” (Released state)

在某些设备上,解码器会先注意到密钥失效。编解码器会触发异步解密错误并自行释放。你监听器收到的不是 DRM 错误,而是 ERROR_CODE_DECODING_FAILED,其底层的 IllegalStateException 消息中提到了“已释放状态”。如果你只将 DRM 代码视为“许可证过期”,那么对于一个本可以通过新许可证正常播放的流,这种错误会导致播放直接终止,并显示为通用的解码器错误。

Face 3: a codec error in the DRM range

Sometimes the codec’s own error wins the race. You get ERROR_CODE_DECODING_FAILED again, but the cause chain contains a MediaCodec.CodecException whose error code sits in the DRM range (no license, license expired). Same root cause, third disguise.

表现形式 4:空的 IllegalStateException

这是最难发现的一种:一个空的 IllegalStateException,没有消息内容,由 MediaCodec 在排队安全输入缓冲区时抛出。没有文本,没有错误代码。唯一的线索是堆栈:顶层帧位于 android.media.MediaCodec 中,且该项目受 DRM 保护。在我们的运行测试中,每百次过期中会出现几次这种情况,这种频率恰好让它看起来像是设备不稳定,而不是一种模式。

One gate for all four

What worked for us is a single check that answers “could this be an expired license?” and only applies to DRM items. Clear content never takes this path, so a real decoder bug on a clear stream still surfaces as an error.

// MediaCodec's ERROR_DRM_* range (frameworks/av MediaErrors.h)
private val CODEC_DRM_ERRORS = -2099..-2000
private val RELEASED_STATE = Regex("(released|uninitialized) state", RegexOption.IGNORE_CASE)

fun looksLikeLicenseExpiry(error: PlaybackException, isDrmItem: Boolean): Boolean {
    if (!isDrmItem) return false
    // Face 1: a real DRM error
    if (error.errorCode == PlaybackException.ERROR_CODE_DRM_LICENSE_EXPIRED || 
        error.errorCode == PlaybackException.ERROR_CODE_DRM_SYSTEM_ERROR) return true
    
    if (error.errorCode != PlaybackException.ERROR_CODE_DECODING_FAILED) return false
    
    var cause: Throwable? = error.cause
    while (cause != null) {
        // Face 3: the codec's own DRM error code
        if (cause is MediaCodecDecoderException && cause.errorCode in CODEC_DRM_ERRORS) return true
        if (cause is MediaCodec.CodecException && cause.errorCode in CODEC_DRM_ERRORS) return true
        
        // Face 2: the codec released itself
        if (cause is IllegalStateException && RELEASED_STATE.containsMatchIn(cause.message.orEmpty())) return true
        
        // Face 4: bare IllegalStateException thrown by MediaCodec itself
        if (cause is IllegalStateException && cause !is MediaCodec.CodecException && 
            cause.stackTrace.firstOrNull()?.className == "android.media.MediaCodec") return true
        
        cause = cause.cause
    }
    return false
}

余震 (The aftershock)

这是最耗费我们时间的部分。你更新了许可证,调用了 prepare(),结果紧接着收到的却是……另一个解码器错误。原因在于渲染器的销毁方式。当播放器在错误发生后禁用其渲染器时,一个抛出异常的刷新操作可能会在每个渲染器重置之前停止循环。音频渲染器可能会保留一个仍绑定在已失效 DRM 会话上的编解码器。更新后的第一次 prepare() 就会被它绊倒。解决方法不是“无限重试”。我们在更新后允许进行少量简单的重新准备(我们设为两次),并且不将它们计入正常的恢复预算。这样一来,“过期 → 更新 → 一两次余震 → 播放”的序列可以在不到一秒的时间内完成,观众完全察觉不到。

Keep a budget

Automatic recovery needs a limit, or a broken license server turns into a request storm. Ours allows two recoveries per 60 seconds per item (aftershocks excluded, as above). After that, the error goes to the app like any other, with the original cause attached so it can be shown or logged.

隐藏在更新背后的内存泄漏

还有一件事,如果你针对 API 35+ 并使用 Media3 1.11:我们发现每次响应式更新时,原生内存都会增加几兆字节。原因是附加在音频编解码器上的响度控制器。在绑定到已失效 DRM 会话的编解码器上,将其从控制器中移除可能会在编解码器本身被释放之前抛出异常,因此编解码器永远不会被释放。在长会话且许可证较短的情况下,每次过期泄漏一个音频解码器会迅速累积。我们通过一个自定义的编解码器适配器工厂解决了这个问题,该工厂在 API 35+ 上跳过加密音频的响度控制器,同时保持明文内容不受影响。此后,原生堆在数百次更新中保持平稳。我们想告诉过去的自己:测试过期。