七种转化RTSP屏显示到web页面的方…

7 ways to stream RTSP on the page

7 ways to stream RTSP on the page

In this article we demonstrate 7 technologically different ways to display a video stream from an IP camera with RTSP support on a web page in a browser.

As a rule, browsers do not support RTSP, so the video stream is converted for a browser using an intermediate server.

Method 1 – RTMP

Browsers do not support the RTMP protocol, but guess who does? The old faithful Flash Player that works enough well even though it does not support all browsers, so it can display the video stream.

Flash player

The code of the player is built on Action Script 3 and looks as follows:

1
2
3
var nc:NetConnection = nc.connect( "rtmp://192.168.88.59/live" ,obj);
var subscribeStream:NetStream = new NetStream(nc);
subscribeStream.play( "rtsp://192.168.88.5/live.sdp" );

In this example:

rtmp://192.168.88.59/live – is the address of the intermediate server that fetches the RTSP video stream from the camera and converts it to RTMP.

rtsp://192.168.88.5/live.sdp – is the RTSP address of the camera.

A little bit superfluous variant of the player on Flex and AS3 is available here.

This method looks as follows:

Flash streaming

Method 2 – RTMP wrapped to HTML5

It is hard to find those willing to keep coding on Action Script 3 these days. So, there is a method with an HTML wrapping that allows controlling the RTMP player from JavaScript. In this variant the flash is loaded to the HTML page only to display picture and play sound.

RTMP-player JavaScript

1
2
var session = Flashphoner.createSession({urlServer: "wss://192.168.88.59:8443" });
session.createStream({name: "rtsp://192.168.88.5/live.sdp" , display:myVideo}).play();

The full source code of the player is here. And the method looks like this:

WCS url stream volume

Method 3 – RTMFP

The RTMFP protocol also works inside the Flash Player. The difference from RTMP is that RTMFP works on top of the UDP, so it is much more suitable for low latency broadcasting.

The AS3 code of the player is identical to that of RTMP except for one letter F added in the line of code where the connection to the server is established.

1
2
3
var nc:NetConnection = nc.connect( "rtmfp://192.168.88.59/live" ,obj);
var subscribeStream:NetStream = new NetStream(nc);
subscribeStream.play( "rtsp://192.168.88.5/live.sdp" );

Nevertheless, here is a screenshot using RTMFP

Screenshot with RTMFP

Method 4 – RTMFP wrapped to HTML5

This way is identical to method 2 except that during initialization we set the RTMFP protocol for the underlying Flash (swf object).

1
2
Var session = Flashphoner.createSession({urlServer: "wss://192.168.88.59:8443" , flashProto: "rtmfp" });
session.createStream({name: "rtsp://192.168.88.5/live.sdp" , display:myVideo}).play();

Player picture:

RTMFP HTML5 - player

Method 5 – WebRTC

In this case we do not use Flash at all, and the video stream is played using means of the browser itself, without using third-party plugins. This method works both in Chrome and Firefox Android browsers, where Flash is not available. WebRTC results in the lowest latency, less than 0.5 seconds.

WebRTC Android Chrome and Android Firefox

The source code of the player is the same:

1
2
var session = Flashphoner.createSession({urlServer: "wss://192.168.88.59:8443" });
session.createStream({name: "rtsp://192.168.88.5/live.sdp" , display:myVideo}).play();

The script automatically detects WebRTC support, and if ti is supported, the stream is played using WebRTC.

Playing WebRTC

Method 6 – Websockets

WebRTC and Flash do not cover all browsers and platforms. For instance, the iOS Safari browser does not support them.

Websockets - WebRTC and Flash

You can deliver a video stream to iOS Safari using Websocket transport (a TCP connection between the browser and the server). Then, the RTSP video stream is channelled through Websockets. After the binary data are received, they can be decoded using JavaScript and rendered on Canvas HTML5 element.

This is what Websocket player does on the iOS Safari browser. The code of the player looks the same:

1
2
var session = Flashphoner.createSession({urlServer: "wss://192.168.88.59:8443" });
session.createStream({name: "rtsp://192.168.88.5/live.sdp" , display:myVideo}).play();

This is somewhat similar to the Flash-based methods when the swf element lies under HTML5. Here, we have a JavaScript application under HTML5 that fetches data via Websockets, decodes them and renders them on Canvas in multiple threads.

Here is how an RTSP stream rendered on Canvas in the iOS Safari browser looks like:

RTSP Canvas in iOS Safari

Method 7 – HLS

When RTSP is converted to HLS, a video stream is divided to segments that are happily downloaded from the server and displayed in the HLS player.

Convert RTSP to HLS

As an HLS player we use video.js. The source code of the player can be downloaded here.

The player looks as follows:

HLS-player stream

Method 8 – Android application, WebRTC

The application retrieves the stream from the server via WebRTC. To goal of the server here is to convert RTSP to WebRTC and feed the result to the mobile application.

Convert RTSP to WebRTC

The Java-code of the player for Android is here and looks like this:

1
2
3
4
5
SessionOptions sessionOptions = new SessionOptions( "wss://192.168.88.59:8443" );
Session session = Flashphoner.createSession(sessionOptions);
StreamOptions streamOptions = new StreamOptions( "rtsp://192.168.88.5/live.sdp" );
Stream playStream = session.createStream(streamOptions);
playStream.play();

A test mobile app of the player can be installed from Google Play, and the sources of the application can be downloaded from here.

Here is how RTSP stream playback via WebRTC looks on Asus Android tablet:

Playing RTSP stream via WebRTC

Method 9 – iOS application, WebRTC

Just like its Android brethren, the iOS application fetches a video stream from the server via WebRTC.

iOS app WebRTC

The Objective-C code of the player looks as shown below:

1
2
3
4
5
6
7
FPWCSApi2SessionOptions *options = [[FPWCSApi2SessionOptions alloc] init];
options.urlServer = @ "wss://192.168.88.59:8443" ;
FPWCSApi2Session *session = [FPWCSApi2 createSession:options error:&error];
FPWCSApi2StreamOptions *options = [[FPWCSApi2StreamOptions alloc] init];
options.name = @ "rtsp://192.168.88.5/live.sdp" ;
FPWCSApi2Stream *stream = [session createStream:options error: nil ];
stream play:&error;

You can download the source code of the player for iOS here.

And you can install the test application that uses the above code chunks from App Store. Operation of the player with the RTSP stream looks as follows:

App Store test app

Results

Let’s put the results together into a summary table:

  Display method Best for Latency
1 RTMP Legacy Flash, Flex or Adobe Air applications medium
2 RTMP + HTML5 IE, Edge, Mac Safari browsers if Flash Player is installed medium
3 RTMFP Legacy Flash, Flex or Adobe Air applications that require low latency low
4 RTMFP + HTML5 IE, Edge, Mac Safari browsers if Flash Player is installed and when low latency is crucial low
5 WebRTC Chrome, Firefox, Opera browsers on mobile devices and desktops on Android and when real-time playback is crucial. real-time
6 Websocket Browsers that lack support for Flash and WebRTC, but the task requires low to medium latency. medium
7 HLS Any browser as long as latency is not important. high
8 Android app, WebRTC Native mobile applications for Android that require real-time latency. real-time
9 iOS app, WebRTC Native mobile applications for iOS that require real-time latency. real-time

 

For testing the methods we used Web Call Server 5 that is capable of converting an RTSP stream and transmitting it to all nine above described directions.

 

References

Web Call Server 5 – a server to broadcast an RTSP stream.

Flash Streaming – an example swf application playing streams via RTMP and RTMFP. Corresponds to methods 1 and 3.
Source – the source code of the swf application on Flex / AS3.

Player – an example web-application that plays an RTSP stream via RTMP, RTMFP, WebRTC, Websocket. Methods 2,4,5,6.
Source – the source code of the web player.

HLS player – an example web player playing HLS. Corresponds to method 7.
Source – the source code of the HLS player.

Android WebRTC player – the example of a mobile application that plays a video stream via WebRTC. Method 8.
Source – the source code of the mobile application.

iOS WebRTC player – the example of a mobile application that plays a video stream via WebRTC. Method 9.
Source – the source code of the mobile application.

### 回答1: 将RTSP2Web集成到项目中可以实现在Web浏览器中实时播放RTSP流。首先,需要在项目中引入RTSP2Web的相关库文件和依赖项。可以通过npm包管理工具安装rtsp2web模块,并在项目的代码中引入。 接下来,需要在项目中设置RTSP2Web的配置选项。可以使用配置文件或者在代码中设置相关参数,例如指定RTSP流的地址、端口号、用户名和密码等。 在项目中创建一个视频播放页面,可以使用HTML5的video标签来显示视频画面。通过调用RTSP2Web提供的API和法,将RTSP流的视频数据传输到video标签中进行播放。 为了实现实时播放,需要使用WebSocket或者Socket.IO等实时通信技术来传输RTSP流的数据。在播放页面中建立与服务器的实时连接,并通过WebSocket将RTSP流的数据传输到前端,然后在前端通过RTSP2Web提供的法解码和播放视频流。 当RTSP流的数据不断传输过来时,可以通过RTSP2Web提供的事件回调函数来处理接收到的视频帧,并更新到视频播放页面中。可以根据需要自定义视频播放的控件和界面样式。 最后,将整合好的项目部署到服务器上进行测试。在浏览器中访问项目的视频播放页面,就可以实时播放RTSP流了。 总结起来,将RTSP2Web集成到项目中需要引入相关库文件、配置参数、创建视频播放页面、使用实时通信技术传输数据并通过事件回调函数来处理视频帧。这样就可以实现在Web浏览器中实时播放RTSP流了。 ### 回答2: 要将RTSP2Web集成到项目中,可按照以下步骤进行操作: 首先,确保项目中已经安装了所需的软件和工具。一般来说,集成RTSP2Web需要使用到服务器软件和Web开发工具。 接下来,下载并导入RTSP2Web的相关代码和库文件。RTSP2Web是一个开源项目,可以在其官网站上找到相应的代码和库文件。将其下载到项目的工作目录中。 然后,根据项目的需求和架构,对RTSP2Web进行相应的配置。根据所使用的服务器软件和Web开发工具的要求,修改RTSP2Web的配置文件,包括服务器IP地址、端口号、访问权限等。 在项目代码中添加与RTSP2Web的交互代码。根据项目的需求,编写代码调用RTSP2Web的接口,实现与摄像头设备的连接和数据传输。可以使用RTSP2Web提供的API,如获取视频流、控制云台等功能。 最后,对集成后的项目进行测试和调试。确保RTSP2Web能够正常工作,并与项目的其他功能进行兼容。可以通过访问Web界面或者其他式,验证RTSP2Web的功能和性能。 总之,将RTSP2Web集成到项目中需要进行一系列的配置和编码操作,以实现对摄像头设备的控制和数据传输。通过合理的调整和测试,可以使得RTSP2Web能够与项目完美结合,为用户带来良好的使用体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值