AudioTrack与AudioFlinger在两个不同进程,他们之间要通过共享内存进行音频的数据交换。
交换的实现通过环形缓冲去来实现,貌似没有同步机制,从实验结果来看,AudioTrack写满缓冲区后AudioFlinger就会去读取。
数据交换的实现主要在AudioTrackShared.cpp中实现,包括AudioTrackClientProxy和AudioTrackServerProxy。
两边数据通过cblk的flag来进行数据的读写。
//两边操作数据的接口
class Proxy : public RefBase {
...
public:
struct Buffer {
size_t mFrameCount; // number of frames available in this buffer
void* mRaw; // pointer to first frame
size_t mNonContig; // number of additional non-contiguous frames available
};
protected:
// 共享内存的一些信息
audio_track_cblk_t* const mCblk; // the control block
void* const mBuffers; // starting address of buffers
const size_t mFrameCount; // not necessarily a power of 2
const size_t mFrameSize; // in bytes
const size_t mFrameCountP2; // mFrameCount rounded to power of 2, streaming mode
const bool mIsOut; // true for AudioTrack, false for AudioRecord
const bool mClientInServer; // true for OutputTrack, false for AudioTrack & AudioRecord
bool mIsShutdown; // latch set to true when shared memory corruption detected
size_t mUnreleased; // unreleased frames remaining from most recent obtainBuffer
};
客户端的流程:获取Buf