基于android 4.1.1 源码


【1】mediaserver启动后会把media相关一些服务添加到servicemanager中,其中就有mediaPlayerService.这样应用启动前,系统就有了mediaPlayerService这个服务程序。

[java] view plain copy
  1. intmain(intargc,char**argv)
  2. {
  3. sp<ProcessState>proc(ProcessState::self());
  4. sp<IServiceManager>sm=defaultServiceManager();
  5. ALOGI("ServiceManager:%p",sm.get());
  6. AudioFlinger::instantiate();
  7. MediaPlayerService::instantiate();
  8. CameraService::instantiate();
  9. AudioPolicyService::instantiate();
  10. ProcessState::self()->startThreadPool();
  11. IPCThreadState::self()->joinThreadPool();
  12. }
[cpp] view plain copy
  1. voidMediaPlayerService::instantiate(){
  2. defaultServiceManager()->addService(
  3. String16("media.player"),newMediaPlayerService());
  4. }

【2】应用层mediaPlayer=newMediaPlayer(); 调用SDK中MediaPlayer.java (frameworks\base\media\java\android\media\MediaPlayer.java)

[java] view plain copy
  1. publicMediaPlayer(){
  2. Looperlooper;
  3. if((looper=Looper.myLooper())!=null){
  4. mEventHandler=newEventHandler(this,looper);
  5. }elseif((looper=Looper.getMainLooper())!=null){
  6. mEventHandler=newEventHandler(this,looper);
  7. }else{
  8. mEventHandler=null;
  9. }
  10. /*Nativesetuprequiresaweakreferencetoourobject.
  11. *It'seasiertocreateitherethaninC++.
  12. */
  13. native_setup(newWeakReference<MediaPlayer>(this));
  14. }

通过JNI方式调用到framework层android_media_MediaPlayer.cpp(\frameworks\base\media\jni\android_media_MediaPlayer.cpp)

[cpp] view plain copy
  1. staticvoid
  2. android_media_MediaPlayer_native_setup(JNIEnv*env,jobjectthiz,jobjectweak_this)
  3. {
  4. ALOGV("native_setup");
  5. sp<MediaPlayer>mp=newMediaPlayer();
  6. if(mp==NULL){
  7. jniThrowException(env,"java/lang/RuntimeException","Outofmemory");
  8. return;
  9. }
  10. //createnewlistenerandgiveittoMediaPlayer
  11. sp<JNIMediaPlayerListener>listener=newJNIMediaPlayerListener(env,thiz,weak_this);
  12. mp->setListener(listener);
  13. //StowournewC++MediaPlayerinanopaquefieldintheJavaobject.
  14. setMediaPlayer(env,thiz,mp);
  15. }

继而调用mediaplayer.cpp(frameworks\av\media\libmedia\mediaplayer.cpp)

【3】在整个应用程序的进程中,mediaplayer.cpp 中setDataSource会从service manager中获得mediaPlayerService服务,然后通过服务来创建player。这个player就是播放器的真实实例。

[cpp] view plain copy
  1. status_tMediaPlayer::setDataSource(constsp<IStreamSource>&source)
  2. {
  3. ALOGV("setDataSource");
  4. status_terr=UNKNOWN_ERROR;
  5. constsp<IMediaPlayerService>&service(getMediaPlayerService());
  6. if(service!=0){
  7. sp<IMediaPlayer>player(service->create(getpid(),this,mAudioSessionId));
  8. if((NO_ERROR!=doSetRetransmitEndpoint(player))||
  9. (NO_ERROR!=player->setDataSource(source))){
  10. player.clear();
  11. }
  12. err=attachNewPlayer(player);
  13. }
  14. returnerr;
  15. }
【4】通过getMediaPlayerService 得到的service其实是BpMediaPlayerService,这是和 mediaPlayerService 进程中的BnMediaPlayerService 相对应负责binder通讯。BpMediaPlayerService中的create其实通过binder机制将CREATE消息发送出去。

[cpp] view plain copy
  1. virtualsp<IMediaPlayer>create(
  2. pid_tpid,constsp<IMediaPlayerClient>&client,intaudioSessionId){
  3. Parceldata,reply;
  4. data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
  5. data.writeInt32(pid);
  6. data.writeStrongBinder(client->asBinder());
  7. data.writeInt32(audioSessionId);
  8. remote()->transact(CREATE,data,&reply);
  9. returninterface_cast<IMediaPlayer>(reply.readStrongBinder());
  10. }

在对面的 BnMediaPlayerService中,通过onTransact()来接受这些消息。并把结果返回。

[cpp] view plain copy
  1. status_tBnMediaPlayerService::onTransact(
  2. uint32_tcode,constParcel&data,Parcel*reply,uint32_tflags)
  3. {
  4. switch(code){
  5. caseCREATE:{
  6. CHECK_INTERFACE(IMediaPlayerService,data,reply);
  7. pid_tpid=data.readInt32();
  8. sp<IMediaPlayerClient>client=
  9. interface_cast<IMediaPlayerClient>(data.readStrongBinder());
  10. intaudioSessionId=data.readInt32();
  11. sp<IMediaPlayer>player=create(pid,client,audioSessionId);
  12. reply->writeStrongBinder(player->asBinder());
  13. returnNO_ERROR;
  14. }break;
  15. caseMAKE_CRYPTO:
  16. }
当发现是CREATE才真正调用了MediaPlayerService 中的create函数。在create函数中其实是创建了一个MediaPlayerService::Client的实例,也就是 说 MediaPlayerService会为每个client应用进程创建一个相应的MediaPlayerService::Client的实例,来提供服务。

[cpp] view plain copy
  1. sp<IMediaPlayer>MediaPlayerService::create(pid_tpid,constsp<IMediaPlayerClient>&client,
  2. intaudioSessionId)
  3. {
  4. int32_tconnId=android_atomic_inc(&mNextConnId);
  5. sp<Client>c=newClient(
  6. this,pid,connId,client,audioSessionId,
  7. IPCThreadState::self()->getCallingUid());
  8. ALOGV("Createnewclient(%d)frompid%d,uid%d,",connId,pid,
  9. IPCThreadState::self()->getCallingUid());
  10. wp<Client>w=c;
  11. {
  12. Mutex::Autolocklock(mLock);
  13. mClients.add(w);
  14. }
  15. returnc;
  16. }

【5】这样 mediaplayer.cpp 就得到了一个player的实例,对他来说这个实例和本地的其他类的实例没什么用法上的区别,殊不知其实这个实例是运行在另外一个进程中。实现这种假象的就是binder机制。获得这个实例后继续player->setDataSource().在 MediaPlayerService的进程中他的实际函数中,才会真正的创建Stagefright的具体实例。 [cpp] view plain copy
  1. status_tMediaPlayerService::Client::setDataSource(intfd,int64_toffset,int64_tlength)
  2. {
  3. ALOGV("setDataSourcefd=%d,offset=%lld,length=%lld",fd,offset,length);
  4. structstatsb;
  5. intret=fstat(fd,&sb);
  6. if(ret!=0){
  7. ALOGE("fstat(%d)failed:%d,%s",fd,ret,strerror(errno));
  8. returnUNKNOWN_ERROR;
  9. }
  10. ALOGV("st_dev=%llu",sb.st_dev);
  11. ALOGV("st_mode=%u",sb.st_mode);
  12. ALOGV("st_uid=%lu",sb.st_uid);
  13. ALOGV("st_gid=%lu",sb.st_gid);
  14. ALOGV("st_size=%llu",sb.st_size);
  15. if(offset>=sb.st_size){
  16. ALOGE("offseterror");
  17. ::close(fd);
  18. returnUNKNOWN_ERROR;
  19. }
  20. if(offset+length>sb.st_size){
  21. length=sb.st_size-offset;
  22. ALOGV("calculatedlength=%lld",length);
  23. }
  24. //Untilre-transmitfunctionalityisaddedtotheexistingcoreandroid
  25. //players,weusethespecialAAHTXplayerwheneverwewereconfiguredfor
  26. //retransmission.
  27. player_typeplayerType=getPlayerType(fd,offset,length);
  28. sp<MediaPlayerBase>p=setDataSource_pre(playerType);
  29. if(p==NULL){
  30. returnNO_INIT;
  31. }
  32. //nowsetdatasource
  33. setDataSource_post(p,p->setDataSource(fd,offset,length));
  34. returnmStatus;
  35. }
[cpp] view plain copy
  1. sp<MediaPlayerBase>MediaPlayerService::Client::setDataSource_pre(
  2. player_typeplayerType)
  3. {
  4. ALOGV("playertype=%d",playerType);
  5. //createtherighttypeofplayer
  6. sp<MediaPlayerBase>p=createPlayer(playerType);
  7. if(p==NULL){
  8. returnp;
  9. }
  10. if(!p->hardwareOutput()){
  11. mAudioOutput=newAudioOutput(mAudioSessionId);
  12. static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
  13. }
  14. returnp;
  15. }
[cpp] view plain copy
  1. sp<MediaPlayerBase>MediaPlayerService::Client::createPlayer(player_typeplayerType)
  2. {
  3. //determineifwehavetherightplayertype
  4. sp<MediaPlayerBase>p=mPlayer;
  5. if((p!=NULL)&&(p->playerType()!=playerType)){
  6. ALOGV("deleteplayer");
  7. p.clear();
  8. }
  9. if(p==NULL){
  10. p=android::createPlayer(playerType,this,notify);
  11. }
  12. if(p!=NULL){
  13. p->setUID(mUID);
  14. }
  15. returnp;
  16. }
[cpp] view plain copy
  1. staticsp<MediaPlayerBase>createPlayer(player_typeplayerType,void*cookie,
  2. notify_callback_fnotifyFunc)
  3. {
  4. sp<MediaPlayerBase>p;
  5. switch(playerType){
  6. caseSONIVOX_PLAYER:
  7. ALOGV("createMidiFile");
  8. p=newMidiFile();
  9. break;
  10. caseSTAGEFRIGHT_PLAYER:
  11. ALOGV("createStagefrightPlayer");
  12. p=newStagefrightPlayer;
  13. break;
  14. caseNU_PLAYER:
  15. ALOGV("createNuPlayer");
  16. p=newNuPlayerDriver;
  17. break;
  18. caseTEST_PLAYER:
  19. ALOGV("CreateTestPlayerstub");
  20. p=newTestPlayerStub();
  21. break;
  22. caseAAH_RX_PLAYER:
  23. ALOGV("create[email protected]RXPlayer");
  24. p=createAAH_RXPlayer();
  25. break;
  26. caseAAH_TX_PLAYER:
  27. ALOGV("create[email protected]TXPlayer");
  28. p=createAAH_TXPlayer();
  29. break;
  30. default:
  31. ALOGE("Unknownplayertype:%d",playerType);
  32. returnNULL;
  33. }
  34. if(p!=NULL){
  35. if(p->initCheck()==NO_ERROR){
  36. p->setNotifyCallback(cookie,notifyFunc);
  37. }else{
  38. p.clear();
  39. }
  40. }
  41. if(p==NULL){
  42. ALOGE("Failedtocreateplayerobject");
  43. }
  44. returnp;
  45. }

在上面中已经看不到opencore的影子了,creaPlayer 中会根据类型来创建播放器的实例。Stagefright的实例就是在这里创建的。

下一步我们能真正进入到Stagefright里了

更多相关文章

  1. Android之TabLayout常见问题解决+TabLayout+ViewPager实现标签页
  2. SystemUI编译遇到的问题
  3. Android(安卓)RIL 架构学习总结
  4. Android(安卓)studio添加按钮点击进入下一页
  5. android pagerView
  6. Android(安卓)SystemServer 启动流程
  7. Android(安卓)RIL 架构学习总结 .
  8. Android(安卓)Activity 四种启动模式
  9. Fresco源码解析 - 创建一个ImagePipeline(一)

随机推荐

  1. android流量统计
  2. Android将死,Web OS才是王道——通过Googl
  3. Android淘宝好评星级进度条RatingBar原来
  4. Android进程永生技术终极揭秘:进程被杀底
  5. android AutoCompleteTextView 实现输入
  6. Android中隐藏ActionBar的方法
  7. Android获取手机方向
  8. android带图片的AlertDialog和文件管理器
  9. Android Button 点击时替换背景颜色和替
  10. 做了六年Android,终于熬出头了,15K到31K全