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.
Get the main thread Looper, asynchronous message communication mechanism.
Obtaining sample rate, channel masking, encoding type, and then verify that the legal argument
invoked native_setup native layer
native_setup ()
Android \ frameworks \ Base \ Core \ JNI \ android_media_AudioTrack.cpp
into the JNI layer
to create AudioTrack native layer.
android \ frameworks \ av \ media \ libaudioclient \ AudioTrack.cpp
Lib into the layer () method parameters set by the set into
status_t AudioTrack :: set ()
to create IAudioTrack.
Get an audio service strategy.
frameworks \ av \ media \ libaudioclient \ AudioSystem.cpp
By binder mechanism binding service.
android \ frameworks \ av \ media \ libaudioclient \ AudioTrack.cpp
进入AudioFlinger
android\frameworks\av\services\audioflinger\AudioFlinger.cpp
依据播放线程ID号查找出相应的PlaybackThread,依据客户端进程pid查找是否已经为该客户进程创建了Client对象。假设没有,则创建一个Client对象。
依据进程pid。为请求播放音频的client创建一个Client对象。
AudioFlinger的成员变量mClients以键值对的形式保存pid和Client对象。这里首先取出pid相应的Client对象,假设该对象为空。则为client进程创建一个新的Client对象。MemoryDealer是个工具类。用于分配共享内存。每个Client都拥有一个MemoryDealer对象,这就意味着每个client进程都是在自己独有的内存空间中分配共享内存。
创建了如同所示的内存大小。
返回继续看AudioFlinger::createTrack()
frameworks\av\services\audioflinger\Threads.cpp
创建Track对象
前面进行一系列的参数赋值,最后返回一个track对象。
进入Track的构造函数,由图可见,继承于TrackBase,因此先进入TrackBase的构造函数进行分析。
TrackBase()
thread,所属播放线程,client所属的客户端,sampleRate,采样率,format音频格式,channelMask,声道,frameCount,音频帧个数。sharedBuffer,共享内存。
得到应用进程id
计算存放音频的buffer大小。
Client不为空。就通过Client来分配buffer,为空,则通过mallc动态分配。
由此可见,TrackBase构造过程主要是为音频播放分配共享内存。
接着进入Track的构造函数进行分析。
为0创建AudioTrackServerProxy代理对象,不为0,创建StaticAudioTrackServerProxy代理对象;
接下来我们返回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。
frameworks\av\services\audioflinger\Tracks.cpp
至此,createTrack_l在AudioFlinger这端的工作基本完成了。
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.
It is able to get to the top, do not know where the wrong or no analysis to
After completely clarify whether there is a problem in modifying it.
The constructor being analyzed so far.