static ngx_int_t ngx_rtmp_notify_disconnect(ngx_rtmp_session_t *s) { ngx_rtmp_notify_srv_conf_t *nscf; ngx_rtmp_netcall_init_t ci; ngx_url_t *url; if (s->auto_pushed || s->relay) { goto next; } nscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_notify_module); url = nscf->url[NGX_RTMP_NOTIFY_DISCONNECT]; if (url == NULL) { goto next; } ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, "notify: disconnect '%V'", &url->url); ngx_memzero(&ci, sizeof(ci)); ci.url = url; ci.create = ngx_rtmp_notify_disconnect_create; ngx_rtmp_netcall_create(s, &ci); next: return next_disconnect(s); }
ngx_chain_t * ngx_rtmp_create_amf(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_rtmp_amf_elt_t *elts, size_t nelts) { ngx_chain_t *first; ngx_int_t rc; ngx_rtmp_core_srv_conf_t *cscf; ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "create: amf nelts=%ui", nelts); cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); first = NULL; rc = ngx_rtmp_append_amf(s, &first, NULL, elts, nelts); if (rc != NGX_OK && first) { ngx_rtmp_free_shared_chain(cscf, first); first = NULL; } if (first) { ngx_rtmp_prepare_message(s, h, NULL, first); } return first; }
static ngx_int_t ngx_rtmp_codec_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in) { ngx_rtmp_codec_ctx_t *ctx; ngx_rtmp_core_srv_conf_t *cscf; ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module); if (ctx == NULL) { return NGX_OK; } cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); if (ctx->avc_header) { ngx_rtmp_free_shared_chain(cscf, ctx->avc_header); ctx->avc_header = NULL; } if (ctx->aac_header) { ngx_rtmp_free_shared_chain(cscf, ctx->aac_header); ctx->aac_header = NULL; } if (ctx->meta) { ngx_rtmp_free_shared_chain(cscf, ctx->meta); ctx->meta = NULL; } return NGX_OK; }
static ngx_buf_t * ngx_rtmp_alloc_handshake_buffer(ngx_rtmp_session_t *s) { ngx_rtmp_core_srv_conf_t *cscf; ngx_chain_t *cl; ngx_buf_t *b; ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "handshake: allocating buffer"); cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); if (cscf->free_hs) { cl = cscf->free_hs; b = cl->buf; cscf->free_hs = cl->next; ngx_free_chain(cscf->pool, cl); } else { b = ngx_pcalloc(cscf->pool, sizeof(ngx_buf_t)); if (b == NULL) { return NULL; } b->memory = 1; b->start = ngx_pcalloc(cscf->pool, NGX_RTMP_HANDSHAKE_BUFSIZE); if (b->start == NULL) { return NULL; } b->end = b->start + NGX_RTMP_HANDSHAKE_BUFSIZE; } b->pos = b->last = b->start; return b; }
ngx_int_t ngx_rtmp_send_amf(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_rtmp_amf_elt_t *elts, size_t nelts) { ngx_chain_t *first; ngx_int_t rc; ngx_rtmp_core_srv_conf_t *cscf; cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); first = NULL; rc = ngx_rtmp_append_amf(s, &first, NULL, elts, nelts); if (rc != NGX_OK || first == NULL) { goto done; } ngx_rtmp_prepare_message(s, h, NULL, first); rc = ngx_rtmp_send_message(s, first, 0); done: ngx_rtmp_free_shared_chain(cscf, first); return rc; }
static ngx_chain_t * ngx_rtmp_notify_disconnect_create(ngx_rtmp_session_t *s, void *arg, ngx_pool_t *pool) { ngx_rtmp_notify_srv_conf_t *nscf; ngx_url_t *url; ngx_chain_t *al, *bl, *pl; ngx_buf_t *b; nscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_notify_module); pl = ngx_alloc_chain_link(pool); if (pl == NULL) { return NULL; } b = ngx_create_temp_buf(pool, sizeof("&call=disconnect") + sizeof("&app=") + s->app.len * 3 + 1 + s->args.len); if (b == NULL) { return NULL; } pl->buf = b; pl->next = NULL; b->last = ngx_cpymem(b->last, (u_char*) "&call=disconnect", sizeof("&call=disconnect") - 1); b->last = ngx_cpymem(b->last, (u_char*) "&app=", sizeof("&app=") - 1); b->last = (u_char*) ngx_escape_uri(b->last, s->app.data, s->app.len, NGX_ESCAPE_ARGS); if (s->args.len) { *b->last++ = '&'; b->last = (u_char *) ngx_cpymem(b->last, s->args.data, s->args.len); } url = nscf->url[NGX_RTMP_NOTIFY_DISCONNECT]; al = ngx_rtmp_netcall_http_format_session(s, pool); if (al == NULL) { return NULL; } al->next = pl; bl = NULL; if (nscf->method == NGX_RTMP_NETCALL_HTTP_POST) { bl = al; al = NULL; } return ngx_rtmp_netcall_http_format_request(nscf->method, &url->host, &url->uri, al, bl, pool, &ngx_rtmp_notify_urlencoded); }
static ngx_int_t ngx_rtmp_send_shared_packet(ngx_rtmp_session_t *s, ngx_chain_t *cl) { ngx_rtmp_core_srv_conf_t *cscf; ngx_int_t rc; if (cl == NULL) { return NGX_ERROR; } cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); rc = ngx_rtmp_send_message(s, cl, 0); ngx_rtmp_free_shared_chain(cscf, cl); return rc; }
void ngx_rtmp_ssl_handshake(ngx_rtmp_session_t *s) { ngx_connection_t *c; ngx_rtmp_ssl_srv_conf_t *sscf; c = s->connection; sscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_ssl_module); if (ngx_ssl_create_connection(&sscf->ssl, c, 0) != NGX_OK) { ngx_rtmp_finalize_session(s); return; } ngx_rtmp_ssl_handshake_handler(c); return; }
void ngx_rtmp_free_handshake_buffers(ngx_rtmp_session_t *s) { ngx_rtmp_core_srv_conf_t *cscf; ngx_chain_t *cl; if (s->hs_buf == NULL) { return; } cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); cl = ngx_alloc_chain_link(cscf->pool); if (cl == NULL) { return; } cl->buf = s->hs_buf; cl->next = cscf->free_hs; cscf->free_hs = cl; s->hs_buf = NULL; }
static ngx_int_t ngx_rtmp_codec_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in) { ngx_rtmp_codec_ctx_t *ctx; ngx_rtmp_core_srv_conf_t *cscf; ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module); if (ctx == NULL) { return NGX_OK; } cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); if (ctx->avc_header) { ngx_rtmp_free_shared_chain(cscf, ctx->avc_header); ctx->avc_header = NULL; } if (ctx->aac_header) { ngx_rtmp_free_shared_chain(cscf, ctx->aac_header); ctx->aac_header = NULL; } if (ctx->store_key_size) { ngx_uint_t i; for (i = 0; i < ctx->store_key_size; i++) { if (ctx->storeframes[i]) { ngx_rtmp_free_shared_chain(cscf, ctx->storeframes[i]); ctx->storeframes[i] = NULL; } } } if (ctx->meta) { ngx_rtmp_free_shared_chain(cscf, ctx->meta); ctx->meta = NULL; } return NGX_OK; }
/* 启动发布或播放 */ static void ngx_rtmp_live_start(ngx_rtmp_session_t *s) { ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_live_app_conf_t *lacf; ngx_chain_t *control; ngx_chain_t *status[3]; size_t n, nstatus; cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module); /* 控制报文 */ control = ngx_rtmp_create_stream_begin(s, NGX_RTMP_MSID); nstatus = 0; /* status报文个数 */ if (lacf->play_restart) { status[nstatus++] = ngx_rtmp_create_status(s, "NetStream.Play.Start", "status", "Start live"); status[nstatus++] = ngx_rtmp_create_sample_access(s); } if (lacf->publish_notify) { status[nstatus++] = ngx_rtmp_create_status(s, "NetStream.Play.PublishNotify", "status", "Start publishing"); } ngx_rtmp_live_set_status(s, control, status, nstatus, 1); if (control) { ngx_rtmp_free_shared_chain(cscf, control); } for (n = 0; n < nstatus; ++n) { ngx_rtmp_free_shared_chain(cscf, status[n]); } }
static void ngx_rtmp_close_session_handler(ngx_event_t *e) { ngx_rtmp_session_t *s; ngx_connection_t *c; ngx_rtmp_core_srv_conf_t *cscf; s = e->data; c = s->connection; cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "close session"); if (s) { ngx_rtmp_fire_event(s, NGX_RTMP_DISCONNECT, NULL, NULL); if (s->ping_evt.timer_set) { ngx_del_timer(&s->ping_evt); } if (s->in_old_pool) { ngx_destroy_pool(s->in_old_pool); } if (s->in_pool) { ngx_destroy_pool(s->in_pool); } ngx_rtmp_free_handshake_buffers(s); while (s->out_pos != s->out_last) { ngx_rtmp_free_shared_chain(cscf, s->out[s->out_pos++]); s->out_pos %= s->out_queue; } } ngx_rtmp_close_connection(c); }
/* 停止直播发布或播放 */ static void ngx_rtmp_live_stop(ngx_rtmp_session_t *s) { ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_live_app_conf_t *lacf; ngx_chain_t *control; ngx_chain_t *status[3]; size_t n, nstatus; cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module); control = ngx_rtmp_create_stream_eof(s, NGX_RTMP_MSID); nstatus = 0; if (lacf->play_restart) { status[nstatus++] = ngx_rtmp_create_status(s, "NetStream.Play.Stop", "status", "Stop live"); } if (lacf->publish_notify) { status[nstatus++] = ngx_rtmp_create_status(s, "NetStream.Play.UnpublishNotify", "status", "Stop publishing"); } ngx_rtmp_live_set_status(s, control, status, nstatus, 0); if (control) { ngx_rtmp_free_shared_chain(cscf, control); } for (n = 0; n < nstatus; ++n) { ngx_rtmp_free_shared_chain(cscf, status[n]); } }
static ngx_rtmp_gop_frame_t * ngx_rtmp_gop_cache_free_frame(ngx_rtmp_session_t *s, ngx_rtmp_gop_frame_t *frame) { ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_gop_cache_ctx_t *ctx; cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); if (cscf == NULL) { return NULL; } ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_gop_cache_module); if (ctx == NULL) { return NULL; } if (frame->frame) { ngx_rtmp_free_shared_chain(cscf, frame->frame); frame->frame = NULL; } if (frame->h.type == NGX_RTMP_MSG_VIDEO) { ctx->video_frame_in_all--; } else if (frame->h.type == NGX_RTMP_MSG_AUDIO) { ctx->audio_frame_in_all--; } ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "gop free frame: type='%s' video_frame_in_cache='%uD' " "audio_frame_in_cache='%uD'", frame->h.type == NGX_RTMP_MSG_VIDEO ? "video" : "audio", ctx->video_frame_in_all, ctx->audio_frame_in_all); return frame->next; }
/* NOTE: this function does not free shared bufs on error */ ngx_int_t ngx_rtmp_append_amf(ngx_rtmp_session_t *s, ngx_chain_t **first, ngx_chain_t **last, ngx_rtmp_amf_elt_t *elts, size_t nelts) { ngx_rtmp_amf_ctx_t act; ngx_rtmp_core_srv_conf_t *cscf; ngx_int_t rc; cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); memset(&act, 0, sizeof(act)); act.arg = cscf; act.alloc = ngx_rtmp_alloc_amf_buf; act.log = s->connection->log; if (first) { act.first = *first; } if (last) { act.link = *last; } rc = ngx_rtmp_amf_write(&act, elts, nelts); if (first) { *first = act.first; } if (last) { *last = act.link; } return rc; }
static ngx_int_t ngx_rtmp_record_node_open(ngx_rtmp_session_t *s, ngx_rtmp_record_rec_ctx_t *rctx) { ngx_rtmp_record_app_conf_t *rracf; ngx_rtmp_core_srv_conf_t *cscf; ngx_err_t err; ngx_str_t path; ngx_int_t mode, create_mode; u_char buf[8], *p; off_t file_size; uint32_t tag_size, mlen, timestamp; rracf = rctx->conf; tag_size = 0; if (rctx->file.fd != NGX_INVALID_FILE) { return NGX_AGAIN; } cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "record: %V opening", &rracf->id); ngx_memzero(rctx, sizeof(*rctx)); rctx->conf = rracf; rctx->last = *ngx_cached_time; rctx->timestamp = ngx_cached_time->sec; ngx_rtmp_record_make_path(s, rctx, &path); mode = rracf->append ? NGX_FILE_RDWR : NGX_FILE_WRONLY; create_mode = rracf->append ? NGX_FILE_CREATE_OR_OPEN : NGX_FILE_TRUNCATE; ngx_memzero(&rctx->file, sizeof(rctx->file)); rctx->file.offset = 0; rctx->file.log = s->connection->log; rctx->file.fd = ngx_open_file(path.data, mode, create_mode, cscf->file_access); ngx_str_set(&rctx->file.name, "recorded"); if (rctx->file.fd == NGX_INVALID_FILE) { err = ngx_errno; if (err != NGX_ENOENT) { ngx_log_error(NGX_LOG_CRIT, s->connection->log, err, "record: %V failed to open file '%V'", &rracf->id, &path); } ngx_rtmp_record_notify_error(s, rctx); return NGX_OK; } #if !(NGX_WIN32) if (rracf->lock_file) { err = ngx_lock_fd(rctx->file.fd); if (err) { ngx_log_error(NGX_LOG_CRIT, s->connection->log, err, "record: %V lock failed", &rracf->id); } } #endif ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "record: %V opened '%V'", &rracf->id, &path); if (rracf->notify) { ngx_rtmp_send_status(s, "NetStream.Record.Start", "status", rracf->id.data ? (char *) rracf->id.data : ""); } if (rracf->append) { file_size = 0; timestamp = 0; #if (NGX_WIN32) { LONG lo, hi; lo = 0; hi = 0; lo = SetFilePointer(rctx->file.fd, lo, &hi, FILE_END); file_size = (lo == INVALID_SET_FILE_POINTER ? (off_t) -1 : (off_t) hi << 32 | (off_t) lo); } #else file_size = lseek(rctx->file.fd, 0, SEEK_END); #endif if (file_size == (off_t) -1) { ngx_log_error(NGX_LOG_CRIT, s->connection->log, ngx_errno, "record: %V seek failed", &rracf->id); goto done; } if (file_size < 4) { goto done; } if (ngx_read_file(&rctx->file, buf, 4, file_size - 4) != 4) { ngx_log_error(NGX_LOG_CRIT, s->connection->log, ngx_errno, "record: %V tag size read failed", &rracf->id); goto done; } p = (u_char *) &tag_size; p[0] = buf[3]; p[1] = buf[2]; p[2] = buf[1]; p[3] = buf[0]; if (tag_size == 0 || tag_size + 4 > file_size) { file_size = 0; goto done; } if (ngx_read_file(&rctx->file, buf, 8, file_size - tag_size - 4) != 8) { ngx_log_error(NGX_LOG_CRIT, s->connection->log, ngx_errno, "record: %V tag read failed", &rracf->id); goto done; } p = (u_char *) &mlen; p[0] = buf[3]; p[1] = buf[2]; p[2] = buf[1]; p[3] = 0; if (tag_size != mlen + 11) { ngx_log_error(NGX_LOG_CRIT, s->connection->log, ngx_errno, "record: %V tag size mismatch: " "tag_size=%uD, mlen=%uD", &rracf->id, tag_size, mlen); goto done; } p = (u_char *) ×tamp; p[3] = buf[7]; p[0] = buf[6]; p[1] = buf[5]; p[2] = buf[4]; done: rctx->file.offset = file_size; rctx->time_shift = timestamp; ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "record: append offset=%O, time=%uD, tag_size=%uD", file_size, timestamp, tag_size); } return NGX_OK; }
static ngx_int_t ngx_rtmp_relay_send_connect(ngx_rtmp_session_t *s) { static double trans = NGX_RTMP_RELAY_CONNECT_TRANS; static double acodecs = 3575; static double vcodecs = 252; static ngx_rtmp_amf_elt_t out_cmd[] = { { NGX_RTMP_AMF_STRING, ngx_string("app"), NULL, 0 }, /* <-- fill */ { NGX_RTMP_AMF_STRING, ngx_string("tcUrl"), NULL, 0 }, /* <-- fill */ { NGX_RTMP_AMF_STRING, ngx_string("pageUrl"), NULL, 0 }, /* <-- fill */ { NGX_RTMP_AMF_STRING, ngx_string("swfUrl"), NULL, 0 }, /* <-- fill */ { NGX_RTMP_AMF_STRING, ngx_string("flashVer"), NULL, 0 }, /* <-- fill */ { NGX_RTMP_AMF_NUMBER, ngx_string("audioCodecs"), &acodecs, 0 }, { NGX_RTMP_AMF_NUMBER, ngx_string("videoCodecs"), &vcodecs, 0 } }; static ngx_rtmp_amf_elt_t out_elts[] = { { NGX_RTMP_AMF_STRING, ngx_null_string, "connect", 0 }, { NGX_RTMP_AMF_NUMBER, ngx_null_string, &trans, 0 }, { NGX_RTMP_AMF_OBJECT, ngx_null_string, out_cmd, sizeof(out_cmd) } }; ngx_rtmp_core_app_conf_t *cacf; ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_relay_ctx_t *ctx; ngx_rtmp_header_t h; size_t len; u_char *p; cacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_core_module); cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_relay_module); if (cacf == NULL || ctx == NULL) { return NGX_ERROR; } /* app */ if (ctx->app.len) { out_cmd[0].data = ctx->app.data; out_cmd[0].len = ctx->app.len; } else { out_cmd[0].data = cacf->name.data; out_cmd[0].len = cacf->name.len; } /* tcUrl */ if (ctx->tc_url.len) { out_cmd[1].data = ctx->tc_url.data; out_cmd[1].len = ctx->tc_url.len; } else { len = sizeof("rtmp://") - 1 + ctx->url.len + sizeof("/") - 1 + ctx->app.len; p = ngx_palloc(s->connection->pool, len); if (p == NULL) { return NGX_ERROR; } out_cmd[1].data = p; p = ngx_cpymem(p, "rtmp://", sizeof("rtmp://") - 1); p = ngx_cpymem(p, ctx->url.data, ctx->url.len); *p++ = '/'; p = ngx_cpymem(p, ctx->app.data, ctx->app.len); out_cmd[1].len = p - (u_char *)out_cmd[1].data; } /* pageUrl */ out_cmd[2].data = ctx->page_url.data; out_cmd[2].len = ctx->page_url.len; /* swfUrl */ out_cmd[3].data = ctx->swf_url.data; out_cmd[3].len = ctx->swf_url.len; /* flashVer */ if (ctx->flash_ver.len) { out_cmd[4].data = ctx->flash_ver.data; out_cmd[4].len = ctx->flash_ver.len; } else { out_cmd[4].data = NGX_RTMP_RELAY_FLASHVER; out_cmd[4].len = sizeof(NGX_RTMP_RELAY_FLASHVER) - 1; } ngx_memzero(&h, sizeof(h)); h.csid = NGX_RTMP_RELAY_CSID_AMF_INI; h.type = NGX_RTMP_MSG_AMF_CMD; 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, sizeof(out_elts) / sizeof(out_elts[0])) != NGX_OK ? NGX_ERROR : NGX_OK; }
static ngx_int_t ngx_rtmp_live_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in, ngx_rtmp_amf_elt_t *out_elts, ngx_uint_t out_elts_size) { ngx_rtmp_live_ctx_t *ctx, *pctx; ngx_chain_t *data, *rpkt; ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_live_app_conf_t *lacf; ngx_rtmp_session_t *ss; ngx_rtmp_header_t ch; ngx_int_t rc; ngx_uint_t prio; ngx_uint_t peers; uint32_t delta; ngx_rtmp_live_chunk_stream_t *cs; u_char *msg_type; msg_type = (u_char *)out_elts[0].data; lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module); if (lacf == NULL) { return NGX_ERROR; } if (!lacf->live || in == NULL || in->buf == NULL) { return NGX_OK; } ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module); if (ctx == NULL || ctx->stream == NULL) { return NGX_OK; } if (ctx->publishing == 0) { ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "live: %s from non-publisher", msg_type); return NGX_OK; } /* drop the data packet if the stream is not active */ if (!ctx->stream->active) { return NGX_OK; } ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "live: %s packet timestamp=%uD", msg_type, h->timestamp); cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); cs = &ctx->cs[2]; cs->active = 1; peers = 0; prio = 0; data = NULL; rc = ngx_rtmp_append_amf(s, &data, NULL, out_elts, out_elts_size); if (rc != NGX_OK) { if (data) { ngx_rtmp_free_shared_chain(cscf, data); } return NGX_ERROR; } ngx_memzero(&ch, sizeof(ch)); ch.timestamp = h->timestamp; ch.msid = NGX_RTMP_MSID; ch.csid = h->csid; ch.type = NGX_RTMP_MSG_AMF_META; delta = ch.timestamp - cs->timestamp; rpkt = ngx_rtmp_append_shared_bufs(cscf, data, in); ngx_rtmp_prepare_message(s, &ch, NULL, rpkt); for (pctx = ctx->stream->ctx; pctx; pctx = pctx->next) { if (pctx == ctx || pctx->paused) { continue; } ss = pctx->session; if (ngx_rtmp_send_message(ss, rpkt, prio) != NGX_OK) { ++pctx->ndropped; cs->dropped += delta; continue; } cs->timestamp += delta; ++peers; ss->current_time = cs->timestamp; } if (data) { ngx_rtmp_free_shared_chain(cscf, data); } if (rpkt) { ngx_rtmp_free_shared_chain(cscf, rpkt); } ngx_rtmp_update_bandwidth(&ctx->stream->bw_in, h->mlen); ngx_rtmp_update_bandwidth(&ctx->stream->bw_out, h->mlen * peers); ngx_rtmp_update_bandwidth(&ctx->stream->bw_in_data, h->mlen); return NGX_OK; }
static ngx_int_t ngx_live_relay_httpflv_parse(ngx_rtmp_session_t *s, ngx_buf_t *b) { u_char ch, *p, *pc; ngx_rtmp_stream_t *st; ngx_rtmp_header_t *h; ngx_chain_t **ll; size_t len; ngx_rtmp_core_srv_conf_t *cscf; ngx_int_t rc = NGX_AGAIN; enum { flv_header_F = 0, flv_header_FL, flv_header_FLV, flv_header_Version, flv_header_Flags, flv_header_DataOffset0, flv_header_DataOffset1, flv_header_DataOffset2, flv_header_DataOffset3, flv_tagsize0, flv_tagsize1, flv_tagsize2, flv_tagsize3, flv_tagtype, flv_datasize0, flv_datasize1, flv_datasize2, flv_timestamp0, flv_timestamp1, flv_timestamp2, flv_timestamp_extended, flv_streamid0, flv_streamid1, flv_streamid2, flv_data } state; state = s->flv_state; cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); for (p = b->pos; p < b->last; ++p) { ch = *p; switch (state) { case flv_header_F: switch (ch) { case 'F': state = flv_header_FL; break; default: rc = NGX_ERROR; goto done; } break; case flv_header_FL: switch (ch) { case 'L': state = flv_header_FLV; break; default: rc = NGX_ERROR; goto done; } break; case flv_header_FLV: switch (ch) { case 'V': state = flv_header_Version; break; default: rc = NGX_ERROR; goto done; } break; case flv_header_Version: s->flv_version = ch; if (s->flv_version != 1) { rc = NGX_ERROR; goto done; } state = flv_header_Flags; break; case flv_header_Flags: s->flv_flags = ch; state = flv_header_DataOffset0; break; case flv_header_DataOffset0: pc = (u_char *) &s->flv_data_offset; pc[3] = ch; state = flv_header_DataOffset1; break; case flv_header_DataOffset1: pc = (u_char *) &s->flv_data_offset; pc[2] = ch; state = flv_header_DataOffset2; break; case flv_header_DataOffset2: pc = (u_char *) &s->flv_data_offset; pc[1] = ch; state = flv_header_DataOffset3; break; case flv_header_DataOffset3: pc = (u_char *) &s->flv_data_offset; pc[0] = ch; state = flv_tagsize0; break; case flv_tagsize0: s->flv_tagsize = 0; pc = (u_char *) &s->flv_tagsize; pc[3] = ch; state = flv_tagsize1; break; case flv_tagsize1: pc = (u_char *) &s->flv_tagsize; pc[2] = ch; state = flv_tagsize2; break; case flv_tagsize2: pc = (u_char *) &s->flv_tagsize; pc[1] = ch; state = flv_tagsize3; break; case flv_tagsize3: pc = (u_char *) &s->flv_tagsize; pc[0] = ch; st = &s->in_streams[0]; h = &st->hdr; if (h->mlen == 0 && s->flv_first_pts == 0) { s->flv_first_pts = 1; if (s->flv_tagsize != 0) { rc = NGX_ERROR; goto done; } } else { if (h->mlen + 11 != s->flv_tagsize) { rc = NGX_ERROR; goto done; } } state = flv_tagtype; break; case flv_tagtype: if (ch != NGX_RTMP_MSG_AMF_META && ch != NGX_RTMP_MSG_AUDIO && ch != NGX_RTMP_MSG_VIDEO) { rc = NGX_ERROR; goto done; } st = &s->in_streams[0]; h = &st->hdr; h->type = ch; state = flv_datasize0; break; case flv_datasize0: st = &s->in_streams[0]; h = &st->hdr; h->mlen = 0; pc = (u_char *) &h->mlen; pc[2] = ch; state = flv_datasize1; break; case flv_datasize1: st = &s->in_streams[0]; h = &st->hdr; pc = (u_char *) &h->mlen; pc[1] = ch; state = flv_datasize2; break; case flv_datasize2: st = &s->in_streams[0]; h = &st->hdr; pc = (u_char *) &h->mlen; pc[0] = ch; state = flv_timestamp0; st->len = h->mlen; break; case flv_timestamp0: st = &s->in_streams[0]; h = &st->hdr; pc = (u_char *) &h->timestamp; pc[2] = ch; state = flv_timestamp1; break; case flv_timestamp1: st = &s->in_streams[0]; h = &st->hdr; pc = (u_char *) &h->timestamp; pc[1] = ch; state = flv_timestamp2; break; case flv_timestamp2: st = &s->in_streams[0]; h = &st->hdr; pc = (u_char *) &h->timestamp; pc[0] = ch; state = flv_timestamp_extended; break; case flv_timestamp_extended: st = &s->in_streams[0]; h = &st->hdr; pc = (u_char *) &h->timestamp; pc[3] = ch; state = flv_streamid0; break; case flv_streamid0: st = &s->in_streams[0]; h = &st->hdr; h->msid = 0; pc = (u_char *) &h->msid; pc[2] = ch; state = flv_streamid1; break; case flv_streamid1: st = &s->in_streams[0]; h = &st->hdr; pc = (u_char *) &h->msid; pc[1] = ch; state = flv_streamid2; break; case flv_streamid2: st = &s->in_streams[0]; h = &st->hdr; pc = (u_char *) &h->msid; pc[0] = ch; state = flv_data; break; case flv_data: st = &s->in_streams[0]; for (ll = &st->in; (*ll) && (*ll)->buf->last == (*ll)->buf->end; ll = &(*ll)->next); for (;;) { if (*ll == NULL) { *ll = ngx_get_chainbuf(cscf->chunk_size, 1); } len = ngx_min(st->len, b->last - p); if ((*ll)->buf->end - (*ll)->buf->last >= (long) len) { (*ll)->buf->last = ngx_cpymem((*ll)->buf->last, p, len); p += len; st->len -= len; break; } len = (*ll)->buf->end - (*ll)->buf->last; (*ll)->buf->last = ngx_cpymem((*ll)->buf->last, p, len); p += len; st->len -= len; ll = &(*ll)->next; } if (st->len != 0) { rc = NGX_AGAIN; goto done; } state = flv_tagsize0; rc = NGX_OK; goto done; } } done: b->pos = p; s->flv_state = state; return rc; }
static ngx_int_t ngx_rtmp_gop_cache_alloc_cache(ngx_rtmp_session_t *s) { ngx_rtmp_codec_ctx_t *codec_ctx; ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_gop_cache_ctx_t *ctx; ngx_rtmp_gop_cache_t *cache, **iter; ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_gop_cache_module); if (ctx == NULL) { return NGX_ERROR; } codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module); if (codec_ctx == NULL) { return NGX_ERROR; } cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); if (cscf == NULL) { return NGX_ERROR; } if (ctx->free_cache) { cache = ctx->free_cache; ctx->free_cache = cache->next; ngx_memzero(cache, sizeof(ngx_rtmp_gop_cache_t)); } else { cache = ngx_pcalloc(ctx->pool, sizeof(ngx_rtmp_gop_cache_t)); if (cache == NULL) { return NGX_ERROR; } } // save video seq header. if (codec_ctx->avc_header && ctx->video_seq_header == NULL) { ctx->video_seq_header = codec_ctx->avc_header; } // save audio seq header. if (codec_ctx->aac_header && ctx->audio_seq_header == NULL) { ctx->audio_seq_header = codec_ctx->aac_header; } // save metadata. if (codec_ctx->meta && ctx->meta == NULL) { ctx->meta_version = codec_ctx->meta_version; ctx->meta = codec_ctx->meta; } if (ctx->cache_head == NULL) { ctx->cache_tail = ctx->cache_head = cache; } else { iter = &ctx->cache_tail->next; *iter = cache; ctx->cache_tail = cache; } ctx->gop_cache_count++; ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "gop alloc cache: gop_cache_count='%uD'", ctx->gop_cache_count); return NGX_OK; }
static void ngx_rtmp_play_read_meta(ngx_rtmp_session_t *s) { ngx_rtmp_play_ctx_t *ctx; ssize_t n; ngx_rtmp_header_t h; ngx_chain_t *out, in; ngx_buf_t in_buf; ngx_rtmp_core_srv_conf_t *cscf; uint32_t size; cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module); if (ctx == NULL) { return; } ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "play: read meta"); /* read tag header */ n = ngx_read_file(&ctx->file, ngx_rtmp_play_header, sizeof(ngx_rtmp_play_header), NGX_RTMP_PLAY_DATA_OFFSET); if (n != sizeof(ngx_rtmp_play_header)) { ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "play: could not read metadata tag header"); return; } if (ngx_rtmp_play_header[0] != NGX_RTMP_MSG_AMF_META) { ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "play: first tag is not metadata, giving up"); return; } ngx_memzero(&h, sizeof(h)); h.type = NGX_RTMP_MSG_AMF_META; h.msid = NGX_RTMP_LIVE_MSID; h.csid = NGX_RTMP_LIVE_CSID_META; size = 0; ngx_rtmp_rmemcpy(&size, ngx_rtmp_play_header + 1, 3); ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "play: metadata size=%D", size); if (size > sizeof(ngx_rtmp_play_buffer)) { ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "play: too big metadata"); return; } /* read metadata */ n = ngx_read_file(&ctx->file, ngx_rtmp_play_buffer, size, sizeof(ngx_rtmp_play_header) + NGX_RTMP_PLAY_DATA_OFFSET); if (n != (ssize_t) size) { ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "play: could not read metadata"); return; } /* prepare input chain */ ngx_memzero(&in, sizeof(in)); ngx_memzero(&in_buf, sizeof(in_buf)); in.buf = &in_buf; in_buf.pos = ngx_rtmp_play_buffer; in_buf.last = ngx_rtmp_play_buffer + size; ngx_rtmp_play_init_index(s, &in); /* output chain */ out = ngx_rtmp_append_shared_bufs(cscf, NULL, &in); ngx_rtmp_prepare_message(s, &h, NULL, out); ngx_rtmp_send_message(s, out, 0); ngx_rtmp_free_shared_chain(cscf, out); }
static void ngx_rtmp_gop_cache_frame(ngx_rtmp_session_t *s, ngx_uint_t prio, ngx_rtmp_header_t *ch, ngx_chain_t *frame) { ngx_rtmp_gop_cache_ctx_t *ctx; ngx_rtmp_codec_ctx_t *codec_ctx; ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_gop_cache_app_conf_t *gacf; ngx_rtmp_gop_frame_t *gf; gacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_gop_cache_module); if (gacf == NULL || !gacf->gop_cache) { return; } cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); if (cscf == NULL) { return; } ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_gop_cache_module); if (ctx == NULL) { return; } codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module); if (codec_ctx == NULL) { return; } if (ch->type == NGX_RTMP_MSG_VIDEO) { // drop video when not H.264 if (codec_ctx->video_codec_id != NGX_RTMP_VIDEO_H264) { ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "drop video non-H.264 encode type timestamp='%uD'", ch->timestamp); return; } // drop non-IDR if (prio != NGX_RTMP_VIDEO_KEY_FRAME && ctx->cache_head == NULL) { ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "drop video non-keyframe timestamp='%uD'", ch->timestamp); return; } } // pure audio if (ctx->video_frame_in_all == 0 && ch->type == NGX_RTMP_MSG_AUDIO) { ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "drop audio frame timestamp='%uD'", ch->timestamp); return; } if (ch->type == NGX_RTMP_MSG_VIDEO && prio == NGX_RTMP_VIDEO_KEY_FRAME) { if (ngx_rtmp_gop_cache_alloc_cache(s) != NGX_OK) { return; } } gf = ngx_rtmp_gop_cache_alloc_frame(s); if (gf == NULL) { return; } gf->h = *ch; gf->prio = prio; gf->next = NULL; gf->frame = ngx_rtmp_append_shared_bufs(cscf, NULL, frame); if (ngx_rtmp_gop_cache_link_frame(s, gf) != NGX_OK) { ngx_rtmp_free_shared_chain(cscf, gf->frame); return; } if (ctx->video_frame_in_all > gacf->gop_max_video_count || ctx->audio_frame_in_all > gacf->gop_max_audio_count || (ctx->video_frame_in_all + ctx->audio_frame_in_all) > gacf->gop_max_frame_count) { ngx_log_error(NGX_LOG_WARN, s->connection->log, 0, "gop cache: video_frame_in_cache='%uD' " "audio_frame_in_cache='%uD' max_video_count='%uD' " "max_audio_count='%uD' gop_max_frame_count='%uD'", ctx->video_frame_in_all, ctx->audio_frame_in_all, gacf->gop_max_video_count, gacf->gop_max_audio_count, gacf->gop_max_frame_count); ngx_rtmp_gop_cache_cleanup(s); return; } ngx_rtmp_gop_cache_update(s); ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "gop cache: cache packet type='%s' timestamp='%uD'", gf->h.type == NGX_RTMP_MSG_AUDIO ? "audio" : "video", gf->h.timestamp); }
static ngx_int_t ngx_rtmp_play_next_entry(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v) { ngx_rtmp_play_app_conf_t *pacf; ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_play_ctx_t *ctx; ngx_rtmp_play_entry_t *pe; u_char *p; static u_char path[NGX_MAX_PATH + 1]; pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module); cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module); for ( ;; ) { if (ctx->file.fd != NGX_INVALID_FILE) { ngx_close_file(ctx->file.fd); ctx->file.fd = NGX_INVALID_FILE; } if (ctx->file_id) { ngx_rtmp_play_cleanup_local_file(s); } ctx->nentry = (ctx->nentry == NGX_CONF_UNSET_UINT ? 0 : ctx->nentry + 1); if (ctx->nentry >= pacf->entries.nelts) { ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "play: all entries failed"); ngx_rtmp_send_status(s, "NetStream.Play.StreamNotFound", "error", "Video on demand stream not found"); break; } pe = ngx_rtmp_play_get_current_entry(s); ngx_log_debug4(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "play: trying %s entry %ui/%uz '%V'", pe->url ? "remote" : "local", ctx->nentry + 1, pacf->entries.nelts, pe->url ? &pe->url->url : pe->root); /* open remote */ if (pe->url) { return ngx_rtmp_play_open_remote(s, v); } /* open local */ p = ngx_snprintf(path, NGX_MAX_PATH, "%V/%s%V", pe->root, v->name + ctx->pfx_size, &ctx->sfx); *p = 0; ctx->file.fd = ngx_open_file(path, NGX_FILE_RDONLY, NGX_FILE_OPEN, cscf->file_access); if (ctx->file.fd == NGX_INVALID_FILE) { ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, ngx_errno, "play: error opening file '%s'", path); continue; } ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "play: open local file '%s'", path); if (ngx_rtmp_play_open(s, v->start) != NGX_OK) { return NGX_ERROR; } break; } return next_play(s, v); }
ngx_rtmp_session_t * ngx_rtmp_init_session(ngx_connection_t *c, ngx_rtmp_addr_conf_t *addr_conf) { ngx_rtmp_session_t *s; ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_error_log_ctx_t *ctx; s = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_session_t) + sizeof(ngx_chain_t *) * ((ngx_rtmp_core_srv_conf_t *) addr_conf->ctx-> srv_conf[ngx_rtmp_core_module .ctx_index])->out_queue); if (s == NULL) { ngx_rtmp_close_connection(c); return NULL; } s->main_conf = addr_conf->ctx->main_conf; s->srv_conf = addr_conf->ctx->srv_conf; s->addr_text = &addr_conf->addr_text; c->data = s; s->connection = c; ctx = ngx_palloc(c->pool, sizeof(ngx_rtmp_error_log_ctx_t)); if (ctx == NULL) { ngx_rtmp_close_connection(c); return NULL; } ctx->client = &c->addr_text; ctx->session = s; c->log->connection = c->number; c->log->handler = ngx_rtmp_log_error; c->log->data = ctx; c->log->action = NULL; c->log_error = NGX_ERROR_INFO; s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_rtmp_max_module); if (s->ctx == NULL) { ngx_rtmp_close_connection(c); return NULL; } cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); s->out_queue = cscf->out_queue; s->out_cork = cscf->out_cork; s->in_streams = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_stream_t) * cscf->max_streams); if (s->in_streams == NULL) { ngx_rtmp_close_connection(c); return NULL; } s->epoch = ngx_current_msec; s->timeout = cscf->timeout; ngx_rtmp_set_chunk_size(s, NGX_RTMP_DEFAULT_CHUNK_SIZE); if (ngx_rtmp_fire_event(s, NGX_RTMP_CONNECT, NULL, NULL) != NGX_OK) { ngx_rtmp_finalize_session(s); return NULL; } return s; }
/* 直播音视频处理 */ static ngx_int_t ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in) { ngx_rtmp_live_ctx_t *ctx, *pctx; ngx_rtmp_codec_ctx_t *codec_ctx; ngx_chain_t *header, *coheader, *meta, *apkt, *aapkt, *acopkt, *rpkt; ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_live_app_conf_t *lacf; ngx_rtmp_session_t *ss; ngx_rtmp_header_t ch; /* 当前rtmp header */ ngx_rtmp_header_t lh; /* 上一个rtmp header*/ ngx_rtmp_header_t clh; /* 音频转化视频的rtmp header */ ngx_int_t rc, mandatory, dummy_audio; ngx_uint_t prio; ngx_uint_t peers; ngx_uint_t meta_version; ngx_uint_t csidx; uint32_t delta; ngx_rtmp_live_chunk_stream_t *cs; #ifdef NGX_DEBUG const char *type_s; type_s = (h->type == NGX_RTMP_MSG_VIDEO ? "video" : "audio"); #endif lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module); if (lacf == NULL) { return NGX_ERROR; } if (!lacf->live || in == NULL || in->buf == NULL) { return NGX_OK; } /* 从会话中获取live ctx */ ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module); if (ctx == NULL || ctx->stream == NULL) { return NGX_OK; } if (ctx->publishing == 0) { ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "live: %s from non-publisher", type_s); return NGX_OK; } if (!ctx->stream->active) { ngx_rtmp_live_start(s); } if (ctx->idle_evt.timer_set) { ngx_add_timer(&ctx->idle_evt, lacf->idle_timeout); } ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "live: %s packet timestamp=%uD", type_s, h->timestamp); s->current_time = h->timestamp; peers = 0; apkt = NULL; aapkt = NULL; acopkt = NULL; header = NULL; coheader = NULL; meta = NULL; meta_version = 0; mandatory = 0; prio = (h->type == NGX_RTMP_MSG_VIDEO ? ngx_rtmp_get_video_frame_type(in) : 0); cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); csidx = !(lacf->interleave || h->type == NGX_RTMP_MSG_VIDEO); /* 视频 csidx为0 音频csidx为0 */ cs = &ctx->cs[csidx]; ngx_memzero(&ch, sizeof(ch)); ch.timestamp = h->timestamp; /* 从接收报文中获取时间戳 */ ch.msid = NGX_RTMP_MSID; ch.csid = cs->csid; ch.type = h->type; /* 从接收报文中获取类型 */ lh = ch; if (cs->active) { lh.timestamp = cs->timestamp; /* 从流中获取时戳 */ } clh = lh; clh.type = (h->type == NGX_RTMP_MSG_AUDIO ? NGX_RTMP_MSG_VIDEO : NGX_RTMP_MSG_AUDIO); /* 如果是音频与视频相互转换,类型一定是视频 */ cs->active = 1; cs->timestamp = ch.timestamp; /* 更新时戳 */ delta = ch.timestamp - lh.timestamp; /* if (delta >> 31) { ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "live: clipping non-monotonical timestamp %uD->%uD", lh.timestamp, ch.timestamp); delta = 0; ch.timestamp = lh.timestamp; } */ rpkt = ngx_rtmp_append_shared_bufs(cscf, NULL, in); ngx_rtmp_prepare_message(s, &ch, &lh, rpkt); codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module); /* 转码处理 */ if (codec_ctx) { if (h->type == NGX_RTMP_MSG_AUDIO) { header = codec_ctx->aac_header; if (lacf->interleave) { coheader = codec_ctx->avc_header; } if (codec_ctx->audio_codec_id == NGX_RTMP_AUDIO_AAC && ngx_rtmp_is_codec_header(in)) { prio = 0; mandatory = 1; } } else { header = codec_ctx->avc_header; if (lacf->interleave) { coheader = codec_ctx->aac_header; } if (codec_ctx->video_codec_id == NGX_RTMP_VIDEO_H264 && ngx_rtmp_is_codec_header(in)) { prio = 0; mandatory = 1; } } if (codec_ctx->meta) { meta = codec_ctx->meta; meta_version = codec_ctx->meta_version; } } /* broadcast to all subscribers */ for (pctx = ctx->stream->ctx; pctx; pctx = pctx->next) { if (pctx == ctx || pctx->paused) { /* 发布rtmp流本身或者该播放流已中止 */ continue; } ss = pctx->session; cs = &pctx->cs[csidx]; /* send metadata */ /* 发送元数据 */ if (meta && meta_version != pctx->meta_version) { ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: meta"); if (ngx_rtmp_send_message(ss, meta, 0) == NGX_OK) { pctx->meta_version = meta_version; } } /* sync stream */ if (cs->active && (lacf->sync && cs->dropped > lacf->sync)) { ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: sync %s dropped=%uD", type_s, cs->dropped); cs->active = 0; cs->dropped = 0; } /* absolute packet */ if (!cs->active) { /* 流没有启动 */ if (mandatory) { ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: skipping header"); continue; } /* 等待视频 */ if (lacf->wait_video && h->type == NGX_RTMP_MSG_AUDIO && !pctx->cs[0].active) { ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: waiting for video"); continue; } /* 等待关键帧 */ if (lacf->wait_key && prio != NGX_RTMP_VIDEO_KEY_FRAME && (lacf->interleave || h->type == NGX_RTMP_MSG_VIDEO)) { ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: skip non-key"); continue; } dummy_audio = 0; /* 生成dummy audio */ if (lacf->wait_video && h->type == NGX_RTMP_MSG_VIDEO && !pctx->cs[1].active) { dummy_audio = 1; if (aapkt == NULL) { aapkt = ngx_rtmp_alloc_shared_buf(cscf); /* aapk如果申请失败,需要作一下失败处理,避免访问空指针 */ if(NULL != aapkt){ ngx_rtmp_prepare_message(s, &clh, NULL, aapkt); } } } /* 转码 */ if (header || coheader) { /* send absolute codec header */ ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: abs %s header timestamp=%uD", type_s, lh.timestamp); if (header) { if (apkt == NULL) { apkt = ngx_rtmp_append_shared_bufs(cscf, NULL, header); ngx_rtmp_prepare_message(s, &lh, NULL, apkt); } rc = ngx_rtmp_send_message(ss, apkt, 0); if (rc != NGX_OK) { continue; } } if (coheader) { if (acopkt == NULL) { acopkt = ngx_rtmp_append_shared_bufs(cscf, NULL, coheader); ngx_rtmp_prepare_message(s, &clh, NULL, acopkt); } rc = ngx_rtmp_send_message(ss, acopkt, 0); if (rc != NGX_OK) { continue; } } else if (dummy_audio) { ngx_rtmp_send_message(ss, aapkt, 0); } cs->timestamp = lh.timestamp; cs->active = 1; ss->current_time = cs->timestamp; } else { /* send absolute packet */ ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: abs %s packet timestamp=%uD", type_s, ch.timestamp); if (apkt == NULL) { apkt = ngx_rtmp_append_shared_bufs(cscf, NULL, in); ngx_rtmp_prepare_message(s, &ch, NULL, apkt); } rc = ngx_rtmp_send_message(ss, apkt, prio); if (rc != NGX_OK) { continue; } cs->timestamp = ch.timestamp; cs->active = 1; ss->current_time = cs->timestamp; ++peers; if (dummy_audio) { ngx_rtmp_send_message(ss, aapkt, 0); } continue; } } /* send relative packet */ ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: rel %s packet delta=%uD", type_s, delta); if (ngx_rtmp_send_message(ss, rpkt, prio) != NGX_OK) { ++pctx->ndropped; cs->dropped += delta; if (mandatory) { ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: mandatory packet failed"); ngx_rtmp_finalize_session(ss); } continue; } cs->timestamp += delta; ++peers; ss->current_time = cs->timestamp; } if (rpkt) { ngx_rtmp_free_shared_chain(cscf, rpkt); } if (apkt) { ngx_rtmp_free_shared_chain(cscf, apkt); } if (aapkt) { ngx_rtmp_free_shared_chain(cscf, aapkt); } if (acopkt) { ngx_rtmp_free_shared_chain(cscf, acopkt); } /* 更新直播流带宽信息 */ ngx_rtmp_update_bandwidth(&ctx->stream->bw_in, h->mlen); ngx_rtmp_update_bandwidth(&ctx->stream->bw_out, h->mlen * peers); ngx_rtmp_update_bandwidth(h->type == NGX_RTMP_MSG_AUDIO ? &ctx->stream->bw_in_audio : &ctx->stream->bw_in_video, h->mlen); return NGX_OK; }
static ngx_int_t ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in) { ngx_rtmp_live_ctx_t *ctx, *pctx; ngx_rtmp_codec_ctx_t *codec_ctx; ngx_chain_t *header, *coheader, *meta, *apkt, *aapkt, *acopkt, *rpkt; ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_live_app_conf_t *lacf; ngx_rtmp_session_t *ss; //指向某一个peer对应的session ngx_rtmp_header_t ch, lh, clh; ngx_int_t rc, mandatory, dummy_audio; ngx_uint_t prio; //Frame Type ngx_uint_t peers; //记录订阅的用户数目 ngx_uint_t meta_version; ngx_uint_t csidx; uint32_t delta; ngx_rtmp_live_chunk_stream_t *cs; const char *type_s; //类型 type_s = (h->type == NGX_RTMP_MSG_VIDEO ? "video" : "audio"); lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module); if (lacf == NULL) { return NGX_ERROR; } if (!lacf->live || in == NULL || in->buf == NULL) { return NGX_OK; } ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module); if (ctx == NULL || ctx->stream == NULL) { return NGX_OK; } // if (ctx->publishing == 0) { ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, "live: name = %s, %s from non-publisher", ctx->stream->name, type_s); return NGX_OK; } if (!ctx->stream->active) { ngx_rtmp_live_start(s); } // 更新计时器 if (ctx->idle_evt.timer_set) { ngx_add_timer(&ctx->idle_evt, lacf->idle_timeout); } s->current_time = h->timestamp; peers = 0; //记录所有的peers数目 apkt = NULL; aapkt = NULL; acopkt = NULL; header = NULL; //aac_header or avc_header coheader = NULL; //avc_header or aac_header meta = NULL; //meta data meta_version = 0; //meta version mandatory = 0; prio = (h->type == NGX_RTMP_MSG_VIDEO ? ngx_rtmp_get_video_frame_type(in) : 0); cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); // 如果是video,并且不允许video跟audio transmitted on the same RTMP chunk stream csidx = !(lacf->interleave || h->type == NGX_RTMP_MSG_VIDEO); cs = &ctx->cs[csidx]; ngx_memzero(&ch, sizeof(ch)); ch.timestamp = h->timestamp; //时间戳 ch.msid = NGX_RTMP_MSID; /* message stream id */ ch.csid = cs->csid; /* chunk stream id,video = 0,audio = 1*/ ch.type = h->type; //音视频 lh = ch; if (cs->active) { lh.timestamp = cs->timestamp; } clh = lh; clh.type = (h->type == NGX_RTMP_MSG_AUDIO ? NGX_RTMP_MSG_VIDEO : NGX_RTMP_MSG_AUDIO); cs->active = 1; cs->timestamp = ch.timestamp; delta = ch.timestamp - lh.timestamp; /* if (delta >> 31) { ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "live: clipping non-monotonical timestamp %uD->%uD", lh.timestamp, ch.timestamp); delta = 0; ch.timestamp = lh.timestamp; } */ rpkt = ngx_rtmp_append_shared_bufs(cscf, NULL, in); ngx_rtmp_prepare_message(s, &ch, &lh, rpkt); codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module); if (codec_ctx) { if (h->type == NGX_RTMP_MSG_AUDIO) { header = codec_ctx->aac_header; if (lacf->interleave) { coheader = codec_ctx->avc_header; } //AAC sequence header if (codec_ctx->audio_codec_id == NGX_RTMP_AUDIO_AAC && ngx_rtmp_is_codec_header(in)) { prio = 0; mandatory = 1; } } else { header = codec_ctx->avc_header; if (lacf->interleave) { coheader = codec_ctx->aac_header; } // AVC sequence header if (codec_ctx->video_codec_id == NGX_RTMP_VIDEO_H264 && ngx_rtmp_is_codec_header(in)) { prio = 0; mandatory = 1; } } if (codec_ctx->meta) { meta = codec_ctx->meta; meta_version = codec_ctx->meta_version; } } /* broadcast to all subscribers */ for (pctx = ctx->stream->ctx; pctx; pctx = pctx->next) { if (pctx == ctx || pctx->paused) { continue; } ss = pctx->session; cs = &pctx->cs[csidx]; /* send metadata */ if (meta && meta_version != pctx->meta_version) { ngx_log_error(NGX_LOG_INFO, ss->connection->log, 0, "live: av name = %s send meta", ctx->stream->name); if (ngx_rtmp_send_message(ss, meta, 0) == NGX_OK) { pctx->meta_version = meta_version; } } /* sync stream */ if (cs->active && (lacf->sync && cs->dropped > lacf->sync)) { ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: sync %s dropped=%uD", type_s, cs->dropped); cs->active = 0; cs->dropped = 0; } /* absolute packet */ if (!cs->active) { if (mandatory) { ngx_log_error(NGX_LOG_INFO, ss->connection->log, 0, "live: av name = %s, skipping header", ctx->stream->name); continue; } if (lacf->wait_video && h->type == NGX_RTMP_MSG_AUDIO && !pctx->cs[0].active) { ngx_log_error(NGX_LOG_INFO, ss->connection->log, 0, "live: av name = %s waiting for video", ctx->stream->name); continue; } // 对于video,先从关键帧开始推送 if (lacf->wait_key && prio != NGX_RTMP_VIDEO_KEY_FRAME && (lacf->interleave || h->type == NGX_RTMP_MSG_VIDEO)) { ngx_log_error(NGX_LOG_INFO, ss->connection->log, 0, "live: av name = %s skip non-key", ctx->stream->name); continue; } dummy_audio = 0; if (lacf->wait_video && h->type == NGX_RTMP_MSG_VIDEO && !pctx->cs[1].active) { dummy_audio = 1; if (aapkt == NULL) { aapkt = ngx_rtmp_alloc_shared_buf(cscf); ngx_rtmp_prepare_message(s, &clh, NULL, aapkt); } } if (header || coheader) { /* send absolute codec header */ ngx_log_error(NGX_LOG_INFO, ss->connection->log, 0, "live: av name = %s abs %s codec header timestamp=%uD", ctx->stream->name, type_s, lh.timestamp); if (header) { if (apkt == NULL) { apkt = ngx_rtmp_append_shared_bufs(cscf, NULL, header); ngx_rtmp_prepare_message(s, &lh, NULL, apkt); } rc = ngx_rtmp_send_message(ss, apkt, 0); if (rc != NGX_OK) { continue; } } if (coheader) { if (acopkt == NULL) { acopkt = ngx_rtmp_append_shared_bufs(cscf, NULL, coheader); ngx_rtmp_prepare_message(s, &clh, NULL, acopkt); } rc = ngx_rtmp_send_message(ss, acopkt, 0); if (rc != NGX_OK) { continue; } } else if (dummy_audio) { ngx_rtmp_send_message(ss, aapkt, 0); } cs->timestamp = lh.timestamp; cs->active = 1; ss->current_time = cs->timestamp; } else { /* send absolute packet */ ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: abs %s packet timestamp=%uD", type_s, ch.timestamp); if (apkt == NULL) { apkt = ngx_rtmp_append_shared_bufs(cscf, NULL, in); ngx_rtmp_prepare_message(s, &ch, NULL, apkt); } rc = ngx_rtmp_send_message(ss, apkt, prio); if (rc != NGX_OK) { continue; } cs->timestamp = ch.timestamp; cs->active = 1; ss->current_time = cs->timestamp; ++peers; if (dummy_audio) { ngx_rtmp_send_message(ss, aapkt, 0); } continue; } } /* send relative packet */ ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: rel %s packet delta=%uD", type_s, delta); if (ngx_rtmp_send_message(ss, rpkt, prio) != NGX_OK) { ++pctx->ndropped; cs->dropped += delta; if (mandatory) { ngx_log_error(NGX_LOG_INFO, ss->connection->log, 0, "live: av name = %s mandatory packet failed", ctx->stream->name); ngx_rtmp_finalize_session(ss); } continue; } cs->timestamp += delta; ++peers; ss->current_time = cs->timestamp; } if (rpkt) { ngx_rtmp_free_shared_chain(cscf, rpkt); } if (apkt) { ngx_rtmp_free_shared_chain(cscf, apkt); } if (aapkt) { ngx_rtmp_free_shared_chain(cscf, aapkt); } if (acopkt) { ngx_rtmp_free_shared_chain(cscf, acopkt); } ngx_rtmp_update_bandwidth(&ctx->stream->bw_in, h->mlen); ngx_rtmp_update_bandwidth(&ctx->stream->bw_out, h->mlen * peers); ngx_rtmp_update_bandwidth(h->type == NGX_RTMP_MSG_AUDIO ? &ctx->stream->bw_in_audio : &ctx->stream->bw_in_video, h->mlen); // ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, // "live: name = %s ,%s packet timestamp = %uD ,pees number = %d, bw_in_bytes = %uD, bw_out_bytes = %uD", // ctx->stream->name ,type_s, h->timestamp, // peers, ctx->stream->bw_in.bytes, ctx->stream->bw_out.bytes); return NGX_OK; }
static ngx_int_t ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in) { ngx_rtmp_live_ctx_t *ctx, *pctx; ngx_rtmp_codec_ctx_t *codec_ctx; ngx_chain_t *out, *peer_out, *header_out, *pheader_out; ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_live_app_conf_t *lacf; ngx_rtmp_session_t *ss; ngx_rtmp_header_t ch, lh; ngx_uint_t prio, peer_prio; ngx_uint_t peers, dropped_peers; size_t header_offset; ngx_uint_t header_version; lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module); if (lacf == NULL) { ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "live: NULL application"); return NGX_ERROR; } ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module); if (!lacf->live || in == NULL || in->buf == NULL || ctx == NULL || ctx->stream == NULL || (h->type != NGX_RTMP_MSG_VIDEO && h->type != NGX_RTMP_MSG_AUDIO)) { return NGX_OK; } if ((ctx->flags & NGX_RTMP_LIVE_PUBLISHING) == 0) { ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "live: received audio/video from non-publisher"); return NGX_OK; } ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "live: av: %s timestamp=%uD", h->type == NGX_RTMP_MSG_VIDEO ? "video" : "audio", h->timestamp); cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); /* prepare output header */ ngx_memzero(&ch, sizeof(ch)); ngx_memzero(&lh, sizeof(lh)); ch.timestamp = h->timestamp; ch.msid = NGX_RTMP_LIVE_MSID; ch.type = h->type; lh.msid = ch.msid; if (h->type == NGX_RTMP_MSG_VIDEO) { prio = ngx_rtmp_get_video_frame_type(in); ch.csid = NGX_RTMP_LIVE_CSID_VIDEO; lh.timestamp = ctx->last_video; ctx->last_video = ch.timestamp; } else { /* audio priority is the same as video key frame's */ prio = NGX_RTMP_VIDEO_KEY_FRAME; ch.csid = NGX_RTMP_LIVE_CSID_AUDIO; lh.timestamp = ctx->last_audio; ctx->last_audio = ch.timestamp; } lh.csid = ch.csid; out = ngx_rtmp_append_shared_bufs(cscf, NULL, in); ngx_rtmp_prepare_message(s, &ch, &lh, out); peers = 0; dropped_peers = 0; codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module); header_out = NULL; pheader_out = NULL; header_offset = 0; header_version = 0; if (codec_ctx) { if (h->type == NGX_RTMP_MSG_AUDIO) { if (codec_ctx->aac_pheader) { header_out = codec_ctx->aac_header; pheader_out = codec_ctx->aac_pheader; header_offset = offsetof(ngx_rtmp_live_ctx_t, aac_version); header_version = codec_ctx->aac_version; } } else { if (codec_ctx->avc_pheader) { header_out = codec_ctx->avc_header; pheader_out = codec_ctx->avc_pheader; header_offset = offsetof(ngx_rtmp_live_ctx_t, avc_version); header_version = codec_ctx->avc_version; } } } /* broadcast to all subscribers */ for (pctx = ctx->stream->ctx; pctx; pctx = pctx->next) { if (pctx == ctx) { continue; } ++peers; ss = pctx->session; /* send absolute frame */ if ((pctx->msg_mask & (1 << h->type)) == 0) { ch.timestamp = ngx_current_msec - ss->epoch; ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: av: abs %s timestamp=%uD", h->type == NGX_RTMP_MSG_VIDEO ? "video" : "audio", ch.timestamp); /* send codec header as abs frame if any */ peer_out = ngx_rtmp_append_shared_bufs(cscf, NULL, header_out ? header_out : in); ngx_rtmp_prepare_message(s, &ch, NULL, peer_out); pctx->msg_mask |= (1 << h->type); if (ngx_rtmp_send_message(ss, peer_out, prio) == NGX_OK && header_out) { *(ngx_uint_t *)((u_char *)pctx + header_offset) = header_version; } ngx_rtmp_free_shared_chain(cscf, peer_out); continue; } /* send AVC/H264 header if newer header has arrived */ if (pheader_out && *(ngx_uint_t *)((u_char *)pctx + header_offset) != header_version) { ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0, "live: sending codec header"); if (ngx_rtmp_send_message(ss, pheader_out, prio) == NGX_OK) { *(ngx_uint_t *)((u_char *)pctx + header_offset) = header_version; } } /* push buffered data */ peer_prio = prio; if (ngx_rtmp_send_message(ss, out, peer_prio) != NGX_OK) { ++pctx->dropped; ++dropped_peers; } } ngx_rtmp_free_shared_chain(cscf, out); ngx_rtmp_update_bandwidth(&ctx->stream->bw_in, h->mlen); ngx_rtmp_update_bandwidth(&ctx->stream->bw_out, h->mlen * (peers - dropped_peers)); return NGX_OK; }
static ngx_chain_t * ngx_rtmp_notify_connect_create(ngx_rtmp_session_t *s, void *arg, ngx_pool_t *pool) { ngx_rtmp_connect_t *v = arg; ngx_rtmp_notify_srv_conf_t *nscf; ngx_url_t *url; ngx_chain_t *al, *bl; ngx_buf_t *b; ngx_str_t *addr_text; size_t app_len, args_len, flashver_len, swf_url_len, tc_url_len, page_url_len; nscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_notify_module); al = ngx_alloc_chain_link(pool); if (al == NULL) { return NULL; } /* these values are still missing in session * so we have to construct the request from * connection struct */ app_len = ngx_strlen(v->app); args_len = ngx_strlen(v->args); flashver_len = ngx_strlen(v->flashver); swf_url_len = ngx_strlen(v->swf_url); tc_url_len = ngx_strlen(v->tc_url); page_url_len = ngx_strlen(v->page_url); addr_text = &s->connection->addr_text; b = ngx_create_temp_buf(pool, sizeof("call=connect") - 1 + sizeof("&app=") - 1 + app_len * 3 + sizeof("&flashver=") - 1 + flashver_len * 3 + sizeof("&swfurl=") - 1 + swf_url_len * 3 + sizeof("&tcurl=") - 1 + tc_url_len * 3 + sizeof("&pageurl=") - 1 + page_url_len * 3 + sizeof("&addr=") - 1 + addr_text->len * 3 + 1 + args_len ); if (b == NULL) { return NULL; } al->buf = b; al->next = NULL; b->last = ngx_cpymem(b->last, (u_char*) "app=", sizeof("app=") - 1); b->last = (u_char*) ngx_escape_uri(b->last, v->app, app_len, NGX_ESCAPE_ARGS); b->last = ngx_cpymem(b->last, (u_char*) "&flashver=", sizeof("&flashver=") - 1); b->last = (u_char*) ngx_escape_uri(b->last, v->flashver, flashver_len, NGX_ESCAPE_ARGS); b->last = ngx_cpymem(b->last, (u_char*) "&swfurl=", sizeof("&swfurl=") - 1); b->last = (u_char*) ngx_escape_uri(b->last, v->swf_url, swf_url_len, NGX_ESCAPE_ARGS); b->last = ngx_cpymem(b->last, (u_char*) "&tcurl=", sizeof("&tcurl=") - 1); b->last = (u_char*) ngx_escape_uri(b->last, v->tc_url, tc_url_len, NGX_ESCAPE_ARGS); b->last = ngx_cpymem(b->last, (u_char*) "&pageurl=", sizeof("&pageurl=") - 1); b->last = (u_char*) ngx_escape_uri(b->last, v->page_url, page_url_len, NGX_ESCAPE_ARGS); b->last = ngx_cpymem(b->last, (u_char*) "&addr=", sizeof("&addr=") -1); b->last = (u_char*) ngx_escape_uri(b->last, addr_text->data, addr_text->len, NGX_ESCAPE_ARGS); b->last = ngx_cpymem(b->last, (u_char*) "&call=connect", sizeof("&call=connect") - 1); if (args_len) { *b->last++ = '&'; b->last = (u_char *) ngx_cpymem(b->last, v->args, args_len); } url = nscf->url[NGX_RTMP_NOTIFY_CONNECT]; bl = NULL; if (nscf->method == NGX_RTMP_NETCALL_HTTP_POST) { bl = al; al = NULL; } return ngx_rtmp_netcall_http_format_request(nscf->method, &url->host, &url->uri, al, bl, pool, &ngx_rtmp_notify_urlencoded); }
static ngx_int_t ngx_rtmp_cmd_connect(ngx_rtmp_session_t *s, ngx_rtmp_connect_t *v) { ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_core_app_conf_t **cacfp; ngx_uint_t n; size_t len; ngx_rtmp_header_t h; static double trans; static double capabilities = NGX_RTMP_CAPABILITIES; static double object_encoding = 0; static ngx_rtmp_amf_elt_t out_obj[] = { { NGX_RTMP_AMF_STRING, ngx_string("fmsVer"), NGX_RTMP_FMS_VERSION, 0 }, { NGX_RTMP_AMF_NUMBER, ngx_string("capabilities"), &capabilities, 0 }, }; static ngx_rtmp_amf_elt_t out_inf[] = { { NGX_RTMP_AMF_STRING, ngx_string("level"), "status", 0 }, { NGX_RTMP_AMF_STRING, ngx_string("code"), "NetConnection.Connect.Success", 0 }, { NGX_RTMP_AMF_STRING, ngx_string("description"), "Connection succeeded.", 0 }, { NGX_RTMP_AMF_NUMBER, ngx_string("objectEncoding"), &object_encoding, 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, &trans, 0 }, { NGX_RTMP_AMF_OBJECT, ngx_null_string, out_obj, sizeof(out_obj) }, { NGX_RTMP_AMF_OBJECT, ngx_null_string, out_inf, sizeof(out_inf) }, }; if (s->connected) { ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, "connect: duplicate connection"); return NGX_ERROR; } cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); ngx_log_debug8(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "connect: app='%s' flashver='%s' swf_url='%s' " "tc_url='%s' page_url='%s' acodecs=%uD vcodecs=%uD " "object_encoding=%ui", v->app, v->flashver, v->swf_url, v->tc_url, v->page_url, (uint32_t)v->acodecs, (uint32_t)v->vcodecs, (ngx_int_t)v->object_encoding); trans = v->trans; /* fill session parameters */ s->connected = 1; ngx_memzero(&h, sizeof(h)); h.csid = NGX_RTMP_CMD_CSID_AMF_INI; h.type = NGX_RTMP_MSG_AMF_CMD; #define NGX_RTMP_SET_STRPAR(name) \ s->name.len = ngx_strlen(v->name); \ s->name.data = ngx_palloc(s->connection->pool, s->name.len); \ ngx_memcpy(s->name.data, v->name, s->name.len) NGX_RTMP_SET_STRPAR(app); NGX_RTMP_SET_STRPAR(flashver); NGX_RTMP_SET_STRPAR(swf_url); NGX_RTMP_SET_STRPAR(tc_url); NGX_RTMP_SET_STRPAR(page_url); #undef NGX_RTMP_SET_STRPAR s->acodecs = v->acodecs; s->vcodecs = v->vcodecs; /* find application & set app_conf */ len = ngx_strlen(v->app); cacfp = cscf->applications.elts; for(n = 0; n < cscf->applications.nelts; ++n, ++cacfp) { if ((*cacfp)->name.len == len && !ngx_strncmp((*cacfp)->name.data, v->app, len)) { /* found app! */ s->app_conf = (*cacfp)->app_conf; break; } } if (s->app_conf == NULL) { ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, "connect: application not found: '%s'", v->app); return NGX_ERROR; } object_encoding = v->object_encoding; /* send all replies */ return ngx_rtmp_send_ack_size(s, cscf->ack_window) != NGX_OK || ngx_rtmp_send_bandwidth(s, cscf->ack_window, NGX_RTMP_LIMIT_DYNAMIC) != NGX_OK || ngx_rtmp_send_chunk_size(s, cscf->chunk_size) != NGX_OK || ngx_rtmp_send_amf(s, &h, out_elts, sizeof(out_elts) / sizeof(out_elts[0])) != NGX_OK ? NGX_ERROR : NGX_OK; }
static void ngx_rtmp_play_send(ngx_event_t *e) { ngx_rtmp_session_t *s; ngx_rtmp_play_ctx_t *ctx; uint32_t last_timestamp; ngx_rtmp_header_t h, lh; ngx_rtmp_core_srv_conf_t *cscf; ngx_chain_t *out, in; ngx_buf_t in_buf; ssize_t n; uint32_t buflen, end_timestamp, size; s = e->data; cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module); if (ctx == NULL) { return; } if (ctx->offset == -1) { ctx->offset = ngx_rtmp_play_timestamp_to_offset(s, ctx->start_timestamp); ctx->start_timestamp = -1; /* set later from actual timestamp */ } ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "play: read tag at offset=%i", ctx->offset); /* read tag header */ n = ngx_read_file(&ctx->file, ngx_rtmp_play_header, sizeof(ngx_rtmp_play_header), ctx->offset); if (n != sizeof(ngx_rtmp_play_header)) { ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "play: could not read flv tag header"); ngx_rtmp_send_user_stream_eof(s, 1); return; } /* parse header fields */ ngx_memzero(&h, sizeof(h)); h.msid = NGX_RTMP_LIVE_MSID; h.type = ngx_rtmp_play_header[0]; size = 0; ngx_rtmp_rmemcpy(&size, ngx_rtmp_play_header + 1, 3); ngx_rtmp_rmemcpy(&h.timestamp, ngx_rtmp_play_header + 4, 3); ((u_char *) &h.timestamp)[3] = ngx_rtmp_play_header[7]; ctx->offset += (sizeof(ngx_rtmp_play_header) + size + 4); last_timestamp = 0; switch (h.type) { case NGX_RTMP_MSG_AUDIO: h.csid = NGX_RTMP_LIVE_CSID_AUDIO; last_timestamp = ctx->last_audio; ctx->last_audio = h.timestamp; break; case NGX_RTMP_MSG_VIDEO: h.csid = NGX_RTMP_LIVE_CSID_VIDEO; last_timestamp = ctx->last_video; ctx->last_video = h.timestamp; break; default: goto skip; } ngx_log_debug4(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "play: read tag type=%i size=%uD timestamp=%uD " "last_timestamp=%uD", (ngx_int_t) h.type,size, h.timestamp, last_timestamp); lh = h; lh.timestamp = last_timestamp; if (size > sizeof(ngx_rtmp_play_buffer)) { ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "play: too big message: %D>%uz", size, sizeof(ngx_rtmp_play_buffer)); goto next; } /* read tag body */ n = ngx_read_file(&ctx->file, ngx_rtmp_play_buffer, size, ctx->offset - size - 4); if (n != (ssize_t) size) { ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "play: could not read flv tag"); return; } /* prepare input chain */ ngx_memzero(&in, sizeof(in)); ngx_memzero(&in_buf, sizeof(in_buf)); in.buf = &in_buf; in_buf.pos = ngx_rtmp_play_buffer; in_buf.last = ngx_rtmp_play_buffer + size; /* output chain */ out = ngx_rtmp_append_shared_bufs(cscf, NULL, &in); ngx_rtmp_prepare_message(s, &h, ctx->msg_mask & (1 << h.type) ? &lh : NULL, out); ngx_rtmp_send_message(s, out, 0); /* TODO: priority */ ngx_rtmp_free_shared_chain(cscf, out); ctx->msg_mask |= (1 << h.type); next: if (ctx->start_timestamp == -1) { ctx->start_timestamp = h.timestamp; ctx->epoch = ngx_current_msec; ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "play: start_timestamp=%i", ctx->start_timestamp); goto skip; } buflen = (s->buflen ? s->buflen : NGX_RTMP_PLAY_DEFAULT_BUFLEN); end_timestamp = (ngx_current_msec - ctx->epoch) + ctx->start_timestamp + buflen; ngx_log_debug5(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "play: %s wait=%D timestamp=%D end_timestamp=%D bufen=%i", h.timestamp > end_timestamp ? "schedule" : "advance", h.timestamp > end_timestamp ? h.timestamp - end_timestamp : 0, h.timestamp, end_timestamp, (ngx_int_t) buflen); /* too much data sent; schedule timeout */ if (h.timestamp > end_timestamp) { ngx_add_timer(e, h.timestamp - end_timestamp); return; } skip: ngx_post_event(e, &ngx_posted_events); }