IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android MediaProjection 代码分析 -> 正文阅读

[移动开发]Android MediaProjection 代码分析

MediaProjection是什么
?


MediaProjection指的是一个令牌,这个令牌授权应用一个能力:去捕捉屏幕内容和系统声音
?* A token granting applications the ability to capture screen contents and/or
* record system audio. The exact capabilities granted depend on the type of
* MediaProjection.

MediaProjection本身并没有太多关于audio/vedio的操作,主要功能是个授权。

MediaProjectionManager:管理MediaProjection令牌的创建等

Manages the retrieval of certain types of MediaProjection?tokens.

Client 侧代码

SystemService怎样和Client 关联的

SystemService怎样和Client 关联的, getSystemService的返回值怎么就是MediaProjection??MediaProjection并没有实现IMediaProjectonManger接口啊。

MediaProjectionManager mProjectionManager = (MediaProjectionManager)   getSystemService(Context.MEDIA_PROJECTION_SERVICE);

在SystemServiceRegistry.java中进行了关联

frameworks/base/core/java/android/app/SystemServiceRegistry.java
registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
? ? ? ? new CachedServiceFetcher<MediaProjectionManager>() {
? ? @Override
? ? public MediaProjectionManager createService(ContextImpl ctx) {
? ? ? ? return new MediaProjectionManager(ctx);
? ? }});

client端代码结构

frameworks/base/media/java/android/media/projection

├── IMediaProjection.aidl
├── IMediaProjectionCallback.aidl
├── IMediaProjectionManager.aidl
├── IMediaProjectionWatcherCallback.aidl
├── MediaProjectionInfo.aidl
├── MediaProjectionInfo.java (表示谁去申请token, 或者说申请MediaProjection token的是谁)
├── MediaProjection.java
├── MediaProjectionManager.java

MadiaProjection.java

MediaProjection.java 成员变量是IMediaProjecton 接口类和 IMediaProjectionCallback 因为start是创建MediaProjection是调用的,还没有实例对象所以不能使用registerCallback 去注册,这也说明为什么IMedaiProjectionCallback只有一个成员onStop; 用户有可能多次调用registerCallback 比如一个audio, 一个vedio, 这样也形成了Map, 每个callback 在哪里执行又关联到Handler.

得到token后,这里有个有关VirtualDisplay.

?

MediaProjectionManager

它是和 systemService关联的 client 类,封装了IMedaProjectionMangaer接口类和Callback, 又重新组合了下IMediaProjectionManager更抽象,更方便使用

?

常规的使用MediaProjection的方法

1. 首先得到MediaProjectionManager

MediaProjectionManager mProjectionManager =(MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);

2.启动MediaProjectionPermissionActivity,且当改Activity结束时得到Intent
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
/**
* Returns an Intent that must be passed to startActivityForResult()
* in order to start screen capture. The activity will prompt
* the user whether to allow screen capture. ?The result of this
* activity should be passed to getMediaProjection.
*/
public Intent createScreenCaptureIntent() {
? ? ? ? Intent i = new Intent();
? ? ? ? final ComponentName mediaProjectionPermissionDialogComponent =
? ? ? ? ? ? ? ? ComponentName.unflattenFromString(
????????????????????????mContext.getResources().getString(
? ? ? ? ? ? ? ? ? ? ? ? com.android.internal.R.string
? ? ? ? ? ? ? ? ? ? ? ? .config_mediaProjectionPermissionDialogComponent));
? ? ? ? i.setComponent(mediaProjectionPermissionDialogComponent);
? ? ? ? return i;
}

3. MediaProjectionPermissionActivity去请求了MediaProjection

frameworks/base/packages/SystemUI/src/com/android/systemui/media/MediaProjectionPermissionActivity.java
MediaProjectionPermissionActivity
public class MediaProjectionPermissionActivity extends Activity
? ? ? ? implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {

? ? private String mPackageName;
? ? private int mUid;
? ? private IMediaProjectionManager mService;

? ? public void onCreate(Bundle icicle) {
? ? ? ? super.onCreate(icicle);

? ? ? ? mPackageName = getCallingPackage();
? ? ? ? IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE);
? ? ? ? mService = IMediaProjectionManager.Stub.asInterface(b);

? ? ? ? PackageManager packageManager = getPackageManager();
? ? ? ? ApplicationInfo aInfo;
? ? ? ? try {
? ? ? ? ? ? aInfo = packageManager.getApplicationInfo(mPackageName, 0);
? ? ? ? ? ? mUid = aInfo.uid;
? ? ? ? }

? ? ? ? try { //mUid, mPackageName已经有Permis
? ? ? ? ? ? if (mService.hasProjectionPermission(mUid, mPackageName)) {
? ? ? ? ? ? ? ? setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));
? ? ? ? ? ? ? ? finish();
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? }

? ? @Override
? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? try {
? ? ? ? ? ? if (which == AlertDialog.BUTTON_POSITIVE) {
? ? ? ? ? ? ? ? setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? // 重点是 获得IMediaProjection, 然后转换为Binder: 赋值到Intent
? ? // MediaProjectionManager.EXTRA_MEDIA_PROJECTION: projection.asBinder()
? ? private Intent getMediaProjectionIntent(int uid, String packageName)
? ? ? ? ? ? throws RemoteException {
? ? ? ? IMediaProjection projection = mService.createProjection(uid, packageName,
? ? ? ? ? ? ? ? ?MediaProjectionManager.TYPE_SCREEN_CAPTURE, false /* permanentGrant */);
? ? ? ? Intent intent = new Intent();
? ? ? ? intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder());
? ? ? ? return intent;
? ? }
}

4. 这样 onActivityResult的输入参数 Intent 就是上面赋值的Intent
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
? ? ? ? if (requestCode == REQUEST_CODE) {
? ? ? ? ? ? if (resultCode == Activity.RESULT_OK) {
? ? ? ? ? ? ? ? startService(com.mtsahakis.mediaprojectiondemo.ScreenCaptureService.getStartIntent(this, resultCode, data));
? ? ? ? ? ? }
? ? ? ? }
}

public int onStartCommand(Intent intent, int flags, int startId) {
? ?if (isStartCommand(intent)) {
? ? ? ? ? ? Intent data = intent.getParcelableExtra(DATA);
? ? ? ? ? ? startProjection(resultCode, data);
? ? }
}

5. 得到MediaProjection
private void startProjection(int resultCode, Intent data) {
? ? ? ? MediaProjectionManager mpManager =
? ? ? ? ? ? ? ? (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
? ? ? ? if (mMediaProjection == null) {
? ? ? ? ? ? mMediaProjection = mpManager.getMediaProjection(resultCode, data);
?? ?}
}

public MediaProjection getMediaProjection(int resultCode, @NonNull Intent resultData) {
? ? ? ? if (resultCode != Activity.RESULT_OK || resultData == null) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? IBinder projection = resultData.getIBinderExtra(EXTRA_MEDIA_PROJECTION);
? ? ? ? if (projection == null) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? return new MediaProjection(mContext, IMediaProjection.Stub.asInterface(projection));
}

最后得到了 MediaProjecton
audio/ vedio用的就是这个

通过反射不显示申请权限对话框

对照上面的方法,下面的操作应该能看懂

private MediaProjection getMediaProjection(Context context) {
? ? ? ? ? MediaProjection mediaProjection = null;
? ? ? ? ? MediaProjectionManager projectionManager = (MediaProjectionManager) context.getSystemService
? ? ? ? ? ? ? ? (Context.MEDIA_PROJECTION_SERVICE);
? ? ? ? ? if (projectionManager == null) {
? ? ? ? ? ? Log.e(TAG, "start record fail, reason : projectionManager is null");
? ? ? ? ? } else {
? ? ? ? ? ? Intent intent = new Intent();
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ApplicationInfo aInfo = context.getPackageManager().getApplicationInfo("xxx", 0);
? ? ? ? ? ? ? ? int uid = aInfo.uid;
? ? ? ? ? ? ? ? Log.w(TAG, "AppInfo: " + aInfo.toString());
? ? ? ? ? ? ? ? Class<?> smClazz = Class.forName("android.os.ServiceManager");
? ? ? ? ? ? ? ? Method getService = smClazz.getDeclaredMethod("getService", String.class);
? ? ? ? ? ? ? ? IBinder binder = (IBinder) getService.invoke(null, Context.MEDIA_PROJECTION_SERVICE);
? ? ? ? ? ? ? ? Class<?> mpmsClazz = Class.forName("android.media.projection.IMediaProjectionManager$Stub");
? ? ? ? ? ? ? ? Method asInterface = mpmsClazz.getDeclaredMethod("asInterface", IBinder.class);
? ? ? ? ? ? ? ? Object service = asInterface.invoke(null, binder);
? ? ? ? ? ? ? ? Class<?> IMediaProjectionManager = service.getClass();?

? ? ? ? ? ? ? ? Method createProjection = IMediaProjectionManager.getDeclaredMethod("createProjection", int.class,
? ? ? ? ? ? ? ? ? ? ? ? String.class, int.class, boolean.class);
? ? ? ? ? ? ? ? Object projection = createProjection.invoke(service, uid, "xxx", TYPE_SCREEN_CAPTURE, false);

? ? ? ? ? ? ? ? Class<?> mpmClazz = Class.forName("android.media.projection.IMediaProjection$Stub$Proxy");
? ? ? ? ? ? ? ? Method asBinder = mpmClazz.getDeclaredMethod("asBinder");
? ? ? ? ? ? ? ? IBinder projectionBinder = (IBinder) asBinder.invoke(projection);

? ? ? ? ? ? ? ? Class<?> intentClazz = Class.forName("android.content.Intent");
? ? ? ? ? ? ? ? Method putExtra = intentClazz.getDeclaredMethod("putExtra", String.class, IBinder.class);
? ? ? ? ? ? ? ? putExtra.invoke(intent, EXTRA_MEDIA_PROJECTION, projectionBinder);
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? Log.e(TAG, "Error in getMediaProjection", e);
? ? ? ? ? ? }

? ? ? ? ? ? mediaProjection = projectionManager.getMediaProjection(Activity.RESULT_OK, intent);
? ? ? ? ? ? if (mediaProjection != null) {
? ? ? ? ? ? ? ? Log.w(TAG, "create mediaProjection successfully");
? ? ? ? ? ? }
? ? ? ? ? }

? ? ? ? ? return mediaProjection;
}

Server 端代码结构

frameworks/base/services/core/java/com/android/server/media/projection

├── MediaProjectionManagerService.java

回调函数的处理

frameworks/base/services/core/java/com/android/server/media/projection/MediaProjectionManagerService.java

所有类作为MediaProjectionMangagerService的内部类, 接口的Stub都有具体的实现,为什么Proxy类没有 (因为proxy已经完整实现了接口函数:mRemote进行调用,不在需要overwrite, 但一般会被封装)

MediaProjection的功能

实现了静音录制,如果使用 remote_submit/mic如果无声录不到声音

?MediaProjection的缺点

如果多个应用都申请MediaProjection 如录屏和投屏等,当前的机制是只允许一个(最后一个),原来明白了,这个问题也是可解的。

? ?private void startProjectionLocked(final MediaProjection projection) {
? ? ? ? if (mProjectionGrant != null) {
? ? ? ? ? ? ? ? mProjectionGrant.stop();
? ? ? ? }
? ? ? ? if (mMediaRouteInfo != null) {
? ? ? ? ? ? mMediaRouter.getFallbackRoute().select();
? ? ? ? }
? ? ? ? mProjectionToken = projection.asBinder();
? ? ? ? mProjectionGrant = projection;
? ? ? ? dispatchStart(projection);
? ? }
  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-07-23 10:54:40  更:2021-07-23 10:55:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/28 15:01:56-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码