Merge pull request #43 from sergey-dryabzhinsky/live-on-fi-handler

Send system (server) date & time on Fi call
This commit is contained in:
Sergey 2015-09-19 03:04:23 +03:00
commit bacdd48aac
3 changed files with 171 additions and 8 deletions

View file

@ -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 */

View file

@ -1183,15 +1183,91 @@ 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_int_t res;
static struct {
u_char time[NGX_TIME_T_LEN + 1];
u_char date[NGX_TIME_T_LEN + 1];
} v;
static ngx_rtmp_amf_elt_t in_dt_elts[] = {
{ NGX_RTMP_AMF_STRING,
ngx_string("sd"),
&v.date, sizeof(v.date) },
{ NGX_RTMP_AMF_STRING,
ngx_string("st"),
&v.time, sizeof(v.time) },
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
"onFi", 0 }
};
return ngx_rtmp_live_data(s, h, in, out_elts,
sizeof(out_elts) / sizeof(out_elts[0]));
static ngx_rtmp_amf_elt_t in_elts[] = {
{ NGX_RTMP_AMF_MIXED_ARRAY,
ngx_null_string,
in_dt_elts, sizeof(in_dt_elts) },
};
static ngx_rtmp_amf_elt_t out_dt_elts[] = {
{ NGX_RTMP_AMF_STRING,
ngx_string("sd"),
NULL, 0 },
{ NGX_RTMP_AMF_STRING,
ngx_string("st"),
NULL, 0 },
};
static ngx_rtmp_amf_elt_t out_elts[] = {
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
"onFi", 0 },
{ NGX_RTMP_AMF_MIXED_ARRAY,
ngx_null_string,
out_dt_elts, sizeof(out_dt_elts) },
};
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;
}
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;
}
ngx_memzero(&v, sizeof(v));
res = ngx_rtmp_receive_amf(s, in, in_elts,
sizeof(in_elts) / sizeof(in_elts[0]));
if (res == NGX_OK) {
ngx_log_error(NGX_LOG_DEBUG, s->connection->log, 0,
"live: onFi: date='%s', time='%s'",
v.date, v.time);
out_dt_elts[0].data = v.date;
out_dt_elts[1].data = v.time;
// Pass through datetime from publisher
return ngx_rtmp_live_data(s, h, in, out_elts,
sizeof(out_elts) / sizeof(out_elts[0]));
} else {
// Send our server datetime
return ngx_rtmp_send_fi(s);
}
}
static ngx_int_t
@ -1200,7 +1276,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 +1334,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;

View file

@ -3,6 +3,7 @@
* Copyright (C) Roman Arutyunyan
*/
#include <sys/time.h>
#include <ngx_config.h>
#include <ngx_core.h>
@ -867,6 +868,93 @@ 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);
ngx_memzero(buf_time, sizeof(buf_time));
ngx_memzero(buf_date, sizeof(buf_date));
errfl = sprintf(buf_time, "%02d:%02d:%02d.%d", tm.tm_hour, tm.tm_min, tm.tm_sec, (int)tv.tv_usec);
// Strange order, but FMLE send like this
errfl = sprintf(buf_date, "%02d-%02d-%04d", tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
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)
{