Ondemand streaming with nginx-rtmp-module Secure links

映像配信

本ページは広告が含まれています。気になる広告をクリック頂けますと、サーバ運営費になります(^^

セキュアリンクを、ライブストリーミングでかける事は出来た。

https://www.techlive.tokyo/archives/1351

これを、オンデマンド配信で利用するためにはどうしたらよいのか。

今回はDebianで実現したので、php-fpmの配置がちょっと違うので注意。

https://www.techlive.tokyo/archives/1331
vod2 の位置にbbb.mp4を配置。セキュアリンクする前にちゃんとオンデマンドできているのか確認する事。

https://www.techlive.tokyo/archives/1423

再生はvideojs を利用し、さらにphpでセキュアリンク用のハッシュを計算させて、それをrtmp url に付加させることで実現している。

 

/etc/nginx/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;
        notify_method get;

        chunk_size 4000;

        # video on demand for flv files
        application vod {
            play /usr/local/nginx/flvs;
            on_play http://localhost:8080/on_play;
        }

        # video on demand for mp4 files
        application vod2 {
            play /usr/local/nginx/mp4s;
            on_play http://localhost:8080/on_play;
        }
    }
}

# HTTP can be used for accessing RTMP stats
http {
    access_log /usr/local/nginx/logs/access-streaming.log;
    error_log /usr/local/nginx/logs/error-streaming.log;

    server {
        # in case we have another web server on port 80
        listen      80;

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

location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

        listen 8080;
        server_name localhost;

        location /on_play {

            # set connection secure link
            secure_link $arg_st,$arg_e;
            secure_link_md5 mysecretkey$arg_app/$arg_name$arg_e;

            # bad hash
            if ($secure_link = "") {
                return 501;
            }

            # link expired
            if ($secure_link = "0") {
                return 502;
            }

            return 200;
        }

    }
}
vi /usr/local/nginx/html/vod.php
<html>
<head>
<link href="http://vjs.zencdn.net/5.11.6/video-js.css" rel="stylesheet">
<script src="http://vjs.zencdn.net/5.11.6/video.js"></script>
</head>
<body>
<video id="rtmp_test" class="video-js vjs-default-skin" autoplay="autoplay" controls="controls" width="800" height="450" data-setup="{}">
<?php
// 秘密鍵 になります。なんでもOKですので、ここでは日本語(UTF-8)で設定してみます。
$secret = 'mysecretkey';
// セキュアなリンク からでないとアクセスできないURIを設定します。
$path   = 'vod2/bbb.mp4';
// 公開鍵として、有効期限を time() + 秒 で設定します。
// -- この有効期限の設定の仕方は、Nginxではこの方法でしか認識できません。
$timestamp = time() + 3600; // 60(秒) x 60(分) = 3600(秒) = 1(時)

// "秘密鍵 + パス + 公開鍵" を Nginxで扱うことができる md5 で暗号化します。
$hash = base64_encode(md5($secret . $path . $timestamp, true));
// +,/,= は、URLパラメータとして扱えないので、置換します。
$hash = strtr($hash, '+/', '-_');
$hash = str_replace('=', '', $hash);

//  セキュアなリンクを出力します。
$url = "{$path}?e={$timestamp}&st={$hash}";
echo '<source src="rtmp://サーバIPアドレス/'.$url.'" type="rtmp/mp4" />'
?>

</video>
</body>
</html>

 

 

タイトルとURLをコピーしました