Skip to content

Commit 45c52fb

Browse files
committed
fix rtmp variables and add codec variables
1 parent a154a2f commit 45c52fb

File tree

5 files changed

+230
-82
lines changed

5 files changed

+230
-82
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
docker/*.tar*
2-
nginx/
32
openresty/
3+
nginx/
44
.git/
55
*.code-workspace
66
.DS_Store

modules/nginx-rtmp-module/ngx_rtmp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
312312
}
313313
}
314314

315+
if (ngx_rtmp_variables_init_vars(cf) != NGX_OK) {
316+
return NGX_CONF_ERROR;
317+
}
318+
315319
*cf = pcf;
316320

317321
if (ngx_rtmp_init_event_handlers(cf, cmcf) != NGX_OK) {

modules/nginx-rtmp-module/ngx_rtmp_codec_module.c

Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
#include "ngx_rtmp_cmd_module.h"
1212
#include "ngx_rtmp_bitop.h"
1313
#include "ngx_rbuf.h"
14-
14+
#include "ngx_rtmp_variables.h"
1515

1616
#define NGX_RTMP_CODEC_META_OFF 0
1717
#define NGX_RTMP_CODEC_META_ON 1
1818
#define NGX_RTMP_CODEC_META_COPY 2
1919

20-
2120
static void * ngx_rtmp_codec_create_app_conf(ngx_conf_t *cf);
2221
static char * ngx_rtmp_codec_merge_app_conf(ngx_conf_t *cf,
2322
void *parent, void *child);
23+
static ngx_int_t ngx_rtmp_codec_add_variables(ngx_conf_t *cf);
2424
static ngx_int_t ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf);
2525
static ngx_int_t ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s);
2626
static ngx_int_t ngx_rtmp_codec_copy_meta(ngx_rtmp_session_t *s,
@@ -38,6 +38,12 @@ static void ngx_rtmp_codec_dump_header(ngx_rtmp_session_t *s, const char *type,
3838
ngx_chain_t *in);
3939
#endif
4040

41+
static ngx_int_t
42+
ngx_rtmp_codec_variable_metadata(ngx_rtmp_session_t *s,
43+
ngx_rtmp_variable_value_t *v, uintptr_t data);
44+
static ngx_int_t
45+
ngx_rtmp_codec_variable_metadata_arg(ngx_rtmp_session_t *s,
46+
ngx_rtmp_variable_value_t *v, uintptr_t data);
4147

4248
typedef struct {
4349
ngx_uint_t meta;
@@ -66,7 +72,7 @@ static ngx_command_t ngx_rtmp_codec_commands[] = {
6672

6773

6874
static ngx_rtmp_module_t ngx_rtmp_codec_module_ctx = {
69-
NULL, /* preconfiguration */
75+
ngx_rtmp_codec_add_variables, /* preconfiguration */
7076
ngx_rtmp_codec_postconfiguration, /* postconfiguration */
7177
NULL, /* create main configuration */
7278
NULL, /* init main configuration */
@@ -92,6 +98,12 @@ ngx_module_t ngx_rtmp_codec_module = {
9298
NGX_MODULE_V1_PADDING
9399
};
94100

101+
static ngx_rtmp_variable_t ngx_rtmp_codec_variabes[] = {
102+
{ ngx_string("metadata"), NULL, ngx_rtmp_codec_variable_metadata,
103+
0, NGX_RTMP_VAR_NOCACHEABLE|NGX_RTMP_VAR_PREFIX, 0 },
104+
{ ngx_string("metadata_"), NULL, ngx_rtmp_codec_variable_metadata_arg,
105+
0, NGX_RTMP_VAR_NOCACHEABLE|NGX_RTMP_VAR_PREFIX, 0 }
106+
};
95107

96108
static const char *
97109
audio_codecs[] = {
@@ -1309,6 +1321,138 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
13091321
return NGX_OK;
13101322
}
13111323

1324+
static ngx_int_t
1325+
ngx_rtmp_codec_variable_metadata(ngx_rtmp_session_t *s,
1326+
ngx_rtmp_variable_value_t *v, uintptr_t data)
1327+
{
1328+
ngx_rtmp_codec_ctx_t *ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
1329+
if (ctx == NULL) {
1330+
return NGX_OK;
1331+
}
1332+
1333+
u_char *metadata_args = ngx_pnalloc(s->pool, 1024);
1334+
u_char *p, *e;
1335+
1336+
p = metadata_args;
1337+
e = metadata_args + 1024;
1338+
1339+
#define NGX_METADATA_APPEND_UINT_VARIABLE(__key, __var) \
1340+
p = ngx_slprintf(p, e, "%s=%ui&", __key, __var); \
1341+
1342+
#define NGX_METADATA_APPEND_DOUBLE_VARIABLE(__key, __var) \
1343+
p = ngx_slprintf(p, e, "%s=%.2f&", __key, __var); \
1344+
1345+
#define NGX_METADATA_APPEND_STRING_VARIABLE(__key, __var) \
1346+
p = ngx_slprintf(p, e, "%s=%s&", __key, __var); \
1347+
1348+
NGX_METADATA_APPEND_UINT_VARIABLE("width", ctx->width);
1349+
NGX_METADATA_APPEND_UINT_VARIABLE("height", ctx->height);
1350+
NGX_METADATA_APPEND_UINT_VARIABLE("duration", ctx->duration);
1351+
NGX_METADATA_APPEND_DOUBLE_VARIABLE("framerate", ctx->frame_rate);
1352+
NGX_METADATA_APPEND_UINT_VARIABLE("fps", ctx->frame_rate);
1353+
NGX_METADATA_APPEND_UINT_VARIABLE("videodatarate", ctx->video_data_rate);
1354+
NGX_METADATA_APPEND_STRING_VARIABLE("videocodec",
1355+
ngx_rtmp_get_video_codec_name(ctx->video_codec_id));
1356+
NGX_METADATA_APPEND_STRING_VARIABLE("audiocodec",
1357+
ngx_rtmp_get_audio_codec_name(ctx->audio_codec_id));
1358+
NGX_METADATA_APPEND_UINT_VARIABLE("audiodatarate", ctx->audio_data_rate);
1359+
// NGX_METADATA_APPEND_STRING_VARIABLE("profile", ctx->profile);
1360+
// NGX_METADATA_APPEND_STRING_VARIABLE("level", ctx->level);
1361+
1362+
v->data = metadata_args;
1363+
v->len = p - metadata_args - 1;
1364+
1365+
v->valid = 1;
1366+
v->no_cacheable = 0;
1367+
v->not_found = 0;
1368+
1369+
return NGX_OK;
1370+
}
1371+
1372+
static ngx_int_t
1373+
ngx_rtmp_codec_variable_metadata_arg(ngx_rtmp_session_t *s,
1374+
ngx_rtmp_variable_value_t *v, uintptr_t data)
1375+
{
1376+
ngx_str_t *name = (ngx_str_t *) data;
1377+
u_char *arg;
1378+
size_t len;
1379+
u_char *codec_name = (u_char*) "unknown";
1380+
1381+
ngx_rtmp_codec_ctx_t *ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
1382+
if (ctx == NULL) {
1383+
return NGX_OK;
1384+
}
1385+
1386+
len = name->len - (sizeof("metadata_") - 1);
1387+
arg = name->data + sizeof("metadata_") - 1;
1388+
1389+
#define NGX_METADATA_SET_UINT_VARIABLE(__key, __var) \
1390+
if (len == ngx_strlen((__key)) && \
1391+
ngx_strncasecmp((u_char *)(__key), arg, len) == 0) { \
1392+
v->data = ngx_pnalloc(s->pool, NGX_INT64_LEN); \
1393+
if (v->data == NULL) { \
1394+
return NGX_ERROR; \
1395+
} \
1396+
v->len = ngx_sprintf(v->data, "%ui", __var) - v->data; \
1397+
return NGX_OK; \
1398+
}
1399+
1400+
#define NGX_METADATA_SET_DOUBLE_VARIABLE(__key, __var) \
1401+
if (len == ngx_strlen((__key)) && \
1402+
ngx_strncasecmp((u_char *)(__key), arg, len) == 0) { \
1403+
v->data = ngx_pnalloc(s->pool, NGX_INT64_LEN); \
1404+
if (v->data == NULL) { \
1405+
return NGX_ERROR; \
1406+
} \
1407+
v->len = ngx_sprintf(v->data, "%.2f", __var) - v->data; \
1408+
return NGX_OK; \
1409+
}
1410+
1411+
#define NGX_METADATA_SET_STRING_VARIABLE(__key, __var) \
1412+
if (len == ngx_strlen((__key)) && \
1413+
ngx_strncasecmp((u_char *)(__key), arg, len) == 0) { \
1414+
v->data = ngx_pnalloc(s->pool, NGX_INT64_LEN); \
1415+
if (v->data == NULL) { \
1416+
return NGX_ERROR; \
1417+
} \
1418+
v->len = ngx_sprintf(v->data, "%s", __var) - v->data; \
1419+
return NGX_OK; \
1420+
}
1421+
1422+
NGX_METADATA_SET_UINT_VARIABLE("width", ctx->width);
1423+
NGX_METADATA_SET_UINT_VARIABLE("height", ctx->height);
1424+
NGX_METADATA_SET_UINT_VARIABLE("duration", ctx->duration);
1425+
NGX_METADATA_SET_DOUBLE_VARIABLE("framerate", ctx->frame_rate);
1426+
NGX_METADATA_SET_UINT_VARIABLE("fps", ctx->frame_rate);
1427+
NGX_METADATA_SET_UINT_VARIABLE("videodatarate", ctx->video_data_rate);
1428+
NGX_METADATA_SET_UINT_VARIABLE("videocodecid", ctx->video_codec_id);
1429+
NGX_METADATA_SET_UINT_VARIABLE("audiodatarate", ctx->audio_data_rate);
1430+
NGX_METADATA_SET_UINT_VARIABLE("audiocodecid", ctx->audio_codec_id);
1431+
NGX_METADATA_SET_STRING_VARIABLE("profile", ctx->profile);
1432+
NGX_METADATA_SET_STRING_VARIABLE("level", ctx->level);
1433+
1434+
if (len == ngx_strlen("audiocodec") &&
1435+
!ngx_strncasecmp(arg, (u_char *) "audiocodec", len))
1436+
{
1437+
codec_name = ngx_rtmp_get_audio_codec_name(ctx->audio_codec_id);
1438+
}
1439+
else if (len == ngx_strlen("videocodec") &&
1440+
!ngx_strncasecmp(arg, (u_char *) "videocodec", len))
1441+
{
1442+
codec_name = ngx_rtmp_get_video_codec_name(ctx->video_codec_id);
1443+
}
1444+
1445+
v->len = ngx_strlen(codec_name);
1446+
v->data = ngx_pnalloc(s->pool, v->len);
1447+
ngx_snprintf(v->data, v->len, "%s", codec_name);
1448+
1449+
1450+
v->valid = 1;
1451+
v->no_cacheable = 0;
1452+
v->not_found = 0;
1453+
1454+
return NGX_OK;
1455+
}
13121456

13131457
static void *
13141458
ngx_rtmp_codec_create_app_conf(ngx_conf_t *cf)
@@ -1337,6 +1481,23 @@ ngx_rtmp_codec_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
13371481
return NGX_CONF_OK;
13381482
}
13391483

1484+
static ngx_int_t
1485+
ngx_rtmp_codec_add_variables(ngx_conf_t *cf)
1486+
{
1487+
ngx_rtmp_variable_t *var, *v;
1488+
1489+
for (v = ngx_rtmp_codec_variabes; v->name.len; v++) {
1490+
var = ngx_rtmp_add_variable(cf, &v->name, v->flags);
1491+
if (var == NULL) {
1492+
return NGX_ERROR;
1493+
}
1494+
1495+
var->get_handler = v->get_handler;
1496+
var->data = v->data;
1497+
}
1498+
1499+
return NGX_OK;
1500+
}
13401501

13411502
static ngx_int_t
13421503
ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf)

modules/nginx-rtmp-module/ngx_rtmp_notify_module.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,8 @@ ngx_rtmp_notify_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in)
20582058

20592059
event = oacf->events[NGX_RTMP_OCLP_META].elts;
20602060
for (i = 0; i < oacf->events[NGX_RTMP_OCLP_META].nelts; ++i, ++event) {
2061-
ngx_rtmp_notify_relay_start(s, event, NGX_RTMP_OCLP_META, 0);
2061+
// ngx_rtmp_notify_relay_start(s, event, NGX_RTMP_OCLP_META, 0);
2062+
ngx_rtmp_notify_pnotify_start(s, NGX_RTMP_OCLP_META);
20622063
}
20632064
}
20642065

0 commit comments

Comments
 (0)