Android音频数据传输

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

MediaPlayer那边就不看了,从AudioTrack开始研究。

1、AudioTrack::write函数

调用函数obtainBuffer获取到一块buffer,然后把传入的数据copy到获取的buffer中。

2、AudioTrack::obtainBuffer函数

该函数的主要功能就是对传入的audioBuffer进行赋值。

看看audioBuffer的类型:

class Buffer

{

public:

enum {

MUTE = 0x00000001

};

uint32_t flags;

int channelCount;

int format;

size_t frameCount;

size_t size;

union {

void* raw;

short* i16;

int8_t* i8;

};

};

其中存放数据的是下面这个东东:

union {

void* raw;

short* i16;

int8_t* i8;

};

对这块东东赋值的代码如下:

audioBuffer->raw = (int8_t *)cblk->buffer(u);

先看其中cblk的来历:

audio_track_cblk_t* cblk = mCblk;

mCblk的赋值在函数AudioTrack::createTrack中:

mCblk = static_cast(cblk->pointer());

cblk的由来:

sp cblk = track->getCblk();

track的由来:

sp track = audioFlinger->createTrack(getpid(),

streamType,

sampleRate,

format,

channelCount,

frameCount,

((uint16_t)flags) << 16,

sharedBuffer,

output,

&mSessionId,

&status);

函数AudioFlinger::createTrack返回的是一个TrackHandle对象:trackHandle = new TrackHandle(track);

return trackHandle;

track的由来:

track = thread->createTrack_l(client, streamType, sampleRate, format, channelCount, frameCount, sharedBuffer, lSessionId, &lStatus);

函数AudioFlinger::PlaybackThread::createTrack_l返回的是一个Track对象:track = new Track(this, client, streamType, sampleRate, format,

channelCount, frameCount, sharedBuffer, sessionId);

return track;

看看函数TrackHandle::getCblk() :

return mTrack->getCblk();

mTrack就是作为构造函数传入的track对象。

函数AudioFlinger::ThreadBase::TrackBase::getCblk() 的实现:

return mCblkMemory;

mCblkMemory的赋值在构造函数AudioFlinger::ThreadBase::TrackBase::TrackBase中:mCblkMemory = client->heap()->allocate(size);

mCblk = static_cast(mCblkMemory->pointer()); // 这个成员变量也很重要

client是构造函数参数:

const sp& client

函数AudioFlinger::Client::heap:

return mMemoryDealer;

mMemoryDealer的赋值在函数AudioFlinger::Client::Client中:

mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client"))

看看函数MemoryDealer::allocate:

sp MemoryDealer::allocate(size_t size)

{

sp memory;

// allocator()直接返回mAllocator

// mAllocator的赋值在构造函数中:mAllocator(new SimpleBestFitAllocator(size))

/×函数SimpleBestFitAllocator::allocate的实现:

size_t SimpleBestFitAllocator::allocate(size_t size, uint32_t flags)

{

Mutex::Autolock _l(mLock);

// 暂止

ssize_t offset = alloc(size, flags);

return offset;

}

×/

const ssize_t offset = allocator()->allocate(size);

if (offset >= 0) {

// heap()直接返回mHeap

// mHeap的赋值在构造函数中:mHeap(new MemoryHeapBase(size, 0, name)) memory = new Allocation(this, heap(), offset, size);

}

return memory;

}

可见前面的mCblkMemory其实就是一个Allocation对象。

相关文档
最新文档