| streamType | the type of the audio stream. See STREAM_VOICE_CALL,STREAM_SYSTEM,STREAM_RING,STREAM_MUSIC,STREAM_ALARM, andSTREAM_NOTIFICATION. |
| sampleRateInHz | the sample rate expressed in Hertz. |
| channelConfig | describes the configuration of the audio channels. SeeCHANNEL_OUT_MONO andCHANNEL_OUT_STEREO |
| audioFormat | the format in which the audio data is represented. SeeENCODING_PCM_16BIT andENCODING_PCM_8BIT |
| bufferSizeInBytes | the total size (in bytes) of the buffer where audio data is read from for playback. If using the AudioTrack in streaming mode, you can write data into this buffer in smaller chunks than this size. If using the AudioTrack in static mode, this is the maximum size of the sound that will be played for this instance. SeegetMinBufferSize(int, int, int) to determine the minimum required buffer size for the successful creation of an AudioTrack instance in streaming mode. Using values smaller than getMinBufferSize() will result in an initialization failure. |
| mode | streaming or static buffer. See MODE_STATIC andMODE_STREAM |
| sessionId | Id of audio session the AudioTrack must be attached to |
AudioTrack有两种数据加载模式:
- MODE_STREAM
在这种模式下,应用程序持续地write音频数据流到AudioTrack中,并且write动作将阻塞直到数据流从Java层传输到native层,同时加入到播放队列中。这种模式适用于播放大音频数据,但该模式也造成了一定的延时;
- MODE_STATIC
在播放之前,先把所有数据一次性write到AudioTrack的内部缓冲区中。适用于播放内存占用小、延时要求较高的音频数据。
frameworksasecorejniandroid_media_AudioTrack.cpp
001.staticintandroid_media_AudioTrack_native_setup(JNIEnv *env, jobject thiz, jobject weak_this,jint streamType, jint sampleRateInHertz, jint javaChannelMask,002.jint audioFormat, jint buffSizeInBytes, jint memoryMode, jintArray jSession)003.{004.ALOGV("sampleRate=%d, audioFormat(from Java)=%d, channel mask=%x, buffSize=%d",005.sampleRateInHertz, audioFormat, javaChannelMask, buffSizeInBytes);006.intafSampleRate;//采样率007.intafFrameCount;//帧数008.//通过AudioSystem从AudioPolicyService中读取对应音频流类型的帧数009.if(AudioSystem::getOutputFrameCount(&afFrameCount, (audio_stream_type_t) streamType) != NO_ERROR) {010.ALOGE("Error creating AudioTrack: Could not get AudioSystem frame count.");011.returnAUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM;012.}013.//通过AudioSystem从AudioPolicyService中读取对应音频流类型的采样率014.if(AudioSystem::getOutputSamplingRate(&afSampleRate, (audio_stream_type_t) streamType) != NO_ERROR) {015.ALOGE("Error creating AudioTrack: Could not get AudioSystem sampling rate.");016.returnAUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM;017.}018.// Java channel masks don't map directly to the native definition, but it's a simple shift019.// to skip the two deprecated channel configurations "default" and "mono".020.uint32_t nativeChannelMask = ((uint32_t)javaChannelMask) >>2;021.//判断是否为输出通道022.if(!audio_is_output_channel(nativeChannelMask)) {023.ALOGE("Error creating AudioTrack: invalid channel mask.");024.returnAUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK;025.}026.//得到通道个数,popcount函数用于统计一个整数中有多少位为1027.intnbChannels = popcount(nativeChannelMask);028.// check the stream type029.audio_stream_type_t atStreamType;030.switch(streamType) {031.caseAUDIO_STREAM_VOICE_CALL:032.caseAUDIO_STREAM_SYSTEM:033.caseAUDIO_STREAM_RING:034.caseAUDIO_STREAM_MUSIC:035.caseAUDIO_STREAM_ALARM:036.caseAUDIO_STREAM_NOTIFICATION:037.caseAUDIO_STREAM_BLUETOOTH_SCO:038.caseAUDIO_STREAM_DTMF:039.atStreamType = (audio_stream_type_t) streamType;040.break;041.default:042.ALOGE("Error creating AudioTrack: unknown stream type.");043.returnAUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE;044.}045.// This function was called from Java, so we compare the format against the Java constants046.if((audioFormat != javaAudioTrackFields.PCM16) && (audioFormat != javaAudioTrackFields.PCM8)) {047.ALOGE("Error creating AudioTrack: unsupported audio format.");048.returnAUDIOTRACK_ERROR_SETUP_INVALIDFORMAT;049.}050.// for the moment 8bitPCM in MODE_STATIC is not supported natively in the AudioTrack C++ class so we declare everything as 16bitPCM, the 8->16bit conversion for MODE_STATIC will be handled in android_media_AudioTrack_native_write_byte()051.if((audioFormat == javaAudioTrackFields.PCM8)052.&& (memoryMode == javaAudioTrackFields.MODE_STATIC)) {053.ALOGV("android_media_AudioTrack_native_setup(): requesting MODE_STATICfor8bit054.buff size of %dbytes, switching to 16bit, buff size of %dbytes",055.buffSizeInBytes,2*buffSizeInBytes);056.audioFormat = javaAudioTrackFields.PCM16;057.// we will need twice the memory to store the data058.buffSizeInBytes *=2;059.}060.//根据不同的采样方式得到一个采样点的字节数061.intbytesPerSample = audioFormat == javaAudioTrackFields.PCM16 ?2:1;062.audio_format_t format = audioFormat == javaAudioTrackFields.PCM16 ?063.AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_8_BIT;064.//根据buffer大小反向计算帧数 , 一帧大小=一个采样点字节数 * 声道数065.intframeCount = buffSizeInBytes / (nbChannels * bytesPerSample);066.//判断参数的合法性067.jclass clazz = env->GetObjectClass(thiz);068.if(clazz == NULL) {069.ALOGE("Can't find %s when setting up callback.", kClassPathName);070.returnAUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;071.}072.if(jSession == NULL) {073.ALOGE("Error creating AudioTrack: invalid session ID pointer");074.returnAUDIOTRACK_ERROR;075.}076.jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);077.if(nSession == NULL) {078.ALOGE("Error creating AudioTrack: Error retrieving session id pointer");079.returnAUDIOTRACK_ERROR;080.}081.intsessionId = nSession[0];082.env->ReleasePrimitiveArrayCritical(jSession, nSession,0);083.nSession = NULL;084.// create the native AudioTrack object085.sp<AudioTrack> lpTrack =newAudioTrack();086.if(lpTrack == NULL) {087.ALOGE("Error creating uninitialized AudioTrack");088.returnAUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;089.}090.// 创建存储音频数据的容器091.AudioTrackJniStorage* lpJniStorage =newAudioTrackJniStorage();092.lpJniStorage->mStreamType = atStreamType;093.//将Java层的AudioTrack引用保存到AudioTrackJniStorage中094.lpJniStorage->mCallbackData.audioTrack_class = (jclass)env->NewGlobalRef(clazz);095.// we use a weak reference so the AudioTrack object can be garbage collected.096.lpJniStorage->mCallbackData.audioTrack_ref = env->NewGlobalRef(weak_this);097.lpJniStorage->mCallbackData.busy =false;098.//初始化不同模式下的native AudioTrack对象099.if(memoryMode == javaAudioTrackFields.MODE_STREAM) {//stream模式100.lpTrack->set(101.atStreamType,// stream type102.sampleRateInHertz,103.format,// word length, PCM104.nativeChannelMask,105.frameCount,106.AUDIO_OUTPUT_FLAG_NONE,107.audioCallback,108.&(lpJniStorage->mCallbackData),//callback, callback data (user)109.0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack110.0,//stream模式下的共享内存在AudioFlinger中创建111.true,// thread can call Java112.sessionId);// audio session ID113.}elseif(memoryMode == javaAudioTrackFields.MODE_STATIC) {//static模式114.// 为AudioTrack分配共享内存区域115.if(!lpJniStorage->allocSharedMem(buffSizeInBytes)) {116.ALOGE("Error creating AudioTrack in static mode: error creating mem heap base");117.gotonative_init_failure;118.}119.lpTrack->set(120.atStreamType,// stream type121.sampleRateInHertz,122.format,// word length, PCM123.nativeChannelMask,124.frameCount,125.AUDIO_OUTPUT_FLAG_NONE,126.audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user));127.0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack128.lpJniStorage->mMemBase,// shared mem129.true,// thread can call Java130.sessionId);// audio session ID131.}132.if(lpTrack->initCheck() != NO_ERROR) {133.ALOGE("Error initializing AudioTrack");134.gotonative_init_failure;135.}136.nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);137.if(nSession == NULL) {138.ALOGE("Error creating AudioTrack: Error retrieving session id pointer");139.gotonative_init_failure;140.}141.// read the audio session ID back from AudioTrack in case we create a new session142.nSession[0] = lpTrack->getSessionId();143.env->ReleasePrimitiveArrayCritical(jSession, nSession,0);144.nSession = NULL;145.{// scope for the lock146.Mutex::Autolock l(sLock);147.sAudioTrackCallBackCookies.add(&lpJniStorage->mCallbackData);148.}149.// save our newly created C++ AudioTrack in the "nativeTrackInJavaObj" field150.// of the Java object (in mNativeTrackInJavaObj)151.setAudioTrack(env, thiz, lpTrack);152.// save the JNI resources so we can free them later153.//ALOGV("storing lpJniStorage: %x154.", (int)lpJniStorage);155.env->SetIntField(thiz, javaAudioTrackFields.jniData, (int)lpJniStorage);156.returnAUDIOTRACK_SUCCESS;157.// failures:158.native_init_failure:159.if(nSession != NULL) {160.env->ReleasePrimitiveArrayCritical(jSession, nSession,0);161.}162.env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_class);163.env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_ref);164.delete lpJniStorage;165.env->SetIntField(thiz, javaAudioTrackFields.jniData,0);166.returnAUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;167.}1. 检查音频参数;
2. 创建一个AudioTrack(native)对象;
3. 创建一个AudioTrackJniStorage对象;
4. 调用set函数初始化AudioTrack;
buffersize = frameCount * 每帧数据量 = frameCount * (Channel数 * 每个Channel数据量)
构造native AudioTrack
frameworksavmedialibmediaAudioTrack.cpp
1.AudioTrack::AudioTrack(): mStatus(NO_INIT),2.mIsTimed(false),3.mPreviousPriority(ANDROID_PRIORITY_NORMAL),4.mPreviousSchedulingGroup(SP_DEFAULT),5.mCblk(NULL)6.{7.}构造AudioTrackJniStorage
AudioTrackJniStorage是音频数据存储的容器,是对匿名共享内存的封装。
01.struct audiotrack_callback_cookie {02.jclass audioTrack_class;03.jobject audioTrack_ref;//Java层AudioTrack对象引用04.bool busy;//忙判断05.Condition cond;//互斥量06.};07.08.classAudioTrackJniStorage {09.public:10.sp<MemoryHeapBase> mMemHeap;11.sp<MemoryBase> mMemBase;12.audiotrack_callback_cookie mCallbackData;13.audio_stream_type_t mStreamType;14.15.AudioTrackJniStorage() {16.mCallbackData.audioTrack_class =0;17.mCallbackData.audioTrack_ref =0;18.mStreamType = AUDIO_STREAM_DEFAULT;19.}20.21.~AudioTrackJniStorage() {22.mMemBase.clear();23.mMemHeap.clear();24.}25./**26.* 分配一块指定大小的匿名共享内存27.* @param sizeInBytes:匿名共享内存大小28.* @return29.*/30.bool allocSharedMem(intsizeInBytes) {31.//创建一个匿名共享内存32.mMemHeap =newMemoryHeapBase(sizeInBytes,0,"AudioTrack Heap Base");33.if(mMemHeap->getHeapID() <0) {34.returnfalse;35.}36.mMemBase =newMemoryBase(mMemHeap,0, sizeInBytes);37.returntrue;38.}39.};40.41./**42.* 创建匿名共享内存区域43.* @param size:匿名共享内存大小44.* @param flags:创建标志位45.* @param name:匿名共享内存名称46.*/47.MemoryHeapBase::MemoryHeapBase(size_t size, uint32_t flags,charconst* name)48.: mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),49.mDevice(0), mNeedUnmap(false), mOffset(0)50.{51.//获取内存页大小52.constsize_t pagesize = getpagesize();53.//字节对齐54.size = ((size + pagesize-1) & ~(pagesize-1));55./* 创建共享内存,打开/dev/ashmem设备,得到一个文件描述符 */56.intfd = ashmem_create_region(name == NULL ?"MemoryHeapBase": name, size);57.ALOGE_IF(fd<0,"error creating ashmem region: %s", strerror(errno));58.if(fd >=0) {59.//通过mmap将匿名共享内存映射到当前进程地址空间60.if(mapfd(fd, size) == NO_ERROR) {61.if(flags & READ_ONLY) {62.ashmem_set_prot_region(fd, PROT_READ);63.}64.}65.}66.}初始化AudioTrack
为AudioTrack设置音频参数信息,在Android4.4中,增加了一个参数transfer_type用于指定音频数据的传输方式,Android4.4定义了4种音频数据传输方式:
enum transfer_type {
TRANSFER_DEFAULT, // not specified explicitly; determine from the other parameters
TRANSFER_CALLBACK, // callback EVENT_MORE_DATA
TRANSFER_OBTAIN, // FIXME deprecated: call obtainBuffer() and releaseBuffer()
TRANSFER_SYNC, // synchronous write()
TRANSFER_SHARED, // shared memory
};
001./**002.* 初始化AudioTrack003.* @param streamType 音频流类型004.* @param sampleRate 采样率005.* @param format 音频格式006.* @param channelMask 输出声道007.* @param frameCount 帧数008.* @param flags 输出标志位009.* @param cbf Callback function. If not null, this function is called periodically010.* to provide new data and inform of marker, position updates, etc.011.* @param user Context for use by the callback receiver.012.* @param notificationFrames The callback function is called each time notificationFrames * PCM frames have been consumed from track input buffer.013.* @param sharedBuffer 共享内存014.* @param threadCanCallJava015.* @param sessionId016.* @return017.*/018.status_t AudioTrack::set(019.audio_stream_type_t streamType,020.uint32_t sampleRate,021.audio_format_t format,022.audio_channel_mask_t channelMask,023.intframeCountInt,024.audio_output_flags_t flags,025.callback_t cbf,026.void* user,027.intnotificationFrames,028.constsp<IMemory>& sharedBuffer,029.bool threadCanCallJava,030.intsessionId,031.transfer_type transferType,032.constaudio_offload_info_t *offloadInfo,033.intuid)034.{035.//设置音频数据传输类型036.switch(transferType) {037.caseTRANSFER_DEFAULT:038.if(sharedBuffer !=0) {039.transferType = TRANSFER_SHARED;040.}elseif(cbf == NULL || threadCanCallJava) {041.transferType = TRANSFER_SYNC;042.}else{043.transferType = TRANSFER_CALLBACK;044.}045.break;046.caseTRANSFER_CALLBACK:047.if(cbf == NULL || sharedBuffer !=0) {048.ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");049.returnBAD_VALUE;050.}051.break;052.caseTRANSFER_OBTAIN:053.caseTRANSFER_SYNC:054.if(sharedBuffer !=0) {055.ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");056.returnBAD_VALUE;057.}058.break;059.caseTRANSFER_SHARED:060.if(sharedBuffer ==0) {061.ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");062.returnBAD_VALUE;063.}064.break;065.default:066.ALOGE("Invalid transfer type %d", transferType);067.returnBAD_VALUE;068.}069.mTransfer = transferType;070.// FIXME "int" here is legacy and will be replaced by size_t later071.if(frameCountInt <0) {072.ALOGE("Invalid frame count %d", frameCountInt);073.returnBAD_VALUE;074.}075.size_t frameCount = frameCountInt;076.ALOGV_IF(sharedBuffer !=0,"sharedBuffer: %p, size: %d", sharedBuffer->pointer(),077.sharedBuffer->size());078.ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);079.AutoMutex lock(mLock);080.// invariant that mAudioTrack != 0 is true only after set() returns successfully081.if(mAudioTrack !=0) {082.ALOGE("Track already in use");083.returnINVALID_OPERATION;084.}085.mOutput =0;086.// 音频流类型设置087.if(streamType == AUDIO_STREAM_DEFAULT) {088.streamType = AUDIO_STREAM_MUSIC;089.}090.//根据音频流类型从AudioPolicyService中得到对应的音频采样率091.if(sampleRate ==0) {092.uint32_t afSampleRate;093.if(AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {094.returnNO_INIT;095.}096.sampleRate = afSampleRate;097.}098.mSampleRate = sampleRate;099.//音频格式设置100.if(format == AUDIO_FORMAT_DEFAULT) {101.format = AUDIO_FORMAT_PCM_16_BIT;102.}103.//如果没有设置声道,则默认设置为立体声通道104.if(channelMask ==0) {105.channelMask = AUDIO_CHANNEL_OUT_STEREO;106.}107.// validate parameters108.if(!audio_is_valid_format(format)) {109.ALOGE("Invalid format %d", format);110.returnBAD_VALUE;111.}112.// AudioFlinger does not currently support 8-bit data in shared memory113.if(format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer !=0) {114.ALOGE("8-bit data in shared memory is not supported");115.returnBAD_VALUE;116.}117.// force direct flag if format is not linear PCM118.// or offload was requested119.if((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)120.|| !audio_is_linear_pcm(format)) {121.ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)122.?"Offload request, forcing to Direct Output"123.:"Not linear PCM, forcing to Direct Output");124.flags = (audio_output_flags_t)125.// FIXME why can't we allow direct AND fast?126.((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);127.}128.// only allow deep buffering for music stream type129.if(streamType != AUDIO_STREAM_MUSIC) {130.flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);131.}132.//输出声道合法性检查133.if(!audio_is_output_channel(channelMask)) {134.ALOGE("Invalid channel mask %#x", channelMask);135.returnBAD_VALUE;136.}137.mChannelMask = channelMask;138.//计算声道个数139.uint32_t channelCount = popcount(channelMask);140.mChannelCount = channelCount;141.if(audio_is_linear_pcm(format)) {142.mFrameSize = channelCount * audio_bytes_per_sample(format);143.mFrameSizeAF = channelCount * sizeof(int16_t);144.}else{145.mFrameSize = sizeof(uint8_t);146.mFrameSizeAF = sizeof(uint8_t);147.}148./**149.* audio_io_handle_t是一个整形值,用于标示音频播放线程,这里更加音频参数150.* 从AudioFlinger中查找用于播放此音频的播放线程,并返回该播放线程的ID值151.*/152.audio_io_handle_t output = AudioSystem::getOutput(153.streamType,154.sampleRate, format, channelMask,155.flags,156.offloadInfo);157.if(output ==0) {158.ALOGE("Could not get audio output for stream type %d", streamType);159.returnBAD_VALUE;160.}161.//AudioTrack初始化162.mVolume[LEFT] =1.0f;163.mVolume[RIGHT] =1.0f;164.mSendLevel =0.0f;165.mFrameCount = frameCount;166.mReqFrameCount = frameCount;167.mNotificationFramesReq = notificationFrames;168.mNotificationFramesAct =0;169.mSessionId = sessionId;170.if(uid == -1|| (IPCThreadState::self()->getCallingPid() != getpid())) {171.mClientUid = IPCThreadState::self()->getCallingUid();172.}else{173.mClientUid = uid;174.}175.mAuxEffectId =0;176.mFlags = flags;177.mCbf = cbf;178.//如果设置了提供音频数据的回调函数,则启动AudioTrackThread线程来提供音频数据179.if(cbf != NULL) {180.mAudioTrackThread =newAudioTrackThread(*this, threadCanCallJava);181.mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO,0/*stack*/);182.}183.// create the IAudioTrack184.status_t status = createTrack_l(streamType,185.sampleRate,186.format,187.frameCount,188.flags,189.sharedBuffer,190.output,191.0/*epoch*/);192.if(status != NO_ERROR) {193.if(mAudioTrackThread !=0) {194.mAudioTrackThread->requestExit();// see comment in AudioTrack.h195.mAudioTrackThread->requestExitAndWait();196.mAudioTrackThread.clear();197.}198.//Use of direct and offloaded output streams is ref counted by audio policy manager.199.// As getOutput was called above and resulted in an output stream to be opened,200.// we need to release it.201.AudioSystem::releaseOutput(output);202.returnstatus;203.}204.mStatus = NO_ERROR;205.mStreamType = streamType;206.mFormat = format;207.mSharedBuffer = sharedBuffer;208.mState = STATE_STOPPED;209.mUserData = user;210.mLoopPeriod =0;211.mMarkerPosition =0;212.mMarkerReached =false;213.mNewPosition =0;214.mUpdatePeriod =0;215.AudioSystem::acquireAudioSessionId(mSessionId);216.mSequence =1;217.mObservedSequence = mSequence;218.mInUnderrun =false;219.mOutput = output;220.returnNO_ERROR;221.}我们知道,AudioPolicyService启动时加载了系统支持的所有音频接口,并且打开了默认的音频输出,打开音频输出时,调用AudioFlinger::openOutput()函数为当前打开的音频输出接口创建一个PlaybackThread线程,同时为该线程分配一个全局唯一的audio_io_handle_t值,并以键值对的形式保存在AudioFlinger的成员变量mPlaybackThreads中。在这里首先根据音频参数通过调用AudioSystem::getOutput()函数得到当前音频输出接口的PlaybackThread线程id号,同时传递给createTrack函数用于创建Track。AudioTrack在AudioFlinger中是以Track来管理的。不过因为它们之间是跨进程的关系,因此需要一个“桥梁”来维护,这个沟通的媒介是IAudioTrack。函数createTrack_l除了为AudioTrack在AudioFlinger中申请一个Track外,还会建立两者间IAudioTrack桥梁。
获取音频输出
获取音频输出就是根据音频参数如采样率、声道、格式等从已经打开的音频输出描述符列表中查找合适的音频输出AudioOutputDescriptor,并返回该音频输出在AudioFlinger中创建的播放线程id号,如果没有合适当前音频输出参数的AudioOutputDescriptor,则请求AudioFlinger打开一个新的音频输出通道,并为当前音频输出创建对应的播放线程,返回该播放线程的id号。具体过程请参考Android AudioPolicyService服务启动过程中的打开输出小节。
创建AudioTrackThread线程
初始化AudioTrack时,如果audioCallback为Null,就会创建AudioTrackThread线程。
AudioTrack支持两种数据输入方式:
1) Push方式:用户主动write,MediaPlayerService通常采用此方式;
2) Pull方式: AudioTrackThread线程通过audioCallback回调函数主动从用户那里获取数据,ToneGenerator就是采用这种方式;
01.bool AudioTrack::AudioTrackThread::threadLoop()02.{03.{04.AutoMutex _l(mMyLock);05.if(mPaused) {06.mMyCond.wait(mMyLock);07.// caller will check for exitPending()08.returntrue;09.}10.}11.//调用创建当前AudioTrackThread线程的AudioTrack的processAudioBuffer函数12.if(!mReceiver.processAudioBuffer(this)) {13.pause();14.}15.returntrue;16.}申请Track
音频播放需要AudioTrack写入音频数据,同时需要AudioFlinger完成混音,因此需要在AudioTrack与AudioFlinger之间建立数据通道,而AudioTrack与AudioFlinger又分属不同的进程空间,Android系统采用Binder通信方式来搭建它们之间的桥梁。
001.status_t AudioTrack::createTrack_l(002.audio_stream_type_t streamType,003.uint32_t sampleRate,004.audio_format_t format,005.size_t frameCount,006.audio_output_flags_t flags,007.constsp<IMemory>& sharedBuffer,008.audio_io_handle_t output,009.size_t epoch)010.{011.status_t status;012.//得到AudioFlinger的代理对象013.constsp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();014.if(audioFlinger ==0) {015.ALOGE("Could not get audioflinger");016.returnNO_INIT;017.}018.//得到输出时延019.uint32_t afLatency;020.status = AudioSystem::getLatency(output, streamType, &afLatency);021.if(status != NO_ERROR) {022.ALOGE("getLatency(%d) failed status %d", output, status);023.returnNO_INIT;024.}025.//得到音频帧数026.size_t afFrameCount;027.status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);028.if(status != NO_ERROR) {029.ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);030.returnNO_INIT;031.}032.//得到采样率033.uint32_t afSampleRate;034.status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);035.if(status != NO_ERROR) {036.ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);037.returnNO_INIT;038.}039.// Client decides whether the track is TIMED (see below), but can only express a preference040.// for FAST. Server will perform additional tests.041.if((flags & AUDIO_OUTPUT_FLAG_FAST) && !(042.// either of these use cases:043.// use case 1: shared buffer044.(sharedBuffer !=0) ||045.// use case 2: callback handler046.(mCbf != NULL))) {047.ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");048.// once denied, do not request again if IAudioTrack is re-created049.flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);050.mFlags = flags;051.}052.ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);053.// The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where054.// n = 1 fast track; nBuffering is ignored055.// n = 2 normal track, no sample rate conversion056.// n = 3 normal track, with sample rate conversion057.// (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)058.// n > 3 very high latency or very small notification interval; nBuffering is ignored059.constuint32_t nBuffering = (sampleRate == afSampleRate) ?2:3;060.mNotificationFramesAct = mNotificationFramesReq;061.if(!audio_is_linear_pcm(format)) {062.if(sharedBuffer !=0) {//static模式063.// Same comment as below about ignoring frameCount parameter for set()064.frameCount = sharedBuffer->size();065.}elseif(frameCount ==0) {066.frameCount = afFrameCount;067.}068.if(mNotificationFramesAct != frameCount) {069.mNotificationFramesAct = frameCount;070.}071.}elseif(sharedBuffer !=0) {// static模式072.// Ensure that buffer alignment matches channel count073.// 8-bit data in shared memory is not currently supported by AudioFlinger074.size_t alignment =/* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */2;075.if(mChannelCount >1) {076.// More than 2 channels does not require stronger alignment than stereo077.alignment <<=1;078.}079.if(((size_t)sharedBuffer->pointer() & (alignment -1)) !=0) {080.ALOGE("Invalid buffer alignment: address %p, channel count %u",081.sharedBuffer->pointer(), mChannelCount);082.returnBAD_VALUE;083.}084.// When initializing a shared buffer AudioTrack via constructors,085.// there's no frameCount parameter.086.// But when initializing a shared buffer AudioTrack via set(),087.// there _is_ a frameCount parameter. We silently ignore it.088.frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);089.}elseif(!(flags & AUDIO_OUTPUT_FLAG_FAST)) {090.// FIXME move these calculations and associated checks to server091.// Ensure that buffer depth covers at least audio hardware latency092.uint32_t minBufCount = afLatency / ((1000* afFrameCount)/afSampleRate);093.ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",094.afFrameCount, minBufCount, afSampleRate, afLatency);095.if(minBufCount <= nBuffering) {096.minBufCount = nBuffering;097.}098.size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;099.ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"", afLatency=%d",minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);100.if(frameCount ==0) {101.frameCount = minFrameCount;102.}elseif(frameCount < minFrameCount) {103.// not ALOGW because it happens all the time when playing key clicks over A2DP104.ALOGV("Minimum buffer size corrected from %d to %d",105.frameCount, minFrameCount);106.frameCount = minFrameCount;107.}108.// Make sure that application is notified with sufficient margin before underrun109.if(mNotificationFramesAct ==0|| mNotificationFramesAct > frameCount/nBuffering) {110.mNotificationFramesAct = frameCount/nBuffering;111.}112.}else{113.// For fast tracks, the frame count calculations and checks are done by server114.}115.IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;116.if(mIsTimed) {117.trackFlags |= IAudioFlinger::TRACK_TIMED;118.}119.pid_t tid = -1;120.if(flags & AUDIO_OUTPUT_FLAG_FAST) {121.trackFlags |= IAudioFlinger::TRACK_FAST;122.if(mAudioTrackThread !=0) {123.tid = mAudioTrackThread->getTid();124.}125.}126.if(flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {127.trackFlags |= IAudioFlinger::TRACK_OFFLOAD;128.}129.//向AudioFlinger发送createTrack请求,在stream模式下sharedBuffer为空,output为AudioFlinger中播放线程的id号130.sp<IAudioTrack> track = audioFlinger->createTrack(streamType,131.sampleRate,132.// AudioFlinger only sees 16-bit PCM133.format == AUDIO_FORMAT_PCM_8_BIT ?134.AUDIO_FORMAT_PCM_16_BIT : format,135.mChannelMask,136.frameCount,137.&trackFlags,138.sharedBuffer,139.output,140.tid,141.&mSessionId,142.mName,143.mClientUid,144.&status);145.if(track ==0) {146.ALOGE("AudioFlinger could not create track, status: %d", status);147.returnstatus;148.}149.//AudioFlinger创建Tack对象时会分配一块共享内存,这里得到这块共享内存的代理对象BpMemory150.sp<IMemory> iMem = track->getCblk();151.if(iMem ==0) {152.ALOGE("Could not get control block");153.returnNO_INIT;154.}155.// invariant that mAudioTrack != 0 is true only after set() returns successfully156.if(mAudioTrack !=0) {157.mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier,this);158.mDeathNotifier.clear();159.}160.//将创建的Track代理对象、匿名共享内存代理对象保存到AudioTrack的成员变量中161.mAudioTrack = track;162.mCblkMemory = iMem;163.//保存匿名共享内存的首地址,在匿名共享内存的头部存放了一个audio_track_cblk_t对象164.audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());165.mCblk = cblk;166.size_t temp = cblk->frameCount_;167.if(temp < frameCount || (frameCount ==0&& temp ==0)) {168.// In current design, AudioTrack client checks and ensures frame count validity before169.// passing it to AudioFlinger so AudioFlinger should not return a different value except170.// for fast track as it uses a special method of assigning frame count.171.ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);172.}173.frameCount = temp;174.mAwaitBoost =false;175.if(flags & AUDIO_OUTPUT_FLAG_FAST) {176.if(trackFlags & IAudioFlinger::TRACK_FAST) {177.ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);178.mAwaitBoost =true;179.if(sharedBuffer ==0) {180.// double-buffering is not required for fast tracks, due to tighter scheduling181.if(mNotificationFramesAct ==0|| mNotificationFramesAct > frameCount) {182.mNotificationFramesAct = frameCount;183.}184.}185.}else{186.ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);187.// once denied, do not request again if IAudioTrack is re-created188.flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);189.mFlags = flags;190.if(sharedBuffer ==0) {//stream模式191.if(mNotificationFramesAct ==0|| mNotificationFramesAct > frameCount/nBuffering) {192.mNotificationFramesAct = frameCount/nBuffering;193.}194.}195.}196.}197.if(flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {198.if(trackFlags & IAudioFlinger::TRACK_OFFLOAD) {199.ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");200.}else{201.ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");202.flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);203.mFlags = flags;204.returnNO_INIT;205.}206.}207.mRefreshRemaining =true;208.// Starting address of buffers in shared memory. If there is a shared buffer, buffers209.// is the value of pointer() for the shared buffer, otherwise buffers points210.// immediately after the control block. This address is for the mapping within client211.// address space. AudioFlinger::TrackBase::mBuffer is for the server address space.212.void* buffers;213.if(sharedBuffer ==0) {//stream模式214.buffers = (char*)cblk + sizeof(audio_track_cblk_t);215.}else{216.buffers = sharedBuffer->pointer();217.}218.mAudioTrack->attachAuxEffect(mAuxEffectId);219.// FIXME don't believe this lie220.mLatency = afLatency + (1000*frameCount) / sampleRate;221.mFrameCount = frameCount;222.// If IAudioTrack is re-created, don't let the requested frameCount223.// decrease. This can confuse clients that cache frameCount().224.if(frameCount > mReqFrameCount) {225.mReqFrameCount = frameCount;226.}227.// update proxy228.if(sharedBuffer ==0) {229.mStaticProxy.clear();230.mProxy =newAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);231.}else{232.mStaticProxy =newStaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);233.mProxy = mStaticProxy;234.}235.mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] *0x1000)) <<16) |236.uint16_t(mVolume[LEFT] *0x1000));237.mProxy->setSendLevel(mSendLevel);238.mProxy->setSampleRate(mSampleRate);239.mProxy->setEpoch(epoch);240.mProxy->setMinimum(mNotificationFramesAct);241.mDeathNotifier =newDeathNotifier(this);242.mAudioTrack->asBinder()->linkToDeath(mDeathNotifier,this);243.returnNO_ERROR;244.}IAudioTrack建立了AudioTrack与AudioFlinger之间的关系,在static模式下,用于存放音频数据的匿名共享内存在AudioTrack这边创建,在stream播放模式下,匿名共享内存却是在AudioFlinger这边创建。这两种播放模式下创建的匿名共享内存是有区别的,stream模式下的匿名共享内存头部会创建一个audio_track_cblk_t对象,用于协调生产者AudioTrack和消费者AudioFlinger之间的步调。createTrack就是在AudioFlinger中创建一个Track对象。
frameworksavservicesaudioflinger AudioFlinger.cpp
001.sp<IAudioTrack> AudioFlinger::createTrack(002.audio_stream_type_t streamType,003.uint32_t sampleRate,004.audio_format_t format,005.audio_channel_mask_t channelMask,006.size_t frameCount,007.IAudioFlinger::track_flags_t *flags,008.constsp<IMemory>& sharedBuffer,009.audio_io_handle_t output,010.pid_t tid,011.int*sessionId,012.String8& name,013.intclientUid,014.status_t *status)015.{016.sp<PlaybackThread::Track> track;017.sp<TrackHandle> trackHandle;018.sp<Client> client;019.status_t lStatus;020.intlSessionId;021.// client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,022.// but if someone uses binder directly they could bypass that and cause us to crash023.if(uint32_t(streamType) >= AUDIO_STREAM_CNT) {024.ALOGE("createTrack() invalid stream type %d", streamType);025.lStatus = BAD_VALUE;026.gotoExit;027.}028.// client is responsible for conversion of 8-bit PCM to 16-bit PCM,029.// and we don't yet support 8.24 or 32-bit PCM030.if(audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {031.ALOGE("createTrack() invalid format %d", format);032.lStatus = BAD_VALUE;033.gotoExit;034.}035.{036.Mutex::Autolock _l(mLock);037.//根据播放线程ID号查找出对应的PlaybackThread,在openout时,播放线程以key/value形式保存在AudioFlinger的mPlaybackThreads中038.PlaybackThread *thread = checkPlaybackThread_l(output);039.PlaybackThread *effectThread = NULL;040.if(thread == NULL) {041.ALOGE("no playback thread found for output handle %d", output);042.lStatus = BAD_VALUE;043.gotoExit;044.}045.pid_t pid = IPCThreadState::self()->getCallingPid();046.//根据客户端进程pid查找是否已经为该客户进程创建了Client对象,如果没有,则创建一个Client对象047.client = registerPid_l(pid);048.ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2: *sessionId);049.if(sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {050.// check if an effect chain with the same session ID is present on another051.// output thread and move it here.052.//遍历所有的播放线程,不包括输出线程,如果该线程中Track的sessionId与当前相同,则取出该线程作为当前Track的effectThread。053.for(size_t i =0; i < mPlaybackThreads.size(); i++) {054.sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);055.if(mPlaybackThreads.keyAt(i) != output) {056.uint32_t sessions = t->hasAudioSession(*sessionId);057.if(sessions & PlaybackThread::EFFECT_SESSION) {058.effectThread = t.get();059.break;060.}061.}062.}063.lSessionId = *sessionId;064.}else{065.// if no audio session id is provided, create one here066.lSessionId = nextUniqueId();067.if(sessionId != NULL) {068.*sessionId = lSessionId;069.}070.}071.ALOGV("createTrack() lSessionId: %d", lSessionId);072.//在找到的PlaybackThread线程中创建Track073.track = thread->createTrack_l(client, streamType, sampleRate, format,074.channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);075.// move effect chain to this output thread if an effect on same session was waiting076.// for a track to be created077.if(lStatus == NO_ERROR && effectThread != NULL) {078.Mutex::Autolock _dl(thread->mLock);079.Mutex::Autolock _sl(effectThread->mLock);080.moveEffectChain_l(lSessionId, effectThread, thread,true);081.}082.// Look for sync events awaiting for a session to be used.083.for(inti =0; i < (int)mPendingSyncEvents.size(); i++) {084.if(mPendingSyncEvents[i]->triggerSession() == lSessionId) {085.if(thread->isValidSyncEvent(mPendingSyncEvents[i])) {086.if(lStatus == NO_ERROR) {087.(void) track->setSyncEvent(mPendingSyncEvents[i]);088.}else{089.mPendingSyncEvents[i]->cancel();090.}091.mPendingSyncEvents.removeAt(i);092.i--;093.}094.}095.}096.}097.//此时Track已成功创建,还需要为该Track创建代理对象TrackHandle098.if(lStatus == NO_ERROR) {099.// s for server's pid, n for normal mixer name, f for fast index100.name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,track->fastIndex());101.trackHandle =newTrackHandle(track);102.}else{103.// remove local strong reference to Client before deleting the Track so that the Client destructor is called by the TrackBase destructor with mLock held104.client.clear();105.track.clear();106.}107.Exit:108.if(status != NULL) {109.*status = lStatus;110.}111./**112.* 向客户进程返回IAudioTrack的代理对象,这样客户进程就可以跨进程访问创建的Track了,113.* 访问方式如下:BpAudioTrack --> BnAudioTrack --> TrackHandle --> Track114.*/115.returntrackHandle;116.}该函数首先以单例模式为应用程序进程创建一个Client对象,直接对话某个客户进程。然后根据播放线程ID找出对应的PlaybackThread,并将创建Track的任务转交给它,PlaybackThread完成Track创建后,由于Track没有通信功能,因此还需要为其创建一个代理通信业务的TrackHandle对象。

构造Client对象
根据进程pid,为请求播放音频的客户端创建一个Client对象。
01.sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)02.{03.// If pid is already in the mClients wp<> map, then use that entry04.// (for which promote() is always != 0), otherwise create a new entry and Client.05.sp<Client> client = mClients.valueFor(pid).promote();06.if(client ==0) {07.client =newClient(this, pid);08.mClients.add(pid, client);09.}10.returnclient;11.}AudioFlinger的成员变量mClients以键值对的形式保存pid和Client对象,这里首先取出pid对应的Client对象,如果该对象为空,则为客户端进程创建一个新的Client对象。
01.AudioFlinger::Client::Client(constsp<AudioFlinger>& audioFlinger, pid_t pid)02.: RefBase(),mAudioFlinger(audioFlinger),03.// FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below04.mMemoryDealer(newMemoryDealer(2*1024*1024,"AudioFlinger::Client")),05.mPid(pid),06.mTimedTrackCount(0)07.{08.// 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer09.}构造Client对象时,创建了一个MemoryDealer对象,该对象用于分配共享内存。
frameworks ativelibsinder MemoryDealer.cpp
1.MemoryDealer::MemoryDealer(size_t size,constchar* name)2.: mHeap(newMemoryHeapBase(size,0, name)),//创建指定大小的共享内存3.mAllocator(newSimpleBestFitAllocator(size))//创建内存分配器4.{5.}MemoryDealer是个工具类,用于分配共享内存,每一个Client都拥有一个MemoryDealer对象,这就意味着每个客户端进程都是在自己独有的内存空间中分配共享内存。MemoryDealer构造时创建了一个大小为2*1024*1024的匿名共享内存,该客户进程所有的AudioTrack在AudioFlinger中创建的Track都是在这块共享内存中分配buffer。
1.SimpleBestFitAllocator::SimpleBestFitAllocator(size_t size)2.{3.size_t pagesize = getpagesize();4.mHeapSize = ((size + pagesize-1) & ~(pagesize-1));//页对齐5.chunk_t* node =newchunk_t(0, mHeapSize / kMemoryAlign);6.mList.insertHead(node);7.}由此可知,当应用程序进程中的AudioTrack请求AudioFlinger在某个PlaybackThread中创建Track对象时,AudioFlinger首先会为应用程序进程创建一个Client对象,同时创建一块大小为2M的共享内存。在创建Track时,Track将在2M共享内存中分配buffer用于音频播放。

创建Track对象
001.sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(002.constsp<AudioFlinger::Client>& client,003.audio_stream_type_t streamType,004.uint32_t sampleRate,005.audio_format_t format,006.audio_channel_mask_t channelMask,007.size_t frameCount,008.constsp<IMemory>& sharedBuffer,009.intsessionId,010.IAudioFlinger::track_flags_t *flags,011.pid_t tid,012.intuid,013.status_t *status)014.{015.sp<Track> track;016.status_t lStatus;017.bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) !=0;018.// client expresses a preference for FAST, but we get the final say019.if(*flags & IAudioFlinger::TRACK_FAST) {020.if(021.// not timed022.(!isTimed) &&023.// either of these use cases:024.(025.// use case 1: shared buffer with any frame count026.(027.(sharedBuffer !=0)028.) ||029.// use case 2: callback handler and frame count is default or at least as large as HAL030.(031.(tid != -1) &&032.((frameCount ==0) ||033.(frameCount >= (mFrameCount * kFastTrackMultiplier)))034.)035.) &&036.// PCM data037.audio_is_linear_pcm(format) &&038.// mono or stereo039.( (channelMask == AUDIO_CHANNEL_OUT_MONO) ||040.(channelMask == AUDIO_CHANNEL_OUT_STEREO) ) &&041.#ifndef FAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE042.// hardware sample rate043.(sampleRate == mSampleRate) &&044.#endif045.// normal mixer has an associated fast mixer046.hasFastMixer() &&047.// there are sufficient fast track slots available048.(mFastTrackAvailMask !=0)049.// FIXME test that MixerThread for this fast track has a capable output HAL050.// FIXME add a permission test also?051.) {052.// if frameCount not specified, then it defaults to fast mixer (HAL) frame count053.if(frameCount ==0) {054.frameCount = mFrameCount * kFastTrackMultiplier;055.}056.ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d",057.frameCount, mFrameCount);058.}else{059.ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d "060."mFrameCount=%d format=%d isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u ""hasFastMixer=%d tid=%d fastTrackAvailMask=%#x",061.isTimed, sharedBuffer.get(), frameCount, mFrameCount, format,062.audio_is_linear_pcm(format),063.channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask);064.*flags &= ~IAudioFlinger::TRACK_FAST;065.// For compatibility with AudioTrack calculation, buffer depth is forced066.// to be at least 2 x the normal mixer frame count and cover audio hardware latency.067.// This is probably too conservative, but legacy application code may depend on it.068.// If you change this calculation, also review the start threshold which is related.069.uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream);070.uint32_t minBufCount = latencyMs / ((1000* mNormalFrameCount) / mSampleRate);071.if(minBufCount <2) {072.minBufCount =2;073.}074.size_t minFrameCount = mNormalFrameCount * minBufCount;075.if(frameCount < minFrameCount) {076.frameCount = minFrameCount;077.}078.}079.}080.if(mType == DIRECT) {081.if((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) {082.if(sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {083.ALOGE("createTrack_l() Bad parameter: sampleRate %u format %d, channelMask 0x%08x ""for output %p with format %d",sampleRate, format, channelMask, mOutput, mFormat);084.lStatus = BAD_VALUE;085.gotoExit;086.}087.}088.}elseif(mType == OFFLOAD) {089.if(sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {090.ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x """foroutput %p with format %d",sampleRate, format, channelMask, mOutput, mFormat);091.lStatus = BAD_VALUE;092.gotoExit;093.}094.}else{095.if((format & AUDIO_FORMAT_MAIN_MASK) != AUDIO_FORMAT_PCM) {096.ALOGE("createTrack_l() Bad parameter: format %d ""097."for output %p with format %d",format, mOutput, mFormat);098.lStatus = BAD_VALUE;099.gotoExit;100.}101.// Resampler implementation limits input sampling rate to 2 x output sampling rate.102.if(sampleRate > mSampleRate*2) {103.ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate);104.lStatus = BAD_VALUE;105.gotoExit;106.}107.}108.lStatus = initCheck();109.if(lStatus != NO_ERROR) {110.ALOGE("Audio driver not initialized.");111.gotoExit;112.}113.{// scope for mLock114.Mutex::Autolock _l(mLock);115.ALOGD("ceateTrack_l() got lock");// SPRD: Add some log116.// all tracks in same audio session must share the same routing strategy otherwise117.// conflicts will happen when tracks are moved from one output to another by audio policy118.// manager119.uint32_t strategy = AudioSystem::getStrategyForStream(streamType);120.for(size_t i =0; i < mTracks.size(); ++i) {121.sp<Track> t = mTracks[i];122.if(t !=0&& !t->isOutputTrack()) {123.uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());124.if(sessionId == t->sessionId() && strategy != actual) {125.ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",126.strategy, actual);127.lStatus = BAD_VALUE;128.gotoExit;129.}130.}131.}132.if(!isTimed) {133.track =newTrack(this, client, streamType, sampleRate, format,134.channelMask, frameCount, sharedBuffer, sessionId, uid, *flags);135.}else{136.track = TimedTrack::create(this, client, streamType, sampleRate, format,137.channelMask, frameCount, sharedBuffer, sessionId, uid);138.}139.if(track ==0|| track->getCblk() == NULL || track->name() <0) {140.lStatus = NO_MEMORY;141.gotoExit;142.}143.mTracks.add(track);144.sp<EffectChain> chain = getEffectChain_l(sessionId);145.if(chain !=0) {146.ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());147.track->setMainBuffer(chain->inBuffer());148.chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));149.chain->incTrackCnt();150.}151.if((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) {152.pid_t callingPid = IPCThreadState::self()->getCallingPid();153.// we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,154.// so ask activity manager to do this on our behalf155.sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp);156.}157.}158.lStatus = NO_ERROR;159.Exit:160.if(status) {161.*status = lStatus;162.}163.returntrack;164.}这里就为AudioTrack创建了一个Track对象。Track继承于TrackBase,因此构造Track时,首先执行TrackBase的构造函数。

001.AudioFlinger::ThreadBase::TrackBase::TrackBase(002.ThreadBase *thread,//所属的播放线程003.constsp<Client>& client,//所属的Client004.uint32_t sampleRate,//采样率005.audio_format_t format,//音频格式006.audio_channel_mask_t channelMask,//声道007.size_t frameCount,//音频帧个数008.constsp<IMemory>& sharedBuffer,//共享内存009.intsessionId,010.intclientUid,011.bool isOut)012.: RefBase(),013.mThread(thread),014.mClient(client),015.mCblk(NULL),016.// mBuffer017.mState(IDLE),018.mSampleRate(sampleRate),019.mFormat(format),020.mChannelMask(channelMask),021.mChannelCount(popcount(channelMask)),022.mFrameSize(audio_is_linear_pcm(format) ?023.mChannelCount * audio_bytes_per_sample(format) : sizeof(int8_t)),024.mFrameCount(frameCount),025.mSessionId(sessionId),026.mIsOut(isOut),027.mServerProxy(NULL),028.mId(android_atomic_inc(&nextTrackId)),029.mTerminated(false)030.{031.// if the caller is us, trust the specified uid032.if(IPCThreadState::self()->getCallingPid() != getpid_cached || clientUid == -1) {033.intnewclientUid = IPCThreadState::self()->getCallingUid();034.if(clientUid != -1&& clientUid != newclientUid) {035.ALOGW("uid %d tried to pass itself off as %d", newclientUid, clientUid);036.}037.clientUid = newclientUid;038.}039.// clientUid contains the uid of the app that is responsible for this track, so we can blame040.//得到应用进程uid041.mUid = clientUid;042.// client == 0 implies sharedBuffer == 0043.ALOG_ASSERT(!(client ==0&& sharedBuffer !=0));044.ALOGV_IF(sharedBuffer !=0,"sharedBuffer: %p, size: %d", sharedBuffer->pointer(),045.sharedBuffer->size());046.//计算audio_track_cblk_t大小047.size_t size = sizeof(audio_track_cblk_t);048.//计算存放音频数据的buffer大小,= frameCount*mFrameSize049.size_t bufferSize = (sharedBuffer ==0? roundup(frameCount) : frameCount) * mFrameSize;050./**051.* stream模式下,需要audio_track_cblk_t来协调生成者和消费者,计算共享内存大小052.* --------------------------------------------------------053.* | audio_track_cblk_t | buffer |054.* --------------------------------------------------------055.*/056.if(sharedBuffer ==0) {//stream模式下057.size += bufferSize;058.}059.//如果Client不为空,就通过Client来分配buffer060.if(client !=0) {061.//请求Client中的MemoryDealer工具类来分配buffer062.mCblkMemory = client->heap()->allocate(size);063.//分配成功064.if(mCblkMemory !=0) {065.//将共享内存的指针强制转换为audio_track_cblk_t066.mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer());067.// can't assume mCblk != NULL068.}else{069.ALOGE("not enough memory for AudioTrack size=%u", size);070.client->heap()->dump("AudioTrack");071.return;072.}073.}else{//Client为空,使用数组方式分配内存空间074.// this syntax avoids calling the audio_track_cblk_t constructor twice075.mCblk = (audio_track_cblk_t *)newuint8_t[size];076.// assume mCblk != NULL077.}078./**079.* 当为应用进程创建了Client对象,则通过Client来分配音频数据buffer,否则通过数组方式分配buffer。080.* stream模式下,在分配好的buffer头部创建audio_track_cblk_t对象,而static模式下,创建单独的081.* audio_track_cblk_t对象。082.*/083.if(mCblk != NULL) {084.// construct the shared structure in-place.085.new(mCblk) audio_track_cblk_t();086.// clear all buffers087.mCblk->frameCount_ = frameCount;088.if(sharedBuffer ==0) {//stream模式089.//将mBuffer指向数据buffer的首地址090.mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);091.//清空数据buffer092.memset(mBuffer,0, bufferSize);093.}else{//static模式094.mBuffer = sharedBuffer->pointer();095.#if0096.mCblk->mFlags = CBLK_FORCEREADY;// FIXME hack, need to fix the track ready logic097.#endif098.}099.#ifdef TEE_SINK100.…101.#endif102.ALOGD("TrackBase constructed");// SPRD: add some log103.}104.}TrackBase构造过程主要是为音频播放分配共享内存,在static模式下,共享内存由应用进程自身分配,但在stream模式,共享内存由AudioFlinger分配,static和stream模式下,都会创建audio_track_cblk_t对象,唯一的区别在于,在stream模式下,audio_track_cblk_t对象创建在共享内存的头部。
static模式:

stream模式:

接下来继续分析Track的构造过程:
01.AudioFlinger::PlaybackThread::Track::Track(02.PlaybackThread *thread,//所属的播放线程03.constsp<Client>& client,//所属的Client04.audio_stream_type_t streamType,//音频流类型05.uint32_t sampleRate,//采样率06.audio_format_t format,//音频格式07.audio_channel_mask_t channelMask,//声道08.size_t frameCount,//音频帧个数09.constsp<IMemory>& sharedBuffer,//共享内存10.intsessionId,11.intuid,12.IAudioFlinger::track_flags_t flags)13.: TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer,sessionId, uid,true/*isOut*/),14.mFillingUpStatus(FS_INVALID),15.// mRetryCount initialized later when needed16.mSharedBuffer(sharedBuffer),17.mStreamType(streamType),18.mName(-1),// see note below19.mMainBuffer(thread->mixBuffer()),20.mAuxBuffer(NULL),21.mAuxEffectId(0), mHasVolumeController(false),22.mPresentationCompleteFrames(0),23.mFlags(flags),24.mFastIndex(-1),25.mCachedVolume(1.0),26.mIsInvalid(false),27.mAudioTrackServerProxy(NULL),28.mResumeToStopping(false)29.{30.if(mCblk != NULL) {//audio_track_cblk_t对象不为空31.if(sharedBuffer ==0) {//stream模式32.mAudioTrackServerProxy =newAudioTrackServerProx
- MODE_STATIC
本文介绍了Android中MediaPlayer和AudioTrack的区别与联系。MediaPlayer可播放多种格式声音文件,会创建解码器;AudioTrack只能播放已解码的PCM流,不创建解码器。MediaPlayer包含AudioTrack。还阐述了AudioTrack的构造过程、数据加载模式、数据输入方式等内容。



1793

被折叠的 条评论
为什么被折叠?



