一、 简介:
awesomeplayer中的音频处理是如下分级的:
AwesomePlayer/
└── AudioPlayer
├── AudioSink
└── AudioTrack
二、 awesomeplayer流程:
上层mediaplayer调用start指令之后,stagefrightplayer调用的是awesomeplayer的play指令:
status_t StagefrightPlayer::start() {
ALOGV("start");
/* 调用awesomeplayer的play指令 */
return mPlayer->play();
}
status_t AwesomePlayer::play_l() {
...
if (mAudioSource != NULL) {
if (mAudioPlayer == NULL) {
/* 1.创建audioplayer */
createAudioPlayer_l();
}
...
if (mVideoSource == NULL) {
/* 2.开启audioplayer */
status_ t err = startAudioPlayer_l(
false /* sendErrorNotification */);
...
}
...
}
}
...
return OK;
}
简化了awesomeplayer的代码,实际上play操作就做了两个操作,创建audioplayer,并且开启audioplayer进行音频数据处理;
三、 audioplayer流程:
awesomeplayer调用createAudioPlayer_l发起audioplayer的创建:
void AwesomePlayer::createAudioPlayer_l()
{
...
mAudioPlayer = new AudioPlayer(mAudioSink, flags, this);
mAudioPlayer->setSource(mAudioSource);
...
}
create操作仅仅是完成参数列表的初始化;
接下来看startAudioPlayer_l函数:
status_t AwesomePlayer::startAudioPlayer_l(bool sendErrorNotification) {
...
err = mAudioPlayer->start(
true /* sourceAlreadyStarted */);
...
}
audioplayer中的start函数比较长:
status_t AudioPlayer::start(bool sourceAlreadyStarted) {
...
/* 这里的mSource就是omxsource */
mFirstBufferResult = mSource->read(&mFirstBuffer, &options);
if (mFirstBufferResult == INFO_FORMAT_CHANGED) {
ALOGV("INFO_FORMAT_CHANGED!!!");
CHECK(mFirstBuffer == NULL);
mFirstBufferResult = OK;
mIsFirstBuffer = false;
} else {
mIsFirstBuffer = true;
}
//使用两种方式处理数据,一种是audiosink,一种是audiotrack
/* 1.以audiosink的方式处理 */
status_t err = mAudioSink->open(
mSampleRate, numChannels, channelMask, audioFormat,
DEFAULT_AUDIOSINK_BUFFERCOUNT,
&AudioPlayer::AudioSinkCallback,
this,
(audio_output_flags_t)flags,
useOffload() ? &offloadInfo : NULL);
...
/* open成功之后调用start */
err = mAudioSink->start();
...
/* 2.自建audiotrack处理音频 */
mAudioTrack = new AudioTrack(
AUDIO_STREAM_MUSIC, mSampleRate, AUDIO_FORMAT_PCM_16_BIT, audioMask,
0 /*frameCount*/, AUDIO_OUTPUT_FLAG_NONE, &AudioCallback, this,
0 /*notificationFrames*/);
...
/* 将audiotrack置为start状态 */
mAudioTrack->start();
...
}
四、 audiosink是什么鬼?
sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
player_type playerType)
{
...
if (!p->hardwareOutput()) {
mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
mPid, mAudioAttributes);
static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
}
...
}
使用AudioOutput对象去构造audiosink,这里会一直调用到awesomeplayer中的setAudioSink:
void AwesomePlayer::setAudioSink(
const sp<MediaPlayerBase::AudioSink> &audioSink) {
Mutex::Autolock autoLock(mLock);
mAudioSink = audioSink;
}
所以,在mediaplayerservice里面实例化的audiooutput对象,就是awesomeplayer中的audiosink;
status_t MediaPlayerService::AudioOutput::start()
{
ALOGV("start");
if (mCallbackData != NULL) {
mCallbackData->endTrackSwitch();
}
if (mTrack != 0) {
mTrack->setVolume(mLeftVolume, mRightVolume);
mTrack->setAuxEffectSendLevel(mSendLevel);
return mTrack->start();
}
ALOGV("start no init !!!!");
return NO_INIT;
}
五、 总结:
1. 整个音频处理是由mediaplayer的start指令发起的,stagefrightplayer的play指令响应的;
2. awesomeplayer的音频处理是由audioplayer来完成的;
3. audioplayer执行 的操作有两个,一个是使用omx解码,另一个是使用audiotrack将pcm数据播出;
4. audioplayer可以直接创建audiotrack或者使用audiosink进行pcm数据的播放,实际运行都是后者;
5. audiosink是mediaplayerservice注册的audiooutput对象,其根本还是使用audiotrack进行pcm数据播放;