前言

好多开发者在调用Android平台RTMP推送或轻量级RTSP服务接口时,采集到的video数据类型多样化,如420sp、I420、yv12、nv21、rgb的,还有的拿到的图像是倒置的,如果开发者在上层转换后,传到底层编码处理,无疑加大了上层处理负担,而且容易因为低效率影响体验,本文以大牛直播SDK的Android平台RTMP推送SDK编码前video数据对接接口为例,看看常用的数据格式有哪些,相关资料,可参考 Github

1. Android摄像头前后camera通过OnPreviewFrame()回调的数据接口:

Android自带的camera摄像头数据对接是最基础的,需要考虑的是摄像头方向问题,比如横屏、竖屏、还有部分定制设备home键在左侧的情况,相对来说处理比较简单,直接上接口,不再赘述。

  1. @Override
  2. public void onPreviewFrame(byte[] data, Camera camera) {
  3. frameCount++;
  4. if (frameCount % 3000 == 0) {
  5. Log.i("OnPre", "gc+");
  6. System.gc();
  7. Log.i("OnPre", "gc-");
  8. }
  9. if (data == null) {
  10. Parameters params = camera.getParameters();
  11. Size size = params.getPreviewSize();
  12. int bufferSize = (((size.width | 0x1f) + 1) * size.height * ImageFormat.getBitsPerPixel(params.getPreviewFormat())) / 8;
  13. camera.addCallbackBuffer(new byte[bufferSize]);
  14. } else {
  15. if (isRTSPPublisherRunning || isPushingRtmp || isRecording || isPushingRtsp) {
  16. libPublisher.SmartPublisherOnCaptureVideoData(publisherHandle, data, data.length, currentCameraType, currentOrigentation);
  17. }
  18. camera.addCallbackBuffer(data);
  19. }
  20. }

对应接口定义:

  1. /**
  2.     * Set live video data(no encoded data).
  3.     *
  4.     * @param cameraType: CAMERA_FACING_BACK with 0, CAMERA_FACING_FRONT with 1
  5.     * 
  6.     * @param curOrg:
  7.          * PORTRAIT = 1;    //竖屏
  8.          * LANDSCAPE = 2;    //横屏 home键在右边的情况
  9.          * LANDSCAPE_LEFT_HOME_KEY = 3; //横屏 home键在左边的情况
  10.     *
  11.     * @return {0} if successful
  12.     */
  13.     public native int SmartPublisherOnCaptureVideoData(long handle, byte[] data, int len, int cameraType, int curOrg);

2. 部分定制设备,只支持YV12的数据:

一般用于第三方摄像头对接之用,第三方摄像头,以YV12和NV21居多,部分需要旋转。

  1.     /**
  2.      * YV12数据接口
  3.      *
  4.      * @param data: YV12 data
  5.      *
  6.      * @param width: 图像宽
  7.      *
  8.      * @param height: 图像高
  9.      *
  10.      * @param y_stride:  y面步长
  11.      *
  12.      * @param v_stride: v面步长
  13.      *
  14.      * @param u_stride: u面步长
  15.      *
  16.      * rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270
  17.      *
  18.      * @return {0} if successful
  19.      */
  20.     public native int SmartPublisherOnYV12Data(long handle, byte[] data, int width, int height, int y_stride,  int v_stride, int u_stride, int rotation_degree);

3. 支持NV21数据接口:

nv21数据接口,除了用于常规的camera数据接入外,部分定制摄像头出来的数据发生翻转,这个接口也支持。

  1.     /**
  2.      * NV21数据接口
  3.      *
  4.      * @param data: nv21 data
  5.      *
  6.      * @param len: data length
  7.      *
  8.      * @param width: 图像宽
  9.      *
  10.      * @param height: 图像高
  11.      *
  12.      * @param y_stride:  y面步长
  13.      *
  14.      * @param uv_stride:  uv面步长
  15.      *
  16.      * rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270
  17.      *
  18.      * @return {0} if successful
  19.      */
  20.     public native int SmartPublisherOnNV21Data(long handle, byte[] data, int len, int width, int height, int y_stride,  int uv_stride, int rotation_degree);
  21.     /**
  22.      * NV21数据接口
  23.      *
  24.      * @param data: nv21 data
  25.      *
  26.      * @param len: data length
  27.      *
  28.      * @param width: 图像宽
  29.      *
  30.      * @param height: 图像高
  31.      *
  32.      * @param y_stride:  y面步长
  33.      *
  34.      * @param uv_stride:  uv面步长
  35.      *
  36.      * rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270
  37.      *
  38.      * @param  is_vertical_flip: 是否垂直翻转, 0不翻转, 1翻转
  39.      *
  40.      * @param  is_horizontal_flip:是否水平翻转, 0不翻转, 1翻转
  41.      *
  42.      * @return {0} if successful
  43.      */
  44.     public native int SmartPublisherOnNV21DataV2(long handle, byte[] data, int len, int width, int height, int y_stride,  int uv_stride, int rotation_degree,
  45.                                                  int is_vertical_flip, int is_horizontal_flip);

4. 支持YUV数据接入:

支持标准的I420数据接口对接。

  1.     /**
  2.     * Set live video data(no encoded data).
  3.     *
  4.     * @param data: I420 data
  5.     * 
  6.     * @param len: I420 data length
  7.     * 
  8.     * @param yStride: y stride
  9.     * 
  10.     * @param uStride: u stride
  11.     * 
  12.     * @param vStride: v stride
  13.     *
  14.     * @return {0} if successful
  15.     */
  16.     public native int SmartPublisherOnCaptureVideoI420Data(long handle,  byte[] data, int len, int yStride, int uStride, int vStride);

5. 支持RGBA数据接入(支持裁剪后数据接入,主要用于同屏场景):

RGBA的主要用于屏幕共享场景下,为了便于推送屏幕部分区域,我们友好的加了裁剪参数。

  1.     /**
  2.     * Set live video data(no encoded data).
  3.     *
  4.     * @param data: RGBA data
  5.     * 
  6.     * @param rowStride: stride information
  7.     * 
  8.     * @param width: width
  9.     * 
  10.     * @param height: height
  11.     *
  12.     * @return {0} if successful
  13.     */
  14.     public native int SmartPublisherOnCaptureVideoRGBAData(long handle,  ByteBuffer data, int rowStride, int width, int height);
  15.     /**
  16.      * 投递裁剪过的RGBA数据
  17.      *
  18.      * @param data: RGBA data
  19.      *
  20.      * @param rowStride: stride information
  21.      *
  22.      * @param width: width
  23.      *
  24.      * @param height: height
  25.      *
  26.      * @param clipedLeft: 左;  clipedTop: 上; clipedwidth: 裁剪后的宽; clipedHeight: 裁剪后的高; 确保传下去裁剪后的宽、高均为偶数
  27.      *
  28.      * @return {0} if successful
  29.      */
  30.     public native int SmartPublisherOnCaptureVideoClipedRGBAData(long handle,  ByteBuffer data, int rowStride, int width, int height, int clipedLeft, int clipedTop, int clipedWidth, int clipedHeight);
  31.     /**
  32.      * Set live video data(no encoded data).
  33.      *
  34.      * @param data: ABGR flip vertical(垂直翻转) data
  35.      *
  36.      * @param rowStride: stride information
  37.      *
  38.      * @param width: width
  39.      *
  40.      * @param height: height
  41.      *
  42.      * @return {0} if successful
  43.      */
  44.     public native int SmartPublisherOnCaptureVideoABGRFlipVerticalData(long handle,  ByteBuffer data, int rowStride, int width, int height);

6. 支持RGB565数据接入(主要用于同屏场景):

同屏场景居多。

  1.     /**
  2.      * Set live video data(no encoded data).
  3.      *
  4.      * @param data: RGB565 data
  5.      *
  6.      * @param row_stride: stride information
  7.      *
  8.      * @param width: width
  9.      *
  10.      * @param height: height
  11.      *
  12.      * @return {0} if successful
  13.      */
  14.     public native int SmartPublisherOnCaptureVideoRGB565Data(long handle,ByteBuffer data, int row_stride, int width, int height);

7. 支持camera数据接入(主要用于camera2接口对接):
    为了更高效率的兼容camera2数据采集模式。

  1. /*
  2.     *  专门为android.media.Image的android.graphics.ImageFormat.YUV_420_888格式提供的接口
  3.     *
  4.     * @param  width: 必须是8的倍数
  5.     *
  6.     * @param  height: 必须是8的倍数
  7.     *
  8.     * @param  crop_left: 剪切左上角水平坐标, 一般根据android.media.Image.getCropRect() 填充
  9.     *
  10.     * @param  crop_top: 剪切左上角垂直坐标, 一般根据android.media.Image.getCropRect() 填充
  11.     *
  12.     * @param  crop_width: 必须是8的倍数, 填0将忽略这个参数, 一般根据android.media.Image.getCropRect() 填充
  13.     *
  14.     * @param  crop_height: 必须是8的倍数, 填0将忽略这个参数,一般根据android.media.Image.getCropRect() 填充
  15.     *
  16.     * @param y_plane 对应android.media.Image.Plane[0].getBuffer()
  17.     *
  18.     * @param y_row_stride 对应android.media.Image.Plane[0].getRowStride()
  19.     *
  20.     * @param u_plane 对应android.media.Image.Plane[1].getBuffer()
  21.     *
  22.     * @param v_plane 对应android.media.Image.Plane[2].getBuffer()
  23.     *
  24.     * @param uv_row_stride 对应android.media.Image.Plane[1].getRowStride()
  25.     *
  26.     * @param uv_pixel_stride 对应android.media.Image.Plane[1].getPixelStride()
  27.     *
  28.     * @param  rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270
  29.     *
  30.     * @param  is_vertical_flip: 是否垂直翻转, 0不翻转, 1翻转
  31.     *
  32.     * @param  is_horizontal_flip:是否水平翻转, 0不翻转, 1翻转
  33.     *
  34.     * @param  scale_width: 缩放宽,必须是8的倍数, 0不缩放
  35.     *
  36.     * @param  scale_height: 缩放高, 必须是8的倍数, 0不缩放
  37.     *
  38.     * @param  scale_filter_mode: 缩放质量, 范围必须是[1,3], 传0使用默认速度
  39.     *
  40.     * @return {0} if successful
  41.     */
  42.     public native int SmartPublisherOnImageYUV420888(long handle, int width, int height,
  43.                                                      int crop_left, int crop_top, int crop_width, int crop_height,
  44.                                                      ByteBuffer y_plane, int y_row_stride,
  45.                                                      ByteBuffer u_plane, ByteBuffer v_plane, int uv_row_stride, int uv_pixel_stride,
  46.                                                      int rotation_degree, int is_vertical_flip, int is_horizontal_flip,
  47.                                                      int scale_width, int scale_height, int scale_filter_mode);

总结:

以上仅是Android编码前video数据接口对接分享,感兴趣的开发者可酌情参考,由此可见,部分公司或开发者提到,一个Android平台的RTMP推送模块只要几个接口,化繁为简几乎是不可能的,除非。。

Android同屏、摄像头RTMP推送常用的数据接口设计探讨的更多相关文章

  1. EasyPusher进行Android UVC外接摄像头直播推送实现方法

    最近EasyPusher针对UVC摄像头做了适配.我们结合了UVCCamera与EasyPusher,支持将UVC摄像头的视频推送到RTSP服务器上.在此特别感谢UVCCamera这个牛逼的项目! 来 ...

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

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

  3. Windows平台摄像头或屏幕RTMP推送介绍:OBS VS SmartPublisher

    好多开发者问道,既然有了OBS,你们为什么还要开发SmartPublisher? 的确,在我们进行Windows平台RTMP推送模块开发之前,市面上为数不多的Windows平台RTMP推流工具当属OB ...

  4. 最简单的基于Flash的流媒体示例:RTMP推送和接收(ActionScript)

    ===================================================== Flash流媒体文章列表: 最简单的基于Flash的流媒体示例:RTMP推送和接收(Acti ...

  5. 解决RTMP推送时间戳问题引起HLS切片不均匀导致手机浏览器播放卡顿的问题

    本文转自EasyDarwin开源团队成员Kim的博客:http://blog.csdn.net/jinlong0603/article/details/74161115 引言 最近在测试EasyNVR ...

  6. EasyRTMP推送扩展支持HEVC(H265) RTMP推送之Metadata结构填写详解

    我们在<EasyNVR摄像机网页直播中,推流组件EasyRTMP推送RTMP扩展支持HEVC(H.265)的方案>中描述了关于EasyRTMP进行RTMP HEVC(H.265)推流的概括 ...

  7. Android 基于Netty的消息推送方案之对象的传递(四)

    在上一篇文章中<Android 基于Netty的消息推送方案之字符串的接收和发送(三)>我们介绍了Netty的字符串传递,我们知道了Netty的消息传递都是基于流,通过ChannelBuf ...

  8. Android 基于Netty的消息推送方案之字符串的接收和发送(三)

    在上一篇文章中<Android 基于Netty的消息推送方案之概念和工作原理(二)> ,我们介绍过一些关于Netty的概念和工作原理的内容,今天我们先来介绍一个叫做ChannelBuffe ...

  9. Android 基于Netty的消息推送方案之概念和工作原理(二)

    上一篇文章中我讲述了关于消息推送的方案以及一个基于Netty实现的一个简单的Hello World,为了更好的理解Hello World中的代码,今天我来讲解一下关于Netty中一些概念和工作原理的内 ...

随机推荐

  1. HMS Core新闻行业解决方案:让技术加上人文的温度

    开发者们,你希望用户如何获取新闻? 有的人靠手机弹窗知天下事,有的人则在新闻应用中尽览每一篇文章:有的人一目十行,有的人则喜欢细细咀嚼:有的人主动探索,有的人则想要应用投其所好. 科技在不断刷新着用户 ...

  2. js中通过ajax调用网上接口

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  3. 【python基础】第10回 周总结

    路径 可以简单的理解为路径就是某个事物所在的具体位置(坐标) 1.相对路径:必须有一个参考系,就是相对于自己的目标文件的位置. 2.绝对路劲:不需要有参考系,是指文件在硬盘上真正存在的路径. 计算机五 ...

  4. 【python基础】第05回 数据类型,交互,格式化输出,运算符

    上节内容回顾 1.python的注释 # 单行注释 pycharm快捷键:ctrl+? '''多行注释''' """多行注释""" 2.py ...

  5. maven编译 出现Process terminated

    问题: 解决方案: 在Settings中配置一下maven

  6. 抓包整理外篇——————autoResponder、composer 、statistics [ 三]

    前言 经过了前文的介绍的部分已经能够为自己抓包提供一个舒适的环境了,但是舒服的拿到我们的包后,可能有些需求还是难以搞定,fiddler 提供了我们一些其他模块,让我们工作轻松,请往下看. 正文 aut ...

  7. 基于 Github Actions 自动部署 Hexo 博客

    前言 前不久使用了 Hexo 搭建独立博客,我是部署在我的腾讯云轻量应用服务器上的,每次都需要 hexo deploy 然后打包.上传.解压和刷新 CDN,非常麻烦.我的服务器配置也不高 2C2G 无 ...

  8. 【cartographer_ros】六: 发布和订阅路标landmark信息

    上一节介绍了陀螺仪Imu传感数据的订阅和发布. 本节会介绍路标Landmark数据的发布和订阅.Landmark在cartographer中作为定位的修正补充,避免定位丢失. 这里着重解释一下Land ...

  9. 贪吃蛇-JavaGUI实现

    开发的大体思路 1.定义数据 2.画上面板(将数据进行初始化赋值) 3.监听事件    键盘监听    事件监听 游戏主界面代码 点击查看代码 package com.Tang.gui.snake; ...

  10. 聊一聊 C# 后台GC 到底是怎么回事?

    一:背景 写这一篇的目的主要是因为.NET领域内几本关于阐述GC方面的书,都是纯理论,所以懂得人自然懂,不懂得人也没法亲自验证,这一篇我就用 windbg + 源码 让大家眼见为实. 二:为什么要引入 ...