[nginx] Mp4: skip tracks shorter than seek position (ticket #414).

Roman Arutyunyan arut at nginx.com
Wed Jan 29 09:35:49 UTC 2014


details:   http://hg.nginx.org/nginx/rev/5730c0193842
branches:  
changeset: 5543:5730c0193842
user:      Roman Arutyunyan <arut at nginx.com>
date:      Wed Jan 29 13:33:45 2014 +0400
description:
Mp4: skip tracks shorter than seek position (ticket #414).

Mp4 module does not check movie and track durations when reading
file.  Instead it generates errors when track metadata is shorter
than seek position.  Now such tracks are skipped and movie duration
check is performed at file read stage.

diffstat:

 src/http/modules/ngx_http_mp4_module.c |  37 ++++++++++++++++++++++++++++-----
 1 files changed, 31 insertions(+), 6 deletions(-)

diffs (82 lines):

diff -r 847c308917af -r 5730c0193842 src/http/modules/ngx_http_mp4_module.c
--- a/src/http/modules/ngx_http_mp4_module.c	Wed Jan 29 13:30:36 2014 +0400
+++ b/src/http/modules/ngx_http_mp4_module.c	Wed Jan 29 13:33:45 2014 +0400
@@ -1208,7 +1208,7 @@ ngx_http_mp4_read_mvhd_atom(ngx_http_mp4
     u_char                 *atom_header;
     size_t                  atom_size;
     uint32_t                timescale;
-    uint64_t                duration;
+    uint64_t                duration, start_time;
     ngx_buf_t              *atom;
     ngx_mp4_mvhd_atom_t    *mvhd_atom;
     ngx_mp4_mvhd64_atom_t  *mvhd64_atom;
@@ -1251,7 +1251,16 @@ ngx_http_mp4_read_mvhd_atom(ngx_http_mp4
                    "mvhd timescale:%uD, duration:%uL, time:%.3fs",
                    timescale, duration, (double) duration / timescale);
 
-    duration -= (uint64_t) mp4->start * timescale / 1000;
+    start_time = (uint64_t) mp4->start * timescale / 1000;
+
+    if (duration < start_time) {
+        ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
+                      "\"%s\" mp4 start time exceeds file duration",
+                      mp4->file.name.data);
+        return NGX_ERROR;
+    }
+
+    duration -= start_time;
 
     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
                    "mvhd new duration:%uL, time:%.3fs",
@@ -1398,7 +1407,7 @@ ngx_http_mp4_read_tkhd_atom(ngx_http_mp4
 {
     u_char                 *atom_header;
     size_t                  atom_size;
-    uint64_t                duration;
+    uint64_t                duration, start_time;
     ngx_buf_t              *atom;
     ngx_http_mp4_trak_t    *trak;
     ngx_mp4_tkhd_atom_t    *tkhd_atom;
@@ -1438,7 +1447,15 @@ ngx_http_mp4_read_tkhd_atom(ngx_http_mp4
                    "tkhd duration:%uL, time:%.3fs",
                    duration, (double) duration / mp4->timescale);
 
-    duration -= (uint64_t) mp4->start * mp4->timescale / 1000;
+    start_time = (uint64_t) mp4->start * mp4->timescale / 1000;
+
+    if (duration < start_time) {
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+                       "tkhd duration is less than start time");
+        return NGX_DECLINED;
+    }
+
+    duration -= start_time;
 
     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
                    "tkhd new duration:%uL, time:%.3fs",
@@ -1541,7 +1558,7 @@ ngx_http_mp4_read_mdhd_atom(ngx_http_mp4
     u_char                 *atom_header;
     size_t                  atom_size;
     uint32_t                timescale;
-    uint64_t                duration;
+    uint64_t                duration, start_time;
     ngx_buf_t              *atom;
     ngx_http_mp4_trak_t    *trak;
     ngx_mp4_mdhd_atom_t    *mdhd_atom;
@@ -1583,7 +1600,15 @@ ngx_http_mp4_read_mdhd_atom(ngx_http_mp4
                    "mdhd timescale:%uD, duration:%uL, time:%.3fs",
                    timescale, duration, (double) duration / timescale);
 
-    duration -= (uint64_t) mp4->start * timescale / 1000;
+    start_time = (uint64_t) mp4->start * timescale / 1000;
+
+    if (duration < start_time) {
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+                       "mdhd duration is less than start time");
+        return NGX_DECLINED;
+    }
+
+    duration -= start_time;
 
     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
                    "mdhd new duration:%uL, time:%.3fs",



More information about the nginx-devel mailing list