diff mbox

[FFmpeg-devel] Fix for ticket 6658 (Dash demuxer segfault)

Message ID DM5PR22MB0681C3992E06C12F0EDF276FFE290@DM5PR22MB0681.namprd22.prod.outlook.com
State Superseded
Headers show

Commit Message

Colin NG Nov. 15, 2017, 2:54 a.m. UTC
- Add a function to handle the base URL Processing described in section 5.6.5 of IEC_23009-1.

- Fix for downloading dash content with byte range info

Comments

Carl Eugen Hoyos Nov. 15, 2017, 3:54 p.m. UTC | #1
2017-11-15 3:54 GMT+01:00 Colin NG <colin_ng@hotmail.com>:
> - Add a function to handle the base URL Processing described in section 5.6.5 of IEC_23009-1.
>
> - Fix for downloading dash content with byte range info

As said: If these are two separate issues, please send two patches.

Use tools/patcheck to check your patch for many style issues.
(No Changelog entry necessary.)

Try to avoid if ((var1 = var2) > 1), instead add an additional line.

Carl Eugen
Colin NG Nov. 15, 2017, 3:58 p.m. UTC | #2
Have submitted already for the byte range issue in another email with title "Download dash content with byte range info"
diff mbox

Patch

diff --git a/libavformat/dashdec.c b/libavformat/dashdec.c
index 0e3afd2..0be8a49 100644
--- a/libavformat/dashdec.c
+++ b/libavformat/dashdec.c
@@ -148,6 +148,10 @@  static uint64_t get_current_time_in_sec(void)
     return  av_gettime() / 1000000;
 }
 
+static int ishttp(char *url) {
+    return (av_strstart(url, "http://", NULL) || av_strstart(url, "https://", NULL)) ;
+}
+
 static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
 {
     struct tm timeinfo;
@@ -392,7 +396,10 @@  static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
     else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
         return AVERROR_INVALIDDATA;
 
-    ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
+    if (pb) av_freep(pb);
+    set_httpheader_options(c, &tmp);
+    ret = avio_open2(pb, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
+
     if (ret >= 0) {
         // update cookies on http response with setcookies.
         char *new_cookies = NULL;
@@ -522,6 +529,22 @@  static enum AVMediaType get_content_type(xmlNodePtr node)
     return type;
 }
 
+static struct fragment * getFragment(char *range)
+{
+    struct fragment * seg =  av_mallocz(sizeof(struct fragment));
+
+    memset(seg, 0, sizeof(struct fragment));
+    seg->size = -1;
+    if (range)  {
+        char *str_end_offset;
+        char *str_offset = av_strtok(range, "-", &str_end_offset);
+        seg->url_offset = strtoll(str_offset, NULL, 10);
+        seg->size = strtoll(str_end_offset, NULL, 10) -seg->url_offset;
+    }
+
+    return seg;
+}
+
 static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep,
                                          xmlNodePtr fragmenturl_node,
                                          xmlNodePtr *baseurl_nodes,
@@ -530,11 +553,13 @@  static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representati
 {
     char *initialization_val = NULL;
     char *media_val = NULL;
+    char *range_val = NULL;
 
     if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
         initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
-        if (initialization_val) {
-            rep->init_section = av_mallocz(sizeof(struct fragment));
+        range_val = xmlGetProp(fragmenturl_node, "range");
+        if (initialization_val || range_val) {
+            rep->init_section = getFragment(range_val);// byte range on
             if (!rep->init_section) {
                 xmlFree(initialization_val);
                 return AVERROR(ENOMEM);
@@ -550,11 +575,13 @@  static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representati
             }
             rep->init_section->size = -1;
             xmlFree(initialization_val);
+            xmlFree(range_val);
         }
     } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
         media_val = xmlGetProp(fragmenturl_node, "media");
-        if (media_val) {
-            struct fragment *seg = av_mallocz(sizeof(struct fragment));
+        range_val = xmlGetProp(fragmenturl_node, "mediaRange");
+        if (media_val || range_val) {
+            struct fragment *seg =  getFragment(range_val);// byte range on
             if (!seg) {
                 xmlFree(media_val);
                 return AVERROR(ENOMEM);
@@ -571,6 +598,7 @@  static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representati
             seg->size = -1;
             dynarray_add(&rep->fragments, &rep->n_fragments, seg);
             xmlFree(media_val);
+            xmlFree(range_val);
         }
     }
 
@@ -613,6 +641,86 @@  static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representat
     return 0;
 }
 
+
+static int resolve_content_path(AVFormatContext *s, const char *url,  xmlNodePtr *baseurl_nodes,  int n_baseurl_nodes) {
+
+    int i;
+    char *text;
+    char tmp_str[MAX_URL_SIZE];
+    char tmp_str_2[MAX_URL_SIZE];
+
+    char *path = av_mallocz(MAX_URL_SIZE);
+    int nameSize = 0;
+    int updated = 0;
+
+    av_strlcpy(tmp_str, url, strlen(url)+1);
+    char *mpdName = strtok (tmp_str," /");
+
+    while ((mpdName =strtok (NULL, "/")) != NULL)
+    {
+        nameSize = strlen(mpdName);
+    }
+
+    memset(path, 0, MAX_URL_SIZE* sizeof(char));
+    av_strlcpy (path, url, strlen(url)-nameSize+1 );
+
+    int rootId = 0;
+    xmlNodePtr  *node = NULL;
+    for (rootId = n_baseurl_nodes-1; rootId >0; rootId--)
+    {
+        if ((node = baseurl_nodes[rootId])== NULL) continue;
+
+        if (ishttp(xmlNodeGetContent(node)) == TRUE)  {
+            break;
+        }
+    }
+
+    node = baseurl_nodes[rootId];
+    char *baseurl = xmlNodeGetContent(node);
+    char *root_url = (!av_strcasecmp(baseurl, ""))? path: baseurl;
+
+    if (node)  {
+        xmlNodeSetContent(node, root_url);
+    }
+
+    int size = strlen(root_url);
+    int isRootHttp= ishttp(root_url);
+
+    char token ='/';
+    if (strncmp(&root_url[size-1],&token, 1) != 0)
+    {
+        strcat(root_url, "/");
+        size++;
+    }
+
+    for (i = 0; i < n_baseurl_nodes; ++i)
+    {
+        if (i==rootId) continue;
+        text = xmlNodeGetContent(baseurl_nodes[i]);
+        if (text)
+        {
+            memset(tmp_str, 0, sizeof(tmp_str));
+
+            if (ishttp(text) == FALSE && isRootHttp == TRUE)
+            {
+                av_strlcpy(tmp_str, root_url, size+1);
+            }
+            if (text)
+            {
+                int start = (strncmp(text, &token, 1) == 0) ? 1: 0;
+                memset(tmp_str_2, 0, sizeof(tmp_str_2));
+                av_strlcat(tmp_str, text+start, MAX_URL_SIZE);
+                xmlFree(text);
+            }
+            xmlNodeSetContent(baseurl_nodes[i], tmp_str);
+            updated = 1;
+        }
+    }
+
+    av_free(path);
+    return updated;
+
+}
 static int parse_manifest_representation(AVFormatContext *s, const char *url,
                                          xmlNodePtr node,
                                          xmlNodePtr adaptionset_node,
@@ -672,6 +780,8 @@  static int parse_manifest_representation(AVFormatContext *s, const char *url,
         baseurl_nodes[2] = adaptionset_baseurl_node;
         baseurl_nodes[3] = representation_baseurl_node;
 
+        resolve_content_path(s, url, baseurl_nodes, 4);
+
         if (representation_segmenttemplate_node || fragment_template_node) {
             fragment_timeline_node = NULL;
             fragment_templates_tab[0] = representation_segmenttemplate_node;
@@ -966,7 +1076,12 @@  static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
             xmlFree(val);
         }
 
-        mpd_baseurl_node = find_child_node_by_name(node, "BaseURL");
+
+        if ((mpd_baseurl_node = find_child_node_by_name(node, "BaseURL")) == NULL)
+        {
+            mpd_baseurl_node = xmlNewNode(node, "BaseURL");
+         }
+
 
         // at now we can handle only one period, with the longest duration
         node = xmlFirstElementChild(node);