Reprint please the head source link and the tail two-dimensional code together reprint, this article from countercurrent fish, article link:
http://blog.csdn.net/hejjunlin/article/details/52465168
The previous article mainly introduced C + + in MediaPlayer's C/S architecture and client-related parts, and interspersed with the mediaplayerservice part. But for this C/s section, there is no amplification to analyze. Android Multimedia Framework Summary (iv) MediaPlayer from Java layer to C + + layer class relationship and prepare and other processes from the overall view, today we put this C/S model to zoom in to see. Also first look at the agenda:
- Triangular relationship flowchart in C/S model
- Mediaplayerservice Associated class diagrams
- The production process of mediaplayerservice
- Mediaplayerservice Adding a service process
- Mediaplayerservice through Binderdriver and MediaPlayer communication processes
Triangular relationship flowchart in C/S model
Here are a few points to summarize:
MediaPlayer is the client, that is, the C-terminal
Mediaplayerservice and Mediaplayerservice::client are server-side. That's what we call the S-end of C/S.
Mediaplayerservice implements the business logic defined by Imediaplayerservice, whose main function is to create a corresponding player based on the URL called by the Mediaplayer::setdatasource input.
Mediaplayerservice::client implements the business logic defined by Imediaplayer, whose main functions include start, stop, pause, resume ..., which is implemented by calling Mediaplayerservice Create the corresponding method in the player to achieve the specific function.
Previously in the fourth chapter of the picture has drawn a whole, today, and then the mediaplayerservice,mediaplayerservice::client,mediaplayer magnified to see their interaction in the actual business.
Mediaplayerservice Associated class diagrams
The above class diagram summarizes the following points:
In a bnxxx or bpxxx are derived from two classes, as follows:
- Class Bpxxx:public ixxx, public bprefbase
- Class Bnxxx:public ixxx, public bbinder
Bpxxx and bnxxx are all derived from ixxx, which is what ixxx do? It can be understood here that defining business logic, we have previously analyzed imediaplayerclient in action, also said. However, the implementation in BPXXX and BNXXX differs in the following ways:
- In Bpxxx, the corresponding Binder_transaction_data is packaged and sent out via the Mremote (Bpbinder) in Bprefbase and waits for the result
- In Bnxxx, the corresponding business logic is implemented by invoking a method in the Bnxxx derived class, such as Mediaplayerservice::client
As can be seen, IBinder is used for inter-process communication.
- Bprefbase has a mremote (Bpbinder) that is used to interact with binder drivers.
- Binder is used to receive related requests from binder drivers and to deal with them.
This article is from the countercurrent fish, article link: http://blog.csdn.net/hejjunlin/article/details/52465168
The production process of mediaplayerservice
Before you know Mediaplayerservice, get to know the next IMediaPlayerService.cpp,
6.0 in the source code is in the Frameworks/av/media/libmediaplayerservice/mediaplayerservice.cpp:
You can see that there are some general business-related definitions here, and then begin to understand Mediaplayerservice
Find the entrance first, in Frameworks/base/media/mediaserver/main_mediaserver.cpp.
First look at the Defaultservicemanager function, as follows:
Using a single case, each process requires only one Bpservicemanager agent, Processstate::self ()->getcontextobject (NULL), Next look at the Getcontextobject (NULL) function,
Then look at Processstate::self ()->getcontextobject (NULL)
The above code summarizes: According to the passed handle handle value is 0, represents servicemanager,new a bpbinder so now is equivalent to:
Gdefaultservicemanager = Interface_cast (new Bpbinder (0));
And then we'll see what Interface_cast did?
In Frameworks/base/include/binder/iinterface.h, the following code is available:
Go on, we'll follow the Iservicemanager inside:
In Frameworks/base/include/binder/iservicemanager.h, the following code is available:
Summarize the above code: Create a new Bpbinder (0) based on the handle handle (0) and create a Bpservicemanager proxy based on this bpbinder.
Let's look at the Bpservicemanager agent:
Here Bpinterface is a template class that represents bpservicemanager at the same time inheriting Bpinterface and Iservicemanager classes
The base class Bpinterface constructor is called:
Mediaplayerservice::instantiate ();//instantiation of Mediaplayerservice
Frameworks/base/media/libmediaplayerservice/mediaplayerservice.cpp
Defaultservicemanager () returns the Bpservicemanager that was just created, calling the Add function.
Bpmediaplayservice as a service agent, then Bnmediaplayerservice must be the implementation end, Mediaplayerservice inherit Bnmediaplayerservice, realize the real business function, The information that is used to process client delivery.
This article is from the countercurrent fish, article link: http://blog.csdn.net/hejjunlin/article/details/52465168
Mediaplayerservice Adding a service process
Let's take a look at Bpservicemanager's AddService () function:
Here, remote () is the Bpbinder (0) object created earlier.
Then look at an interesting name, talkwithdriver implementation, as the name implies, and driver talk:
Ipcthreadstate::jointhreadpool (), Processstate::self ()->startthreadpool ()
The thread loop Talkwithdriver waits for client clients to request the request from the Binder read command to be processed.
So far Mediaplayerservice's server has been registered with Service Explorer ServiceManager.
Mediaplayerservice through Binderdriver and MediaPlayer communication processes
Let's look at how the client obtains the proxy for the service and communicates with the server.
For example, we use the MediaPlayer business function decode to parse the URL that plays a network video.
Here we mainly analyze Getmediaplayerservice, the client is how to query the ServiceManager Explorer service and obtain the agent.
- First get the Bpservicemanager agent, and then call the GetService () function to query the service Explorer ServiceManager for the service called STRING16 ("Media.player").
Located in Frameworks/base/libs/binder/iservicemanager.cpp:
- Here we first package the request into parcel, then call the remote ()->transact () function, which we analyzed before bpservicemanager::remote () returns
Is the previous new Bpbinder (0) corresponding handle is ServiceManager. Continue to the Bpbinder to find the implementation code:
In the Frameworks/base/libs/binder/bpbinder.cpp
- The last call to the Ipcthreadstate transact () function, ipcthreadstate is specifically provided through the Binder interprocess communication interface.
In this step, the Writetransactiondata function is first populated with the MOUT structure, and the contents of the Mout are:
Here Binder_transaction_data TR content is:
Tr.data content is:
This waitforresponse () function is waiting for processstate to return information:
The last return is: return Reply.readstrongbinder (); Enter into Parcel's readstrongbinder () function
Here Flat->type is binder_type_handle, so call the Processstate::getstrongproxyforhandle () function
The handle here is the mediaplayerservice corresponding binder handle maintained within ServiceManager, this processstate is based on this handle
New has a bpbinder and saves it so that the next time you need to get the same handle from the ServiceManager request, you can return directly.
Finally, the Mediaplayerservice agent is obtained based on the returned Bpbinder:
Smediaplayerservice = Interface_cast (binder);
According to the previous ServiceManager, the last call is the Imediaplayerservice asinterface () macro function
This obtains a proxy Bpmediaplayerservice object, its remote () is Bpbinder (handle), the handle is to a total of services ServiceManager
The binder handle to the mediaplayerservice that is queried.
Finally, we summarize the following:
- In the actual business, as Mediaplayer::setdatasource returns, a bpmediaplayer corresponding to Mediaplayerservice::client is created for obtaining mediaplayerservice: The functions of the client.
- And how did MediaPlayer find mediaplayerservice::client? Only Mediaplayerservice has registered with ServiceManager, so MediaPlayer must first obtain bpmediaplayerservice, and then through the Bpmediaservice management function create , to create a mediaplayerservice::client.
- Why not just define a MediaPlayer to register with ServiceManager?
Mediaplayerservice contains not only the client, but also the audiooutput,audiocache,mediaconfigclient function. Mediaplayerservice is a Media Services window (driver a bit like a venue, in this venue, communication good information), Mediaplayerservice Business talks, contracts signed back, and then according to the requirements of the contract, arrange for different open-person hair to do.
The first time to get blog update reminders, as well as more Android dry, source code Analysis , Welcome to follow my public number, sweep the bottom QR code or long press to identify two-dimensional code, you can pay attention to.
If you feel good, easy to praise, but also to the author's affirmation, can also share this public number to you more people, original not easy
Android Multimedia Framework Summary (vii) C/S Architecture supplement and Mediaservice MediaPlayer