Skip to content

Commit

Permalink
add live stage and stage time
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWoo committed Mar 6, 2019
1 parent 2d13318 commit 30d9565
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 3 deletions.
3 changes: 3 additions & 0 deletions http/ngx_http_flv_live_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,9 @@ ngx_http_flv_live_handler(ngx_http_request_t *r)

s->prepare_handler = ngx_http_flv_live_prepare_out_chain;

s->stage = NGX_LIVE_PLAY;
s->ptime = ngx_current_msec;

if (ngx_rtmp_play_filter(s, &v) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
Expand Down
6 changes: 6 additions & 0 deletions ngx_live_relay_httpflv.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ ngx_live_relay_httpflv_recv(void *request, ngx_http_request_t *hcr)
s = request;
status_code = ngx_http_client_status_code(hcr);

s->stage = NGX_LIVE_PLAY;
s->ptime = ngx_current_msec;

if (status_code != NGX_HTTP_OK) {
ngx_live_relay_httpflv_error(s, status_code);
ngx_http_client_finalize_request(hcr, 1);
Expand Down Expand Up @@ -534,6 +537,9 @@ ngx_live_relay_httpflv_send_request(ngx_rtmp_session_t *s,
{ ngx_null_string, ngx_null_string }
};

s->stage = NGX_LIVE_CONNECT;
s->connect_time = ngx_current_msec;

hcr = ngx_http_client_get(s->log, &request_url, headers, s);
ngx_http_client_set_read_handler(hcr, ngx_live_relay_httpflv_recv);

Expand Down
12 changes: 12 additions & 0 deletions ngx_live_relay_rtmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ ngx_live_relay_rtmp_send_connect(ngx_rtmp_session_t *s)
h.csid = NGX_RTMP_RELAY_CSID_AMF_INI;
h.type = NGX_RTMP_MSG_AMF_CMD;

s->status = NGX_LIVE_CONNECT;
s->connect_time = ngx_current_msec;

return ngx_rtmp_send_chunk_size(s, cscf->chunk_size) != NGX_OK
|| ngx_rtmp_send_ack_size(s, cscf->ack_window) != NGX_OK
|| ngx_rtmp_send_amf(s, &h, out_elts,
Expand Down Expand Up @@ -270,6 +273,9 @@ ngx_live_relay_rtmp_send_create_stream(ngx_rtmp_session_t *s)
h.csid = NGX_RTMP_RELAY_CSID_AMF_INI;
h.type = NGX_RTMP_MSG_AMF_CMD;

s->stage = NGX_LIVE_CREATE_STREAM;
s->create_stream_time = ngx_current_msec;

return ngx_rtmp_send_amf(s, &h, out_elts,
sizeof(out_elts) / sizeof(out_elts[0]));
}
Expand Down Expand Up @@ -333,6 +339,9 @@ ngx_live_relay_rtmp_send_publish(ngx_rtmp_session_t *s)
h.msid = NGX_RTMP_RELAY_MSID;
h.type = NGX_RTMP_MSG_AMF_CMD;

s->stage = NGX_LIVE_PUBLISH;
s->ptime = ngx_current_msec;

return ngx_rtmp_send_amf(s, &h, out_elts,
sizeof(out_elts) / sizeof(out_elts[0]));
}
Expand Down Expand Up @@ -406,6 +415,9 @@ ngx_live_relay_rtmp_send_play(ngx_rtmp_session_t *s)
h.msid = NGX_RTMP_RELAY_MSID;
h.type = NGX_RTMP_MSG_AMF_CMD;

s->stage = NGX_LIVE_PLAY;
s->ptime = ngx_current_msec;

return ngx_rtmp_send_amf(s, &h, out_elts,
sizeof(out_elts) / sizeof(out_elts[0])) != NGX_OK
|| ngx_rtmp_send_set_buflen(s, NGX_RTMP_RELAY_MSID,
Expand Down
24 changes: 24 additions & 0 deletions ngx_rtmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ typedef struct ngx_live_stream_s ngx_live_stream_t;
typedef struct ngx_live_server_s ngx_live_server_t;
typedef struct ngx_rtmp_addr_conf_s ngx_rtmp_addr_conf_t;

#define NGX_LIVE_INIT 0
#define NGX_LIVE_HANDSHAKE_DONE 1
#define NGX_LIVE_CONNECT 2
#define NGX_LIVE_CREATE_STREAM 3
#define NGX_LIVE_PUBLISH 4
#define NGX_LIVE_PLAY 5
#define NGX_LIVE_AV 6
#define NGX_LIVE_CLOSE 7

extern char *ngx_live_stage[];

struct ngx_rtmp_session_s {
uint32_t signature; /* "RTMP" */ /* <-- FIXME wtf */

Expand Down Expand Up @@ -289,6 +300,19 @@ struct ngx_rtmp_session_s {
ngx_uint_t nframe;
ngx_rtmp_prepared_pt prepare_handler;

/* for trace and statistics */
ngx_uint_t stage;
ngx_msec_t init_time;
ngx_msec_t handshake_done_time;
ngx_msec_t connect_time;
ngx_msec_t create_stream_time;
ngx_msec_t ptime; /* publish or play time */
ngx_msec_t first_data; /* audio video or metadata*/
ngx_msec_t first_metadata;
ngx_msec_t first_audio;
ngx_msec_t first_video;
ngx_msec_t close_stream_time;

/* circular buffer of RTMP message pointers */
ngx_msec_t timeout;
uint32_t out_bytes;
Expand Down
18 changes: 16 additions & 2 deletions ngx_rtmp_cmd_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ ngx_rtmp_play_filter(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
}

ngx_int_t
ngx_rtmp_close_stream_filter(ngx_rtmp_session_t *s,
ngx_rtmp_close_stream_t *v)
ngx_rtmp_close_stream_filter(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
{
if (s->closed) {
ngx_log_error(NGX_LOG_INFO, s->log, 0, "session has been closed");
Expand Down Expand Up @@ -415,6 +414,9 @@ ngx_rtmp_cmd_connect_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,

s->live_server = ngx_live_create_server(&s->serverid);

s->stage = NGX_LIVE_CONNECT;
s->connect_time = ngx_current_msec;

return ngx_rtmp_connect(s, &v);
}

Expand Down Expand Up @@ -551,6 +553,9 @@ ngx_rtmp_cmd_create_stream_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,

ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->log, 0, "createStream");

s->stage = NGX_LIVE_CREATE_STREAM;
s->create_stream_time = ngx_current_msec;

return ngx_rtmp_create_stream(s, &v);
}

Expand Down Expand Up @@ -710,6 +715,9 @@ ngx_rtmp_cmd_publish_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
"publish: name='%s' args='%s' type=%s silent=%d",
v.name, v.args, v.type, v.silent);

s->stage = NGX_LIVE_PUBLISH;
s->ptime = ngx_current_msec;

return ngx_rtmp_publish_filter(s, &v);
}

Expand Down Expand Up @@ -779,6 +787,9 @@ ngx_rtmp_cmd_play_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
(ngx_int_t) v.duration, (ngx_int_t) v.reset,
(ngx_int_t) v.silent);

s->stage = NGX_LIVE_PLAY;
s->ptime = ngx_current_msec;

return ngx_rtmp_play_filter(s, &v);
}

Expand Down Expand Up @@ -913,6 +924,9 @@ static ngx_int_t
ngx_rtmp_cmd_disconnect_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
s->stage = NGX_LIVE_CLOSE;
s->close_stream_time = ngx_current_msec;

ngx_log_error(NGX_LOG_INFO, s->log, 0, "disconnect");

return ngx_rtmp_disconnect(s);
Expand Down
3 changes: 3 additions & 0 deletions ngx_rtmp_handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ ngx_rtmp_handshake_done(ngx_rtmp_session_t *s)
return;
}

s->stage = NGX_LIVE_HANDSHAKE_DONE;
s->handshake_done_time = ngx_current_msec;

ngx_rtmp_cycle(s);
}

Expand Down
18 changes: 17 additions & 1 deletion ngx_rtmp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ typedef struct {
} ngx_rtmp_error_log_ctx_t;


char *ngx_live_stage[] = {
"init",
"handshake_done",
"connect",
"create_stream",
"publish",
"play",
"audio_video",
"close_stream",
};


void
ngx_rtmp_init_connection(ngx_connection_t *c)
{
Expand Down Expand Up @@ -185,7 +197,8 @@ ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len)
return p;
}

p = ngx_snprintf(buf, len, ", server: %V, session: %p", s->addr_text, s);
p = ngx_snprintf(buf, len, ", server: %V, session: %p, stage: %s",
s->addr_text, s, ngx_live_stage[s->stage]);
len -= p - buf;
buf = p;

Expand Down Expand Up @@ -645,6 +658,9 @@ ngx_rtmp_create_session(ngx_rtmp_addr_conf_t *addr_conf)
s->log->handler = ngx_rtmp_log_error;
s->log->data = ctx;

s->stage = NGX_LIVE_INIT;
s->init_time = ngx_current_msec;

return s;

destroy:
Expand Down
58 changes: 58 additions & 0 deletions ngx_rtmp_monitor_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ ngx_rtmp_monitor_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
return NGX_OK;
}

static ngx_int_t
ngx_rtmp_monitor_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
if (s->first_metadata == 0) {
s->stage = NGX_LIVE_AV;
s->first_metadata = ngx_current_msec;
s->first_data = s->first_data == 0? ngx_current_msec: s->first_data;
}

ngx_log_error(NGX_LOG_INFO, s->log, 0, "receive metadata");

return NGX_OK;
}

static ngx_int_t
ngx_rtmp_monitor_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
{
Expand All @@ -229,6 +244,34 @@ ngx_rtmp_monitor_frame(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_rtmp_monitor_app_conf_t *macf;
ngx_rtmp_monitor_ctx_t *ctx;

if (s->first_audio == 0 && h->type == NGX_RTMP_MSG_AUDIO) {
s->stage = NGX_LIVE_AV;
s->first_audio = ngx_current_msec;
s->first_data = s->first_data == 0? ngx_current_msec: s->first_data;
}

if (s->first_video == 0 && h->type == NGX_RTMP_MSG_VIDEO) {
s->stage = NGX_LIVE_AV;
s->first_video = ngx_current_msec;
s->first_data = s->first_data == 0? ngx_current_msec: s->first_data;
}

if (h->type == NGX_RTMP_MSG_AUDIO && is_header) {
if (s->publishing) {
ngx_log_error(NGX_LOG_INFO, s->log, 0, "receive audio header");
} else {
ngx_log_error(NGX_LOG_INFO, s->log, 0, "send audio header");
}
}

if (h->type == NGX_RTMP_MSG_VIDEO && is_header) {
if (s->publishing) {
ngx_log_error(NGX_LOG_INFO, s->log, 0, "receive video header");
} else {
ngx_log_error(NGX_LOG_INFO, s->log, 0, "send video header");
}
}

if (h->type != NGX_RTMP_MSG_VIDEO) {
return;
}
Expand Down Expand Up @@ -354,6 +397,7 @@ ngx_rtmp_monitor_postconfiguration(ngx_conf_t *cf)
{
ngx_rtmp_core_main_conf_t *cmcf;
ngx_rtmp_handler_pt *h;
ngx_rtmp_amf_handler_t *ch;

cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);

Expand All @@ -363,6 +407,20 @@ ngx_rtmp_monitor_postconfiguration(ngx_conf_t *cf)
h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_VIDEO]);
*h = ngx_rtmp_monitor_av;

/* register metadata handler */
ch = ngx_array_push(&cmcf->amf);
if (ch == NULL) {
return NGX_ERROR;
}
ngx_str_set(&ch->name, "@setDataFrame");
ch->handler = ngx_rtmp_monitor_meta_data;

ch = ngx_array_push(&cmcf->amf);
if (ch == NULL) {
return NGX_ERROR;
}
ngx_str_set(&ch->name, "onMetaData");
ch->handler = ngx_rtmp_monitor_meta_data;

next_close_stream = ngx_rtmp_close_stream;
ngx_rtmp_close_stream = ngx_rtmp_monitor_close_stream;
Expand Down

0 comments on commit 30d9565

Please sign in to comment.