aosp14/15上的针对单app录屏原理探索及自实现的方案讨论

22 阅读2分钟

背景:

在aosp高版本14/15上出现了一个新的功能是可以针对某个单独的app进行录屏,这样功能的目的主要还是为了保护用户手机上的隐私保护。具体功能的展示图如下: 在这里插入图片描述

在这里插入图片描述 在这里插入图片描述

新版本的单独app进行录制视频功能的交互大概就是上面的,整个过程就是让用户可以选择一个app,然后启动这个app到前台,再进行单独录屏处理。

自实现单app录屏方案:

如果针对单app进行录屏的功能要我们完全自己实现会如何实现呢?大家是否有相关的方案呢? 其实这里大家学习过SurfaceFlinger课程时候,有给大家讲解过一个实战项目是针对某个app进行单图层进行截图方案,是不是和这个很类似,大家可以考虑一下是否可以进行简单的修改就可以实现对这个单app录屏功能。这个自己方案实现单app录屏功能就留给各位学员的一个作业了,大家自行实现,实现后拿出来给马哥看看。

aosp实现单app录屏核心原理剖析

这里针对aosp上实现单app录屏的进行一些代码的剖析,本篇不进行详细的源码分析,主要把核心原理给大家讲解清楚

核心代码:


//mRecordedWindowContainer就是选择的app的WindowContainer,这里对于app的图层进行Mirror
mRecordedSurface = SurfaceControl.mirrorSurface(
        mRecordedWindowContainer.getSurfaceControl());
SurfaceControl.Transaction transaction =
        mDisplayContent.mWmService.mTransactionFactory.get()
                // Set the mMirroredSurface's parent to the root SurfaceControl for this
                // DisplayContent. This brings the new mirrored hierarchy under this
                // DisplayContent,
                // so SurfaceControl will write the layers of this hierarchy to the
                // output surface
                // provided by the app.
                .reparent(mRecordedSurface, mDisplayContent.getSurfaceControl())//mirror app后的图层进行挂载到displaycontent下面
                // Reparent the SurfaceControl of this DisplayContent to null, to prevent
                // content
                // being added to it. This ensures that no app launched explicitly on the
                // VirtualDisplay will show up as part of the mirrored content.
                .reparent(mDisplayContent.getWindowingLayer(), null)
                .reparent(mDisplayContent.getOverlayLayer(), null);
// Retrieve the size of the DisplayArea to mirror.
updateMirroredSurface(transaction, mRecordedWindowContainer.getBounds(), surfaceSize);
transaction.apply();

核心原理如下: 1、创建一个新的虚拟屏幕DisplayContent

2、对选择的app对应的WindowContainer会进行mirror图层,这样目的是为了可以在新录屏DisplayContent进行放置

3、针对app进行mirrorSurface的图层SurfaceControl,会直接挂载到录屏的DisplayContent上面

在这里插入图片描述

更多framework干货技术,关注下面↓ “千里马学框架”