Protect playlist with a mutex so that we can add content safely from e.g. examine...
[dcpomatic.git] / src / lib / playlist.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <libcxml/cxml.h>
21 #include <boost/shared_ptr.hpp>
22 #include <boost/lexical_cast.hpp>
23 #include "playlist.h"
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"
32 #include "job.h"
33 #include "config.h"
34 #include "util.h"
35
36 #include "i18n.h"
37
38 using std::list;
39 using std::cout;
40 using std::vector;
41 using std::min;
42 using std::max;
43 using std::string;
44 using std::stringstream;
45 using std::pair;
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;
51
52 Playlist::Playlist ()
53         : _sequence_video (true)
54         , _sequencing_video (false)
55 {
56
57 }
58
59 Playlist::~Playlist ()
60 {
61         _content.clear ();
62         reconnect ();
63 }
64
65 void
66 Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
67 {
68         if (property == ContentProperty::LENGTH) {
69                 maybe_sequence_video ();
70         }
71         
72         ContentChanged (content, property, frequent);
73 }
74
75 void
76 Playlist::maybe_sequence_video ()
77 {
78         boost::mutex::scoped_lock lm (_mutex);
79         
80         if (!_sequence_video || _sequencing_video) {
81                 return;
82         }
83         
84         _sequencing_video = true;
85         
86         ContentList cl = _content;
87         sort (cl.begin(), cl.end(), ContentSorter ());
88         Time last = 0;
89         for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
90                 if (!dynamic_pointer_cast<VideoContent> (*i)) {
91                         continue;
92                 }
93                 
94                 (*i)->set_position (last);
95                 last = (*i)->end ();
96         }
97         
98         _sequencing_video = false;
99 }
100
101 string
102 Playlist::video_identifier () const
103 {
104         boost::mutex::scoped_lock lm (_mutex);
105         
106         string t;
107         
108         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
109                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
110                 if (vc) {
111                         t += vc->identifier ();
112                 }
113         }
114
115         return md5_digest (t.c_str(), t.length());
116 }
117
118 /** @param node <Playlist> node */
119 void
120 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
121 {
122         boost::mutex::scoped_lock lm (_mutex);
123         
124         list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
125         for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
126                 _content.push_back (content_factory (film, *i));
127         }
128
129         reconnect ();
130 }
131
132 /** @param node <Playlist> node */
133 void
134 Playlist::as_xml (xmlpp::Node* node)
135 {
136         boost::mutex::scoped_lock lm (_mutex);
137         
138         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
139                 (*i)->as_xml (node->add_child ("Content"));
140         }
141 }
142
143 void
144 Playlist::add (shared_ptr<Content> c)
145 {
146         {
147                 boost::mutex::scoped_lock lm (_mutex);
148                 _content.push_back (c);
149                 reconnect ();
150         }
151         
152         Changed ();
153 }
154
155 void
156 Playlist::remove (shared_ptr<Content> c)
157 {
158         bool changed = false;
159         
160         {
161                 boost::mutex::scoped_lock lm (_mutex);
162                 ContentList::iterator i = _content.begin ();
163                 while (i != _content.end() && *i != c) {
164                         ++i;
165                 }
166                 
167                 if (i != _content.end ()) {
168                         _content.erase (i);
169                         reconnect ();
170                         changed = true;
171                 }
172         }
173
174         if (changed) {
175                 Changed ();
176         }
177 }
178
179 void
180 Playlist::remove (ContentList c)
181 {
182         bool changed = false;
183
184         {
185                 boost::mutex::scoped_lock lm (_mutex);
186                 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
187                         ContentList::iterator j = _content.begin ();
188                         while (j != _content.end() && *j != *i) {
189                                 ++j;
190                         }
191                         
192                         if (j != _content.end ()) {
193                                 _content.erase (j);
194                                 changed = true;
195                         }
196                 }
197
198                 if (changed) {
199                         reconnect ();
200                 }
201         }
202
203         if (changed) {
204                 Changed ();
205         }
206 }
207
208 bool
209 Playlist::has_subtitles () const
210 {
211         boost::mutex::scoped_lock lm (_mutex);
212         
213         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
214                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
215                 if (fc && !fc->subtitle_streams().empty()) {
216                         return true;
217                 }
218         }
219
220         return false;
221 }
222
223 class FrameRateCandidate
224 {
225 public:
226         FrameRateCandidate (float source_, int dcp_)
227                 : source (source_)
228                 , dcp (dcp_)
229         {}
230
231         float source;
232         int dcp;
233 };
234
235 int
236 Playlist::best_dcp_frame_rate () const
237 {
238         boost::mutex::scoped_lock lm (_mutex);
239         
240         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
241
242         /* Work out what rates we could manage, including those achieved by using skip / repeat. */
243         list<FrameRateCandidate> candidates;
244
245         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
246         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
247                 candidates.push_back (FrameRateCandidate (*i, *i));
248         }
249
250         /* Then the skip/repeat ones */
251         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
252                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
253                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
254         }
255
256         /* Pick the best one */
257         float error = std::numeric_limits<float>::max ();
258         optional<FrameRateCandidate> best;
259         list<FrameRateCandidate>::iterator i = candidates.begin();
260         while (i != candidates.end()) {
261
262                 float this_error = 0;
263                 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
264                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
265                         if (!vc) {
266                                 continue;
267                         }
268
269                         /* Use the largest difference between DCP and source as the "error" */
270                         this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
271                 }
272
273                 if (this_error < error) {
274                         error = this_error;
275                         best = *i;
276                 }
277
278                 ++i;
279         }
280
281         if (!best) {
282                 return 24;
283         }
284         
285         return best->dcp;
286 }
287
288 Time
289 Playlist::length () const
290 {
291         boost::mutex::scoped_lock lm (_mutex);
292         
293         Time len = 0;
294         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
295                 len = max (len, (*i)->end ());
296         }
297
298         return len;
299 }
300
301 /* Caller must hold a lock on _mutex */
302 void
303 Playlist::reconnect ()
304 {
305         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
306                 i->disconnect ();
307         }
308
309         _content_connections.clear ();
310                 
311         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
312                 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
313         }
314 }
315
316 Time
317 Playlist::video_end () const
318 {
319         boost::mutex::scoped_lock lm (_mutex);
320         
321         Time end = 0;
322         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
323                 if (dynamic_pointer_cast<const VideoContent> (*i)) {
324                         end = max (end, (*i)->end ());
325                 }
326         }
327
328         return end;
329 }
330
331 void
332 Playlist::set_sequence_video (bool s)
333 {
334         boost::mutex::scoped_lock lm (_mutex);
335         _sequence_video = s;
336 }
337
338 bool
339 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
340 {
341         return a->position() < b->position();
342 }
343
344 /** @return content in an undefined order */
345 ContentList
346 Playlist::content () const
347 {
348         boost::mutex::scoped_lock lm (_mutex);
349         return _content;
350 }
351
352 void
353 Playlist::repeat (ContentList c, int n)
354 {
355         {
356                 boost::mutex::scoped_lock lm (_mutex);
357                 pair<Time, Time> range (TIME_MAX, 0);
358                 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
359                         range.first = min (range.first, (*i)->position ());
360                         range.second = max (range.second, (*i)->position ());
361                         range.first = min (range.first, (*i)->end ());
362                         range.second = max (range.second, (*i)->end ());
363                 }
364                 
365                 Time pos = range.second;
366                 for (int i = 0; i < n; ++i) {
367                         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
368                                 shared_ptr<Content> copy = (*i)->clone ();
369                                 copy->set_position (pos + copy->position() - range.first);
370                                 _content.push_back (copy);
371                         }
372                         pos += range.second - range.first;
373                 }
374                 
375                 reconnect ();
376         }
377         
378         Changed ();
379 }