从 ExoPlayer 源码分析视频无法播放问题_exoplayer 源码解析(1)

从 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;
    } 
    ...    
}
复制代码


**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数HarmonyOS鸿蒙开发工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年HarmonyOS鸿蒙开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/035fc2025a91b244afc1e10fd0f250e6.png)
![img](https://img-blog.csdnimg.cn/img_convert/e014f3d24684b79139cc948daaf6c7e1.png)
![img](https://img-blog.csdnimg.cn/img_convert/0b19f05f15bfd4f6947a21d36dcd1e2f.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上HarmonyOS鸿蒙开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新**

**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)**
![img](https://img-blog.csdnimg.cn/img_convert/e2f9d842f9d96b739c6084447f53703d.png)

**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

频,并且会持续更新**

**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)**
[外链图片转存中...(img-vmXxHdDV-1712878427771)]

**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值