
What happened
最近负责的项目中碰到了在部分手机上无法播放视频的问题,我们接入的是 ExoPlayer 三方库,从 log 看出现的是 Decoder init failed,也是网上常见的 4001 (ERROR_CODE_DECODER_INIT_FAILED)的问题。

在 Google 搜索无果后,决定深入源码中去一步一步探究,找到问题的所在,果然功夫不负有心人,最终从源码中找到了解决方案,分享出来希望也能帮助到大家。
Google 上相关问题: github.com/google/ExoP…
Find the bug
应用对比
由于我之前负责其他的项目中也使用了 ExoPlayer 来播放的动态壁纸的,在这几个机型上测试发是可以正常播放动态壁纸,这样可以大概率排除是机型的问题。
随即引入 ExoPlayer 库写了一个简单的 Demo,测试对比发现在该机型上可以播放网上找一个视频链接,但无法播放我们的视频链接,初步怀疑视频格式在某些机型上不支持。
源码分析
从 log 中可以看出是 MediaCodecVideoRenderer 抛出了 ExoPlaybackException,从调用栈关系可以发现最终是调用到了 MediaCodecRenderer -> maybeInitCodecWithFallback() ,然后再去源码中分析其逻辑。
private void maybeInitCodecWithFallback(...) {
...
while (codec == null) {
...
try {
initCodec(codecInfo, crypto);
} catch (Exception e) {
Log.w(TAG, "Failed to initialize decoder: " + codecInfo, e);
DecoderInitializationException exception = new DecoderInitializationException(inputFormat, e, mediaCryptoRequiresSecureDecoder, codecInfo);
...
}
}
}
public DecoderInitializationException(
Format format,
@Nullable Throwable cause,
boolean secureDecoderRequired,
MediaCodecInfo mediaCodecInfo) {
this(
"Decoder init failed: " + mediaCodecInfo.name + ", " + format ,
cause,
format.sampleMimeType,
secureDecoderRequired,
mediaCodecInfo,
Util.SDK_INT >= 21 ? getDiagnosticInfoV21(cause) : null,
/* fallbackDecoderInitializationException= */ null);
}
复制代码
从以上源码可以看出,正是调用了 initCodec() 出现了异常,然后抛出了 DecoderInitializationException其打印的异常信息也和 log 中的一致,继续追 initCodec() 中的逻辑。
private void initCodec(...) {
...
codec = codecAdapterFactory.createAdapter(configuration);
...
}
复制代码
通过打断点调试发现,其逻辑走到了 DefaultMediaCodecAdapterFactory 的 createAdapter() 中,继续跟到了 SynchronousMediaCodecAdapter.Factory 中的 createAdapter() 中,最终调用了 MediaCodec 中的 configure() 导致的异常。(从源码中可以看出,在DefaultMediaCodecAdapterFactory 中有 if 逻辑,但其实最终逻辑都会调用到 MediaCodec 中,所以无需关注该 if 逻辑)
public final class DefaultMediaCodecAdapterFactory implements MediaCodecAdapter.Factory {
...
@Override
public MediaCodecAdapter createAdapter(MediaCodecAdapter.Configuration configuration)
throws IOException {
if ((asynchronousMode == MODE_ENABLED && Util.SDK_INT >= 23)
|| (asynchronousMode == MODE_DEFAULT && Util.SDK_INT >= 31)) {
...
AsynchronousMediaCodecAdapter.Factory factory =
new AsynchronousMediaCodecAdapter.Factory(
trackType,
enableSynchronizeCodecInteractionsWithQueueing,
enableImmediateCodecStartAfterFlush);
return factory.createAdapter(configuration);
}
return new SynchronousMediaCodecAdapter.Factory().createAdapter(configuration);
}
}
public class SynchronousMediaCodecAdapter implements MediaCodecAdapter {
public static class Factory implements MediaCodecAdapter.Factory {
@Override
public MediaCodecAdapter createAdapter(Configuration configuration) throws IOException {
...
try {
codec = createCodec(configuration);
TraceUtil.beginSection("configureCodec");
codec.configure(
configuration.mediaFormat,
configuration.surface,
configuration.crypto,
configuration.flags);
...
return new SynchronousMediaCodecAdapter(codec, inputSurface);
} catch (IOException | RuntimeException e) {
...
}
}
}
复制代码
final public class MediaCodec {
...
public void configure(...) {
configure(format, surface, crypto, null, flags);
}
private void configure(...) {
if (crypto != null && descramblerBinder != null) {
throw new IllegalArgumentException("Can't use crypto and descrambler together!");
}
...
native_configure(keys, values, surface, crypto, descramblerBinder, flags);
}
private native final void native_configure(...);
...
}
复制代码
可以看出最终调用的是 C/C++ 的代码,一般在这里出现了异常,那对于 Android 端看似是无能为力的,但此时我又从另一个角度去思考,正常能播放的机型和无法播放的机型,到底是哪些参数有差别呢? 于是又一步一步回退去排查整个流程中 MediaCodecInfo 对象的中值,经过不断排查,最终发现以下核心逻辑代码:
public abstract class MediaCodecRenderer extends BaseRenderer {
...
private void maybeInitCodecWithFallback(
MediaCrypto crypto, boolean mediaCryptoRequiresSecureDecoder)
throws DecoderInitializationException {
...
try {
// 获取可用的解码器 list
List<MediaCodecInfo> allAvailableCodecInfos =
getAvailableCodecInfos(mediaCryptoRequiresSecureDecoder);
availableCodecInfos = new ArrayDeque<>();
// 默认为false,所以走的只获取可用 list 中的第一个数据
if (enableDecoderFallback) {
availableCodecInfos.addAll(allAvailableCodecInfos) ;
} else if (!allAvailableCodecInfos.isEmpty()) {
availableCodecInfos.add(allAvailableCodecInfos.get(0)) ;
}
...
}
...
// 循环去找可用的 list 中是否能有解码器初始化成功
while (codec == null) {
MediaCodecInfo codecInfo = availableCodecInfos.peekFirst();
if (!shouldInitCodec(codecInfo)) {
return;
}
try {
initCodec(codecInfo, crypto);
} catch (Exception e) {
...
}
}
availableCodecInfos = null;
}
...
}
复制代码
从中可以看出,首先会通过 getAvailableCodecInfos() 获取一组可用的解码器 list,然后通过逻辑判断将该 list 中全部还是第一个加到队列 availableCodecInfos 中,接下来通过 while 循环,不断的从 availableCodecInfos 队列中取第一个,去尝试初始化看能否成功,直到找到了成功初始化的解码器。
/*
@param enableDecoderFallback Whether to enable fallback to lower-priority decoders if decoder initialization fails. This may result in using a decoder that is less efficient or slower than the primary decoder.
/
复制代码
从上面注释可以了解到 enableDecoderFallback 参数的含义,如果设置为true,可能会导致性能降低(软解性能不如硬解),默认相当于优先初始化硬解。
解决方案
其实非常简单就能解决了,设置 setEnableDecoderFallback(true), 大功告成!
ExoPlayer player = new ExoPlayer.Builder(context)
.setRenderersFactory(new DefaultRenderersFactory(context).setEnableDecoderFallback(true))
.build();
网友评论