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设置给了它。