深入了解mediaserver-3

6. MediaPlayer如何与MediaPlayerService交互

6.1    MeidaPlayerService根据MediaPlayer的请求,创建对应的MeidaPlayer

[html]  view plain copy
  1. //MediaPlayerService.cpp  
  2. static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,  
  3.         notify_callback_f notifyFunc)  
  4. {  
  5.     sp<MediaPlayerBase> p;  
  6.     switch (playerType) {  
  7.         case XX_PLAYER:  
  8.             LOGV("Create XXPlayer");  
  9.             p = new XXPlayerService();  
  10.             break;  
  11. #ifndef NO_OPENCORE  
  12.         case PV_PLAYER:  
  13.             LOGV(" create PVPlayer");  
  14.             p = new PVPlayer();  
  15.             break;  
  16. #endif  
  17.         case SONIVOX_PLAYER:  
  18.             LOGV(" create MidiFile");  
  19.             p = new MidiFile();  
  20.             break;  
  21.         case VORBIS_PLAYER:  
  22.             LOGV(" create VorbisPlayer");  
  23.             p = new VorbisPlayer();  
  24.             break;  
  25. #if BUILD_WITH_FULL_STAGEFRIGHT  
  26.         case STAGEFRIGHT_PLAYER:  
  27.             LOGV(" create StagefrightPlayer");  
  28.             p = new StagefrightPlayer;  
  29.             break;  
  30. #endif  
  31.         case TEST_PLAYER:  
  32.             LOGV("Create Test Player stub");  
  33.             p = new TestPlayerStub();  
  34.             break;  
  35.     }  
  36.     if (p != NULL) {  
  37.         if (p->initCheck() == NO_ERROR) {  
  38.             p->setNotifyCallback(cookie, notifyFunc);  
  39.         } else {  
  40.             p.clear();  
  41.         }  
  42.     }  
  43.     if (p == NULL) {  
  44.         LOGE("Failed to create player object");  
  45.     }  
  46.     return p;  
  47. }  
[html]  view plain copy
  1. class MediaPlayer : public BnMediaPlayerClient,  
  2.                     public virtual IMediaDeathNotifier  
  3.   
  4. =============================================================  
  5. class IMediaPlayerClient: public IInterface  
  6. {  
  7. public:  
  8.     DECLARE_META_INTERFACE(MediaPlayerClient);  
  9.   
  10.     virtual void notify(int msg, int ext1, int ext2) = 0;  
  11. };  
  12.   
  13. class BnMediaPlayerClient: public BnInterface<IMediaPlayerClient>  
  14. {  
  15. public:  
  16.     virtual status_t    onTransact( uint32_t code,  
  17.                                     const Parcel& data,  
  18.                                     Parcel* reply,  
  19.                                     uint32_t flags = 0);  
  20. };  
  21. =============================================================  
  22. class IMediaDeathNotifier: virtual public RefBase  
  23. {  
  24. public:  
  25.     IMediaDeathNotifier() { addObitRecipient(this); }  
  26.     virtual ~IMediaDeathNotifier() { removeObitRecipient(this); }  
  27.   
  28.     virtual void died() = 0;  
  29.     //establish binder interface to MediaPlayerService  
  30.     static const sp<IMediaPlayerService>& getMediaPlayerService();  
  31.   
  32. private:  
  33.     IMediaDeathNotifier &operator=(const IMediaDeathNotifier &);  
  34.     IMediaDeathNotifier(const IMediaDeathNotifier &);  
  35.   
  36.     static void addObitRecipient(const wp<IMediaDeathNotifier>& recipient);  
  37.     static void removeObitRecipient(const wp<IMediaDeathNotifier>& recipient);  
  38.   
  39.     class DeathNotifier: public IBinder::DeathRecipient  
  40.     {  
  41.     public:  
  42.                 DeathNotifier() {}  
  43.         virtual ~DeathNotifier();  
  44.   
  45.         virtual void binderDied(const wp<IBinder>& who);  
  46.     };  
  47.   
  48.     friend class DeathNotifier;  
  49.   
  50.     static  Mutex                                   sServiceLock;  
  51.     static  sp<IMediaPlayerService>                 sMediaPlayerService;  
  52.     static  sp<DeathNotifier>                       sDeathNotifier;  
  53.     static  SortedVector< wp<IMediaDeathNotifier> > sObitRecipients;  
  54. }  

6.2 通过ServiceManager获取BpMediaPlayerService

[cpp]  view plain copy
  1. const sp<IMediaPlayerService>& IMediaDeathNotifier::getMediaPlayerService()  
  2. {  
  3.     LOGV("getMediaPlayerService");  
  4.     Mutex::Autolock _l(sServiceLock);  
  5.     if (sMediaPlayerService.get() == 0) {  
  6.         sp<IServiceManager> sm = defaultServiceManager();  
  7.         sp<IBinder> binder;  
  8.         do {  
  9.             binder = sm->getService(String16("media.player"));  
  10.             if (binder != 0) {  
  11.                 break;  
  12.              }  
  13.              LOGW("Media player service not published, waiting...");  
  14.              usleep(500000); // 0.5 s  
  15.         } while(true);  
  16.   
  17.         if (sDeathNotifier == NULL) {  
  18.         sDeathNotifier = new DeathNotifier();  
  19.     }  
  20.     binder->linkToDeath(sDeathNotifier);  
  21.   
  22.     // sMediaPlayerService中保存的为BpMediaPlayerService  
  23.   
  24.     sMediaPlayerService = interface_cast<IMediaPlayerService>(binder);  
  25.     }  
  26.     LOGE_IF(sMediaPlayerService == 0, "no media player service!?");  
  27.     return sMediaPlayerService;  
  28. }  

6.3 获取mPlayer(IMediaPlayer,实质为BpMediaPlayer) 

[cpp]  view plain copy
  1. status_t MediaPlayer::setDataSource(  
  2.         const char *url, const KeyedVector<String8, String8> *headers)  
  3. {  
  4.     LOGV("setDataSource(%s)", url);  
  5.     status_t err = BAD_VALUE;  
  6.     if (url != NULL) {  
  7.         //service实质上是BpMediaPlayerService  
  8.         const sp<IMediaPlayerService>& service(getMediaPlayerService());  
  9.   
  10.         if (service != 0) {  
  11.             // Player实质上是BpMediaPlayer  
  12.             sp<IMediaPlayer> player(service->create(getpid(), this, url, headers));  
  13.             //把BpMediaPlayer赋值给成员变量mPlayer,如果以前的mPlayer有效,则Disconnect以前的Player  
  14.             err = setDataSource(player);  
  15.         }  
  16.     }  
  17.     return err;  
  18. }  

BpMediaPlayerService::create (Client端)

[cpp]  view plain copy
  1. class BpMediaPlayerService: public BpInterface<IMediaPlayerService>  
  2. {  
  3. public:  
  4.     BpMediaPlayerService(const sp<IBinder>& impl)  
  5.         : BpInterface<IMediaPlayerService>(impl)  
  6.     {  
  7.     }  
  8.   
  9.     virtual sp<IMediaPlayer> create(  
  10.             pid_t pid, const sp<IMediaPlayerClient>& client,  
  11.             const char* url, const KeyedVector<String8, String8> *headers) {  
  12.         Parcel data, reply;  
  13.         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());  
  14.         data.writeInt32(pid);  
  15.         data.writeStrongBinder(client->asBinder());  
  16.         data.writeCString(url);  
  17.   
  18.         if (headers == NULL) {  
  19.             data.writeInt32(0);  
  20.         } else {  
  21.             // serialize the headers  
  22.             data.writeInt32(headers->size());  
  23.             for (size_t i = 0; i < headers->size(); ++i) {  
  24.                 data.writeString8(headers->keyAt(i));  
  25.                 data.writeString8(headers->valueAt(i));  
  26.             }  
  27.         }  
  28.   
  29.         remote()->transact(CREATE_URL, data, &reply);  //传递到BnMediaPlayerSerivce执行  
  30.         return interface_cast<IMediaPlayer>(reply.readStrongBinder()); //返回BpMediaPlayer  
  31.     }  
  32. }  

在BnMediaPlayerService处理CREATE_URL(Server端)

[cpp]  view plain copy
  1. status_t BnMediaPlayerService::onTransact(  
  2.     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)  
  3. {  
  4.     switch(code) {  
  5.         case CREATE_URL: { //收到上面的CREATE_URL并进行处理  
  6.             CHECK_INTERFACE(IMediaPlayerService, data, reply);  
  7.             pid_t pid = data.readInt32();  
  8.             sp<IMediaPlayerClient> client = //实质保存的为BpMediaPlayerClient,用于向MediaPlayer发通知消息,它为Client,MediaPlayer为Sever  
  9.                 interface_cast<IMediaPlayerClient>(data.readStrongBinder());  
  10.             const char* url = data.readCString();  
  11.   
  12.             KeyedVector<String8, String8> headers;  
  13.             int32_t numHeaders = data.readInt32();  
  14.             for (int i = 0; i < numHeaders; ++i) {  
  15.                 String8 key = data.readString8();  
  16.                 String8 value = data.readString8();  
  17.                 headers.add(key, value);  
  18.             }  
  19.             //在BnMediaPlayerService的派生类MediaPlayerService中实现,创建一个Client对象(Client:public BnMediaPlayer)  
  20.             sp<IMediaPlayer> player = create(  
  21.                     pid, client, url, numHeaders > 0 ? &headers : NULL);  
  22.   
  23.             reply->writeStrongBinder(player->asBinder());  
  24.             return NO_ERROR;  
  25.         } break;  
  26. }  

MediaPlayerService::create才是真正的实现创建IMediaPlayer

[cpp]  view plain copy
  1. <p>sp<IMediaPlayer> MediaPlayerService::create(  
  2.         pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,  
  3.         const KeyedVector<String8, String8> *headers)  
  4. {  
  5.     int32_t connId = android_atomic_inc(&mNextConnId);</p><p>     //创建一个Clinet,class Client : public BnMediaPlayer,真正实现start, stop, pause, resume...  
  6.     sp<Client> c = new Client(this, pid, connId, client);  
  7.     LOGV("Create new client(%d) from pid %d, url=%s, connId=%d", connId, pid, url, connId);</p><p>    //分析url,并创建真正的player(demuxer,decoder)  
  8.     if (NO_ERROR != c->setDataSource(url, headers))  
  9.     {  
  10.         c.clear();  
  11.         return c;  
  12.     }  
  13.     wp<Client> w = c;  
  14.     Mutex::Autolock lock(mLock);  
  15.     mClients.add(w);  
  16.     return c;//返回此BnMediaPlayer派生类的对象  
  17. }</p>  

MediaPlayerService::Client::setDataSource分析URL类型,然后创建对应的Player

[cpp]  view plain copy
  1. status_t MediaPlayerService::Client::setDataSource(  
  2.         const char *url, const KeyedVector<String8, String8> *headers)  
  3. {  
  4.     LOGV("setDataSource(%s)", url);  
  5.     if (url == NULL)  
  6.         return UNKNOWN_ERROR;  
  7.   
  8.     if (strncmp(url, "content://", 10) == 0) {  
  9.         // get a filedescriptor for the content Uri and  
  10.         // pass it to the setDataSource(fd) method  
  11.   
  12.         String16 url16(url);  
  13.         int fd = android::openContentProviderFile(url16);  
  14.         if (fd < 0)  
  15.         {  
  16.             LOGE("Couldn't open fd for %s", url);  
  17.             return UNKNOWN_ERROR;  
  18.         }  
  19.         setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus  
  20.         close(fd);  
  21.         return mStatus;  
  22.     } else {  
  23.         //根据url返回对应的player_type,如: PV_PLAYER,STAGEFRIGHT_PLAYER, XX_PLAYER  
  24.         player_type playerType = getPlayerType(url);  
  25.         LOGV("player type = %d", playerType);  
  26.   
  27.         // create the right type of player,建立真正的AVPlayer  
  28.         sp<MediaPlayerBase> p = createPlayer(playerType);  
  29.         if (p == NULL) return NO_INIT;  
  30.   
  31.         if (!p->hardwareOutput()) {  
  32.             mAudioOutput = new AudioOutput();  
  33.             static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);  
  34.         }  
  35.   
  36.         // now set data source,把URL传递给真正的AVPlayer  
  37.         LOGV(" setDataSource");  
  38.         mStatus = p->setDataSource(url, headers);  
  39.         p->setOutRange( mService->getLeft(), mService->getTop(), mService->getWidth(), mService->getHeight());   
  40.         if (mStatus == NO_ERROR) {  
  41.             mPlayer = p;  
  42.         } else {  
  43.             LOGE("  error: %d", mStatus);  
  44.         }  
  45.         return mStatus;  
  46.     }  
  47. }  

创建真正的AVPlayer

[cpp]  view plain copy
  1. sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)  
  2. {  
  3.     // determine if we have the right player type  
  4.     sp<MediaPlayerBase> p = mPlayer;  
  5.     LOGV("Enter MediaPlayerService::Client::createPlayer: playerType is %d", playerType);  
  6.     if ((p != NULL) && (p->playerType() != playerType)) {  
  7.         LOGV("p->playerType() is: %d, , delete player", p->playerType());  
  8.         p.clear();  
  9.     }  
  10.     if (p == NULL) {  
  11.         LOGV("Create new player");  
  12.         p = android::createPlayer(playerType, this, notify);  
  13.     }  
  14.     return p;  
  15. }  
  16. static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,  
  17.         notify_callback_f notifyFunc)  
  18. {  
  19.     sp<MediaPlayerBase> p;  
  20.     switch (playerType) {  
  21.         case XX_PLAYER:  
  22.             LOGV("Create XXPlayer");  
  23.             p = new XXPlayerService();  
  24.             break;  
  25. #ifndef NO_OPENCORE  
  26.         case PV_PLAYER:  
  27.             LOGV(" create PVPlayer");  
  28.             p = new PVPlayer();  
  29.             break;  
  30. #endif  
  31.         case SONIVOX_PLAYER:  
  32.             LOGV(" create MidiFile");  
  33.             p = new MidiFile();  
  34.             break;  
  35.         case VORBIS_PLAYER:  
  36.             LOGV(" create VorbisPlayer");  
  37.             p = new VorbisPlayer();  
  38.             break;  
  39. #if BUILD_WITH_FULL_STAGEFRIGHT  
  40.         case STAGEFRIGHT_PLAYER:  
  41.             LOGV(" create StagefrightPlayer");  
  42.             p = new StagefrightPlayer;  
  43.             break;  
  44. #endif  
  45.         case TEST_PLAYER:  
  46.             LOGV("Create Test Player stub");  
  47.             p = new TestPlayerStub();  
  48.             break;  
  49.     }  
  50.     if (p != NULL) {  
  51.         if (p->initCheck() == NO_ERROR) {  
  52.             p->setNotifyCallback(cookie, notifyFunc);  
  53.         } else {  
  54.             p.clear();  
  55.         }  
  56.     }  
  57.     if (p == NULL) {  
  58.         LOGE("Failed to create player object");  
  59.     }  
  60.     return p;  
  61. }  

6.4 开始播放MediaPlayer::start

MediaPlayer::mPlayer实质为BpMediaPlayer

MediaPlayerService::Client::mPlayer实质为真正的AVPlayer,如:PVPlayer, XXPlayer

class PVPlayer: public MediaPlayerInterface

class MediaPlayerInterface : public MediaPlayerBase

 

MediaPlayer::start()  -->  mPlayer->start(BpMediaPlayer::start) --> remote()->transact(START, data, &reply)--> BpBinder::transact--> IPCThreadState::self()->transact--> ioctl(写) --> ioctl(读,在IPCThreadState::joinThreadPool中调用) --> BBinder::transact --> BnMediaPlayer::onTransact --> MediaPlayerService::Client::start -->MediaPlayerService::Client::mPlayer->start(PVPlayer->start)

 

你可能感兴趣的:(深入了解mediaserver-3)