Android平台Camera2数据如何对接RTMP推流到服务器

1. Camera2架构

在Google 推出Android 5.0的时候, Android Camera API 版本升级到了API2(android.hardware.camera2), 之前使用的API1(android.hardware.camera)就被标为 Deprecated 了。

Camera API2相较于API1有很大不同, 并且API2是为了配合HAL3进行使用的, API2有很多API1不支持的特性, 比如:

  1. 更先进的API架构;
  2. 可以获取更多的帧(预览/拍照)信息以及手动控制每一帧的参数;
  3. 对Camera的控制更加完全(比如支持调整focus distance, 剪裁预览/拍照图片);
  4. 支持更多图片格式(yuv/raw)以及高速连拍等。

在API架构方面, Camera2和之前的Camera有很大区别, APP和底层Camera之前可以想象成用管道方式连接, 如下图:

fig.1

这里引用了管道的概念将安卓设备和摄像头之间联通起来,系统向摄像头发送 Capture 请求,而摄像头会返回 CameraMetadata。这一切建立在一个叫作 CameraCaptureSession 的会话中。

下面是 camera2包中的主要类:

fig.2

其中 CameraManager 是那个站在高处统管所有摄像投设备(CameraDevice)的管理者,而每个 CameraDevice 自己会负责建立 CameraCaptureSession 以及建立 CaptureRequest。

CameraCharacteristics 是 CameraDevice 的属性描述类,非要做个对比的话,那么它与原来的 CameraInfo 有相似性。

CameraManager处于顶层管理位置负责检测获取所有摄像头及其特性和传入指定的CameraDevice.StateCallback回调打开指定摄像头,CameraDevice是负责管理抽象对象,包括监听Camera 的状态回调CameraDevice.StateCallback、创建CameraCaptureSession和CameraRequest,CameraCaptureSession用于描述一次图像捕获操作,主要负责监听自己会话的状态回调CameraCaptureSession.StateCallback和CameraCaptureSession.CaptureCallback捕获回调,还有发送处理CameraRequest;CameraRequest则可以看成是一个"JavaBean"的作用用于描述希望什么样的配置来处理这次请求;最后三个回调用于监听对应的状态。

2. 官方解释

The android.hardware.camera2 package provides an interface to individual camera devices connected to an Android device. It replaces the deprecated Camera class.

This package models a camera device as a pipeline, which takes in input requests for capturing a single frame, captures the single image per the request, and then outputs one capture result metadata packet, plus a set of output image buffers for the request. The requests are processed in-order, and multiple requests can be in flight at once. Since the camera device is a pipeline with multiple stages, having multiple requests in flight is required to maintain full framerate on most Android devices.

To enumerate, query, and open available camera devices, obtain a CameraManager instance.

Individual CameraDevices provide a set of static property information that describes the hardware device and the available settings and output parameters for the device. This information is provided through the CameraCharacteristics object, and is available through getCameraCharacteristics(String)

To capture or stream images from a camera device, the application must first create a camera capture session with a set of output Surfaces for use with the camera device, with createCaptureSession(SessionConfiguration). Each Surface has to be pre-configured with an appropriate size and format (if applicable) to match the sizes and formats available from the camera device. A target Surface can be obtained from a variety of classes, including SurfaceViewSurfaceTexture via Surface(SurfaceTexture)MediaCodecMediaRecorderAllocation, and ImageReader.

Generally, camera preview images are sent to SurfaceView or TextureView (via its SurfaceTexture). Capture of JPEG images or RAW buffers for DngCreator can be done with ImageReader with the JPEG and RAW_SENSOR formats. Application-driven processing of camera data in RenderScript, OpenGL ES, or directly in managed or native code is best done through Allocation with a YUV TypeSurfaceTexture, and ImageReader with a YUV_420_888 format, respectively.

The application then needs to construct a CaptureRequest, which defines all the capture parameters needed by a camera device to capture a single image. The request also lists which of the configured output Surfaces should be used as targets for this capture. The CameraDevice has a factory method for creating a request builder for a given use case, which is optimized for the Android device the application is running on.

Once the request has been set up, it can be handed to the active capture session either for a one-shot capture or for an endlessly repeating use. Both methods also have a variant that accepts a list of requests to use as a burst capture / repeating burst. Repeating requests have a lower priority than captures, so a request submitted through capture() while there's a repeating request configured will be captured before any new instances of the currently repeating (burst) capture will begin capture.

After processing a request, the camera device will produce a TotalCaptureResult object, which contains information about the state of the camera device at time of capture, and the final settings used. These may vary somewhat from the request, if rounding or resolving contradictory parameters was necessary. The camera device will also send a frame of image data into each of the output Surfaces included in the request. These are produced asynchronously relative to the output CaptureResult, sometimes substantially later.

3. Camera2 API调用基础流程:

  1. 通过context.getSystemService(Context.CAMERA_SERVICE) 获取CameraManager;
  2. 调用CameraManager .open()方法在回调中得到CameraDevice;
  3. 通过CameraDevice.createCaptureSession() 在回调中获取CameraCaptureSession;
  4. 构建CaptureRequest, 有三种模式可选 预览/拍照/录像.;
  5. 通过 CameraCaptureSession发送CaptureRequest, capture表示只发一次请求, setRepeatingRequest表示不断发送请求;
  6. 拍照数据可以在ImageReader.OnImageAvailableListener回调中获取, CaptureCallback中则可获取拍照实际的参数和Camera当前状态。

4. 如何实现camera2数据对接RTMP推送:

通过OnImageAvailableListenerImpl 获取到原始数据,推送端以大牛直播SDK https://github.com/daniulive/SmarterStreaming/ 的万能推送接口为例,获取数据后,调用SmartPublisherOnImageYUV420888() 完成数据传送,底层进行二次处理后,编码后传输即可。

接口描述:

	/**  专门为android.media.Image的android.graphics.ImageFormat.YUV_420_888格式提供的接口** @param  width: 必须是8的倍数** @param  height: 必须是8的倍数** @param  crop_left: 剪切左上角水平坐标, 一般根据android.media.Image.getCropRect() 填充** @param  crop_top: 剪切左上角垂直坐标, 一般根据android.media.Image.getCropRect() 填充** @param  crop_width: 必须是8的倍数, 填0将忽略这个参数, 一般根据android.media.Image.getCropRect() 填充** @param  crop_height: 必须是8的倍数, 填0将忽略这个参数,一般根据android.media.Image.getCropRect() 填充** @param y_plane 对应android.media.Image.Plane[0].getBuffer()** @param y_row_stride 对应android.media.Image.Plane[0].getRowStride()** @param u_plane 对应android.media.Image.Plane[1].getBuffer()** @param v_plane 对应android.media.Image.Plane[2].getBuffer()** @param uv_row_stride 对应android.media.Image.Plane[1].getRowStride()** @param uv_pixel_stride 对应android.media.Image.Plane[1].getPixelStride()** @param  rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270** @param  is_vertical_flip: 是否垂直翻转, 0不翻转, 1翻转** @param  is_horizontal_flip:是否水平翻转, 0不翻转, 1翻转** @param  scale_width: 缩放宽,必须是8的倍数, 0不缩放** @param  scale_height: 缩放高, 必须是8的倍数, 0不缩放** @param  scale_filter_mode: 缩放质量, 范围必须是[1,3], 传0使用默认速度** @return {0} if successful*/public native int SmartPublisherOnImageYUV420888(long handle, int width, int height,int crop_left, int crop_top, int crop_width, int crop_height,ByteBuffer y_plane, int y_row_stride,ByteBuffer u_plane, ByteBuffer v_plane, int uv_row_stride, int uv_pixel_stride,int rotation_degree, int is_vertical_flip, int is_horizontal_flip,int scale_width, int scale_height, int scale_filter_mode);
    private class OnImageAvailableListenerImpl implements ImageReader.OnImageAvailableListener {@Overridepublic void onImageAvailable(ImageReader reader) {Image image = reader.acquireLatestImage();if ( image != null ){if ( camera2Listener != null ){camera2Listener.onCameraImageData(image);}image.close();}}}
    @Overridepublic void onCameraImageData(Image image) {synchronized(this){Rect crop_rect = image.getCropRect();if(isPushingRtmp || isRTSPPublisherRunning) {if(libPublisher != null){Image.Plane[] planes = image.getPlanes();//  crop_rect.left, crop_rect.top, crop_rect.width(), crop_rect.height(),//  这里缩放宽高可以填0,使用原视视频宽高都可以的libPublisher. SmartPublisherOnImageYUV420888(publisherHandle, image.getWidth(), image.getHeight(),crop_rect.left, crop_rect.top, crop_rect.width(), crop_rect.height(),planes[0].getBuffer(), planes[0].getRowStride(),planes[1].getBuffer(), planes[2].getBuffer(), planes[1].getRowStride(), planes[1].getPixelStride(),displayOrientation, 0, 0,videoWidth, videoHeight, 1);}}}}

5. Camera2对焦API扩展说明

关于CONTROL_AF_MODE描述:

当前是否开启自动对焦,以及设置它的模式。
它只有在 android.control.mode = AUTO 和镜头没有固定焦距(i.e android.lens.info.minimumFocusDistance > 0)的情况下,才有用。
当aeMode 为 OFF时,AF的行为取决了设备。

建议在将android.control.aeMode设置为OFF之前使用android.control.afTrigger锁定AF,或者在AE关闭时将AF模式设置为OFF。
它的值有:

  1. OFF:自动对焦程序不再控制镜头;foucusDistance 由application控制。
  2. AUTO:基本自动对焦模式。在这个模式中,镜头不会移动,除非 autofocus trigger 被触发。当 trigger是activated的时候,AF的状态将转换为ACTIVE_SCAN,然后出 scan的结果(FOCUSED or NOT_FOCUSED) 如果镜头没有固定焦距,所有设备都支持。
  3. MACRO:特写聚焦模式。在这个模式中,镜头不回移动,除非autofocus trigger 的行为被调用。 当 trigger 被触发后,AF的状态将转换为ACTIVE_SCAN,然后出扫描结果(FOCUSED or NOT_FOCUSED)。这个模式对那些离镜头很近的物体的对焦进行优化。也就是微距。
  4. CONTINUOUS_VIDEO:在该模式中,AF算法连续地修改镜头位置以尝试提供恒定对焦的图像流,缺点是对焦过程中焦点的移动较慢。
    The focusing behavior should be suitable for good quality video recording; typically this means slower focus movement and no overshoots. When the AF trigger is not involved, the AF algorithm should start in INACTIVE state, and then transition into PASSIVE_SCAN and PASSIVE_FOCUSED states as appropriate. When the AF trigger is activated, the algorithm should immediately transition into AF_FOCUSED or AF_NOT_FOCUSED as appropriate, and lock the lens position until a cancel AF trigger is received.
    一旦收到取消,算法应转换回INACTIVE并恢复被动扫描。 请注意,此行为与CONTINUOUS_PICTURE不同,因为必须立即取消正在进行的PASSIVE_SCAN。

  5. CONTINUOUS_PICTURE:在该模式中,AF算法连续地修改镜头位置以尝试提供恒定对焦的图像流,对焦的过程尽可能的快,建议使用。
    The focusing behavior should be suitable for still image capture; typically this means focusing as fast as possible. When the AF trigger is not involved, the AF algorithm should start in INACTIVE state, and then transition into PASSIVE_SCAN and PASSIVE_FOCUSED states as appropriate as it attempts to maintain focus. When the AF trigger is activated, the algorithm should finish its PASSIVE_SCAN if active, and then transition into AF_FOCUSED or AF_NOT_FOCUSED as appropriate, and lock the lens position until a cancel AF trigger is received.

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

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

相关文章

钉钉一个人怎么多部门 钉钉一个人加入多个部门的技巧

公司的组织机构总是会出现少几个人员,一个人身兼多职的情况,这对于我们设计审核流程的人来说是很多痛的一件事,会写好几种的条件判断,一有人离职了就得重新调整流程。如果一个人可以同时加入多个部门,这样这个问题就很…

IE浏览器下如何低延迟播放RTSP或RTMP流

首先,虽然本文是介绍IE浏览器下OCX控件播放RTSP或RTMP,但这种方式并不推荐,毕竟它只能用于IE浏览器环境下,局限太大,而且随着微软IE浏览器的更新,不确定后续支持情况。当然,话说回来&#xff0c…

Android、iOS平台RTMP/RTSP播放器实现实时音量调节

介绍移动端RTMP、RTSP播放器实时音量调节之前,我们之前也写过,为什么windows播放端加这样的接口,windows端播放器在多窗口大屏显示的场景下尤其需要,尽管我们老早就有了实时静音接口,相对实时静音来说,播放…

千牛如何装修店铺 千牛装修店铺的教程

1、首先登录千牛卖家版,输入淘宝账号和密码。 2、在界面的横向工作栏中选择店铺管理,找到【店铺装修】选项。 千牛如何装修店铺?千牛装修店铺的教程 3、在左侧工具栏中选择需要修改的部分,目前可选择模块、配色、页头、页面、CSS等部分。…

Windows平台RTMP播放器/RTSP播放器如何在播放窗口添加OSD文字叠加

好多开发者在做Windows平台特别是单屏多画面显示时,希望像监控摄像机一样,可以在播放画面添加OSD台标,以实现字符叠加效果,大多开发者可很轻松的实现以上效果,针对此,本文以大牛直播SDK (Github…

6款真正好用的播放器推荐

GOM player GOM player 是一款本身装有视频播放所需的解码,及占用系统资源少,并且能以最优秀的画质来观看多种格式影片的播放程序。 可以支持播放大多数当前流行的视频格式,如:MP4、AVI、WMV、MKV、MOV、FLV 等),并且…

win7系统电脑运行速度的提升方法

win7系统是一款被大多数用户们认可的好用系统,在不断的对win7系统的使用中很多用户们都在寻找win7提高电脑运行速度的方法,今天小编就为大家带来了win7系统电脑运行速度的提升方法,让我们一起来看一下吧。 win7系统电脑运行速度的提升方法 …

Windows平台Unity3d下如何同时播放多路RTSP或RTMP流

好多开发者在做AR、VR或者教育类产品时,苦于如何在windows平台构建一个稳定且低延迟的RTSP或者RTMP播放器,如果基于Unity3d完全重新开发一个播放器,代价大、而且周期长,不适合快速出产品,我们认为当前最好的方式就是集…

如何修改Win11睡眠时间

近日,微软发布了 Win11 的首个预览版,所有之前已加入到 Insider Preview 的用户都能收到这个版本。该版本融入了发布会里的很多变化,但并未添加 Microsoft Teams 和安卓应用支持。相信很多朋友已经安装体验了一把。大家知道为了节省电量&…

Android平台使用Camera2(5.0+)替代过时的Camera

转自:https://forums.developer.amazon.com/articles/2707/using-camera2-to-replace-deprecated-camera-api.html From Android 5.0(API Level 21) the new Camera2 API(android.hardware.Camera2) is introduced which now gives full manual control over Andro…

RTMP播放器开发填坑之道

好多开发者提到,在目前开源播放器如此泛滥的情况下,为什么还需要做自研框架的RTMP播放器,自研和开源播放器,到底好在哪些方面?以下大概聊聊我们的一点经验,感兴趣的,可以关注 github&#xff1a…

Win11系统语言修改不了中文怎么办

一些升级了Win11系统的朋友发现升级后发现是英文版的,怎么把英文版的换成中文版的呢?下面为大家带来如何把Win11系统语言从英文变成中文,方法非常简单。 Win11系统语言修改不了中文怎么办 1、首先打开区域和语言设置(搜索),看看是否有“显…

Vscode —— 解决Vscode终端无法使用npm的命令的问题

在cmd中可以正常执行npm -v等指令,但是在vs code终端中,无法执行npm -v,node -v等指令 出现报错 解决办法👇 方法一:【右键单击Vscode】以【管理员身份运行】,【重启Vscode】 方法二:①【用户变量】的【path】添加npm所在路径的…

RTSP播放器开发填坑之道

好多开发者提到,在目前开源播放器如此泛滥的情况下,为什么还需要做自研框架的RTSP播放器,自研和开源播放器,到底好在哪些方面?以下大概聊聊我们的一点经验,感兴趣的,可以关注 github&#xff1a…

ijkplayer、VLC Player、SmartPlayer、ExoPlayer播放器比较

ijkPlayer ijkPlayer是BiliBili公司维护的一个开源工程,基于ffmpeg开发的一个播放器软件,支持Android和iOS平台,整个ijkplayer就是以ffplay为基础,如果只是使用它进行播放,集成也较为简单,使用也和MediaPl…

Win11系统设置绿色护眼模式的方法

随着Win11系统电脑的逐步推广,Win11系统在市场上的用户量也是越来越大,不少电脑办公群体也是很青睐于使用Win11系统,然而,长时间的办公容易造成视觉疲劳,为此,很多人希望把电脑的显示颜色改成”护眼绿“&am…

浏览器播放RTSP视频流几种解决方案

方案一: H5 websocket_rtsp_proxy 实现视频流直播 Streamedian 提供了一种“html5_rtsp_player websock_rtsp_proxy”的技术方案,可以通过html5的video标签直接播放RTSP的视频流。 整个架构如下图所示,分为服务器端和浏览器端两部分: 方案…

技术员联盟Win11 64位官方全新旗舰版镜像V2021.08

技术员联盟Win11 64位官方全新旗舰版镜像V2021.08以微软官方纯净版作为母盘对系统进行了全面优化更新,用户使用更加流畅顺手,轻松体验到系统的优秀性能,适用目前市场最新机型以及老旧机型,多种安装方式供用户选择,且更…

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 mS…

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

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