Support for keeping video in sequence when changing lengths; tie selection in timelin...
[dcpomatic.git] / src / lib / playlist.cc
index 8f4a35ac2790d51c7d918ca1bbbdc80ee35bb492..d32ec6ba8ac911e70295d70840a1ce67347be4b7 100644 (file)
@@ -49,6 +49,8 @@ using boost::lexical_cast;
 
 Playlist::Playlist ()
        : _loop (1)
+       , _sequence_video (true)
+       , _sequencing_video (false)
 {
 
 }
@@ -56,14 +58,40 @@ Playlist::Playlist ()
 Playlist::Playlist (shared_ptr<const Playlist> other)
        : _loop (other->_loop)
 {
-       for (RegionList::const_iterator i = other->_regions.begin(); i != other->_regions.end(); ++i) {
-               _regions.push_back (Region (i->content->clone(), i->time, this));
+       for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) {
+               _content.push_back ((*i)->clone ());
        }
 }
 
+Playlist::~Playlist ()
+{
+       _content.clear ();
+       reconnect ();
+}
+
 void
 Playlist::content_changed (weak_ptr<Content> c, int p)
 {
+       if (p == ContentProperty::LENGTH && _sequence_video && !_sequencing_video) {
+               cout << "sequencing.\n";
+               _sequencing_video = true;
+
+               ContentList cl = _content;
+               sort (cl.begin(), cl.end(), ContentSorter ());
+               Time last = 0;
+               for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
+                       if (!dynamic_pointer_cast<VideoContent> (*i)) {
+                               continue;
+                       }
+
+                       (*i)->set_start (last);
+                       cout << (*i)->file() << " -> " << last << "\n";
+                       last = (*i)->end ();
+               }
+
+               _sequencing_video = false;
+       }
+       
        ContentChanged (c, p);
 }
 
@@ -72,14 +100,14 @@ Playlist::audio_digest () const
 {
        string t;
        
-       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
-               if (!dynamic_pointer_cast<const AudioContent> (i->content)) {
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               if (!dynamic_pointer_cast<const AudioContent> (*i)) {
                        continue;
                }
                
-               t += i->content->digest ();
+               t += (*i)->digest ();
 
-               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
+               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
                if (fc) {
                        t += lexical_cast<string> (fc->audio_stream()->id);
                }
@@ -95,13 +123,13 @@ Playlist::video_digest () const
 {
        string t;
        
-       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
-               if (!dynamic_pointer_cast<const VideoContent> (i->content)) {
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               if (!dynamic_pointer_cast<const VideoContent> (*i)) {
                        continue;
                }
                
-               t += i->content->digest ();
-               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
+               t += (*i)->digest ();
+               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
                if (fc && fc->subtitle_stream()) {
                        t += fc->subtitle_stream()->id;
                }
@@ -112,44 +140,62 @@ Playlist::video_digest () const
        return md5_digest (t.c_str(), t.length());
 }
 
+/** @param node <Playlist> node */
 void
-Playlist::set_from_xml (shared_ptr<const cxml::Node> node)
+Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
 {
-       list<shared_ptr<cxml::Node> > c = node->node_children ("Region");
+       list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
        for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
-               _regions.push_back (Region (*i, this));
+               string const type = (*i)->string_child ("Type");
+
+               boost::shared_ptr<Content> content;
+
+               if (type == "FFmpeg") {
+                       content.reset (new FFmpegContent (film, *i));
+               } else if (type == "ImageMagick") {
+                       content.reset (new ImageMagickContent (film, *i));
+               } else if (type == "Sndfile") {
+                       content.reset (new SndfileContent (film, *i));
+               }
+
+               _content.push_back (content);
        }
 
+       reconnect ();
        _loop = node->number_child<int> ("Loop");
+       _sequence_video = node->bool_child ("SequenceVideo");
 }
 
+/** @param node <Playlist> node */
 void
 Playlist::as_xml (xmlpp::Node* node)
 {
-       for (RegionList::iterator i = _regions.begin(); i != _regions.end(); ++i) {
-               i->as_xml (node->add_child ("Region"));
+       for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+               (*i)->as_xml (node->add_child ("Content"));
        }
 
        node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
+       node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
 }
 
 void
 Playlist::add (shared_ptr<Content> c)
 {
-       _regions.push_back (Region (c, 0, this));
+       _content.push_back (c);
+       reconnect ();
        Changed ();
 }
 
 void
 Playlist::remove (shared_ptr<Content> c)
 {
-       RegionList::iterator i = _regions.begin ();
-       while (i != _regions.end() && i->content != c) {
+       ContentList::iterator i = _content.begin ();
+       while (i != _content.end() && *i != c) {
                ++i;
        }
        
-       if (i != _regions.end ()) {
-               _regions.erase (i);
+       if (i != _content.end ()) {
+               _content.erase (i);
                Changed ();
        }
 }
@@ -164,8 +210,8 @@ Playlist::set_loop (int l)
 bool
 Playlist::has_subtitles () const
 {
-       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
-               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (i->content);
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
                if (fc && !fc->subtitle_streams().empty()) {
                        return true;
                }
@@ -174,38 +220,6 @@ Playlist::has_subtitles () const
        return false;
 }
 
-Playlist::Region::Region (shared_ptr<Content> c, Time t, Playlist* p)
-       : content (c)
-       , time (t)
-{
-       connection = c->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
-}
-
-Playlist::Region::Region (shared_ptr<const cxml::Node> node, Playlist* p)
-{
-       shared_ptr<const cxml::Node> content_node = node->node_child ("Content");
-       string const type = content_node->string_child ("Type");
-
-       if (type == "FFmpeg") {
-               content.reset (new FFmpegContent (content_node));
-       } else if (type == "ImageMagick") {
-               content.reset (new ImageMagickContent (content_node));
-       } else if (type == "Sndfile") {
-               content.reset (new SndfileContent (content_node));
-       }
-
-       time = node->number_child<Time> ("Time");
-       connection = content->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
-}
-
-void
-Playlist::Region::as_xml (xmlpp::Node* node) const
-{
-       xmlpp::Node* sub = node->add_child ("Content");
-       content->as_xml (sub);
-       sub->add_child ("Time")->add_child_text (lexical_cast<string> (time));
-}
-
 class FrameRateCandidate
 {
 public:
@@ -244,8 +258,8 @@ Playlist::best_dcp_frame_rate () const
        while (i != candidates.end()) {
 
                float this_error = std::numeric_limits<float>::max ();
-               for (RegionList::const_iterator j = _regions.begin(); j != _regions.end(); ++j) {
-                       shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j->content);
+               for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
+                       shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
                        if (!vc) {
                                continue;
                        }
@@ -269,13 +283,51 @@ Playlist::best_dcp_frame_rate () const
 }
 
 Time
-Playlist::length (shared_ptr<const Film> film) const
+Playlist::length () const
 {
        Time len = 0;
-       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
-               Time const t = i->time + i->content->length (film);
-               len = max (len, t);
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               len = max (len, (*i)->end ());
        }
 
        return len;
 }
+
+void
+Playlist::reconnect ()
+{
+       for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
+               i->disconnect ();
+       }
+
+       _content_connections.clear ();
+               
+       for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+               _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
+       }
+}
+
+Time
+Playlist::video_end () const
+{
+       Time end = 0;
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               if (dynamic_pointer_cast<const VideoContent> (*i)) {
+                       end = max (end, (*i)->end ());
+               }
+       }
+
+       return end;
+}
+
+void
+Playlist::set_sequence_video (bool s)
+{
+       _sequence_video = s;
+}
+
+bool
+ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
+{
+       return a->start() < b->start();
+}