nginx.conf中关于nginx-rtmp-module配置指令详解

/ nginx / 没有评论 / 1694浏览

nginx.conf中关于nginx-rtmp-module配置指令详解

译序:截至 Jul 8th,2013 官方公布的最新 Nginx RTMP 模块 nginx-rtmp-module 指令详解。

指令

Core

rtmp

server

rtmp {
    server {
    }
}

listen

server {
    listen 1935;
}

application

server {
    listen 1935;
    application myapp {
    }
}

timeout

timeout 60s;

ping

ping 3m;
ping_timeout 30s;

ping_timeout

max_streams

max_streams 32;

ack_window

ack_window 5000000;

chunk_size

chunk_size 4096;

max_queue

max_message 1M;
out_queue
out_cork

Access

allow

allow publish 127.0.0.1;
deny publish all;
allow play 192.168.0.0/24;
deny play all;**

deny

Exec

exec

  1. * $name - 流的名字。
  2. * $app - 应用名。
  3. * $addr - 客户端地址。
  4. * $flashver - 客户端 flash 版本。
  5. * $swfurl - 客户端 swf url。
  6. * $tcurl - 客户端 tc url。
  7. * $pageurl - 客户端页面 url。

可以在 exec 指令中定义 Shell 格式的转向符用于写输出和接收输入。支持如下

  1. * 截断输出 >file。
  2. * 附加输出 >>file。
  3. * 重定向描述符类似于 1>&2。
  4. * 输入 <file。

以下 ffmpeg 调用将输入流转码为 HLS-ready 流(H264/AAC)。运行这个示例,FFmpeg 须编译为支持 libx264 & libfaac。

application src {
    live on;
    exec ffmpeg -i rtmp://localhost/src/$name -vcodec libx264 -vprofile baseline -g 10 -s 300x200 -acodec libfaac -ar 44100 -ac 1 -f flv rtmp://localhost/hls/$name 2>>/var/log/ffmpeg-$name.log;
}

application hls {
    live on;
    hls on;
    hls_path /tmp/hls;
    hls_fragment 15s;
}

exec_static

exec_static ffmpeg -i http://example.com/video.ts -c copy -f flv rtmp://localhost/myapp/mystream;**

exec_kill_signal

exec_kill_signal term;
exec_kill_signal usr1;
exec_kill_signal 3;

respawn

respawn off;

respawn_timeout

respawn_timeout 10s;

exec_publish

exec_play

exec_play_done

exec_publish_done

exec_record_done

# track client info
exec_play bash -c "echo $addr $pageurl >> /tmp/clients";
exec_publish bash -c "echo $addr $flashver >> /tmp/publishers";

# convert recorded file to mp4 format
exec_record_done ffmpeg -y -i $path -acodec libmp3lame -ar 44100 -ac 1 -vcodec libx264 $path.mp4;

Live

live

live on;

meta

meta off;

interleave

interleave on;

wait_key

wait_key on;

wait_video

wait_video on;

publish_notify

publish_notify on;

drop_idle_publisher

drop_idle_publisher 10s;

sync

sync 10ms;

play_restart

play_restart off;

Record

record

record all;
record audio keyframes;

record_path

record_path /tmp/rec;

record_suffix

record_suffix _recorded.flv;

录制后缀可以匹配 strftime 格式。以下指令

record_suffix -%d-%b-%y-%T.flv

将会产生形如 mystream-24-Apr-13-18:23:38.flv 的文件。所有支持 strftime 格式的选项可以在 strftime man page 里进行查找。

record_unique

record_unique on;

record_append

record_append on;

record_lock

record_lock on;

在 FreeBSD 上你可以使用 flock 工具检查。在 Linux 上 flock 和 fcntl 无关,因此你需要去写一个简单的脚本来检查文件的锁定状态。以下 isunlocked.py 是一个这样的脚本的示例。

#!/usr/bin/python 
import fcntl, sys

sys.stderr.close()
fcntl.lockf(open(sys.argv[1], "a"), fcntl.LOCK_EX|fcntl.LOCK_NB)

record_max_size

record_max_size 128K;

record_max_frames

record_max_frames 2;

record_interval

record_interval 1s;
record_interval 15m;

recorder

application {
    live on;
    # default recorder
    record all;
    record_path /var/rec;

    recorder audio {
        record audio;
        record_suffix .audio.flv;
    }

    recorder chunked {
        record all;
        record_interval 15s;
        record_path /var/rec/chunked;
    }
}

record_notify

recorder myrec {
    record all manual;
    record_path /var/rec;
    record_notify on;
}

VOD

play

application vod {
    play /var/flvs;
}

application vod_http {
    play http://myserver.com/vod;
}

application vod_mirror {
    # try local location first, then access remote location
    play /var/local_mirror http://myserver.com/vod;
}

播放 /var/flvs/dir/file.flv:

ffplay rtmp://localhost/vod/dir/file.flv

play_temp_path

play_temp_path /www;
play http://example.com/videos;**

play_local_path

# search file in /tmp/videos.
# if not found play from remote location
# and store in /tmp/videos


play_local_path /tmp/videos;
play /tmp/videos http://example.com/videos;

Relay

pull

如果某参数的值包含空格,那么你应该在整个 key=value 对周围使用引号,比如:'pageUrl=FAKE PAGE URL'。

pull rtmp://cdn.example.com/main/ch?id=12563 name=channel_a;
pull rtmp://cdn2.example.com/another/a?b=1&c=d pageUrl=http://www.example.com/video.html swfUrl=http://www.example.com/player.swf live=1;
pull rtmp://cdn.example.com/main/ch?id=12563 name=channel_a static;

push

push_reconnect

push_reconnect 1s;

session_relay

session_relay on;

Notify

on_connect

# ...
streamer: "rtmp://localhost/myapp?connarg1=a&connarg2=b",
file: "mystream?strarg1=c&strarg2=d",
# ...

Ffplay(带有 librtmp)示例:

ffplay "rtmp://localhost app=myapp?connarg1=a&connarg2=b playpath=mystream?strarg1=c&strarg2=d"

使用例子:

on_connect http://example.com/my_auth;

重定向例子:

location /on_connect {
  if ($arg_flashver != "my_secret_flashver") {
    rewrite ^.\*$ fallback? permanent;
  }
  return 200;
}

on_play

http {
    # ...
    location /local_redirect {
        rewrite ^.*$ newname? permanent;
    }
    location /remote_redirect {
        # no domain name here, only ip
        rewrite ^.*$ rtmp://192.168.1.123/someapp/somename? permanent;
    }
    # ...
}


rtmp {
    # ...
    application myapp1 {
        live on;
        # stream will be redirected to 'newname'
        on_play http://localhost:8080/local_redirect;
    }
    application myapp2 {
        live on;
        # stream will be pulled from remote location
        # requires nginx >= 1.3.10
        on_play http://localhost:8080/remote_redirect;
    }
    # ...
}

HTTP 请求接收到一些个参数。在 application/x-www-form-urlencoded MIME 类型下使用 POST 方法。以下参数会被传送给调用者:

  1. call=play。
  2. addr - 客户端 IP 地址。
  3. app - application 名。
  4. flashVer - 客户端 flash 版本。
  5. swfUrl - 客户端 swf url。
  6. tcUrl - tcUrl。
  7. pageUrl - 客户端页面 url。
  8. name - 流名。

出了上述参数之外其他所有播放命令参数显式地发送回调。例如如果一个流由 url rtmp://localhost/app/movie?a=100&b=face&foo=bar 访问,然后呢 a,b 和 foo 发送回调。

on_play http://example.com/my_callback;

on_publish

on_done

on_play_done

on_publish_done

on_record_done

on_record_done http://example.com/recorded;

on_update

on_update http://example.com/update;

notify_update_timeout

notify_update_timeout 10s;
on_update http://example.com/update;

notify_update_strict

notify_update_strict on;
on_update http://example.com/update;

notify_relay_redirect

notify_relay_redirect on;

notify_method

notify_method get;

在 http{} 部分使用 GET 方法处理通知可以使用这种方法:

location /on_play {
    if ($arg_pageUrl ~\* localhost) {
        return 200;
    }
    return 500;
}

HLS

hls

hls on;
hls_path /tmp/hls;
hls_fragment 15s;

在 http{} 段为客户端播放 HLS 设置在以下位置设置:

http {
  ...
  server {
    ...
    location /hls {
      types {
        application/vnd.apple.mpegurl m3u8;
      }
      alias /tmp/hls;
    }
  }
}

hls_path

hls_fragment

hls_playlist_length

hls_playlist_length 10m;

hls_sync

hls_sync 100ms;

hls_continuous

hls_continuous on;

hls_nested

hls_nested on;

hls_cleanup

hls_cleanup off;

Access log

access_log

log_format new '$remote_addr';
access_log logs/rtmp_access.log new;
access_log logs/rtmp_access.log;
access_log off;

log_format

$remote_addr [$time_local] $command "$app" "$name" "$args" - 
$bytes_received $bytes_sent "$pageurl" "$flashver" ($session_readable_time)

Limits

max_connections 100;

Statistics

statistics 模块不同于本文列举的其他模块,它是 NGINX HTTP 模块。因此 statistics 指令应该位于 http{} 块内部。

rtmp_stat

http {
  server {
    location /stat {
      rtmp_stat all;
      rtmp_stat_stylesheet stat.xsl;
    }
    location /stat.xsl {
      root /path/to/stat/xsl/file;
    }
  }
}

rtmp_stat_stylesheet

Multi-worker live streaming

多 worker 直播流是通过推送流到剩余的 nginx worker 实现的。

rtmp_auto_push

rtmp_auto_push_reconnect

rtmp_socket_dir

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /var/sock;

rtmp {
    server {
        listen 1935;
        application myapp {
            live on;
        }
    }
}

Control

control 模块是 NGINX HTTP 模块,应该放在 http{} 块之内。

rtmp_control

http {
  server {
    location /control {
      rtmp_control all;
    }
  }
}