1 流媒体服务器nginx搭建:
#nginx的rtmp模块源码
git clone GitHub - arut/nginx-rtmp-module: NGINX-based Media Streaming Server
#nginx的依赖pcre源码
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
nginx安装脚本:
auto/configure --prefix=/usr/local/nginx \
--with-pcre=../pcre-8.39 \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_flv_module \
--with-http_mp4_module \
--add-module=../nginx-rtmp-module/
必须添加nginx-rtmp-module模块.
2 nginx配置文件修改:
以下是ngxin.conf文件内容
#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 8080; # 80端口修改为8080
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
root /home/azheng/www; # 添加的web服务器默认地址
index index.html index.htm; # 要访问的web服务器文件,测试web播放视频流用
}
#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;
# }
#}
}
# 添加rtmp服务器配置信息
rtmp {
server {
listen 8081; # 监听的端口
chunk_size 4000;
application live { # rtmp推流请求路径
live on; # 开启实时
hls on; # 开启hls
hls_path /home/azheng/live; # 文件存放路径
hls_fragment 5s; # 每个TS文件包含5秒的视频内容
}
}
}
3 启动nginx服务器
sudo /usr/local/nginx/sbin/nginx # 启动nginx服务器
sudo /usr/local/nginx/sbin/nginx -s reload # 重新加载配置文件
sudo /usr/local/nginx/sbin/nginx -s stop # 停止nginx服务器
4 ffmpeg转换rtsp视频流并推流rtmp至服务器
ffmpeg安装必须先编译安装H264和其他视各种频编码器, 编译过程, 此处不赘述.
ffmpeg推流命令:
ffmpeg -i "rtsp://admin:admin12345@192.168.0.64:554/h264/ch33/main/av_stream" -vcodec copy -acodec copy -f flv -y rtmp://localhost:8081/live/room
注意: 格式和地址
VLC播放器测试拉流和推流数据无法获取到视频, 可能和编码器有关. 测试RTSP和RTMP视频流程序在testVideo目录.
推流可以使用ffmpeg的api去做。
5 web播放rtmp视频流或者发起通话
端口: 8866
web播放程序: web目录中index.html
在index.html中source标签中添加rtmp视频流地址.
web发起通话:
在index.html中rtmp标签中添加rtmp添加要和通话的机器人的rtmp地址.
注意:
浏览器要启用FLash.
本地打开index.html, 无法播放.
web播放代码必须放在服务器上, 通过浏览器访问才能正常播放.
谷歌浏览器, IE浏览器测试OK.
6 播放web的通话
#因为arm64无法解决falsh plugin的问题,通过web方式无法实现,改用7的方法
端口: 8080
访问入口: web目录中的index0.html
当收到通话命令后, 机器人启动浏览器(Google Chrome不能是最新70版本的, 最新版本取消了"允许"flash的设置),
设置"http://localhost:8080/"为主页, 启动后自动播放web的通话.
当web断开通话, 再重新建立通话, 播放正常.
谷歌浏览器, IE浏览器测试OK.
7 播放rtmp语音
使用ffplay播放rtmp视频流中的语音数据:
ffplay "rtmp-address -1" nodisp
注意:
ffmpeg需要添加ffplay的依赖后,重新编译才会生成ffplay.
测试播放正常.