Android Binder Mechanism III (anonymous service)

Source: Internet
Author: User

What is an anonymous service? Any service that does not have a register on the ServiceManager is anonymous service.

Or take a sample example to see the code:

status_t mediaplayer::setdatasource (int fd, int64_t offset, int64_t length) {    status_t err = unknown_error;    Const sp<imediaplayerservice>& Service (Getmediaplayerservice ());    if (service! = 0) {        sp<imediaplayer> player (service->create (this, Maudiosessionid));        if ((No_error! = dosetretransmitendpoint (player)) | |            (No_error! = Player->setdatasource (fd, offset, length))) {            player.clear ();        }        Err = Attachnewplayer (player);    }    return err;}

In the Bpmediaplayerservice. The implementation of Create is for example the following:

Virtual sp<imediaplayer> Create (        const sp<imediaplayerclient>& Client, int audiosessionid) {    Parcel data, reply;    Data.writeinterfacetoken (Imediaplayerservice::getinterfacedescriptor ());    Data.writestrongbinder (Client->asbinder ());    Data.writeint32 (Audiosessionid);    Remote ()->transact (CREATE, data, &reply);    Return interface_cast<imediaplayer> (Reply.readstrongbinder ());}
Jump directly to the server Mediaplayerservice to see the real implementation of Create:

Sp<imediaplayer> mediaplayerservice::create (const sp<imediaplayerclient>& client,        int Audiosessionid) {    pid_t pid = ipcthreadstate::self ()->getcallingpid ();    int32_t Connid = Android_atomic_inc (&mnextconnid);    sp<client> C = new Client (this            , PID, Connid, Client, Audiosessionid,            ipcthreadstate::self () Getcallinguid ());    ALOGV ("Create new Client (%d) from PID%d, uid%d,", Connid, PID,         ipcthreadstate::self ()->getcallinguid ());    /* Add by Gary. Start {-----------------------------------*    /C->setscreen (mscreen);    /* Add by Gary. End   -----------------------------------}} *    /C->setsubgate (mglobalsubgate);  2012-03-12, add the global interfaces to control the subtitle gate    wp<client> w = c;    {        Mutex::autolock lock (mLock);        Mclients.add (w);    }    return c;}

From the code, we can see that service->create (this, Maudiosessionid) is a Bpmediaplayer object that returns a parameter of the client type, The Client is Mediaplayerservice's private inner class, which is declared as: Class Client:publicbnmediaplayer.

In this way, the server and client of binder communication are established. Client side Bpmediaplayer is used by MediaPlayer, and server-side bnmediaplayer is used by Mediaplayerservice.

How did Bpmediaplayer get the handle value of Bnmediaplayer? The binder driver holds the various data for this binder entity when Mediaplayerservice returns the binder's reference. Create a node and look at the following code:

status_t bnmediaplayerservice::ontransact (    uint32_t Code, const parcel& data, parcel* reply, uint32_t flags) { C1/>switch (code) {case        CREATE: {            check_interface (imediaplayerservice, data, reply);            sp<imediaplayerclient> client =                interface_cast<imediaplayerclient> (Data.readstrongbinder ());            int audiosessionid = Data.readint32 ();            Sp<imediaplayer> player = Create (client, Audiosessionid);            Reply->writestrongbinder (Player->asbinder ());            return no_error;        } Break, ...}

The answer is in this sentence: Reply->writestrongbinder (Player->asbinder ());

When this reply is written to the binder driver, the driver specifically handles such ibinder type of data, creating a handle for Bbinder.

Communication can be communicated after the channel is established: Player->setdatasource (FD, offset, length)

The subsequent implementation is the same as the normal service in the previous article.

Android Binder Mechanism III (anonymous service)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.