Fix mis-merge.
[dcpomatic.git] / src / lib / playlist.cc
index 912d90f0e5f482d19404101e282e713a1268b48e..1535ad61f1809a8e47ba1a47a2b966593cfbfdc0 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)
 {
 
 }
@@ -58,54 +58,61 @@ 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) {
+               maybe_sequence_video ();
+       }
+       
        ContentChanged (c, p);
 }
 
-string
-Playlist::audio_digest () const
+
+void
+Playlist::maybe_sequence_video ()
 {
-       string t;
+       if (!_sequence_video || _sequencing_video) {
+               return;
+       }
+       
+       _sequencing_video = true;
        
-       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
-               if (!dynamic_pointer_cast<const AudioContent> (i->content)) {
+       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;
                }
                
-               t += i->content->digest ();
-
-               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
-               if (fc) {
-                       t += lexical_cast<string> (fc->audio_stream()->id);
-               }
+               (*i)->set_start (last);
+               last = (*i)->end ();
        }
-
-       t += lexical_cast<string> (_loop);
-
-       return md5_digest (t.c_str(), t.length());
+       
+       _sequencing_video = false;
 }
 
 string
-Playlist::video_digest () const
+Playlist::video_identifier () const
 {
        string t;
        
-       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
-               if (!dynamic_pointer_cast<const VideoContent> (i->content)) {
-                       continue;
-               }
-               
-               t += i->content->digest ();
-               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
-               if (fc && fc->subtitle_stream()) {
-                       t += fc->subtitle_stream()->id;
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
+               if (vc) {
+                       t += vc->identifier ();
                }
        }
 
@@ -114,44 +121,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 ();
        }
 }
@@ -166,8 +191,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;
                }
@@ -176,38 +201,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);
-       node->add_child ("Time")->add_child_text (lexical_cast<string> (time));
-}
-
 class FrameRateCandidate
 {
 public:
@@ -239,20 +232,21 @@ Playlist::best_dcp_frame_rate () const
                candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
        }
 
-       /* Pick the best one, bailing early if we hit an exact match */
+       /* Pick the best one */
        float error = std::numeric_limits<float>::max ();
        optional<FrameRateCandidate> best;
        list<FrameRateCandidate>::iterator i = candidates.begin();
        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);
+               float this_error = 0;
+               for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
+                       shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
                        if (!vc) {
                                continue;
                        }
 
-                       this_error += fabs (i->source - vc->video_frame_rate ());
+                       /* Use the largest difference between DCP and source as the "error" */
+                       this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
                }
 
                if (this_error < error) {
@@ -271,13 +265,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();
+}