标签:
在分析AudioTrack的时候,第一步会new AudioTrack,并调用他的set方法。在set方法的最后调用了createTrack_l创建音轨。我们现在来分析createTrack_l的流程。
在分析createTrack_l之前,我们先来了解Android音频流的从PCM到输出的路线。首先,我们的PCM音频数据一般会在用户端,而混音会在AudioFlinger端,因此需要把PCM数据传送给AudioFlinger,因此需要开辟出一块内存用于数据传送;数据到了AudioFlinger之后,可以给PCM数据调节音量,增加音效等(即混音),因此还需要一块内存用于音效处理,这块buffer在getOutput内已经开辟;混音完成后即可把PCM数据输出给音频设备进行播放。
creatTrack_l的任务主要是创建音轨,即开辟出数据传送的内存。具体实现是创建出一块share buffer,这块buffer既可以被AudioTrack写入,又可以被AudioFlinger读取进行混音。
createTrack总体可以分为三个步骤:
AudioTrack按照如下方式获取framecount
status_t AudioTrack::createTrack_l( status = AudioSystem::getLatency(output, streamType, &afLatency); status = AudioSystem::getFrameCount(output, streamType, &afFrameCount); status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate); if (!audio_is_linear_pcm(format)) { if (sharedBuffer != 0) { // Same comment as below about ignoring frameCount parameter for set() frameCount = sharedBuffer->size(); } else if (frameCount == 0) { frameCount = afFrameCount; } if (mNotificationFramesAct != frameCount) { mNotificationFramesAct = frameCount; } } else if (sharedBuffer != 0) { // user share buffer,we donot neet to allocate // Ensure that buffer alignment matches channel count // 8-bit data in shared memory is not currently supported by AudioFlinger size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2; if (mChannelCount > 1) { alignment <<= 1; } if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) { return BAD_VALUE; } frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t); } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) { // non-fast uint32_t minBufCount = 2; if (minBufCount <= nBuffering) { minBufCount = nBuffering; } // calculate buffer size by param from AudioFlinger size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate; if (frameCount == 0) { frameCount = minFrameCount; } else if (frameCount < minFrameCount) { frameCount = minFrameCount; } } else { // For fast tracks, the frame count calculations and checks are done by server }
先看一下AudioTrack计算framecount时的式子:
minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
afFrameCount与afSampleRate都是从AudioFlinger得到的两个参数。
因此有如下公式:
$BufferSeconds = \frac{afFrameCount}{afSampleRate} = \frac{frameCount}{sampleRate}$
计算出buffer中包含多少秒音频数据。
下面是一个buffer实例,虽然sample rate一般都会是44100,但是为了方便画图,下面以5代替
AudioFlinger获取AfFrameCount的过程如下:
//AudioFlinger.cpp size_t AudioFlinger::frameCount(audio_io_handle_t output) const { return thread->frameCount(); } //Thread.h virtual size_t frameCount() const { return mNormalFrameCount; } //Thread.cpp void AudioFlinger::PlaybackThread::readOutputParameters() { mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; mNormalFrameCount = multiplier * mFrameCount; } //Audio_hw.c #define SHORT_PERIOD_SIZE 512 static size_t out_get_buffer_size_low_latency(const struct audio_stream *stream) { struct tuna_stream_out *out = (struct tuna_stream_out *)stream; /* take resampling into account and return the closest majoring multiple of 16 frames, as audioflinger expects audio buffers to be a multiple of 16 frames. Note: we use the default rate here from pcm_config_tones.rate. */ size_t size = (SHORT_PERIOD_SIZE * DEFAULT_OUT_SAMPLING_RATE) / pcm_config_tones.rate; size = ((size + 15) / 16) * 16; return size * audio_stream_frame_size((struct audio_stream *)stream); }
获取与AfSampleRate的过程如下:
//AudioFlinger.cpp uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const { return thread->sampleRate(); } //Thread.h uint32_t sampleRate() const { return mSampleRate; } //Thread.cpp where sample rate be initialized void AudioFlinger::PlaybackThread::readOutputParameters() { mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); } //Audio_hw.c #define DEFAULT_OUT_SAMPLING_RATE 44100 // 48000 is possible but interacts poorly with HDMI static uint32_t out_get_sample_rate(const struct audio_stream *stream) { return DEFAULT_OUT_SAMPLING_RATE; }
而minFrameCount则包含了minBufferCount,即share buffer有多少个Mixer Buffer的大小
// The client‘s AudioTrack buffer is divided into n parts for purpose of wakeup by server, where // n = 1 fast track; nBuffering is ignored // n = 2 normal track, no sample rate conversion // n = 3 normal track, with sample rate conversion // (pessimistic; some non-1:1 conversion ratios don‘t actually need triple-buffering) // n > 3 very high latency or very small notification interval; nBuffering is ignored
AudioTrack是通过调用AudioFlinger的createTrack的方法来实现创建share buffer。createTrack的步骤如下:
sp<IAudioTrack> AudioFlinger::createTrack(...) { PlaybackThread *thread = checkPlaybackThread_l(output); track = thread->createTrack_l(client, streamType, sampleRate, format, channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus); trackHandle = new TrackHandle(track); return trackHandle; }
还记得getOutput时所创建的PlaybackThread吗?PlaybackThread会在创建MixerThread时一同被创建。在getOutput内,我们把该thread放进了mPlaybackThreads进行维护。现在我们有需要把它取出来。
AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const { return mPlaybackThreads.valueFor(output).get(); }
在createTrack_l内调用了new Track来实现创建share buffer
sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(...) { track = new Track(this, client, streamType, sampleRate, format, channelMask, frameCount, sharedBuffer, sessionId, uid, *flags); }
Track的父类是TrackBase,因此会先构建TrackBase对象
// TrackBase constructor must be called with AudioFlinger::mLock held AudioFlinger::ThreadBase::TrackBase::TrackBase(...) { // buffer header size_t size = sizeof(audio_track_cblk_t); // buffer content size size_t bufferSize = (sharedBuffer == 0 ? roundup(frameCount) : frameCount) * mFrameSize; if (sharedBuffer == 0) { size += bufferSize; } if (client != 0) { //allocate share buffer mCblkMemory = client->heap()->allocate(size); if (mCblkMemory != 0) { mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); // can‘t assume mCblk != NULL } else { ALOGE("not enough memory for AudioTrack size=%u", size); client->heap()->dump("AudioTrack"); return; } } else { // this syntax avoids calling the audio_track_cblk_t constructor twice mCblk = (audio_track_cblk_t *) new uint8_t[size]; // assume mCblk != NULL } // construct the shared structure in-place. if (mCblk != NULL) { // this is header above buffer content new(mCblk) audio_track_cblk_t(); // clear all buffers mCblk->frameCount_ = frameCount; if (sharedBuffer == 0) { mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); memset(mBuffer, 0, bufferSize); } else { mBuffer = sharedBuffer->pointer(); } } }
其中,创建出来的buffer需要包含存放Audio PCM data的share buffer,还需要包含audio_track_cblk_t这个buffer头。调用heap->allocate这个函数来创建share buffer,buffer头部调用new(mCblk) audio_track_cblk_t;这种定位new的方式来创建。buffer的结构如下:
new Track在构造函数体内,会创建AudioTrackServerProxy,这个对象会被用作AudioFlinger这边的buffer操作,由于share buffer是跨线程,甚至是跨进程的,而Proxy可以保证buffer访问的线程安全。
AudioFlinger::PlaybackThread::Track::Track( { mAudioTrackServerProxy = new AudioTrackServerProxy(mCblk, mBuffer, frameCount,mFrameSize); mServerProxy = mAudioTrackServerProxy; }
由于share buffer不止会在AudioFlinger这端被读取,还会在AudioTrack这端被写入,因此创建出来的Track需要被传送回AudioTrack。而在binder间传送对象只有binder对象,因此需要构建binder对象TrackHandle,返回给AudioTrack。
sp<IAudioTrack> AudioFlinger::createTrack(...) { trackHandle = new TrackHandle(track); } // TrackHandle is a BnBinder object class TrackHandle : public android::BnAudioTrack { ... }
至此,createTrack_l在AudioFlinger这端的工作基本完成了。
有ServerProxy,相应地也会有ClientProxy,AudioTrackClientProxy就是在AudioTrack端可以对Track(share buffer)进行操作的类。
从AudioFlinger的createTrack返回TrackHandle后,就能通过TrackHandle的相关函数获得Track的信息,如buffer的起始地址等。用这些信息构造AudioTrackClientProxy.
status_t AudioTrack::createTrack_l(...) { sp<IAudioTrack> track = audioFlinger->createTrack(...); sp<IMemory> iMem = track->getCblk(); audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer()); mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF); }
最后,总结一下各个对象间的关系。
AudioFlinger:
AudioTrack:
createTrack_l的总体流程如下:
标签:
原文地址:http://www.cnblogs.com/TaigaCon/p/4772066.html