使用pingos实现在线直播
使用pingos实现在线直播
(一)Pingos
PingOS依赖NGINX构建,并且继承arut和AlexWoo的nginx-rtmp-module模块。修复arut和AlexWoo版本存在的部分问题外,PingOS在编码和直播协议以及其他方面做了多项功能扩展。
1.服务器功能
1.直播协议: RTMP、HTTP(S)-FLV、HTTP(S)-TS、HLS(支持HTTPS)、HLS+(支持HTTPS)、DASH(支持HTTPS)。
2.音视频编码: H264、H265、MP3、AAC。
3.直播录像: FLV文件格式和TS文件格式。
4.GOP缓存: 实现秒开和内存复用。
5.application支持通配符: “ * ”号通配符实现自动匹配推拉流时使用的application名字,无需累赘的配置。
6.VHOST功能: 支持配置多个server域名。
7.控制台接口: 通过HTTP API接口控制推流、拉流以及录像过程。
8.配置动态加载: 修改配置文件后无需对nginx做任何操作就可读取最新配置。
9.流量计费: 通过配置自定义流量日志。
10变量参数配置: 配置文件中使用变量。
11.进程间回源: 进程间相互拉流,解决了原生nginx-rtmp-module模块多进程拉流失败的问题。
12.集群化功能: 服务器间推拉流功能(http-flv、rtmp协议)。
13.html5网页播放器: pingos-player播放器将持续兼容各浏览器平台,以及多种直播协议。
2.快速安装
# 快速安装
git clone https://github.com/pingostack/pingos.git
cd pingos
./release.sh -i
# 启动服务
cd /usr/local/pingos/
./sbin/nginx
3.操作说明
推流
推流地址:rtmp://ip/live/流名
播放地址
rtmp 播放:rtmp://ip/live/流名
http(s)-flv 播放:http(s)://ip/flv/流名
hls 播放:http(s)://ip/hls/流名.m3u8
hls+ 播放:http(s)://ip/hls2/流名.m3u8
http(s)-ts 播放:http(s)://ip/ts/流名
直播流监控后台
访问地址:http://ip/rtmp_stat 通过该页面可以查看当前正在发生的推流和播放记录。
html5播放器
访问地址: http://ip/h5player/flv 这个播放器是基于flv.js的网页播放器,可以实现无插件播放http-flv直播流。 一旦你能够访问这个页面,说明你的直播服务器已经成功搭建起来了。
(二)Pingos的配置引导
1.配置结构
rtmp { server { server_name pingos.io; listen 1935; ... application * { live on; ... } ... } ... }
http { server { location / { ... } ... } ... }
|
2.配置约束
1. 位置约束
对于不同的配置项,配置所在的位置也不同: 例如,端口配置 “listen 1935;” 只允许在server下出现,而rtmp直播开关 “live on;” 则可以在rtmp层 server层 和application层出现。
2. 参数数量约束
不同的配置项对配置参数的数量有不同的要求,可能有些配置项不需要后跟参数,而有些需要后跟1个或者多个参数。
3. 参数类型约束
block类型,例如 “rtmp” “server” “application” 这种需要后跟 “{ }” 配置块的类型。
dir类型,后跟系统路径,例如录像文件的存放位置 “live_record_path /data/record;”
time类型,例如缓存时长的配置 “cache_time 3s;”
固定选项的类型,有些配置的参数是固定的几个选项。
复合类型,有些配置的参数比较复杂,可能需要很多的参数,并且参数类型不同,后文会详细介绍。
3.配置模板
使用这个配置模板可以满足你对单台服务器直播的需求。(已经开启多进程)
推流地址
rtmp://ip/live/流名
播放地址
rtmp rtmp://ip/live/流名
http-flv http://ip/flv/流名
hls http://ip/hls/流名.m3u8
hls+ http://ip/hls2/流名.m3u8
http-ts http://ip/ts/流名
(三)OBS安装与使用
1.OBS的安装
在obs的官网https://obsproject.com下载对应系统版本的obs安装包进行安装。
2.obs的使用
窗口采集只对某个窗口进行采集。
显示器采集对整个显示器中的显示内容采集。
音频输入采集将计算机中的声音进行采集。
视频采集设备通过摄像头采集图像,例如对直播人物进行采集。
需要注意的是最好使画布大小与输出分辨率大小一致。
3.如何降低直播延时
降低延时的方法可以参考https://pingos.io/docs/zh/qa-lowlatency中给出的方法降低延时。
4.使用obs进行推流
在obs的设置中设置推流服务器地址以及串流密匙。
服务器即配置的nginx推流服务器。密匙可以理解为直播房间号码。
5.使用VCL进行播放
在VCL中配置播放源地址。
根据下面配置可以知道推流地址配置了两个一个为rtmp://ip:端口号/live/流名,另外一个为rtmp;//ip:端口号/hls/流名。播放两个直播的地址分别为rtmp://ip:端口号/live/流名和http:///ip:端口号/hls/流名/index.m3u8。
直接使用live推流进行播放时,会出现画面卡顿的现象,具体原因目前暂不清楚。在这里推荐使用hls。
附录
使用这个配置模板可以满足你对单台服务器直播的需求。
#user nobody; worker_processes 1;
#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;
#pid logs/nginx.pid;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on;
server { listen 80; server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { root html; index index.html index.htm; }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #}
# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
# another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias;
# location / { # root html; # index index.html index.htm; # } #}
# HTTPS server # #server { # listen 443 ssl; # server_name localhost;
# ssl_certificate cert.pem; # ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on;
# location / { # root html; # index index.html index.htm; # } #} # server { listen 8080;
location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /usr/local/nginx/nginx-rtmp-module/; }
location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; }
autoindex on; alias /usr/share/nginx/html/hls;
expires -1; add_header Cache-Control no-cache;
add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } } }
rtmp { server { listen 1935; chunk_size 4000; application vod { play /usr/share/nginx/html/vod/flvs/; } application live { live on; } application hls { live on; hls on; hls_path /usr/share/nginx/html/hls; hls_fragment 5s; hls_playlist_length 15s; hls_continuous on; hls_cleanup on; hls_nested on; } } }
|
本文来自博客园,作者:jkbolck,转载请注明原文链接:https://www.cnblogs.com/lwjblogs/p/15875513.html