mirror of
https://github.com/zotanmew/nginx-rtmp-module.git
synced 2024-05-20 18:01:08 +02:00
Add client side bandwidth detection
This commit is contained in:
parent
7f64585b75
commit
2d9b53fb95
|
@ -592,6 +592,8 @@ ngx_int_t ngx_rtmp_send_fi(ngx_rtmp_session_t *s);
|
|||
ngx_int_t ngx_rtmp_send_bwcheck(ngx_rtmp_session_t *s, u_char *payload);
|
||||
ngx_int_t ngx_rtmp_send_bwdone(ngx_rtmp_session_t *s,
|
||||
double kbitDown, ngx_uint_t deltaDown, double deltaTime, ngx_msec_t latency);
|
||||
ngx_int_t ngx_rtmp_send_onclientbwcheck(ngx_rtmp_session_t *s, double inTrans,
|
||||
double cOutBytes, double cInBytes, ngx_uint_t inTime);
|
||||
|
||||
|
||||
/* Frame types */
|
||||
|
|
|
@ -327,6 +327,88 @@ ngx_rtmp_bandwidth_detection_start(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Bandwidth detection from client side
|
||||
*/
|
||||
static ngx_int_t
|
||||
ngx_rtmp_bandwidth_detection_clientcheck(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in)
|
||||
{
|
||||
|
||||
ngx_rtmp_bandwidth_detection_app_conf_t *acf;
|
||||
ngx_rtmp_bandwidth_detection_ctx_t *bw_ctx;
|
||||
|
||||
static struct {
|
||||
double trans;
|
||||
ngx_msec_t time;
|
||||
} v;
|
||||
|
||||
static ngx_rtmp_amf_elt_t in_elts[] = {
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_null_string,
|
||||
&v.trans, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NULL,
|
||||
ngx_null_string,
|
||||
NULL, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_null_string,
|
||||
&v.time, 0 },
|
||||
|
||||
};
|
||||
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
||||
"bandwidth_detection: bwcheck");
|
||||
|
||||
if (s->relay) {
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
||||
"bandwidth_detection: bwcheck - no relay please!");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
acf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_bandwidth_detection_module);
|
||||
if (acf == NULL) {
|
||||
ngx_log_error(NGX_LOG_WARN, s->connection->log, 0,
|
||||
"bandwidth_detection: bwcheck - no app config!");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
bw_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_bandwidth_detection_module);
|
||||
if (bw_ctx == NULL) {
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
||||
"bandwidth_detection: bwcheck - no context! create new and set for module and session!");
|
||||
|
||||
bw_ctx = ngx_palloc(s->connection->pool, sizeof(ngx_rtmp_bandwidth_detection_ctx_t));
|
||||
if (bw_ctx == NULL) {
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
||||
"bandwidth_detection: bwcheck - no context created!");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
ngx_rtmp_set_ctx(s, bw_ctx, ngx_rtmp_bandwidth_detection_module);
|
||||
ngx_memzero(bw_ctx, sizeof(*bw_ctx));
|
||||
|
||||
}
|
||||
|
||||
ngx_memzero(&v, sizeof(v));
|
||||
if (ngx_rtmp_receive_amf(s, in, in_elts,
|
||||
sizeof(in_elts) / sizeof(in_elts[0])))
|
||||
{
|
||||
ngx_log_error(NGX_LOG_WARN, s->connection->log, 0,
|
||||
"bandwidth_detection: bwcheck - no packet readed!");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
||||
"bandwidth_detection: bwcheck: trans='%f' time='%ui'",
|
||||
v.trans, v.time);
|
||||
|
||||
// Send first packet with empty payload - for latency calculation
|
||||
return ngx_rtmp_send_onclientbwcheck(s, v.trans, s->out_bytes, s->in_bytes, v.time);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* End bandwidth detection here
|
||||
*/
|
||||
|
@ -451,7 +533,7 @@ ngx_rtmp_bandwidth_detection_postconfiguration(ngx_conf_t *cf)
|
|||
|
||||
ch = ngx_array_push(&cmcf->amf);
|
||||
ngx_str_set(&ch->name, "onClientBWCheck");
|
||||
ch->handler = ngx_rtmp_bandwidth_detection_start;
|
||||
ch->handler = ngx_rtmp_bandwidth_detection_clientcheck;
|
||||
|
||||
return NGX_OK;
|
||||
}
|
||||
|
|
|
@ -1127,3 +1127,69 @@ ngx_rtmp_send_bwdone(ngx_rtmp_session_t *s,
|
|||
return ngx_rtmp_send_shared_packet(s,
|
||||
ngx_rtmp_create_bwdone(s, kbitDown, deltaDown, deltaTime, latency));
|
||||
}
|
||||
|
||||
|
||||
ngx_int_t
|
||||
ngx_rtmp_send_onclientbwcheck(ngx_rtmp_session_t *s, double inTrans,
|
||||
double cOutBytes, double cInBytes, ngx_uint_t inTime)
|
||||
{
|
||||
ngx_rtmp_core_srv_conf_t *cscf;
|
||||
ngx_rtmp_core_app_conf_t **cacfp;
|
||||
ngx_uint_t n;
|
||||
ngx_rtmp_header_t h;
|
||||
u_char *p;
|
||||
|
||||
static struct {
|
||||
double cOutBytes;
|
||||
double cInBytes;
|
||||
ngx_msec_t time;
|
||||
double trans;
|
||||
} v;
|
||||
|
||||
static ngx_rtmp_amf_elt_t out_inf[] = {
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_string("cOutBytes"),
|
||||
&v.cOutBytes, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_string("cInBytes"),
|
||||
&v.cInBytes, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_string("time"),
|
||||
&v.time, 0 },
|
||||
|
||||
};
|
||||
|
||||
static ngx_rtmp_amf_elt_t out_elts[] = {
|
||||
|
||||
{ NGX_RTMP_AMF_STRING,
|
||||
ngx_null_string,
|
||||
"_result", 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_null_string,
|
||||
&v.trans, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_OBJECT,
|
||||
ngx_null_string,
|
||||
out_obj, sizeof(out_inf) },
|
||||
|
||||
};
|
||||
|
||||
ngx_memzero(&v, sizeof(v));
|
||||
|
||||
v.cOutBytes = cOutBytes;
|
||||
v.cInBytes = cInBytes;
|
||||
v.time = inTime;
|
||||
v.trans = inTrans;
|
||||
|
||||
ngx_memzero(&h, sizeof(h));
|
||||
h.csid = NGX_RTMP_CSID_AMF_INI;
|
||||
h.type = NGX_RTMP_MSG_AMF_CMD;
|
||||
|
||||
return ngx_rtmp_send_amf(s, &h, out_elts,
|
||||
sizeof(out_elts) / sizeof(out_elts[0])) == NGX_OK ?
|
||||
NGX_DONE : NGX_ERROR;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue