ExoPlayer使用可以參考:
https://exoplayer.dev/
一进栽、ExoPlayer運(yùn)行流程
ExoPlayer的運(yùn)行流程是source->buffer->decoder->renderer玄窝。從視頻源讀取數(shù)據(jù)到buffer(使用SampleQueue來管理buffer)巴席,MediaCodec從buffer中取數(shù)據(jù)解碼后耙考,交給renderer ,視頻使用 surface顯示既穆,音頻使用AudioTrack 播放隐解。 從視頻源里讀取數(shù)據(jù)并沒有采用MediaExtractor ,而是根據(jù)不同的視頻格式使用自己的Extractor,比如對于mp4文件升酣,使用Mp4Extractor舷暮。對視頻renderer,封裝在MediaCodecVideoRenderer噩茄。對音頻播放下面,封裝在MediaCodecAudioRenderer。
二巢墅、ExoPlayer線程模型
ExoPlayer里有個(gè)Playback的HandlerThread诸狭,調(diào)用者在主線程通過Handler發(fā)消息券膀,Playback線程處理消息,下面通過給視頻播放設(shè)置Surface看下消息傳遞流程驯遇。
ExoPlayer播放視頻使用PlayerView芹彬,PlayerView本質(zhì)使用TextureView,SurfaceView或GLSurfaceView這些View來顯示圖像叉庐。拿SurfaceView舉例舒帮,當(dāng)SurfaceView在UI上顯示時(shí),會(huì)回調(diào)surfaceCreated,在SimpleExoPlayer里注冊了一系列的回調(diào)監(jiān)聽陡叠,其中就有surfaceCreated玩郊,在surfaceCreated里調(diào)用了:
for (Renderer renderer : renderers) {
if (renderer.getTrackType() == C.TRACK_TYPE_VIDEO) {
messages.add(
player
.createMessage(renderer)
.setType(Renderer.MSG_SET_SURFACE)
.setPayload(surface)
.send());
}
}
renderers里有視頻,音頻枉阵,字幕等renderer译红,這里只關(guān)注視頻renderder,如果是 視頻renderer兴溜,就封裝一個(gè)PlayerMessage發(fā)送出去侦厚。看下createMessage怎生成PlayerMessage:
public PlayerMessage createMessage(Target target) {
return new PlayerMessage(
internalPlayer,\\ sender
target, \\target
playbackInfo.timeline,
getCurrentWindowIndex(),
internalPlayerHandler);
}
send()的調(diào)用:
public PlayerMessage send() {
sender.sendMessage(this);
return this;
}
可見PlayerMessage是通過internalPlayer發(fā)送拙徽,然后target(renderder)去接收刨沦。
internalPlayer其實(shí)就是ExoPlayerImplInternal,sendMessage源碼是 :
@Override
public synchronized void sendMessage(PlayerMessage message) {
handler.obtainMessage(MSG_SEND_MESSAGE, message).sendToTarget();
}
hander是就是封裝后的HandlerWrapper膘怕,本質(zhì)使用Playback線程的looper想诅,callback是ExoPlayerImplInternal自己,于是消息傳遞到ExoPlayerImplInternal的handleMessage方法岛心,這時(shí)運(yùn)行在Playback線程中来破,handleMessage中處理msg.what為MSG_SEND_MESSAGE的方法是調(diào)用sendMessageInternal,轉(zhuǎn)了幾次后調(diào)用到deliverMessage:
private void deliverMessage(PlayerMessage message) throws ExoPlaybackException {
try {
message.getTarget().handleMessage(message.getType(), message.getPayload());
} finally {
message.markAsProcessed(/* isDelivered= */ true);
}
}
message.getTarget就是之前的renderer忘古,對于視頻讳癌,就是MediaCodecVideoRenderer,看看MediaCodecVideoRenderer的handleMessage:
@Override
public void handleMessage(int messageType, @Nullable Object message) throws ExoPlaybackException {
if (messageType == MSG_SET_SURFACE) {
setSurface((Surface) message);
}
}
private void setSurface(Surface surface) throws ExoPlaybackException {
....
setOutputSurfaceV23(codec, surface);
....
}
protected void setOutputSurfaceV23(MediaCodec codec, Surface surface) {
codec.setOutputSurface(surface);
}
這里codec就是MediaCodec存皂,調(diào)用了MediaCodec的setOutputSurface方法設(shè)置了surface晌坤。
三、事件監(jiān)聽
在SimpleExoPlayer構(gòu)造方法里有個(gè)componentListener成員變量旦袋,componentListener里面定義了各種事件,比如視頻相關(guān)的監(jiān)聽SurfaceHolder.Callback,VideoRendererEventListener,這些都在里面定義了骤菠。在SimpleExoPlayer里還有個(gè)renderers,renderers對應(yīng)視頻疤孕,音頻這些最終顯示或播放商乎,SimpleExoPlayer把componentListener塞給了各個(gè)renderers。拿VideoRenderer為例祭阀,DefaultRenderersFactory里調(diào)用buildVideoRenderers:
MediaCodecVideoRenderer videoRenderer =
new MediaCodecVideoRenderer(
context,
mediaCodecSelector,
allowedVideoJoiningTimeMs,
enableDecoderFallback,
eventHandler,
eventListener, //VideoRendererEventListener
MAX_DROPPED_VIDEO_FRAME_COUNT_TO_NOTIFY);
這樣就把VideoRendererEventListener(也是componentListener)傳遞給了MediaCodecVideoRenderer鹉戚,MediaCodecVideoRenderer把這個(gè)eventListener和eventHandler封裝成了EventDispatcher鲜戒,EventDispatcher的作用是利用eventHandler使回調(diào)在相應(yīng)的線程中去執(zhí)行,一般在主線程里抹凳。
比如onRenderedFirstFrame事件遏餐,事件發(fā)生時(shí),MediaCodecVideoRenderer調(diào)用 eventDispatcher.renderedFirstFrame(surface); 通過handler在相應(yīng)線程里調(diào)用(listener).onRenderedFirstFrame(surface)赢底,實(shí)現(xiàn)了監(jiān)聽失都。
SimpleExoPlayer里對這些事件監(jiān)聽到了,怎么傳給UI層的PlayerView?看看SimpleExoPlayer里onRenderedFirstFrame對回調(diào)怎么處理的:
private final CopyOnWriteArraySet<com.google.android.exoplayer2.video.VideoListener>
videoListeners;
public void onRenderedFirstFrame(Surface surface) {
if (SimpleExoPlayer.this.surface == surface) {
for (com.google.android.exoplayer2.video.VideoListener videoListener : videoListeners) {
videoListener.onRenderedFirstFrame();
}
}
}
@Override
public void addVideoListener(com.google.android.exoplayer2.video.VideoListener listener) {
// Don't verify application thread. We allow calls to this method from any thread.
Assertions.checkNotNull(listener);
videoListeners.add(listener);
}
答案在PlayView里調(diào)用addVideoListener幸冻。如果要擴(kuò)展監(jiān)聽粹庞,只需要在相應(yīng)的listerner里加入新的事件。