EasyPusher实现Android手机屏幕桌面直播,实时推送操作画面,用于手游直播等应用
2023-09-14 08:59:59 时间
本文转自EasyDarwin开源团队成员John的博客:http://blog.csdn.net/jyt0551/article/details/52651194
由于Android 5.0提供了捕获当前屏幕画面的SDK,基于该SDK,EasyPusher实现了实时推送手机屏幕画面的功能。经测试,效果很不错,延迟也比较低,画面也比较流畅。该功能可运用到小型会议PPT演示、手游直播等行业。
具体来说,MediaProjection 类可以将当前屏幕画面采集到一个surface里面,而MediaCodec可以从一个surface里面获取视频数据源。我们让MediaProjection投射到MediaCodec创建的Surface,MediaCodec就可以获取到MediaProjection投射的视频了。如图所示:
MediaProjectionMediaProjectionSurfaceSurfaceMediaCodecMediaCodecEasyPusherEasyPusher投射屏幕提供视频源编码编码数据通过Pusher推送
在这里就不再详细描述代码的实现,主要介绍下两个接口:
VirtualDisplay createVirtualDisplay (String name,
int width,
int height,
int dpi,
int flags,
Surface surface,
VirtualDisplay.Callback callback,
Handler handler)
Creates a VirtualDisplay to capture the contents of the screen.
Parameters
name String: The name of the virtual display, must be non-empty.
要创建的投射器的名称,非空
width int: The width of the virtual display in pixels. Must be greater than 0.
投射后视频的宽度,这里的宽度就是实际上后面MediaCodec初始化的宽度.
height int: The height of the virtual display in pixels. Must be greater than 0.
投射后视频的高度,这里的宽度就是实际上后面MediaCodec初始化的高度.
dpi int: The density of the virtual display in dpi. Must be greater than 0.
投射器的像素密度,未理解啥意思,我们直接用DisplayMetrics的densityDpi即可.
flags int: A combination of virtual display flags. See DisplayManager for the full list of flags.
我们传
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR|DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC|DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION 即可。
surface Surface: The surface to which the content of the virtual display should be rendered, or null if there is none initially.
投射器要投射到的Surface
callback VirtualDisplay.Callback: Callback to call when the virtual display's state changes, or null if none.
投射器更改后的状态回调,我们这里不需要,传null即可。
handler Handler: The Handler on which the callback should be invoked, or null if the callback should be invoked on the calling thread's main Looper.
回调函数将在该Handler所在的线程调用,我们也不需要,传null即可。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
createInputSurface
Surface createInputSurface ()
Requests a Surface to use as the input to an encoder, in place of input buffers.
该接口创建一个作为编码器输入的Surface。
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
然后,将后者得到的Surface作为第6个参数传给前者,前者就可以获取到屏幕数据了~
如下图所示,在VLC访问RTSP地址,即可看到屏幕直播。
EasyPusher项目见:https://github.com/EasyDarwin/EasyPusher
获取更多信息
Copyright © EasyDarwin.org 2012-2016
相关文章
- Android 图形架构
- android移动点餐系统内容和要求,基于Android云计算的移动点餐系统
- strictmode android,Android 应用性能优化-StrictMode(严格模式)
- android okio使用方法,Android 开源框架 Okio 原理剖析「建议收藏」
- Android 屏幕适配之框架(AndroidAutoSize)(今日头条)适配
- android进阶之了解Android系统与开机过程
- android退出app代码,Android应用退出代码各种方式
- Android App程序退出 黑屏问题
- android declare-styleable 和style,Android 关于declare-styleable属性的写法….
- android短信验证码方案,Android之短信验证码
- 【Android布局】在程序中设置android gravity 和 android layout Gravity属性
- android跳转到相册需要权限,Android打开相册获取图片路径[通俗易懂]
- android.support.v4.app.Fragment$InstantiationException问题解决
- 【Android RTMP】NV21 图像旋转处理 ( 快速搭建 RTMP 服务器 Shell 脚本 | 创建 RTMP 服务器镜像 | 浏览器观看直播 | 前置 / 后置摄像头图像旋转效果展示 )
- 【错误记录】Android Studio 4.2.1 编译报错 ( Kotlin 版本推荐设置 1.5.0 )
- 【错误记录】Android 模拟器安装应用报错 ( INSTALL_FAILED_INSUFFICIENT_STORAGE )
- 【ijkplayer】编译 Android 版本的 ijkplayer ② ( 切换到 k0.8.8 分支 | 执行 init-android.sh 脚本进行初始化操作 )
- 【Android UI】贝塞尔曲线 ⑤ ( 德卡斯特里奥算法 | 贝塞尔曲线递推公式 )
- 【错误记录】Android Studio 编译报错 ( Could not resolve com.android.tools.build:gradle:7.4.2. )
- Android-SQLite数据库操作详解手机开发
- [android] 轮播图-无限循环详解手机开发
- Android热修复Tinker接入实战详解手机开发
- Android中的ProgressBar的android:indeterminate详解手机开发
- 小米能通过后门在你的 Android 手机上静默安装任何应用
- Android系统自带样式(android:theme)
- ANDROID完美退出APP的实例代码