Permalink
Browse files

refactored seek & pause in vod

  • Loading branch information...
arut committed Oct 5, 2012
1 parent fb82437 commit 2ef9075f2b2fb4a782f330e8cb194e5714c509be
Showing with 194 additions and 157 deletions.
  1. +10 −108 ngx_rtmp_cmd_module.c
  2. +29 −7 ngx_rtmp_flv_module.c
  3. +82 −14 ngx_rtmp_mp4_module.c
  4. +67 −27 ngx_rtmp_play_module.c
  5. +5 −1 ngx_rtmp_play_module.h
  6. +1 −0 ngx_rtmp_send.c
View
@@ -1003,71 +1003,17 @@ ngx_rtmp_cmd_pause_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
static ngx_int_t
ngx_rtmp_cmd_pause(ngx_rtmp_session_t *s, ngx_rtmp_pause_t *v)
{
- ngx_rtmp_header_t h;
-
- static double trans;
-
- static ngx_rtmp_amf_elt_t out_inf[] = {
-
- { NGX_RTMP_AMF_STRING,
- ngx_string("code"),
- "NetStream.Pause.Notify", 0 },
-
- { NGX_RTMP_AMF_STRING,
- ngx_string("level"),
- "status", 0 },
-
- { NGX_RTMP_AMF_STRING,
- ngx_string("description"),
- "Paused.", 0 },
- };
-
- static ngx_rtmp_amf_elt_t out_elts[] = {
-
- { NGX_RTMP_AMF_STRING,
- ngx_null_string,
- "onStatus", 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_log_error(NGX_LOG_INFO, s->connection->log, 0,
- "pause: state='%i' position=%i",
- v->pause, (ngx_int_t) v->position);
-
- /* send onStatus reply */
- ngx_memzero(&h, sizeof(h));
- h.type = NGX_RTMP_MSG_AMF_CMD;
- h.csid = NGX_RTMP_CMD_CSID_AMF;
- h.msid = NGX_RTMP_CMD_MSID;
+ "pause: state='%i' position=%i",
+ v->pause, (ngx_int_t) v->position);
if (v->pause) {
- out_inf[0].data = "NetStream.Pause.Notify";
- out_inf[2].data = "Paused.";
- return ngx_rtmp_send_user_stream_eof(s, NGX_RTMP_CMD_MSID) != NGX_OK
- || ngx_rtmp_send_amf(s, &h, out_elts,
- sizeof(out_elts) / sizeof(out_elts[0])) != NGX_OK
- ? NGX_ERROR
- : NGX_OK;
+ return ngx_rtmp_send_status(s, "NetStream.Pause.Notify", "status",
+ "Paused");
+ } else {
+ return ngx_rtmp_send_status(s, "NetStream.Unpause.Notify", "status",
+ "Unpaused");
}
-
- out_inf[0].data = "NetStream.Unpause.Notify";
- out_inf[2].data = "Unpaused.";
- return ngx_rtmp_send_user_stream_begin(s, NGX_RTMP_CMD_MSID) != NGX_OK
- || ngx_rtmp_send_amf(s, &h, out_elts,
- sizeof(out_elts) / sizeof(out_elts[0])) != NGX_OK
- ? NGX_ERROR
- : NGX_OK;
}
@@ -1124,57 +1070,13 @@ ngx_rtmp_cmd_seek_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
static ngx_int_t
ngx_rtmp_cmd_seek(ngx_rtmp_session_t *s, ngx_rtmp_seek_t *v)
{
- ngx_rtmp_header_t h;
-
- static double trans;
-
- static ngx_rtmp_amf_elt_t out_inf[] = {
-
- { NGX_RTMP_AMF_STRING,
- ngx_string("code"),
- "NetStream.Play.Reset", 0 },
-
- { NGX_RTMP_AMF_STRING,
- ngx_string("level"),
- "status", 0 },
-
- { NGX_RTMP_AMF_STRING,
- ngx_string("description"),
- "Paused.", 0 },
- };
-
- static ngx_rtmp_amf_elt_t out_elts[] = {
-
- { NGX_RTMP_AMF_STRING,
- ngx_null_string,
- "onStatus", 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_log_error(NGX_LOG_INFO, s->connection->log, 0,
- "seek: offset=%i", (ngx_int_t) v->offset);
-
- /* send onStatus reply */
- ngx_memzero(&h, sizeof(h));
- h.type = NGX_RTMP_MSG_AMF_CMD;
- h.csid = NGX_RTMP_CMD_CSID_AMF;
- h.msid = NGX_RTMP_CMD_MSID;
+ "seek: offset=%i", (ngx_int_t) v->offset);
return (ngx_rtmp_send_user_stream_eof(s, NGX_RTMP_CMD_MSID) != NGX_OK
|| ngx_rtmp_send_user_stream_begin(s, NGX_RTMP_CMD_MSID) != NGX_OK
- || ngx_rtmp_send_amf(s, &h, out_elts,
- sizeof(out_elts) / sizeof(out_elts[0])) != NGX_OK)
+ || ngx_rtmp_send_status(s, "NetStream.Seek.Notify", "status",
+ "Seeking")
? NGX_ERROR
: NGX_OK;
}
View
@@ -13,10 +13,12 @@ static void ngx_rtmp_flv_read_meta(ngx_rtmp_session_t *s, ngx_file_t *f);
static ngx_int_t ngx_rtmp_flv_timestamp_to_offset(ngx_rtmp_session_t *s,
ngx_file_t *f, ngx_int_t timestamp);
static ngx_int_t ngx_rtmp_flv_init(ngx_rtmp_session_t *s, ngx_file_t *f);
-static ngx_int_t ngx_rtmp_flv_start(ngx_rtmp_session_t *s, ngx_file_t *f,
+static ngx_int_t ngx_rtmp_flv_start(ngx_rtmp_session_t *s, ngx_file_t *f);
+static ngx_int_t ngx_rtmp_flv_seek(ngx_rtmp_session_t *s, ngx_file_t *f,
ngx_uint_t offset);
static ngx_int_t ngx_rtmp_flv_stop(ngx_rtmp_session_t *s, ngx_file_t *f);
-static ngx_int_t ngx_rtmp_flv_send(ngx_rtmp_session_t *s, ngx_file_t *f);
+static ngx_int_t ngx_rtmp_flv_send(ngx_rtmp_session_t *s, ngx_file_t *f,
+ ngx_uint_t *ts);
typedef struct {
@@ -389,7 +391,7 @@ ngx_rtmp_flv_read_meta(ngx_rtmp_session_t *s, ngx_file_t *f)
static ngx_int_t
-ngx_rtmp_flv_send(ngx_rtmp_session_t *s, ngx_file_t *f)
+ngx_rtmp_flv_send(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t *ts)
{
ngx_rtmp_flv_ctx_t *ctx;
uint32_t last_timestamp;
@@ -567,7 +569,7 @@ ngx_rtmp_flv_init(ngx_rtmp_session_t *s, ngx_file_t *f)
static ngx_int_t
-ngx_rtmp_flv_start(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t timestamp)
+ngx_rtmp_flv_start(ngx_rtmp_session_t *s, ngx_file_t *f)
{
ngx_rtmp_flv_ctx_t *ctx;
@@ -577,17 +579,36 @@ ngx_rtmp_flv_start(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t timestamp)
return NGX_OK;
}
- ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
- "flv: start timestamp=%ui", timestamp);
+ ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
+ "flv: start");
- ctx->start_timestamp = timestamp;
ctx->offset = -1;
ctx->msg_mask = 0;
return NGX_OK;
}
+static ngx_int_t
+ngx_rtmp_flv_seek(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t timestamp)
+{
+ ngx_rtmp_flv_ctx_t *ctx;
+
+ ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_flv_module);
+
+ if (ctx == NULL) {
+ return NGX_OK;
+ }
+
+ ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
+ "flv: seek timestamp=%ui", timestamp);
+
+ ctx->start_timestamp = timestamp;
+
+ return NGX_OK;
+}
+
+
static ngx_int_t
ngx_rtmp_flv_stop(ngx_rtmp_session_t *s, ngx_file_t *f)
{
@@ -635,6 +656,7 @@ ngx_rtmp_flv_postconfiguration(ngx_conf_t *cf)
fmt->init = ngx_rtmp_flv_init;
fmt->start = ngx_rtmp_flv_start;
+ fmt->seek = ngx_rtmp_flv_seek;
fmt->stop = ngx_rtmp_flv_stop;
fmt->send = ngx_rtmp_flv_send;
View
@@ -9,12 +9,15 @@
static ngx_int_t ngx_rtmp_mp4_postconfiguration(ngx_conf_t *cf);
-static ngx_int_t ngx_rtmp_mp4_init(ngx_rtmp_session_t *s, ngx_file_t *f);
-static ngx_int_t ngx_rtmp_mp4_done(ngx_rtmp_session_t *s, ngx_file_t *f);
-static ngx_int_t ngx_rtmp_mp4_start(ngx_rtmp_session_t *s, ngx_file_t *f,
- ngx_uint_t offset);
-static ngx_int_t ngx_rtmp_mp4_stop(ngx_rtmp_session_t *s, ngx_file_t *f);
-static ngx_int_t ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f);
+static ngx_int_t ngx_rtmp_mp4_init(ngx_rtmp_session_t *s, ngx_file_t *f);
+static ngx_int_t ngx_rtmp_mp4_done(ngx_rtmp_session_t *s, ngx_file_t *f);
+static ngx_int_t ngx_rtmp_mp4_start(ngx_rtmp_session_t *s, ngx_file_t *f);
+static ngx_int_t ngx_rtmp_mp4_seek(ngx_rtmp_session_t *s, ngx_file_t *f,
+ ngx_uint_t offset);
+static ngx_int_t ngx_rtmp_mp4_stop(ngx_rtmp_session_t *s, ngx_file_t *f);
+static ngx_int_t ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f,
+ ngx_uint_t *ts);
+static ngx_int_t ngx_rtmp_mp4_reset(ngx_rtmp_session_t *s);
#pragma pack(push,4)
@@ -1921,7 +1924,7 @@ ngx_rtmp_mp4_seek_track(ngx_rtmp_session_t *s, ngx_rtmp_mp4_track_t *t,
static ngx_int_t
-ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f)
+ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t *ts)
{
ngx_rtmp_mp4_ctx_t *ctx;
ngx_buf_t in_buf;
@@ -1962,6 +1965,7 @@ ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f)
sched = 0;
active = 0;
+ last_timestamp = 0;
end_timestamp = ctx->start_timestamp +
(ngx_current_msec - ctx->epoch) + buflen;
@@ -2123,7 +2127,17 @@ ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f)
return sched;
}
- return active ? NGX_OK : NGX_DONE;
+ if (active) {
+ return NGX_OK;
+ }
+
+ if (ts) {
+ *ts = last_timestamp;
+ }
+
+ /*ngx_rtmp_mp4_reset(s);*/
+
+ return NGX_DONE;
}
@@ -2227,7 +2241,7 @@ ngx_rtmp_mp4_done(ngx_rtmp_session_t *s, ngx_file_t *f)
static ngx_int_t
-ngx_rtmp_mp4_start(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t timestamp)
+ngx_rtmp_mp4_seek(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t timestamp)
{
ngx_rtmp_mp4_ctx_t *ctx;
ngx_uint_t n;
@@ -2239,7 +2253,7 @@ ngx_rtmp_mp4_start(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t timestamp)
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
- "mp4: start timestamp=%ui", timestamp);
+ "mp4: seek timestamp=%ui", timestamp);
for (n = 0; n < ctx->ntracks; ++n) {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
@@ -2248,20 +2262,73 @@ ngx_rtmp_mp4_start(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t timestamp)
ngx_rtmp_mp4_seek_track(s, &ctx->tracks[n], timestamp);
}
- ctx->epoch = ngx_current_msec;
ctx->start_timestamp = timestamp;
+ return ngx_rtmp_mp4_reset(s);
+}
+
+
+static ngx_int_t
+ngx_rtmp_mp4_start(ngx_rtmp_session_t *s, ngx_file_t *f)
+{
+ ngx_rtmp_mp4_ctx_t *ctx;
+
+ ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_mp4_module);
+
+ if (ctx == NULL) {
+ return NGX_OK;
+ }
+
+ ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
+ "mp4: start timestamp=%uD", ctx->start_timestamp);
+
+ ctx->epoch = ngx_current_msec;
+
+ return NGX_OK;/*ngx_rtmp_mp4_reset(s);*/
+}
+
+
+static ngx_int_t
+ngx_rtmp_mp4_reset(ngx_rtmp_session_t *s)
+{
+ ngx_rtmp_mp4_ctx_t *ctx;
+ ngx_rtmp_mp4_cursor_t *cr;
+ ngx_rtmp_mp4_track_t *t;
+ ngx_uint_t n;
+
+ ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_mp4_module);
+
+ if (ctx == NULL) {
+ return NGX_OK;
+ }
+
+ t = &ctx->tracks[0];
+ for (n = 0; n < ctx->ntracks; ++n, ++t) {
+ cr = &t->cursor;
+ cr->not_first = 0;
+ }
+
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_mp4_stop(ngx_rtmp_session_t *s, ngx_file_t *f)
{
- ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
- "mp4: stop");
+ ngx_rtmp_mp4_ctx_t *ctx;
- return NGX_OK;
+ ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_mp4_module);
+
+ if (ctx == NULL) {
+ return NGX_OK;
+ }
+
+ ctx->start_timestamp += (ngx_current_msec - ctx->epoch);
+
+ ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
+ "mp4: stop timestamp=%uD", ctx->start_timestamp);
+
+ return NGX_OK;/*ngx_rtmp_mp4_reset(s);*/
}
@@ -2294,6 +2361,7 @@ ngx_rtmp_mp4_postconfiguration(ngx_conf_t *cf)
fmt->init = ngx_rtmp_mp4_init;
fmt->done = ngx_rtmp_mp4_done;
+ fmt->seek = ngx_rtmp_mp4_seek;
fmt->start = ngx_rtmp_mp4_start;
fmt->stop = ngx_rtmp_mp4_stop;
fmt->send = ngx_rtmp_mp4_send;
Oops, something went wrong.

0 comments on commit 2ef9075

Please sign in to comment.