Android Audio Subsystem - get_audio_flinger - 01

 

 

 

1.    AudioSystem::get_audio_flinger()

Frameworks/av/media/libmedia/AudioSystem.cpp

constsp<IAudioFlinger>& AudioSystem::get_audio_flinger()

{

    Mutex::Autolock _l(gLock);

    if (gAudioFlinger == 0) {

       sp<IServiceManager> sm = defaultServiceManager();

        sp<IBinder> binder;

        do {

           binder =sm->getService(String16("media.audio_flinger"));

            if (binder != 0)

                break;

            ALOGW("AudioFlinger notpublished, waiting...");

            usleep(500000); // 0.5 s

        } while (true);

        if (gAudioFlingerClient == NULL) {

           gAudioFlingerClient = new AudioFlingerClient();

        } else {

            if (gAudioErrorCallback) {

                gAudioErrorCallback(NO_ERROR);

            }

        }

       binder->linkToDeath(gAudioFlingerClient);

       gAudioFlinger =interface_cast<IAudioFlinger>(binder);

       gAudioFlinger->registerClient(gAudioFlingerClient);

    }

    ALOGE_IF(gAudioFlinger==0, "noAudioFlinger!?");

 

    return gAudioFlinger;

}

 

 

 

2.    sp<IServiceManager>defaultServiceManager()

Frameworks/native/libs/binder/IServiceManager.cpp

sp<IServiceManager> defaultServiceManager()

{

    if (gDefaultServiceManager!= NULL) return gDefaultServiceManager;

   

    {

        AutoMutex_l(gDefaultServiceManagerLock);

        if(gDefaultServiceManager == NULL) {

           gDefaultServiceManager = interface_cast<IServiceManager>(

               ProcessState::self()->getContextObject(NULL));

        }

    }

   

    returngDefaultServiceManager;

}

 

3.    sp<ProcessState>ProcessState::self()

Frameworks/native/libs/binder/ProcessState.cpp

sp<ProcessState> ProcessState::self()

{

    Mutex::Autolock _l(gProcessMutex);

    if (gProcess != NULL) {

        return gProcess;

    }

    gProcess= new ProcessState;

    return gProcess;

}

 

4.    sp<ProcessState>ProcessState::self()

Frameworks/native/libs/binder/ProcessState.cpp

ProcessState::ProcessState()

    : mDriverFD(open_driver())

    , mVMStart(MAP_FAILED)

    , mManagesContexts(false)

    ,mBinderContextCheckFunc(NULL)

    ,mBinderContextUserData(NULL)

    ,mThreadPoolStarted(false)

    , mThreadPoolSeq(1)

{

    if (mDriverFD >= 0) {

        // XXX Ideally, thereshould be a specific define for whether we

        // have mmap (orwhether we could possibly have the kernel module

        // availabla).

#if !defined(HAVE_WIN32_IPC)

        // mmap the binder,providing a chunk of virtual address space to receive transactions.

        mVMStart = mmap(0,BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);

        if (mVMStart ==MAP_FAILED) {

            // *sigh*

            ALOGE("Using/dev/binder failed: unable to mmap transaction memory.\n");

            close(mDriverFD);

            mDriverFD = -1;

        }

#else

        mDriverFD = -1;

#endif

    }

 

   LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not beopened.  Terminating.");

}

 

5.    sp<ProcessState>ProcessState::self()

Frameworks/native/libs/binder/ProcessState.cpp

sp<IBinder> ProcessState::getContextObject(constsp<IBinder>& caller)

{

    returngetStrongProxyForHandle(0);

}

 

sp<IBinder> ProcessState::getStrongProxyForHandle(int32_thandle)

{

    sp<IBinder> result;

 

    AutoMutex _l(mLock);

 

    handle_entry* e =lookupHandleLocked(handle);

 

    if (e != NULL) {

        // We need to create anew BpBinder if there isn't currently one, OR we

        // are unable toacquire a weak reference on this current one. See comment

        // ingetWeakProxyForHandle() for more info about this.

        IBinder* b =e->binder;

        if (b == NULL ||!e->refs->attemptIncWeak(this)) {

            b = newBpBinder(handle);

            e->binder = b;

            if (b) e->refs= b->getWeakRefs();

            result = b;

        } else {

            // This little bitof nastyness is to allow us to add a primary

            // reference tothe remote proxy when this team doesn't have one

            // but anotherteam is sending the handle to us.

           result.force_set(b);

           e->refs->decWeak(this);

        }

    }

 

    return result;  //  返回BpBinder(handle); handle0,代表ServiceManager

}

 

6.    interface_cast<IServiceManager>(BpBinder(handle))

frameworks/native/libs/binder/IInterface.h

   android::sp<I##INTERFACE> I##INTERFACE::asInterface(                \

            constandroid::sp<android::IBinder>& obj)                   \

    {                                                                   \

       android::sp<I##INTERFACE> intr;                                 \

        if (obj != NULL){                                             \

            intr =static_cast<I##INTERFACE*>(                          \

               obj->queryLocalInterface(                               \

                       I##INTERFACE::descriptor).get());               \

            if (intr == NULL){                                        \

                intr = new Bp##INTERFACE(obj);                          \

            }                                                          \

        }                                                              \

        return intr;                                                    \

    }                                                                  \

在这个函数里,将作如下事情:

1.)    查询BpBinder(0)的本地接口,这儿应该返回NULL

2.)    NewBpServiceManager(BpBinder(0))对象并返回这个对象

3.)    BpServiceManagermRemote将指向BpBinder(0)

 

 

 

7.      BpServiceManager::getService()

Frameworks/native/libs/binder/IServiceManager.cpp

    virtual sp<IBinder> getService(constString16& name) const

    {

        unsigned n;

        for (n = 0; n < 5; n++){

           sp<IBinder> svc = checkService(name);

            if (svc != NULL) return svc;

            ALOGI("Waiting for service%s...\n", String8(name).string());

            sleep(1);

        }

        return NULL;

}

 

 

 

 

 

 

   virtual sp<IBinder> checkService( const String16& name) const

    {

       Parcel data, reply;

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

       data.writeString16(name);

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

       return reply.readStrongBinder();

    }

// 这儿remote()返回的是BpServiceManager::mRemote成员变量,

// 它指向的是前面defaultServiceManager()过程中 BpBinder(0)对象。

// 因此调用的是BpBinder::transact()

 

 

8.      BpBinder::transact()

 

status_tBpBinder::transact(

    uint32_t code, const Parcel& data,Parcel* reply, uint32_t flags)

{

    // Once a binder has died, it will nevercome back to life.

    if (mAlive) {

//这儿将创建一个新的IPCThreadState,

// 每个线程都有一个IPCThreadState实例,都有一个mIn, mOut

// mIn用来接收来自binder设备的数据,

//mOut用来存储发往binder设备的数据

 

       status_t status= IPCThreadState::self()->transact(

           mHandle,code, data, reply, flags);

        if (status == DEAD_OBJECT) mAlive = 0;

        return status;

    }

 

    return DEAD_OBJECT;

}

 

9.     IPCThreadState*IPCThreadState::self()

Frameworks/native/libs/binder/IPCThreadState.cpp

IPCThreadState*IPCThreadState::self()

{

    if (gHaveTLS) {

restart:

        const pthread_key_t k = gTLS;

        IPCThreadState* st =(IPCThreadState*)pthread_getspecific(k);

        if (st) return st;

        return new IPCThreadState;

    }

   

    if (gShutdown) return NULL;

   

    pthread_mutex_lock(&gTLSMutex);

    if (!gHaveTLS) {

        if (pthread_key_create(&gTLS,threadDestructor) != 0) {

           pthread_mutex_unlock(&gTLSMutex);

            return NULL;

        }

        gHaveTLS = true;

    }

    pthread_mutex_unlock(&gTLSMutex);

    goto restart;

}

 

 

IPCThreadState::IPCThreadState()

    : mProcess(ProcessState::self()),

      mMyThreadId(androidGetTid()),

      mStrictModePolicy(0),

      mLastTransactionBinderFlags(0)

{

   pthread_setspecific(gTLS, this);

    clearCaller();

    mIn.setDataCapacity(256);

    mOut.setDataCapacity(256);

}

 

 

 

 

你可能感兴趣的:(Android Audio Subsystem - get_audio_flinger - 01)