Support for keeping video in sequence when changing lengths; tie selection in timelin...
[dcpomatic.git] / src / lib / playlist.cc
index ad71f87d749bd489d2cb35106d7a353901547d25..d32ec6ba8ac911e70295d70840a1ce67347be4b7 100644 (file)
@@ -1,5 +1,3 @@
-/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
-
 /*
     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
 
@@ -51,6 +49,8 @@ using boost::lexical_cast;
 
 Playlist::Playlist ()
        : _loop (1)
+       , _sequence_video (true)
+       , _sequencing_video (false)
 {
 
 }
@@ -72,6 +72,26 @@ Playlist::~Playlist ()
 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);
 }
 
@@ -122,7 +142,7 @@ Playlist::video_digest () const
 
 /** @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 ("Content");
        for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
@@ -131,15 +151,19 @@ Playlist::set_from_xml (shared_ptr<const cxml::Node> node)
                boost::shared_ptr<Content> content;
 
                if (type == "FFmpeg") {
-                       content.reset (new FFmpegContent (*i));
+                       content.reset (new FFmpegContent (film, *i));
                } else if (type == "ImageMagick") {
-                       content.reset (new ImageMagickContent (*i));
+                       content.reset (new ImageMagickContent (film, *i));
                } else if (type == "Sndfile") {
-                       content.reset (new SndfileContent (*i));
+                       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 */
@@ -151,6 +175,7 @@ Playlist::as_xml (xmlpp::Node* node)
        }
 
        node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
+       node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
 }
 
 void
@@ -258,12 +283,11 @@ Playlist::best_dcp_frame_rate () const
 }
 
 Time
-Playlist::length (shared_ptr<const Film> film) const
+Playlist::length () const
 {
        Time len = 0;
        for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
-               Time const t = (*i)->time() + (*i)->length (film);
-               len = max (len, t);
+               len = max (len, (*i)->end ());
        }
 
        return len;
@@ -283,3 +307,27 @@ Playlist::reconnect ()
        }
 }
 
+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();
+}