android javacv直播,无插件即时视频播放

基于项目的需求,因为在我们实际的应用场景中,每个楼宇至少有100个摄像头,如果接入50个楼宇的话,就会有5000多个摄像头,如果是后端将所有的摄像头数据全部转换的话,会消耗大量的CPU和网络IO,而且也会产生很多的临时的m3u8文件,因此性能上一个很大的瓶颈。基于此,因此,需要设计一种即时查看IP Camera的方式,当用户浏览时,服务器进行RTSP流到HLS的转换,当不浏览时,即停止转换。在我们的场景中,当鼠标用户点击摄像头图标时,即开始浏览,当用户关闭浏览窗口时,则停止转换。

实现方案

因此为了实现这个问题,必须首先解决如何优雅的启动和停止Ffmpeg的视频流转换的问题,然后再把它包装成restful service即可以满足需求,先给出两种实现方案。

02bb1f61714a

方案一、基于javacv实现视频流的转换

JavaCV首先提供了计算机视觉领域研究人员常用的函数库的封装:包括OpenCV, FFmpeg, libdc1394, PGRFlyCapture, OpenKinect, videoInput, 和 ARToolKitPlus库,其中FFmpeg库的封装我们可以用来进行视频的转换。详细的文章可以参考

https://blog.csdn.net/eguid_1/article/details/52678775

视频的转换比较简单,不考虑音频的转换。

增加依赖

使用开源库https://github.com/bytedeco/javacv的javacv库, 首先引入此库。

org.bytedeco

javacv-platform

1.5.3

视频流转码

javacv的使用实例可以参考网站

https://www.codota.com/code/java/classes/org.bytedeco.javacv.FFmpegFrameGrabber

,里面有很多实例可以参考,下面的代码片段是一个简单的实例,实现转码。/**

* Record video frame by frame

*

* @Param inputFile- The address can be webcast / taped address, or a remote / local file path

* @param outputFile

* - This address is only an address file, if you use this method to push the streaming media server will complain, because there is no set encoding format

* @throws FrameGrabber.Exception

* @throws FrameRecorder.Exception

* @throws org.bytedeco.javacv.FrameRecorder.Exception     */

public static void frameRecord(String inputFile, String outputFile, int audioChannel)

throws Exception, org.bytedeco.javacv.FrameRecorder.Exception {

boolean start = true; // this variable is recommended to set a global control variable for controlling the recording ends

// Get video source

FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputFile);

// streaming output address resolution (length, height), whether the recorded audio (0: not record / 1: record)

FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputFile, 1280, 720, audioChannel);

// start taking video source

recordByFrame(grabber, recorder, isStart);

}

private static void recordByFrame(FFmpegFrameGrabber grabber, FFmpegFrameRecorder recorder, Boolean status)

throws Exception, org.bytedeco.javacv.FrameRecorder.Exception {

try {

// This method is recommended in thread

grabber.start();

recorder.start();

Frame frame = null;

while (status&& (frame = grabber.grabFrame()) != null) {

recorder.record(frame);

}

recorder.stop();

grabber.stop();

} finally {

if (grabber != null) {

grabber.stop();

}

}

}

测试转码public static void main(String[] args)

throws FrameRecorder.Exception, FrameGrabber.Exception, InterruptedException {

String inputFile = "rtsp://admin:!honeywell1@192.168.100.55:554/cam/realmonitor?channel=1&subtype=0";

// Decodes-encodes

String outputFile = "recorde.mp4";

frameRecord(inputFile, outputFile,1);

}

方案二、基于开源rtsp-stream实现视频流的转换

rtsp-stream是一个易于使用,为已有的系统提供在浏览器中播放原始的RTSP流的现成解决方案。它提供了视频流的启动,停止转码的方法以及获取视频列表的服务,并提供了用户认证和授权功能。github网站:

https://github.com/Roverr/rtsp-stream

API 列表

它提供的服务如下:/start - 启动流转换

/stream/{id} - 获取视频流

/list - 获取视频流列表/stop - 停止流转换

认证

rtsp-stream为每个service提供了无认证和JWT认证两种方式,可以跟存在的系统中进行认证服务对接。默认是无认证方式,对于是私有网络,用户无法在internet下访问到此服务,相对安全,可以采用无认证方式,如果在internet环境下一定需要有认证设置。

02bb1f61714a

认证相关的环境变量设置如下:

Env variableDescriptionDefaultTypeRTSP_STREAM_AUTH_JWT_ENABLEDIndicates if the service should use the JWT authentication for the requestsfalsebooleanRTPS_STREAM_AUTH_JWT_SECRETThe secret used for creating the JWT tokensmacilacistringRTSP_STREAM_AUTH_JWT_PUB_PATHPath to the public shared RSA key./key.pubstringRTSP_STREAM_AUTH_JWT_METHODCan be secret or rsa. Changes how the application does the JWT verification.secretstring

API介绍

POST /start

启动对视频流进行转换。请求参数:RTSP uri和alias。其中alias是可选参数。Requires payload:{

"uri": "rtsp://username:password@host",

"alias": "camera1"

}

Response:{

"uri": "/stream/id/index.m3u8",

"running": true,

"id": "id",

"alias": "camera1"

}

GET /stream/{id}/*file视频流的HLS的块文件。浏览器会基于给定m3u8文件获取流的块文件。比如,http://localhost:8080/stream/bd761e69-af21-4e70-9fed-7f2f9032fa61/index.m3u8

http://localhost:8080/stream/bd761e69-af21-4e70-9fed-7f2f9032fa61/1654.ts

http://localhost:8080/stream/bd761e69-af21-4e70-9fed-7f2f9032fa61/1655.ts

http://localhost:8080/stream/bd761e69-af21-4e70-9fed-7f2f9032fa61/1656.ts

GET /list

获取当前的视频列表。Response:[

{

"running": true,

"uri": "/stream/9f4fa8eb-98c0-4ef6-9b89-b115d13bb192/index.m3u8",

"id": "9f4fa8eb-98c0-4ef6-9b89-b115d13bb192",

"alias": "9f4fa8eb-98c0-4ef6-9b89-b115d13bb192"

},

{

"running": false,

"uri": "/stream/camera1/index.m3u8",

"id": "8ab9a89c-8271-4c89-97b7-c91372f4c1b0",

"alias": "camera1"

}

]

POST /stop从视频流列表中停止或者移动此视频流。可以传入streamid或者alias,通过这两个中的一个确定视频流。

Requires payload:{

"id": "40b1cc1b-bf19-4b07-8359-e934e7222109",

"alias": "camera1",

"remove": true, // optional - indicates if stream should be removed as well from list or not

"wait": false // optional - indicates if the call should wait for the stream to stop

}

ResponseEmpty 200

Empty 404

测试

使用roverr/rtsp-stream:2-management来测试此功能,这个组件里包含了一个测试UI,8080端口是视频流转换服务端口,80是前端页面端口.docker run -p 80:80 -p 8080:8080 roverr/rtsp-stream:2-management

打开测试网址http://localhost,增加RSTSP的地址,然后选择返回的视频流块文件,即可播放此视频。

02bb1f61714a

写在最后

综合这两种方案,都能实现视频的即时播放功能,第一种方案需仍然需要使用javacv库来实现视频流的转换服务,第二种方案已经提供了成熟的接口,因此,更加适合我们的项目需求,性能测试比之前的方案都有很大的提高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一: 使用javacv来实现,最终也是用过ffmpeg来进行编码和推流,javacv实现到可以直接接收摄像头的帧数据 需要自己实现的代码只是打开摄像头,写一个SurfaceView进行预览,然后实现PreviewCallback将摄像头每一帧的数据交给javacv即可 javacv地址:https://github.com/bytedeco/javacv demo地址:https://github.com/beautifulSoup/RtmpRecoder/tree/master 二: 使用Android自带的编码工具,可实现硬编码,这里有一个国内大神开源的封装很完善的的库yasea,第一种方法需要实现的Camera采集部分也一起封装好了,进行一些简单配置就可以实现编码推流,并且yasea目前已经直接支持摄像头的热切换,和各种滤镜效果 yasea地址(内置demo):https://github.com/begeekmyfriend/yasea 服务器 流媒体服务器我用的是srs,项目地址:https://github.com/ossrs/srs 关于srs的编译、配置、部署、在官方wiki中已经写的很详细了,并且srs同样是国内开发人员开源的项目,有全中文的文档,看起来很方便 这里有最基本的简单编译部署过程 Android直播实现(二)srs流媒体服务器部署 播放android端的播放使用vitamio,还是国内的开源播放器,是不是感觉国内的前辈们越来越屌了^~^! vitamio支持几乎所有常见的的视频格式和流媒体协议 vitamio地址(内置demo):https://github.com/yixia/VitamioBundle 这里使用的是yaesa库,先介绍一下直播实现的流程:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值