MediaPlayer-MediaPlayerService-MediaPlayerService::Client的三角关系

Unity EventSystem IXXXHandler事件详解 官方文档链接:http://docs.unity3d.com/460/Documentation/Manual/SupportedEvents.html 部分内容参考:https://blog.csdn.net/lyh916/article/details/44570503   Unity自己的事件系统开放了一些接口,我们只需要实现这些接口就行。但是这个接口所代表的含义需要我们深入理解。 ... 阅读详情

http://blog.csdn.net/myarrow/article/details/7054936

1. MediaPlayer是客户端

2. MediaPlayerService和MediaPlayerService::Client是服务器端。

2.1 MediaPlayerService实现IMediaPlayerService定义的业务逻辑,其主要功能是根据MediaPlayer::setDataSource输入的URL调用create函数创建对应的Player.

2.2 MediaPlayerService::Client实现IMediaPlayer定义的业务逻辑,其主要功能包括start, stop, pause, resume...,其实现方法是调用MediaPlayerService create的Player中的对应方法来实现具体功能。

2.3 它们的三角恋如下图所示:

 3. IXX是什么东东?

在一个BnXXX或BpXXX都派生于两个类,具体情况如下:

class BpXXX : public IXXX, public BpRefBase

class BnXXX : public IXXX, public BBinder

3.1 看到了吧,BpXXX和BnXXX都派生于IXXX,哪IXXX又是做什么的呢? 明确地告诉你,定义业务逻辑,但在BpXXX与BnXXX中的实现方式不同:

1) 在BpXXX中,把对应的binder_transaction_data打包之后通过BpRefBase中的mRemote(BpBinder)发送出去,并等待结果

2) 在BnXXX中,实现对应的业务逻辑,通过调用BnXXX派生类中的方法来实现,如MediaPlayerService::Client

懂了吧, IXXX定义业务逻辑

3.2 哪另外一个做什么呢?

从上图可以看出,是用来进行进程间通信用的。

BpRefBase中有一个mRemote(BpBinder)用来与Binder驱动交互用的。

BBinder是用来从Binder驱动中接收相关请求,并进行相关处理的。

 

4. 不得不说的MediaPlayerService

class MediaPlayerService : public BnMediaPlayerService

它本身用来实现IMediaPlayerService中定义的业务逻辑,可以看看IMediaPlayerService到底定义了些什么东东?

  1. class IMediaPlayerService: public IInterface  
  2. {  
  3. public:  
  4.     DECLARE_META_INTERFACE(MediaPlayerService);  
  5.   
  6.     virtual sp<IMediaRecorder>  createMediaRecorder(pid_t pid) = 0;  
  7.     virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid) = 0;  
  8.     virtual sp<IMediaPlayer>    create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url, const KeyedVector<String8, String8> *headers = NULL) = 0;  
  9.     virtual sp<IMediaPlayer>    create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length) = 0;  
  10.     virtual sp<IMediaConfig>    createMediaConfig(pid_t pid ) = 0;  
  11.     virtual sp<IMemory>         decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) = 0;  
  12.     virtual sp<IMemory>         decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) = 0;  
  13.     virtual sp<IOMX>            getOMX() = 0;  
  14.   
  15.     // Take a peek at currently playing audio, for visualization purposes.  
  16.     // This returns a buffer of 16 bit mono PCM data, or NULL if no visualization buffer is currently available.  
  17.     virtual sp<IMemory>         snoop() = 0;  
  18. };  
class IMediaPlayerService: public IInterface
{
public:
    DECLARE_META_INTERFACE(MediaPlayerService);

    virtual sp<IMediaRecorder>  createMediaRecorder(pid_t pid) = 0;
    virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid) = 0;
    virtual sp<IMediaPlayer>    create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url, const KeyedVector<String8, String8> *headers = NULL) = 0;
    virtual sp<IMediaPlayer>    create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length) = 0;
    virtual sp<IMediaConfig>    createMediaConfig(pid_t pid ) = 0;
    virtual sp<IMemory>         decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) = 0;
    virtual sp<IMemory>         decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) = 0;
    virtual sp<IOMX>            getOMX() = 0;

    // Take a peek at currently playing audio, for visualization purposes.
    // This returns a buffer of 16 bit mono PCM data, or NULL if no visualization buffer is currently available.
    virtual sp<IMemory>         snoop() = 0;
};


从上面可以看出,还说自己是一个MediaPlayerService,而且还向大内总管ServiceManager进行了注册,就这么个create和decode,能做什么呢? 我们最熟悉的start ,stop, pause, resume...怎么不见呢?没有这些东东,MediaPlayer需要的功能如何实现呢?

 带着这么多疑问,我们先看看MediaPlayerService是如何实现上述功能的,它到底做了些什么?

还是让代码自己说话吧:

  1. 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);  
  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);  
  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;  
  17. }  
sp<IMediaPlayer> MediaPlayerService::create(
        pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,
        const KeyedVector<String8, String8> *headers)
{
    int32_t connId = android_atomic_inc(&mNextConnId);
    sp<Client> c = new Client(this, pid, connId, client);
    LOGV("Create new client(%d) from pid %d, url=%s, connId=%d", connId, pid, url, connId);
    if (NO_ERROR != c->setDataSource(url, headers))
    {
        c.clear();
        return c;
    }
    wp<Client> w = c;
    Mutex::Autolock lock(mLock);
    mClients.add(w);
    return c;
}

原来如此,它负责创建MediaPlayer需要的对应功能,创建了Client(MediaPlayerService),MediaPlayerService::Client的定义如下:

class Client : public BnMediaPlayer, 它实现了IMediaPlayer定义的业务逻辑,哪就看看IMediaPlayer定义了些什么:

  1. class IMediaPlayer: public IInterface  
  2. {  
  3. public:  
  4.     DECLARE_META_INTERFACE(MediaPlayer);  
  5.   
  6.     virtual void            disconnect() = 0;  
  7.   
  8.     virtual status_t        setVideoSurface(const sp<ISurface>& surface) = 0;  
  9.     virtual status_t        setSurfaceXY(int x,int y) = 0;  /*add by h00186041   add interface for surface left-up position*/  
  10.     virtual status_t        setSurfaceWH(int w,int h) = 0;  /*add by h00186041  add interface for surface size*/  
  11.     virtual status_t        prepareAsync() = 0;  
  12.     virtual status_t        start() = 0;  
  13.     virtual status_t        stop() = 0;  
  14.     virtual status_t        pause() = 0;  
  15.     virtual status_t        isPlaying(bool* state) = 0;  
  16.     virtual status_t        seekTo(int msec) = 0;  
  17.     virtual status_t        getCurrentPosition(int* msec) = 0;  
  18.     virtual status_t        getDuration(int* msec) = 0;  
  19.     virtual status_t        reset() = 0;  
  20.     virtual status_t        setAudioStreamType(int type) = 0;  
  21.     virtual status_t        setLooping(int loop) = 0;  
  22.     virtual status_t        setVolume(float leftVolume, float rightVolume) = 0;  
  23.     virtual status_t        suspend() = 0;  
  24.     virtual status_t        resume() = 0;  
  25.   
  26.     // Invoke a generic method on the player by using opaque parcels  
  27.     // for the request and reply.  
  28.     // @param request Parcel that must start with the media player  
  29.     // interface token.  
  30.     // @param[out] reply Parcel to hold the reply data. Cannot be null.  
  31.     // @return OK if the invocation was made successfully.  
  32.     virtual status_t        invoke(const Parcel& request, Parcel *reply) = 0;  
  33.   
  34.     // Set a new metadata filter.  
  35.     // @param filter A set of allow and drop rules serialized in a Parcel.  
  36.     // @return OK if the invocation was made successfully.  
  37.     virtual status_t        setMetadataFilter(const Parcel& filter) = 0;  
  38.   
  39.     // Retrieve a set of metadata.  
  40.     // @param update_only Include only the metadata that have changed  
  41.     //                    since the last invocation of getMetadata.  
  42.     //                    The set is built using the unfiltered  
  43.     //                    notifications the native player sent to the  
  44.     //                    MediaPlayerService during that period of  
  45.     //                    time. If false, all the metadatas are considered.  
  46.     // @param apply_filter If true, once the metadata set has been built based  
  47.     //                     on the value update_only, the current filter is  
  48.     //                     applied.  
  49.     // @param[out] metadata On exit contains a set (possibly empty) of metadata.  
  50.     //                      Valid only if the call returned OK.  
  51.     // @return OK if the invocation was made successfully.  
  52.     virtual status_t        getMetadata(bool update_only,  
  53.                                         bool apply_filter,  
  54.                                         Parcel *metadata) = 0;  
  55. }  
class IMediaPlayer: public IInterface
{
public:
    DECLARE_META_INTERFACE(MediaPlayer);

    virtual void            disconnect() = 0;

    virtual status_t        setVideoSurface(const sp<ISurface>& surface) = 0;
    virtual status_t        setSurfaceXY(int x,int y) = 0;  /*add by h00186041   add interface for surface left-up position*/
    virtual status_t        setSurfaceWH(int w,int h) = 0;  /*add by h00186041  add interface for surface size*/
    virtual status_t        prepareAsync() = 0;
    virtual status_t        start() = 0;
    virtual status_t        stop() = 0;
    virtual status_t        pause() = 0;
    virtual status_t        isPlaying(bool* state) = 0;
    virtual status_t        seekTo(int msec) = 0;
    virtual status_t        getCurrentPosition(int* msec) = 0;
    virtual status_t        getDuration(int* msec) = 0;
    virtual status_t        reset() = 0;
    virtual status_t        setAudioStreamType(int type) = 0;
    virtual status_t        setLooping(int loop) = 0;
    virtual status_t        setVolume(float leftVolume, float rightVolume) = 0;
    virtual status_t        suspend() = 0;
    virtual status_t        resume() = 0;

    // Invoke a generic method on the player by using opaque parcels
    // for the request and reply.
    // @param request Parcel that must start with the media player
    // interface token.
    // @param[out] reply Parcel to hold the reply data. Cannot be null.
    // @return OK if the invocation was made successfully.
    virtual status_t        invoke(const Parcel& request, Parcel *reply) = 0;

    // Set a new metadata filter.
    // @param filter A set of allow and drop rules serialized in a Parcel.
    // @return OK if the invocation was made successfully.
    virtual status_t        setMetadataFilter(const Parcel& filter) = 0;

    // Retrieve a set of metadata.
    // @param update_only Include only the metadata that have changed
    //                    since the last invocation of getMetadata.
    //                    The set is built using the unfiltered
    //                    notifications the native player sent to the
    //                    MediaPlayerService during that period of
    //                    time. If false, all the metadatas are considered.
    // @param apply_filter If true, once the metadata set has been built based
    //                     on the value update_only, the current filter is
    //                     applied.
    // @param[out] metadata On exit contains a set (possibly empty) of metadata.
    //                      Valid only if the call returned OK.
    // @return OK if the invocation was made successfully.
    virtual status_t        getMetadata(bool update_only,
                                        bool apply_filter,
                                        Parcel *metadata) = 0;
}


终于找到了思念已久的start, stop, pause, resume...

所以MediaPlayer::setDataSource返回时,会创建一个与MediaPlayerService::Client对应的BpMediaPlayer,用于获取MediaPlayerService::Client的各项功能。

4. 1 MediaPlayer又是如何找到MediaPlayerService::Client的呢? 只有MediaPlayerService才向ServiceManager进行了注册,所以MediaPlayer必须先获取BpMediaPlayerService,然后通过BpMediaService的管理功能create,来创建一个MediaPlayerService::Client.

4.2 为什么不直接定义一个MediaPlayer向ServiceManager注册呢?

也许是为了系统简化吧,MediaPlayerService包含的功能不只是Client, 还有AudioOutput,AudioCache,MediaConfigClient功能。现在明白了吧,MediaPlayerService就是一个媒体服务的接口,它先把客人接回来,再根据客人的需求,安排不同的员工去做,就这么简单。



MediaPlayer-MediaPlayerService-MediaPlayerService Client的三角 MediaPlayer-MediaPlayerService-MediaPlayerService Client的三角 阅读详情

相关推荐

I Love xxx.rar

程序员表白神奇,显示表白人名称,相恋时间,心形,动态背景等。预览http://wfhuang.coding.me/LoveJuan/

No unique bean of type [com.xxx.service.Ixxx] is defined: expected single matchi

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.xxx.service.impl.TestxxxImpl': Unsatisfied dependency expressed through bean property 'xxx': ...

青春有块盒子的地 2万+

【Nature Communications】超高介电常数材料 Hf0.5Zr0.5O2(HZO)

复旦大学AnQuanJiang团队在《NatureCommunications》发表研究,通过原子层沉积技术制备Hf0.5Zr0.5O2(HZO)薄膜电容器,结合等离子体处理获得突破性性能:介电常数达921,储能密度584J/cm³,效率近100%。研究揭示了铁电性消失后氧空位有序排列产生超高介电常数的机制,并通过同步辐射衍射和STEM分析证实了微观结构转变。该材料在85-300K温度范围内保持稳定,经1万亿次循环后仍能维持100μC/cm²电荷密度,为高密度DRAM和CMOS器件集成提供了新方案。

荷塘阅色 2484

关于android native层的Ixxx 接口中Binder机制的探讨

一直没仔细研究这个,今天抽时间整理一下。 最能解惑的文章是这篇对mediaplayer的解析 http://blog.csdn.net/wyl123/article/details/4300673 我按自己的思路整理+无指向归总。 背景: 一般涉及到Hardware或者native部分的android java API的实现,在越过JNI的映射机制之后,很多背后都有一个Bin

wugq_dl的博客 1万+

USB HID虚拟电池:Windows零驱动电源状态模拟方案

虚拟电池是一种在硬件测试与功耗分析中替代真实电池、实现可控电源状态注入的关键技术。其核心原理是通过标准USB HID协议(特别是UPS设备类)向操作系统上报可编程的AC/DC状态、剩余电量、容量参数等,绕过内核驱动限制,达成毫秒级响应与跨Windows版本兼容。该技术显著提升SoC能效验证、电池健康度校准及低电量行为触发等场景的可重复性与精度。本文基于CH554微控制器实现符合HID UPS规范的物理层虚拟电池设备,支持零驱动安装、USB原生识别与实时参数调控,适用于笔记本平台功耗建模、电源管理策略验证及自

weixin_35755188的博客 363

ElasticSearch读写性能调优

深层次理解,虽然是每隔一秒就会将内存中的segment数据刷新到segment file中,但实际上这里并没有直接到磁盘文件,而是先写入到OS Cache中。以refresh_interval为间隔时间,将保存在ES 内存缓存区中的数据 刷新到 os的文件系统缓存中,定期清空es的buffer,生成segment。注意:ES 的默认设置,已经综合考虑了数据可靠性,搜索的实时性,写入速度,一般不要盲目修改。将segment刷新到os cache中,并开放了查询权限,以提升搜索的实时性。

qq_44027353的博客 1814

ls -lrt,df -h,netstat -na ,grep -i xxx几个Linux命令介绍

用到的几个命令:ls -lrtdf -hdf -h .netstat -na | grep -i 50200cat /etc/services | grep -i XXX以下进行详细介绍:1. 按时间排序:ls -lrtls按时间排序ls -lt 从新到旧ls -lrt 从旧到新Linux下如何查看一个目录下的内容?并按照修改时间升序排序命令:ls -lrt详细解释:-l ...

weixin_34296641的博客 519

YOLOv13改进策略【小目标改进】| Shape-NWD:融合改进,结合Shape-IoU和NWD 更好地适应小目标特性

Bw−wgt2h−hgt2weight2Bweight2w−wgt​2h−hgt​2​其中weight2weight = 2weight2。

Limiiiing的博客 601

MediaPlayer-MediaPlayerService-MediaPlayerService Client的三角

MediaPlayer-MediaPlayerService-MediaPlayerService Client的三角

hddghhfd的博客 969

OpenClaw常见4大赋能工作场景

OpenClaw不仅仅是一个AI工具,更像是个人与企业的“数字操作系统”——它连接大模型与真实世界,让“思考”与“行动”无缝闭环。

王国平的博客 631

精密装配机器人系列编程:ABB IRB 120_(20).编程项目设计与实施

在精密装配机器人系列编程中,项目的成功设计与实施涉及多个关键步骤。从项目需求分析到最终调试,每一个环节都需要细致的考虑和专业的技术。选择合适的编程语言、设计良好的程序结构、掌握调试技巧以及解决常见问题,都是确保机器人高效、稳定运行的重要因素。通过不断优化和改进,可以提高生产效率和产品质量,满足不断变化的生产需求。

zhubeibei168的博客 1249

Android Multimedia框架总结(七)C++中MediaPlayer的C/S架构补充及MediaService介绍

前面一篇主要介绍c++中MediaPlayer的C/S架构中和Client相关部分,并中间穿插了mediaplayerservice的部分。但是对于这块C/S部分,没有放大去分析。- C/S模型中的三角关系流程图 - MediaPlayerService相关联的类图 - MediaPlayerService的产生过程 - MediaPlayerService添加服务过程 - MediaPlayerService通过

欢迎关注公众号:【码农突围】,公号后台回复9999,可以获取一份500页的LeetCode刷题笔记。 8032

[PCIe5.0] 5.3.1.4.2 D3Cold State

主电源(Vcc)完全断开,仅保留极少量辅助电源(Vaux)用于维持基本唤醒功能设备功能完全关闭,所有寄存器状态丢失(除非有特殊存储)需要完全重新初始化才能恢复工作比D3Hot状态功耗更低,但唤醒延迟更长。

系统固件工程师 461

Code Fragment-使用IXXX声明接口,IXXXImpl作为其子类

在很简单的实现关系,可以如此,看上去很清晰。 接口如下: public interface ITracker { public void startTrack(); public void tracking(); public void endTrack(); } 实现类如下: public class TrackImpl implements ITracker

time_hunter的专栏 4594

PrimeTime low power-SMVA分析(4)

这些功能只有在将timing_enable_cross_voltage_domain_analysis变量设置为true时才能使用。DVFS场景与在分布式多场景分析(DMSA)中分析的运行条件场景无关。- check_timing 和 report_exceptions 命令通过将检查或报告应用于所有可能的场景和电压组合来支持SMVA。- 相位锁定环生成的时钟(create_generated_clock -pll_feedback 和 -pll_output)在多个场景的情况下,返回最差情况的值。

QRBQ 654

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

java.lang.NoSuchFieldError: No static field septal_line3 of type I in class com/example/xxxxx/xxxxxx

03-27 08:41:48.942 29308-29308/com.xx.xxxxxxxxxxxxx E/AndroidRuntime: FATAL EXCEPTION: main Process: com.xx.xxxxxxxxxxxxx, PID: 29308 java.lang.NoSuchFieldError: No static field septal_line3 ...

sinat_35958166的博客 19万+

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Android BINDER详解

1.进程间通信的本质(2个进程) 用户空间的进程如果想相互通信,必须经过内核,因为不同进程的用户地址空间是独立的,但是共享同一个内核空间. 内核为了支持进程间通信,一般会有一个驱动,以字符设备的形式存在(也可以是其它形式,这个驱动的本质就是在不同的进程间传递数据).两个进程间通信一般以client/server的形式进行,大致流程如下: ser...

Android系统攻城狮 4418

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Set up your own PBX with Asterisk

Important: To log stuff to the console, either use Verbose(), or use NoOp() but the latter will only work if you set "verbosity" to at least 3 (in the console, type "set verbose 3").Why choose

demon_evil的专栏 12万+

ffmpeg -i xxx.mp4 At least one output file must be specified

这个问题指的是视频流的输出格式没有指定,可以随便弄个字符串来把视频流输出为字符串名字的文件中,但是不是很想输出成文件,所有指定输出格式为空。

asitMJ的博客 3485

linux下 - lrt命令,ls -lrt,df -h,netstat -na ,grep -i xxx几个Linux命令介绍

用到的几个命令:ls -lrtdf -hdf -h .netstat -na | grep -i 50200cat /etc/services | grep -i XXX以下进行详细介绍:1. 按时间排序:ls -lrtls按时间排序ls -lt 从新到旧ls -lrt 从旧到新Linux下如何查看一个目录下的内容?并按照修改时间升序排序命令:ls -lrt详细解释:-l use a lo...

weixin_26870313的博客 2016

MyBatis的MapperScan和Mapper注解,你会混淆吗?

笔者参与的产品开发时使用了多模块结构,不同模块使用 SpringBoot ,基本结构差不多,最近排查一个问题时,发现两个模块,一个没有使用 `@MapperScan` ,另一个使用了该注解,但是 MyBatis 的 `Mapper` 类都能被正确扫描到。 统一代码结构,去掉注解后,一个模块报错,提示注入 DAO 类失败。于是对比了下两个工程,跟踪两个注解的使用方法,找到了问题的根源。本文来整理下这个过程。

本博客纯属个人网络笔记,如对阁下有用,甚喜;否则,关掉即可,不喜勿喷。专心写代码,不做键盘侠! 2881
上一篇: interface_cast做什么的?&&Android中真正的Player
下一篇: 深入分析Android (build/core/*.mk脚本)
GabbyZang
博客等级 码龄14年 180粉丝 73原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值