nginx+ffmpge+rtmp实现媒体流的直播

首先大概说一下这几个东西使用的流程。
这几个的原理就是,先用ffmpeg把媒体数据即视频或音频从传感器那里获取到,然后按照用于输入参数的要求压缩或编码成一串数据流,再把这一串数据流通过rtmp模块发给nginx,让他当做html数据发送给请求的客户,谁让他是html服务器呢。可见,rtmp只有在ffmpge和nginx之间,而nginx与用户之间不是rtmp协议,而是http协议。

首先下载nginx源码和nginx-rtmp-module的源码,放在同一个目录下面,因为编译nginx时需要使用nginx-rtmp-module的源码。
nginx的安装参考这里
nginx源码下载链接地址 官网下载链接地址
nginx-rtmp-module的源码下载页面 这里写链接内容

提醒一下,如果提示找不到OpenSSL的library,而你又能在系统中找到openssl,那多半是因为没有安装openssl-devel,使用:

sudo yum install openssl-devel

来安装。我本来是Fedora23中已经安装过了,后来在Centos7.0中安装时发现openssl已经有了,为什么还是找不到,就是因为缺少了openssl-devel。CentOS环境安装openssl-devel

注意,配置nginx的时候需要添加状态统计模块,这样方便查看客户端连接情况:

./configure --prefix=$PWD/installed --add-module=/home/bluez/work/nginx-rtmp-module --with-http_ssl_module --with-debug --with-http_stub_status_module

ffmpeg的安装我好想已经记录过了。这里

接下显示配置nginx的配置文件。


#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;
        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;
    #    }
    #}
    #rtmp stat
    #location /stat {
  #   rtmp_stat all;
      #rtmp_stat_stylesheet stat.xsl;
  # }

    #location /stat.xsl {
  #   root /home/zhangwei/work/60G/my_tools/nginx-rtmp-modules;
  # }

   location /ngx_status {
           stub_status on;
           access_log off;
       }

}

rtmp{
  server {
  listen 1935;
  #chunk_size 4096;
  application myapp {
            live on;
      }

  application htl {
            live on;
            hls on;
            hls_path /tmp/hls;
      }

    }
  }

真正有用的是最后添加的那个rtmp的数据块。
先运行nginx,我的nginx保存在的它根目录的installed/sbin/nginx中

 ./installed/sbin/nginx -c /home/zhangz/work/60G/my_tools/nginx-1.13.0/installed/conf/nginx.conf

然后运行

ffmpeg -f alsa -i default:CARD=Device -ar 44100 -ac 2 -acodec aac -strict -2  -f flv rtmp://127.0.0.1:1935/myapp/test1

注意,客户端不要使用vlc测试,使用vlc会出现卡顿的情况,我从app store上下了一个mplayer,测试正常没有卡顿。

你可能感兴趣的:(linux)