demux: dash: add Timeline merging
authorFrancois Cartegnie <fcvlcdev@free.fr>
Thu, 8 Jan 2015 19:00:12 +0000 (20:00 +0100)
committerFrancois Cartegnie <fcvlcdev@free.fr>
Mon, 12 Jan 2015 19:21:57 +0000 (20:21 +0100)
modules/demux/dash/mpd/SegmentTimeline.cpp
modules/demux/dash/mpd/SegmentTimeline.h

index 64bdb0a..d7140c0 100644 (file)
@@ -128,6 +128,54 @@ size_t SegmentTimeline::prune(mtime_t scaled)
     return prunednow;
 }
 
+void SegmentTimeline::mergeWith(SegmentTimeline &other)
+{
+    if(elements.empty())
+    {
+        while(other.elements.size())
+        {
+            elements.push_back(other.elements.front());
+            other.elements.pop_front();
+        }
+        return;
+    }
+
+    Element *last = elements.back();
+    while(other.elements.size())
+    {
+        Element *el = other.elements.front();
+        other.elements.pop_front();
+        if(el->t == last->t) /* Same element, but prev could have been middle of repeat */
+        {
+            last->r = std::max(last->r, el->r);
+            delete el;
+        }
+        else if(el->t > last->t) /* Did not exist in previous list */
+        {
+            if( el->t - last->t >= last->d * (mtime_t)(last->r + 1) )
+            {
+                elements.push_back(el);
+                last = el;
+            }
+            else if(last->d == el->d) /* should always be in that case */
+            {
+                last->r = ((el->t - last->t) / last->d) - 1;
+                elements.push_back(el);
+                last = el;
+            }
+            else
+            {
+                /* borked: skip */
+                delete el;
+            }
+        }
+        else
+        {
+            delete el;
+        }
+    }
+}
+
 SegmentTimeline::Element::Element(mtime_t d_, uint64_t r_, mtime_t t_)
 {
     d = d_;
index 46f87a7..fec2771 100644 (file)
@@ -42,6 +42,7 @@ namespace dash
                 uint64_t getElementNumberByScaledPlaybackTime(time_t) const;
                 mtime_t getScaledPlaybackTimeByElementNumber(uint64_t) const;
                 size_t prune(mtime_t);
+                void mergeWith(SegmentTimeline &);
 
             private:
                 std::list<Element *> elements;