From a344ee66070f4bd2bf682615e2ee9453c044e3a3 Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Fri, 18 Sep 2015 23:24:45 +0300 Subject: [PATCH] Append support of Fi command. Return system date and time. --- ngx_rtmp.h | 1 + ngx_rtmp_live_module.c | 25 +++++++----- ngx_rtmp_send.c | 87 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 10 deletions(-) diff --git a/ngx_rtmp.h b/ngx_rtmp.h index 06e728b..a49193d 100644 --- a/ngx_rtmp.h +++ b/ngx_rtmp.h @@ -588,6 +588,7 @@ ngx_int_t ngx_rtmp_send_redirect_status(ngx_rtmp_session_t *s, 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, char *desc); ngx_int_t ngx_rtmp_send_fcunpublish(ngx_rtmp_session_t *s, char *desc); +ngx_int_t ngx_rtmp_send_fi(ngx_rtmp_session_t *s); /* Frame types */ diff --git a/ngx_rtmp_live_module.c b/ngx_rtmp_live_module.c index 45d6aab..fbe9480 100644 --- a/ngx_rtmp_live_module.c +++ b/ngx_rtmp_live_module.c @@ -1183,15 +1183,22 @@ static ngx_int_t ngx_rtmp_live_on_fi(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in) { - static ngx_rtmp_amf_elt_t out_elts[] = { + ngx_rtmp_live_app_conf_t *lacf; - { NGX_RTMP_AMF_STRING, - ngx_null_string, - "onFi", 0 } - }; + lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module); + if (lacf == NULL) { + ngx_log_error(NGX_LOG_DEBUG, s->connection->log, 0, + "live: Fi - no live config!"); + return NGX_ERROR; + } - return ngx_rtmp_live_data(s, h, in, out_elts, - sizeof(out_elts) / sizeof(out_elts[0])); + if (!lacf->live || in == NULL || in->buf == NULL) { + ngx_log_error(NGX_LOG_DEBUG, s->connection->log, 0, + "live: Fi - no live or no buffer!"); + return NGX_OK; + } + + return ngx_rtmp_send_fi(s); } static ngx_int_t @@ -1200,7 +1207,6 @@ ngx_rtmp_live_on_fcpublish(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, { ngx_rtmp_live_app_conf_t *lacf; - ngx_rtmp_live_ctx_t *ctx; static struct { double trans; @@ -1259,7 +1265,6 @@ ngx_rtmp_live_on_fcunpublish(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, { ngx_rtmp_live_app_conf_t *lacf; - ngx_rtmp_live_ctx_t *ctx; static struct { double trans; @@ -1434,7 +1439,7 @@ ngx_rtmp_live_postconfiguration(ngx_conf_t *cf) ch->handler = ngx_rtmp_live_on_cue_point; ch = ngx_array_push(&cmcf->amf); - ngx_str_set(&ch->name, "onFi"); + ngx_str_set(&ch->name, "Fi"); ch->handler = ngx_rtmp_live_on_fi; ch = ngx_array_push(&cmcf->amf); diff --git a/ngx_rtmp_send.c b/ngx_rtmp_send.c index 3f7e76b..a613c6d 100644 --- a/ngx_rtmp_send.c +++ b/ngx_rtmp_send.c @@ -3,6 +3,7 @@ * Copyright (C) Roman Arutyunyan */ +#include #include #include @@ -867,6 +868,92 @@ ngx_rtmp_send_fcunpublish(ngx_rtmp_session_t *s, char *desc) } +ngx_chain_t * +ngx_rtmp_create_fi(ngx_rtmp_session_t *s) +{ + ngx_rtmp_header_t h; + static double trans; + + struct tm tm; + struct timeval tv; + struct timezone tz; + int errfl; + + static u_char buf_time[NGX_TIME_T_LEN + 1]; + static u_char buf_date[NGX_TIME_T_LEN + 1]; + + + static ngx_rtmp_amf_elt_t out_inf[] = { + + { NGX_RTMP_AMF_STRING, + ngx_string("st"), + NULL, 0 }, + + { NGX_RTMP_AMF_STRING, + ngx_string("sd"), + NULL, 0 }, + }; + + static ngx_rtmp_amf_elt_t out_elts[] = { + + { NGX_RTMP_AMF_STRING, + ngx_null_string, + "onFi", 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) }, + }; + + trans = 0; + + errfl = gettimeofday(&tv, &tz); + + if (errfl) { + ngx_log_error(NGX_LOG_DEBUG, s->connection->log, 0, + "create: fi - can't get time!"); + return NULL; + } + + ngx_libc_localtime((time_t)tv.tv_sec, &tm); + + memset(buf_time, 0, sizeof(buf_time)); + memset(buf_date, 0, sizeof(buf_date)); + + errfl = sprintf(buf_time, "%02d:%02d:%02d.%d", tm.tm_hour, tm.tm_min, tm.tm_sec, (int)tv.tv_usec); + errfl = sprintf(buf_date, "%04d-%02d-%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); + + out_inf[0].data = buf_time; + out_inf[1].data = buf_date; + + 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_fi(ngx_rtmp_session_t *s) +{ + return ngx_rtmp_send_shared_packet(s, + ngx_rtmp_create_fi(s)); +} + + ngx_chain_t * ngx_rtmp_create_sample_access(ngx_rtmp_session_t *s) {