Skip to content

Commit

Permalink
[dev]nginx: make nginx-rtmp module BigEndian friendly]
Browse files Browse the repository at this point in the history
  • Loading branch information
cq65617875 committed Mar 31, 2022
1 parent 4e12f45 commit f8c8ecb
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 334 deletions.
18 changes: 16 additions & 2 deletions hls/ngx_rtmp_hls_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ ngx_rtmp_hls_append_sps_pps(ngx_rtmp_session_t *s, ngx_buf_t *out)
return NGX_ERROR;
}

ngx_rtmp_rmemcpy(&len, &rlen, 2);
len=ntohs(rlen);

ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"hls: header NAL length: %uz", (size_t) len);
Expand Down Expand Up @@ -1920,7 +1920,21 @@ ngx_rtmp_hls_video(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
}

len = 0;
ngx_rtmp_rmemcpy(&len, &rlen, nal_bytes);

switch (nal_bytes) {
case 1:
len=*(uint8_t*)&rlen;
break;
case 2:
len=ntohs(*(uint16_t*)&rlen);
break;
case 3:
len=n3toh4((u_char*)&rlen);
break;
case 4:
len=ntohl(rlen);
break;
};

if (len == 0) {
continue;
Expand Down
16 changes: 0 additions & 16 deletions ngx_rtmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1081,22 +1081,6 @@ ngx_rtmp_fire_event(ngx_rtmp_session_t *s, ngx_uint_t evt,
}


void *
ngx_rtmp_rmemcpy(void *dst, const void* src, size_t n)
{
u_char *d, *s;

d = dst;
s = (u_char*)src + n - 1;

while(s >= (u_char*)src) {
*d++ = *s--;
}

return dst;
}


static ngx_int_t
ngx_rtmp_init_process(ngx_cycle_t *cycle)
{
Expand Down
37 changes: 18 additions & 19 deletions ngx_rtmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,34 +608,33 @@ ngx_int_t ngx_rtmp_fire_event(ngx_rtmp_session_t *s, ngx_uint_t evt,
ngx_int_t ngx_rtmp_set_chunk_size(ngx_rtmp_session_t *s, ngx_uint_t size);


/* Bit reverse: we need big-endians in many places */
void * ngx_rtmp_rmemcpy(void *dst, const void* src, size_t n);

#define ngx_rtmp_rcpymem(dst, src, n) \
(((u_char*)ngx_rtmp_rmemcpy(dst, src, n)) + (n))


static ngx_inline uint16_t
ngx_rtmp_r16(uint16_t n)
/* Bit agnosticism: we need network to host byte-order conversion in many places */
static ngx_inline uint64_t
ntohll(uint64_t n)
{
return (n << 8) | (n >> 8);
#if (NGX_HAVE_LITTLE_ENDIAN)
return (uint64_t) ntohl((uint32_t) n) << 32 |
ntohl((uint32_t) (n >> 32));
#else
return n;
#endif
}


static ngx_inline uint32_t
ngx_rtmp_r32(uint32_t n)
n3toh4(u_char* src)
{
return (n << 24) | ((n << 8) & 0xff0000) | ((n >> 8) & 0xff00) | (n >> 24);
return ((uint32_t)src[0]<<16)|((uint32_t)src[1]<<8)|src[2];
}


static ngx_inline uint64_t
ngx_rtmp_r64(uint64_t n)
static ngx_inline u_char*
h4ton3(u_char* dst, uint32_t src)
{
return (uint64_t) ngx_rtmp_r32((uint32_t) n) << 32 |
ngx_rtmp_r32((uint32_t) (n >> 32));
}
dst[0]=(u_char)(src>>16);
dst[1]=(u_char)(src>>8);
dst[2]=(u_char)src;

return dst+3;
}

/* Receiving messages */
ngx_int_t ngx_rtmp_receive_message(ngx_rtmp_session_t *s,
Expand Down
59 changes: 17 additions & 42 deletions ngx_rtmp_amf.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,6 @@
#include "ngx_rtmp.h"
#include <string.h>


static ngx_inline void*
ngx_rtmp_amf_reverse_copy(void *dst, void* src, size_t len)
{
size_t k;

if (dst == NULL || src == NULL) {
return NULL;
}

for(k = 0; k < len; ++k) {
((u_char*)dst)[k] = ((u_char*)src)[len - 1 - k];
}

return dst;
}

#define NGX_RTMP_AMF_DEBUG_SIZE 16

#ifdef NGX_DEBUG
Expand Down Expand Up @@ -208,7 +191,7 @@ ngx_rtmp_amf_read_object(ngx_rtmp_amf_ctx_t *ctx, ngx_rtmp_amf_elt_t *elts,
return NGX_ERROR;
}

ngx_rtmp_amf_reverse_copy(&len, buf, 2);
len=ntohs(*(uint16_t*)&buf[0]);

if (!len)
break;
Expand Down Expand Up @@ -259,7 +242,7 @@ ngx_rtmp_amf_read_array(ngx_rtmp_amf_ctx_t *ctx, ngx_rtmp_amf_elt_t *elts,
if (ngx_rtmp_amf_get(ctx, buf, 4) != NGX_OK)
return NGX_ERROR;

ngx_rtmp_amf_reverse_copy(&len, buf, 4);
len=ntohl(*(uint32_t*)&buf[0]);

for (n = 0; n < len; ++n) {
if (ngx_rtmp_amf_read(ctx, n < nelts ? &elts[n] : NULL, 1) != NGX_OK)
Expand Down Expand Up @@ -355,10 +338,9 @@ ngx_rtmp_amf_read(ngx_rtmp_amf_ctx_t *ctx, ngx_rtmp_amf_elt_t *elts,

switch (type) {
case NGX_RTMP_AMF_NUMBER:
if (ngx_rtmp_amf_get(ctx, buf, 8) != NGX_OK) {
if (ngx_rtmp_amf_get(ctx, data, 8) != NGX_OK) {
return NGX_ERROR;
}
ngx_rtmp_amf_reverse_copy(data, buf, 8);
break;

case NGX_RTMP_AMF_BOOLEAN:
Expand All @@ -371,7 +353,7 @@ ngx_rtmp_amf_read(ngx_rtmp_amf_ctx_t *ctx, ngx_rtmp_amf_elt_t *elts,
if (ngx_rtmp_amf_get(ctx, buf, 2) != NGX_OK) {
return NGX_ERROR;
}
ngx_rtmp_amf_reverse_copy(&len, buf, 2);
len=ntohs(*(uint16_t*)buf);

if (data == NULL) {
rc = ngx_rtmp_amf_get(ctx, data, len);
Expand Down Expand Up @@ -442,14 +424,14 @@ ngx_rtmp_amf_read(ngx_rtmp_amf_ctx_t *ctx, ngx_rtmp_amf_elt_t *elts,
if (ngx_rtmp_amf_get(ctx, buf, 2) != NGX_OK) {
return NGX_ERROR;
}
ngx_rtmp_amf_reverse_copy(data, buf, 2);
*(uint16_t*)data=ntohs(*(uint16_t*)buf);
break;

case NGX_RTMP_AMF_INT32:
if (ngx_rtmp_amf_get(ctx, buf, 4) != NGX_OK) {
return NGX_ERROR;
}
ngx_rtmp_amf_reverse_copy(data, buf, 4);
*(uint32_t*)data=ntohs(*(uint32_t*)buf);
break;

case NGX_RTMP_AMF_END:
Expand Down Expand Up @@ -480,9 +462,8 @@ ngx_rtmp_amf_write_object(ngx_rtmp_amf_ctx_t *ctx,

len = (uint16_t) elts[n].name.len;

if (ngx_rtmp_amf_put(ctx,
ngx_rtmp_amf_reverse_copy(buf,
&len, 2), 2) != NGX_OK)
*(uint16_t*)buf = htons(len);
if (ngx_rtmp_amf_put(ctx, buf, 2) != NGX_OK)
{
return NGX_ERROR;
}
Expand Down Expand Up @@ -513,9 +494,8 @@ ngx_rtmp_amf_write_array(ngx_rtmp_amf_ctx_t *ctx,
u_char buf[4];

len = nelts;
if (ngx_rtmp_amf_put(ctx,
ngx_rtmp_amf_reverse_copy(buf,
&len, 4), 4) != NGX_OK)
*(uint32_t*)buf = htonl(len);
if (ngx_rtmp_amf_put(ctx, buf, 4) != NGX_OK)
{
return NGX_ERROR;
}
Expand Down Expand Up @@ -558,9 +538,7 @@ ngx_rtmp_amf_write(ngx_rtmp_amf_ctx_t *ctx,

switch(type) {
case NGX_RTMP_AMF_NUMBER:
if (ngx_rtmp_amf_put(ctx,
ngx_rtmp_amf_reverse_copy(buf,
data, 8), 8) != NGX_OK)
if (ngx_rtmp_amf_put(ctx, data, 8) != NGX_OK)
{
return NGX_ERROR;
}
Expand All @@ -577,9 +555,8 @@ ngx_rtmp_amf_write(ngx_rtmp_amf_ctx_t *ctx,
len = (uint16_t) ngx_strlen((u_char*) data);
}

if (ngx_rtmp_amf_put(ctx,
ngx_rtmp_amf_reverse_copy(buf,
&len, 2), 2) != NGX_OK)
*(uint16_t*)buf = htons(len);
if (ngx_rtmp_amf_put(ctx, buf, 2) != NGX_OK)
{
return NGX_ERROR;
}
Expand Down Expand Up @@ -626,18 +603,16 @@ ngx_rtmp_amf_write(ngx_rtmp_amf_ctx_t *ctx,
break;

case NGX_RTMP_AMF_INT16:
if (ngx_rtmp_amf_put(ctx,
ngx_rtmp_amf_reverse_copy(buf,
data, 2), 2) != NGX_OK)
*(uint16_t*)buf = htons(*(uint16_t*)data);
if (ngx_rtmp_amf_put(ctx, buf, 2) != NGX_OK)
{
return NGX_ERROR;
}
break;

case NGX_RTMP_AMF_INT32:
if (ngx_rtmp_amf_put(ctx,
ngx_rtmp_amf_reverse_copy(buf,
data, 4), 4) != NGX_OK)
*(uint32_t*)buf = htonl(*(uint32_t*)data);
if (ngx_rtmp_amf_put(ctx, buf, 4) != NGX_OK)
{
return NGX_ERROR;
}
Expand Down
2 changes: 1 addition & 1 deletion ngx_rtmp_bitop.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ uint64_t ngx_rtmp_bit_read_golomb(ngx_rtmp_bit_reader_t *br);
((uint32_t) ngx_rtmp_bit_read(br, 32))

#define ngx_rtmp_bit_read_64(br) \
((uint64_t) ngx_rtmp_read(br, 64))
((uint64_t) ngx_rtmp_bit_read(br, 64))


#endif /* _NGX_RTMP_BITOP_H_INCLUDED_ */
2 changes: 1 addition & 1 deletion ngx_rtmp_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ ngx_rtmp_eval(void *ctx, ngx_str_t *in, ngx_rtmp_eval_t **e, ngx_str_t *out,
}

/* fall through */

/* fall through */
case ESCAPE:
ngx_rtmp_eval_append(&b, &c, 1, log);
state = NORMAL;
Expand Down
20 changes: 6 additions & 14 deletions ngx_rtmp_flv_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ ngx_rtmp_flv_fill_index(ngx_rtmp_amf_ctx_t *ctx, ngx_rtmp_flv_index_t *idx)
return NGX_ERROR;
}

ngx_rtmp_rmemcpy(&nelts, b->pos + ctx->offset, 4);
nelts=htonl(*(uint32_t*)(b->pos + ctx->offset));

idx->nelts = nelts;
idx->offset = ctx->offset + 4;
Expand Down Expand Up @@ -201,11 +201,7 @@ ngx_rtmp_flv_init_index(ngx_rtmp_session_t *s, ngx_chain_t *in)
static double
ngx_rtmp_flv_index_value(void *src)
{
double v;

ngx_rtmp_rmemcpy(&v, src, 8);

return v;
return *(double*)src;
}


Expand Down Expand Up @@ -352,8 +348,7 @@ ngx_rtmp_flv_read_meta(ngx_rtmp_session_t *s, ngx_file_t *f)
h.msid = NGX_RTMP_MSID;
h.csid = NGX_RTMP_CSID_AMF;

size = 0;
ngx_rtmp_rmemcpy(&size, ngx_rtmp_flv_header + 1, 3);
size = n3toh4(ngx_rtmp_flv_header + 1);

ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"flv: metadata size=%D", size);
Expand Down Expand Up @@ -440,12 +435,9 @@ ngx_rtmp_flv_send(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t *ts)
h.msid = NGX_RTMP_MSID;
h.type = ngx_rtmp_flv_header[0];

size = 0;

ngx_rtmp_rmemcpy(&size, ngx_rtmp_flv_header + 1, 3);
ngx_rtmp_rmemcpy(&h.timestamp, ngx_rtmp_flv_header + 4, 3);

((u_char *) &h.timestamp)[3] = ngx_rtmp_flv_header[7];
size = n3toh4(ngx_rtmp_flv_header + 1);
h.timestamp = n3toh4(ngx_rtmp_flv_header + 4);
h.timestamp |= ((uint32_t) ngx_rtmp_flv_header[7] << 24);

ctx->offset += (sizeof(ngx_rtmp_flv_header) + size + 4);

Expand Down
Loading

0 comments on commit f8c8ecb

Please sign in to comment.