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之前可以想象成用管道方式连接, 如下图:

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

下面是 camera2包中的主要类:

其中 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 {

        @Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireLatestImage(); if ( image != null )
{
if ( camera2Listener != null )
{
camera2Listener.onCameraImageData(image);
} image.close();
}
}
}

    @Override
public 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.

Android平台Camera2数据如何对接RTMP推流到服务器的更多相关文章

  1. 安卓平台RTMP推流或轻量级RTSP服务(摄像头或同屏)编码前数据接入类型总结

    很多开发者在做Android平台RTMP推流或轻量级RTSP服务(摄像头或同屏)时,总感觉接口不够用,以大牛直播SDK为例 (Github) 我们来总结下,我们常规需要支持的编码前音视频数据有哪些类型 ...

  2. Android平台摄像头/屏幕/外部数据采集及RTMP推送接口设计描述

    好多开发者提到,为什么大牛直播SDK的Android平台RTMP推送接口怎么这么多?不像一些开源或者商业RTMP推送一样,就几个接口,简单明了. 不解释,以Android平台RTMP推送模块常用接口, ...

  3. 将EasyRTMP_RTSP移植到Android平台实现的RTSP拉流转推RTMP直播流功能

    本文转自EasyDarwin开源团队成员Kim的博客:http://blog.csdn.net/jinlong0603/article/details/73253044 前言 安防互联网化的需求已经越 ...

  4. Android平台RTMP/RTSP播放器开发系列--解码和绘制

    本文主要抛砖引玉,粗略介绍下Android平台RTMP/RTSP播放器中解码和绘制相关的部分(Github). 解码 提到解码,大家都知道软硬解,甚至一些公司觉得硬解码已经足够通用,慢慢抛弃软解了,如 ...

  5. Android 安卓直播开源: RTMP 推流SDK

    前些日子在github上提交了基于GPUImage的iOS直播推流SDK(https://github.com/runner365/GPUImageRtmpPush) 最近整理了Android直播推流 ...

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

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

  7. ffmpeg+EasyDSS流媒体服务器实现稳定的rtmp推流直播

    本文转自EasyDarwin团队成员Alex的博客:http://blog.csdn.net/cai6811376/article/details/74783269 需求 在做EasyDSS开发时,总 ...

  8. EasyRTMP实现的rtmp推流的基本协议流程

    EasyRTMP介绍 EasyRTMP是结合了多种音视频缓存及网络技术的一个rtmp直播推流端,包括:圆形缓冲区(circular buffer).智能丢帧.自动重连.rtmp协议等等多种技术,能够非 ...

  9. Android流媒体开发之路三:基于NDK开发Android平台RTSP播放器

    基于NDK开发Android平台RTSP播放器 最近做了不少android端的开发,有推流.播放.直播.对讲等各种应用,做了RTMP.RTSP.HTTP-FLV.自定义等各种协议,还是有不少收获和心得 ...

随机推荐

  1. iOS全埋点解决方案-APP和H5打通

    前言 ​ 所谓的 APP 和 H5 打通,是指 H5 集成 JavaScript 数据采集 SDK 后,H5 触发的事件不直接同步给服务器,而是先发给 APP 端的数据采集 SDK,经过 APP 端数 ...

  2. mysql InnoDB通过.frm和.ibd恢复表和数据

    ibdata1是一个用来构建innodb系统表空间的文件,这个文件包含了innodb表的元数据.撤销记录.修改buffer和双写buffer.如果file-per-table选项打开的话,该文件则不一 ...

  3. UiPath存在图像Image Exists的介绍和使用

    一.Image Exists的介绍 检查是否在指定的UI元素中找到图像,输出的是一个布尔值 二.Image Exists在UiPath中的使用 1. 打开设计器,在设计库中新建一个Sequence,为 ...

  4. Git代码提交报错 (Your branch is up to date with 'origin/master)

    一.前言 今天码云上提交自己本地的一个SpringBoot+Vue的小项目,由于前端代码提交第一次时候提交码云上文件夹下为空,于是自己将本地代码复制到码云拉取下来代码文件夹下,然而git add . ...

  5. 鼠标右键打开powershell

    不需要更改配置文件什么的. 在桌面空白处按住Shift键同时鼠标右击,看看是不是就有了呢.

  6. gitlab和jenkins做持续集成构建教程

    背景介绍 上一个轮回,我花了三篇文章的时间着重向大家介绍了在条件有限的情况下,如果优雅地进行前端发版和迭代.庆七一,热烈庆祝香港回归,人民生活水平越来越好,昨天上午我自掏腰包买了台服务器,决定由冷兵器 ...

  7. NC15163 逆序数

    NC15163 逆序数 题目 题目描述 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数.比如一个序列为 \ ...

  8. C# 11 的新特性和改进前瞻

    前言 .NET 7 的开发还剩下一个多月就要进入 RC,C# 11 的新特性和改进也即将敲定.在这个时间点上,不少新特性都已经实现完毕并合并入主分支 C# 11 包含的新特性和改进非常多,类型系统相比 ...

  9. Java方法的重写

    package Demo.oop.APP.Demo04; //启动器 public class application { public static void main(String[] args) ...

  10. testNG框架,使用@BeforeClass标注的代码,执行失败不抛出异常,只提示test ignore的解决方法

    郁闷了好久的一个问题,排错调试的时候是真滴麻烦... Google一圈,发现是testNG的Bug,升级testNG>=6.9.5,就能解决.