什么是混音
多个应用程序播放,每个APP端都会创建AudioTrack,每个AudioTrack都会通过共享内存和播放线程的Track来传递数据,每个应用发送的音频数据格式可能都不相同,而声卡仅支持固定的几种格式,除非发送的格式就是声卡本身支持的格式,否则这里都需要进行重采样/混音(playbackthread中使用mAudioMixer的一些操作把硬件不支持的音频格式转化为硬件支持的音频格式,这个过程叫做重采样)。
源码分析
先从PlayBackThread看起,总结起来一共做了着几件事
- 开启threadLoop线程
- 没数据挂起等待数据
- 有数据分别执行
prepareTracks_l
,threadLoop_mix
,threadLoop_write
// 构造方法执行,调用这个方法
void AudioFlinger::PlaybackThread::onFirstRef()
{
// 开启线程
run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO);
}
bool AudioFlinger::PlaybackThread::threadLoop() {
Mutex::Autolock _l(mLock);
// lyh 处理配置信息
processConfigEvents_l();
if ((mActiveTracks.isEmpty() && systemTime() > mStandbyTimeNs) ||
isSuspended()) {
// put audio hardware into standby after short delay
if (shouldStandby_l()) {
threadLoop_standby();
}
if (mActiveTracks.isEmpty() && mConfigEvents.isEmpty()) {
IPCThreadState::self()->flushCommands();
// lyh 线程休眠点,直到广播唤醒
mWaitWorkCV.wait(mLock);
continue;
}
}
// lyh 混音准备 返回READY
mMixerStatus = prepareTracks_l(&tracksToRemove);
if (mMixerStatus == MIXER_TRACKS_READY) {
// lyh 混音准备好,开始混音
threadLoop_mix();
}
if (mMixerBufferValid) {
// lyh 把数据从thread.mMixerBuffer复制到thread.mSinkBuffer
memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat,
mNormalFrameCount * (mChannelCount + mHapticChannelCount));
}
if (!waitingAsyncCallback()) {
// lyh 音频输出
ret = threadLoop_write();
}
}
从MixerThread作为例子来看prepareTracks_l
,threadLoop_mix
,threadLoop_write
prepareTracks_l
在准备混音的过程主要做了几件事情:
- 设置混音所需要的参数,包括:音量,混音的源buffer,目的buffer,音频格式,是否重采样等。
- 删除被加入tracksToRemove的track
- 返回当前状态mMixerStatus
AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(){
// lyh 状态修改为空闲
mixer_state mixerStatus = MIXER_IDLE;
for (size_t i=0 ; i<count ; i++) {
const sp<Track> t = mActiveTracks[i];
Track* const track = t.get();
size_t framesReady = track->framesReady();
// lyh 进行AudioMixer参数设置
if ((framesReady >= minFrames) && track->isReady() &&
!track->isPaused() && !track->isTerminated())
{
// lyh 设置可以混音
mAudioMixer->enable(trackId);
// lyh 音轨 左 右 aux
mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME0, &vlf);
mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME1, &vrf);
mAudioMixer->setParameter(trackId, param, AudioMixer::AUXLEVEL, &vaf);
// lyh 音频格式
mAudioMixer->setParameter(
trackId,
AudioMixer::TRACK,
AudioMixer::FORMAT, (void *)track->format());
// lyh 音轨mask,哪个需要或者不需要混音
mAudioMixer->setParameter(
trackId,
AudioMixer::TRACK,
AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask());
mAudioMixer->setParameter(
trackId,
AudioMixer::TRACK,
AudioMixer::MIXER_CHANNEL_MASK,
(void *)(uintptr_t)(mChannelMask | mHapticChannelMask));
// lyh 重采样
mAudioMixer->setParameter(
trackId,
AudioMixer::RESAMPLE,
AudioMixer::SAMPLE_RATE,
(void *)(uintptr_t)reqSampleRate);
AudioPlaybackRate playbackRate = proxy->getPlaybackRate();
mAudioMixer->setParameter(
trackId,
AudioMixer::TIMESTRETCH,
AudioMixer::PLAYBACK_RATE,
&playbackRate);
if (mMixerBufferEnabled
&& (track->mainBuffer() == mSinkBuffer
|| track->mainBuffer() == mMixerBuffer)) {
mAudioMixer->setParameter(
trackId,
AudioMixer::TRACK,
AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
// lyh 目的buffer
mAudioMixer->setParameter(
trackId,
AudioMixer::TRACK,
AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer);
// TODO: override track->mainBuffer()?
mMixerBufferValid = true;
} else {
mAudioMixer->setParameter(
trackId,
AudioMixer::TRACK,
AudioMixer::MIXER_FORMAT, (void *)EFFECT_BUFFER_FORMAT);
mAudioMixer->setParameter(
trackId,
AudioMixer::TRACK,
AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
}
mAudioMixer->setParameter(
trackId,
AudioMixer::TRACK,
AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
mAudioMixer->setParameter(
trackId,
AudioMixer::TRACK,
AudioMixer::HAPTIC_ENABLED, (void *)(uintptr_t)track->getHapticPlaybackEnabled());
mAudioMixer->setParameter(
trackId,
AudioMixer::TRACK,
AudioMixer::HAPTIC_INTENSITY, (void *)(uintptr_t)track->getHapticIntensity());
// lyh 混音准备好了
mixerStatus = MIXER_TRACKS_READY;
}
}
// lyh 从mActiveTracks移除
removeTracks_l(*tracksToRemove);
return mixerStatus;
}
threadLoop_mix
void AudioFlinger::MixerThread::threadLoop_mix()
{
// mix buffers...
// lyh 开启混音,调用AudioMixer.cpp#preProcess 和 postProcess
mAudioMixer->process();
// lyh 处理了多少数据
mCurrentWriteLength = mSinkBufferSize;
}
threadLoop_write
ssize_t AudioFlinger::MixerThread::threadLoop_write() {
return PlaybackThread::threadLoop_write();
}
ssize_t AudioFlinger::PlaybackThread::threadLoop_write()
{
ssize_t bytesWritten;
if (mNormalSink != 0) {
// lyh 将buffer写到声卡上
ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count);
}
mNumWrites++;
mInWrite = false;
if (mStandby) {
mThreadMetrics.logBeginInterval();
mStandby = false;
}
// lyh 返回写入的数据量
return bytesWritten;
}
总结下过程
prepareTracks_l
- 确定enabled track, disabled track
- 对于enabled track, 设置mState.tracks[x]中的参数
threadLoop_mix
- 处理数据(比如重采样)、混音
- 确定hook:逐个分析mState.tracks[x]的数据, 根据它的格式确定tracks[x].hook,再确定总的mState.hook (这里hook方面的比较抽象,暂时还不能很好的理解)
- 调用hook:调用总的mState.hook即可, 它会再去调用每一个mState.tracks[x].hook
- 混音后的数据会放在mState.outputTemp临时Buffer中
- 然后转换格式后存入 thread.mMixerBuffer
memcpy_by_audio_format
- 把数据从thread.mMixerBuffer或thread.mEffectBuffer复制到thread.mSinkBuffer
threadLoop_write
- 把thread.mSinkBuffer写到声卡上
threadLoop_exit