1.MediaPlayer初始化流程
EventHandler是后面处理数据回调的handler. 在AudioFlinger.cpp中获取nextUniqueId:
audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
{
// This is the internal API, so it is OK to assert on bad parameter.
LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
for (int retry = 0; retry < maxRetries; retry++) {
// The cast allows wraparound from max positive to min negative instead of abort
uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
(uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
// allow wrap by skipping 0 and -1 for session ids
if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
return (audio_unique_id_t) (base | use);
}
}
// We have no way of recovering from wraparound
LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
// TODO Use a floor after wraparound. This may need a mutex.
}
2. 设置播放视频路径setDataSource 接口分析
<1>. 在MediaPlayerService的setDataSource_pre中有获取IOmx监听服务是否挂掉,创建NuPlayerDriver,监听音频路由变化,创建AudioOutput:
通过上面过程可知sp<MediaPlayerBase> p是NuPlayerDriver类:
NuPlayerDriver中的mAudioSink就是AudioOutput类,它的mPlayer是NuPlayer这个类。
<2>. NuPlayerDriver后面调用到 NuPlayer中去了,下面看下NuPlayer的setDataSourceAsync函数
在这个函数中创建了一个GenericSource对象,并且将fd设置给了它。
3. 设置setSurface
在NuPlayer的setVideoSurfaceTextureAsync函数中,调用了Message的kWhatSetVideoSurface,在这个Message处理中创建了FlushDecoderAction、SetSurfaceAction 、SimpleAction、ResumeDecoderAction这些的对象,并且在processDeferredActions函数中执行了各个Action的execute函数,这块的分析放到了prepare中分析,主要是初始化NuPlayerDecoder.cpp,NuPlayerCCDecoder.cpp,MediaCodec.cpp
4. prepare流程分析流程分析
<1>. 在jni中android_media_MediaPlayer_prepare函数,会重新设置surface,然后再调用prepare.这个setVideoSurfaceTexture跟setSurface里面的 mp->setVideoSurfaceTexture(new_st);流程是一样的:
参考:
1. Android MediaPlayer播放视频详细步骤_APDL_10的博客-CSDN博客_android mediaplayer