2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <libcxml/cxml.h>
21 #include <boost/shared_ptr.hpp>
22 #include <boost/lexical_cast.hpp>
24 #include "sndfile_content.h"
25 #include "sndfile_decoder.h"
26 #include "video_content.h"
27 #include "ffmpeg_decoder.h"
28 #include "ffmpeg_content.h"
29 #include "still_image_decoder.h"
30 #include "still_image_content.h"
31 #include "content_factory.h"
44 using std::stringstream;
46 using boost::optional;
47 using boost::shared_ptr;
48 using boost::weak_ptr;
49 using boost::dynamic_pointer_cast;
50 using boost::lexical_cast;
53 : _sequence_video (true)
54 , _sequencing_video (false)
59 Playlist::~Playlist ()
66 Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
68 if (property == ContentProperty::LENGTH) {
69 maybe_sequence_video ();
72 ContentChanged (content, property, frequent);
77 Playlist::maybe_sequence_video ()
79 if (!_sequence_video || _sequencing_video) {
83 _sequencing_video = true;
85 ContentList cl = _content;
86 sort (cl.begin(), cl.end(), ContentSorter ());
88 for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
89 if (!dynamic_pointer_cast<VideoContent> (*i)) {
93 (*i)->set_start (last);
97 _sequencing_video = false;
101 Playlist::video_identifier () const
105 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
106 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
108 t += vc->identifier ();
112 return md5_digest (t.c_str(), t.length());
115 /** @param node <Playlist> node */
117 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
119 list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
120 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
121 _content.push_back (content_factory (film, *i));
127 /** @param node <Playlist> node */
129 Playlist::as_xml (xmlpp::Node* node)
131 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
132 (*i)->as_xml (node->add_child ("Content"));
137 Playlist::add (shared_ptr<Content> c)
139 _content.push_back (c);
145 Playlist::remove (shared_ptr<Content> c)
147 ContentList::iterator i = _content.begin ();
148 while (i != _content.end() && *i != c) {
152 if (i != _content.end ()) {
159 Playlist::remove (ContentList c)
161 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
162 ContentList::iterator j = _content.begin ();
163 while (j != _content.end() && *j != *i) {
167 if (j != _content.end ()) {
176 Playlist::has_subtitles () const
178 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
179 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
180 if (fc && !fc->subtitle_streams().empty()) {
188 class FrameRateCandidate
191 FrameRateCandidate (float source_, int dcp_)
201 Playlist::best_dcp_frame_rate () const
203 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
205 /* Work out what rates we could manage, including those achieved by using skip / repeat. */
206 list<FrameRateCandidate> candidates;
208 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
209 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
210 candidates.push_back (FrameRateCandidate (*i, *i));
213 /* Then the skip/repeat ones */
214 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
215 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
216 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
219 /* Pick the best one */
220 float error = std::numeric_limits<float>::max ();
221 optional<FrameRateCandidate> best;
222 list<FrameRateCandidate>::iterator i = candidates.begin();
223 while (i != candidates.end()) {
225 float this_error = 0;
226 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
227 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
232 /* Use the largest difference between DCP and source as the "error" */
233 this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
236 if (this_error < error) {
252 Playlist::length () const
255 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
256 len = max (len, (*i)->end ());
263 Playlist::reconnect ()
265 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
269 _content_connections.clear ();
271 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
272 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
277 Playlist::video_end () const
280 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
281 if (dynamic_pointer_cast<const VideoContent> (*i)) {
282 end = max (end, (*i)->end ());
290 Playlist::set_sequence_video (bool s)
296 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
298 return a->start() < b->start();
301 /** @return content in an undefined order */
303 Playlist::content () const
309 Playlist::repeat (ContentList c, int n)
311 pair<Time, Time> range (TIME_MAX, 0);
312 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
313 range.first = min (range.first, (*i)->start ());
314 range.second = max (range.second, (*i)->start ());
315 range.first = min (range.first, (*i)->end ());
316 range.second = max (range.second, (*i)->end ());
319 Time pos = range.second;
320 for (int i = 0; i < n; ++i) {
321 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
322 shared_ptr<Content> copy = (*i)->clone ();
323 copy->set_start (pos + copy->start() - range.first);
324 _content.push_back (copy);
326 pos += range.second - range.first;