Android播放器之SurfaceView与GLSurfaceView

先看Surface

Surface的官方介绍:Handle onto a raw buffer that is being managed by the screen compositor,Surface是一个raw buffer的句柄,通过它在raw buffer上进行绘制,可以通过Surface获得一个Canvas。

Canvas canvas = mSurface.lockCanvas(null);
mSurface.unlockCanvasAndPost(canvas);

SurfaceView

SurfaceView对Surface进行了一次封装,它内部帮我们管理了一个Surface。我们使用SurfaceView其实最终都是获取到这个Surface去绘制,可参看官方解释:

Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen

The surface is Z ordered so that it is behind the window holding its SurfaceView; the SurfaceView punches a hole in its window to allow its surface to be displayed. The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full alpha-blended composite will be performed each time the Surface changes.

The transparent region that makes the surface visible is based on the layout positions in the view hierarchy. If the post-layout transform properties are used to draw a sibling view on top of the SurfaceView, the view may not be properly composited with the surface.

Access to the underlying surface is provided via the SurfaceHolder interface, which can be retrieved by calling getHolder().

The Surface will be created for you while the SurfaceView's window is visible; you should implement SurfaceHolder.Callback#surfaceCreated and SurfaceHolder.Callback#surfaceDestroyed to discover when the Surface is created and destroyed as the window is shown and hidden.

One of the purposes of this class is to provide a surface in which a secondary thread can render into the screen. If you are going to use it this way, you need to be aware of some threading semantics:

  • All SurfaceView and SurfaceHolder.Callback methods will be called from the thread running the SurfaceView's window (typically the main thread of the application). They thus need to correctly synchronize with any state that is also touched by the drawing thread.
  • You must ensure that the drawing thread only touches the underlying Surface while it is valid -- between SurfaceHolder.Callback.surfaceCreated() and SurfaceHolder.Callback.surfaceDestroyed().

绘制过程:

  • 通过SurfaceHolder.getSurface可以获取到Surface;
  • 通过Surface.lockCanvas可以获取到Surface的Canvas;
  • 使用Canvas绘制图像;
  • 使用Surface.unlockCanvasAndPost可以释放Canvas。

GLSurfaceView

GLSurfaceView继承自SurfaceView,对SurfaceView又做了一次封装,方便我们在安卓中使用OpenGL。

GLSurfaceView提供了以下特性:

  • 提供并且管理一个独立的Surface;
  • 提供并且管理一个EGL display,它能让opengl把内容渲染到上述的Surface上;
  • 支持用户自定义渲染器(Render),通过setRenderer设置一个自定义的Renderer;
  • 让渲染器在独立的GLThread线程里运作,和UI线程分离;
  • 支持按需渲染(on-demand)和连续渲染(continuous)两种模式;
  • GPU加速:GLSurfaceView的效率是SurfaceView的30倍以上,SurfaceView使用画布进行绘制,GLSurfaceView利用GPU加速提高了绘制效率;
  • View的绘制onDraw(Canvas canvas)使用Skia渲染引擎渲染,而GLSurfaceView的渲染器Renderer的onDrawFrame(GL10 gl)使用opengl绘制引擎进行渲染。

参看官方解释:

An implementation of SurfaceView that uses the dedicated surface for displaying OpenGL rendering.

A GLSurfaceView provides the following features:

  • Manages a surface, which is a special piece of memory that can be composited into the Android view system.
  • Manages an EGL display, which enables OpenGL to render into a surface.
  • Accepts a user-provided Renderer object that does the actual rendering.
  • Renders on a dedicated thread to decouple rendering performance from the UI thread.
  • Supports both on-demand and continuous rendering.
  • Optionally wraps, traces, and/or error-checks the renderer's OpenGL calls.

总结

除了上述区别外,SurfaceView通用性更好,GLSurfaceView渲染更细腻,如果想让普通的SurfaceView渲染效果更好,可以加抗锯齿效果,不过抗锯齿效果会有一定的性能消耗,硬解码设置surface模式的话,直接用普通的SurfaceView。

一般兼容性比较好的播放器,会同时支持SurfaceView和GLSurfaceView两种模式供用户根据实际场景选择,以大牛直播SDK(Github)的Android平台RTSP和RTMP播放端为例:

    /* Create rendering */private boolean CreateView() {if (sSurfaceView == null) {Log.i(TAG, "CreateView..");String manufacturer = Build.MANUFACTURER;Log.i(TAG, "CreateView, current manufacturer: " + manufacturer);if (is_enable_hardware_render_mode) {//hardware render模式,第二个参数设置为falsesSurfaceView = NTRenderer.CreateRenderer(this, false);} else {//这个字符串可以自己定义,例如判断华为就填写huawei,魅族就填写meizuif ("huawei".equalsIgnoreCase(manufacturer)) {sSurfaceView = NTRenderer.CreateRenderer(this, true);} else {/** useOpenGLES2: If with true: Check if system supports openGLES, if* supported, it will choose openGLES. If with false: it will set* with default surfaceView;*/sSurfaceView = NTRenderer.CreateRenderer(this, true);}}}if (sSurfaceView == null) {Log.i(TAG, "Create render failed..");return false;}if (is_enable_hardware_render_mode) {SurfaceHolder surfaceHolder = sSurfaceView.getHolder();if (surfaceHolder == null) {Log.e(TAG, "CreateView, surfaceHolder with null..");}surfaceHolder.addCallback(this);}return true;}

感兴趣的开发者可以参考官方文档。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/553065.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

几款知名RTMP推流模块比较:OBS VS SmartPublisher VS Flash Media Live Encoder

OBS 功能强大,几乎所有你想要的场景它都有,用起来很顺手。可以将桌面、摄像头、程序窗口通过rtmp推送到流媒体服务器上。 当然如果你是开发者,想基于OBS做二次开发,实现二次产品化的化,难度比较大,OBS代码…

中关村Win11 32位微软原版ISO V2021.08

中关村Win11 32位微软原版ISO V2021.08以微软官方原版作为母盘对系统进行了全面优化更新,支持一键化智能安装,在操作舒适性和便捷性方面进行了优化设计,流畅稳定,系统进行了全面优化,大大降低了发生蓝屏和黑屏的可能&a…

如何用轻量级RTSP服务本地生成RTSP测试URL

最近发现好多开发者都在搜索可用的RTSP测试URL,目前公网实际可测试的RTSP URL非常少,即便是可用,分辨率和网络也非常差,不适合长期测试。 针对此,我们的建议是最好直接网上买个海康或大华的摄像头,一般来说…

Windows平台RTSP|RTMP播放端SDK集成说明

2.1 demo说明 大牛直播SDK提供C/C#两套接口,对外提供32/64位debug/release库,C和C#接口一一对应,C#接口比C接口增加前缀NT_PB_;WIN-PlayerSDK-CPP-Demo:播放端SDK对应的C接口的demo;WIN-PlayerSDK-CSharp-…

微软为Win11用户更新了剪贴工具、计算器以及邮件和日历应用

微软今天宣布为Windows 11的几个预装应用程序推出第一次更新,以下应用程序的更新正在向开发渠道的Windows Insiders推出。微软正在将经典的Snipping Tool和Snip & Sketch合并为一个截屏应用程序,而计算器、邮件和日历都将以新的视觉风格以配合Window…

Windows平台RTMP直播推送集成简要说明

好多开发者在集成大牛直播SDK (官方)的Windows平台RTMP推送模块时吓一跳,怎么这么多接口?本文做个简单的拆分: 初始化 初始化之前,如需设置日志路径,调用NTSmartLog.NT_SL_SetPath(log_path);…

如何实现Android端获取RTSP|RTMP流转推RTMP

技术背景 最近不少开发者找到我们,他们在做智能家居等传统行业时,希望实现在Android板件拉取本地的RTSP或RTMP流,然后对外推送RTMP出去,亦或内部启个轻量级RTSP服务,提供个对外对接的媒介URL,简单来说&…

foxmail地址簿怎么添加分组 foxmail地址簿新建分组的教程

foxmail有很多联系人,想要合理管理联系人,该怎么创建分组呢?下面我们就来看看详细的教程。 1、在进行打开地址簿之后,进行点击选中一个文件夹,然后进行点击"新建组" foxmail地址簿怎么添加分组? foxmail地址簿新建分…

Android平台RTSP轻量级服务|RTMP推送摄像头或屏幕之音频接口设计

好多开发者在做Android平台录像或者RTSP轻量级服务、RTMP推送相关模块时,对需要设计哪些常用接口会心存疑惑,本文主要以大牛直播SDK(官方)为例,简单介绍下Android平台直播推送SDK所有音频相关的接口,感兴趣…

Win7系统更换软件图标的详细方法

Win7系统怎么更换软件图标?相信许多用户会安装很多软件或游戏,并且安装这些软件会在桌面上生成图标,但是一些用户认为某些软件图标不好,因此他们想替换这些图标,但他们不知道怎样做,这里小编就和大家分享Win7系统更换…

QT实现低延迟的RTSP、RTMP播放器

好多开发者在QT环境下实现RTMP或RTSP播放时,首先考虑到的是集成VLC,集成后,却发现VLC在延迟、断网重连、稳定性等各个方面不尽人意,无法满足上线环境需求。本文以调用大牛直播SDK(官方)的Windows平台播放端…

Android对接实现内网无纸化会议|智慧教室|实时同屏功能

背景 本文主要讲的是基于Android平台实现RTMP的技术方案设计,基础架构图如下: 组网注意事项 1. 组网:无线组网,需要好的AP模块才能撑得住大的并发流量,推送端到AP,最好是有线网链接; 2. 服务…

bilibili里的硬币有什么用 硬币的作用详细介绍

很多bilibili中的用户发现自己的账户中拥有很多的硬币,却不知道这些硬币有什么用,下面小编就为大家带来硬币的作用介绍。 bilibili里的硬币有什么用 硬币的作用详细介绍 1、视频评分。向喜欢的视频投硬币以示鼓励(一个视频收到的硬币多了就会被推荐) …

Windows平台RTMP推送|轻量级RTSP服务实现本地摄像头|屏幕|叠加数据预览

背景 大家在做Windows平台RTMP推送或轻量级RTSP服务的时候,不管是采集屏幕还是采集摄像头,亦或屏幕摄像头的叠加模式,总会有这样的诉求,采集到的数据,希望能本地看看具体采集的数据或者图像实际效果,也就是…

Outlook2016怎么修改默认数据库

Outlook2016中想要修改默认数据库,该怎么修改呢?下面我们就来看看详细的教程。 Outlook2016怎么修改默认数据库? 1、下载安装outlook软件。 Outlook2016怎么修改默认数据库? 2、双击打开outlook软件。 Outlook2016怎么修改默认数据库? 3、点击“文件”——…

Windows平台RTMP|RTSP播放器为什么要兼容GDI绘制

为什么要支持GDI 先说结论,Windows平台播放渲染这块,一般来说99%以上的机器都是支持D3D的,实现GDI模式绘制,除了为了好的兼容性外,在远程连接的场景下,D3D创建不成功,需要使用GDI模式。 简单来…

Windows平台RTMP|RTSP播放器实现画面全屏功能

我们在Windows平台实现RTSP或者RTMP播放的时候,有个功能是绕不开的,那就是播放窗口全屏。本文就以大牛直播SDK(官方)的Windows播放器为例,大概讲下大概实现: 全屏播放需要考虑的点不多: 第一&…

tim怎么设置检测到新版本自动安装 tim安全自动更新的开启方法

TIM想要开启自动检测新版本并下载安装,该怎么设置呢?下面我们就来看看详细的教程。 1、首先,在你的 电脑中找到TIM; tim怎么设置检测到新版本自动安装?tim安全自动更新的开启方法 2、打开TIM,登录你的个人账号,登陆之后&…

Android平台实现Unity3D下RTMP推送

像Unity3D下的RTMP或RTSP播放器一样,好多开发者苦于在Unity环境下,如何高效率低延迟的把数据采集并编码实时推送到流媒体服务器,实现Unity场景下的低延迟推拉流方案。 关于屏幕采集,有两种方案: 1. 直接封装Android原…

Windows平台实现Unity下窗体|摄像头|屏幕采集推送

技术背景 随着Unity3D的应用范围越来越广,越来越多的行业开始基于Unity3D开发产品,如传统行业中虚拟仿真教育、航空工业、室内设计、城市规划、工业仿真等领域。 基于此,好多开发者苦于在Unity环境下,没有低延迟的推拉流解决方案…