您的位置:首页 > 移动开发 > Android开发

EasyPusher Android实时推送当前屏幕画面

2016-09-24 16:02 344 查看
由于Android 5.0提供了捕获当前屏幕画面的SDK,基于该SDK,EasyPusher实现了实时推送手机屏幕画面的功能。经测试,效果很不错,延迟也比较低,画面也比较流畅。该功能可运用到小型会议PPT演示、手游直播等行业。

具体来说,MediaProjection 类可以将当前屏幕画面采集到一个surface里面,而MediaCodec可以从一个surface里面获取视频数据源。我们让MediaProjection投射到MediaCodec创建的Surface,MediaCodec就可以获取到MediaProjection投射的视频了。如图所示:

Created with Raphaël 2.1.0MediaProjectionMediaProjectionSurfaceSurfaceMediaCodecMediaCodecEasyPusherEasyPusher投射屏幕提供视频源编码编码数据通过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即可。


createInputSurface

Surface createInputSurface ()
Requests a Surface to use as the input to an encoder, in place of input buffers.

该接口创建一个作为编码器输入的Surface。


然后,将后者得到的Surface作为第6个参数传给前者,前者就可以获取到屏幕数据了~

如下图所示,在VLC访问RTSP地址,即可看到屏幕直播。



EasyPusher项目见:https://github.com/EasyDarwin/EasyPusher
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: