Real Time Messaging Protocol(简称 RTMP)是 Macromedia 开发的一套视频直播协议,现在属于 Adobe。这套方案需要搭建专门的 RTMP 流媒体服务如 Adobe Media Server,并且在浏览器中只能使用 Flash 实现播放器。它的实时性非常好,延迟很小,但无法支持移动端 WEB 播放是它的硬伤。
虽然无法在iOS的H5页面播放,但是对于iOS原生应用是可以自己写解码去解析的, RTMP 延迟低、实时性较好。浏览器端,HTML5 video
标签无法播放 RTMP 协议的视频,可以通过 video.js 来实现。
<link href=“http://vjs.zencdn.net/5.8.8/video-js.css” rel=“stylesheet”>
<video id=“example_video_1″ class=“video-js vjs-default-skin” controls preload=“auto” width=“640” height=“264” loop=“loop” webkit-playsinline>
<source src=“rtmp://10.14.221.17:1935/rtmplive/home” type=‘rtmp/flv’>
</video>
<script src=“http://vjs.zencdn.net/5.8.8/video.js”></script>
<script>
videojs.options.flash.swf = ‘video.swf’;
videojs(‘example_video_1′).ready(function() {
this.play();
});
</script>
一、服务端
1. 下载nginx-rtmp-module模块
地址:https://github.com/arut/nginx-rtmp-module
2. 编译安装nginx
cd ./nginx-1.10.0/
./configure --add-module=/path/to/nginx-rtmp-module --with-http_ssl_module
make
make install
注:根据实际情况修改两处代码路径
3. 配置nginx
3.1 加入节点
rtmp {
server {
listen 1935;
application mytv {
live on;
}
}
}
注:mytv是应用名称
3.2 RTMP监听状态(可选)
http {
server {
listen 8080;
# This URL provides RTMP statistics in XML
location /stat {
rtmp_stat all;
# Use this stylesheet to view XML as web page
# in browser
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# XML stylesheet to view RTMP stats.
# Copy stat.xsl wherever you want
# and put the full directory path here
root /data/wwwroot/rtmp/;
}
}
}
把nginx-rtmp-module文件夹中的stat.xsl复制到/data/wwwroot/rtmp/文件夹中
监控地址:http://localhost:8080/stat
二、推流端
1. 下载OBS
地址:https://obsproject.com/download
2. 配置OBS
推流地址:rtmp://192.168.240.128/mytv/ #记得修改IP,下同
其他配置参考:http://www.douyu.com/cms/zhibo/201311/13/250.shtml
3. 开始推流
此时可以监听到【publishing】状态的进程
注:记得开放1935端口
三、接收端
1. 下载jwplayer
地址:https://dashboard.jwplayer.com/#/players/downloads #需要注册登录
2. html示例
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="my_video"></div>
</body>
</html>
<script src="./jwplayer/jwplayer.js"></script>
<script>
jwplayer.key = "yourkey";
jwplayer('my_video').setup({
file: 'rtmp://192.168.240.128/mytv/',
});
</script>
注:记得修改key
四、验证推流
1. rtmp配置参考
地址:https://github.com/arut/nginx-rtmp-module/wiki/Directives#on_publish
rtmp {
server {
listen 1935;
on_publish http://localhost/rtmp/auth.php;
application mytv {
live on;
}
}
}
注:记得重启nginx
2. auth.php
<?php
if ($_POST['key'] == 'root') {
header('HTTP/1.1 200 OK');
header('Status: 200 OK');
} else {
header('HTTP/1.1 403 Forbidden');
header('Status: 403 Forbidden');
}
返回2xx RTMP通过,返回3xx RTMP重定向,其他均为失败。
请根据你的业务逻辑修改验证流程。
3.OBS配置
流秘钥:?key=root #不是推流地址