ExoPlayer架构详解与源码分析(14)——ProgressiveMediaPeriod

系列文章目录

ExoPlayer架构详解与源码分析(1)——前言
ExoPlayer架构详解与源码分析(2)——Player
ExoPlayer架构详解与源码分析(3)——Timeline
ExoPlayer架构详解与源码分析(4)——整体架构
ExoPlayer架构详解与源码分析(5)——MediaSource
ExoPlayer架构详解与源码分析(6)——MediaPeriod
ExoPlayer架构详解与源码分析(7)——SampleQueue
ExoPlayer架构详解与源码分析(8)——Loader
ExoPlayer架构详解与源码分析(9)——TsExtractor
ExoPlayer架构详解与源码分析(10)——H264Reader
ExoPlayer架构详解与源码分析(11)——DataSource
ExoPlayer架构详解与源码分析(12)——Cache
ExoPlayer架构详解与源码分析(13)——TeeDataSource和CacheDataSource
ExoPlayer架构详解与源码分析(14)——ProgressiveMediaPeriod


文章目录

  • 系列文章目录
  • 前言
  • ProgressiveMediaPeriod
  • 总结


前言

中途间隔了一段时间,之前写了那么多铺垫,终于看到ProgressiveMediaPeriod实现部分了

ProgressiveMediaPeriod

有了之前的那些铺垫,这里直接看源码了

  @Overridepublic void prepare(Callback callback, long positionUs) {this.callback = callback;loadCondition.open();//保证继续加载的开关打开,Loader不阻塞startLoading();}private void startLoading() {ExtractingLoadable loadable =//创建loadable 共Loader加载new ExtractingLoadable(//extractorOutput监听,也就是Loader加载过程中会回调ProgressiveMediaPeriod的track,endTracksuri, dataSource, progressiveMediaExtractor, /* extractorOutput= */ this, loadCondition);if (prepared) {//如果已经准备完成Assertions.checkState(isPendingReset());if (durationUs != C.TIME_UNSET && pendingResetPositionUs > durationUs) {//当前定位位置已经超过总时长,直接加载结束loadingFinished = true;pendingResetPositionUs = C.TIME_UNSET;return;}//通过seekMap查找出当前时间对于的数据位置loadable.setLoadPosition(checkNotNull(seekMap).getSeekPoints(pendingResetPositionUs).first.position,pendingResetPositionUs);for (SampleQueue sampleQueue : sampleQueues) {//将所有的轨道开始时间同步sampleQueue.setStartTimeUs(pendingResetPositionUs);}pendingResetPositionUs = C.TIME_UNSET;}//获取开始加载时所有轨道已经提前的数据块总数,后面通过当前的数据块总数和开始的数量对比,可以判断出是否加载了新的数据extractedSamplesCountAtStartOfLoad = getExtractedSamplesCount();long elapsedRealtimeMs =loader.startLoading(//Loader开始加载,具体过程参照Loader部分的文章,此时加载状态的回调this,也就是加载完成后会调用ProgressiveMediaPeriod  onLoadCompleted,seekMaploadable, this, loadErrorHandlingPolicy.getMinimumLoadableRetryCount(dataType));DataSpec dataSpec = loadable.dataSpec;//触发监听mediaSourceEventDispatcher.loadStarted(new LoadEventInfo(loadable.loadTaskId, dataSpec, elapsedRealtimeMs),C.DATA_TYPE_MEDIA,C.TRACK_TYPE_UNKNOWN,/* trackFormat= */ null,C.SELECTION_REASON_UNKNOWN,/* trackSelectionData= */ null,/* mediaStartTimeUs= */ loadable.seekTimeUs,durationUs);}@Override//Loader加载时如果解析器需要输出Sample数据会先回调track,获取TrackOutputpublic TrackOutput track(int id, int type) {return prepareTrackOutput(new TrackId(id, /* isIcyTrack= */ false));}//构建TrackOutputprivate TrackOutput prepareTrackOutput(TrackId id) {int trackCount = sampleQueues.length;for (int i = 0; i < trackCount; i++) {//查询当前的sampleQueue是否已创建直接返回if (id.equals(sampleQueueTrackIds[i])) {return sampleQueues[i];}}SampleQueue trackOutput =//创建新的SampleQueue对应一个新的轨道,这里传入了缓存分配器allocatorSampleQueue.createWithDrm(allocator, drmSessionManager, drmEventDispatcher);trackOutput.setUpstreamFormatChangeListener(this);//设置Format改变的监听@NullableTypeTrackId[] sampleQueueTrackIds = Arrays.copyOf(this.sampleQueueTrackIds, trackCount + 1);sampleQueueTrackIds[trackCount] = id;this.sampleQueueTrackIds = Util.castNonNullTypeArray(sampleQueueTrackIds);//更新全局的SampleQueue数组@NullableType SampleQueue[] sampleQueues = Arrays.copyOf(this.sampleQueues, trackCount + 1);sampleQueues[trackCount] = trackOutput;this.sampleQueues = Util.castNonNullTypeArray(sampleQueues);return trackOutput;}@Override//当解析器已经将所有轨道解析出来时会调用此方法,参照H264Reader部分public void endTracks() {sampleQueuesBuilt = true;//当前方法在解析的子线程中回调,需要将方法方法当前ProgressiveMediaPeriod的线程中执行maybeFinishPreparehandler.post(maybeFinishPrepareRunnable);}private void maybeFinishPrepare() {if (released || prepared || !sampleQueuesBuilt || seekMap == null) {return;}//确保所有轨道均已解析for (SampleQueue sampleQueue : sampleQueues) {if (sampleQueue.getUpstreamFormat() == null) {return;}}//阻塞住loader的解析,防止继续更新sampleQueuesloadCondition.close();//创建TrackGroup,trackState 供后续的selectTracks使用int trackCount = sampleQueues.length;TrackGroup[] trackArray = new TrackGroup[trackCount];boolean[] trackIsAudioVideoFlags = new boolean[trackCount];for (int i = 0; i < trackCount; i++) {Format trackFormat = checkNotNull(sampleQueues[i].getUpstreamFormat());@Nullable String mimeType = trackFormat.sampleMimeType;boolean isAudio = MimeTypes.isAudio(mimeType);boolean isAudioVideo = isAudio || MimeTypes.isVideo(mimeType);trackIsAudioVideoFlags[i] = isAudioVideo;haveAudioVideoTracks |= isAudioVideo;...trackFormat = trackFormat.copyWithCryptoType(drmSessionManager.getCryptoType(trackFormat));trackArray[i] = new TrackGroup(/* id= */ Integer.toString(i), trackFormat);}trackState = new TrackState(new TrackGroupArray(trackArray), trackIsAudioVideoFlags);prepared = true;//标记准备完成checkNotNull(callback).onPrepared(this);//通知上层prepared ,上层接下就会去调用ProgressiveMediaPeriod.selectTracks获取轨道信息}@Override//获取轨道public long selectTracks(@NullableType ExoTrackSelection[] selections,//TrackSelector 部分会说到boolean[] mayRetainStreamFlags,@NullableType SampleStream[] streams,//Renderer部分会说的boolean[] streamResetFlags,long positionUs) {assertPrepared();//确保已经准备完成TrackGroupArray tracks = trackState.tracks;boolean[] trackEnabledStates = trackState.trackEnabledStates;int oldEnabledTrackCount = enabledTrackCount;// 去除原来mayRetainStreamFlags标记的无需保留的轨道for (int i = 0; i < selections.length; i++) {if (streams[i] != null && (selections[i] == null || !mayRetainStreamFlags[i])) {int track = ((SampleStreamImpl) streams[i]).track;Assertions.checkState(trackEnabledStates[track]);enabledTrackCount--;trackEnabledStates[track] = false;streams[i] = null;}}//如果是第一次selectTracks,而且positionUs 位置又不为0,或者之前一次selectTracks禁用了所有的轨道,这2种情况就需要Seekboolean seekRequired = seenFirstTrackSelection ? oldEnabledTrackCount == 0 : positionUs != 0;// 开始通过selections选择新的轨道for (int i = 0; i < selections.length; i++) {if (streams[i] == null && selections[i] != null) {ExoTrackSelection selection = selections[i];Assertions.checkState(selection.length() == 1);Assertions.checkState(selection.getIndexInTrackGroup(0) == 0);int track = tracks.indexOf(selection.getTrackGroup());Assertions.checkState(!trackEnabledStates[track]);enabledTrackCount++;trackEnabledStates[track] = true;streams[i] = new SampleStreamImpl(track);//向入参中赋值streamResetFlags[i] = true;if (!seekRequired) {SampleQueue sampleQueue = sampleQueues[track];seekRequired =!sampleQueue.seekTo(positionUs, /* allowTimeBeyondBuffer= */ true)&& sampleQueue.getReadIndex() != 0;}}}if (enabledTrackCount == 0) {pendingDeferredRetry = false;notifyDiscontinuity = false;if (loader.isLoading()) {// Discard as much as we can synchronously.for (SampleQueue sampleQueue : sampleQueues) {sampleQueue.discardToEnd();}loader.cancelLoading();} else {for (SampleQueue sampleQueue : sampleQueues) {sampleQueue.reset();}}} else if (seekRequired) {positionUs = seekToUs(positionUs);// We'll need to reset renderers consuming from all streams due to the seek.for (int i = 0; i < streams.length; i++) {if (streams[i] != null) {streamResetFlags[i] = true;}}}seenFirstTrackSelection = true;return positionUs;}@Override//解析器解析出SeekMap时回调public void seekMap(SeekMap seekMap) {handler.post(() -> setSeekMap(seekMap));}@Overridepublic boolean continueLoading(long playbackPositionUs) {if (loadingFinished//加载完成,出错等情况返回false|| loader.hasFatalError()|| pendingDeferredRetry|| (prepared && enabledTrackCount == 0)) {return false;}boolean continuedLoading = loadCondition.open();if (!loader.isLoading()) {//当前没有正在加载startLoading();//再次startLoadingcontinuedLoading = true;}return continuedLoading;}@Override//Loader加载完成后回调public void onLoadCompleted(ExtractingLoadable loadable, long elapsedRealtimeMs, long loadDurationMs) {if (durationUs == C.TIME_UNSET && seekMap != null) {//此时durationUs 未知,seekMap 已经有了boolean isSeekable = seekMap.isSeekable();long largestQueuedTimestampUs =//查询所有轨道中时间戳最大值getLargestQueuedTimestampUs(/* includeDisabledTracks= */ true);durationUs =//将最大值+10毫秒作为当前媒体的时长largestQueuedTimestampUs == Long.MIN_VALUE? 0: largestQueuedTimestampUs + DEFAULT_LAST_SAMPLE_DURATION_US;//此时durationUs 等信息已知可以通过MediaSource更新一把TimeLinelistener.onSourceInfoRefreshed(durationUs, isSeekable, isLive);}StatsDataSource dataSource = loadable.dataSource;//StatsDataSource可以缓存Uri和ResponseHeader,获取这些值构建LoadEventInfo LoadEventInfo loadEventInfo =new LoadEventInfo(loadable.loadTaskId,loadable.dataSpec,dataSource.getLastOpenedUri(),dataSource.getLastResponseHeaders(),elapsedRealtimeMs,loadDurationMs,dataSource.getBytesRead());loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);//触发监听mediaSourceEventDispatcher.loadCompleted(loadEventInfo,C.DATA_TYPE_MEDIA,C.TRACK_TYPE_UNKNOWN,/* trackFormat= */ null,C.SELECTION_REASON_UNKNOWN,/* trackSelectionData= */ null,/* mediaStartTimeUs= */ loadable.seekTimeUs,durationUs);loadingFinished = true;//此时的loadingFinished 已为true,没设置为false前是无法continueLoading的//请求上层继续加载,是否继续加载由上层决定,如果上层同意继续加载会调用ProgressiveMediaPeriod的continueLoadingcheckNotNull(callback).onContinueLoadingRequested(this);}

这里再总结下rogressiveMediaPeriod执行过程:

  1. 首先调用prepare方法启动Loader去获取资源的轨道信息,此处参考ExoPlayer架构详解与源码分析(8)——Loader
  2. Loader中的解析器获取到信息后回回调endTrack通知ProgressiveMediaPeriod prepare完成
  3. ProgressiveMediaPeriod 会进一步通知上层此时MeidaSource已经准备完成,上层再Renderer绘制前会调用ProgressiveMediaPeriod selectTracks选择轨道数据,也就将SampleQueue中的数据提供出去,此处参考ExoPlayer架构详解与源码分析(7)——SampleQueue
  4. 同时Loader从打开到当前加载数据量超过1M(默认值)时就会阻塞当前的Loader,然后询问上层是否继续加载
  5. 上层主要是通过后面要将LoadControl判断,如果上层决定继续加载更多数据,就会调用ProgressiveMediaPeriod continueLoading解开阻塞的锁继续加载。因为我们知道数据是加载到内存中的,如果无限制的加载肯定是不行的,需要有节制的加载和释放数据。而LoadControl就负责这块工作后面会讲到。
  6. 而上面过程当需要释放已经播放的内存时就会调用discardBuffer方法释放Sample中的内存
  7. 当所有的数据加载完成的时候会调用onLoadCompleted将loadingFinished 标记为true

总结

到这里Exoplayer中最复杂的组件MediaSource就全部分析完毕了,之前说了MediaSource在整个运载火箭中的角色就类似于燃料系统,确保火箭顺利升空,在运行过程中持续稳定的为火箭提供燃料。这些燃料提供给谁了呢,或者说是被谁消耗了呢。这就是下面要讲的火箭的核心发动机Renderers,也是Exoplayer四大组件中另一个重要的角色。


版权声明 ©
本文为CSDN作者山雨楼原创文章
转载请注明出处
原创不易,觉得有用的话,收藏转发点赞支持

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/38769.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

高考完的假期想学c语言 要注意那些问题?

在开始前刚好我有一些资料&#xff0c;是我根据网友给的问题精心整理了一份「c语言的资料从专业入门到高级教程」&#xff0c; 点个关注在评论区回复“666”之后私信回复“666”&#xff0c;全部无偿共享给大家&#xff01;&#xff01;&#xff01;其实建议高考完之后好好玩一…

线上问题定位分析宝典——Linux中定位JVM问题常用命令

查询Java进程ID #ps axu | grep java #ps elf | grep java查看机器负载及CPU信息 #top -p 1(进程ID) #top (查看所有进程)获取CPU飙升线程堆栈 1. top -c 找到CPU飙升进程ID&#xff1b; 2. top -Hbp 9702(替换成进程ID) 找到CPU飙升线程ID&#xff1b; 3. $ printf &quo…

Java 7新特性深度解析:提升效率与功能

文章目录 Java 7新特性深度解析&#xff1a;提升效率与功能一、Switch中添加对String类型的支持二、数字字面量的改进三、异常处理&#xff08;捕获多个异常&#xff09;四、增强泛型推断五、NIO2.0&#xff08;AIO&#xff09;新IO的支持六、SR292与InvokeDynamic七、Path接口…

64.ThreadLocal造成的内存泄漏

内存泄漏 程序中已动态分配的堆内存,由于某种原因程序为释放和无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果。内存泄漏的堆积终将导致内存溢出。 内存溢出 没有足够的内存提供申请者使用。 ThreadLocal出现内存泄漏的真实原因 内存泄漏的发…

Java中的多线程与并发编程详解

Java中的多线程与并发编程详解 大家好&#xff0c;我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01; 在当今软件开发中&#xff0c;利用多核处理器的能力并行执行任务已成为提高应用…

Transformer拆积木

文章目录 ConceptsEmbeddingEncoderDecoderSelf-Attention matric calculationFinal Linear and Softmax LayerLoss function 参考 学一下已经问鼎中原七年之久的Transformer Concepts 开始拆积木&#xff01; Embedding Encoder Decoder Self-Attention matric calculati…

【文档+源码+调试讲解】科研经费管理系统

目 录 目 录 摘 要 ABSTRACT 1 绪论 1.1 课题背景 1.2 研究现状 1.3 研究内容 2 系统开发环境 2.1 vue技术 2.2 JAVA技术 2.3 MYSQL数据库 2.4 B/S结构 2.5 SSM框架技术 3 系统分析 3.1 可行性分析 3.1.1 技术可行性 3.1.2 操作可行性 3.1.3 经济可行性 3.1…

解析服务器地址异常的原因和解决方法

在网络利用开发和运维进程中&#xff0c;解析服务器地址异常是常见的问题之一。特别是在触及到跨境业务和国际网络传输时&#xff0c;由于网络环境的复杂性&#xff0c;解析服务器地址异常可能会致使用户没法正常访问网站或利用程序。 解析服务器地址异常可能由多种缘由引发&am…

虚拟机的网络配置

&#x1f4d1;打牌 &#xff1a; da pai ge的个人主页 &#x1f324;️个人专栏 &#xff1a; da pai ge的博客专栏 ☁️ 每一步都向着梦想靠近&#xff0c;坚持就是胜利的序曲 一 …

手机系统设置选项

通用设置选项 1. 忽略电池优化选项 参考 https://blog.csdn.net/dodod2012/article/details/132045963 <uses-permission android:name"android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>public static boolean isIgnoreBatteryOption(Context c…

俄罗斯ozon运费计算工具,跨境电商ozon物流运费计算工具

OZON平台服装类目卖家而言&#xff0c;如何快速、准确地为产品定价&#xff0c;并有效管理运费成本&#xff0c;直接关系到市场竞争力与利润空间。接下来我们看看俄罗斯ozon运费计算工具&#xff0c;跨境电商ozon物流运费计算工具。 萌啦Ozon定价工具&#xff1a;智能模拟&…

Cesium----加载SuperMap的S3M地形

在原生Cesium中加载S3M地形&#xff0c;需要用到Supermap发布的一个插件&#xff1a;iClient3D-for-WebGL&#xff0c; 在vite vure3&#xff0c;cesium 1.119中进行了实现&#xff0c;注意的点在于需要把SuperMap3D 放置在cesium的Build路径下 然后在代码中直接调用SuperMap3…

windows重装系统

一、下载Ventoy工具&#xff0c;制作启动盘 官网地址&#xff1a;https://www.ventoy.net/cn/download.html 电脑插入用来制作系统盘的U盘&#xff0c;建议大小在8G以上。 双击打开刚解压出来的Ventoy2Disk.exe文件。打开界面如图&#xff1a; 确认U盘&#xff0c;如图&am…

【HICE】基于httpd下的web服务器搭建

1.下载httpd&#xff1a; dnf install httpd -y 2.进入httpd中&#xff1a; cd /etc/httpd cd conf.d 3.编辑一个新的vhost.conf 4.重启httpd服务 systemctl restart httpd 5.关闭防火墙 systemctl stop firewalld setenforce 0 6.文本写入&#xff08;网页编辑&…

8年经验之谈!自动化测试框架该如何搭建?

前言 最近好多小伙伴都在说接口自动化测试&#xff0c;那么究竟什么是接口自动化测试呢&#xff1f;让我们一起往下看就知道了&#xff0c;首先我们得先弄清楚下面这个问题。 为什么要做&#xff08;自动化&#xff09;接口测试&#xff1f; 1、由于现在各个系统的复杂度不断…

springboot的MultipartFile转File读取

在Spring Boot中&#xff0c;处理文件上传时&#xff0c;MultipartFile接口被用来封装上传的文件信息。 如果需要将MultipartFile转换为Java标准的File对象进行读取。 以下是具体的操作流程&#xff1a; 1. 创建临时文件 首先&#xff0c;需要将接收到的MultipartFile对象转…

准化 | 水系统碳中和标准体系初见成效

2024年5月31日&#xff0c;中华环保联合会发布《团体标准公告 2024年第10号&#xff08;总第78号&#xff09;》&#xff0c;批准发布了由中华环保联合会提出并归口的《废水处理温室气体监测技术规程》(T/ACEF 142-2024)、《工业水系统碳排放核算方法与报告指南》(T/ACEF143-20…

yarn不同操作系统的安装与配置

Yarn 是一个快速、可靠且安全的依赖包管理工具&#xff0c;用于替代 npm。以下是在不同操作系统上安装和配置 Yarn 的步骤。 1. 安装 Node.js 在安装 Yarn 之前&#xff0c;请确保已经安装了 Node.js&#xff0c;因为 Yarn 需要 Node.js 环境。你可以在 Node.js — Run JavaSc…

昇思25天学习打卡营第十五天|基于MobileNetv2的垃圾分类

基于MobileNetv2的垃圾分类 MobileNetv2模型原理介绍 MobileNet网络是由Google团队于2017年提出的专注于移动端、嵌入式或IoT设备的轻量级CNN网络&#xff0c;相比于传统的卷积神经网络&#xff0c;MobileNet网络使用深度可分离卷积&#xff08;Depthwise Separable Convolut…

Zabbix 6.0 案例

自定义监控内容 案列&#xff1a;自定义监控客户端服务器登录的人数 需求&#xff1a;限制登录人数不超过 3 个&#xff0c;超过 3 个就发出报警信息 1.在客户端创建自定义 key 明确需要执行的 linux 命令 who | wc -l 2.在被监控主机的配置文件目录中&#xff08;/etc/za…