LoginSignup
4
9

More than 5 years have passed since last update.

nginx + RTMPモジュールによるライブストリーミング(統計とロギング)

Posted at

スクリーンショット 2016-05-04 16.20.08.png

nginx + RTMPモジュールによるライブストリーミングは事例が多く、実験的に配信する所までは辿り着くことができます。
しかし、実際に不特定多数に公開する前には、いくつか把握しておいたほうが良いことがあります。

  1. きちんと動作しているのか、動かない場合には、どのエラーログを見ればよいのか?
  2. 統計情報を確認することはできるのか?
  3. どのくらいのアクセスまで捌くことができるのか?

ここでは、1,2に関して記載します。
(3は準備中)

環境

  • CentOS 6.7
  • nginx 1.9.15
    • nginx-rtmp-module

インストール

  • rtmpモジュールを組み込んだnginxのインストール
# yum install -y pcre zlib openssl
# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.9.15.tar.gz
# tar xvf nginx-1.9.15.tar.gz

# wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
# unzip master.zip 

# cd nginx-1.9.15
# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --with-http_ssl_module --add-module=../nginx-rtmp-module-master --with-debug 
# make
# make install
# nginx -v
  • ドキュメントルート ログディレクトリの作成(ここら辺は、好きな所へ……)
# mkdir /home/www
# mkdir /home/logs
  • 設定ファイルの配置(nginx.conf)
# mkdir /etc/nginx
# cp /usr/local/src/nginx-1.9.15/conf/nginx.conf /etc/nginx/
  • 設定ファイルの編集(nginx.conf)
# vi /etc/nginx/nginx.conf 
/etc/nginx/nginx.conf
# デバッグ用のログ取得(warn,notice,info,debugのレベルがある.infoぐらいのレベルが丁度良い)
error_log  /home/logs/error.log  info;

http {
    # http(プロトコル用)アクセスログ
    access_log  /home/logs/access.log  main;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /home/www;
            index  index.html index.htm;
        }

        # 統計情報表示設定
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet /stat.xsl;
        }

        location /stat.xsl {
            root /home/www/stats;
        }
    }
}

# rtmpモジュールの設定ブロックを追加
rtmp {
   server {
      listen 1935;
      chunk_size 4096;

      # rtmp(プロトコル用)アクセスログ
      access_log  /home/logs/rtmp.access.log combined

      application live {
         live on;
         record off;
      }
   }
}

# vi /etc/init.d/nginx
# chmod 755 /etc/init.d/nginx
# /etc/init.d/nginx configtest 
# /etc/init.d/nginx start

配信準備

  • 再生用ページ
index.html
<!DOCTYPE html>
<html lang="en" class="">
<head>
  <link href="http://vjs.zencdn.net/4.2.0/video-js.css" rel="stylesheet">
  <script src="http://vjs.zencdn.net/4.2.0/video.js"></script>
</head>
<body>
  <video id="rtmp live test" class="video-js vjs-default-skin" controls
   preload="auto" width="640" height="264" data-setup='{}'>
    <source src="rtmp://***.***.***.***/live/test" type='rtmp/mp4'>
    <p class="vjs-no-js">
      To view this video please enable JavaScript, and consider upgrading to a web browser
      that <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>
    </p>
  </video>
</body>
</html>

ログの確認

アクセスログ

  • ログフォーマット
    • PUBLISH: 配信用デバイス(PC)から配信が開始された
    • PLAY: ブラウザからアクセスされ配信が始まった
rtmp.access.log
***.***.***.*** [04/May/2016:16:04:38 +0900] PUBLISH "live" "test" "" - 45828392 529 "" "FMLE/3.0 (compatible; obs-studi" (2m 18s)
***.***.***.*** [04/May/2016:15:10:22 +0900] PLAY "live" "test" "" - 627 54573547 "http://***.***.***.***/" "MAC 21,0,0,216" (2m 46s)

エラーログ

  • 配信開始(client connected -> connect -> createStream -> publish)
error.log
[info] 1572#0: *2 client connected '***.***.***.**'
[info] 1572#0: *2 connect: app='live' args='' flashver='FMLE/3.0 (compatible; obs-studi' swf_url='rtmp://***.***.***.***/live' tc_url='rtmp://***.***.***.***/live' page_url='' acodecs=0 vcodecs=0 object_encoding=0, client: ***.***.***.***, server: 0.0.0.0:1935
[info] 1572#0: *2 createStream, client: ***.***.***.***, server: 0.0.0.0:1935
[info] 1572#0: *2 publish: name='test' args='' type=live silent=0, client: ***.***.***.***, server: 0.0.0.0:1935
  • 配信終了(deleteStream -> disconnect -> deleteStream)
    • deleteStreamが二回ある謎。配信開始のclient connectedに対応する破棄という意味なのか……?
error.log
[info] 1572#0: *2 deleteStream, client: ***.***.***.***, server: 0.0.0.0:1935
[info] 1572#0: *2 disconnect, client: ***.***.***.***, server: 0.0.0.0:1935
[info] 1572#0: *2 deleteStream, client: ***.***.***.***, server: 0.0.0.0:1935
  • 再生(client connected -> connect -> createStream -> play)
error.log
[info] 1572#0: *6 client connected '***.***.***.***'
[info] 1572#0: *6 connect: app='live' args='' flashver='MAC 21,0,0,216' swf_url='http://vjs.zencdn.net/4.2/video-js.swf' tc_url='rtmp://192.168.100.102/live/' page_url='http://bash:error.log/' acodecs=3575 vcodecs=252 object_encoding=3, client: ***.***.***.***, server: 0.0.0.0:1935
[info] 1572#0: *6 createStream, client: ***.***.***.***, server: 0.0.0.0:1935
[info] 1572#0: *6 play: name='test' args='' start=0 duration=0 reset=0 silent=0, client: ***.***.***.***, server: 0.0.0.0:1935
  • 一時停止、再開

    • infoレベルのログには出力されない
  • ブラウザを閉じる、再読み込み

    • ストリーミングの場合、停止操作がないので、ブラウザを閉じた場合や、同じクライアントから再アクセスがあった場合に、コネクションを破棄していると思われる
error.log
[info] 1572#0: *6 disconnect, client: ***.***.***.***, server: 0.0.0.0:1935
[info] 1572#0: *6 deleteStream, client: ***.***.***.***, server: 0.0.0.0:1935

統計情報の表示

  • 統計用テンプレートの配置
# cp /usr/local/src/nginx-rtmp-module-master/stat.xsl /home/www/stats/
  • 統計画面へアクセス
    • http://.../stats/
    • 前述の統計画面が表示される
    • アクセス制限は、必要に応じて

その他

用語

  • RTMP(RealTimeMessageProtocol)
    • 音声:64バイト, 動画:128バイト
    • Flashで再生
  • HLS(HTTP Live Streaming)
    • iOS/Androidで再生
  • OBS(OpenBroadcastSoftware)
    • 配信用ソフト(カメラとマイクが付いているPC/Macであればよい)

configureオプション

モジュール名 用途
ngx_http_ssl_module SSLサポート --with-http_ssl_module
nginx-rtmp-module-master rtmp拡張モジュール --add-module=../nginx-rtmp-module-master
-- rtmp拡張モジュールのデバッグモード --with-debug

参照

4
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
9