Learn the usage of C++ version Binder from the calling process of setPhoneState

1. Start with the caller

AudioSystemsetPhoneState中:

aps->setPhoneState(state),

aps type:

sp<IAudioPolicyService>

2. IAudioPolicyService is an abstract class, find a subclass, a total of two

BnAudioPolicyServicepublic BnInterface<IAudioPolicyService>

BpAudioPolicyServicepublic BpInterface<IAudioPolicyService>

3. What the hell is BnInterfaceBpInterface ?

It turned out to be a C++ template class

template<typenameINTERFACE>

class BnInterface :public INTERFACE。。。

class BpInterface :public INTERFACE。。。

The current situation is IAudioPolicyService in angle brackets .

So the above is equivalent to classBnInterface: public IAudioPolicyService

That is to say, BnInterfaceBpInterface is inherited from IAudioPolicyService


4. After calling AudioSystem , the caller needs to find the implementation. The implementation of setPhoneState that is actually called is in the middle of BnAudioPolicyService and BpAudioPolicyService . Obviously, BnAudioPolicyService does not implement any member functions from the grandfather class IAudioPolicyService , but BpAudioPolicyService has an implementation of,

virtual status_tsetPhoneState(audio_mode_t state)

{

Parcel data,reply;

data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());

data.writeInt32(state);

remote()->transact(SET_PHONE_STATE, data, &reply);

returnstatic_cast <status_t> (reply.readInt32());

}

6. And BnAudioPolicyService implements the onTransact function from Dad. The implementation of the function is to do some corresponding operations according to various codes , such as setPhoneState corresponding to:

caseSET_PHONE_STATE: {

CHECK_INTERFACE(IAudioPolicyService, data, reply);

reply->writeInt32(static_cast <uint32_t>(setPhoneState(

(audio_mode_t) data.readInt32())));

returnNO_ERROR;

} break;

This is obvious and crucial,

One transact , one onTransact, one send and one receive, one sing and one harmony, use one code to complete the communication.


7. The setPhoneState() method is obviously called in onTransact , but this class itself does not implement setPhoneState(), so look down and find its subclasses

AudioPolicyServicepublic BnAudioPolicyService

In AudioPolicyService : (implemented in AudioPolicyInterfaceImp.cpp , surprise!)

mAudioPolicyManager->setPhoneState(state);

Although you may already be very excited to shout out, the implementation is in the AudioPolicyManager , but please don't worry!

Please see:

AudioPolicyInterface*mAudioPolicyManager;

The assignment of this variable is not in AudioPolicyInterfaceImp.cpp at all , which caused me to search for several minutes. The implementation of the AudioPolicyServicethe AudioPolicyInterfaceImp.cpp file, also has a pure AudioPolicyService.cpp . The author please call me and explain this matter to me, is there something wrong?

mAudioPolicyManager= createAudioPolicyManager(mAudioPolicyClient);

This line of code comes from voidAudioPolicyService::onFirstRef()

Do you understand something, let me rub it, mediaserver will directly start two major services, AudioPolicy and AudioFlinger , when booting . This variable is initialized at time!


8. The createAudioPolicyManager function mentioned above is another "virtual" function, which gives people a ubiquitous rush. In this way, various mobile phone manufacturers and chip manufacturers can implement their own AudioPolicyManager . In short, we know that the function call has entered the AudioPolicyManger at this time . Going any further, readers should complain that what I wrote is long and stinky, and it's a thousand miles away from the topic.


9. By the way, BnAudioPolicyService and BpAudioPolicyService, as mentioned earlier , realize the Binder call when they sing and harmonize. Before I start talking, you must be as unbelievable as I was when I watched it for the first time. The implementation is in the same file. Wouldn't it be over if you call it directly? Why do we have to use this method of transact, onTransact ?

Don't forget that we are talking about Binder. The application scenario is cross-process. That is to say , the BnAudioPolicyServiceBpAudioPolicyService code files are written in the same file for your convenience. In fact, they are completely two processes when they run. We have analyzed BnAudioPolicyServicethe mediaserver process. What is the process of BpAudioPolicyServiceDepends on the caller, the caller of AudioSystemSearch globally, and it is not difficult to find that the AudioSystem class from the java layer is called through jni( I am talking about the setPhoneState function, not all ) , and can eventually be traced back to AudioService . The AudioManager interface is corresponding to the user side that can be used .


10. To sort out the nonsense, it is a function called by the third-party application through the AudioManager layer by layer, and a function implemented in the mediaserver process through the Binder call . It is the so-called cross-process.


11. As for how the binder in the middle is called through code ?

Binder is another process, a service process. Think about it, when you buy something on Taobao, does Taobao deliver it to you? They are all forwarded by express for you. Maybe binder is a courier! Ha ha

I don't know the details, after all, the title of this article is the usage, not the principle! ! ! hahaha


Please see the principle:

http://blog.csdn.net/coding_glacier/article/details/7520199

Guess you like

Origin blog.csdn.net/bberdong/article/details/52576625