mirror of
https://github.com/zotanmew/nginx-rtmp-module.git
synced 2024-05-10 14:31:07 +02:00
New module for bandwidth detection
This commit is contained in:
parent
7e68afde6f
commit
a613300142
7
config
7
config
|
@ -7,6 +7,7 @@ RTMP_CORE_MODULES=" \
|
|||
ngx_rtmp_access_module \
|
||||
ngx_rtmp_record_module \
|
||||
ngx_rtmp_live_module \
|
||||
ngx_rtmp_bandwidth_detection_module \
|
||||
ngx_rtmp_play_module \
|
||||
ngx_rtmp_flv_module \
|
||||
ngx_rtmp_mp4_module \
|
||||
|
@ -33,6 +34,7 @@ RTMP_DEPS=" \
|
|||
$ngx_addon_dir/ngx_rtmp.h \
|
||||
$ngx_addon_dir/ngx_rtmp_version.h \
|
||||
$ngx_addon_dir/ngx_rtmp_live_module.h \
|
||||
$ngx_addon_dir/ngx_rtmp_bandwidth_detection_module.h \
|
||||
$ngx_addon_dir/ngx_rtmp_netcall_module.h \
|
||||
$ngx_addon_dir/ngx_rtmp_play_module.h \
|
||||
$ngx_addon_dir/ngx_rtmp_record_module.h \
|
||||
|
@ -59,6 +61,7 @@ RTMP_CORE_SRCS=" \
|
|||
$ngx_addon_dir/ngx_rtmp_access_module.c \
|
||||
$ngx_addon_dir/ngx_rtmp_record_module.c \
|
||||
$ngx_addon_dir/ngx_rtmp_live_module.c \
|
||||
$ngx_addon_dir/ngx_rtmp_bandwidth_detection_module.c \
|
||||
$ngx_addon_dir/ngx_rtmp_play_module.c \
|
||||
$ngx_addon_dir/ngx_rtmp_flv_module.c \
|
||||
$ngx_addon_dir/ngx_rtmp_mp4_module.c \
|
||||
|
@ -111,6 +114,6 @@ fi
|
|||
|
||||
USE_OPENSSL=YES
|
||||
|
||||
CFLAGS="$CFLAGS -I$ngx_addon_dir"
|
||||
# CFLAGS="$CFLAGS -I$ngx_addon_dir"
|
||||
# Debug build with all warnings as errors
|
||||
# CFLAGS="$CFLAGS -I$ngx_addon_dir -Wall -Wpointer-arith -Wno-unused-parameter -Werror"
|
||||
CFLAGS="$CFLAGS -I$ngx_addon_dir -Wall -Wpointer-arith -Wno-unused-parameter -Werror"
|
||||
|
|
|
@ -589,6 +589,9 @@ ngx_int_t ngx_rtmp_send_close_method(ngx_rtmp_session_t *s, char *methodName);
|
|||
ngx_int_t ngx_rtmp_send_fcpublish(ngx_rtmp_session_t *s, u_char *desc);
|
||||
ngx_int_t ngx_rtmp_send_fcunpublish(ngx_rtmp_session_t *s, u_char *desc);
|
||||
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);
|
||||
|
||||
|
||||
/* Frame types */
|
||||
|
|
434
ngx_rtmp_bandwidth_detection_module.c
Normal file
434
ngx_rtmp_bandwidth_detection_module.c
Normal file
|
@ -0,0 +1,434 @@
|
|||
|
||||
/*
|
||||
* Copyright (C) Sergey Dryabzhinsky, 2016
|
||||
*
|
||||
* Based on http://permalink.gmane.org/gmane.comp.web.flash.red5/5869
|
||||
* And live & stat modules
|
||||
*/
|
||||
|
||||
|
||||
#include <ngx_config.h>
|
||||
#include <ngx_core.h>
|
||||
#include "ngx_rtmp_bandwidth_detection_module.h"
|
||||
#include "ngx_rtmp_live_module.h"
|
||||
#include "ngx_rtmp_cmd_module.h"
|
||||
#include "ngx_rtmp_transitions.h"
|
||||
|
||||
|
||||
#define NGX_RTMP_BANDWIDTH_DETECTION_PAYLOAD_LENGTH 16*1024
|
||||
|
||||
|
||||
static ngx_int_t ngx_rtmp_bandwidth_detection_postconfiguration(ngx_conf_t *cf);
|
||||
static void * ngx_rtmp_bandwidth_detection_create_app_conf(ngx_conf_t *cf);
|
||||
static char * ngx_rtmp_bandwidth_detection_merge_app_conf(ngx_conf_t *cf,
|
||||
void *parent, void *child);
|
||||
static char *ngx_rtmp_bandwidth_detection_set_msec_slot(ngx_conf_t *cf, ngx_command_t *cmd,
|
||||
void *conf);
|
||||
static ngx_int_t ngx_rtmp_bandwidth_detection_start(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in);
|
||||
static ngx_int_t ngx_rtmp_bandwidth_detection_check_result(ngx_rtmp_session_t *s);
|
||||
|
||||
|
||||
static ngx_command_t ngx_rtmp_bandwidth_detection_commands[] = {
|
||||
|
||||
{ ngx_string("latency_min"),
|
||||
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
|
||||
ngx_rtmp_bandwidth_detection_set_msec_slot,
|
||||
NGX_RTMP_APP_CONF_OFFSET,
|
||||
offsetof(ngx_rtmp_bandwidth_detection_app_conf_t, latency_min),
|
||||
NULL },
|
||||
|
||||
{ ngx_string("latency_max"),
|
||||
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
|
||||
ngx_rtmp_bandwidth_detection_set_msec_slot,
|
||||
NGX_RTMP_APP_CONF_OFFSET,
|
||||
offsetof(ngx_rtmp_bandwidth_detection_app_conf_t, latency_max),
|
||||
NULL },
|
||||
|
||||
{ ngx_string("latency_undef"),
|
||||
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
|
||||
ngx_rtmp_bandwidth_detection_set_msec_slot,
|
||||
NGX_RTMP_APP_CONF_OFFSET,
|
||||
offsetof(ngx_rtmp_bandwidth_detection_app_conf_t, latency_undef),
|
||||
NULL },
|
||||
|
||||
{ ngx_string("test_time"),
|
||||
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
|
||||
ngx_rtmp_bandwidth_detection_set_msec_slot,
|
||||
NGX_RTMP_APP_CONF_OFFSET,
|
||||
offsetof(ngx_rtmp_bandwidth_detection_app_conf_t, test_time),
|
||||
NULL },
|
||||
|
||||
ngx_null_command
|
||||
};
|
||||
|
||||
|
||||
static ngx_rtmp_module_t ngx_rtmp_bandwidth_detection_module_ctx = {
|
||||
NULL, /* preconfiguration */
|
||||
ngx_rtmp_bandwidth_detection_postconfiguration, /* postconfiguration */
|
||||
NULL, /* create main configuration */
|
||||
NULL, /* init main configuration */
|
||||
NULL, /* create server configuration */
|
||||
NULL, /* merge server configuration */
|
||||
ngx_rtmp_bandwidth_detection_create_app_conf, /* create app configuration */
|
||||
ngx_rtmp_bandwidth_detection_merge_app_conf /* merge app configuration */
|
||||
};
|
||||
|
||||
|
||||
ngx_module_t ngx_rtmp_bandwidth_detection_module = {
|
||||
NGX_MODULE_V1,
|
||||
&ngx_rtmp_bandwidth_detection_module_ctx, /* module context */
|
||||
ngx_rtmp_bandwidth_detection_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_bandwidth_detection_create_app_conf(ngx_conf_t *cf)
|
||||
{
|
||||
ngx_rtmp_bandwidth_detection_app_conf_t *acf;
|
||||
ngx_uint_t i;
|
||||
|
||||
acf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_bandwidth_detection_app_conf_t));
|
||||
if (acf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
acf->latency_max = NGX_CONF_UNSET;
|
||||
acf->latency_min = NGX_CONF_UNSET_MSEC;
|
||||
acf->latency_undef = NGX_CONF_UNSET_MSEC;
|
||||
acf->test_time = NGX_CONF_UNSET_MSEC;
|
||||
|
||||
/* Init payload only once with some random garbage */
|
||||
acf->payload = ngx_pcalloc(cf->pool, NGX_RTMP_BANDWIDTH_DETECTION_PAYLOAD_LENGTH + 1);
|
||||
for (i=0; i<NGX_RTMP_BANDWIDTH_DETECTION_PAYLOAD_LENGTH; i++) {
|
||||
acf->payload[i] = ngx_random();
|
||||
}
|
||||
|
||||
return acf;
|
||||
}
|
||||
|
||||
|
||||
static char *
|
||||
ngx_rtmp_bandwidth_detection_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
|
||||
{
|
||||
ngx_rtmp_bandwidth_detection_app_conf_t *prev = parent;
|
||||
ngx_rtmp_bandwidth_detection_app_conf_t *conf = child;
|
||||
|
||||
ngx_conf_merge_msec_value(conf->latency_max, prev->latency_max, 800);
|
||||
ngx_conf_merge_msec_value(conf->latency_min, prev->latency_min, 10);
|
||||
ngx_conf_merge_msec_value(conf->latency_undef, prev->latency_undef, 100);
|
||||
ngx_conf_merge_msec_value(conf->test_time, prev->test_time, 2000);
|
||||
|
||||
// One payload for all
|
||||
conf->payload = prev->payload;
|
||||
|
||||
return NGX_CONF_OK;
|
||||
}
|
||||
|
||||
|
||||
static char *
|
||||
ngx_rtmp_bandwidth_detection_set_msec_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
||||
{
|
||||
char *p = conf;
|
||||
ngx_str_t *value;
|
||||
ngx_msec_t *msp;
|
||||
|
||||
msp = (ngx_msec_t *) (p + cmd->offset);
|
||||
|
||||
value = cf->args->elts;
|
||||
|
||||
if (value[1].len == sizeof("off") - 1 &&
|
||||
ngx_strncasecmp(value[1].data, (u_char *) "off", value[1].len) == 0)
|
||||
{
|
||||
*msp = 0;
|
||||
return NGX_CONF_OK;
|
||||
}
|
||||
|
||||
return ngx_conf_set_msec_slot(cf, cmd, conf);
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_rtmp_bandwidth_detection_on_result(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
|
||||
ngx_chain_t *in)
|
||||
{
|
||||
ngx_rtmp_bandwidth_detection_ctx_t *ctx;
|
||||
|
||||
static struct {
|
||||
double trans;
|
||||
ngx_uint_t count;
|
||||
} v;
|
||||
|
||||
static ngx_rtmp_amf_elt_t in_inf[] = {
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_null_string,
|
||||
&v.count, 0 },
|
||||
|
||||
};
|
||||
|
||||
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_OBJECT,
|
||||
ngx_null_string,
|
||||
in_inf, sizeof(in_inf) },
|
||||
};
|
||||
|
||||
|
||||
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_bandwidth_detection_module);
|
||||
if (ctx == NULL || s->relay) {
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
ngx_memzero(&v, sizeof(v));
|
||||
if (ngx_rtmp_receive_amf(s, in, in_elts,
|
||||
sizeof(in_elts) / sizeof(in_elts[0])))
|
||||
{
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
||||
"relay: _result: level='%s' code='%s' description='%s'",
|
||||
v.level, v.code, v.desc);
|
||||
|
||||
switch ((ngx_int_t)v.trans) {
|
||||
case NGX_RTMP_BANDWIDTH_DETECTION_BWCHECK_TRANS:
|
||||
return ngx_rtmp_bandwidth_detection_check_result(s);
|
||||
break;
|
||||
|
||||
case NGX_RTMP_BANDWIDTH_DETECTION_BWDONE_TRANS:
|
||||
/* Need to test it. Maybe need to set this before send bwDone. */
|
||||
ctx->active = 0;
|
||||
break;
|
||||
default:
|
||||
return NGX_OK;
|
||||
}
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Start bandwidth detection here
|
||||
*/
|
||||
static ngx_int_t
|
||||
ngx_rtmp_bandwidth_detection_start(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;
|
||||
ngx_rtmp_live_ctx_t *lv_ctx;
|
||||
|
||||
static ngx_rtmp_amf_elt_t in_elts[] = {
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_null_string,
|
||||
"checkBandwidth", 0 },
|
||||
|
||||
};
|
||||
|
||||
if (s->relay) {
|
||||
ngx_log_debug0(NGX_LOG_DEBUG, s->connection->log, 0,
|
||||
"bandwidth_detection: start - 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: start - no app config!");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (!acf->test_time || in == NULL || in->buf == NULL) {
|
||||
ngx_log_debug0(NGX_LOG_DEBUG, s->connection->log, 0,
|
||||
"bandwidth_detection: start - no test time or no buffer!");
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
bw_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_bandwidth_detection_module);
|
||||
if (bw_ctx == NULL) {
|
||||
ngx_log_debug0(NGX_LOG_DEBUG, s->connection->log, 0,
|
||||
"bandwidth_detection: start - no bw context!");
|
||||
return NGX_OK;
|
||||
}
|
||||
if (bw_ctx->active) {
|
||||
ngx_log_debug0(NGX_LOG_DEBUG, s->connection->log, 0,
|
||||
"bandwidth_detection: start - already active!");
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
lv_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
|
||||
if (lv_ctx == NULL) {
|
||||
ngx_log_debug0(NGX_LOG_DEBUG, s->connection->log, 0,
|
||||
"bandwidth_detection: start - no live context!");
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
if (ngx_rtmp_receive_amf(s, in, in_elts,
|
||||
sizeof(in_elts) / sizeof(in_elts[0])) != NGX_OK) {
|
||||
ngx_log_error(NGX_LOG_WARN, s->connection->log, 0,
|
||||
"bandwidth_detection: start - not received command packet!");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
bw_ctx->active = 1;
|
||||
bw_ctx->bw_begin_time = ngx_cached_time->msec;
|
||||
bw_ctx->pkt_sent = 1;
|
||||
bw_ctx->pkt_received = 0;
|
||||
bw_ctx->pkt_recv_time1 = 0;
|
||||
bw_ctx->pkt_recv_time2 = 0;
|
||||
bw_ctx->cum_latency = 0;
|
||||
bw_ctx->latency = acf->latency_min;
|
||||
bw_ctx->bytes_out = lv_ctx->stream->bw_out.bytes;
|
||||
|
||||
// Send first packet with empty payload - for latency calculation
|
||||
return ngx_rtmp_send_bwcheck(s, NULL);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* End bandwidth detection here
|
||||
*/
|
||||
static ngx_int_t
|
||||
ngx_rtmp_bandwidth_detection_check_result(ngx_rtmp_session_t *s)
|
||||
{
|
||||
|
||||
ngx_rtmp_bandwidth_detection_app_conf_t *acf;
|
||||
ngx_rtmp_bandwidth_detection_ctx_t *bw_ctx;
|
||||
ngx_rtmp_live_ctx_t *lv_ctx;
|
||||
ngx_uint_t timePassed;
|
||||
ngx_uint_t deltaDown;
|
||||
double deltaTime;
|
||||
double kbitDown;
|
||||
uint64_t bytesOut;
|
||||
|
||||
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: check - no app config!");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (!acf->test_time) {
|
||||
ngx_log_error(NGX_LOG_WARN, s->connection->log, 0,
|
||||
"bandwidth_detection: check - no test time!");
|
||||
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, s->connection->log, 0,
|
||||
"bandwidth_detection: check - no bw context!");
|
||||
return NGX_OK;
|
||||
}
|
||||
if (!bw_ctx->active) {
|
||||
ngx_log_debug0(NGX_LOG_DEBUG, s->connection->log, 0,
|
||||
"bandwidth_detection: check - not active!");
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
lv_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
|
||||
if (lv_ctx == NULL) {
|
||||
ngx_log_debug0(NGX_LOG_DEBUG, s->connection->log, 0,
|
||||
"bandwidth_detection: check - no live context!");
|
||||
return NGX_OK;
|
||||
}
|
||||
if (lv_ctx->stream == NULL) {
|
||||
ngx_log_debug0(NGX_LOG_DEBUG, s->connection->log, 0,
|
||||
"bandwidth_detection: check - no live stream!");
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
timePassed = ngx_cached_time->msec - bw_ctx->bw_begin_time;
|
||||
bw_ctx->pkt_received ++;
|
||||
|
||||
if (bw_ctx->pkt_received == 1) {
|
||||
bw_ctx->latency = ngx_min(timePassed, acf->latency_max);
|
||||
bw_ctx->latency = ngx_max(bw_ctx->latency, acf->latency_min);
|
||||
bw_ctx->pkt_recv_time1 = ngx_cached_time->msec;
|
||||
}
|
||||
|
||||
if (bw_ctx->pkt_received == 2) {
|
||||
bw_ctx->pkt_recv_time2 = ngx_cached_time->msec;
|
||||
}
|
||||
|
||||
// If we have a hi-speed network with low latency send more to determine
|
||||
// better bandwidth numbers, send no more than 6 packets
|
||||
if (bw_ctx->pkt_received == 2 && timePassed < acf->test_time) {
|
||||
|
||||
bw_ctx->pkt_sent ++;
|
||||
bw_ctx->cum_latency ++;
|
||||
return ngx_rtmp_send_bwcheck(s, acf->payload);
|
||||
|
||||
} else if (bw_ctx->pkt_received == bw_ctx->pkt_sent) {
|
||||
|
||||
// See if we need to normalize latency
|
||||
if ( bw_ctx->latency >= acf->latency_undef ) {
|
||||
|
||||
// make sure we detect sattelite and modem correctly
|
||||
if (bw_ctx->pkt_recv_time2 - bw_ctx->pkt_recv_time1 > 1000) {
|
||||
bw_ctx->latency = acf->latency_undef;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bytesOut = lv_ctx->stream->bw_out.bytes;
|
||||
deltaDown = (bytesOut - bw_ctx->bytes_out) *8/1000.;
|
||||
deltaTime = ( (ngx_cached_time->msec - bw_ctx->bw_begin_time) - (bw_ctx->latency*bw_ctx->cum_latency))/1000.;
|
||||
|
||||
if (deltaTime <= 0) deltaTime = (ngx_cached_time->msec - bw_ctx->bw_begin_time)/1000.;
|
||||
|
||||
kbitDown = deltaDown/deltaTime;
|
||||
|
||||
ngx_log_debug0(NGX_LOG_DEBUG, s->connection->log, 0,
|
||||
"bandwidth_detection: check - done!");
|
||||
ngx_log_debug5(NGX_LOG_DEBUG, s->connection->log, 0,
|
||||
"bandwidth_detection: check - kbitDown=%ui, deltaDown=%.3f, deltaTime=%.3f, latency=%.3f, KBytes=%ui",
|
||||
kbitDown, deltaDown, deltaTime, bw_ctx->latency, (bytesOut - bw_ctx->bytes_out)/1024);
|
||||
|
||||
return ngx_rtmp_send_bwdone(s, kbitDown, deltaDown, deltaTime, bw_ctx->latency);
|
||||
}
|
||||
|
||||
if (bw_ctx->pkt_sent == 1 && bw_ctx->pkt_received == 1) {
|
||||
// First call
|
||||
return ngx_rtmp_send_bwcheck(s, acf->payload);
|
||||
}
|
||||
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_rtmp_bandwidth_detection_postconfiguration(ngx_conf_t *cf)
|
||||
{
|
||||
ngx_rtmp_core_main_conf_t *cmcf;
|
||||
ngx_rtmp_amf_handler_t *ch;
|
||||
|
||||
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
|
||||
|
||||
ch = ngx_array_push(&cmcf->amf);
|
||||
ngx_str_set(&ch->name, "checkBandwidth");
|
||||
ch->handler = ngx_rtmp_bandwidth_detection_start;
|
||||
|
||||
ch = ngx_array_push(&cmcf->amf);
|
||||
ngx_str_set(&ch->name, "_result");
|
||||
ch->handler = ngx_rtmp_bandwidth_detection_on_result;
|
||||
|
||||
return NGX_OK;
|
||||
}
|
50
ngx_rtmp_bandwidth_detection_module.h
Normal file
50
ngx_rtmp_bandwidth_detection_module.h
Normal file
|
@ -0,0 +1,50 @@
|
|||
|
||||
/*
|
||||
* Copyright (C) Sergey Dryabzhinsky, 2016
|
||||
*
|
||||
* Based on http://permalink.gmane.org/gmane.comp.web.flash.red5/5869
|
||||
* And live & stat modules
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _NGX_RTMP_BANDWIDTH_DETECTION_H_INCLUDED_
|
||||
#define _NGX_RTMP_BANDWIDTH_DETECTION_H_INCLUDED_
|
||||
|
||||
#include <ngx_config.h>
|
||||
#include <ngx_core.h>
|
||||
#include "ngx_rtmp.h"
|
||||
#include "ngx_rtmp_cmd_module.h"
|
||||
#include "ngx_rtmp_bandwidth.h"
|
||||
#include "ngx_rtmp_streams.h"
|
||||
|
||||
|
||||
typedef struct ngx_rtmp_bandwidth_detection_ctx_s ngx_rtmp_bandwidth_detection_ctx_t;
|
||||
|
||||
struct ngx_rtmp_bandwidth_detection_ctx_s {
|
||||
ngx_rtmp_session_t *session;
|
||||
uint64_t bytes_out;
|
||||
ngx_msec_t bw_begin_time;
|
||||
ngx_uint_t pkt_sent;
|
||||
ngx_uint_t pkt_received;
|
||||
ngx_msec_t pkt_recv_time1;
|
||||
ngx_msec_t pkt_recv_time2;
|
||||
ngx_uint_t cum_latency;
|
||||
ngx_msec_t latency;
|
||||
unsigned active:1;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
ngx_msec_t latency_min; // Minimal detectable latency, msec
|
||||
ngx_msec_t latency_max; // Maximum latency assumed, msec
|
||||
ngx_msec_t latency_undef; // Default latency for modem, msec
|
||||
// or satellite connections or undetectable one
|
||||
ngx_msec_t test_time; // How long test bandwidth, msec
|
||||
|
||||
u_char *payload; // Payload data for all
|
||||
} ngx_rtmp_bandwidth_detection_app_conf_t;
|
||||
|
||||
|
||||
extern ngx_module_t ngx_rtmp_bandwidth_detection_module;
|
||||
|
||||
|
||||
#endif /* _NGX_RTMP_BANDWIDTH_DETECTION_H_INCLUDED_ */
|
139
ngx_rtmp_send.c
139
ngx_rtmp_send.c
|
@ -8,6 +8,7 @@
|
|||
#include "ngx_rtmp.h"
|
||||
#include "ngx_rtmp_amf.h"
|
||||
#include "ngx_rtmp_streams.h"
|
||||
#include "ngx_rtmp_transitions.h"
|
||||
|
||||
|
||||
#define NGX_RTMP_USER_START(s, tp) \
|
||||
|
@ -985,3 +986,141 @@ ngx_rtmp_send_sample_access(ngx_rtmp_session_t *s)
|
|||
return ngx_rtmp_send_shared_packet(s,
|
||||
ngx_rtmp_create_sample_access(s));
|
||||
}
|
||||
|
||||
/* -------------------------------------- For bandwidth detection ---------------------------------- */
|
||||
|
||||
ngx_chain_t *
|
||||
ngx_rtmp_create_bwcheck(ngx_rtmp_session_t *s, u_char *payload)
|
||||
{
|
||||
ngx_rtmp_header_t h;
|
||||
|
||||
static double trans;
|
||||
|
||||
static ngx_rtmp_amf_elt_t out_inf[] = {
|
||||
|
||||
{ NGX_RTMP_AMF_STRING,
|
||||
ngx_null_string,
|
||||
NULL, 0 },
|
||||
|
||||
};
|
||||
|
||||
static ngx_rtmp_amf_elt_t out_elts[] = {
|
||||
|
||||
{ NGX_RTMP_AMF_STRING,
|
||||
ngx_null_string,
|
||||
"onBWCheck", 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_null_string,
|
||||
&trans, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NULL,
|
||||
ngx_null_string,
|
||||
NULL, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_OBJECT,
|
||||
ngx_null_string,
|
||||
out_inf,
|
||||
sizeof(out_inf) },
|
||||
};
|
||||
|
||||
out_inf[0].data = payload;
|
||||
trans = NGX_RTMP_BANDWIDTH_DETECTION_BWCHECK_TRANS;
|
||||
|
||||
memset(&h, 0, sizeof(h));
|
||||
|
||||
h.type = NGX_RTMP_MSG_AMF_CMD;
|
||||
h.csid = NGX_RTMP_CSID_AMF;
|
||||
h.msid = NGX_RTMP_MSID;
|
||||
|
||||
return ngx_rtmp_create_amf(s, &h, out_elts,
|
||||
sizeof(out_elts) / sizeof(out_elts[0]));
|
||||
}
|
||||
|
||||
ngx_int_t
|
||||
ngx_rtmp_send_bwcheck(ngx_rtmp_session_t *s, u_char *payload)
|
||||
{
|
||||
return ngx_rtmp_send_shared_packet(s,
|
||||
ngx_rtmp_create_bwcheck(s, payload));
|
||||
}
|
||||
|
||||
|
||||
ngx_chain_t *
|
||||
ngx_rtmp_create_bwdone(ngx_rtmp_session_t *s,
|
||||
double kbitDown, ngx_uint_t deltaDown, double deltaTime, ngx_msec_t latency)
|
||||
{
|
||||
ngx_rtmp_header_t h;
|
||||
|
||||
static double trans;
|
||||
static struct {
|
||||
double kbitDown;
|
||||
ngx_uint_t deltaDown;
|
||||
double deltaTime;
|
||||
ngx_msec_t latency;
|
||||
} v;
|
||||
|
||||
static ngx_rtmp_amf_elt_t out_inf[] = {
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_null_string,
|
||||
&v.kbitDown, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_null_string,
|
||||
&v.deltaDown, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_null_string,
|
||||
&v.deltaTime, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_null_string,
|
||||
&v.latency, 0 },
|
||||
|
||||
};
|
||||
|
||||
static ngx_rtmp_amf_elt_t out_elts[] = {
|
||||
|
||||
{ NGX_RTMP_AMF_STRING,
|
||||
ngx_null_string,
|
||||
"onBWDone", 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NUMBER,
|
||||
ngx_null_string,
|
||||
&trans, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_NULL,
|
||||
ngx_null_string,
|
||||
NULL, 0 },
|
||||
|
||||
{ NGX_RTMP_AMF_OBJECT,
|
||||
ngx_null_string,
|
||||
out_inf,
|
||||
sizeof(out_inf) },
|
||||
};
|
||||
|
||||
ngx_memzero(&v, sizeof(v));
|
||||
v.kbitDown = kbitDown;
|
||||
v.deltaDown = deltaDown;
|
||||
v.deltaTime = deltaTime;
|
||||
v.latency = latency;
|
||||
trans = NGX_RTMP_BANDWIDTH_DETECTION_BWDONE_TRANS;
|
||||
|
||||
memset(&h, 0, sizeof(h));
|
||||
|
||||
h.type = NGX_RTMP_MSG_AMF_CMD;
|
||||
h.csid = NGX_RTMP_CSID_AMF;
|
||||
h.msid = NGX_RTMP_MSID;
|
||||
|
||||
return ngx_rtmp_create_amf(s, &h, out_elts,
|
||||
sizeof(out_elts) / sizeof(out_elts[0]));
|
||||
}
|
||||
|
||||
|
||||
ngx_int_t
|
||||
ngx_rtmp_send_bwdone(ngx_rtmp_session_t *s,
|
||||
double kbitDown, ngx_uint_t deltaDown, double deltaTime, ngx_msec_t latency)
|
||||
{
|
||||
return ngx_rtmp_send_shared_packet(s,
|
||||
ngx_rtmp_create_bwdone(s, kbitDown, deltaDown, deltaTime, latency));
|
||||
}
|
||||
|
|
15
ngx_rtmp_transitions.h
Normal file
15
ngx_rtmp_transitions.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
|
||||
/*
|
||||
* Copyright (C) Sergey Dryabzhinsky, 2016
|
||||
*
|
||||
* Based on http://permalink.gmane.org/gmane.comp.web.flash.red5/5869
|
||||
* And live & stat modules
|
||||
*/
|
||||
|
||||
#ifndef _NGX_RTMP_TRANSITIONS_H_INCLUDED_
|
||||
#define _NGX_RTMP_TRANSITIONS_H_INCLUDED_
|
||||
|
||||
#define NGX_RTMP_BANDWIDTH_DETECTION_BWCHECK_TRANS 3
|
||||
#define NGX_RTMP_BANDWIDTH_DETECTION_BWDONE_TRANS 4
|
||||
|
||||
#endif // _NGX_RTMP_TRANSITIONS_H_INCLUDED_
|
Loading…
Reference in a new issue