PHP写在线视频直播技术详解

本文转自:https://www.cnblogs.com/zx-admin/p/5697447.html

废话一句,如果你要做高性能服务器服务,请去读底层的东西 http tcp/ip socket 了解基础协议,你对如何建造高性能服务器会有一个深度的了解

目前楼主专注php开发,最直接的方法就是使用lnmp去直接做,搜索以下资料,发现还是行得通的,先把基础架构列出来

前端页面 php  

弹幕flash+js 数据来源是redis集群  

及时聊天 redis集群 +js长连接

礼物系统 

在线调用ffmpeg对流媒体进行转码

服务器流媒体 nginx-rtmp-module  的支持

基于HTTP的FLV/MP4 VOD点播
HLS (HTTP Live Streaming) M3U8的支持
基于http的操作(发布、播放、录制)
可以很好的协同现有的流媒体服务器以及播放器一起工作
在线调用ffmpeg对流媒体进行转码
H264/AAC音视频编码格式的支持

服务器端的并发和负载是很大的问题,在兼顾服务器数量+宽带的,后面在详解

视频加速 cdn

支持的pc 安卓 ios需要优化 因为 nginx-rtmp也是支持HLS 

视频源支持 obs 目前这个是pc开源的比较好的

安卓或者ios目前没有发现比较好的开源视频源推送的,欢迎推荐

背景和资料

目前流行的流媒体服务器

http://www.oschina.net/project/tag/111/streaming

进行综合对比,发现就PHP作为开发语言来说,nginx肯定是比较方便,因为大部分需要的东西都可以直接配置而且配置也不是很麻烦

又花了很多时间去搜索性能对比,发现nginx-rtmp的性能还是不错的,而且后期做负载和水平扩展,都是很方便的

开发文档

https://github.com/arut/nginx-rtmp-module/wiki/Directives

http://blog.csdn.net/defonds/article/details/9274479/

http://blog.csdn.net/cccallen/article/details/8440191/

所以最后决定是nginx-rtmp作为流媒体服务器,数据库上5.6+因为5.6+的版本把主从的日志复制变成了多线程复制,性能更好,配置更方便

http://www.ttlsa.com/mysql/summary-of-the-new-features-of-mysql5_6/

建议上5.7 QPS 更强,最新版,因为是全新项目,编译 的时候可以把 memory存储引擎加上,在测试服在测试比较方便

 5.7版本新特性说明

http://www.oschina.net/translate/whats-new-in-mysql-5-7-generally-available?cmp&p=4

作为需要高并发的网站,建议最好上php7,因为官方鸟哥早就说了,性能增加30%左右,如果你需要超高并发,请上golang,就并发来说,php的资源消耗是很大的

php的并发扩展来说,多进程,虽然也有多线程,但是7以上的版本的扩展不知道是否已经更新,这个也是个问题,如果你了解go语言就会发现go在并发方面做得很好

协程比多线程的资源消耗更小,而且nginx也是支持go的,但是性能怎么样,目前没有测试过 

http://blog.csdn.net/win_lin/article/details/41379799

内存数据库目前大部分都是redis集群,我也是选的这个

整体下来作为一个php来说,这样的开发成本是最低的

 目前基于php7的框架目前没有,所示自己开发一个小的耦合性低,方便可以扩展的标准mvc框架,模仿CI,目前3.X系列没有说明支持php7

据说CI 4 .x版本会支持

http://codeigniter.org.cn/forums/thread-22318-1-1.html

网站开发测试

 测试服搭建

vmware

服务器 centos 6.5 和 centos 7.2

php7.1 mysql 5.7 nginx 1.9

组件基本都是最新版


user nginx;
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 10240;
}

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;


rtmp {
server {
listen 1935;

application mytv {  //千万不能使用myapp作为名字 ,不知道为什么就是始终无法播放 访问路径是rtmp://ip/mytv/ 可以使用vlc播放器播放,作为测试
live on;
}


application hls {
live on;
hls on;
hls_path /usr/local/nginx/hls;  //这个地址最好和
hls_fragment 5s; 
}
}
}

http {
include mime.types;
default_type application/octet-stream;


sendfile on;

access_log on; 

access_log /usr/local/nginx/html/rtmp_access.log;


server {
listen 8081;
server_name localhost 192.168.1.70 127.0.0.1;


location /hls {

types {
application/vnd.apple.mpegurl m3u8;            请注意是加上on_public url/x.php 在这个php文件做权限和id对应播放房间的判断,是根据传入的id进行生产id.m3u8文件的
video/mp2t ts;                                 访问url是http://ip/id.m3u8
}
root /usr/local/nginx/hls;

}

}

server {

listen 8080;
location /stat {

rtmp_stat all;

rtmp_stat_stylesheet stat.xsl;

}

location /stat.xsl {

root /usr/local/nginx/html/; #在nginx-rtmp-module源码根目录   查看当前服务器推流情况的xsl统计

}

}

server {

listen 8082;
location / {

root /usr/local/nginx/html/;

}
}

server {

listen 80;
location / {

root /usr/local/nginx/html/;

}
}
}


user nginx;
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 10240;
}

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;


rtmp {
server {
listen 1935;

application mytv {  //千万不能使用myapp作为名字 ,不知道为什么就是始终无法播放 访问路径是rtmp://ip/mytv/ 可以使用vlc播放器播放,作为测试
live on;
}


application hls {
live on;
hls on;
hls_path /usr/local/nginx/hls;  //这个地址最好和
hls_fragment 5s; 
}
}
}

http {
include mime.types;
default_type application/octet-stream;


sendfile on;

access_log on; 

access_log /usr/local/nginx/html/rtmp_access.log;


server {
listen 8081;
server_name localhost 192.168.1.70 127.0.0.1;


location /hls {

types {
application/vnd.apple.mpegurl m3u8;            请注意是加上on_public url/x.php 在这个php文件做权限和id对应播放房间的判断,是根据传入的id进行生产id.m3u8文件的
video/mp2t ts;                                 访问url是http://ip/id.m3u8
}
root /usr/local/nginx/hls;

}

}

server {

listen 8080;
location /stat {

rtmp_stat all;

rtmp_stat_stylesheet stat.xsl;

}

location /stat.xsl {

root /usr/local/nginx/html/; #在nginx-rtmp-module源码根目录   查看当前服务器推流情况的xsl统计

}

}

server {

listen 8082;
location / {

root /usr/local/nginx/html/;

}
}

server {

listen 80;
location / {

root /usr/local/nginx/html/;

}
}
}

  网页测试播放器hls可以直接使用h5直接播放

rtmp可以使用 ckplayer的flash进行播放,安卓手机端目前没有测试,因为手机uc默认是没有flash,但是使用hls就可以,因为ios默认就是支持的

obs 推流地址 :rtmp://ip/mytv/ 播放也是这个 

这个配置文件会再次更新,仅供参考

./configure \
--prefix=/usr/local/nginx \
--add-module=/usr/local/src/nginx-rtmp-module-master \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/

这个编译其实已经把hls包含进去了
复制代码

 2016年8月16日16:46:20  今天下午测试,使用腾讯云作为服务器端的,rtmp测试效果延迟6-10s,本地串流到服务器,hls还没有测试

还有一个问题就是腾讯云的安全组,你需要把你的机器加入开放所有端口,默认只开放80端口,可以直接IP访问

复制代码
user  nginx;
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  10240;
}

   rtmp_auto_push on;
   rtmp_auto_push_reconnect 1s;


rtmp {
    server {
      listen 1935;

      application mytv {
           live on;
      }

         
   application hls {
            live on;
            hls on;
            hls_path /usr/local/nginx/hls;
            hls_fragment 5s; 
        }
   }
}

http {
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;

    access_log on;   
          
    access_log /usr/local/nginx/html/rtmp_access.log;


    server {
        listen       8081;
        server_name  localhost 192.168.1.170 127.0.0.1;


        location /hls {
            
            types {
               # application/vnd.apple.mpegurl m3u8;
            application/x-mpegurl m3u8;
                video/mp2t ts;
            }
            alias /usr/local/nginx/hls;
            
        }
    

    }



    server {

       listen      8080;
       location /stat {

       rtmp_stat all;

       rtmp_stat_stylesheet stat.xsl;

       }

 

       location /stat.xsl {

           root /usr/local/nginx/html/;  #在nginx-rtmp-module源码根目录

       }

   }





 server {

       listen      80;
       server_name  localhost 192.168.1.170 127.0.0.1;
       location / {

         root /usr/local/nginx/html/;
         index index.php;
       }

         location ~ \.php$ {
            root           /usr/local/nginx/html/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }


   }
}

 播放地址 http://192.168.1.170:8081/hls/.m3u8

实际开发就会根据用户ID.m3u8 来播放,所以需要一些nginx的知识,推流地址  rtmp://192.168.1.170/hls/  在加密的串流码 里面带上用户ID,是否是大主播,等等的标识,来分流服务器,pull push cdn等

所以nginx和nginx-rtmp的详细知识很重要QQ群 247823727 博客文件如果不能下载请进群下载 

发表评论

电子邮件地址不会被公开。 必填项已用*标注