Android Audio: AudioTrack constructor analysis

Before analyzing the difference between AudioTrack AudioTrack first analysis and the MediaPlayer:

  • AudioTrack only play pcm raw data, you can not play the video.

  • MediaPlayer can play video and audio.

  • AudioTrack only supports pcm original audio data.

  • MediaPlayer support mp3, wav, aac ...

  • MediaPlayer create the specified format decoder will be at the bottom, the audio data into pcm pcm then to
    go play. MediaPlayer bottom creates AudioTrack, the decoded data AudioTrack to play.

  • Each audio stream corresponds to one instance of a class AudioTrack, each registered to the AudioTrack when creating
    AudioFlinger in all the AudioFlinger AudioTrack by mixing (Mixer), and then conveyed to AudioHardware for playback, while the maximum current Android create 32 audio streams, that is to say, Mixer up to simultaneously process the data stream 32 AudioTrack of.

AudioTrack up personal feeling sort of process is very painful, the middle may have a lot of problems, a lot of reference blog, as well as internships master guide, a sort of Android P AudioTrack beginning of the text flow out after work with the more familiar , and then modify.

AudioTrack constructor analysis:

android\frameworks\base\media\java\android\media\AudioTrack.java

Go constructor private.
Here Insert Picture Description
Get the main thread Looper, asynchronous message communication mechanism.
Here Insert Picture Description
Obtaining sample rate, channel masking, encoding type, and then verify that the legal argument
Here Insert Picture Description
invoked native_setup native layer
Here Insert Picture Description
native_setup ()
Here Insert Picture Description
Android \ frameworks \ Base \ Core \ JNI \ android_media_AudioTrack.cpp
Here Insert Picture Description
into the JNI layer
Here Insert Picture Description
to create AudioTrack native layer.
Here Insert Picture Description
android \ frameworks \ av \ media \ libaudioclient \ AudioTrack.cpp

Lib into the layer () method parameters set by the set into
Here Insert Picture Description
status_t AudioTrack :: set ()
Here Insert Picture Description
to create IAudioTrack.
Here Insert Picture Description
Get an audio service strategy.
Here Insert Picture Description
frameworks \ av \ media \ libaudioclient \ AudioSystem.cpp

By binder mechanism binding service.
Here Insert Picture Description
android \ frameworks \ av \ media \ libaudioclient \ AudioTrack.cpp

进入AudioFlinger
Here Insert Picture Description
android\frameworks\av\services\audioflinger\AudioFlinger.cpp
Here Insert Picture Description
依据播放线程ID号查找出相应的PlaybackThread,依据客户端进程pid查找是否已经为该客户进程创建了Client对象。假设没有,则创建一个Client对象。
Here Insert Picture Description
依据进程pid。为请求播放音频的client创建一个Client对象。
Here Insert Picture Description
AudioFlinger的成员变量mClients以键值对的形式保存pid和Client对象。这里首先取出pid相应的Client对象,假设该对象为空。则为client进程创建一个新的Client对象。MemoryDealer是个工具类。用于分配共享内存。每个Client都拥有一个MemoryDealer对象,这就意味着每个client进程都是在自己独有的内存空间中分配共享内存。
Here Insert Picture Description
创建了如同所示的内存大小。
Here Insert Picture Description
返回继续看AudioFlinger::createTrack()
Here Insert Picture Description
frameworks\av\services\audioflinger\Threads.cpp

创建Track对象
Here Insert Picture Description
前面进行一系列的参数赋值,最后返回一个track对象。
Here Insert Picture Description
进入Track的构造函数,由图可见,继承于TrackBase,因此先进入TrackBase的构造函数进行分析。
Here Insert Picture Description
TrackBase()

thread,所属播放线程,client所属的客户端,sampleRate,采样率,format音频格式,channelMask,声道,frameCount,音频帧个数。sharedBuffer,共享内存。
Here Insert Picture Description
得到应用进程id
Here Insert Picture Description
计算存放音频的buffer大小。
Here Insert Picture Description
Client不为空。就通过Client来分配buffer,为空,则通过mallc动态分配。
Here Insert Picture Description
由此可见,TrackBase构造过程主要是为音频播放分配共享内存。
接着进入Track的构造函数进行分析。Here Insert Picture Description

为0创建AudioTrackServerProxy代理对象,不为0,创建StaticAudioTrackServerProxy代理对象;
Here Insert Picture Description
接下来我们返回AudioFlinger的createTrack函数中,来看这个track对象到底做了什么事情。

android\frameworks\av\services\audioflinger\AudioFlinger.cpp

创建Track的binder对象TrackHandle,Track由于需要通过binder返回给AudioTrack,因此是个binder对象,该对象会包含share buffer的信息。由于share buffer不止会在AudioFlinger这端被读取,还会在AudioTrack这端被写入,因此创建出来的Track需要被传送回AudioTrack。而在binder间传送对象只有binder对象,因此需要构建binder对象TrackHandle,返回给AudioTrack。
Here Insert Picture Description
frameworks\av\services\audioflinger\Tracks.cpp

至此,createTrack_l在AudioFlinger这端的工作基本完成了。
Here Insert Picture Description

ps: I have around here very clearly: You can refer to other blog: [Android] createTrack_l

frameworks\av\media\libaudioclient\AudioTrack.cpp

The creatTrack in return AudioTrack.cpp.
Here Insert Picture Description
It is able to get to the top, do not know where the wrong or no analysis toHere Insert Picture Description

After completely clarify whether there is a problem in modifying it.

The constructor being analyzed so far.
Here Insert Picture Description

Published 39 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43443900/article/details/103933776