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

实现方案

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

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

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

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

增加依赖

使用开源库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环境下一定需要有认证设置。

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

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,通过这两个中的一个确定视频流。

R

equires 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的地址,然后选择返回的视频流块文件,即可播放此视频。

写在最后

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