├── .github
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── FUNDING.yml
├── ISSUE_TEMPLATE.md
└── workflows
│ └── nginx-http-flv-module.yml
├── AUTHORS
├── LICENSE
├── README.CN.md
├── README.md
├── config
├── dash
├── ngx_rtmp_dash_module.c
├── ngx_rtmp_mp4.c
└── ngx_rtmp_mp4.h
├── doc
└── README.md
├── hls
├── ngx_rtmp_hls_module.c
├── ngx_rtmp_hls_module.h
├── ngx_rtmp_mpegts.c
├── ngx_rtmp_mpegts.h
├── ngx_rtmp_mpegts_crc.c
└── ngx_rtmp_mpegts_crc.h
├── ngx_http_flv_live_module.c
├── ngx_http_flv_live_module.h
├── ngx_rtmp.c
├── ngx_rtmp.h
├── ngx_rtmp_access_module.c
├── ngx_rtmp_amf.c
├── ngx_rtmp_amf.h
├── ngx_rtmp_auto_push_module.c
├── ngx_rtmp_bandwidth.c
├── ngx_rtmp_bandwidth.h
├── ngx_rtmp_bitop.c
├── ngx_rtmp_bitop.h
├── ngx_rtmp_cmd_module.c
├── ngx_rtmp_cmd_module.h
├── ngx_rtmp_codec_module.c
├── ngx_rtmp_codec_module.h
├── ngx_rtmp_control_module.c
├── ngx_rtmp_core_module.c
├── ngx_rtmp_eval.c
├── ngx_rtmp_eval.h
├── ngx_rtmp_exec_module.c
├── ngx_rtmp_flv_live_index_module.c
├── ngx_rtmp_flv_module.c
├── ngx_rtmp_gop_cache_module.c
├── ngx_rtmp_gop_cache_module.h
├── ngx_rtmp_handler.c
├── ngx_rtmp_handshake.c
├── ngx_rtmp_init.c
├── ngx_rtmp_limit_module.c
├── ngx_rtmp_live_module.c
├── ngx_rtmp_live_module.h
├── ngx_rtmp_log_module.c
├── ngx_rtmp_mp4_module.c
├── ngx_rtmp_netcall_module.c
├── ngx_rtmp_netcall_module.h
├── ngx_rtmp_notify_module.c
├── ngx_rtmp_parse.c
├── ngx_rtmp_play_module.c
├── ngx_rtmp_play_module.h
├── ngx_rtmp_proxy_protocol.c
├── ngx_rtmp_proxy_protocol.h
├── ngx_rtmp_receive.c
├── ngx_rtmp_record_module.c
├── ngx_rtmp_record_module.h
├── ngx_rtmp_relay_module.c
├── ngx_rtmp_relay_module.h
├── ngx_rtmp_send.c
├── ngx_rtmp_shared.c
├── ngx_rtmp_stat_module.c
├── ngx_rtmp_streams.h
├── ngx_rtmp_variables.c
├── ngx_rtmp_variables.h
├── ngx_rtmp_version.h
├── samples
├── flv.js.png
└── jwplayer_vlc.png
├── stat.xsl
└── test
├── README.md
├── dump.sh
├── ffstream.sh
├── nginx.conf
├── play.sh
├── rtmp-publisher
├── README.md
├── RtmpPlayer.mxml
├── RtmpPlayer.swf
├── RtmpPlayerLight.mxml
├── RtmpPlayerLight.swf
├── RtmpPublisher.mxml
├── RtmpPublisher.swf
├── player.html
├── publisher.html
└── swfobject.js
└── www
├── bg.jpg
├── index.html
├── jwplayer
├── jwplayer.flash.swf
└── jwplayer.js
├── jwplayer_old
├── player.swf
└── swfobject.js
└── record.html
/.github/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | * Using welcoming and inclusive language
12 | * Being respectful of differing viewpoints and experiences
13 | * Gracefully accepting constructive criticism
14 | * Focusing on what is best for the community
15 | * Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | * Trolling, insulting/derogatory comments, and personal or political attacks
21 | * Public or private harassment
22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission
23 | * Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at winshining@163.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38 |
39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40 |
41 | ## Attribution
42 |
43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44 |
45 | [homepage]: http://contributor-covenant.org
46 | [version]: http://contributor-covenant.org/version/1/4/
47 |
--------------------------------------------------------------------------------
/.github/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | ## Guidelines to contribute
2 |
3 | #### **When you find a bug**
4 |
5 | * **Ensure the bug was not already reported** by searching on GitHub under [Issues](https://github.com/winshining/nginx-http-flv-module/issues).
6 |
7 | * If there is no issue addressing the problem, [open a new one](https://github.com/winshining/nginx-http-flv-module/issues/new). Be sure to include a **title prefixed by '[bug]' and clear description**, as much relevant information as possible, and a **code sample** or an **executable test case** demonstrating the expected behavior that is not occurring.
8 |
9 | #### **Write a patch that fixes a bug**
10 |
11 | * Open a new GitHub pull request with the patch.
12 |
13 | * Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.
14 |
15 | * Before submitting, be sure the commit description is prefixed by:
16 | * **[add]** if new features were added.
17 | * **[dev]** if codes were changed.
18 | * **[fix]** if bugs were fixed.
19 | * **[misc]** if some changes were done and bugs were fixed.
20 |
21 | * Ensure that your codes conform to code conventions:
22 | * All files are prefixed by 'ngx\_'.
23 | * Include #ifndef \_FILE\_NAME\_H\_INCLUDED\_, #define \_FILE\_NAME\_H\_INCLUDED\_ and #endif in header files.
24 | * Comments use /* ... */ are preferable.
25 | * It would be better that built-in types appear before customized types.
26 | * There should be no less than 2 spaces between types and variables.
27 | * Variables are aligned by character, not '\*'.
28 | * No more than 80 characters in a single code or comment line.
29 | * Two blank lines between two functions, styles of macro and type definitions are same as functions.
30 |
31 | #### **Add a new feature or change an existing one**
32 |
33 | * Open an issue on GitHub prefixed by '[feature]' until you have collected positive feedback about the change.
34 |
35 | #### **Questions about the source code**
36 |
37 | * Open an issue on GitHub prefixed by '[misc]', describe as clear as possible.
38 |
39 | Thanks!
40 |
41 | Winshining
42 |
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | custom: ['https://www.paypal.me/ShingWong']
2 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | When you meet a bug, please open the issue including a title prefixed by '[bug]' and describe it as follows:
2 | (当你碰到一个 bug,请在提出问题时以 '[bug]' 为前缀写明标题,并且像下面的内容一样描述它):
3 |
4 | ### Expected behavior (期望行为)
5 |
6 | ### Actual behavior (实际行为)
7 |
8 | ### OS and Nginx version (操作系统和 Nginx 版本号)
9 |
10 | ### Configuration file (配置文件)
11 |
12 | ### Steps to reproduce the behavior (复现问题步骤)
13 |
14 | ### Error log if any (错误日志)
15 |
--------------------------------------------------------------------------------
/.github/workflows/nginx-http-flv-module.yml:
--------------------------------------------------------------------------------
1 | name: nginx-http-flv-module CI
2 |
3 | on:
4 | push:
5 | branches: [ "master" ]
6 | pull_request:
7 | branches: [ "master" ]
8 |
9 | jobs:
10 | build:
11 |
12 | runs-on: ubuntu-latest
13 |
14 | env:
15 | NGINX_VERSION: nginx-1.28.0
16 |
17 | steps:
18 | - uses: actions/checkout@v3
19 | - name: download nginx
20 | working-directory: ../
21 | run: wget https://nginx.org/download/${{env.NGINX_VERSION}}.tar.gz
22 | - name: uncompress nginx
23 | working-directory: ../
24 | run: tar zxvf ${{env.NGINX_VERSION}}.tar.gz
25 | - name: configure (build into nginx)
26 | working-directory: ../${{env.NGINX_VERSION}}
27 | run: ./configure --add-module=../nginx-http-flv-module
28 | - name: make
29 | working-directory: ../${{env.NGINX_VERSION}}
30 | run: make
31 | - name: clean
32 | working-directory: ../${{env.NGINX_VERSION}}
33 | run: make clean
34 | - name: configure (build as a dynamic module)
35 | working-directory: ../${{env.NGINX_VERSION}}
36 | run: ./configure --add-dynamic-module=../nginx-http-flv-module
37 | - name: make
38 | working-directory: ../${{env.NGINX_VERSION}}
39 | run: make
40 | - name: remove
41 | working-directory: ../
42 | run: rm -rf "${{env.NGINX_VERSION}}*"
43 |
--------------------------------------------------------------------------------
/AUTHORS:
--------------------------------------------------------------------------------
1 | Project author:
2 |
3 | Roman Arutyunyan
4 | Moscow, Russia
5 | Contacts:
6 | arut@qip.ru
7 | arutyunyan.roman@gmail.com
8 |
9 | Winshining
10 | Beijing, China
11 | Contacts:
12 | winshining@163.com
13 |
14 | Gnolizuh
15 | Beijing, China
16 | Contacts:
17 | huzilong_007@163.com
18 | huzilong@kingsoft.com
19 |
20 | han4235
21 | Suzhou, China
22 | Contacts:
23 | https://github.com/han4235
24 |
25 | plainheart
26 | Zhengzhou, China
27 | Contacts:
28 | https://github.com/plainheart
29 |
30 | HeyJupiter:
31 | Seattle, US
32 | Contacts:
33 | https://github.com/HeyJupiter
34 |
35 | Vladimir Vainer
36 | -
37 | Contacts:
38 | https://github.com/ferreus
39 |
40 | ever4Keny
41 | China
42 | Contacts:
43 | https://github.com/ever4Keny
44 |
45 | spacewander
46 | Guangzhou, China
47 | Contacts:
48 | spacewanderlzx@gmail.com
49 |
50 | ham3r
51 | -
52 | Contacts:
53 | https://github.com/ham3r
54 |
55 | deamos
56 | Scranton, PA
57 | Contacts:
58 | https://github.com/deamos
59 |
60 | vacing
61 | Shenzhen, China
62 | Contacts:
63 | https://github.com/vacing
64 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 2-Clause License
2 |
3 | Copyright (c) 2012-2017, Roman Arutyunyan
4 | Copyright (c) 2017-2025, Winshining
5 | Copyright (c) 2018, han4235, Vladimir Vainer
6 | Copyright (c) 2018-2019, plainheart, HeyJupiter
7 | Copyright (c) 2019, ever4Keny
8 | Copyright (c) 2020, spacewander, ham3r
9 | Copyright (c) 2022, deamons
10 | Copyright (c) 2024, vacing
11 | All rights reserved.
12 |
13 | Redistribution and use in source and binary forms, with or without
14 | modification, are permitted provided that the following conditions are met:
15 |
16 | * Redistributions of source code must retain the above copyright notice, this
17 | list of conditions and the following disclaimer.
18 |
19 | * Redistributions in binary form must reproduce the above copyright notice,
20 | this list of conditions and the following disclaimer in the documentation
21 | and/or other materials provided with the distribution.
22 |
23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 |
--------------------------------------------------------------------------------
/README.CN.md:
--------------------------------------------------------------------------------
1 | # nginx-http-flv-module
2 |
3 | 
4 |
5 | 一款基于 [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) 的流媒体服务器。
6 |
7 | [English README](https://github.com/winshining/nginx-http-flv-module/blob/master/README.md)。
8 |
9 | 如果您喜欢这个模块,可以通过赞赏来支持我的工作,非常感谢!
10 |
11 | 
12 |
13 | ### 感谢
14 |
15 | * Igor Sysoev,[NGINX](http://nginx.org) 的作者。
16 |
17 | * Roman Arutyunyan,[nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) 的作者。
18 |
19 | * 贡献者,详情见 [AUTHORS](https://github.com/winshining/nginx-http-flv-module/blob/master/AUTHORS)。
20 |
21 | ## 功能
22 |
23 | * [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) 提供的所有功能。
24 |
25 | * nginx-http-flv-module 的其他功能与 [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) 的对比:
26 |
27 | | 功能 | nginx-http-flv-module | nginx-rtmp-module | 备注 |
28 | | :----------------------: | :-------------------: | :---------------: | :--------------------------------------: |
29 | | HTTP-FLV (播放) | √ | x | 支持 HTTPS-FLV 和 chunked 回复 |
30 | | GOP 缓存 | √ | x | |
31 | | 虚拟主机 | √ | x | |
32 | | 省略 `listen` 配置项 | √ | 见备注 | 配置中必须有一个 `listen` |
33 | | RTMP/HTTP-FLV 纯音频支持 | √ | 见备注 |`wait_video` 或 `wait_key` 开启后无法工作 |
34 | | HLS 单轨支持 | √ | x | |
35 | | `reuseport` 支持 | √ | x | |
36 | | 定时打印访问记录 | √ | x | |
37 | | JSON 风格的数据信息 | √ | x | |
38 | | 录制的数据信息 | √ | x | |
39 | | 大小端无关 | √ | 见备注 | `big-endian` 分支部分支持 |
40 |
41 | ## 兼容性
42 |
43 | [NGINX](http://nginx.org) 的版本**应该**大于或者等于 1.2.6,与其他版本的兼容性未知。
44 |
45 | ## 支持的系统
46 |
47 | * Linux(推荐)/ FreeBSD / MacOS / Windows(受限)。
48 |
49 | ## 支持的播放器
50 |
51 | * [VLC](http://www.videolan.org) (RTMP & HTTP-FLV) / [OBS](https://obsproject.com) (RTMP & HTTP-FLV) / [JW Player](https://www.jwplayer.com) (RTMP) / [flv.js](https://github.com/Bilibili/flv.js) (HTTP-FLV).
52 |
53 | ### 注意
54 |
55 | * Adobe 将在 2020 年 12 月 31 日之后停止对 [flash 播放器](https://www.adobe.com/products/flashplayer.html) 的官方支持,详情见 [Adobe Flash Player EOL General Information Page](https://www.adobe.com/products/flashplayer/end-of-life.html)。主流浏览器随后将移除 flash 播放器,使用 flash 播放器的插件将不再可用。
56 |
57 | * [flv.js](https://github.com/Bilibili/flv.js) 只能运行在支持 [Media Source Extensions](https://www.w3.org/TR/media-source) 的浏览器上。
58 |
59 | ## 依赖
60 |
61 | * 在类 Unix 系统上,需要 GNU make,用于调用编译器来编译软件。
62 |
63 | * 在类 Unix 系统上,需要 GCC。或者在 Windows 上,需要 MSVC,用于编译软件。
64 |
65 | * 在类 Unix 系统上,需要 GDB,用于调试软件(可选)。
66 |
67 | * [FFmpeg](http://ffmpeg.org) 或者 [OBS](https://obsproject.com),用于发布媒体流。
68 |
69 | * [VLC](http://www.videolan.org)(推荐)或者 [flv.js](https://github.com/Bilibili/flv.js)(推荐),用于播放媒体流。
70 |
71 | * 如果 NGINX 要支持正则表达式,需要 [PCRE库](http://www.pcre.org)。
72 |
73 | * 如果 NGINX 要支持加密访问,需要 [OpenSSL库](https://www.openssl.org)。
74 |
75 | * 如果 NGINX 要支持压缩,需要 [zlib库](http://www.zlib.net)。
76 |
77 | ## 创建
78 |
79 | ### 注意
80 |
81 | nginx-http-flv-module 包含了 [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) 所有的功能,所以**不要**将 nginx-http-flv-module 和 [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) 一起编译。
82 |
83 | ### 在 Windows 上
84 |
85 | 编译步骤请参考 [Building nginx on the Win32 platform with Visual C](http://nginx.org/en/docs/howto_build_on_win32.html),不要忘了在 `Run configure script` 步骤中添加 `--add-module=/path/to/nginx-http-flv-module`。
86 |
87 | #### 注意
88 |
89 | 如果使用没有完整支持 x64 的编译器来编译此模块,例如 VS2010,请务必使用默认设置(目标机器类型 x86)。
90 |
91 | ### 在类 Unix 系统上
92 |
93 | 下载 [NGINX](http://nginx.org) 和 nginx-http-flv-module。
94 |
95 | 将它们解压到某一路径。
96 |
97 | 打开 NGINX 的源代码路径并执行:
98 |
99 | #### 将模块编译进 [NGINX](http://nginx.org)
100 |
101 | ./configure --add-module=/path/to/nginx-http-flv-module
102 | make
103 | make install
104 |
105 | 或者
106 |
107 | #### 将模块编译为动态模块
108 |
109 | ./configure --add-dynamic-module=/path/to/nginx-http-flv-module
110 | make
111 | make install
112 |
113 | #### 注意
114 |
115 | 如果将模块编译为动态模块,那么 [NGINX](http://nginx.org) 的版本号**必须**大于或者等于 1.9.11。
116 |
117 | ## 使用方法
118 |
119 | 关于 [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) 用法的详情,请参考 [README.md](https://github.com/arut/nginx-rtmp-module/blob/master/README.md)。
120 |
121 | ### 发布
122 |
123 | 为了简单起见,不用转码:
124 |
125 | ffmpeg -re -i MEDIA_FILE_NAME -c copy -f flv rtmp://example.com[:port]/appname/streamname
126 |
127 | #### 注意
128 |
129 | 一些旧版本的 [FFmpeg](http://ffmpeg.org) 不支持选项 `-c copy`,可以使用选项 `-vcodec copy -acodec copy` 替代。
130 |
131 | `appname` 用于匹配 rtmp 配置块中的 application 块(更多详情见下文)。
132 |
133 | `streamname` 可以随意指定,但是**不能**省略。
134 |
135 | **RTMP 默认端口**为 **1935**,如果要使用其他端口,必须指定 `:port`。
136 |
137 | ### 播放
138 |
139 | #### HTTP-FLV 方式
140 |
141 | http://example.com[:port]/dir?[port=xxx&]app=appname&stream=streamname
142 |
143 | #### 注意
144 |
145 | * 如果使用 [ffplay](http://www.ffmpeg.org/ffplay.html) 命令行方式播放流,那么**必须**为上述的 url 加上引号,否则 url 中的参数会被丢弃(有些不太智能的 shell 会把 "&" 解释为"后台运行")。
146 |
147 | * 如果使用 [flv.js](https://github.com/Bilibili/flv.js) 播放流,那么请保证发布的流被正确编码,因为 [flv.js](https://github.com/Bilibili/flv.js) **只支持 H.264 编码的视频和 AAC/MP3 编码的音频**。
148 |
149 | 参数 `dir` 用于匹配 http 配置块中的 location 块(更多详情见下文)。
150 |
151 | **HTTP 默认端口**为 **80**, 如果使用了其他端口,必须指定 `:port`。
152 |
153 | **RTMP 默认端口**为 **1935**,如果使用了其他端口,必须指定 `port=xxx`。
154 |
155 | 参数 `app` 的值(appname)用来匹配 application 块,但是如果请求的 `app` 出现在多个 server 块中,并且这些 server 块有相同的地址和端口配置,那么还需要用匹配主机名的 `server_name` 配置项来区分请求的是哪个 application 块,否则,将匹配第一个 application 块。
156 |
157 | 参数 `stream` 的值(streamname)用来匹配发布的流的名称。
158 |
159 | #### 例子
160 |
161 | 假设在 `http` 配置块中的 `listen` 配置项是:
162 |
163 | http {
164 | ...
165 | server {
166 | listen 8080; #不是默认的 80 端口
167 | ...
168 |
169 | location /live {
170 | flv_live on;
171 | }
172 | }
173 | }
174 |
175 | 在 `rtmp` 配置块中的 `listen` 配置项是:
176 |
177 | rtmp {
178 | ...
179 | server {
180 | listen 1985; #不是默认的 1935 端口
181 | ...
182 |
183 | application myapp {
184 | live on;
185 | }
186 | }
187 | }
188 |
189 | 并且发布的流的名称是 `mystream`,那么基于 HTTP 的播放 url 是:
190 |
191 | http://example.com:8080/live?port=1985&app=myapp&stream=mystream
192 |
193 | #### 注意
194 |
195 | 由于一些播放器不支持 HTTP 块传输, 这种情况下最好在指定了 `flv_live on;` 的 location 中指定 `chunked_transfer_encoding off`,否则播放会失败。
196 |
197 | #### RTMP 方式
198 |
199 | rtmp://example.com[:port]/appname/streamname
200 |
201 | #### HLS 方式
202 |
203 | http://example.com[:port]/dir/streamname.m3u8
204 |
205 | #### DASH 方式
206 |
207 | http://example.com[:port]/dir/streamname.mpd
208 |
209 | ## 示例图片
210 |
211 | ### RTMP ([JW Player](https://www.jwplayer.com)) & HTTP-FLV ([VLC](http://www.videolan.org))
212 |
213 | 
214 |
215 | ### HTTP-FLV ([flv.js](https://github.com/Bilibili/flv.js))
216 |
217 | 
218 |
219 | ## nginx.conf 实例
220 |
221 | ### 注意
222 |
223 | 配置项 `rtmp_auto_push`,`rtmp_auto_push_reconnect` 和 `rtmp_socket_dir` 在 Windows 上不起作用,除了 Windows 10 17063 以及后续版本之外,因为多进程模式的 `relay` 需要 Unix domain socket 的支持,详情请参考 [Unix domain socket on Windows 10](https://blogs.msdn.microsoft.com/commandline/2017/12/19/af_unix-comes-to-windows)。
224 |
225 | 最好将配置项 `worker_processes` 设置为 1,因为在多进程模式下,`ngx_rtmp_stat_module` 可能不会从指定的 worker 进程获取统计数据,因为 HTTP 请求是被随机分配给 worker 进程的。`ngx_rtmp_control_module` 也有同样的问题。这个问题可以通过这个补丁 [per-worker-listener](https://github.com/arut/nginx-patches/blob/master/per-worker-listener) 优化。
226 |
227 | 另外,`vhost` 功能在单进程模式下没有问题,但是在多进程模式下还不能完全正确运行,等待修复。例如,下面的配置在多进程模式下是没有问题的:
228 |
229 | rtmp {
230 | ...
231 | server {
232 | listen 1935;
233 | server_name domain_name;
234 |
235 | application myapp {
236 | ...
237 | }
238 | }
239 | }
240 |
241 | 而使用下面的配置,当 publisher 在第二个 `server` 上发布媒体流,播放请求以该配置(不管端口是不是 1935)访问非 publisher 的 worker 进程时是有问题的:
242 |
243 | rtmp {
244 | ...
245 | server {
246 | listen 1935;
247 | server_name 1st_domain_name;
248 |
249 | application myapp {
250 | ...
251 | }
252 | }
253 |
254 | server {
255 | listen 1945;
256 | server_name 2nd_domain_name;
257 |
258 | application myapp {
259 | ...
260 | }
261 | }
262 | }
263 |
264 | 如果 [NGINX](http://nginx.org) 是以多进程模式运行并且平台支持 socket 选项 `SO_REUSEPORT`,那么在配置项 `listen` 后添加选项 `reuseport` 可以解决惊群问题。
265 |
266 | rtmp {
267 | ...
268 |
269 | server {
270 | listen 1935 reuseport;
271 | ...
272 | }
273 | }
274 |
275 | ### 配置实例
276 |
277 | worker_processes 1; #运行在 Windows 上时,设置为 1,因为 Windows 不支持 Unix domain socket
278 | #worker_processes auto; #1.3.8 和 1.2.5 以及之后的版本
279 |
280 | #worker_cpu_affinity 0001 0010 0100 1000; #只能用于 FreeBSD 和 Linux
281 | #worker_cpu_affinity auto; #1.9.10 以及之后的版本
282 |
283 | error_log logs/error.log error;
284 |
285 | #如果此模块被编译为动态模块并且要使用与 RTMP 相关的功
286 | #能时,必须指定下面的配置项并且它必须位于 events 配置
287 | #项之前,否则 NGINX 启动时不会加载此模块或者加载失败
288 |
289 | #load_module modules/ngx_http_flv_live_module.so;
290 |
291 | events {
292 | worker_connections 4096;
293 | }
294 |
295 | http {
296 | include mime.types;
297 | default_type application/octet-stream;
298 |
299 | keepalive_timeout 65;
300 |
301 | server {
302 | listen 80;
303 |
304 | location / {
305 | root /var/www;
306 | index index.html index.htm;
307 | }
308 |
309 | error_page 500 502 503 504 /50x.html;
310 | location = /50x.html {
311 | root html;
312 | }
313 |
314 | location /live {
315 | flv_live on; #打开 HTTP 播放 FLV 直播流功能
316 | chunked_transfer_encoding on; #支持 'Transfer-Encoding: chunked' 方式回复
317 |
318 | add_header 'Access-Control-Allow-Origin' '*'; #添加额外的 HTTP 头
319 | add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的 HTTP 头
320 | }
321 |
322 | location /hls {
323 | types {
324 | application/vnd.apple.mpegurl m3u8;
325 | video/mp2t ts;
326 | }
327 |
328 | root /tmp;
329 | add_header 'Cache-Control' 'no-cache';
330 | }
331 |
332 | location /dash {
333 | root /tmp;
334 | add_header 'Cache-Control' 'no-cache';
335 | }
336 |
337 | location /stat {
338 | #推流播放和录制统计数据的配置
339 |
340 | rtmp_stat all;
341 | rtmp_stat_stylesheet stat.xsl;
342 | }
343 |
344 | location /stat.xsl {
345 | root /var/www/rtmp; #指定 stat.xsl 的位置
346 | }
347 |
348 | #如果需要 JSON 风格的 stat, 不用指定 stat.xsl
349 | #但是需要指定一个新的配置项 rtmp_stat_format
350 |
351 | #location /stat {
352 | # rtmp_stat all;
353 | # rtmp_stat_format json;
354 | #}
355 |
356 | location /control {
357 | rtmp_control all; #rtmp 控制模块的配置
358 | }
359 | }
360 | }
361 |
362 | rtmp_auto_push on;
363 | rtmp_auto_push_reconnect 1s;
364 | rtmp_socket_dir /tmp;
365 |
366 | rtmp {
367 | out_queue 4096;
368 | out_cork 8;
369 | max_streams 128;
370 | timeout 15s;
371 | drop_idle_publisher 15s;
372 |
373 | log_interval 5s; #log 模块在 access.log 中记录日志的间隔时间,对调试非常有用
374 | log_size 1m; #log 模块用来记录日志的缓冲区大小
375 |
376 | server {
377 | listen 1935;
378 | server_name www.test.*; #用于虚拟主机名后缀通配
379 |
380 | application myapp {
381 | live on;
382 | gop_cache on; #打开 GOP 缓存,减少首屏等待时间
383 | }
384 |
385 | application hls {
386 | live on;
387 | hls on;
388 | hls_path /tmp/hls;
389 | }
390 |
391 | application dash {
392 | live on;
393 | dash on;
394 | dash_path /tmp/dash;
395 | }
396 | }
397 |
398 | server {
399 | listen 1935;
400 | server_name *.test.com; #用于虚拟主机名前缀通配
401 |
402 | application myapp {
403 | live on;
404 | gop_cache on; #打开 GOP 缓存,减少首屏等待时间
405 | }
406 | }
407 |
408 | server {
409 | listen 1935;
410 | server_name www.test.com; #用于虚拟主机名完全匹配
411 |
412 | application myapp {
413 | live on;
414 | gop_cache on; #打开 GOP 缓存,减少首屏等待时间
415 | }
416 | }
417 | }
418 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # nginx-http-flv-module
2 |
3 | 
4 |
5 | A media streaming server based on [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module).
6 |
7 | [中文说明](https://github.com/winshining/nginx-http-flv-module/blob/master/README.CN.md).
8 |
9 | Donate if you like this module. Many thanks to you!
10 |
11 |
12 |
13 | ### Credits
14 |
15 | * Igor Sysoev, the creator of [NGINX](http://nginx.org).
16 |
17 | * Roman Arutyunyan, who created [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module).
18 |
19 | * Contributors, refer to [AUTHORS](https://github.com/winshining/nginx-http-flv-module/blob/master/AUTHORS) for details.
20 |
21 | ## Features
22 |
23 | * All features [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) provides.
24 |
25 | * Other features provided by nginx-http-flv-module vs [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module):
26 |
27 | | Features | nginx-http-flv-module | nginx-rtmp-module | Remarks |
28 | | :--------------------------------: | :-------------------: | :---------------: | :--------------------------------------------: |
29 | | HTTP-FLV (for play) | √ | x | HTTPS-FLV and chunked response supported |
30 | | GOP cache | √ | x | |
31 | | Virtual Host | √ | x | |
32 | | Omit `listen` directive | √ | See remarks | There MUST be at least one `listen` directive |
33 | |Audio-only support for RTMP/HTTP-FLV| √ | See remarks | Won't work if `wait_video` or `wait_key` is on |
34 | | Single-track support for HLS | √ | x | |
35 | | `reuseport` support | √ | x | |
36 | | Timer for access log | √ | x | |
37 | | JSON style statistics | √ | x | |
38 | | Statistics for recordings | √ | x | |
39 | | Independent of endianness | √ | See remarks | Partially supported in branch `big-endian` |
40 |
41 | ## Compatibility
42 |
43 | The [NGINX](http://nginx.org) version **SHOULD** be equal to or greater than 1.2.6, the compatibility with other versions is unknown.
44 |
45 | ## Systems supported
46 |
47 | * Linux (recommended) / FreeBSD / MacOS / Windows (limited).
48 |
49 | ## Players supported
50 |
51 | * [VLC](http://www.videolan.org) (RTMP & HTTP-FLV) / [OBS](https://obsproject.com) (RTMP & HTTP-FLV) / [JW Player](https://www.jwplayer.com) (RTMP) / [flv.js](https://github.com/Bilibili/flv.js) (HTTP-FLV).
52 |
53 | ### Note
54 |
55 | * [Flash player](https://www.adobe.com/products/flashplayer.html) will be no longer supported officially by Adobe after December 31, 2020, refer to [Adobe Flash Player EOL General Information Page](https://www.adobe.com/products/flashplayer/end-of-life.html) for details. Plugins that use flash player won't work after the major browsers subsequently remove flash player.
56 |
57 | * [flv.js](https://github.com/Bilibili/flv.js) can only run with browsers that support [Media Source Extensions](https://www.w3.org/TR/media-source).
58 |
59 | ## Prerequisites
60 |
61 | * GNU make for activating compiler on Unix-like systems to compile software.
62 |
63 | * GCC for compilation on Unix-like systems or MSVC for compilation on Windows.
64 |
65 | * GDB for debug on Unix-like systems.
66 |
67 | * [FFmpeg](http://ffmpeg.org) or [OBS](https://obsproject.com) for publishing media streams.
68 |
69 | * [VLC](http://www.videolan.org) (recommended) or [flv.js](https://github.com/Bilibili/flv.js) (recommended) for playing media streams.
70 |
71 | * [PCRE](http://www.pcre.org) for NGINX if regular expressions needed.
72 |
73 | * [OpenSSL](https://www.openssl.org) for NGINX if encrypted access needed.
74 |
75 | * [zlib](http://www.zlib.net) for NGINX if compression needed.
76 |
77 | ## Build
78 |
79 | ### Note
80 |
81 | nginx-http-flv-module has all features that [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) provides, so **DON'T** compile nginx-http-flv-module along with [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module).
82 |
83 | ### On Windows
84 |
85 | For details about build steps, please refer to [Building nginx on the Win32 platform with Visual C](http://nginx.org/en/docs/howto_build_on_win32.html), and don't forget to add `--add-module=/path/to/nginx-http-flv-module` in `Run configure script` step.
86 |
87 | #### Note
88 |
89 | If some compilers which do not support x64 perfectly, VS2010 for example, are used to compile the module, please make sure that the default settings are used (target machine type x86).
90 |
91 | ### On Unix-like systems
92 |
93 | Download [NGINX](http://nginx.org) and nginx-http-flv-module.
94 |
95 | Uncompress them.
96 |
97 | cd to NGINX source directory & run this:
98 |
99 | #### Compile the module into [NGINX](http://nginx.org)
100 |
101 | ./configure --add-module=/path/to/nginx-http-flv-module
102 | make
103 | make install
104 |
105 | or
106 |
107 | #### Compile the module as a dynamic module
108 |
109 | ./configure --add-dynamic-module=/path/to/nginx-http-flv-module
110 | make
111 | make install
112 |
113 | #### Note
114 |
115 | If the module is compiled as a dynamic module, the [NGINX](http://nginx.org) version **MUST** be equal to or greater than 1.9.11.
116 |
117 | ## Usage
118 |
119 | For details of usages of [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module), please refer to [README.md](https://github.com/arut/nginx-rtmp-module/blob/master/README.md).
120 |
121 | ### Publish
122 |
123 | For simplicity, transcoding is not used (so **-c copy** is used):
124 |
125 | ffmpeg -re -i MEDIA_FILE_NAME -c copy -f flv rtmp://example.com[:port]/appname/streamname
126 |
127 | #### Note
128 |
129 | Some legacy versions of [FFmpeg](http://ffmpeg.org) don't support the option `-c copy`, the options `-vcodec copy -acodec copy` can be used instead.
130 |
131 | The `appname` is used to match an application block in rtmp block (see below for details).
132 |
133 | The `streamname` can be specified at will but can **NOT** be omitted.
134 |
135 | The **default port for RTMP** is **1935**, if some other ports were used, `:port` must be specified.
136 |
137 | ### Play
138 |
139 | #### via HTTP-FLV
140 |
141 | http://example.com[:port]/dir?[port=xxx&]app=appname&stream=streamname
142 |
143 | #### Note
144 |
145 | * If [ffplay](http://www.ffmpeg.org/ffplay.html) is used in command line to play the stream, the url above **MUST** be enclosed by quotation marks, or arguments in url will be discarded (some shells not so smart will interpret "&" as "run in background").
146 |
147 | * If [flv.js](https://github.com/Bilibili/flv.js) is used to play the stream, make sure that the published stream is encoded properly, for [flv.js](https://github.com/Bilibili/flv.js) supports **ONLY H.264 encoded video and AAC/MP3 encoded audio**.
148 |
149 | The `dir` is used to match location blocks in http block (see below for details).
150 |
151 | The **default port for HTTP** is **80**, if some other ports were used, `:port` must be specified.
152 |
153 | The **default port for RTMP** is **1935**, if some other ports were used, `port=xxx` must be specified.
154 |
155 | The value of `app` (appname) is used to match an application block, but if the requested `app` appears in several server blocks and those blocks have the same address and port configuration, host name matches `server_name` directive will be additionally used to identify the requested application block, otherwise the first one is matched.
156 |
157 | The value of `stream` (streamname) is used to match the name of published stream.
158 |
159 | #### Example
160 |
161 | Assume that `listen` directive specified in `http` block is:
162 |
163 | http {
164 | ...
165 | server {
166 | listen 8080; #not default port 80
167 | ...
168 |
169 | location /live {
170 | flv_live on;
171 | }
172 | }
173 | }
174 |
175 | And `listen` directive specified in `rtmp` block is:
176 |
177 | rtmp {
178 | ...
179 | server {
180 | listen 1985; #not default port 1935
181 | ...
182 |
183 | application myapp {
184 | live on;
185 | }
186 | }
187 | }
188 |
189 | And the name of published stream is `mystream`, then the url of playback based on HTTP is:
190 |
191 | http://example.com:8080/live?port=1985&app=myapp&stream=mystream
192 |
193 | #### Note
194 |
195 | Since some players don't support HTTP chunked transmission, it's better to specify `chunked_transfer_encoding off;` in location where `flv_live on;` is specified in this case, or play will fail.
196 |
197 | #### via RTMP
198 |
199 | rtmp://example.com[:port]/appname/streamname
200 |
201 | #### via HLS
202 |
203 | http://example.com[:port]/dir/streamname.m3u8
204 |
205 | #### via DASH
206 |
207 | http://example.com[:port]/dir/streamname.mpd
208 |
209 | ## Sample Pictures
210 |
211 | ### RTMP ([JW Player](https://www.jwplayer.com)) & HTTP-FLV ([VLC](http://www.videolan.org))
212 |
213 | 
214 |
215 | ### HTTP-FLV ([flv.js](https://github.com/Bilibili/flv.js))
216 |
217 | 
218 |
219 | ## Example nginx.conf
220 |
221 | ### Note
222 |
223 | The directives `rtmp_auto_push`, `rtmp_auto_push_reconnect` and `rtmp_socket_dir` will not function on Windows except on Windows 10 17063 and later versions, because `relay` in multiple processes mode needs help of Unix domain socket, please refer to [Unix domain socket on Windows 10](https://blogs.msdn.microsoft.com/commandline/2017/12/19/af_unix-comes-to-windows) for details.
224 |
225 | It's better to specify the directive `worker_processes` as 1, because `ngx_rtmp_stat_module` may not get statistics from a specified worker process in multi-processes mode, for HTTP requests are randomly distributed to worker processes. `ngx_rtmp_control_module` has the same problem. The problem can be optimized by this patch [per-worker-listener](https://github.com/arut/nginx-patches/blob/master/per-worker-listener).
226 |
227 | In addtion, `vhost` feature is OK in single process mode but not perfect in multi-processes mode yet, waiting to be fixed. For example, the following configuration is OK in multi-processes mode:
228 |
229 | rtmp {
230 | ...
231 | server {
232 | listen 1935;
233 | server_name domain_name;
234 |
235 | application myapp {
236 | ...
237 | }
238 | }
239 | }
240 |
241 | While the following configuration doesn't work properly for play requests distinated to the second `server` (whether port is 1935 or not) of non-publisher worker processes:
242 |
243 | rtmp {
244 | ...
245 | server {
246 | listen 1935;
247 | server_name 1st_domain_name;
248 |
249 | application myapp {
250 | ...
251 | }
252 | }
253 |
254 | server {
255 | listen 1945;
256 | server_name 2nd_domain_name;
257 |
258 | application myapp {
259 | ...
260 | }
261 | }
262 | }
263 |
264 | If [NGINX](http://nginx.org) is running in muti-processes mode and socket option `SO_REUSEPORT` is supported by platform, adding option `reuseport` for the directive `listen` will resolve the thundering herd problem.
265 |
266 | rtmp {
267 | ...
268 |
269 | server {
270 | listen 1935 reuseport;
271 | ...
272 | }
273 | }
274 |
275 | ### Example configuration
276 |
277 | worker_processes 1; #should be 1 for Windows, for it doesn't support Unix domain socket
278 | #worker_processes auto; #from versions 1.3.8 and 1.2.5
279 |
280 | #worker_cpu_affinity 0001 0010 0100 1000; #only available on FreeBSD and Linux
281 | #worker_cpu_affinity auto; #from version 1.9.10
282 |
283 | error_log logs/error.log error;
284 |
285 | #if the module is compiled as a dynamic module and features relevant
286 | #to RTMP are needed, the command below MUST be specified and MUST be
287 | #located before events directive, otherwise the module won't be loaded
288 | #or will be loaded unsuccessfully when NGINX is started
289 |
290 | #load_module modules/ngx_http_flv_live_module.so;
291 |
292 | events {
293 | worker_connections 4096;
294 | }
295 |
296 | http {
297 | include mime.types;
298 | default_type application/octet-stream;
299 |
300 | keepalive_timeout 65;
301 |
302 | server {
303 | listen 80;
304 |
305 | location / {
306 | root /var/www;
307 | index index.html index.htm;
308 | }
309 |
310 | error_page 500 502 503 504 /50x.html;
311 | location = /50x.html {
312 | root html;
313 | }
314 |
315 | location /live {
316 | flv_live on; #open flv live streaming (subscribe)
317 | chunked_transfer_encoding on; #open 'Transfer-Encoding: chunked' response
318 |
319 | add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
320 | add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
321 | }
322 |
323 | location /hls {
324 | types {
325 | application/vnd.apple.mpegurl m3u8;
326 | video/mp2t ts;
327 | }
328 |
329 | root /tmp;
330 | add_header 'Cache-Control' 'no-cache';
331 | }
332 |
333 | location /dash {
334 | root /tmp;
335 | add_header 'Cache-Control' 'no-cache';
336 | }
337 |
338 | location /stat {
339 | #configuration of streaming & recording statistics
340 |
341 | rtmp_stat all;
342 | rtmp_stat_stylesheet stat.xsl;
343 | }
344 |
345 | location /stat.xsl {
346 | root /var/www/rtmp; #specify in where stat.xsl located
347 | }
348 |
349 | #if JSON style stat needed, no need to specify
350 | #stat.xsl but a new directive rtmp_stat_format
351 |
352 | #location /stat {
353 | # rtmp_stat all;
354 | # rtmp_stat_format json;
355 | #}
356 |
357 | location /control {
358 | rtmp_control all; #configuration of control module of rtmp
359 | }
360 | }
361 | }
362 |
363 | rtmp_auto_push on;
364 | rtmp_auto_push_reconnect 1s;
365 | rtmp_socket_dir /tmp;
366 |
367 | rtmp {
368 | out_queue 4096;
369 | out_cork 8;
370 | max_streams 128;
371 | timeout 15s;
372 | drop_idle_publisher 15s;
373 |
374 | log_interval 5s; #interval used by log module to log in access.log, it is very useful for debug
375 | log_size 1m; #buffer size used by log module to log in access.log
376 |
377 | server {
378 | listen 1935;
379 | server_name www.test.*; #for suffix wildcard matching of virtual host name
380 |
381 | application myapp {
382 | live on;
383 | gop_cache on; #open GOP cache for reducing the wating time for the first picture of video
384 | }
385 |
386 | application hls {
387 | live on;
388 | hls on;
389 | hls_path /tmp/hls;
390 | }
391 |
392 | application dash {
393 | live on;
394 | dash on;
395 | dash_path /tmp/dash;
396 | }
397 | }
398 |
399 | server {
400 | listen 1935;
401 | server_name *.test.com; #for prefix wildcard matching of virtual host name
402 |
403 | application myapp {
404 | live on;
405 | gop_cache on; #open GOP cache for reducing the wating time for the first picture of video
406 | }
407 | }
408 |
409 | server {
410 | listen 1935;
411 | server_name www.test.com; #for completely matching of virtual host name
412 |
413 | application myapp {
414 | live on;
415 | gop_cache on; #open GOP cache for reducing the wating time for the first picture of video
416 | }
417 | }
418 | }
419 |
--------------------------------------------------------------------------------
/config:
--------------------------------------------------------------------------------
1 | ngx_addon_name="ngx_http_flv_live_module"
2 |
3 | RTMP_CORE_MODULES=" \
4 | ngx_rtmp_module \
5 | ngx_rtmp_core_module \
6 | ngx_rtmp_cmd_module \
7 | ngx_rtmp_gop_cache_module \
8 | ngx_rtmp_codec_module \
9 | ngx_rtmp_access_module \
10 | ngx_rtmp_record_module \
11 | ngx_rtmp_live_module \
12 | ngx_rtmp_flv_live_index_module \
13 | ngx_rtmp_play_module \
14 | ngx_rtmp_flv_module \
15 | ngx_rtmp_mp4_module \
16 | ngx_rtmp_netcall_module \
17 | ngx_rtmp_relay_module \
18 | ngx_rtmp_exec_module \
19 | ngx_rtmp_auto_push_module \
20 | ngx_rtmp_auto_push_index_module \
21 | ngx_rtmp_log_module \
22 | ngx_rtmp_limit_module \
23 | ngx_rtmp_hls_module \
24 | ngx_rtmp_dash_module \
25 | ngx_rtmp_notify_module \
26 | "
27 |
28 |
29 | RTMP_HTTP_MODULES=" \
30 | ngx_rtmp_stat_module \
31 | ngx_rtmp_control_module \
32 | ngx_http_flv_live_module \
33 | "
34 |
35 |
36 | RTMP_DEPS=" \
37 | $ngx_addon_dir/ngx_rtmp_amf.h \
38 | $ngx_addon_dir/ngx_rtmp_bandwidth.h \
39 | $ngx_addon_dir/ngx_rtmp_cmd_module.h \
40 | $ngx_addon_dir/ngx_rtmp_gop_cache_module.h \
41 | $ngx_addon_dir/ngx_rtmp_codec_module.h \
42 | $ngx_addon_dir/ngx_rtmp_eval.h \
43 | $ngx_addon_dir/ngx_rtmp.h \
44 | $ngx_addon_dir/ngx_rtmp_version.h \
45 | $ngx_addon_dir/ngx_rtmp_live_module.h \
46 | $ngx_addon_dir/ngx_rtmp_netcall_module.h \
47 | $ngx_addon_dir/ngx_rtmp_play_module.h \
48 | $ngx_addon_dir/ngx_rtmp_record_module.h \
49 | $ngx_addon_dir/ngx_rtmp_relay_module.h \
50 | $ngx_addon_dir/ngx_rtmp_streams.h \
51 | $ngx_addon_dir/ngx_rtmp_bitop.h \
52 | $ngx_addon_dir/ngx_rtmp_proxy_protocol.h \
53 | $ngx_addon_dir/ngx_rtmp_variables.h \
54 | $ngx_addon_dir/hls/ngx_rtmp_hls_module.h \
55 | $ngx_addon_dir/hls/ngx_rtmp_mpegts.h \
56 | $ngx_addon_dir/hls/ngx_rtmp_mpegts_crc.h \
57 | $ngx_addon_dir/dash/ngx_rtmp_mp4.h \
58 | "
59 |
60 |
61 | RTMP_CORE_SRCS=" \
62 | $ngx_addon_dir/ngx_rtmp.c \
63 | $ngx_addon_dir/ngx_rtmp_init.c \
64 | $ngx_addon_dir/ngx_rtmp_handshake.c \
65 | $ngx_addon_dir/ngx_rtmp_handler.c \
66 | $ngx_addon_dir/ngx_rtmp_amf.c \
67 | $ngx_addon_dir/ngx_rtmp_send.c \
68 | $ngx_addon_dir/ngx_rtmp_shared.c \
69 | $ngx_addon_dir/ngx_rtmp_eval.c \
70 | $ngx_addon_dir/ngx_rtmp_receive.c \
71 | $ngx_addon_dir/ngx_rtmp_core_module.c \
72 | $ngx_addon_dir/ngx_rtmp_cmd_module.c \
73 | $ngx_addon_dir/ngx_rtmp_gop_cache_module.c \
74 | $ngx_addon_dir/ngx_rtmp_codec_module.c \
75 | $ngx_addon_dir/ngx_rtmp_access_module.c \
76 | $ngx_addon_dir/ngx_rtmp_record_module.c \
77 | $ngx_addon_dir/ngx_rtmp_live_module.c \
78 | $ngx_addon_dir/ngx_rtmp_flv_live_index_module.c \
79 | $ngx_addon_dir/ngx_rtmp_play_module.c \
80 | $ngx_addon_dir/ngx_rtmp_flv_module.c \
81 | $ngx_addon_dir/ngx_rtmp_mp4_module.c \
82 | $ngx_addon_dir/ngx_rtmp_netcall_module.c \
83 | $ngx_addon_dir/ngx_rtmp_relay_module.c \
84 | $ngx_addon_dir/ngx_rtmp_bandwidth.c \
85 | $ngx_addon_dir/ngx_rtmp_exec_module.c \
86 | $ngx_addon_dir/ngx_rtmp_auto_push_module.c \
87 | $ngx_addon_dir/ngx_rtmp_notify_module.c \
88 | $ngx_addon_dir/ngx_rtmp_log_module.c \
89 | $ngx_addon_dir/ngx_rtmp_limit_module.c \
90 | $ngx_addon_dir/ngx_rtmp_bitop.c \
91 | $ngx_addon_dir/ngx_rtmp_proxy_protocol.c \
92 | $ngx_addon_dir/ngx_rtmp_variables.c \
93 | $ngx_addon_dir/ngx_rtmp_parse.c \
94 | $ngx_addon_dir/hls/ngx_rtmp_hls_module.c \
95 | $ngx_addon_dir/dash/ngx_rtmp_dash_module.c \
96 | $ngx_addon_dir/hls/ngx_rtmp_mpegts.c \
97 | $ngx_addon_dir/hls/ngx_rtmp_mpegts_crc.c \
98 | $ngx_addon_dir/dash/ngx_rtmp_mp4.c \
99 | "
100 |
101 |
102 | RTMP_HTTP_DEPS=" \
103 | $ngx_addon_dir/ngx_http_flv_live_module.h \
104 | "
105 |
106 |
107 | RTMP_HTTP_SRCS=" \
108 | $ngx_addon_dir/ngx_rtmp_stat_module.c \
109 | $ngx_addon_dir/ngx_rtmp_control_module.c \
110 | $ngx_addon_dir/ngx_http_flv_live_module.c \
111 | "
112 |
113 | if [ -f auto/module ] ; then
114 | ngx_module_incs=$ngx_addon_dir
115 | ngx_module_deps="$RTMP_DEPS $RTMP_HTTP_DEPS"
116 |
117 | if [ $ngx_module_link = DYNAMIC ] ; then
118 | ngx_module_name="$ngx_addon_name $RTMP_CORE_MODULES $RTMP_HTTP_MODULES"
119 | ngx_module_srcs="$RTMP_CORE_SRCS $RTMP_HTTP_SRCS"
120 |
121 | . auto/module
122 |
123 | dynamic_modules=`eval echo '$'"${ngx_module}_MODULES" | sed -e "s/ \{0,\}$ngx_addon_name//"`
124 | eval ${ngx_module}_MODULES=\"$dynamic_modules\"
125 | unset dynamic_modules
126 |
127 | order_modules=`eval echo '$'"${ngx_module}_ORDER"`
128 | if [ -n "$order_modules" ]
129 | then
130 | eval ${ngx_module}_ORDER=\"`echo "$order_modules" | sed -e "s/ \{0,\}$ngx_addon_name//"`\"
131 | unset order_modules
132 | fi
133 | else
134 | ngx_module_type=CORE
135 | ngx_module_name=$RTMP_CORE_MODULES
136 | ngx_module_deps=$RTMP_DEPS
137 | ngx_module_srcs=$RTMP_CORE_SRCS
138 |
139 | . auto/module
140 |
141 |
142 | ngx_module_type=HTTP
143 | ngx_module_name=$RTMP_HTTP_MODULES
144 | ngx_module_deps=$RTMP_HTTP_DEPS
145 | ngx_module_srcs=$RTMP_HTTP_SRCS
146 |
147 | . auto/module
148 | fi
149 |
150 | else
151 | CORE_MODULES="$CORE_MODULES $RTMP_CORE_MODULES"
152 | HTTP_MODULES="$HTTP_MODULES $RTMP_HTTP_MODULES"
153 |
154 | NGX_ADDON_DEPS="$NGX_ADDON_DEPS $RTMP_DEPS $RTMP_HTTP_DEPS"
155 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $RTMP_CORE_SRCS $RTMP_HTTP_SRCS"
156 |
157 | CFLAGS="$CFLAGS -I$ngx_addon_dir"
158 | fi
159 |
160 | USE_OPENSSL=YES
161 |
162 |
--------------------------------------------------------------------------------
/dash/ngx_rtmp_mp4.h:
--------------------------------------------------------------------------------
1 |
2 |
3 | #ifndef _NGX_RTMP_MP4_H_INCLUDED_
4 | #define _NGX_RTMP_MP4_H_INCLUDED_
5 |
6 |
7 | #include
8 | #include
9 | #include
10 |
11 |
12 | #define NGX_RTMP_MP4_SAMPLE_SIZE 0x01
13 | #define NGX_RTMP_MP4_SAMPLE_DURATION 0x02
14 | #define NGX_RTMP_MP4_SAMPLE_DELAY 0x04
15 | #define NGX_RTMP_MP4_SAMPLE_KEY 0x08
16 |
17 |
18 | typedef struct {
19 | uint32_t size;
20 | uint32_t duration;
21 | uint32_t delay;
22 | uint32_t timestamp;
23 | unsigned key:1;
24 | } ngx_rtmp_mp4_sample_t;
25 |
26 |
27 | typedef enum {
28 | NGX_RTMP_MP4_FILETYPE_INIT,
29 | NGX_RTMP_MP4_FILETYPE_SEG
30 | } ngx_rtmp_mp4_file_type_t;
31 |
32 |
33 | typedef enum {
34 | NGX_RTMP_MP4_VIDEO_TRACK,
35 | NGX_RTMP_MP4_AUDIO_TRACK
36 | } ngx_rtmp_mp4_track_type_t;
37 |
38 |
39 | ngx_int_t ngx_rtmp_mp4_write_ftyp(ngx_buf_t *b);
40 | ngx_int_t ngx_rtmp_mp4_write_styp(ngx_buf_t *b);
41 | ngx_int_t ngx_rtmp_mp4_write_moov(ngx_rtmp_session_t *s, ngx_buf_t *b,
42 | ngx_rtmp_mp4_track_type_t ttype);
43 | ngx_int_t ngx_rtmp_mp4_write_moof(ngx_buf_t *b, uint32_t earliest_pres_time,
44 | uint32_t sample_count, ngx_rtmp_mp4_sample_t *samples,
45 | ngx_uint_t sample_mask, uint32_t index);
46 | ngx_int_t ngx_rtmp_mp4_write_sidx(ngx_buf_t *b,
47 | ngx_uint_t reference_size, uint32_t earliest_pres_time,
48 | uint32_t latest_pres_time);
49 | ngx_uint_t ngx_rtmp_mp4_write_mdat(ngx_buf_t *b, ngx_uint_t size);
50 |
51 |
52 | #endif /* _NGX_RTMP_MP4_H_INCLUDED_ */
53 |
--------------------------------------------------------------------------------
/doc/README.md:
--------------------------------------------------------------------------------
1 | Documentation is available here:
2 | https://github.com/arut/nginx-rtmp-module/wiki
3 |
--------------------------------------------------------------------------------
/hls/ngx_rtmp_hls_module.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Winshining
4 | */
5 |
6 |
7 | #ifndef _NGX_RTMP_HLS_MODULE_H_
8 | #define _NGX_RTMP_HLS_MODULE_H_
9 |
10 |
11 | ngx_int_t ngx_rtmp_hls_copy(ngx_rtmp_session_t *s, void *dst, u_char **src,
12 | size_t n, ngx_chain_t **in);
13 |
14 |
15 | #endif /* _NGX_RTMP_HLS_MODULE_H_ */
16 |
--------------------------------------------------------------------------------
/hls/ngx_rtmp_mpegts.c:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | * Copyright (C) Winshining
5 | */
6 |
7 |
8 | #include
9 | #include
10 | #include "ngx_rtmp_mpegts.h"
11 | #include "ngx_rtmp_mpegts_crc.h"
12 |
13 |
14 | static u_char ngx_rtmp_mpegts_header[] = {
15 |
16 | /* TS */
17 | 0x47, 0x40, 0x00, 0x10, 0x00,
18 | /* PSI */
19 | 0x00, 0xb0, 0x0d, 0x00, 0x01, 0xc1, 0x00, 0x00,
20 | /* PAT */
21 | 0x00, 0x01, 0xf0, 0x01,
22 | /* CRC */
23 | 0x2e, 0x70, 0x19, 0x05,
24 | /* stuffing 167 bytes */
25 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
26 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
27 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
28 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
29 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
30 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
31 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
32 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
33 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
34 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
35 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
36 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
37 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
38 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
40 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 |
43 | /* TS */
44 | 0x47, 0x50, 0x01, 0x10, 0x00,
45 | /* PSI */
46 | 0x02, 0xb0, 0x17, 0x00, 0x01, 0xc1, 0x00, 0x00,
47 | /* PMT */
48 | 0xe1, 0x00,
49 | 0xf0, 0x00,
50 | 0xff, 0xff, 0xff, 0xff, 0xff, /* video, filled dynamically */
51 | 0xff, 0xff, 0xff, 0xff, 0xff, /* audio, filled dynamically */
52 | /* CRC */
53 | 0xff, 0xff, 0xff, 0xff, /* calculated dynamically */
54 | /* stuffing 157 bytes */
55 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
58 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
59 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
63 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
71 | };
72 |
73 |
74 | static u_char ngx_rtmp_mpegts_h264_header[] = {
75 | 0x1b, 0xe1, 0x00, 0xf0, 0x00
76 | };
77 |
78 |
79 | static u_char ngx_rtmp_mpegts_aac_header[] = {
80 | 0x0f, 0xe1, 0x01, 0xf0, 0x00
81 | };
82 |
83 |
84 | static u_char ngx_rtmp_mpegts_mp3_header[] = {
85 | 0x03, 0xe1, 0x01, 0xf0, 0x00
86 | };
87 |
88 |
89 | #define NGX_RTMP_MPEGTS_PMT_CRC_START_OFFSET 193
90 | #define NGX_RTMP_MPEGTS_PMT_SECTION_LENGTH_OFFSET 195
91 | #define NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET 205
92 | #define NGX_RTMP_MPEGTS_PMT_CRC_MIN_LENGTH 12
93 | #define NGX_RTMP_MPEGTS_STREAM_BYTES 5
94 |
95 |
96 | /* 700 ms PCR delay */
97 | #define NGX_RTMP_HLS_DELAY 63000
98 |
99 |
100 | static ngx_int_t
101 | ngx_rtmp_mpegts_write_file(ngx_rtmp_mpegts_file_t *file, u_char *in,
102 | size_t in_size)
103 | {
104 | u_char *out;
105 | size_t out_size, n;
106 | ssize_t rc;
107 |
108 | static u_char buf[1024];
109 |
110 | if (!file->encrypt) {
111 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
112 | "mpegts: write %uz bytes", in_size);
113 |
114 | rc = ngx_write_fd(file->fd, in, in_size);
115 | if (rc < 0) {
116 | return NGX_ERROR;
117 | }
118 |
119 | return NGX_OK;
120 | }
121 |
122 | /* encrypt */
123 |
124 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
125 | "mpegts: write %uz encrypted bytes", in_size);
126 |
127 | out = buf;
128 | out_size = sizeof(buf);
129 |
130 | if (file->size > 0 && file->size + in_size >= 16) {
131 | ngx_memcpy(file->buf + file->size, in, 16 - file->size);
132 |
133 | in += 16 - file->size;
134 | in_size -= 16 - file->size;
135 |
136 | AES_cbc_encrypt(file->buf, out, 16, &file->key, file->iv, AES_ENCRYPT);
137 |
138 | out += 16;
139 | out_size -= 16;
140 |
141 | file->size = 0;
142 | }
143 |
144 | for ( ;; ) {
145 | n = in_size & ~0x0f;
146 |
147 | if (n > 0) {
148 | if (n > out_size) {
149 | n = out_size;
150 | }
151 |
152 | AES_cbc_encrypt(in, out, n, &file->key, file->iv, AES_ENCRYPT);
153 |
154 | in += n;
155 | in_size -= n;
156 |
157 | } else if (out == buf) {
158 | break;
159 | }
160 |
161 | rc = ngx_write_fd(file->fd, buf, out - buf + n);
162 | if (rc < 0) {
163 | return NGX_ERROR;
164 | }
165 |
166 | out = buf;
167 | out_size = sizeof(buf);
168 | }
169 |
170 | if (in_size) {
171 | ngx_memcpy(file->buf + file->size, in, in_size);
172 | file->size += in_size;
173 | }
174 |
175 | return NGX_OK;
176 | }
177 |
178 |
179 | static ngx_int_t
180 | ngx_rtmp_mpegts_write_header(ngx_rtmp_mpegts_file_t *file,
181 | ngx_rtmp_codec_ctx_t *codec_ctx, ngx_uint_t counter)
182 | {
183 | ngx_int_t stream_bytes;
184 | ngx_rtmp_mpegts_crc_t crc;
185 | u_char buf[sizeof(ngx_rtmp_mpegts_header)];
186 |
187 | if (codec_ctx->video_codec_id == 0 && codec_ctx->audio_codec_id == 0) {
188 | return NGX_ERROR;
189 | }
190 |
191 | stream_bytes = 0;
192 | ngx_memcpy(buf, ngx_rtmp_mpegts_header, sizeof(ngx_rtmp_mpegts_header));
193 |
194 | /* 4 bits, periodical */
195 | counter %= 0x10;
196 | /* fill headers */
197 | buf[3] = (buf[3] & 0xf0) + (u_char) counter;
198 | buf[191] = (buf[191] & 0xf0) + (u_char) counter;
199 |
200 | if (codec_ctx->video_codec_id) {
201 | /* video info */
202 | ngx_memcpy(buf + NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET + stream_bytes,
203 | ngx_rtmp_mpegts_h264_header, NGX_RTMP_MPEGTS_STREAM_BYTES);
204 |
205 | stream_bytes += NGX_RTMP_MPEGTS_STREAM_BYTES;
206 | }
207 |
208 | if (codec_ctx->audio_codec_id) {
209 | /* audio info */
210 | if (codec_ctx->audio_codec_id == NGX_RTMP_AUDIO_AAC) {
211 | ngx_memcpy(buf + NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET + stream_bytes,
212 | ngx_rtmp_mpegts_aac_header, NGX_RTMP_MPEGTS_STREAM_BYTES);
213 | } else {
214 | ngx_memcpy(buf + NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET + stream_bytes,
215 | ngx_rtmp_mpegts_mp3_header, NGX_RTMP_MPEGTS_STREAM_BYTES);
216 | }
217 |
218 | stream_bytes += NGX_RTMP_MPEGTS_STREAM_BYTES;
219 | }
220 |
221 | /* calculate section length */
222 | buf[NGX_RTMP_MPEGTS_PMT_SECTION_LENGTH_OFFSET] = 13 + stream_bytes;
223 |
224 | /* calculate CRC */
225 | crc = ngx_rtmp_mpegts_crc_init();
226 | crc = ngx_rtmp_mpegts_crc_update(crc,
227 | buf + NGX_RTMP_MPEGTS_PMT_CRC_START_OFFSET,
228 | NGX_RTMP_MPEGTS_PMT_CRC_MIN_LENGTH + stream_bytes);
229 | crc = ngx_rtmp_mpegts_crc_finalize(crc);
230 |
231 | buf[NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET + stream_bytes] = (crc >> 24) & 0xff;
232 | buf[NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET + stream_bytes + 1] = (crc >> 16) & 0xff;
233 | buf[NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET + stream_bytes + 2] = (crc >> 8) & 0xff;
234 | buf[NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET + stream_bytes + 3] = crc & 0xff;
235 |
236 | return ngx_rtmp_mpegts_write_file(file, buf, sizeof(buf));
237 | }
238 |
239 |
240 | static u_char *
241 | ngx_rtmp_mpegts_write_pcr(u_char *p, uint64_t pcr)
242 | {
243 | *p++ = (u_char) (pcr >> 25);
244 | *p++ = (u_char) (pcr >> 17);
245 | *p++ = (u_char) (pcr >> 9);
246 | *p++ = (u_char) (pcr >> 1);
247 | *p++ = (u_char) (pcr << 7 | 0x7e);
248 | *p++ = 0;
249 |
250 | return p;
251 | }
252 |
253 |
254 | static u_char *
255 | ngx_rtmp_mpegts_write_pts(u_char *p, ngx_uint_t fb, uint64_t pts)
256 | {
257 | ngx_uint_t val;
258 |
259 | val = fb << 4 | (((pts >> 30) & 0x07) << 1) | 1;
260 | *p++ = (u_char) val;
261 |
262 | val = (((pts >> 15) & 0x7fff) << 1) | 1;
263 | *p++ = (u_char) (val >> 8);
264 | *p++ = (u_char) val;
265 |
266 | val = (((pts) & 0x7fff) << 1) | 1;
267 | *p++ = (u_char) (val >> 8);
268 | *p++ = (u_char) val;
269 |
270 | return p;
271 | }
272 |
273 |
274 | ngx_int_t
275 | ngx_rtmp_mpegts_write_frame(ngx_rtmp_mpegts_file_t *file,
276 | ngx_rtmp_mpegts_frame_t *f, ngx_buf_t *b)
277 | {
278 | ngx_uint_t pes_size, header_size, body_size, in_size, stuff_size, flags;
279 | u_char packet[188], *p, *base;
280 | ngx_int_t first, rc;
281 |
282 | ngx_log_debug6(NGX_LOG_DEBUG_CORE, file->log, 0,
283 | "mpegts: pid=%ui, sid=%ui, pts=%uL, "
284 | "dts=%uL, key=%ui, size=%ui",
285 | f->pid, f->sid, f->pts, f->dts,
286 | (ngx_uint_t) f->key, (size_t) (b->last - b->pos));
287 |
288 | first = 1;
289 |
290 | while (b->pos < b->last) {
291 | p = packet;
292 |
293 | f->cc++;
294 |
295 | *p++ = 0x47;
296 | *p++ = (u_char) (f->pid >> 8);
297 |
298 | if (first) {
299 | p[-1] |= 0x40;
300 | }
301 |
302 | *p++ = (u_char) f->pid;
303 | *p++ = 0x10 | (f->cc & 0x0f); /* payload */
304 |
305 | if (first) {
306 |
307 | if (f->key) {
308 | packet[3] |= 0x20; /* adaptation */
309 |
310 | *p++ = 7; /* size */
311 | *p++ = 0x50; /* random access + PCR */
312 |
313 | p = ngx_rtmp_mpegts_write_pcr(p, f->dts - NGX_RTMP_HLS_DELAY);
314 | }
315 |
316 | /* PES header */
317 |
318 | *p++ = 0x00;
319 | *p++ = 0x00;
320 | *p++ = 0x01;
321 | *p++ = (u_char) f->sid;
322 |
323 | header_size = 5;
324 | flags = 0x80; /* PTS */
325 |
326 | if (f->dts != f->pts) {
327 | header_size += 5;
328 | flags |= 0x40; /* DTS */
329 | }
330 |
331 | pes_size = (b->last - b->pos) + header_size + 3;
332 | if (pes_size > 0xffff) {
333 | pes_size = 0;
334 | }
335 |
336 | *p++ = (u_char) (pes_size >> 8);
337 | *p++ = (u_char) pes_size;
338 | *p++ = 0x80; /* H222 */
339 | *p++ = (u_char) flags;
340 | *p++ = (u_char) header_size;
341 |
342 | p = ngx_rtmp_mpegts_write_pts(p, flags >> 6, f->pts +
343 | NGX_RTMP_HLS_DELAY);
344 |
345 | if (f->dts != f->pts) {
346 | p = ngx_rtmp_mpegts_write_pts(p, 1, f->dts +
347 | NGX_RTMP_HLS_DELAY);
348 | }
349 |
350 | first = 0;
351 | }
352 |
353 | body_size = (ngx_uint_t) (packet + sizeof(packet) - p);
354 | in_size = (ngx_uint_t) (b->last - b->pos);
355 |
356 | if (body_size <= in_size) {
357 | ngx_memcpy(p, b->pos, body_size);
358 | b->pos += body_size;
359 |
360 | } else {
361 | stuff_size = (body_size - in_size);
362 |
363 | if (packet[3] & 0x20) {
364 |
365 | /* has adaptation */
366 |
367 | base = &packet[5] + packet[4];
368 | p = ngx_movemem(base + stuff_size, base, p - base);
369 | ngx_memset(base, 0xff, stuff_size);
370 | packet[4] += (u_char) stuff_size;
371 |
372 | } else {
373 |
374 | /* no adaptation */
375 |
376 | packet[3] |= 0x20;
377 | p = ngx_movemem(&packet[4] + stuff_size, &packet[4],
378 | p - &packet[4]);
379 |
380 | packet[4] = (u_char) (stuff_size - 1);
381 | if (stuff_size >= 2) {
382 | packet[5] = 0;
383 | ngx_memset(&packet[6], 0xff, stuff_size - 2);
384 | }
385 | }
386 |
387 | ngx_memcpy(p, b->pos, in_size);
388 | b->pos = b->last;
389 | }
390 |
391 | rc = ngx_rtmp_mpegts_write_file(file, packet, sizeof(packet));
392 | if (rc != NGX_OK) {
393 | return rc;
394 | }
395 | }
396 |
397 | return NGX_OK;
398 | }
399 |
400 |
401 | ngx_int_t
402 | ngx_rtmp_mpegts_init_encryption(ngx_rtmp_mpegts_file_t *file,
403 | u_char *key, size_t key_len, uint64_t iv)
404 | {
405 | if (AES_set_encrypt_key(key, key_len * 8, &file->key)) {
406 | return NGX_ERROR;
407 | }
408 |
409 | ngx_memzero(file->iv, 8);
410 |
411 | file->iv[8] = (u_char) (iv >> 56);
412 | file->iv[9] = (u_char) (iv >> 48);
413 | file->iv[10] = (u_char) (iv >> 40);
414 | file->iv[11] = (u_char) (iv >> 32);
415 | file->iv[12] = (u_char) (iv >> 24);
416 | file->iv[13] = (u_char) (iv >> 16);
417 | file->iv[14] = (u_char) (iv >> 8);
418 | file->iv[15] = (u_char) (iv);
419 |
420 | file->encrypt = 1;
421 |
422 | return NGX_OK;
423 | }
424 |
425 |
426 | ngx_int_t
427 | ngx_rtmp_mpegts_open_file(ngx_rtmp_mpegts_file_t *file, u_char *path,
428 | ngx_rtmp_codec_ctx_t *codec_ctx, ngx_uint_t counter, ngx_log_t *log)
429 | {
430 | file->log = log;
431 |
432 | file->fd = ngx_open_file(path, NGX_FILE_WRONLY, NGX_FILE_TRUNCATE,
433 | NGX_FILE_DEFAULT_ACCESS);
434 |
435 | if (file->fd == NGX_INVALID_FILE) {
436 | ngx_log_error(NGX_LOG_ERR, log, ngx_errno,
437 | "hls: error creating fragment file");
438 | return NGX_ERROR;
439 | }
440 |
441 | file->size = 0;
442 |
443 | if (ngx_rtmp_mpegts_write_header(file, codec_ctx, counter) != NGX_OK) {
444 | ngx_log_error(NGX_LOG_ERR, log, ngx_errno,
445 | "hls: error writing fragment header");
446 | ngx_close_file(file->fd);
447 | return NGX_ERROR;
448 | }
449 |
450 | return NGX_OK;
451 | }
452 |
453 |
454 | ngx_int_t
455 | ngx_rtmp_mpegts_close_file(ngx_rtmp_mpegts_file_t *file)
456 | {
457 | u_char buf[16];
458 | ssize_t rc;
459 |
460 | if (file->encrypt) {
461 | ngx_memset(file->buf + file->size, 16 - file->size, 16 - file->size);
462 |
463 | AES_cbc_encrypt(file->buf, buf, 16, &file->key, file->iv, AES_ENCRYPT);
464 |
465 | rc = ngx_write_fd(file->fd, buf, 16);
466 | if (rc < 0) {
467 | return NGX_ERROR;
468 | }
469 | }
470 |
471 | ngx_close_file(file->fd);
472 |
473 | return NGX_OK;
474 | }
475 |
--------------------------------------------------------------------------------
/hls/ngx_rtmp_mpegts.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | * Copyright (C) Winshining
5 | */
6 |
7 |
8 | #ifndef _NGX_RTMP_MPEGTS_H_INCLUDED_
9 | #define _NGX_RTMP_MPEGTS_H_INCLUDED_
10 |
11 |
12 | #include
13 | #include
14 | #include
15 | #include "ngx_rtmp_codec_module.h"
16 |
17 |
18 | typedef struct {
19 | ngx_fd_t fd;
20 | ngx_log_t *log;
21 | unsigned encrypt:1;
22 | unsigned size:4;
23 | u_char buf[16];
24 | u_char iv[16];
25 | AES_KEY key;
26 | } ngx_rtmp_mpegts_file_t;
27 |
28 |
29 | typedef struct {
30 | uint64_t pts;
31 | uint64_t dts;
32 | ngx_uint_t pid;
33 | ngx_uint_t sid;
34 | ngx_uint_t cc;
35 | unsigned key:1;
36 | } ngx_rtmp_mpegts_frame_t;
37 |
38 |
39 | ngx_int_t ngx_rtmp_mpegts_init_encryption(ngx_rtmp_mpegts_file_t *file,
40 | u_char *key, size_t key_len, uint64_t iv);
41 | ngx_int_t ngx_rtmp_mpegts_open_file(ngx_rtmp_mpegts_file_t *file, u_char *path,
42 | ngx_rtmp_codec_ctx_t *codec_ctx, ngx_uint_t counter, ngx_log_t *log);
43 | ngx_int_t ngx_rtmp_mpegts_close_file(ngx_rtmp_mpegts_file_t *file);
44 | ngx_int_t ngx_rtmp_mpegts_write_frame(ngx_rtmp_mpegts_file_t *file,
45 | ngx_rtmp_mpegts_frame_t *f, ngx_buf_t *b);
46 |
47 |
48 | #endif /* _NGX_RTMP_MPEGTS_H_INCLUDED_ */
49 |
--------------------------------------------------------------------------------
/hls/ngx_rtmp_mpegts_crc.c:
--------------------------------------------------------------------------------
1 | /**
2 | * \file crc.c
3 | * Functions and types for CRC checks.
4 | *
5 | * Generated on Thu May 5 15:32:31 2016,
6 | * by pycrc v0.9, https://pycrc.org
7 | * using the configuration:
8 | * Width = 32
9 | * Poly = 0x04c11db7
10 | * Xor_In = 0xffffffff
11 | * ReflectIn = False
12 | * Xor_Out = 0x00000000
13 | * ReflectOut = False
14 | * Algorithm = table-driven
15 | *****************************************************************************/
16 | #include
17 | #include "ngx_rtmp_mpegts_crc.h" /* include the header file generated with pycrc */
18 |
19 |
20 | /**
21 | * Static table used for the table_driven implementation.
22 | *****************************************************************************/
23 | static const ngx_rtmp_mpegts_crc_t ngx_rtmp_mpegts_crc_table[256] = {
24 | 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
25 | 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
26 | 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
27 | 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd,
28 | 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5,
29 | 0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
30 | 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
31 | 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d,
32 | 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
33 | 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca,
34 | 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02,
35 | 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
36 | 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692,
37 | 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a,
38 | 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
39 | 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a,
40 | 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
41 | 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
42 | 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b,
43 | 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623,
44 | 0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
45 | 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
46 | 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b,
47 | 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
48 | 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c,
49 | 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24,
50 | 0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
51 | 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654,
52 | 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c,
53 | 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
54 | 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
55 | 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
56 | };
57 |
58 |
59 | /**
60 | * Update the crc value with new data.
61 | *
62 | * \param crc The current crc value.
63 | * \param data Pointer to a buffer of \a data_len bytes.
64 | * \param data_len Number of bytes in the \a data buffer.
65 | * \return The updated crc value.
66 | *****************************************************************************/
67 | ngx_rtmp_mpegts_crc_t
68 | ngx_rtmp_mpegts_crc_update(ngx_rtmp_mpegts_crc_t crc, const void *data, size_t data_len)
69 | {
70 | uint32_t tbl_idx;
71 | const u_char *d = (const unsigned char *) data;
72 |
73 | while (data_len--) {
74 | tbl_idx = ((crc >> 24) ^ *d) & 0xff;
75 | crc = (ngx_rtmp_mpegts_crc_table[tbl_idx] ^ (crc << 8)) & 0xffffffff;
76 |
77 | d++;
78 | }
79 |
80 | return crc & 0xffffffff;
81 | }
82 |
--------------------------------------------------------------------------------
/hls/ngx_rtmp_mpegts_crc.h:
--------------------------------------------------------------------------------
1 | /**
2 | * \file crc.h
3 | * Functions and types for CRC checks.
4 | *
5 | * Generated on Thu May 5 15:32:22 2016,
6 | * by pycrc v0.9, https://pycrc.org
7 | * using the configuration:
8 | * Width = 32
9 | * Poly = 0x04c11db7
10 | * Xor_In = 0xffffffff
11 | * ReflectIn = False
12 | * Xor_Out = 0x00000000
13 | * ReflectOut = False
14 | * Algorithm = table-driven
15 | *****************************************************************************/
16 | #ifndef _NGX_RTMP_MPEGTS_CRC_H_INCLUDED_
17 | #define _NGX_RTMP_MPEGTS_CRC_H_INCLUDED_
18 |
19 |
20 | #include
21 |
22 |
23 | #ifdef __cplusplus
24 | extern "C" {
25 | #endif
26 |
27 |
28 | /**
29 | * The definition of the used algorithm.
30 | *
31 | * This is not used anywhere in the generated code, but it may be used by the
32 | * application code to call algoritm-specific code, is desired.
33 | *****************************************************************************/
34 | #define CRC_ALGO_TABLE_DRIVEN 1
35 |
36 |
37 | /**
38 | * The type of the CRC values.
39 | *
40 | * This type must be big enough to contain at least 32 bits.
41 | *****************************************************************************/
42 | typedef uint_fast32_t ngx_rtmp_mpegts_crc_t;
43 |
44 |
45 | /**
46 | * Calculate the initial crc value.
47 | *
48 | * \return The initial crc value.
49 | *****************************************************************************/
50 | static ngx_inline ngx_rtmp_mpegts_crc_t ngx_rtmp_mpegts_crc_init(void)
51 | {
52 | return 0xffffffff;
53 | }
54 |
55 |
56 | /**
57 | * Update the crc value with new data.
58 | *
59 | * \param crc The current crc value.
60 | * \param data Pointer to a buffer of \a data_len bytes.
61 | * \param data_len Number of bytes in the \a data buffer.
62 | * \return The updated crc value.
63 | *****************************************************************************/
64 | ngx_rtmp_mpegts_crc_t ngx_rtmp_mpegts_crc_update(ngx_rtmp_mpegts_crc_t crc,
65 | const void *data, size_t data_len);
66 |
67 |
68 | /**
69 | * Calculate the final crc value.
70 | *
71 | * \param crc The current crc value.
72 | * \return The final crc value.
73 | *****************************************************************************/
74 | static ngx_inline ngx_rtmp_mpegts_crc_t
75 | ngx_rtmp_mpegts_crc_finalize(ngx_rtmp_mpegts_crc_t crc)
76 | {
77 | return crc ^ 0x00000000;
78 | }
79 |
80 |
81 | #ifdef __cplusplus
82 | } /* closing brace for extern "C" */
83 | #endif
84 |
85 | #endif /* _NGX_RTMP_MPEGTS_CRC_H_INCLUDED_ */
86 |
--------------------------------------------------------------------------------
/ngx_http_flv_live_module.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Winshining
4 | */
5 |
6 | #ifndef _NGX_HTTP_FLV_LIVE_H_INCLUDED_
7 | #define _NGX_HTTP_FLV_LIVE_H_INCLUDED_
8 |
9 |
10 | #include "ngx_rtmp_cmd_module.h"
11 | #include "ngx_rtmp_live_module.h"
12 | #include "ngx_rtmp_codec_module.h"
13 |
14 |
15 | #define NGX_HASH_MAX_SIZE 0x80
16 | #define NGX_HASH_MAX_BUKET_SIZE 0x40
17 | #define NGX_BUFF_MAX_SIZE 0x80
18 | #define NGX_FLV_TAG_HEADER_SIZE 11
19 |
20 |
21 | extern ngx_module_t ngx_rtmp_module;
22 |
23 |
24 | extern ngx_rtmp_play_pt http_flv_live_next_play;
25 | extern ngx_rtmp_close_stream_pt http_flv_live_next_close_stream;
26 |
27 |
28 | #define ngx_rtmp_cycle_get_module_main_conf(cycle, module) \
29 | (cycle->conf_ctx[ngx_rtmp_module.index] ? \
30 | ((ngx_rtmp_conf_ctx_t *) cycle->conf_ctx[ngx_rtmp_module.index]) \
31 | ->main_conf[module.ctx_index]: \
32 | NULL)
33 |
34 |
35 | typedef struct ngx_http_flv_live_ctx_s {
36 | ngx_rtmp_session_t *s;
37 | ngx_flag_t header_sent;
38 |
39 | ngx_str_t app;
40 | ngx_str_t port;
41 | ngx_str_t stream;
42 | } ngx_http_flv_live_ctx_t;
43 |
44 |
45 | typedef struct ngx_http_flv_live_conf_s {
46 | ngx_flag_t flv_live;
47 | } ngx_http_flv_live_conf_t;
48 |
49 |
50 | typedef struct {
51 | ngx_chain_t *meta;
52 | ngx_chain_t *apkt;
53 | ngx_chain_t *acopkt;
54 | ngx_chain_t *rpkt;
55 |
56 | ngx_int_t (*send_message_pt)(ngx_rtmp_session_t *s,
57 | ngx_chain_t *out, ngx_uint_t priority);
58 | ngx_chain_t *(*meta_message_pt)(ngx_rtmp_session_t *s,
59 | ngx_chain_t *in);
60 | ngx_chain_t *(*append_message_pt)(ngx_rtmp_session_t *s,
61 | ngx_rtmp_header_t *h, ngx_rtmp_header_t *lh,
62 | ngx_chain_t *in);
63 | void (*free_message_pt)(ngx_rtmp_session_t *s,
64 | ngx_chain_t *in);
65 | } ngx_rtmp_live_proc_handler_t;
66 |
67 |
68 | ngx_int_t ngx_http_flv_live_play(ngx_rtmp_session_t *s,
69 | ngx_rtmp_play_t *v);
70 | ngx_int_t ngx_http_flv_live_close_stream(ngx_rtmp_session_t *s,
71 | ngx_rtmp_close_stream_t *v);
72 |
73 | ngx_int_t ngx_http_flv_live_send_header(ngx_rtmp_session_t *s);
74 | void ngx_http_flv_live_set_status(ngx_rtmp_session_t *s, unsigned active);
75 |
76 |
77 | #endif
78 |
79 |
--------------------------------------------------------------------------------
/ngx_rtmp_access_module.c:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #include
8 | #include
9 | #include "ngx_rtmp.h"
10 | #include "ngx_rtmp_cmd_module.h"
11 |
12 |
13 | static ngx_rtmp_publish_pt next_publish;
14 | static ngx_rtmp_play_pt next_play;
15 |
16 |
17 | #define NGX_RTMP_ACCESS_PUBLISH 0x01
18 | #define NGX_RTMP_ACCESS_PLAY 0x02
19 |
20 |
21 | static char * ngx_rtmp_access_rule(ngx_conf_t *cf, ngx_command_t *cmd,
22 | void *conf);
23 | static ngx_int_t ngx_rtmp_access_postconfiguration(ngx_conf_t *cf);
24 | static void * ngx_rtmp_access_create_app_conf(ngx_conf_t *cf);
25 | static char * ngx_rtmp_access_merge_app_conf(ngx_conf_t *cf,
26 | void *parent, void *child);
27 |
28 |
29 | typedef struct {
30 | in_addr_t mask;
31 | in_addr_t addr;
32 | ngx_uint_t deny;
33 | ngx_uint_t flags;
34 | } ngx_rtmp_access_rule_t;
35 |
36 |
37 | #if (NGX_HAVE_INET6)
38 |
39 | typedef struct {
40 | struct in6_addr addr;
41 | struct in6_addr mask;
42 | ngx_uint_t deny;
43 | ngx_uint_t flags;
44 | } ngx_rtmp_access_rule6_t;
45 |
46 | #endif
47 |
48 |
49 | typedef struct {
50 | ngx_array_t rules; /* array of ngx_rtmp_access_rule_t */
51 | #if (NGX_HAVE_INET6)
52 | ngx_array_t rules6; /* array of ngx_rtmp_access_rule6_t */
53 | #endif
54 | } ngx_rtmp_access_app_conf_t;
55 |
56 |
57 | static ngx_command_t ngx_rtmp_access_commands[] = {
58 |
59 | { ngx_string("allow"),
60 | NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE12,
61 | ngx_rtmp_access_rule,
62 | NGX_RTMP_APP_CONF_OFFSET,
63 | 0,
64 | NULL },
65 |
66 | { ngx_string("deny"),
67 | NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE12,
68 | ngx_rtmp_access_rule,
69 | NGX_RTMP_APP_CONF_OFFSET,
70 | 0,
71 | NULL },
72 |
73 | ngx_null_command
74 | };
75 |
76 |
77 | static ngx_rtmp_module_t ngx_rtmp_access_module_ctx = {
78 | NULL, /* preconfiguration */
79 | ngx_rtmp_access_postconfiguration, /* postconfiguration */
80 | NULL, /* create main configuration */
81 | NULL, /* init main configuration */
82 | NULL, /* create server configuration */
83 | NULL, /* merge server configuration */
84 | ngx_rtmp_access_create_app_conf, /* create app configuration */
85 | ngx_rtmp_access_merge_app_conf, /* merge app configuration */
86 | };
87 |
88 |
89 | ngx_module_t ngx_rtmp_access_module = {
90 | NGX_MODULE_V1,
91 | &ngx_rtmp_access_module_ctx, /* module context */
92 | ngx_rtmp_access_commands, /* module directives */
93 | NGX_RTMP_MODULE, /* module type */
94 | NULL, /* init master */
95 | NULL, /* init module */
96 | NULL, /* init process */
97 | NULL, /* init thread */
98 | NULL, /* exit thread */
99 | NULL, /* exit process */
100 | NULL, /* exit master */
101 | NGX_MODULE_V1_PADDING
102 | };
103 |
104 |
105 | static void *
106 | ngx_rtmp_access_create_app_conf(ngx_conf_t *cf)
107 | {
108 | ngx_rtmp_access_app_conf_t *aacf;
109 |
110 | aacf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_access_app_conf_t));
111 | if (aacf == NULL) {
112 | return NULL;
113 | }
114 |
115 | if (ngx_array_init(&aacf->rules, cf->pool, 1,
116 | sizeof(ngx_rtmp_access_rule_t))
117 | != NGX_OK)
118 | {
119 | return NULL;
120 | }
121 |
122 | #if (NGX_HAVE_INET6)
123 | if (ngx_array_init(&aacf->rules6, cf->pool, 1,
124 | sizeof(ngx_rtmp_access_rule6_t))
125 | != NGX_OK)
126 | {
127 | return NULL;
128 | }
129 | #endif
130 |
131 | return aacf;
132 | }
133 |
134 |
135 | static ngx_int_t
136 | ngx_rtmp_access_merge_rules(ngx_array_t *prev, ngx_array_t *rules)
137 | {
138 | void *p;
139 |
140 | if (prev->nelts == 0) {
141 | return NGX_OK;
142 | }
143 |
144 | if (rules->nelts == 0) {
145 | *rules = *prev;
146 | return NGX_OK;
147 | }
148 |
149 | p = ngx_array_push_n(rules, prev->nelts);
150 | if (p == NULL) {
151 | return NGX_ERROR;
152 | }
153 |
154 | ngx_memcpy(p, prev->elts, prev->size * prev->nelts);
155 |
156 | return NGX_OK;
157 | }
158 |
159 |
160 | static char *
161 | ngx_rtmp_access_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
162 | {
163 | ngx_rtmp_access_app_conf_t *prev = parent;
164 | ngx_rtmp_access_app_conf_t *conf = child;
165 |
166 | if (ngx_rtmp_access_merge_rules(&prev->rules, &conf->rules) != NGX_OK) {
167 | return NGX_CONF_ERROR;
168 | }
169 |
170 | #if (NGX_HAVE_INET6)
171 | if (ngx_rtmp_access_merge_rules(&prev->rules6, &conf->rules6) != NGX_OK) {
172 | return NGX_CONF_ERROR;
173 | }
174 | #endif
175 |
176 | return NGX_CONF_OK;
177 | }
178 |
179 |
180 | static ngx_int_t
181 | ngx_rtmp_access_found(ngx_rtmp_session_t *s, ngx_uint_t deny)
182 | {
183 | if (deny) {
184 | ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
185 | "access forbidden by rule");
186 | return NGX_ERROR;
187 | }
188 |
189 | return NGX_OK;
190 | }
191 |
192 |
193 | static ngx_int_t
194 | ngx_rtmp_access_inet(ngx_rtmp_session_t *s, in_addr_t addr, ngx_uint_t flag)
195 | {
196 | ngx_uint_t i;
197 | ngx_rtmp_access_rule_t *rule;
198 | ngx_rtmp_access_app_conf_t *ascf;
199 |
200 | ascf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_access_module);
201 |
202 | rule = ascf->rules.elts;
203 | for (i = 0; i < ascf->rules.nelts; i++) {
204 |
205 | ngx_log_debug3(NGX_LOG_DEBUG_HTTP, s->connection->log, 0,
206 | "access: %08XD %08XD %08XD",
207 | addr, rule[i].mask, rule[i].addr);
208 |
209 | if ((addr & rule[i].mask) == rule[i].addr && (flag & rule[i].flags)) {
210 | return ngx_rtmp_access_found(s, rule[i].deny);
211 | }
212 | }
213 |
214 | return NGX_OK;
215 | }
216 |
217 |
218 | #if (NGX_HAVE_INET6)
219 |
220 | static ngx_int_t
221 | ngx_rtmp_access_inet6(ngx_rtmp_session_t *s, u_char *p, ngx_uint_t flag)
222 | {
223 | ngx_uint_t n;
224 | ngx_uint_t i;
225 | ngx_rtmp_access_rule6_t *rule6;
226 | ngx_rtmp_access_app_conf_t *ascf;
227 |
228 | ascf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_access_module);
229 |
230 | rule6 = ascf->rules6.elts;
231 | for (i = 0; i < ascf->rules6.nelts; i++) {
232 |
233 | #if (NGX_DEBUG)
234 | {
235 | size_t cl, ml, al;
236 | u_char ct[NGX_INET6_ADDRSTRLEN];
237 | u_char mt[NGX_INET6_ADDRSTRLEN];
238 | u_char at[NGX_INET6_ADDRSTRLEN];
239 |
240 | cl = ngx_inet6_ntop(p, ct, NGX_INET6_ADDRSTRLEN);
241 | ml = ngx_inet6_ntop(rule6[i].mask.s6_addr, mt, NGX_INET6_ADDRSTRLEN);
242 | al = ngx_inet6_ntop(rule6[i].addr.s6_addr, at, NGX_INET6_ADDRSTRLEN);
243 |
244 | ngx_log_debug6(NGX_LOG_DEBUG_HTTP, s->connection->log, 0,
245 | "access: %*s %*s %*s", cl, ct, ml, mt, al, at);
246 | }
247 | #endif
248 |
249 | for (n = 0; n < 16; n++) {
250 | if ((p[n] & rule6[i].mask.s6_addr[n]) != rule6[i].addr.s6_addr[n]) {
251 | goto next;
252 | }
253 | }
254 |
255 | if (flag & rule6[i].flags) {
256 | return ngx_rtmp_access_found(s, rule6[i].deny);
257 | }
258 |
259 | next:
260 | continue;
261 | }
262 |
263 | return NGX_OK;
264 | }
265 |
266 | #endif
267 |
268 |
269 | static ngx_int_t
270 | ngx_rtmp_access(ngx_rtmp_session_t *s, ngx_uint_t flag)
271 | {
272 | struct sockaddr_in *sin;
273 | ngx_rtmp_access_app_conf_t *ascf;
274 | #if (NGX_HAVE_INET6)
275 | u_char *p;
276 | in_addr_t addr;
277 | struct sockaddr_in6 *sin6;
278 | #endif
279 |
280 | ascf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_access_module);
281 | if (ascf == NULL) {
282 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, s->connection->log, 0,
283 | "access: NULL app conf");
284 | return NGX_ERROR;
285 | }
286 |
287 | /* relay etc */
288 | if (s->connection->sockaddr == NULL) {
289 | return NGX_OK;
290 | }
291 |
292 | switch (s->connection->sockaddr->sa_family) {
293 |
294 | case AF_INET:
295 | sin = (struct sockaddr_in *) s->connection->sockaddr;
296 | return ngx_rtmp_access_inet(s, sin->sin_addr.s_addr, flag);
297 |
298 | #if (NGX_HAVE_INET6)
299 |
300 | case AF_INET6:
301 | sin6 = (struct sockaddr_in6 *) s->connection->sockaddr;
302 | p = sin6->sin6_addr.s6_addr;
303 |
304 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
305 | addr = p[12] << 24;
306 | addr += p[13] << 16;
307 | addr += p[14] << 8;
308 | addr += p[15];
309 | return ngx_rtmp_access_inet(s, htonl(addr), flag);
310 | }
311 |
312 | return ngx_rtmp_access_inet6(s, p, flag);
313 |
314 | #endif
315 | }
316 |
317 | return NGX_OK;
318 | }
319 |
320 |
321 | static char *
322 | ngx_rtmp_access_rule(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
323 | {
324 | ngx_rtmp_access_app_conf_t *ascf = conf;
325 |
326 | ngx_int_t rc;
327 | ngx_uint_t all;
328 | ngx_str_t *value;
329 | ngx_cidr_t cidr;
330 | ngx_rtmp_access_rule_t *rule;
331 | #if (NGX_HAVE_INET6)
332 | ngx_rtmp_access_rule6_t *rule6;
333 | #endif
334 | size_t n;
335 | ngx_uint_t flags;
336 |
337 | ngx_memzero(&cidr, sizeof(ngx_cidr_t));
338 |
339 | value = cf->args->elts;
340 |
341 | n = 1;
342 | flags = 0;
343 |
344 | if (cf->args->nelts == 2) {
345 |
346 | flags = NGX_RTMP_ACCESS_PUBLISH | NGX_RTMP_ACCESS_PLAY;
347 |
348 | } else {
349 |
350 | for(; n < cf->args->nelts - 1; ++n) {
351 |
352 | if (value[n].len == sizeof("publish") - 1 &&
353 | ngx_strcmp(value[1].data, "publish") == 0)
354 | {
355 | flags |= NGX_RTMP_ACCESS_PUBLISH;
356 | continue;
357 |
358 | }
359 |
360 | if (value[n].len == sizeof("play") - 1 &&
361 | ngx_strcmp(value[1].data, "play") == 0)
362 | {
363 | flags |= NGX_RTMP_ACCESS_PLAY;
364 | continue;
365 |
366 | }
367 |
368 | ngx_log_error(NGX_LOG_ERR, cf->log, 0,
369 | "unexpected access specified: '%V'", &value[n]);
370 | return NGX_CONF_ERROR;
371 | }
372 | }
373 |
374 | all = (value[n].len == 3 && ngx_strcmp(value[n].data, "all") == 0);
375 |
376 | if (!all) {
377 |
378 | rc = ngx_ptocidr(&value[n], &cidr);
379 |
380 | if (rc == NGX_ERROR) {
381 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
382 | "invalid parameter \"%V\"", &value[1]);
383 | return NGX_CONF_ERROR;
384 | }
385 |
386 | if (rc == NGX_DONE) {
387 | ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
388 | "low address bits of %V are meaningless",
389 | &value[1]);
390 | }
391 | }
392 |
393 | switch (cidr.family) {
394 |
395 | #if (NGX_HAVE_INET6)
396 | case AF_INET6:
397 | case 0: /* all */
398 |
399 | rule6 = ngx_array_push(&ascf->rules6);
400 | if (rule6 == NULL) {
401 | return NGX_CONF_ERROR;
402 | }
403 |
404 | rule6->mask = cidr.u.in6.mask;
405 | rule6->addr = cidr.u.in6.addr;
406 | rule6->deny = (value[0].data[0] == 'd') ? 1 : 0;
407 | rule6->flags = flags;
408 |
409 | if (!all) {
410 | break;
411 | }
412 |
413 | /* "all" passes through */
414 | #endif
415 | /* fall through */
416 |
417 | default: /* AF_INET */
418 |
419 | rule = ngx_array_push(&ascf->rules);
420 | if (rule == NULL) {
421 | return NGX_CONF_ERROR;
422 | }
423 |
424 | rule->mask = cidr.u.in.mask;
425 | rule->addr = cidr.u.in.addr;
426 | rule->deny = (value[0].data[0] == 'd') ? 1 : 0;
427 | rule->flags = flags;
428 | }
429 |
430 | return NGX_CONF_OK;
431 | }
432 |
433 |
434 | static ngx_int_t
435 | ngx_rtmp_access_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
436 | {
437 | if (s->auto_pushed) {
438 | goto next;
439 | }
440 |
441 | if (ngx_rtmp_access(s, NGX_RTMP_ACCESS_PUBLISH) != NGX_OK) {
442 | return NGX_ERROR;
443 | }
444 |
445 | next:
446 | return next_publish(s, v);
447 | }
448 |
449 |
450 | static ngx_int_t
451 | ngx_rtmp_access_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
452 | {
453 | if (ngx_rtmp_access(s, NGX_RTMP_ACCESS_PLAY) != NGX_OK) {
454 | return NGX_ERROR;
455 | }
456 |
457 | return next_play(s, v);
458 | }
459 |
460 |
461 | static ngx_int_t
462 | ngx_rtmp_access_postconfiguration(ngx_conf_t *cf)
463 | {
464 | /* chain handlers */
465 | next_publish = ngx_rtmp_publish;
466 | ngx_rtmp_publish = ngx_rtmp_access_publish;
467 |
468 | next_play = ngx_rtmp_play;
469 | ngx_rtmp_play = ngx_rtmp_access_play;
470 |
471 | return NGX_OK;
472 | }
473 |
--------------------------------------------------------------------------------
/ngx_rtmp_amf.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #ifndef _NGX_RTMP_AMF_H_INCLUDED_
8 | #define _NGX_RTMP_AMF_H_INCLUDED_
9 |
10 |
11 | #include
12 | #include
13 |
14 |
15 | /* basic types */
16 | #define NGX_RTMP_AMF_NUMBER 0x00
17 | #define NGX_RTMP_AMF_BOOLEAN 0x01
18 | #define NGX_RTMP_AMF_STRING 0x02
19 | #define NGX_RTMP_AMF_OBJECT 0x03
20 | #define NGX_RTMP_AMF_NULL 0x05
21 | #define NGX_RTMP_AMF_ARRAY_NULL 0x06
22 | #define NGX_RTMP_AMF_MIXED_ARRAY 0x08
23 | #define NGX_RTMP_AMF_END 0x09
24 | #define NGX_RTMP_AMF_ARRAY 0x0a
25 |
26 | /* extended types */
27 | #define NGX_RTMP_AMF_INT8 0x0100
28 | #define NGX_RTMP_AMF_INT16 0x0101
29 | #define NGX_RTMP_AMF_INT32 0x0102
30 | #define NGX_RTMP_AMF_VARIANT_ 0x0103
31 |
32 | /* r/w flags */
33 | #define NGX_RTMP_AMF_OPTIONAL 0x1000
34 | #define NGX_RTMP_AMF_TYPELESS 0x2000
35 | #define NGX_RTMP_AMF_CONTEXT 0x4000
36 |
37 | #define NGX_RTMP_AMF_VARIANT (NGX_RTMP_AMF_VARIANT_\
38 | |NGX_RTMP_AMF_TYPELESS)
39 |
40 |
41 | typedef struct {
42 | ngx_int_t type;
43 | ngx_str_t name;
44 | void *data;
45 | size_t len;
46 | } ngx_rtmp_amf_elt_t;
47 |
48 |
49 | typedef ngx_chain_t * (*ngx_rtmp_amf_alloc_pt)(void *arg);
50 |
51 |
52 | typedef struct {
53 | ngx_chain_t *link, *first;
54 | size_t offset;
55 | ngx_rtmp_amf_alloc_pt alloc;
56 | void *arg;
57 | ngx_log_t *log;
58 | } ngx_rtmp_amf_ctx_t;
59 |
60 |
61 | /* reading AMF */
62 | ngx_int_t ngx_rtmp_amf_read(ngx_rtmp_amf_ctx_t *ctx,
63 | ngx_rtmp_amf_elt_t *elts, size_t nelts);
64 |
65 | /* writing AMF */
66 | ngx_int_t ngx_rtmp_amf_write(ngx_rtmp_amf_ctx_t *ctx,
67 | ngx_rtmp_amf_elt_t *elts, size_t nelts);
68 |
69 |
70 | #endif /* _NGX_RTMP_AMF_H_INCLUDED_ */
71 |
72 |
--------------------------------------------------------------------------------
/ngx_rtmp_bandwidth.c:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #include
8 | #include
9 | #include "ngx_rtmp_bandwidth.h"
10 |
11 |
12 | void
13 | ngx_rtmp_update_bandwidth(ngx_rtmp_bandwidth_t *bw, uint32_t bytes)
14 | {
15 | if (ngx_cached_time->sec > bw->intl_end) {
16 | bw->bandwidth = ngx_cached_time->sec >
17 | bw->intl_end + NGX_RTMP_BANDWIDTH_INTERVAL
18 | ? 0
19 | : bw->intl_bytes / NGX_RTMP_BANDWIDTH_INTERVAL;
20 | bw->intl_bytes = 0;
21 | bw->intl_end = ngx_cached_time->sec + NGX_RTMP_BANDWIDTH_INTERVAL;
22 | }
23 |
24 | bw->bytes += bytes;
25 | bw->intl_bytes += bytes;
26 | }
27 |
--------------------------------------------------------------------------------
/ngx_rtmp_bandwidth.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #ifndef _NGX_RTMP_BANDWIDTH_H_INCLUDED_
8 | #define _NGX_RTMP_BANDWIDTH_H_INCLUDED_
9 |
10 |
11 | #include
12 | #include
13 |
14 |
15 | /* Bandwidth update interval in seconds */
16 | #define NGX_RTMP_BANDWIDTH_INTERVAL 10
17 |
18 |
19 | typedef struct {
20 | uint64_t bytes;
21 | uint64_t bandwidth; /* bytes/sec */
22 |
23 | time_t intl_end;
24 | uint64_t intl_bytes;
25 | } ngx_rtmp_bandwidth_t;
26 |
27 |
28 | void ngx_rtmp_update_bandwidth(ngx_rtmp_bandwidth_t *bw, uint32_t bytes);
29 |
30 |
31 | #endif /* _NGX_RTMP_BANDWIDTH_H_INCLUDED_ */
32 |
--------------------------------------------------------------------------------
/ngx_rtmp_bitop.c:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #include
8 | #include
9 | #include "ngx_rtmp_bitop.h"
10 |
11 |
12 | void
13 | ngx_rtmp_bit_init_reader(ngx_rtmp_bit_reader_t *br, u_char *pos, u_char *last)
14 | {
15 | ngx_memzero(br, sizeof(ngx_rtmp_bit_reader_t));
16 |
17 | br->pos = pos;
18 | br->last = last;
19 | }
20 |
21 |
22 | uint64_t
23 | ngx_rtmp_bit_read(ngx_rtmp_bit_reader_t *br, ngx_uint_t n)
24 | {
25 | uint64_t v;
26 | ngx_uint_t d;
27 |
28 | v = 0;
29 |
30 | while (n) {
31 |
32 | if (br->pos >= br->last) {
33 | br->err = 1;
34 | return 0;
35 | }
36 |
37 | d = (br->offs + n > 8 ? (ngx_uint_t) (8 - br->offs) : n);
38 |
39 | v <<= d;
40 | v += (*br->pos >> (8 - br->offs - d)) & ((u_char) 0xff >> (8 - d));
41 |
42 | br->offs += d;
43 | n -= d;
44 |
45 | if (br->offs == 8) {
46 | br->pos++;
47 | br->offs = 0;
48 | }
49 | }
50 |
51 | return v;
52 | }
53 |
54 |
55 | uint64_t
56 | ngx_rtmp_bit_read_golomb(ngx_rtmp_bit_reader_t *br)
57 | {
58 | ngx_uint_t n;
59 |
60 | for (n = 0; ngx_rtmp_bit_read(br, 1) == 0 && !br->err; n++);
61 |
62 | return ((uint64_t) 1 << n) + ngx_rtmp_bit_read(br, n) - 1;
63 | }
64 |
--------------------------------------------------------------------------------
/ngx_rtmp_bitop.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #ifndef _NGX_RTMP_BITOP_H_INCLUDED_
8 | #define _NGX_RTMP_BITOP_H_INCLUDED_
9 |
10 |
11 | #include
12 | #include
13 |
14 |
15 | typedef struct {
16 | u_char *pos;
17 | u_char *last;
18 | ngx_uint_t offs;
19 | ngx_uint_t err;
20 | } ngx_rtmp_bit_reader_t;
21 |
22 |
23 | void ngx_rtmp_bit_init_reader(ngx_rtmp_bit_reader_t *br, u_char *pos,
24 | u_char *last);
25 | uint64_t ngx_rtmp_bit_read(ngx_rtmp_bit_reader_t *br, ngx_uint_t n);
26 | uint64_t ngx_rtmp_bit_read_golomb(ngx_rtmp_bit_reader_t *br);
27 |
28 |
29 | #define ngx_rtmp_bit_read_err(br) ((br)->err)
30 |
31 | #define ngx_rtmp_bit_read_eof(br) ((br)->pos == (br)->last)
32 |
33 | #define ngx_rtmp_bit_read_8(br) \
34 | ((uint8_t) ngx_rtmp_bit_read(br, 8))
35 |
36 | #define ngx_rtmp_bit_read_16(br) \
37 | ((uint16_t) ngx_rtmp_bit_read(br, 16))
38 |
39 | #define ngx_rtmp_bit_read_32(br) \
40 | ((uint32_t) ngx_rtmp_bit_read(br, 32))
41 |
42 | #define ngx_rtmp_bit_read_64(br) \
43 | ((uint64_t) ngx_rtmp_bit_read(br, 64))
44 |
45 |
46 | #endif /* _NGX_RTMP_BITOP_H_INCLUDED_ */
47 |
--------------------------------------------------------------------------------
/ngx_rtmp_cmd_module.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #ifndef _NGX_RTMP_CMD_H_INCLUDED_
8 | #define _NGX_RTMP_CMD_H_INCLUDED_
9 |
10 |
11 | #include
12 | #include
13 | #include
14 | #include "ngx_rtmp.h"
15 |
16 |
17 | #define NGX_RTMP_MAX_NAME 256
18 | #define NGX_RTMP_MAX_URL 256
19 | #define NGX_RTMP_MAX_ARGS NGX_RTMP_MAX_NAME
20 |
21 |
22 | /* Basic RTMP call support */
23 |
24 | typedef struct {
25 | double trans;
26 | u_char app[NGX_RTMP_MAX_NAME];
27 | u_char args[NGX_RTMP_MAX_ARGS];
28 | u_char flashver[64];
29 | u_char swf_url[NGX_RTMP_MAX_URL];
30 | u_char tc_url[NGX_RTMP_MAX_URL];
31 | double acodecs;
32 | double vcodecs;
33 | u_char page_url[NGX_RTMP_MAX_URL];
34 | u_char server_name[NGX_RTMP_MAX_URL];
35 | double object_encoding;
36 | } ngx_rtmp_connect_t;
37 |
38 |
39 | typedef struct {
40 | double trans;
41 | double stream;
42 | } ngx_rtmp_create_stream_t;
43 |
44 |
45 | typedef struct {
46 | double stream;
47 | } ngx_rtmp_delete_stream_t;
48 |
49 |
50 | typedef struct {
51 | double stream;
52 | } ngx_rtmp_close_stream_t;
53 |
54 |
55 | typedef struct {
56 | u_char name[NGX_RTMP_MAX_NAME];
57 | u_char args[NGX_RTMP_MAX_ARGS];
58 | u_char type[16];
59 | int silent;
60 | } ngx_rtmp_publish_t;
61 |
62 |
63 | typedef struct {
64 | u_char name[NGX_RTMP_MAX_NAME];
65 | u_char args[NGX_RTMP_MAX_ARGS];
66 | double start;
67 | double duration;
68 | int reset;
69 | int silent;
70 | } ngx_rtmp_play_t;
71 |
72 |
73 | typedef struct {
74 | double offset;
75 | } ngx_rtmp_seek_t;
76 |
77 |
78 | typedef struct {
79 | uint8_t pause;
80 | double position;
81 | } ngx_rtmp_pause_t;
82 |
83 |
84 | typedef struct {
85 | uint32_t msid;
86 | } ngx_rtmp_msid_t;
87 |
88 |
89 | typedef ngx_rtmp_msid_t ngx_rtmp_stream_begin_t;
90 | typedef ngx_rtmp_msid_t ngx_rtmp_stream_eof_t;
91 | typedef ngx_rtmp_msid_t ngx_rtmp_stream_dry_t;
92 | typedef ngx_rtmp_msid_t ngx_rtmp_recorded_t;
93 |
94 |
95 | typedef struct {
96 | uint32_t msid;
97 | uint32_t buflen;
98 | } ngx_rtmp_set_buflen_t;
99 |
100 |
101 | void ngx_rtmp_cmd_fill_args(u_char name[NGX_RTMP_MAX_NAME],
102 | u_char args[NGX_RTMP_MAX_ARGS]);
103 |
104 |
105 | typedef ngx_int_t (*ngx_rtmp_connect_pt)(ngx_rtmp_session_t *s,
106 | ngx_rtmp_connect_t *v);
107 | typedef ngx_int_t (*ngx_rtmp_disconnect_pt)(ngx_rtmp_session_t *s);
108 | typedef ngx_int_t (*ngx_rtmp_create_stream_pt)(ngx_rtmp_session_t *s,
109 | ngx_rtmp_create_stream_t *v);
110 | typedef ngx_int_t (*ngx_rtmp_close_stream_pt)(ngx_rtmp_session_t *s,
111 | ngx_rtmp_close_stream_t *v);
112 | typedef ngx_int_t (*ngx_rtmp_delete_stream_pt)(ngx_rtmp_session_t *s,
113 | ngx_rtmp_delete_stream_t *v);
114 | typedef ngx_int_t (*ngx_rtmp_publish_pt)(ngx_rtmp_session_t *s,
115 | ngx_rtmp_publish_t *v);
116 | typedef ngx_int_t (*ngx_rtmp_play_pt)(ngx_rtmp_session_t *s,
117 | ngx_rtmp_play_t *v);
118 | typedef ngx_int_t (*ngx_rtmp_seek_pt)(ngx_rtmp_session_t *s,
119 | ngx_rtmp_seek_t *v);
120 | typedef ngx_int_t (*ngx_rtmp_pause_pt)(ngx_rtmp_session_t *s,
121 | ngx_rtmp_pause_t *v);
122 |
123 | typedef ngx_int_t (*ngx_rtmp_stream_begin_pt)(ngx_rtmp_session_t *s,
124 | ngx_rtmp_stream_begin_t *v);
125 | typedef ngx_int_t (*ngx_rtmp_stream_eof_pt)(ngx_rtmp_session_t *s,
126 | ngx_rtmp_stream_eof_t *v);
127 | typedef ngx_int_t (*ngx_rtmp_stream_dry_pt)(ngx_rtmp_session_t *s,
128 | ngx_rtmp_stream_dry_t *v);
129 | typedef ngx_int_t (*ngx_rtmp_recorded_pt)(ngx_rtmp_session_t *s,
130 | ngx_rtmp_recorded_t *v);
131 | typedef ngx_int_t (*ngx_rtmp_set_buflen_pt)(ngx_rtmp_session_t *s,
132 | ngx_rtmp_set_buflen_t *v);
133 |
134 |
135 | extern ngx_rtmp_connect_pt ngx_rtmp_connect;
136 | extern ngx_rtmp_disconnect_pt ngx_rtmp_disconnect;
137 | extern ngx_rtmp_create_stream_pt ngx_rtmp_create_stream;
138 | extern ngx_rtmp_close_stream_pt ngx_rtmp_close_stream;
139 | extern ngx_rtmp_delete_stream_pt ngx_rtmp_delete_stream;
140 | extern ngx_rtmp_publish_pt ngx_rtmp_publish;
141 | extern ngx_rtmp_play_pt ngx_rtmp_play;
142 | extern ngx_rtmp_seek_pt ngx_rtmp_seek;
143 | extern ngx_rtmp_pause_pt ngx_rtmp_pause;
144 |
145 | extern ngx_rtmp_stream_begin_pt ngx_rtmp_stream_begin;
146 | extern ngx_rtmp_stream_eof_pt ngx_rtmp_stream_eof;
147 | extern ngx_rtmp_stream_dry_pt ngx_rtmp_stream_dry;
148 | extern ngx_rtmp_set_buflen_pt ngx_rtmp_set_buflen;
149 | extern ngx_rtmp_recorded_pt ngx_rtmp_recorded;
150 |
151 |
152 | #endif /*_NGX_RTMP_CMD_H_INCLUDED_ */
153 |
--------------------------------------------------------------------------------
/ngx_rtmp_codec_module.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | * Copyright (C) Winshining
5 | */
6 |
7 |
8 | #ifndef _NGX_RTMP_CODEC_H_INCLUDED_
9 | #define _NGX_RTMP_CODEC_H_INCLUDED_
10 |
11 |
12 | #include
13 | #include
14 | #include "ngx_rtmp.h"
15 |
16 |
17 | /* AVC NAL unit types */
18 | enum {
19 | NGX_RTMP_NALU_SLICE = 1,
20 | NGX_RTMP_NALU_DPA = 2,
21 | NGX_RTMP_NALU_DPB = 3,
22 | NGX_RTMP_NALU_DPC = 4,
23 | NGX_RTMP_NALU_IDR = 5,
24 | NGX_RTMP_NALU_SEI = 6,
25 | NGX_RTMP_NALU_SPS = 7,
26 | NGX_RTMP_NALU_PPS = 8,
27 | NGX_RTMP_NALU_AUD = 9,
28 | NGX_RTMP_NALU_EOSEQ = 10,
29 | NGX_RTMP_NALU_EOSTREAM = 11,
30 | NGX_RTMP_NALU_FILL = 12,
31 | NGX_RTMP_NALU_SPS_EXT = 13,
32 | NGX_RTMP_NALU_AUXILIARY_SLICE = 19
33 | };
34 |
35 |
36 | /* AVC frame types */
37 | enum {
38 | NGX_RTMP_FRAME_IDR = 1,
39 | NGX_RTMP_FRAME_INTER = 2,
40 | NGX_RTMP_FRAME_DISPOSABLE = 3,
41 | NGX_RTMP_FRAME_GENERATED = 4,
42 | NGX_RTMP_FRAME_VIDEOINFOCMD = 5
43 | };
44 |
45 |
46 | /* Audio codecs */
47 | enum {
48 | /* Uncompressed codec id is actually 0,
49 | * but we use another value for consistency */
50 | NGX_RTMP_AUDIO_UNCOMPRESSED = 16,
51 | NGX_RTMP_AUDIO_ADPCM = 1,
52 | NGX_RTMP_AUDIO_MP3 = 2,
53 | NGX_RTMP_AUDIO_LINEAR_LE = 3,
54 | NGX_RTMP_AUDIO_NELLY16 = 4,
55 | NGX_RTMP_AUDIO_NELLY8 = 5,
56 | NGX_RTMP_AUDIO_NELLY = 6,
57 | NGX_RTMP_AUDIO_G711A = 7,
58 | NGX_RTMP_AUDIO_G711U = 8,
59 | NGX_RTMP_AUDIO_AAC = 10,
60 | NGX_RTMP_AUDIO_SPEEX = 11,
61 | NGX_RTMP_AUDIO_MP3_8 = 14,
62 | NGX_RTMP_AUDIO_DEVSPEC = 15
63 | };
64 |
65 |
66 | /* Video codecs */
67 | enum {
68 | NGX_RTMP_VIDEO_JPEG = 1,
69 | NGX_RTMP_VIDEO_SORENSON_H263 = 2,
70 | NGX_RTMP_VIDEO_SCREEN = 3,
71 | NGX_RTMP_VIDEO_ON2_VP6 = 4,
72 | NGX_RTMP_VIDEO_ON2_VP6_ALPHA = 5,
73 | NGX_RTMP_VIDEO_SCREEN2 = 6,
74 | NGX_RTMP_VIDEO_H264 = 7
75 | };
76 |
77 |
78 | u_char * ngx_rtmp_get_audio_codec_name(ngx_uint_t id);
79 | u_char * ngx_rtmp_get_video_codec_name(ngx_uint_t id);
80 |
81 |
82 | #define NGX_RTMP_SPS_MAX_LENGTH 256
83 |
84 | typedef struct {
85 | ngx_uint_t width;
86 | ngx_uint_t height;
87 | double duration;
88 | double frame_rate;
89 | double video_data_rate;
90 | ngx_uint_t video_codec_id;
91 | double audio_data_rate;
92 | ngx_uint_t audio_codec_id;
93 | ngx_uint_t aac_profile;
94 | ngx_uint_t aac_chan_conf;
95 | ngx_uint_t aac_sbr;
96 | ngx_uint_t aac_ps;
97 | ngx_uint_t avc_profile;
98 | ngx_uint_t avc_compat;
99 | ngx_uint_t avc_level;
100 | ngx_uint_t avc_nal_bytes;
101 | ngx_uint_t avc_ref_frames;
102 | ngx_uint_t sample_rate; /* 5512, 11025, 22050, 44100 */
103 | ngx_uint_t sample_size; /* 1=8bit, 2=16bit */
104 | ngx_uint_t audio_channels; /* 1, 2 */
105 | u_char profile[32];
106 | u_char level[32];
107 |
108 | ngx_chain_t *avc_header;
109 | ngx_chain_t *aac_header;
110 |
111 | ngx_chain_t *meta;
112 | ngx_uint_t meta_version;
113 | } ngx_rtmp_codec_ctx_t;
114 |
115 |
116 | extern ngx_module_t ngx_rtmp_codec_module;
117 |
118 |
119 | #endif /* _NGX_RTMP_LIVE_H_INCLUDED_ */
120 |
--------------------------------------------------------------------------------
/ngx_rtmp_eval.c:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #include
8 | #include
9 | #include "ngx_rtmp_eval.h"
10 |
11 |
12 | #define NGX_RTMP_EVAL_BUFLEN 16
13 |
14 |
15 | static void
16 | ngx_rtmp_eval_session_str(void *ctx, ngx_rtmp_eval_t *e, ngx_str_t *ret)
17 | {
18 | *ret = *(ngx_str_t *) ((u_char *) ctx + e->offset);
19 | }
20 |
21 |
22 | static void
23 | ngx_rtmp_eval_connection_str(void *ctx, ngx_rtmp_eval_t *e, ngx_str_t *ret)
24 | {
25 | ngx_rtmp_session_t *s = ctx;
26 |
27 | *ret = *(ngx_str_t *) ((u_char *) s->connection + e->offset);
28 | }
29 |
30 |
31 | ngx_rtmp_eval_t ngx_rtmp_eval_session[] = {
32 |
33 | { ngx_string("app"),
34 | ngx_rtmp_eval_session_str,
35 | offsetof(ngx_rtmp_session_t, app) },
36 |
37 | { ngx_string("flashver"),
38 | ngx_rtmp_eval_session_str,
39 | offsetof(ngx_rtmp_session_t, flashver) },
40 |
41 | { ngx_string("swfurl"),
42 | ngx_rtmp_eval_session_str,
43 | offsetof(ngx_rtmp_session_t, swf_url) },
44 |
45 | { ngx_string("tcurl"),
46 | ngx_rtmp_eval_session_str,
47 | offsetof(ngx_rtmp_session_t, tc_url) },
48 |
49 | { ngx_string("pageurl"),
50 | ngx_rtmp_eval_session_str,
51 | offsetof(ngx_rtmp_session_t, page_url) },
52 |
53 | { ngx_string("addr"),
54 | ngx_rtmp_eval_connection_str,
55 | offsetof(ngx_connection_t, addr_text) },
56 |
57 | ngx_rtmp_null_eval
58 | };
59 |
60 |
61 | static void
62 | ngx_rtmp_eval_append(ngx_buf_t *b, void *data, size_t len, ngx_log_t *log)
63 | {
64 | size_t buf_len;
65 | u_char *old;
66 |
67 | if (b->last + len > b->end) {
68 | buf_len = (2 * (b->last - b->pos) + len +
69 | NGX_RTMP_EVAL_BUFLEN - 1) & ~(NGX_RTMP_EVAL_BUFLEN - 1);
70 |
71 | old = b->start;
72 |
73 | b->start = ngx_alloc(buf_len, log);
74 | if (b->start == NULL) {
75 | return;
76 | }
77 |
78 | b->last = ngx_cpymem(b->start, b->pos, b->last - b->pos);
79 | b->pos = b->start;
80 | b->end = b->start + buf_len;
81 |
82 | ngx_free(old);
83 | }
84 |
85 | b->last = ngx_cpymem(b->last, data, len);
86 | }
87 |
88 |
89 | static void
90 | ngx_rtmp_eval_append_var(void *ctx, ngx_buf_t *b, ngx_rtmp_eval_t **e,
91 | ngx_str_t *name, ngx_log_t *log)
92 | {
93 | ngx_str_t v;
94 | ngx_rtmp_eval_t *ee;
95 |
96 | for (; *e; ++e) {
97 | for (ee = *e; ee->handler; ++ee) {
98 | if (ee->name.len == name->len &&
99 | ngx_memcmp(ee->name.data, name->data, name->len) == 0)
100 | {
101 | ee->handler(ctx, ee, &v);
102 | ngx_rtmp_eval_append(b, v.data, v.len, log);
103 | }
104 | }
105 | }
106 | }
107 |
108 |
109 | ngx_int_t
110 | ngx_rtmp_eval(void *ctx, ngx_str_t *in, ngx_rtmp_eval_t **e, ngx_str_t *out,
111 | ngx_log_t *log)
112 | {
113 | u_char c, *p;
114 | ngx_str_t name;
115 | ngx_buf_t b;
116 | ngx_uint_t n;
117 |
118 | enum {
119 | NORMAL,
120 | ESCAPE,
121 | NAME,
122 | SNAME
123 | } state = NORMAL;
124 |
125 | b.pos = b.last = b.start = ngx_alloc(NGX_RTMP_EVAL_BUFLEN, log);
126 | if (b.pos == NULL) {
127 | return NGX_ERROR;
128 | }
129 |
130 | b.end = b.pos + NGX_RTMP_EVAL_BUFLEN;
131 | name.data = NULL;
132 |
133 | for (n = 0; n < in->len; ++n) {
134 | p = &in->data[n];
135 | c = *p;
136 |
137 | switch (state) {
138 | case SNAME:
139 | if (c != '}') {
140 | continue;
141 | }
142 |
143 | name.len = p - name.data;
144 | ngx_rtmp_eval_append_var(ctx, &b, e, &name, log);
145 |
146 | state = NORMAL;
147 |
148 | continue;
149 |
150 | case NAME:
151 | if (c == '{' && name.data == p) {
152 | ++name.data;
153 | state = SNAME;
154 | continue;
155 | }
156 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
157 | continue;
158 | }
159 |
160 | name.len = p - name.data;
161 | ngx_rtmp_eval_append_var(ctx, &b, e, &name, log);
162 |
163 | /* fall through */
164 |
165 | case NORMAL:
166 | switch (c) {
167 | case '$':
168 | name.data = p + 1;
169 | state = NAME;
170 | continue;
171 | case '\\':
172 | state = ESCAPE;
173 | continue;
174 | default:
175 | break;
176 | }
177 |
178 | /* fall through */
179 |
180 | case ESCAPE:
181 | ngx_rtmp_eval_append(&b, &c, 1, log);
182 | state = NORMAL;
183 | break;
184 |
185 | }
186 | }
187 |
188 | if (state == NAME) {
189 | p = &in->data[n];
190 | name.len = p - name.data;
191 | ngx_rtmp_eval_append_var(ctx, &b, e, &name, log);
192 | }
193 |
194 | c = 0;
195 | ngx_rtmp_eval_append(&b, &c, 1, log);
196 |
197 | out->data = b.pos;
198 | out->len = b.last - b.pos - 1;
199 |
200 | return NGX_OK;
201 | }
202 |
203 |
204 | ngx_int_t
205 | ngx_rtmp_eval_streams(ngx_str_t *in)
206 | {
207 | #if !(NGX_WIN32)
208 | ngx_int_t mode, create, v, close_src;
209 | ngx_fd_t dst, src;
210 | u_char *path;
211 |
212 | path = in->data;
213 |
214 | while (*path >= '0' && *path <= '9') {
215 | path++;
216 | }
217 |
218 | switch ((char) *path) {
219 |
220 | case '>':
221 |
222 | v = (path == in->data ? 1 : ngx_atoi(in->data, path - in->data));
223 | if (v == NGX_ERROR) {
224 | return NGX_ERROR;
225 | }
226 |
227 | dst = (ngx_fd_t) v;
228 | mode = NGX_FILE_WRONLY;
229 | create = NGX_FILE_TRUNCATE;
230 | path++;
231 |
232 | if (*path == (u_char) '>') {
233 | mode = NGX_FILE_APPEND;
234 | create = NGX_FILE_CREATE_OR_OPEN;
235 | path++;
236 | }
237 |
238 | break;
239 |
240 | case '<':
241 |
242 | v = (path == in->data ? 0 : ngx_atoi(in->data, path - in->data));
243 | if (v == NGX_ERROR) {
244 | return NGX_ERROR;
245 | }
246 |
247 | dst = (ngx_fd_t) v;
248 | mode = NGX_FILE_RDONLY;
249 | create = NGX_FILE_OPEN;
250 | path++;
251 |
252 | break;
253 |
254 | default:
255 |
256 | return NGX_DONE;
257 | }
258 |
259 | if (*path == (u_char) '&') {
260 |
261 | path++;
262 | v = ngx_atoi(path, in->data + in->len - path);
263 | if (v == NGX_ERROR) {
264 | return NGX_ERROR;
265 | }
266 | src = (ngx_fd_t) v;
267 | close_src = 0;
268 |
269 | } else {
270 |
271 | src = ngx_open_file(path, mode, create, NGX_FILE_DEFAULT_ACCESS);
272 | if (src == NGX_INVALID_FILE) {
273 | return NGX_ERROR;
274 | }
275 | close_src = 1;
276 |
277 | }
278 |
279 | if (src == dst) {
280 | return NGX_OK;
281 | }
282 |
283 | dup2(src, dst);
284 |
285 | if (close_src) {
286 | ngx_close_file(src);
287 | }
288 | return NGX_OK;
289 |
290 | #else
291 | return NGX_DONE;
292 | #endif
293 | }
294 |
--------------------------------------------------------------------------------
/ngx_rtmp_eval.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #ifndef _NGX_RTMP_EVAL_H_INCLUDED_
8 | #define _NGX_RTMP_EVAL_H_INCLUDED_
9 |
10 |
11 | #include
12 | #include
13 | #include "ngx_rtmp.h"
14 |
15 |
16 | typedef struct ngx_rtmp_eval_s ngx_rtmp_eval_t;
17 |
18 |
19 | typedef void (* ngx_rtmp_eval_pt)(void *ctx, ngx_rtmp_eval_t *e,
20 | ngx_str_t *ret);
21 |
22 |
23 | struct ngx_rtmp_eval_s {
24 | ngx_str_t name;
25 | ngx_rtmp_eval_pt handler;
26 | ngx_uint_t offset;
27 | };
28 |
29 |
30 | #define ngx_rtmp_null_eval { ngx_null_string, NULL, 0 }
31 |
32 |
33 | /* standard session eval variables */
34 | extern ngx_rtmp_eval_t ngx_rtmp_eval_session[];
35 |
36 |
37 | ngx_int_t ngx_rtmp_eval(void *ctx, ngx_str_t *in, ngx_rtmp_eval_t **e,
38 | ngx_str_t *out, ngx_log_t *log);
39 |
40 |
41 | ngx_int_t ngx_rtmp_eval_streams(ngx_str_t *in);
42 |
43 |
44 | #endif /* _NGX_RTMP_EVAL_H_INCLUDED_ */
45 |
--------------------------------------------------------------------------------
/ngx_rtmp_flv_live_index_module.c:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Winshining
4 | */
5 |
6 | #include
7 | #include
8 | #include "ngx_http_flv_live_module.h"
9 |
10 |
11 | static ngx_rtmp_play_pt next_play;
12 | static ngx_rtmp_close_stream_pt next_close_stream;
13 |
14 |
15 | static ngx_int_t ngx_rtmp_flv_live_index_postconfiguration(ngx_conf_t *cf);
16 |
17 |
18 | static ngx_rtmp_module_t ngx_rtmp_flv_live_module_ctx = {
19 | NULL,
20 | ngx_rtmp_flv_live_index_postconfiguration, /* postconfiguration */
21 | NULL,
22 | NULL,
23 | NULL,
24 | NULL,
25 | NULL,
26 | NULL
27 | };
28 |
29 |
30 | static ngx_command_t ngx_rtmp_flv_live_index_commands[] = {
31 | ngx_null_command
32 | };
33 |
34 |
35 | ngx_module_t ngx_rtmp_flv_live_index_module = {
36 | NGX_MODULE_V1,
37 | &ngx_rtmp_flv_live_module_ctx,
38 | ngx_rtmp_flv_live_index_commands,
39 | NGX_RTMP_MODULE,
40 | NULL,
41 | NULL,
42 | NULL,
43 | NULL,
44 | NULL,
45 | NULL,
46 | NULL,
47 | NGX_MODULE_V1_PADDING
48 | };
49 |
50 |
51 | static ngx_int_t
52 | ngx_rtmp_flv_live_index_postconfiguration(ngx_conf_t *cf)
53 | {
54 | next_play = ngx_rtmp_play;
55 | ngx_rtmp_play = ngx_http_flv_live_play;
56 |
57 | next_close_stream = ngx_rtmp_close_stream;
58 | ngx_rtmp_close_stream = ngx_http_flv_live_close_stream;
59 |
60 | http_flv_live_next_play = next_play;
61 | http_flv_live_next_close_stream = next_close_stream;
62 |
63 | return NGX_OK;
64 | }
65 |
66 |
--------------------------------------------------------------------------------
/ngx_rtmp_gop_cache_module.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Gnolizuh
4 | * Copyright (C) Winshining
5 | */
6 |
7 | #ifndef _NGX_RTMP_GOP_CACHE_H_INCLUDE_
8 | #define _NGX_RTMP_GOP_CACHE_H_INCLUDE_
9 |
10 |
11 | #define NGX_GOP_CACHE_POOL_CREATE_SIZE 4096
12 |
13 |
14 | typedef struct ngx_rtmp_gop_frame_s ngx_rtmp_gop_frame_t;
15 | typedef struct ngx_rtmp_gop_cache_s ngx_rtmp_gop_cache_t;
16 |
17 |
18 | struct ngx_rtmp_gop_frame_s {
19 | ngx_rtmp_header_t h;
20 | ngx_uint_t prio;
21 | ngx_chain_t *frame;
22 | ngx_rtmp_gop_frame_t *next;
23 | };
24 |
25 |
26 | struct ngx_rtmp_gop_cache_s {
27 | ngx_rtmp_gop_frame_t *frame_head;
28 | ngx_rtmp_gop_frame_t *frame_tail;
29 | ngx_rtmp_gop_cache_t *next;
30 |
31 | ngx_chain_t *video_seq_header;
32 | ngx_chain_t *audio_seq_header;
33 | ngx_chain_t *meta;
34 |
35 | ngx_uint_t meta_version;
36 |
37 | ngx_int_t video_frame_in_this;
38 | ngx_int_t audio_frame_in_this;
39 | };
40 |
41 |
42 | typedef struct ngx_rtmp_gop_cache_app_conf_s {
43 | ngx_flag_t gop_cache;
44 | size_t gop_cache_count;
45 | size_t gop_max_frame_count;
46 | size_t gop_max_video_count;
47 | size_t gop_max_audio_count;
48 | } ngx_rtmp_gop_cache_app_conf_t;
49 |
50 |
51 | typedef struct ngx_rtmp_gop_cache_ctx_s {
52 | ngx_pool_t *pool;
53 | ngx_rtmp_gop_cache_t *cache_head;
54 | ngx_rtmp_gop_cache_t *cache_tail;
55 | ngx_rtmp_gop_cache_t *free_cache;
56 | ngx_rtmp_gop_frame_t *free_frame;
57 |
58 | size_t gop_cache_count;
59 | size_t video_frame_in_all;
60 | size_t audio_frame_in_all;
61 | } ngx_rtmp_gop_cache_ctx_t;
62 |
63 |
64 | #endif
65 |
66 |
--------------------------------------------------------------------------------
/ngx_rtmp_init.c:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | * Copyright (C) Winshining
5 | */
6 |
7 |
8 | #include
9 | #include
10 | #include "ngx_rtmp.h"
11 | #include "ngx_rtmp_proxy_protocol.h"
12 |
13 |
14 | static void ngx_rtmp_close_connection(ngx_connection_t *c);
15 | static void ngx_rtmp_process_unix_socket(ngx_rtmp_connection_t *rconn);
16 |
17 |
18 | void
19 | ngx_rtmp_init_connection(ngx_connection_t *c)
20 | {
21 | ngx_uint_t i;
22 | ngx_rtmp_port_t *port;
23 | struct sockaddr_in *sin;
24 | ngx_rtmp_in_addr_t *addr;
25 | ngx_rtmp_connection_t *rconn;
26 | ngx_rtmp_session_t *s;
27 | ngx_int_t unix_socket;
28 | #if (NGX_HAVE_INET6)
29 | struct sockaddr_in6 *sin6;
30 | ngx_rtmp_in6_addr_t *addr6;
31 | #endif
32 |
33 | rconn = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_connection_t));
34 | if (rconn == NULL) {
35 | ngx_rtmp_close_connection(c);
36 | return;
37 | }
38 |
39 | ++ngx_rtmp_naccepted;
40 |
41 | c->data = rconn;
42 |
43 | /* find the server configuration for the address:port */
44 |
45 | port = c->listening->servers;
46 | unix_socket = 0;
47 |
48 | if (port->naddrs > 1) {
49 |
50 | /*
51 | * There are several addresses on this port and one of them
52 | * is the "*:port" wildcard so getsockname() is needed to determine
53 | * the server address.
54 | *
55 | * AcceptEx() already gave this address.
56 | */
57 |
58 | if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
59 | ngx_rtmp_close_connection(c);
60 | return;
61 | }
62 |
63 | switch (c->local_sockaddr->sa_family) {
64 |
65 | #if (NGX_HAVE_INET6)
66 | case AF_INET6:
67 | sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
68 |
69 | addr6 = port->addrs;
70 |
71 | /* the last address is "*" */
72 |
73 | for (i = 0; i < port->naddrs - 1; i++) {
74 | if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
75 | break;
76 | }
77 | }
78 |
79 | rconn->addr_conf = &addr6[i].conf;
80 |
81 | break;
82 | #endif
83 |
84 | case AF_UNIX:
85 | unix_socket = 1;
86 |
87 | ngx_rtmp_process_unix_socket(rconn);
88 |
89 | break;
90 |
91 | default: /* AF_INET */
92 | sin = (struct sockaddr_in *) c->local_sockaddr;
93 |
94 | addr = port->addrs;
95 |
96 | /* the last address is "*" */
97 |
98 | for (i = 0; i < port->naddrs - 1; i++) {
99 | if (addr[i].addr == sin->sin_addr.s_addr) {
100 | break;
101 | }
102 | }
103 |
104 | rconn->addr_conf = &addr[i].conf;
105 |
106 | break;
107 | }
108 |
109 | } else {
110 | switch (c->local_sockaddr->sa_family) {
111 |
112 | #if (NGX_HAVE_INET6)
113 | case AF_INET6:
114 | addr6 = port->addrs;
115 | rconn->addr_conf = &addr6[0].conf;
116 | break;
117 | #endif
118 |
119 | case AF_UNIX:
120 | unix_socket = 1;
121 |
122 | ngx_rtmp_process_unix_socket(rconn);
123 |
124 | break;
125 |
126 | default: /* AF_INET */
127 | addr = port->addrs;
128 | rconn->addr_conf = &addr[0].conf;
129 | break;
130 | }
131 | }
132 |
133 | /* the default server configuration for the address:port */
134 | rconn->conf_ctx = rconn->addr_conf->default_server->ctx;
135 |
136 | ngx_log_error(NGX_LOG_INFO, c->log, 0, "*%ui client connected '%V'",
137 | c->number, &c->addr_text);
138 |
139 | s = ngx_rtmp_init_session(c, rconn->addr_conf);
140 | if (s == NULL) {
141 | return;
142 | }
143 |
144 | /* only auto-pushed connections are
145 | * done through unix socket */
146 |
147 | s->auto_pushed = unix_socket;
148 |
149 | if (rconn->addr_conf->proxy_protocol) {
150 | ngx_rtmp_proxy_protocol(s);
151 |
152 | } else {
153 | ngx_rtmp_handshake(s);
154 | }
155 | }
156 |
157 |
158 | ngx_rtmp_session_t *
159 | ngx_rtmp_init_session(ngx_connection_t *c, ngx_rtmp_addr_conf_t *addr_conf)
160 | {
161 | ngx_rtmp_session_t *s;
162 | ngx_rtmp_core_srv_conf_t *cscf;
163 | ngx_rtmp_error_log_ctx_t *ctx;
164 |
165 | s = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_session_t));
166 | if (s == NULL) {
167 | goto failed;
168 | }
169 |
170 | s->rtmp_connection = c->data;
171 |
172 | s->main_conf = addr_conf->default_server->ctx->main_conf;
173 | s->srv_conf = addr_conf->default_server->ctx->srv_conf;
174 |
175 | s->addr_text = &addr_conf->addr_text;
176 |
177 | c->data = s;
178 | s->connection = c;
179 |
180 | ctx = ngx_palloc(c->pool, sizeof(ngx_rtmp_error_log_ctx_t));
181 | if (ctx == NULL) {
182 | goto failed;
183 | }
184 |
185 | ctx->client = &c->addr_text;
186 | ctx->session = s;
187 |
188 | c->log->connection = c->number;
189 | c->log->handler = ngx_rtmp_log_error;
190 | c->log->data = ctx;
191 | c->log->action = NULL;
192 |
193 | c->log_error = NGX_ERROR_INFO;
194 |
195 | s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_rtmp_max_module);
196 | if (s->ctx == NULL) {
197 | goto failed;
198 | }
199 |
200 | s->out_pool = ngx_create_pool(4096, c->log);
201 | if (s->out_pool == NULL) {
202 | goto failed;
203 | }
204 |
205 | s->out = ngx_pcalloc(s->out_pool, sizeof(ngx_chain_t *)
206 | * ((ngx_rtmp_core_srv_conf_t *)
207 | addr_conf->default_server->ctx->srv_conf
208 | [ngx_rtmp_core_module.ctx_index])->out_queue);
209 | if (s->out == NULL) {
210 | goto failed;
211 | }
212 |
213 | s->in_streams_pool = ngx_create_pool(4096, c->log);
214 | if (s->in_streams_pool == NULL) {
215 | goto failed;
216 | }
217 |
218 | cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
219 |
220 | s->out_queue = cscf->out_queue;
221 | s->out_cork = cscf->out_cork;
222 | s->in_streams = ngx_pcalloc(s->in_streams_pool, sizeof(ngx_rtmp_stream_t)
223 | * cscf->max_streams);
224 | if (s->in_streams == NULL) {
225 | goto failed;
226 | }
227 |
228 | #if (nginx_version >= 1007005)
229 | ngx_queue_init(&s->posted_dry_events);
230 | #endif
231 |
232 | s->epoch = ngx_current_msec;
233 | s->timeout = cscf->timeout;
234 | s->buflen = cscf->buflen;
235 | ngx_rtmp_set_chunk_size(s, NGX_RTMP_DEFAULT_CHUNK_SIZE);
236 |
237 |
238 | if (ngx_rtmp_fire_event(s, NGX_RTMP_CONNECT, NULL, NULL) != NGX_OK) {
239 | ngx_rtmp_finalize_session(s);
240 | return NULL;
241 | }
242 |
243 | return s;
244 |
245 | failed:
246 | if (s && s->out_pool) {
247 | ngx_destroy_pool(s->out_pool);
248 | s->out_pool = NULL;
249 | }
250 |
251 | if (s && s->in_streams_pool) {
252 | ngx_destroy_pool(s->in_streams_pool);
253 | s->in_streams_pool = NULL;
254 | }
255 |
256 | ngx_rtmp_close_connection(c);
257 | return NULL;
258 | }
259 |
260 |
261 | u_char *
262 | ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len)
263 | {
264 | u_char *p;
265 | ngx_rtmp_session_t *s;
266 | ngx_rtmp_error_log_ctx_t *ctx;
267 |
268 | if (log->action) {
269 | p = ngx_snprintf(buf, len, " while %s", log->action);
270 | len -= p - buf;
271 | buf = p;
272 | }
273 |
274 | ctx = log->data;
275 |
276 | p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
277 | len -= p - buf;
278 | buf = p;
279 |
280 | s = ctx->session;
281 |
282 | if (s == NULL) {
283 | return p;
284 | }
285 |
286 | p = ngx_snprintf(buf, len, ", server: %V", s->addr_text);
287 | len -= p - buf;
288 | buf = p;
289 |
290 | return p;
291 | }
292 |
293 |
294 | static void
295 | ngx_rtmp_close_connection(ngx_connection_t *c)
296 | {
297 | ngx_pool_t *pool;
298 |
299 | ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "close connection");
300 |
301 | #if (NGX_STAT_STUB)
302 | (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
303 | #endif
304 |
305 | pool = c->pool;
306 | ngx_close_connection(c);
307 | ngx_destroy_pool(pool);
308 | }
309 |
310 |
311 | static void
312 | ngx_rtmp_close_session_handler(ngx_event_t *e)
313 | {
314 | ngx_rtmp_session_t *s;
315 | ngx_connection_t *c;
316 | ngx_rtmp_core_srv_conf_t *cscf;
317 |
318 | s = e->data;
319 | c = s->connection;
320 |
321 | cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
322 |
323 | ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "close session");
324 |
325 | ngx_rtmp_fire_event(s, NGX_RTMP_DISCONNECT, NULL, NULL);
326 |
327 | if (s->ping_evt.timer_set) {
328 | ngx_del_timer(&s->ping_evt);
329 | }
330 |
331 | if (s->in_old_pool) {
332 | ngx_destroy_pool(s->in_old_pool);
333 | }
334 |
335 | if (s->in_pool) {
336 | ngx_destroy_pool(s->in_pool);
337 | }
338 |
339 | ngx_rtmp_free_handshake_buffers(s);
340 |
341 | while (s->out_pos != s->out_last) {
342 | ngx_rtmp_free_shared_chain(cscf, s->out[s->out_pos]);
343 |
344 | s->out_pos++;
345 | s->out_pos %= s->out_queue;
346 | }
347 |
348 | if (s->in_streams_pool) {
349 | ngx_destroy_pool(s->in_streams_pool);
350 | }
351 |
352 | if (s->out_pool) {
353 | ngx_destroy_pool(s->out_pool);
354 | }
355 |
356 | ngx_rtmp_close_connection(c);
357 | }
358 |
359 |
360 | void
361 | ngx_rtmp_finalize_session(ngx_rtmp_session_t *s)
362 | {
363 | ngx_event_t *e;
364 | ngx_connection_t *c;
365 |
366 | c = s->connection;
367 | if (c->destroyed) {
368 | return;
369 | }
370 |
371 | ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "finalize session");
372 |
373 | c->destroyed = 1;
374 | e = &s->close;
375 | e->data = s;
376 | e->handler = ngx_rtmp_close_session_handler;
377 | e->log = c->log;
378 |
379 | ngx_post_event(e, &ngx_posted_events);
380 | }
381 |
382 |
383 | static void
384 | ngx_rtmp_process_unix_socket(ngx_rtmp_connection_t *rconn)
385 | {
386 | ngx_uint_t i;
387 | ngx_rtmp_port_t *port;
388 | struct sockaddr_in *sin;
389 | ngx_rtmp_in_addr_t *addr;
390 | #if (NGX_HAVE_INET6)
391 | struct sockaddr_in6 *sin6;
392 | ngx_rtmp_in6_addr_t *addr6;
393 | #endif
394 | ngx_listening_t *ls;
395 |
396 | ls = ngx_cycle->listening.elts;
397 | for (i = 0; i < ngx_cycle->listening.nelts; ++i, ++ls) {
398 | if (ls->handler == ngx_rtmp_init_connection) {
399 | break;
400 | }
401 | }
402 |
403 | port = ls->servers;
404 |
405 | if (port->naddrs > 1) {
406 | switch (ls->sockaddr->sa_family) {
407 |
408 | #if (NGX_HAVE_INET6)
409 | case AF_INET6:
410 | sin6 = (struct sockaddr_in6 *) ls->sockaddr;
411 |
412 | addr6 = port->addrs;
413 |
414 | /* the last address is "*" */
415 |
416 | for (i = 0; i < port->naddrs - 1; i++) {
417 | if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
418 | break;
419 | }
420 | }
421 |
422 | rconn->addr_conf = &addr6[i].conf;
423 |
424 | break;
425 | #endif
426 |
427 | default: /* AF_INET */
428 | sin = (struct sockaddr_in *) ls->sockaddr;
429 |
430 | addr = port->addrs;
431 |
432 | /* the last address is "*" */
433 |
434 | for (i = 0; i < port->naddrs - 1; i++) {
435 | if (addr[i].addr == sin->sin_addr.s_addr) {
436 | break;
437 | }
438 | }
439 |
440 | rconn->addr_conf = &addr[i].conf;
441 | }
442 | } else {
443 | switch (ls->sockaddr->sa_family) {
444 |
445 | #if (NGX_HAVE_INET6)
446 | case AF_INET6:
447 | addr6 = port->addrs;
448 | rconn->addr_conf = &addr6[0].conf;
449 | break;
450 | #endif
451 |
452 | default: /* AF_INET */
453 | addr = port->addrs;
454 | rconn->addr_conf = &addr[0].conf;
455 | }
456 | }
457 | }
458 |
459 |
--------------------------------------------------------------------------------
/ngx_rtmp_limit_module.c:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #include
8 | #include
9 | #include "ngx_rtmp.h"
10 |
11 |
12 | typedef struct {
13 | ngx_int_t max_conn;
14 | ngx_shm_zone_t *shm_zone;
15 | } ngx_rtmp_limit_main_conf_t;
16 |
17 |
18 | static ngx_str_t shm_name = ngx_string("rtmp_limit");
19 |
20 |
21 | static ngx_int_t ngx_rtmp_limit_postconfiguration(ngx_conf_t *cf);
22 | static void *ngx_rtmp_limit_create_main_conf(ngx_conf_t *cf);
23 |
24 |
25 | static ngx_command_t ngx_rtmp_limit_commands[] = {
26 |
27 | { ngx_string("max_connections"),
28 | NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
29 | ngx_conf_set_num_slot,
30 | NGX_RTMP_MAIN_CONF_OFFSET,
31 | offsetof(ngx_rtmp_limit_main_conf_t, max_conn),
32 | NULL },
33 |
34 | ngx_null_command
35 | };
36 |
37 |
38 | static ngx_rtmp_module_t ngx_rtmp_limit_module_ctx = {
39 | NULL, /* preconfiguration */
40 | ngx_rtmp_limit_postconfiguration, /* postconfiguration */
41 | ngx_rtmp_limit_create_main_conf, /* create main configuration */
42 | NULL, /* init main configuration */
43 | NULL, /* create server configuration */
44 | NULL, /* merge server configuration */
45 | NULL, /* create app configuration */
46 | NULL /* merge app configuration */
47 | };
48 |
49 |
50 | ngx_module_t ngx_rtmp_limit_module = {
51 | NGX_MODULE_V1,
52 | &ngx_rtmp_limit_module_ctx, /* module context */
53 | ngx_rtmp_limit_commands, /* module directives */
54 | NGX_RTMP_MODULE, /* module type */
55 | NULL, /* init master */
56 | NULL, /* init module */
57 | NULL, /* init process */
58 | NULL, /* init thread */
59 | NULL, /* exit thread */
60 | NULL, /* exit process */
61 | NULL, /* exit master */
62 | NGX_MODULE_V1_PADDING
63 | };
64 |
65 |
66 | static void *
67 | ngx_rtmp_limit_create_main_conf(ngx_conf_t *cf)
68 | {
69 | ngx_rtmp_limit_main_conf_t *lmcf;
70 |
71 | lmcf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_limit_main_conf_t));
72 | if (lmcf == NULL) {
73 | return NULL;
74 | }
75 |
76 | lmcf->max_conn = NGX_CONF_UNSET;
77 |
78 | return lmcf;
79 | }
80 |
81 |
82 | static ngx_int_t
83 | ngx_rtmp_limit_connect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
84 | ngx_chain_t *in)
85 | {
86 | ngx_rtmp_limit_main_conf_t *lmcf;
87 | ngx_slab_pool_t *shpool;
88 | ngx_shm_zone_t *shm_zone;
89 | uint32_t *nconn, n;
90 | ngx_int_t rc;
91 |
92 | lmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_limit_module);
93 | if (lmcf->max_conn == NGX_CONF_UNSET) {
94 | return NGX_OK;
95 | }
96 |
97 | shm_zone = lmcf->shm_zone;
98 | shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
99 | nconn = shm_zone->data;
100 |
101 | ngx_shmtx_lock(&shpool->mutex);
102 | n = ++*nconn;
103 | ngx_shmtx_unlock(&shpool->mutex);
104 |
105 | rc = n > (ngx_uint_t) lmcf->max_conn ? NGX_ERROR : NGX_OK;
106 |
107 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
108 | "limit: inc connection counter: %uD", n);
109 |
110 | if (rc != NGX_OK) {
111 | ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
112 | "limit: too many connections: %uD > %i",
113 | n, lmcf->max_conn);
114 | }
115 |
116 | return rc;
117 | }
118 |
119 |
120 | static ngx_int_t
121 | ngx_rtmp_limit_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
122 | ngx_chain_t *in)
123 | {
124 | ngx_rtmp_limit_main_conf_t *lmcf;
125 | ngx_slab_pool_t *shpool;
126 | ngx_shm_zone_t *shm_zone;
127 | uint32_t *nconn, n;
128 |
129 | lmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_limit_module);
130 | if (lmcf->max_conn == NGX_CONF_UNSET) {
131 | return NGX_OK;
132 | }
133 |
134 | shm_zone = lmcf->shm_zone;
135 | shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
136 | nconn = shm_zone->data;
137 |
138 | ngx_shmtx_lock(&shpool->mutex);
139 | n = --*nconn;
140 | ngx_shmtx_unlock(&shpool->mutex);
141 |
142 | (void) n;
143 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
144 | "limit: dec connection counter: %uD", n);
145 |
146 | return NGX_OK;
147 | }
148 |
149 |
150 | static ngx_int_t
151 | ngx_rtmp_limit_shm_init(ngx_shm_zone_t *shm_zone, void *data)
152 | {
153 | ngx_slab_pool_t *shpool;
154 | uint32_t *nconn;
155 |
156 | if (data) {
157 | shm_zone->data = data;
158 | return NGX_OK;
159 | }
160 |
161 | shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
162 |
163 | nconn = ngx_slab_alloc(shpool, 4);
164 | if (nconn == NULL) {
165 | return NGX_ERROR;
166 | }
167 |
168 | *nconn = 0;
169 |
170 | shm_zone->data = nconn;
171 |
172 | return NGX_OK;
173 | }
174 |
175 |
176 | static ngx_int_t
177 | ngx_rtmp_limit_postconfiguration(ngx_conf_t *cf)
178 | {
179 | ngx_rtmp_core_main_conf_t *cmcf;
180 | ngx_rtmp_limit_main_conf_t *lmcf;
181 | ngx_rtmp_handler_pt *h;
182 |
183 | cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
184 |
185 | h = ngx_array_push(&cmcf->events[NGX_RTMP_CONNECT]);
186 | *h = ngx_rtmp_limit_connect;
187 |
188 | h = ngx_array_push(&cmcf->events[NGX_RTMP_DISCONNECT]);
189 | *h = ngx_rtmp_limit_disconnect;
190 |
191 | lmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_limit_module);
192 | if (lmcf->max_conn == NGX_CONF_UNSET) {
193 | return NGX_OK;
194 | }
195 |
196 | lmcf->shm_zone = ngx_shared_memory_add(cf, &shm_name, ngx_pagesize * 2,
197 | &ngx_rtmp_limit_module);
198 | if (lmcf->shm_zone == NULL) {
199 | return NGX_ERROR;
200 | }
201 |
202 | lmcf->shm_zone->init = ngx_rtmp_limit_shm_init;
203 |
204 | return NGX_OK;
205 | }
206 |
--------------------------------------------------------------------------------
/ngx_rtmp_live_module.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | * Copyright (C) Winshining
5 | */
6 |
7 |
8 | #ifndef _NGX_RTMP_LIVE_H_INCLUDED_
9 | #define _NGX_RTMP_LIVE_H_INCLUDED_
10 |
11 |
12 | #include
13 | #include
14 | #include "ngx_rtmp.h"
15 | #include "ngx_rtmp_cmd_module.h"
16 | #include "ngx_rtmp_bandwidth.h"
17 | #include "ngx_rtmp_streams.h"
18 |
19 |
20 | typedef struct ngx_rtmp_live_ctx_s ngx_rtmp_live_ctx_t;
21 | typedef struct ngx_rtmp_live_stream_s ngx_rtmp_live_stream_t;
22 |
23 |
24 | typedef struct {
25 | unsigned active:1;
26 | uint32_t timestamp;
27 | uint32_t csid;
28 | uint32_t dropped;
29 | } ngx_rtmp_live_chunk_stream_t;
30 |
31 |
32 | struct ngx_rtmp_live_ctx_s {
33 | ngx_rtmp_session_t *session;
34 | ngx_rtmp_live_stream_t *stream;
35 | ngx_rtmp_live_ctx_t *next;
36 | ngx_uint_t ndropped;
37 | ngx_rtmp_live_chunk_stream_t cs[2];
38 | ngx_uint_t meta_version;
39 | ngx_event_t idle_evt;
40 | unsigned active:1;
41 | unsigned publishing:1;
42 | unsigned silent:1;
43 | unsigned paused:1;
44 | ngx_uint_t protocol;
45 | };
46 |
47 |
48 | struct ngx_rtmp_live_stream_s {
49 | u_char name[NGX_RTMP_MAX_NAME];
50 | ngx_rtmp_live_stream_t *next;
51 | ngx_rtmp_live_ctx_t *ctx;
52 | ngx_rtmp_live_ctx_t *pub_ctx;
53 | ngx_rtmp_bandwidth_t bw_in;
54 | ngx_rtmp_bandwidth_t bw_in_audio;
55 | ngx_rtmp_bandwidth_t bw_in_video;
56 | ngx_rtmp_bandwidth_t bw_in_data;
57 | ngx_rtmp_bandwidth_t bw_out;
58 | ngx_msec_t epoch;
59 | unsigned active:1;
60 | unsigned publishing:1;
61 | };
62 |
63 |
64 | typedef struct {
65 | ngx_int_t nbuckets;
66 | ngx_rtmp_live_stream_t **streams;
67 | ngx_flag_t live;
68 | ngx_flag_t meta;
69 | ngx_msec_t sync;
70 | ngx_msec_t idle_timeout;
71 | ngx_flag_t atc;
72 | ngx_flag_t interleave;
73 | ngx_flag_t wait_key;
74 | ngx_flag_t wait_video;
75 | ngx_flag_t publish_notify;
76 | ngx_flag_t play_restart;
77 | ngx_flag_t idle_streams;
78 | ngx_msec_t buflen;
79 | ngx_pool_t *pool;
80 | ngx_rtmp_live_stream_t *free_streams;
81 | } ngx_rtmp_live_app_conf_t;
82 |
83 |
84 | extern ngx_module_t ngx_rtmp_live_module;
85 |
86 |
87 | ngx_rtmp_live_stream_t **ngx_rtmp_live_get_stream(ngx_rtmp_session_t *s,
88 | u_char *name, int create);
89 |
90 |
91 | #endif /* _NGX_RTMP_LIVE_H_INCLUDED_ */
92 |
--------------------------------------------------------------------------------
/ngx_rtmp_netcall_module.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #ifndef _NGX_RTMP_NETCALL_H_INCLUDED_
8 | #define _NGX_RTMP_NETCALL_H_INCLUDED_
9 |
10 |
11 | #include
12 | #include
13 | #include "ngx_rtmp.h"
14 |
15 |
16 | typedef ngx_chain_t * (*ngx_rtmp_netcall_create_pt)(ngx_rtmp_session_t *s,
17 | void *arg, ngx_pool_t *pool);
18 | typedef ngx_int_t (*ngx_rtmp_netcall_filter_pt)(ngx_chain_t *in);
19 | typedef ngx_int_t (*ngx_rtmp_netcall_sink_pt)(ngx_rtmp_session_t *s,
20 | ngx_chain_t *in);
21 | typedef ngx_int_t (*ngx_rtmp_netcall_handle_pt)(ngx_rtmp_session_t *s,
22 | void *arg, ngx_chain_t *in);
23 |
24 | #define NGX_RTMP_NETCALL_HTTP_GET 0
25 | #define NGX_RTMP_NETCALL_HTTP_POST 1
26 |
27 |
28 | /* If handle is NULL then netcall is created detached
29 | * which means it's completely independent of RTMP
30 | * session and its result is never visible to anyone.
31 | *
32 | * WARNING: It's not recommended to create non-detached
33 | * netcalls from disconect handlers. Netcall disconnect
34 | * handler which detaches active netcalls is executed
35 | * BEFORE your handler. It leads to a crash
36 | * after netcall connection is closed */
37 | typedef struct {
38 | ngx_url_t *url;
39 | ngx_rtmp_netcall_create_pt create;
40 | ngx_rtmp_netcall_filter_pt filter;
41 | ngx_rtmp_netcall_sink_pt sink;
42 | ngx_rtmp_netcall_handle_pt handle;
43 | void *arg;
44 | size_t argsize;
45 | } ngx_rtmp_netcall_init_t;
46 |
47 |
48 | ngx_int_t ngx_rtmp_netcall_create(ngx_rtmp_session_t *s,
49 | ngx_rtmp_netcall_init_t *ci);
50 |
51 |
52 | /* HTTP handling */
53 | ngx_chain_t * ngx_rtmp_netcall_http_format_session(ngx_rtmp_session_t *s,
54 | ngx_pool_t *pool);
55 | ngx_chain_t * ngx_rtmp_netcall_http_format_request(ngx_int_t method,
56 | ngx_str_t *host, ngx_str_t *uri, ngx_chain_t *args, ngx_chain_t *body,
57 | ngx_pool_t *pool, ngx_str_t *content_type);
58 | ngx_chain_t * ngx_rtmp_netcall_http_skip_header(ngx_chain_t *in);
59 |
60 |
61 | /* Memcache handling */
62 | ngx_chain_t * ngx_rtmp_netcall_memcache_set(ngx_rtmp_session_t *s,
63 | ngx_pool_t *pool, ngx_str_t *key, ngx_str_t *value,
64 | ngx_uint_t flags, ngx_uint_t sec);
65 |
66 |
67 | #endif /* _NGX_RTMP_NETCALL_H_INCLUDED_ */
68 |
--------------------------------------------------------------------------------
/ngx_rtmp_play_module.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #ifndef _NGX_RTMP_PLAY_H_INCLUDED_
8 | #define _NGX_RTMP_PLAY_H_INCLUDED_
9 |
10 |
11 | #include
12 | #include
13 | #include "ngx_rtmp.h"
14 | #include "ngx_rtmp_cmd_module.h"
15 |
16 |
17 | typedef ngx_int_t (*ngx_rtmp_play_init_pt) (ngx_rtmp_session_t *s,
18 | ngx_file_t *f, ngx_int_t aindex, ngx_int_t vindex);
19 | typedef ngx_int_t (*ngx_rtmp_play_done_pt) (ngx_rtmp_session_t *s,
20 | ngx_file_t *f);
21 | typedef ngx_int_t (*ngx_rtmp_play_start_pt) (ngx_rtmp_session_t *s,
22 | ngx_file_t *f);
23 | typedef ngx_int_t (*ngx_rtmp_play_seek_pt) (ngx_rtmp_session_t *s,
24 | ngx_file_t *f, ngx_uint_t offs);
25 | typedef ngx_int_t (*ngx_rtmp_play_stop_pt) (ngx_rtmp_session_t *s,
26 | ngx_file_t *f);
27 | typedef ngx_int_t (*ngx_rtmp_play_send_pt) (ngx_rtmp_session_t *s,
28 | ngx_file_t *f, ngx_uint_t *ts);
29 |
30 |
31 | typedef struct {
32 | ngx_str_t name;
33 | ngx_str_t pfx;
34 | ngx_str_t sfx;
35 |
36 | ngx_rtmp_play_init_pt init;
37 | ngx_rtmp_play_done_pt done;
38 | ngx_rtmp_play_start_pt start;
39 | ngx_rtmp_play_seek_pt seek;
40 | ngx_rtmp_play_stop_pt stop;
41 | ngx_rtmp_play_send_pt send;
42 | } ngx_rtmp_play_fmt_t;
43 |
44 |
45 | typedef struct ngx_rtmp_play_ctx_s ngx_rtmp_play_ctx_t;
46 |
47 |
48 | struct ngx_rtmp_play_ctx_s {
49 | ngx_rtmp_session_t *session;
50 | ngx_file_t file;
51 | ngx_rtmp_play_fmt_t *fmt;
52 | ngx_event_t send_evt;
53 | unsigned playing:1;
54 | unsigned opened:1;
55 | unsigned joined:1;
56 | ngx_uint_t ncrs;
57 | ngx_uint_t nheader;
58 | ngx_uint_t nbody;
59 | size_t pfx_size;
60 | ngx_str_t sfx;
61 | ngx_uint_t file_id;
62 | ngx_int_t aindex, vindex;
63 | ngx_uint_t nentry;
64 | ngx_uint_t post_seek;
65 | u_char name[NGX_RTMP_MAX_NAME];
66 | ngx_rtmp_play_ctx_t *next;
67 | };
68 |
69 |
70 | typedef struct {
71 | ngx_str_t *root;
72 | ngx_url_t *url;
73 | } ngx_rtmp_play_entry_t;
74 |
75 |
76 | typedef struct {
77 | ngx_str_t temp_path;
78 | ngx_str_t local_path;
79 | ngx_array_t entries; /* ngx_rtmp_play_entry_t * */
80 | ngx_uint_t nbuckets;
81 | ngx_rtmp_play_ctx_t **ctx;
82 | } ngx_rtmp_play_app_conf_t;
83 |
84 |
85 | typedef struct {
86 | ngx_array_t fmts; /* ngx_rtmp_play_fmt_t * */
87 | } ngx_rtmp_play_main_conf_t;
88 |
89 |
90 | extern ngx_module_t ngx_rtmp_play_module;
91 |
92 |
93 | #endif /* _NGX_RTMP_PLAY_H_INCLUDED_ */
94 |
--------------------------------------------------------------------------------
/ngx_rtmp_proxy_protocol.c:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #include
8 | #include
9 | #include
10 | #include "ngx_rtmp_proxy_protocol.h"
11 |
12 |
13 | static void ngx_rtmp_proxy_protocol_recv(ngx_event_t *rev);
14 |
15 |
16 | void
17 | ngx_rtmp_proxy_protocol(ngx_rtmp_session_t *s)
18 | {
19 | ngx_event_t *rev;
20 | ngx_connection_t *c;
21 |
22 | c = s->connection;
23 | rev = c->read;
24 | rev->handler = ngx_rtmp_proxy_protocol_recv;
25 |
26 | ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
27 | "proxy_protocol: start");
28 |
29 | if (rev->ready) {
30 | /* the deferred accept(), rtsig, aio, iocp */
31 |
32 | if (ngx_use_accept_mutex) {
33 | ngx_post_event(rev, &ngx_posted_events);
34 | return;
35 | }
36 |
37 | rev->handler(rev);
38 | return;
39 | }
40 |
41 | ngx_add_timer(rev, s->timeout);
42 |
43 | if (ngx_handle_read_event(rev, 0) != NGX_OK) {
44 | ngx_rtmp_finalize_session(s);
45 | return;
46 | }
47 | }
48 |
49 |
50 | static void
51 | ngx_rtmp_proxy_protocol_recv(ngx_event_t *rev)
52 | {
53 | u_char buf[107], *p, *pp, *text;
54 | size_t len;
55 | ssize_t n;
56 | ngx_err_t err;
57 | ngx_int_t i;
58 | ngx_addr_t addr;
59 | ngx_connection_t *c;
60 | ngx_rtmp_session_t *s;
61 |
62 | c = rev->data;
63 | s = c->data;
64 |
65 | if (c->destroyed) {
66 | return;
67 | }
68 |
69 | if (rev->timedout) {
70 | ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
71 | "proxy_protocol: recv: client timed out");
72 | c->timedout = 1;
73 | ngx_rtmp_finalize_session(s);
74 | return;
75 | }
76 |
77 | if (rev->timer_set) {
78 | ngx_del_timer(rev);
79 | }
80 |
81 | n = recv(c->fd, (char *) buf, sizeof(buf), MSG_PEEK);
82 |
83 | err = ngx_socket_errno;
84 |
85 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0, "recv(): %d", n);
86 |
87 | if (n == -1) {
88 |
89 | if (err == NGX_EAGAIN) {
90 | ngx_add_timer(rev, s->timeout);
91 |
92 | if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
93 | ngx_rtmp_finalize_session(s);
94 | }
95 |
96 | return;
97 | }
98 |
99 | ngx_rtmp_finalize_session(s);
100 |
101 | return;
102 | }
103 |
104 | p = buf;
105 |
106 | if (n <= 8 && ngx_strncmp(p, "PROXY ", 6) != 0) {
107 | goto bad_header;
108 | }
109 |
110 | n -= 6;
111 | p += 6;
112 |
113 | ngx_memzero(&addr, sizeof(ngx_addr_t));
114 |
115 | if (n >= 7 && ngx_strncmp(p, "UNKNOWN", 7) == 0) {
116 | n -= 7;
117 | p += 7;
118 | goto skip;
119 | }
120 |
121 | if (n < 5 || ngx_strncmp(p, "TCP", 3) != 0
122 | || (p[3] != '4' && p[3] != '6') || p[4] != ' ')
123 | {
124 | goto bad_header;
125 | }
126 |
127 | n -= 5;
128 | p += 5;
129 |
130 | pp = ngx_strlchr(p, p + n, ' ');
131 |
132 | if (pp == NULL) {
133 | goto bad_header;
134 | }
135 |
136 | if (ngx_parse_addr(s->connection->pool, &addr, p, pp - p) != NGX_OK) {
137 | goto bad_header;
138 | }
139 |
140 | n -= pp - p;
141 | p = pp;
142 |
143 | skip:
144 |
145 | for (i = 0; i + 1 < n; i++) {
146 | if (p[i] == CR && p[i + 1] == LF) {
147 | break;
148 | }
149 | }
150 |
151 | if (i + 1 >= n) {
152 | goto bad_header;
153 | }
154 |
155 | n = p - buf + i + 2;
156 |
157 | if (c->recv(c, buf, n) != n) {
158 | goto failed;
159 | }
160 |
161 | if (addr.socklen) {
162 | text = ngx_palloc(s->connection->pool, NGX_SOCKADDR_STRLEN);
163 |
164 | if (text == NULL) {
165 | goto failed;
166 | }
167 |
168 | len = ngx_sock_ntop(addr.sockaddr,
169 | #if (nginx_version >= 1005003)
170 | addr.socklen,
171 | #endif
172 | text, NGX_SOCKADDR_STRLEN, 0);
173 | if (len == 0) {
174 | goto failed;
175 | }
176 |
177 | c->sockaddr = addr.sockaddr;
178 | c->socklen = addr.socklen;
179 | c->addr_text.data = text;
180 | c->addr_text.len = len;
181 |
182 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
183 | "proxy_protocol: remote_addr:'%V'", &c->addr_text);
184 | }
185 |
186 | ngx_rtmp_handshake(s);
187 |
188 | return;
189 |
190 | bad_header:
191 |
192 | ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxy_protocol: bad header");
193 |
194 | failed:
195 |
196 | ngx_rtmp_finalize_session(s);
197 | }
198 |
--------------------------------------------------------------------------------
/ngx_rtmp_proxy_protocol.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #ifndef _NGX_RTMP_PROXY_PROTOCOL_H_INCLUDED_
8 | #define _NGX_RTMP_PROXY_PROTOCOL_H_INCLUDED_
9 |
10 |
11 | #include
12 | #include
13 | #include "ngx_rtmp.h"
14 |
15 |
16 | void ngx_rtmp_proxy_protocol(ngx_rtmp_session_t *c);
17 |
18 |
19 | #endif /* _NGX_RTMP_PROXY_PROTOCOL_H_INCLUDED_ */
20 |
--------------------------------------------------------------------------------
/ngx_rtmp_receive.c:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | * Copyright (C) Winshining
5 | */
6 |
7 |
8 | #include
9 | #include
10 | #include "ngx_rtmp.h"
11 | #include "ngx_rtmp_amf.h"
12 | #include "ngx_rtmp_cmd_module.h"
13 | #include
14 |
15 |
16 | ngx_int_t
17 | ngx_rtmp_protocol_message_handler(ngx_rtmp_session_t *s,
18 | ngx_rtmp_header_t *h, ngx_chain_t *in)
19 | {
20 | ngx_buf_t *b;
21 | uint32_t val;
22 | uint8_t limit;
23 |
24 | b = in->buf;
25 |
26 | if (b->last - b->pos < 4) {
27 | ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
28 | "too small buffer for %d message: %d",
29 | (int)h->type, b->last - b->pos);
30 | return NGX_OK;
31 | }
32 |
33 | val = ntohl(*(uint32_t *) b->pos);
34 |
35 | switch(h->type) {
36 | case NGX_RTMP_MSG_CHUNK_SIZE:
37 | /* set chunk size =val */
38 | ngx_rtmp_set_chunk_size(s, val);
39 | break;
40 |
41 | case NGX_RTMP_MSG_ABORT:
42 | /* abort chunk stream =val */
43 | break;
44 |
45 | case NGX_RTMP_MSG_ACK:
46 | /* receive ack with sequence number =val */
47 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
48 | "receive ack seq=%uD", val);
49 | break;
50 |
51 | case NGX_RTMP_MSG_ACK_SIZE:
52 | /* receive window size =val */
53 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
54 | "receive ack_size=%uD", val);
55 | s->ack_size = val;
56 | break;
57 |
58 | case NGX_RTMP_MSG_BANDWIDTH:
59 | if (b->last - b->pos >= 5) {
60 | limit = *(uint8_t*)&b->pos[4];
61 |
62 | (void)val;
63 | (void)limit;
64 |
65 | ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
66 | "receive bandwidth=%uD limit=%d",
67 | val, (int)limit);
68 |
69 | /* receive window size =val
70 | * && limit */
71 | }
72 | break;
73 |
74 | default:
75 | return NGX_ERROR;
76 | }
77 |
78 | return NGX_OK;
79 | }
80 |
81 |
82 | ngx_int_t
83 | ngx_rtmp_user_message_handler(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
84 | ngx_chain_t *in)
85 | {
86 | ngx_buf_t *b;
87 | uint16_t evt;
88 | uint32_t val;
89 |
90 | b = in->buf;
91 |
92 | if (b->last - b->pos < 6) {
93 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
94 | "too small buffer for user message: %d",
95 | b->last - b->pos);
96 | return NGX_OK;
97 | }
98 |
99 | evt = ntohs(*(uint16_t *) b->pos);
100 |
101 | ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
102 | "RTMP recv user evt %s (%i)",
103 | ngx_rtmp_user_message_type(evt), (ngx_int_t) evt);
104 |
105 | val = ntohl(*(uint32_t *) (b->pos + 2));
106 |
107 | switch(evt) {
108 | case NGX_RTMP_USER_STREAM_BEGIN:
109 | {
110 | ngx_rtmp_stream_begin_t v;
111 |
112 | v.msid = val;
113 |
114 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
115 | "receive: stream_begin msid=%uD", v.msid);
116 |
117 | return ngx_rtmp_stream_begin(s, &v);
118 | }
119 |
120 | case NGX_RTMP_USER_STREAM_EOF:
121 | {
122 | ngx_rtmp_stream_eof_t v;
123 |
124 | v.msid = val;
125 |
126 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
127 | "receive: stream_eof msid=%uD", v.msid);
128 |
129 | return ngx_rtmp_stream_eof(s, &v);
130 | }
131 |
132 | case NGX_RTMP_USER_STREAM_DRY:
133 | {
134 | ngx_rtmp_stream_dry_t v;
135 |
136 | v.msid = val;
137 |
138 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
139 | "receive: stream_dry msid=%uD", v.msid);
140 |
141 | return ngx_rtmp_stream_dry(s, &v);
142 | }
143 |
144 | case NGX_RTMP_USER_SET_BUFLEN:
145 | {
146 | ngx_rtmp_set_buflen_t v;
147 |
148 | v.msid = val;
149 |
150 | if (b->last - b->pos < 10) {
151 | return NGX_OK;
152 | }
153 |
154 | v.buflen = ntohl(*(uint32_t *) (b->pos + 6));
155 |
156 | ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
157 | "receive: set_buflen msid=%uD buflen=%uD",
158 | v.msid, v.buflen);
159 |
160 | /*TODO: move this to play module */
161 | s->buflen = v.buflen;
162 |
163 | return ngx_rtmp_set_buflen(s, &v);
164 | }
165 |
166 | case NGX_RTMP_USER_RECORDED:
167 | {
168 | ngx_rtmp_recorded_t v;
169 |
170 | v.msid = val;
171 |
172 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
173 | "receive: recorded msid=%uD", v.msid);
174 |
175 | return ngx_rtmp_recorded(s, &v);
176 | }
177 |
178 | case NGX_RTMP_USER_PING_REQUEST:
179 | return ngx_rtmp_send_ping_response(s, val);
180 |
181 | case NGX_RTMP_USER_PING_RESPONSE:
182 |
183 | /* val = incoming timestamp */
184 |
185 | ngx_rtmp_reset_ping(s);
186 |
187 | return NGX_OK;
188 |
189 | default:
190 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
191 | "unexpected user event: %i", (ngx_int_t) evt);
192 |
193 | return NGX_OK;
194 | }
195 | }
196 |
197 |
198 | static ngx_int_t
199 | ngx_rtmp_fetch(ngx_chain_t **in, u_char *ret)
200 | {
201 | while (*in && (*in)->buf->pos >= (*in)->buf->last) {
202 | *in = (*in)->next;
203 | }
204 |
205 | if (*in == NULL) {
206 | return NGX_DONE;
207 | }
208 |
209 | *ret = *(*in)->buf->pos++;
210 |
211 | return NGX_OK;
212 | }
213 |
214 |
215 | static ngx_int_t
216 | ngx_rtmp_fetch_uint8(ngx_chain_t **in, uint8_t *ret)
217 | {
218 | return ngx_rtmp_fetch(in, (u_char *) ret);
219 | }
220 |
221 |
222 | static ngx_int_t
223 | ngx_rtmp_fetch_uint32(ngx_chain_t **in, uint32_t *ret, ngx_int_t n)
224 | {
225 | u_char r;
226 | ngx_int_t rc;
227 | uint32_t val;
228 |
229 | *ret = 0;
230 | val = 0;
231 |
232 | while (--n >= 0) {
233 | rc = ngx_rtmp_fetch(in, &r);
234 | if (rc != NGX_OK) {
235 | return rc;
236 | }
237 |
238 | val = (val << 8) | r;
239 | }
240 |
241 | *ret = val;
242 |
243 | return NGX_OK;
244 | }
245 |
246 |
247 | ngx_int_t
248 | ngx_rtmp_aggregate_message_handler(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
249 | ngx_chain_t *in)
250 | {
251 | uint32_t base_time, timestamp, prev_size;
252 | size_t len;
253 | ngx_int_t first;
254 | u_char *last;
255 | ngx_int_t rc;
256 | ngx_buf_t *b;
257 | ngx_chain_t *cl, *next;
258 | ngx_rtmp_header_t ch;
259 |
260 | ch = *h;
261 |
262 | first = 1;
263 | base_time = 0;
264 |
265 | while (in) {
266 | if (ngx_rtmp_fetch_uint8(&in, &ch.type) != NGX_OK) {
267 | return NGX_OK;
268 | }
269 |
270 | if (ngx_rtmp_fetch_uint32(&in, &ch.mlen, 3) != NGX_OK) {
271 | return NGX_ERROR;
272 | }
273 |
274 | if (ngx_rtmp_fetch_uint32(&in, ×tamp, 3) != NGX_OK) {
275 | return NGX_ERROR;
276 | }
277 |
278 | if (ngx_rtmp_fetch_uint8(&in, (uint8_t *) ×tamp + 3) != NGX_OK)
279 | {
280 | return NGX_ERROR;
281 | }
282 |
283 | if (ngx_rtmp_fetch_uint32(&in, &ch.msid, 3) != NGX_OK)
284 | {
285 | return NGX_ERROR;
286 | }
287 |
288 | if (first) {
289 | base_time = timestamp;
290 | first = 0;
291 | }
292 |
293 | ngx_log_debug6(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
294 | "RTMP aggregate %s (%d) len=%uD time=%uD (+%D) msid=%uD",
295 | ngx_rtmp_message_type(ch.type),
296 | (ngx_int_t) ch.type, ch.mlen, ch.timestamp,
297 | timestamp - base_time, ch.msid);
298 |
299 | /* limit chain */
300 |
301 | len = 0;
302 | cl = in;
303 | while (cl) {
304 | b = cl->buf;
305 | len += (b->last - b->pos);
306 | if (len > ch.mlen) {
307 | break;
308 | }
309 | cl = cl->next;
310 | }
311 |
312 | if (cl == NULL) {
313 | ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
314 | "RTMP error parsing aggregate");
315 | return NGX_ERROR;
316 | }
317 |
318 | next = cl->next;
319 | cl->next = NULL;
320 | b = cl->buf;
321 | last = b->last;
322 | b->last -= (len - ch.mlen);
323 |
324 | /* handle aggregated message */
325 |
326 | ch.timestamp = h->timestamp + timestamp - base_time;
327 |
328 | rc = ngx_rtmp_receive_message(s, &ch, in);
329 |
330 | /* restore chain before checking the result */
331 |
332 | in = cl;
333 | in->next = next;
334 | b->pos = b->last;
335 | b->last = last;
336 |
337 | if (rc != NGX_OK) {
338 | return rc;
339 | }
340 |
341 | /* read 32-bit previous tag size */
342 |
343 | if (ngx_rtmp_fetch_uint32(&in, &prev_size, 4) != NGX_OK) {
344 | return NGX_OK;
345 | }
346 |
347 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
348 | "RTMP aggregate prev_size=%uD", prev_size);
349 | }
350 |
351 | return NGX_OK;
352 | }
353 |
354 |
355 | ngx_int_t
356 | ngx_rtmp_amf_message_handler(ngx_rtmp_session_t *s,
357 | ngx_rtmp_header_t *h, ngx_chain_t *in)
358 | {
359 | ngx_rtmp_amf_ctx_t act;
360 | ngx_rtmp_core_main_conf_t *cmcf;
361 | ngx_array_t *ch;
362 | ngx_rtmp_handler_pt *ph;
363 | ngx_chain_t *cl;
364 | ngx_int_t amf_len;
365 | size_t len, n;
366 |
367 | static u_char func[128];
368 |
369 | static ngx_rtmp_amf_elt_t elts[] = {
370 |
371 | { NGX_RTMP_AMF_STRING,
372 | ngx_null_string,
373 | func, sizeof(func) },
374 | };
375 |
376 | /* AMF command names come with string type, but shared object names
377 | * come without type */
378 | if (h->type == NGX_RTMP_MSG_AMF_SHARED ||
379 | h->type == NGX_RTMP_MSG_AMF3_SHARED)
380 | {
381 | elts[0].type |= NGX_RTMP_AMF_TYPELESS;
382 | } else {
383 | elts[0].type &= ~NGX_RTMP_AMF_TYPELESS;
384 | }
385 |
386 | if ((h->type == NGX_RTMP_MSG_AMF3_SHARED ||
387 | h->type == NGX_RTMP_MSG_AMF3_META ||
388 | h->type == NGX_RTMP_MSG_AMF3_CMD)
389 | && in->buf->last > in->buf->pos)
390 | {
391 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
392 | "AMF3 prefix: %ui", (ngx_int_t)*in->buf->pos);
393 | ++in->buf->pos;
394 | }
395 |
396 | cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
397 |
398 | /*
399 | * work around the buggy option `-map` in FFmpeg, see:
400 | * https://trac.ffmpeg.org/ticket/10565
401 | */
402 | if (in->buf->pos[0] == NGX_RTMP_AMF_NUMBER) {
403 | cl = in;
404 | amf_len = 0;
405 |
406 | while (cl) {
407 | amf_len += cl->buf->last - cl->buf->pos;
408 | /* type: 1B, number payload: 8B */
409 | if (amf_len >= 9) {
410 | break;
411 | }
412 |
413 | cl = cl->next;
414 | }
415 |
416 | if (amf_len < 9) {
417 | ngx_log_error(NGX_LOG_WARN, s->connection->log, 0,
418 | "AMF malformed: type=%d, length=%D, ignored",
419 | NGX_RTMP_AMF_NUMBER, amf_len);
420 | return NGX_OK;
421 | }
422 | }
423 |
424 | /* read AMF func name & transaction id */
425 | ngx_memzero(&act, sizeof(act));
426 | act.link = in;
427 | act.log = s->connection->log;
428 | memset(func, 0, sizeof(func));
429 |
430 | if (ngx_rtmp_amf_read(&act, elts,
431 | sizeof(elts) / sizeof(elts[0])) != NGX_OK)
432 | {
433 | ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
434 | "AMF cmd failed");
435 | return NGX_ERROR;
436 | }
437 |
438 | /* skip name */
439 | in = act.link;
440 | in->buf->pos += act.offset;
441 |
442 | len = ngx_strlen(func);
443 |
444 | ch = ngx_hash_find(&cmcf->amf_hash,
445 | ngx_hash_strlow(func, func, len), func, len);
446 |
447 | if (ch && ch->nelts) {
448 | ph = ch->elts;
449 | for (n = 0; n < ch->nelts; ++n, ++ph) {
450 | ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
451 | "AMF func '%s' passed to handler %d/%d",
452 | func, n, ch->nelts);
453 | switch ((*ph)(s, h, in)) {
454 | case NGX_ERROR:
455 | return NGX_ERROR;
456 | case NGX_DONE:
457 | return NGX_OK;
458 | }
459 | }
460 | } else {
461 | ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
462 | "AMF cmd '%s' no handler", func);
463 | }
464 |
465 | return NGX_OK;
466 | }
467 |
468 |
469 | ngx_int_t
470 | ngx_rtmp_receive_amf(ngx_rtmp_session_t *s, ngx_chain_t *in,
471 | ngx_rtmp_amf_elt_t *elts, size_t nelts)
472 | {
473 | ngx_rtmp_amf_ctx_t act;
474 |
475 | ngx_memzero(&act, sizeof(act));
476 | act.link = in;
477 | act.log = s->connection->log;
478 |
479 | return ngx_rtmp_amf_read(&act, elts, nelts);
480 | }
481 |
--------------------------------------------------------------------------------
/ngx_rtmp_record_module.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | * Copyright (C) Winshining
5 | */
6 |
7 |
8 | #ifndef _NGX_RTMP_RECORD_H_INCLUDED_
9 | #define _NGX_RTMP_RECORD_H_INCLUDED_
10 |
11 |
12 | #include
13 | #include
14 | #include "ngx_rtmp.h"
15 |
16 |
17 | #define NGX_RTMP_RECORD_OFF 0x01
18 | #define NGX_RTMP_RECORD_AUDIO 0x02
19 | #define NGX_RTMP_RECORD_VIDEO 0x04
20 | #define NGX_RTMP_RECORD_KEYFRAMES 0x08
21 | #define NGX_RTMP_RECORD_MANUAL 0x10
22 |
23 |
24 | typedef struct {
25 | ngx_str_t id;
26 | ngx_uint_t flags;
27 | ngx_str_t path;
28 | size_t max_size;
29 | size_t max_frames;
30 | ngx_msec_t interval;
31 | ngx_str_t suffix;
32 | ngx_flag_t unique;
33 | ngx_flag_t append;
34 | ngx_flag_t lock_file;
35 | ngx_flag_t notify;
36 | ngx_url_t *url;
37 |
38 | void **rec_conf;
39 | ngx_array_t rec; /* ngx_rtmp_record_app_conf_t * */
40 | } ngx_rtmp_record_app_conf_t;
41 |
42 |
43 | typedef struct {
44 | ngx_rtmp_record_app_conf_t *conf;
45 | ngx_file_t file;
46 | ngx_uint_t nframes;
47 | uint32_t epoch, time_shift;
48 | ngx_time_t last;
49 | time_t timestamp;
50 | unsigned failed:1;
51 | unsigned initialized:1;
52 | unsigned aac_header_sent:1;
53 | unsigned avc_header_sent:1;
54 | unsigned video_key_sent:1;
55 | unsigned audio:1;
56 | unsigned video:1;
57 | } ngx_rtmp_record_rec_ctx_t;
58 |
59 |
60 | typedef struct {
61 | ngx_array_t rec; /* ngx_rtmp_record_rec_ctx_t */
62 | u_char name[NGX_RTMP_MAX_NAME];
63 | u_char args[NGX_RTMP_MAX_ARGS];
64 | } ngx_rtmp_record_ctx_t;
65 |
66 |
67 | ngx_uint_t ngx_rtmp_record_find(ngx_rtmp_record_app_conf_t *racf,
68 | ngx_str_t *id);
69 |
70 |
71 | /* Manual recording control,
72 | * 'n' is record node index in config array.
73 | * Note: these functions allocate path in static buffer */
74 |
75 | ngx_int_t ngx_rtmp_record_open(ngx_rtmp_session_t *s, ngx_uint_t n,
76 | ngx_str_t *path);
77 | ngx_int_t ngx_rtmp_record_close(ngx_rtmp_session_t *s, ngx_uint_t n,
78 | ngx_str_t *path);
79 |
80 | void ngx_rtmp_record_get_path(ngx_rtmp_session_t *s,
81 | ngx_rtmp_record_rec_ctx_t *rctx, ngx_str_t *path);
82 |
83 |
84 | typedef struct {
85 | ngx_str_t recorder;
86 | ngx_str_t path;
87 | } ngx_rtmp_record_done_t;
88 |
89 |
90 | typedef ngx_int_t (*ngx_rtmp_record_done_pt)(ngx_rtmp_session_t *s,
91 | ngx_rtmp_record_done_t *v);
92 |
93 |
94 | extern ngx_rtmp_record_done_pt ngx_rtmp_record_done;
95 |
96 |
97 | extern ngx_module_t ngx_rtmp_record_module;
98 |
99 |
100 | #endif /* _NGX_RTMP_RECORD_H_INCLUDED_ */
101 |
--------------------------------------------------------------------------------
/ngx_rtmp_relay_module.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #ifndef _NGX_RTMP_RELAY_H_INCLUDED_
8 | #define _NGX_RTMP_RELAY_H_INCLUDED_
9 |
10 |
11 | #include
12 | #include
13 | #include "ngx_rtmp.h"
14 |
15 |
16 | typedef struct {
17 | ngx_url_t url;
18 | ngx_str_t app;
19 | ngx_str_t name;
20 | ngx_str_t tc_url;
21 | ngx_str_t page_url;
22 | ngx_str_t swf_url;
23 | ngx_str_t flash_ver;
24 | ngx_str_t play_path;
25 | ngx_int_t live;
26 | ngx_int_t start;
27 | ngx_int_t stop;
28 |
29 | void *tag; /* usually module reference */
30 | void *data; /* module-specific data */
31 | ngx_uint_t counter; /* mutable connection counter */
32 | } ngx_rtmp_relay_target_t;
33 |
34 |
35 | typedef struct ngx_rtmp_relay_ctx_s ngx_rtmp_relay_ctx_t;
36 |
37 | struct ngx_rtmp_relay_ctx_s {
38 | ngx_str_t server_name;
39 | ngx_str_t name;
40 | ngx_str_t url;
41 | ngx_log_t log;
42 | ngx_rtmp_session_t *session;
43 | ngx_rtmp_relay_ctx_t *publish;
44 | ngx_rtmp_relay_ctx_t *play;
45 | ngx_rtmp_relay_ctx_t *next;
46 |
47 | ngx_str_t app;
48 | ngx_str_t tc_url;
49 | ngx_str_t page_url;
50 | ngx_str_t swf_url;
51 | ngx_str_t flash_ver;
52 | ngx_str_t play_path;
53 | ngx_int_t live;
54 | ngx_int_t start;
55 | ngx_int_t stop;
56 |
57 | ngx_event_t push_evt;
58 | ngx_event_t *static_evt;
59 | void *tag;
60 | void *data;
61 | };
62 |
63 |
64 | typedef struct {
65 | ngx_array_t pulls; /* ngx_rtmp_relay_target_t * */
66 | ngx_array_t pushes; /* ngx_rtmp_relay_target_t * */
67 | ngx_array_t static_pulls; /* ngx_rtmp_relay_target_t * */
68 | ngx_array_t static_events; /* ngx_event_t * */
69 | ngx_log_t *log;
70 | ngx_uint_t nbuckets;
71 | ngx_msec_t buflen;
72 | ngx_flag_t session_relay;
73 | ngx_msec_t push_reconnect;
74 | ngx_msec_t pull_reconnect;
75 | ngx_rtmp_relay_ctx_t **ctx;
76 | } ngx_rtmp_relay_app_conf_t;
77 |
78 |
79 | extern ngx_module_t ngx_rtmp_relay_module;
80 |
81 |
82 | ngx_int_t ngx_rtmp_relay_pull(ngx_rtmp_session_t *s, ngx_str_t *name,
83 | ngx_rtmp_relay_target_t *target);
84 | ngx_int_t ngx_rtmp_relay_push(ngx_rtmp_session_t *s, ngx_str_t *name,
85 | ngx_rtmp_relay_target_t *target);
86 |
87 |
88 | #endif /* _NGX_RTMP_RELAY_H_INCLUDED_ */
89 |
--------------------------------------------------------------------------------
/ngx_rtmp_shared.c:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #include
8 | #include
9 | #include "ngx_rtmp.h"
10 |
11 |
12 | ngx_chain_t *
13 | ngx_rtmp_alloc_shared_buf(ngx_rtmp_core_srv_conf_t *cscf)
14 | {
15 | u_char *p;
16 | ngx_chain_t *out;
17 | ngx_buf_t *b;
18 | size_t size;
19 |
20 | if (cscf->free) {
21 | out = cscf->free;
22 | cscf->free = out->next;
23 |
24 | } else {
25 |
26 | size = cscf->chunk_size + NGX_RTMP_MAX_CHUNK_HEADER;
27 |
28 | p = ngx_pcalloc(cscf->pool, NGX_RTMP_REFCOUNT_BYTES
29 | + sizeof(ngx_chain_t)
30 | + sizeof(ngx_buf_t)
31 | + size);
32 | if (p == NULL) {
33 | return NULL;
34 | }
35 |
36 | p += NGX_RTMP_REFCOUNT_BYTES;
37 | out = (ngx_chain_t *)p;
38 |
39 | p += sizeof(ngx_chain_t);
40 | out->buf = (ngx_buf_t *)p;
41 |
42 | p += sizeof(ngx_buf_t);
43 | out->buf->start = p;
44 | out->buf->end = p + size;
45 | }
46 |
47 | out->next = NULL;
48 | b = out->buf;
49 | b->pos = b->last = b->start + NGX_RTMP_MAX_CHUNK_HEADER;
50 | b->memory = 1;
51 |
52 | /* buffer has refcount =1 when created! */
53 | ngx_rtmp_ref_set(out, 1);
54 |
55 | return out;
56 | }
57 |
58 |
59 | void
60 | ngx_rtmp_free_shared_chain(ngx_rtmp_core_srv_conf_t *cscf, ngx_chain_t *in)
61 | {
62 | ngx_chain_t *cl;
63 |
64 | if (ngx_rtmp_ref_put(in)) {
65 | return;
66 | }
67 |
68 | for (cl = in; ; cl = cl->next) {
69 | if (cl->next == NULL) {
70 | cl->next = cscf->free;
71 | cscf->free = in;
72 | return;
73 | }
74 | }
75 | }
76 |
77 |
78 | ngx_chain_t *
79 | ngx_rtmp_append_shared_bufs(ngx_rtmp_core_srv_conf_t *cscf,
80 | ngx_chain_t *head, ngx_chain_t *in)
81 | {
82 | ngx_chain_t *l, **ll;
83 | u_char *p;
84 | size_t size;
85 |
86 | ll = &head;
87 | p = in->buf->pos;
88 | l = head;
89 |
90 | if (l) {
91 | for(; l->next; l = l->next);
92 | ll = &l->next;
93 | }
94 |
95 | for ( ;; ) {
96 |
97 | if (l == NULL || l->buf->last == l->buf->end) {
98 | l = ngx_rtmp_alloc_shared_buf(cscf);
99 | if (l == NULL || l->buf == NULL) {
100 | break;
101 | }
102 |
103 | *ll = l;
104 | ll = &l->next;
105 | }
106 |
107 | while (l->buf->end - l->buf->last >= in->buf->last - p) {
108 | l->buf->last = ngx_cpymem(l->buf->last, p,
109 | in->buf->last - p);
110 | in = in->next;
111 | if (in == NULL) {
112 | goto done;
113 | }
114 | p = in->buf->pos;
115 | }
116 |
117 | size = l->buf->end - l->buf->last;
118 | l->buf->last = ngx_cpymem(l->buf->last, p, size);
119 | p += size;
120 | }
121 |
122 | done:
123 | *ll = NULL;
124 |
125 | return head;
126 | }
127 |
--------------------------------------------------------------------------------
/ngx_rtmp_streams.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | */
5 |
6 |
7 | #ifndef _NGX_RTMP_STREAMS_H_INCLUDED_
8 | #define _NGX_RTMP_STREAMS_H_INCLUDED_
9 |
10 |
11 | #define NGX_RTMP_MSID 1
12 |
13 | #define NGX_RTMP_CSID_AMF_INI 3
14 | #define NGX_RTMP_CSID_AMF 5
15 | #define NGX_RTMP_CSID_AUDIO 6
16 | #define NGX_RTMP_CSID_VIDEO 7
17 |
18 |
19 | #endif /* _NGX_RTMP_STREAMS_H_INCLUDED_ */
20 |
--------------------------------------------------------------------------------
/ngx_rtmp_variables.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Igor Sysoev
4 | * Copyright (C) Nginx, Inc.
5 | * Copyright (C) Winshining
6 | */
7 |
8 |
9 | #ifndef _NGX_RTMP_VARIABLES_H_INCLUDED_
10 | #define _NGX_RTMP_VARIABLES_H_INCLUDED_
11 |
12 |
13 | #include
14 | #include
15 |
16 |
17 | typedef ngx_variable_value_t ngx_rtmp_variable_value_t;
18 |
19 | #define ngx_rtmp_variable(v) { sizeof(v) - 1, 1, 0, 0, 0, (u_char *) v }
20 |
21 | typedef struct ngx_rtmp_variable_s ngx_rtmp_variable_t;
22 |
23 | typedef void (*ngx_rtmp_set_variable_pt) (ngx_rtmp_session_t *s,
24 | ngx_rtmp_variable_value_t *v, uintptr_t data);
25 | typedef ngx_int_t (*ngx_rtmp_get_variable_pt) (ngx_rtmp_session_t *s,
26 | ngx_rtmp_variable_value_t *v, uintptr_t data);
27 |
28 |
29 | #define NGX_RTMP_VAR_CHANGEABLE 1
30 | #define NGX_RTMP_VAR_NOCACHEABLE 2
31 | #define NGX_RTMP_VAR_INDEXED 4
32 | #define NGX_RTMP_VAR_NOHASH 8
33 | #define NGX_RTMP_VAR_WEAK 16
34 | #define NGX_RTMP_VAR_PREFIX 32
35 |
36 |
37 | struct ngx_rtmp_variable_s {
38 | ngx_str_t name; /* must be first to build the hash */
39 | ngx_rtmp_set_variable_pt set_handler;
40 | ngx_rtmp_get_variable_pt get_handler;
41 | uintptr_t data;
42 | ngx_uint_t flags;
43 | ngx_uint_t index;
44 | };
45 |
46 |
47 | ngx_rtmp_variable_t *ngx_rtmp_add_variable(ngx_conf_t *cf, ngx_str_t *name,
48 | ngx_uint_t flags);
49 | ngx_int_t ngx_rtmp_get_variable_index(ngx_conf_t *cf, ngx_str_t *name);
50 | ngx_rtmp_variable_value_t *ngx_rtmp_get_indexed_variable(ngx_rtmp_session_t *s,
51 | ngx_uint_t index);
52 | ngx_rtmp_variable_value_t *ngx_rtmp_get_flushed_variable(ngx_rtmp_session_t *s,
53 | ngx_uint_t index);
54 |
55 | ngx_rtmp_variable_value_t *ngx_rtmp_get_variable(ngx_rtmp_session_t *s,
56 | ngx_str_t *name, ngx_uint_t key);
57 |
58 |
59 | #if (NGX_PCRE)
60 |
61 | typedef struct {
62 | ngx_uint_t capture;
63 | ngx_int_t index;
64 | } ngx_rtmp_regex_variable_t;
65 |
66 |
67 | typedef struct {
68 | ngx_regex_t *regex;
69 | ngx_uint_t ncaptures;
70 | ngx_rtmp_regex_variable_t *variables;
71 | ngx_uint_t nvariables;
72 | ngx_str_t name;
73 | } ngx_rtmp_regex_t;
74 |
75 |
76 | typedef struct {
77 | ngx_rtmp_regex_t *regex;
78 | void *value;
79 | } ngx_rtmp_map_regex_t;
80 |
81 |
82 | ngx_rtmp_regex_t *ngx_rtmp_regex_compile(ngx_conf_t *cf,
83 | ngx_regex_compile_t *rc);
84 | ngx_int_t ngx_rtmp_regex_exec(ngx_rtmp_session_t *s, ngx_rtmp_regex_t *re,
85 | ngx_str_t *str);
86 |
87 | #endif
88 |
89 |
90 | typedef struct {
91 | ngx_hash_combined_t hash;
92 | #if (NGX_PCRE)
93 | ngx_rtmp_map_regex_t *regex;
94 | ngx_uint_t nregex;
95 | #endif
96 | } ngx_rtmp_map_t;
97 |
98 |
99 | void *ngx_rtmp_map_find(ngx_rtmp_session_t *s, ngx_rtmp_map_t *map,
100 | ngx_str_t *match);
101 |
102 |
103 | ngx_int_t ngx_rtmp_variables_add_core_vars(ngx_conf_t *cf);
104 | ngx_int_t ngx_rtmp_variables_init_vars(ngx_conf_t *cf);
105 |
106 |
107 | extern ngx_rtmp_variable_value_t ngx_rtmp_variable_null_value;
108 | extern ngx_rtmp_variable_value_t ngx_rtmp_variable_true_value;
109 |
110 |
111 | #endif /* _NGX_RTMP_VARIABLES_H_INCLUDED_ */
112 |
--------------------------------------------------------------------------------
/ngx_rtmp_version.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright (C) Roman Arutyunyan
4 | * Copyright (C) Winshining
5 | */
6 |
7 |
8 | #ifndef _NGX_RTMP_VERSION_H_INCLUDED_
9 | #define _NGX_RTMP_VERSION_H_INCLUDED_
10 |
11 |
12 | #define nginx_rtmp_version 1002012
13 | #define NGINX_RTMP_VERSION "1.2.12"
14 |
15 |
16 | #endif /* _NGX_RTMP_VERSION_H_INCLUDED_ */
17 |
--------------------------------------------------------------------------------
/samples/flv.js.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/winshining/nginx-http-flv-module/472c339f798d7cead138320373de829eee59c105/samples/flv.js.png
--------------------------------------------------------------------------------
/samples/jwplayer_vlc.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/winshining/nginx-http-flv-module/472c339f798d7cead138320373de829eee59c105/samples/jwplayer_vlc.png
--------------------------------------------------------------------------------
/test/README.md:
--------------------------------------------------------------------------------
1 | # RTMP tests
2 |
3 | nginx.conf is sample config for testing nginx-rtmp.
4 | Please update paths in it before using.
5 |
6 | RTMP port: 1935, HTTP port: 8080
7 |
8 | * http://localhost:8080/ - play myapp/mystream with JWPlayer
9 | * http://localhost:8080/record.html - capture myapp/mystream from webcam with old JWPlayer
10 | * http://localhost:8080/rtmp-publisher/player.html - play myapp/mystream with the test flash applet
11 | * http://localhost:8080/rtmp-publisher/publisher.html - capture myapp/mystream with the test flash applet
12 |
--------------------------------------------------------------------------------
/test/dump.sh:
--------------------------------------------------------------------------------
1 | rtmpdump -v -r "rtmp://localhost/myapp/mystream"
2 |
--------------------------------------------------------------------------------
/test/ffstream.sh:
--------------------------------------------------------------------------------
1 | ffmpeg -loglevel verbose -re -i ~/movie.avi -f flv rtmp://localhost/myapp/mystream
2 |
--------------------------------------------------------------------------------
/test/nginx.conf:
--------------------------------------------------------------------------------
1 | worker_processes 1;
2 |
3 | error_log logs/error.log debug;
4 |
5 | events {
6 | worker_connections 1024;
7 | }
8 |
9 | rtmp {
10 | server {
11 | listen 1935;
12 |
13 | application myapp {
14 | live on;
15 |
16 | #record keyframes;
17 | #record_path /tmp;
18 | #record_max_size 128K;
19 | #record_interval 30s;
20 | #record_suffix .this.is.flv;
21 |
22 | #on_publish http://localhost:8080/publish;
23 | #on_play http://localhost:8080/play;
24 | #on_record_done http://localhost:8080/record_done;
25 | }
26 | }
27 | }
28 |
29 | http {
30 | server {
31 | listen 8080;
32 |
33 | location /stat {
34 | rtmp_stat all;
35 | rtmp_stat_stylesheet stat.xsl;
36 | }
37 |
38 | location /stat.xsl {
39 | root /path/to/nginx-rtmp-module/;
40 | }
41 |
42 | location /control {
43 | rtmp_control all;
44 | }
45 |
46 | #location /publish {
47 | # return 201;
48 | #}
49 |
50 | #location /play {
51 | # return 202;
52 | #}
53 |
54 | #location /record_done {
55 | # return 203;
56 | #}
57 |
58 | location /rtmp-publisher {
59 | root /path/to/nginx-rtmp-module/test;
60 | }
61 |
62 | location / {
63 | root /path/to/nginx-rtmp-module/test/www;
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/test/play.sh:
--------------------------------------------------------------------------------
1 | ffplay -loglevel verbose "rtmp://localhost/myapp/mystream"
2 |
--------------------------------------------------------------------------------
/test/rtmp-publisher/README.md:
--------------------------------------------------------------------------------
1 | # RTMP Publisher
2 |
3 | Simple RTMP publisher.
4 |
5 | Edit the following flashvars in publisher.html & player.html to suite your needs.
6 |
7 | streamer: RTMP endpoint
8 | file: live stream name
9 |
10 | ## Compile
11 |
12 | Install flex sdk http://www.adobe.com/devnet/flex/flex-sdk-download.html
13 |
14 | mxmlc RtmpPublisher.mxml
15 | mxmlc RtmpPlayer.mxml
16 |
--------------------------------------------------------------------------------
/test/rtmp-publisher/RtmpPlayer.mxml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/test/rtmp-publisher/RtmpPlayer.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/winshining/nginx-http-flv-module/472c339f798d7cead138320373de829eee59c105/test/rtmp-publisher/RtmpPlayer.swf
--------------------------------------------------------------------------------
/test/rtmp-publisher/RtmpPlayerLight.mxml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
97 |
98 |
99 |
100 |
101 |
102 |
--------------------------------------------------------------------------------
/test/rtmp-publisher/RtmpPlayerLight.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/winshining/nginx-http-flv-module/472c339f798d7cead138320373de829eee59c105/test/rtmp-publisher/RtmpPlayerLight.swf
--------------------------------------------------------------------------------
/test/rtmp-publisher/RtmpPublisher.mxml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
--------------------------------------------------------------------------------
/test/rtmp-publisher/RtmpPublisher.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/winshining/nginx-http-flv-module/472c339f798d7cead138320373de829eee59c105/test/rtmp-publisher/RtmpPublisher.swf
--------------------------------------------------------------------------------
/test/rtmp-publisher/player.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | RTMP Player
5 |
6 |
16 |
17 |
18 |
19 |
Flash not installed
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/test/rtmp-publisher/publisher.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | RTMP Publisher
5 |
6 |
13 |
14 |
15 |
16 |
Flash not installed
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/test/rtmp-publisher/swfobject.js:
--------------------------------------------------------------------------------
1 | /* SWFObject v2.2
2 | is released under the MIT License
3 | */
4 | var swfobject=function(){var D="undefined",r="object",S="Shockwave Flash",W="ShockwaveFlash.ShockwaveFlash",q="application/x-shockwave-flash",R="SWFObjectExprInst",x="onreadystatechange",O=window,j=document,t=navigator,T=false,U=[h],o=[],N=[],I=[],l,Q,E,B,J=false,a=false,n,G,m=true,M=function(){var aa=typeof j.getElementById!=D&&typeof j.getElementsByTagName!=D&&typeof j.createElement!=D,ah=t.userAgent.toLowerCase(),Y=t.platform.toLowerCase(),ae=Y?/win/.test(Y):/win/.test(ah),ac=Y?/mac/.test(Y):/mac/.test(ah),af=/webkit/.test(ah)?parseFloat(ah.replace(/^.*webkit\/(\d+(\.\d+)?).*$/,"$1")):false,X=!+"\v1",ag=[0,0,0],ab=null;if(typeof t.plugins!=D&&typeof t.plugins[S]==r){ab=t.plugins[S].description;if(ab&&!(typeof t.mimeTypes!=D&&t.mimeTypes[q]&&!t.mimeTypes[q].enabledPlugin)){T=true;X=false;ab=ab.replace(/^.*\s+(\S+\s+\S+$)/,"$1");ag[0]=parseInt(ab.replace(/^(.*)\..*$/,"$1"),10);ag[1]=parseInt(ab.replace(/^.*\.(.*)\s.*$/,"$1"),10);ag[2]=/[a-zA-Z]/.test(ab)?parseInt(ab.replace(/^.*[a-zA-Z]+(.*)$/,"$1"),10):0}}else{if(typeof O.ActiveXObject!=D){try{var ad=new ActiveXObject(W);if(ad){ab=ad.GetVariable("$version");if(ab){X=true;ab=ab.split(" ")[1].split(",");ag=[parseInt(ab[0],10),parseInt(ab[1],10),parseInt(ab[2],10)]}}}catch(Z){}}}return{w3:aa,pv:ag,wk:af,ie:X,win:ae,mac:ac}}(),k=function(){if(!M.w3){return}if((typeof j.readyState!=D&&j.readyState=="complete")||(typeof j.readyState==D&&(j.getElementsByTagName("body")[0]||j.body))){f()}if(!J){if(typeof j.addEventListener!=D){j.addEventListener("DOMContentLoaded",f,false)}if(M.ie&&M.win){j.attachEvent(x,function(){if(j.readyState=="complete"){j.detachEvent(x,arguments.callee);f()}});if(O==top){(function(){if(J){return}try{j.documentElement.doScroll("left")}catch(X){setTimeout(arguments.callee,0);return}f()})()}}if(M.wk){(function(){if(J){return}if(!/loaded|complete/.test(j.readyState)){setTimeout(arguments.callee,0);return}f()})()}s(f)}}();function f(){if(J){return}try{var Z=j.getElementsByTagName("body")[0].appendChild(C("span"));Z.parentNode.removeChild(Z)}catch(aa){return}J=true;var X=U.length;for(var Y=0;Y0){for(var af=0;af0){var ae=c(Y);if(ae){if(F(o[af].swfVersion)&&!(M.wk&&M.wk<312)){w(Y,true);if(ab){aa.success=true;aa.ref=z(Y);ab(aa)}}else{if(o[af].expressInstall&&A()){var ai={};ai.data=o[af].expressInstall;ai.width=ae.getAttribute("width")||"0";ai.height=ae.getAttribute("height")||"0";if(ae.getAttribute("class")){ai.styleclass=ae.getAttribute("class")}if(ae.getAttribute("align")){ai.align=ae.getAttribute("align")}var ah={};var X=ae.getElementsByTagName("param");var ac=X.length;for(var ad=0;ad'}}aa.outerHTML='";N[N.length]=ai.id;X=c(ai.id)}else{var Z=C(r);Z.setAttribute("type",q);for(var ac in ai){if(ai[ac]!=Object.prototype[ac]){if(ac.toLowerCase()=="styleclass"){Z.setAttribute("class",ai[ac])}else{if(ac.toLowerCase()!="classid"){Z.setAttribute(ac,ai[ac])}}}}for(var ab in ag){if(ag[ab]!=Object.prototype[ab]&&ab.toLowerCase()!="movie"){e(Z,ab,ag[ab])}}aa.parentNode.replaceChild(Z,aa);X=Z}}return X}function e(Z,X,Y){var aa=C("param");aa.setAttribute("name",X);aa.setAttribute("value",Y);Z.appendChild(aa)}function y(Y){var X=c(Y);if(X&&X.nodeName=="OBJECT"){if(M.ie&&M.win){X.style.display="none";(function(){if(X.readyState==4){b(Y)}else{setTimeout(arguments.callee,10)}})()}else{X.parentNode.removeChild(X)}}}function b(Z){var Y=c(Z);if(Y){for(var X in Y){if(typeof Y[X]=="function"){Y[X]=null}}Y.parentNode.removeChild(Y)}}function c(Z){var X=null;try{X=j.getElementById(Z)}catch(Y){}return X}function C(X){return j.createElement(X)}function i(Z,X,Y){Z.attachEvent(X,Y);I[I.length]=[Z,X,Y]}function F(Z){var Y=M.pv,X=Z.split(".");X[0]=parseInt(X[0],10);X[1]=parseInt(X[1],10)||0;X[2]=parseInt(X[2],10)||0;return(Y[0]>X[0]||(Y[0]==X[0]&&Y[1]>X[1])||(Y[0]==X[0]&&Y[1]==X[1]&&Y[2]>=X[2]))?true:false}function v(ac,Y,ad,ab){if(M.ie&&M.mac){return}var aa=j.getElementsByTagName("head")[0];if(!aa){return}var X=(ad&&typeof ad=="string")?ad:"screen";if(ab){n=null;G=null}if(!n||G!=X){var Z=C("style");Z.setAttribute("type","text/css");Z.setAttribute("media",X);n=aa.appendChild(Z);if(M.ie&&M.win&&typeof j.styleSheets!=D&&j.styleSheets.length>0){n=j.styleSheets[j.styleSheets.length-1]}G=X}if(M.ie&&M.win){if(n&&typeof n.addRule==r){n.addRule(ac,Y)}}else{if(n&&typeof j.createTextNode!=D){n.appendChild(j.createTextNode(ac+" {"+Y+"}"))}}}function w(Z,X){if(!m){return}var Y=X?"visible":"hidden";if(J&&c(Z)){c(Z).style.visibility=Y}else{v("#"+Z,"visibility:"+Y)}}function L(Y){var Z=/[\\\"<>\.;]/;var X=Z.exec(Y)!=null;return X&&typeof encodeURIComponent!=D?encodeURIComponent(Y):Y}var d=function(){if(M.ie&&M.win){window.attachEvent("onunload",function(){var ac=I.length;for(var ab=0;abPlay | Record
2 |
3 |
4 |
5 | Loading the player ...
6 |
20 |
--------------------------------------------------------------------------------
/test/www/jwplayer/jwplayer.flash.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/winshining/nginx-http-flv-module/472c339f798d7cead138320373de829eee59c105/test/www/jwplayer/jwplayer.flash.swf
--------------------------------------------------------------------------------
/test/www/jwplayer_old/player.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/winshining/nginx-http-flv-module/472c339f798d7cead138320373de829eee59c105/test/www/jwplayer_old/player.swf
--------------------------------------------------------------------------------
/test/www/jwplayer_old/swfobject.js:
--------------------------------------------------------------------------------
1 | /* SWFObject v2.1
2 | Copyright (c) 2007-2008 Geoff Stearns, Michael Williams, and Bobby van der Sluis
3 | This software is released under the MIT License
4 | */
5 | var swfobject=function(){var b="undefined",Q="object",n="Shockwave Flash",p="ShockwaveFlash.ShockwaveFlash",P="application/x-shockwave-flash",m="SWFObjectExprInst",j=window,K=document,T=navigator,o=[],N=[],i=[],d=[],J,Z=null,M=null,l=null,e=false,A=false;var h=function(){var v=typeof K.getElementById!=b&&typeof K.getElementsByTagName!=b&&typeof K.createElement!=b,AC=[0,0,0],x=null;if(typeof T.plugins!=b&&typeof T.plugins[n]==Q){x=T.plugins[n].description;if(x&&!(typeof T.mimeTypes!=b&&T.mimeTypes[P]&&!T.mimeTypes[P].enabledPlugin)){x=x.replace(/^.*\s+(\S+\s+\S+$)/,"$1");AC[0]=parseInt(x.replace(/^(.*)\..*$/,"$1"),10);AC[1]=parseInt(x.replace(/^.*\.(.*)\s.*$/,"$1"),10);AC[2]=/r/.test(x)?parseInt(x.replace(/^.*r(.*)$/,"$1"),10):0}}else{if(typeof j.ActiveXObject!=b){var y=null,AB=false;try{y=new ActiveXObject(p+".7")}catch(t){try{y=new ActiveXObject(p+".6");AC=[6,0,21];y.AllowScriptAccess="always"}catch(t){if(AC[0]==6){AB=true}}if(!AB){try{y=new ActiveXObject(p)}catch(t){}}}if(!AB&&y){try{x=y.GetVariable("$version");if(x){x=x.split(" ")[1].split(",");AC=[parseInt(x[0],10),parseInt(x[1],10),parseInt(x[2],10)]}}catch(t){}}}}var AD=T.userAgent.toLowerCase(),r=T.platform.toLowerCase(),AA=/webkit/.test(AD)?parseFloat(AD.replace(/^.*webkit\/(\d+(\.\d+)?).*$/,"$1")):false,q=false,z=r?/win/.test(r):/win/.test(AD),w=r?/mac/.test(r):/mac/.test(AD);/*@cc_on q=true;@if(@_win32)z=true;@elif(@_mac)w=true;@end@*/return{w3cdom:v,pv:AC,webkit:AA,ie:q,win:z,mac:w}}();var L=function(){if(!h.w3cdom){return }f(H);if(h.ie&&h.win){try{K.write("
4 |
34 |
35 |
36 |
37 |
38 |