forked from arut/nginx-rtmp-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_rtmp_receive.c
464 lines (350 loc) · 11.4 KB
/
ngx_rtmp_receive.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
/*
* Copyright (C) Roman Arutyunyan
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include "ngx_rtmp.h"
#include "ngx_rtmp_amf.h"
#include "ngx_rtmp_cmd_module.h"
#include <string.h>
ngx_int_t
ngx_rtmp_protocol_message_handler(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in)
{
ngx_buf_t *b;
u_char *p;
uint32_t val;
uint8_t limit;
b = in->buf;
if (b->last - b->pos < 4) {
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"too small buffer for %d message: %d",
(int)h->type, b->last - b->pos);
return NGX_OK;
}
p = (u_char*)&val;
p[0] = b->pos[3];
p[1] = b->pos[2];
p[2] = b->pos[1];
p[3] = b->pos[0];
switch(h->type) {
case NGX_RTMP_MSG_CHUNK_SIZE:
/* set chunk size =val */
ngx_rtmp_set_chunk_size(s, val);
break;
case NGX_RTMP_MSG_ABORT:
/* abort chunk stream =val */
break;
case NGX_RTMP_MSG_ACK:
/* receive ack with sequence number =val */
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"receive ack seq=%uD", val);
break;
case NGX_RTMP_MSG_ACK_SIZE:
/* receive window size =val */
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"receive ack_size=%uD", val);
s->ack_size = val;
break;
case NGX_RTMP_MSG_BANDWIDTH:
if (b->last - b->pos >= 5) {
limit = *(uint8_t*)&b->pos[4];
(void)val;
(void)limit;
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"receive bandwidth=%uD limit=%d",
val, (int)limit);
/* receive window size =val
* && limit */
}
break;
default:
return NGX_ERROR;
}
return NGX_OK;
}
ngx_int_t
ngx_rtmp_user_message_handler(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
ngx_buf_t *b;
u_char *p;
uint16_t evt;
uint32_t val;
b = in->buf;
if (b->last - b->pos < 6) {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"too small buffer for user message: %d",
b->last - b->pos);
return NGX_OK;
}
p = (u_char*)&evt;
p[0] = b->pos[1];
p[1] = b->pos[0];
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"RTMP recv user evt %s (%i)",
ngx_rtmp_user_message_type(evt), (ngx_int_t) evt);
p = (u_char *) &val;
p[0] = b->pos[5];
p[1] = b->pos[4];
p[2] = b->pos[3];
p[3] = b->pos[2];
switch(evt) {
case NGX_RTMP_USER_STREAM_BEGIN:
{
ngx_rtmp_stream_begin_t v;
v.msid = val;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"receive: stream_begin msid=%uD", v.msid);
return ngx_rtmp_stream_begin(s, &v);
}
case NGX_RTMP_USER_STREAM_EOF:
{
ngx_rtmp_stream_eof_t v;
v.msid = val;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"receive: stream_eof msid=%uD", v.msid);
return ngx_rtmp_stream_eof(s, &v);
}
case NGX_RTMP_USER_STREAM_DRY:
{
ngx_rtmp_stream_dry_t v;
v.msid = val;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"receive: stream_dry msid=%uD", v.msid);
return ngx_rtmp_stream_dry(s, &v);
}
case NGX_RTMP_USER_SET_BUFLEN:
{
ngx_rtmp_set_buflen_t v;
v.msid = val;
if (b->last - b->pos < 10) {
return NGX_OK;
}
p = (u_char *) &v.buflen;
p[0] = b->pos[9];
p[1] = b->pos[8];
p[2] = b->pos[7];
p[3] = b->pos[6];
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"receive: set_buflen msid=%uD buflen=%uD",
v.msid, v.buflen);
/*TODO: move this to play module */
s->buflen = v.buflen;
return ngx_rtmp_set_buflen(s, &v);
}
case NGX_RTMP_USER_RECORDED:
{
ngx_rtmp_recorded_t v;
v.msid = val;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"receive: recorded msid=%uD", v.msid);
return ngx_rtmp_recorded(s, &v);
}
case NGX_RTMP_USER_PING_REQUEST:
return ngx_rtmp_send_ping_response(s, val);
case NGX_RTMP_USER_PING_RESPONSE:
/* val = incoming timestamp */
ngx_rtmp_reset_ping(s);
return NGX_OK;
default:
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"unexpected user event: %i", (ngx_int_t) evt);
return NGX_OK;
}
}
static ngx_int_t
ngx_rtmp_fetch(ngx_chain_t **in, u_char *ret)
{
while (*in && (*in)->buf->pos >= (*in)->buf->last) {
*in = (*in)->next;
}
if (*in == NULL) {
return NGX_DONE;
}
*ret = *(*in)->buf->pos++;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_fetch_uint8(ngx_chain_t **in, uint8_t *ret)
{
return ngx_rtmp_fetch(in, (u_char *) ret);
}
static ngx_int_t
ngx_rtmp_fetch_uint32(ngx_chain_t **in, uint32_t *ret, ngx_int_t n)
{
u_char *r = (u_char *) ret;
ngx_int_t rc;
*ret = 0;
while (--n >= 0) {
rc = ngx_rtmp_fetch(in, &r[n]);
if (rc != NGX_OK) {
return rc;
}
}
return NGX_OK;
}
ngx_int_t
ngx_rtmp_aggregate_message_handler(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
uint32_t base_time, timestamp, prev_size;
size_t len;
ngx_int_t first;
u_char *last;
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t *cl, *next;
ngx_rtmp_header_t ch;
ch = *h;
first = 1;
base_time = 0;
while (in) {
if (ngx_rtmp_fetch_uint8(&in, &ch.type) != NGX_OK) {
return NGX_OK;
}
if (ngx_rtmp_fetch_uint32(&in, &ch.mlen, 3) != NGX_OK) {
return NGX_ERROR;
}
if (ngx_rtmp_fetch_uint32(&in, ×tamp, 3) != NGX_OK) {
return NGX_ERROR;
}
if (ngx_rtmp_fetch_uint8(&in, (uint8_t *) ×tamp + 3) != NGX_OK)
{
return NGX_ERROR;
}
if (ngx_rtmp_fetch_uint32(&in, &ch.msid, 3) != NGX_OK)
{
return NGX_ERROR;
}
if (first) {
base_time = timestamp;
first = 0;
}
ngx_log_debug6(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"RTMP aggregate %s (%d) len=%uD time=%uD (+%D) msid=%uD",
ngx_rtmp_message_type(ch.type),
(ngx_int_t) ch.type, ch.mlen, ch.timestamp,
timestamp - base_time, ch.msid);
/* limit chain */
len = 0;
cl = in;
while (cl) {
b = cl->buf;
len += (b->last - b->pos);
if (len > ch.mlen) {
break;
}
cl = cl->next;
}
if (cl == NULL) {
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
"RTMP error parsing aggregate");
return NGX_ERROR;
}
next = cl->next;
cl->next = NULL;
b = cl->buf;
last = b->last;
b->last -= (len - ch.mlen);
/* handle aggregated message */
ch.timestamp = h->timestamp + timestamp - base_time;
rc = ngx_rtmp_receive_message(s, &ch, in);
/* restore chain before checking the result */
in = cl;
in->next = next;
b->pos = b->last;
b->last = last;
if (rc != NGX_OK) {
return rc;
}
/* read 32-bit previous tag size */
if (ngx_rtmp_fetch_uint32(&in, &prev_size, 4) != NGX_OK) {
return NGX_OK;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"RTMP aggregate prev_size=%uD", prev_size);
}
return NGX_OK;
}
ngx_int_t
ngx_rtmp_amf_message_handler(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in)
{
ngx_rtmp_amf_ctx_t act;
ngx_rtmp_core_main_conf_t *cmcf;
ngx_array_t *ch;
ngx_rtmp_handler_pt *ph;
size_t len, n;
static u_char func[128];
static ngx_rtmp_amf_elt_t elts[] = {
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
func, sizeof(func) },
};
/* AMF command names come with string type, but shared object names
* come without type */
if (h->type == NGX_RTMP_MSG_AMF_SHARED ||
h->type == NGX_RTMP_MSG_AMF3_SHARED)
{
elts[0].type |= NGX_RTMP_AMF_TYPELESS;
} else {
elts[0].type &= ~NGX_RTMP_AMF_TYPELESS;
}
if ((h->type == NGX_RTMP_MSG_AMF3_SHARED ||
h->type == NGX_RTMP_MSG_AMF3_META ||
h->type == NGX_RTMP_MSG_AMF3_CMD)
&& in->buf->last > in->buf->pos)
{
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"AMF3 prefix: %ui", (ngx_int_t)*in->buf->pos);
++in->buf->pos;
}
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
/* read AMF func name & transaction id */
ngx_memzero(&act, sizeof(act));
act.link = in;
act.log = s->connection->log;
memset(func, 0, sizeof(func));
if (ngx_rtmp_amf_read(&act, elts,
sizeof(elts) / sizeof(elts[0])) != NGX_OK)
{
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"AMF cmd failed");
return NGX_ERROR;
}
/* skip name */
in = act.link;
in->buf->pos += act.offset;
len = ngx_strlen(func);
ch = ngx_hash_find(&cmcf->amf_hash,
ngx_hash_strlow(func, func, len), func, len);
if (ch && ch->nelts) {
ph = ch->elts;
for (n = 0; n < ch->nelts; ++n, ++ph) {
ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"AMF func '%s' passed to handler %d/%d",
func, n, ch->nelts);
switch ((*ph)(s, h, in)) {
case NGX_ERROR:
return NGX_ERROR;
case NGX_DONE:
return NGX_OK;
}
}
} else {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"AMF cmd '%s' no handler", func);
}
return NGX_OK;
}
ngx_int_t
ngx_rtmp_receive_amf(ngx_rtmp_session_t *s, ngx_chain_t *in,
ngx_rtmp_amf_elt_t *elts, size_t nelts)
{
ngx_rtmp_amf_ctx_t act;
ngx_memzero(&act, sizeof(act));
act.link = in;
act.log = s->connection->log;
return ngx_rtmp_amf_read(&act, elts, nelts);
}