[关闭]
@Awille 2022-07-02T04:02:36.000000Z 字数 2910 阅读 211

ExoPlayer缓存与数据加载逻辑

Exoplayer android 缓存


在ExoPlayer当中的数据获取,无论是预缓存,还是直接获取播放数据,逻辑都是统一的,具体的不同操作,是通过包装不同的DataSource来实现的。

1、DataSource & DataSink 流操作组件

1.1、Basic Info

DataSource: 类型:interface, 官方释义:

A component from which streams of data can be read.

读取流数据的组件

 public interface DataSource {

  void addTransferListener(TransferListener transferListener);

  long open(DataSpec dataSpec) throws IOException;

  int read(byte[] buffer, int offset, int readLength) throws IOException;

  @Nullable Uri getUri();

  void close() throws IOException;
}

DataSink: 类型:interface, 官方释义:

A component to which streams of data can be written.

(写入流数据组件)

public interface DataSink {

  long open(DataSpec dataSpec) throws IOException;

  int write(byte[] buffer, int offset, int readLength) throws IOException;

  void close() throws IOException;
}

1.2、Some Implementation of DataSink

1.3、Some implementations of DataSource

2、ExoPlayer 预缓存逻辑(CacheUtils)

2.1、使用DataSource简析

使用的DataSource为CacheDataSource

public class CacheDataSource {
   DataSource upstreamDataSource;
    DataSource cacheWriteDataSource;
    DataSource cacheReadDataSource;
}

2.2、整体流程

CacheDataSource.png-59.5kB

整个读取不断重复dataSource.open dataSource.read过程

3、播放视频时数据加载

播放视频时数据加载核心逻辑也在CacheDataSource,关键数据加载的核心逻辑是完全复用的。

3.1、CacheDataSource在视频播放加载时与cache时的差异

4、ExoPlayer内部缓存任务优先级编排支持

当前我们使用的是2.10.8版本,维护一个优先级队列,只有队列中的优先级最高的任务可执行,优先级低于最高优先级的任务会被阻塞,当任务完成后我们主动去操作优先级队列,这时会尝试唤醒能运行的任务。 这里要注意这里是任务所在线程阻塞,所以线程池的线程数需要大一些。

5、结论

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注