forked from arut/nginx-rtmp-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_rtmp_enotify_module.c
589 lines (433 loc) · 14.7 KB
/
ngx_rtmp_enotify_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* Copyright (c) 2012 Roman Arutyunyan
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include "ngx_rtmp.h"
#include "ngx_rtmp_eval.h"
#include "ngx_rtmp_cmd_module.h"
#include "ngx_rtmp_record_module.h"
#include <stdlib.h>
#ifdef NGX_LINUX
#include <unistd.h>
#endif
static ngx_rtmp_publish_pt next_publish;
static ngx_rtmp_play_pt next_play;
static ngx_rtmp_close_stream_pt next_close_stream;
static ngx_rtmp_record_done_pt next_record_done;
static char *ngx_rtmp_enotify_on_event(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_int_t ngx_rtmp_enotify_postconfiguration(ngx_conf_t *cf);
static void * ngx_rtmp_enotify_create_app_conf(ngx_conf_t *cf);
static char * ngx_rtmp_enotify_merge_app_conf(ngx_conf_t *cf,
void *parent, void *child);
#define NGX_RTMP_ENOTIFY_PUBLISHING 0x01
#define NGX_RTMP_ENOTIFY_PLAYING 0x02
enum {
NGX_RTMP_ENOTIFY_PUBLISH,
NGX_RTMP_ENOTIFY_PLAY,
NGX_RTMP_ENOTIFY_PUBLISH_DONE,
NGX_RTMP_ENOTIFY_PLAY_DONE,
NGX_RTMP_ENOTIFY_RECORD_DONE,
NGX_RTMP_ENOTIFY_MAX
};
typedef struct {
ngx_str_t cmd;
ngx_array_t args; /* ngx_str_t */
} ngx_rtmp_enotify_conf_t;
typedef struct {
ngx_rtmp_enotify_conf_t *event[NGX_RTMP_ENOTIFY_MAX];
ngx_flag_t active;
} ngx_rtmp_enotify_app_conf_t;
typedef struct {
ngx_uint_t flags;
u_char name[NGX_RTMP_MAX_NAME];
u_char args[NGX_RTMP_MAX_ARGS];
ngx_str_t path;
ngx_str_t recorder;
} ngx_rtmp_enotify_ctx_t;
static ngx_command_t ngx_rtmp_enotify_commands[] = {
{ ngx_string("exec_publish"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_1MORE,
ngx_rtmp_enotify_on_event,
NGX_RTMP_APP_CONF_OFFSET,
0,
NULL },
{ ngx_string("exec_play"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_1MORE,
ngx_rtmp_enotify_on_event,
NGX_RTMP_APP_CONF_OFFSET,
0,
NULL },
{ ngx_string("exec_publish_done"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_1MORE,
ngx_rtmp_enotify_on_event,
NGX_RTMP_APP_CONF_OFFSET,
0,
NULL },
{ ngx_string("exec_play_done"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_1MORE,
ngx_rtmp_enotify_on_event,
NGX_RTMP_APP_CONF_OFFSET,
0,
NULL },
{ ngx_string("exec_record_done"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_RTMP_REC_CONF|
NGX_CONF_1MORE,
ngx_rtmp_enotify_on_event,
NGX_RTMP_APP_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_rtmp_module_t ngx_rtmp_enotify_module_ctx = {
NULL, /* preconfiguration */
ngx_rtmp_enotify_postconfiguration, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_rtmp_enotify_create_app_conf, /* create app configuration */
ngx_rtmp_enotify_merge_app_conf /* merge app configuration */
};
ngx_module_t ngx_rtmp_enotify_module = {
NGX_MODULE_V1,
&ngx_rtmp_enotify_module_ctx, /* module context */
ngx_rtmp_enotify_commands, /* module directives */
NGX_RTMP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static void
ngx_rtmp_enotify_eval_astr(ngx_rtmp_session_t *s, ngx_rtmp_eval_t *e,
ngx_str_t *ret)
{
ngx_rtmp_enotify_ctx_t *ctx;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_enotify_module);
if (ctx == NULL) {
ret->len = 0;
return;
}
ret->data = (u_char *) ctx + e->offset;
ret->len = ngx_strlen(ret->data);
}
static void
ngx_rtmp_enotify_eval_str(ngx_rtmp_session_t *s, ngx_rtmp_eval_t *e,
ngx_str_t *ret)
{
ngx_rtmp_enotify_ctx_t *ctx;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_enotify_module);
if (ctx == NULL) {
ret->len = 0;
return;
}
*ret = *(ngx_str_t *) ((u_char *) ctx + e->offset);
}
static ngx_rtmp_eval_t ngx_rtmp_enotify_eval[] = {
{ ngx_string("name"),
ngx_rtmp_enotify_eval_astr,
offsetof(ngx_rtmp_enotify_ctx_t, name) },
{ ngx_string("args"),
ngx_rtmp_enotify_eval_astr,
offsetof(ngx_rtmp_enotify_ctx_t, args) },
{ ngx_string("path"),
ngx_rtmp_enotify_eval_str,
offsetof(ngx_rtmp_enotify_ctx_t, path) },
{ ngx_string("recorder"),
ngx_rtmp_enotify_eval_str,
offsetof(ngx_rtmp_enotify_ctx_t, recorder) },
ngx_rtmp_null_eval
};
static ngx_rtmp_eval_t * ngx_rtmp_enotify_eval_p[] = {
ngx_rtmp_eval_session,
ngx_rtmp_enotify_eval,
NULL
};
static void *
ngx_rtmp_enotify_create_app_conf(ngx_conf_t *cf)
{
ngx_rtmp_enotify_app_conf_t *enacf;
ngx_uint_t n;
enacf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_enotify_app_conf_t));
if (enacf == NULL) {
return NULL;
}
for (n = 0; n < NGX_RTMP_ENOTIFY_MAX; ++n) {
enacf->event[n] = NGX_CONF_UNSET_PTR;
}
return enacf;
}
static char *
ngx_rtmp_enotify_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_rtmp_enotify_app_conf_t *prev = parent;
ngx_rtmp_enotify_app_conf_t *conf = child;
ngx_uint_t n;
for (n = 0; n < NGX_RTMP_ENOTIFY_MAX; ++n) {
ngx_conf_merge_ptr_value(conf->event[n], prev->event[n], NULL);
if (conf->event[n]) {
conf->active = 1;
}
}
if (conf->active) {
prev->active = 1;
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_rtmp_enotify_exec(ngx_rtmp_session_t *s, ngx_rtmp_enotify_conf_t *ec)
{
#if !(NGX_WIN32)
int pid, fd, maxfd;
ngx_str_t a, *arg_in;
char **args, **arg_out;
ngx_uint_t n;
pid = fork();
switch (pid) {
case -1:
ngx_log_error(NGX_LOG_INFO, s->connection->log, ngx_errno,
"enotify: fork failed");
return NGX_ERROR;
case 0:
/* child */
/* close all descriptors */
maxfd = sysconf(_SC_OPEN_MAX);
for (fd = 0; fd < maxfd; ++fd) {
close(fd);
}
fd = open("/dev/null", O_RDWR);
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
args = ngx_palloc(s->connection->pool,
(ec->args.nelts + 2) * sizeof(char *));
if (args == NULL) {
exit(1);
}
arg_in = ec->args.elts;
arg_out = args;
*arg_out++ = (char *) ec->cmd.data;
for (n = 0; n < ec->args.nelts; ++n, ++arg_in) {
ngx_rtmp_eval(s, arg_in, ngx_rtmp_enotify_eval_p, &a);
if (ngx_rtmp_eval_streams(&a) != NGX_DONE) {
continue;
}
*arg_out++ = (char *) a.data;
}
*arg_out = NULL;
if (execvp((char *) ec->cmd.data, args) == -1) {
exit(1);
}
break;
default:
/* parent */
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"enotify: child '%V' started pid=%ui",
&ec->cmd, (ngx_uint_t)pid);
break;
}
#endif /* NGX_WIN32 */
return NGX_OK;
}
static void
ngx_rtmp_enotify_init(ngx_rtmp_session_t *s,
u_char name[NGX_RTMP_MAX_NAME], u_char args[NGX_RTMP_MAX_ARGS],
ngx_uint_t flags)
{
ngx_rtmp_enotify_ctx_t *ctx;
ngx_rtmp_enotify_app_conf_t *enacf;
enacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_enotify_module);
if (!enacf->active) {
return;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_enotify_module);
if (ctx == NULL) {
ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_rtmp_enotify_ctx_t));
if (ctx == NULL) {
return;
}
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_enotify_module);
}
ngx_memcpy(ctx->name, name, NGX_RTMP_MAX_NAME);
ngx_memcpy(ctx->args, args, NGX_RTMP_MAX_ARGS);
ctx->flags |= flags;
}
static ngx_int_t
ngx_rtmp_enotify_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
{
ngx_rtmp_enotify_app_conf_t *enacf;
ngx_rtmp_enotify_conf_t *ec;
if (s->auto_pushed) {
goto next;
}
enacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_enotify_module);
if (enacf == NULL) {
goto next;
}
ngx_rtmp_enotify_init(s, v->name, v->args, NGX_RTMP_ENOTIFY_PUBLISHING);
ec = enacf->event[NGX_RTMP_ENOTIFY_PUBLISH];
if (ec == NULL) {
goto next;
}
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
"enotify: publish '%V'", &ec->cmd);
ngx_rtmp_enotify_exec(s, ec);
next:
return next_publish(s, v);
}
static ngx_int_t
ngx_rtmp_enotify_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
{
ngx_rtmp_enotify_app_conf_t *enacf;
ngx_rtmp_enotify_conf_t *ec;
if (s->auto_pushed) {
goto next;
}
enacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_enotify_module);
if (enacf == NULL) {
goto next;
}
ngx_rtmp_enotify_init(s, v->name, v->args, NGX_RTMP_ENOTIFY_PLAYING);
ec = enacf->event[NGX_RTMP_ENOTIFY_PLAY];
if (ec == NULL) {
goto next;
}
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
"enotify: play '%V'", &ec->cmd);
ngx_rtmp_enotify_exec(s, ec);
next:
return next_play(s, v);
}
static ngx_int_t
ngx_rtmp_enotify_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t
*v)
{
ngx_rtmp_enotify_ctx_t *ctx;
ngx_rtmp_enotify_app_conf_t *enacf;
ngx_rtmp_enotify_conf_t *ec;
if (s->auto_pushed) {
goto next;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_enotify_module);
if (ctx == NULL) {
goto next;
}
enacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_enotify_module);
if (enacf == NULL) {
goto next;
}
if (enacf->event[NGX_RTMP_ENOTIFY_PUBLISH_DONE] &&
(ctx->flags & NGX_RTMP_ENOTIFY_PUBLISHING))
{
ec = enacf->event[NGX_RTMP_ENOTIFY_PUBLISH_DONE];
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
"enotify: publish_done '%V'", &ec->cmd);
ngx_rtmp_enotify_exec(s, ec);
}
if (enacf->event[NGX_RTMP_ENOTIFY_PLAY_DONE] &&
(ctx->flags & NGX_RTMP_ENOTIFY_PLAYING))
{
ec = enacf->event[NGX_RTMP_ENOTIFY_PLAY_DONE];
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
"enotify: play_done '%V'", &ec->cmd);
ngx_rtmp_enotify_exec(s, ec);
}
ctx->flags = 0;
next:
return next_close_stream(s, v);
}
static ngx_int_t
ngx_rtmp_enotify_record_done(ngx_rtmp_session_t *s, ngx_rtmp_record_done_t *v)
{
ngx_rtmp_enotify_app_conf_t *enacf;
ngx_rtmp_enotify_conf_t *ec;
ngx_rtmp_enotify_ctx_t *ctx;
if (s->auto_pushed) {
goto next;
}
enacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_enotify_module);
if (enacf == NULL || enacf->event[NGX_RTMP_ENOTIFY_RECORD_DONE] == NULL) {
goto next;
}
ec = enacf->event[NGX_RTMP_ENOTIFY_RECORD_DONE];
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
"enotify: record_done %V recorder=%V path='%V'",
&ec->cmd, &v->recorder, &v->path);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_enotify_module);
if (ctx == NULL) {
goto next;
}
ctx->recorder = v->recorder;
ctx->path = v->path;
ngx_rtmp_enotify_exec(s, ec);
next:
return next_record_done(s, v);
}
static char *
ngx_rtmp_enotify_on_event(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_rtmp_enotify_app_conf_t *enacf;
ngx_rtmp_enotify_conf_t *ec;
ngx_str_t *name, *value, *s;
size_t nargs;
ngx_uint_t n;
value = cf->args->elts;
name = &value[0];
ec = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_enotify_conf_t));
if (ec == NULL) {
return NGX_CONF_ERROR;
}
ec->cmd = value[1];
nargs = cf->args->nelts - 2;
if (nargs) {
if (ngx_array_init(&ec->args, cf->pool, nargs, sizeof(ngx_str_t))
!= NGX_OK)
{
return NGX_CONF_ERROR;
}
s = ngx_array_push_n(&ec->args, nargs);
for (n = 2; n < cf->args->nelts; ++n, ++s) {
*s = value[n];
}
}
enacf = ngx_rtmp_conf_get_module_app_conf(cf, ngx_rtmp_enotify_module);
n = 0;
switch (name->len) {
case sizeof("exec_play") - 1:
n = NGX_RTMP_ENOTIFY_PLAY;
break;
case sizeof("exec_publish") - 1:
n = NGX_RTMP_ENOTIFY_PUBLISH;
break;
case sizeof("exec_play_done") - 1:
n = NGX_RTMP_ENOTIFY_PLAY_DONE;
break;
case sizeof("exec_record_done") - 1:
n = NGX_RTMP_ENOTIFY_RECORD_DONE;
break;
case sizeof("exec_publish_done") - 1:
n = NGX_RTMP_ENOTIFY_PUBLISH_DONE;
break;
}
enacf->event[n] = ec;
return NGX_CONF_OK;
}
static ngx_int_t
ngx_rtmp_enotify_postconfiguration(ngx_conf_t *cf)
{
next_publish = ngx_rtmp_publish;
ngx_rtmp_publish = ngx_rtmp_enotify_publish;
next_play = ngx_rtmp_play;
ngx_rtmp_play = ngx_rtmp_enotify_play;
next_close_stream = ngx_rtmp_close_stream;
ngx_rtmp_close_stream = ngx_rtmp_enotify_close_stream;
next_record_done = ngx_rtmp_record_done;
ngx_rtmp_record_done = ngx_rtmp_enotify_record_done;
return NGX_OK;
}