Various bits and pieces.
[dcpomatic.git] / src / lib / film.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include <stdexcept>
23 #include <iostream>
24 #include <algorithm>
25 #include <fstream>
26 #include <cstdlib>
27 #include <sstream>
28 #include <iomanip>
29 #include <unistd.h>
30 #include <boost/filesystem.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <boost/lexical_cast.hpp>
33 #include <boost/date_time.hpp>
34 #include <libxml++/libxml++.h>
35 #include <libcxml/cxml.h>
36 #include "film.h"
37 #include "container.h"
38 #include "job.h"
39 #include "filter.h"
40 #include "util.h"
41 #include "job_manager.h"
42 #include "ab_transcode_job.h"
43 #include "transcode_job.h"
44 #include "scp_dcp_job.h"
45 #include "log.h"
46 #include "exceptions.h"
47 #include "examine_content_job.h"
48 #include "scaler.h"
49 #include "config.h"
50 #include "version.h"
51 #include "ui_signaller.h"
52 #include "analyse_audio_job.h"
53 #include "playlist.h"
54 #include "player.h"
55 #include "ffmpeg_content.h"
56 #include "imagemagick_content.h"
57 #include "sndfile_content.h"
58 #include "dcp_content_type.h"
59
60 #include "i18n.h"
61
62 using std::string;
63 using std::stringstream;
64 using std::multimap;
65 using std::pair;
66 using std::map;
67 using std::vector;
68 using std::ifstream;
69 using std::ofstream;
70 using std::setfill;
71 using std::min;
72 using std::make_pair;
73 using std::endl;
74 using std::cout;
75 using std::list;
76 using boost::shared_ptr;
77 using boost::lexical_cast;
78 using boost::to_upper_copy;
79 using boost::ends_with;
80 using boost::starts_with;
81 using boost::optional;
82 using libdcp::Size;
83
84 int const Film::state_version = 4;
85
86 /** Construct a Film object in a given directory.
87  *
88  *  @param d Film directory.
89  */
90
91 Film::Film (string d)
92         : _playlist (new Playlist)
93         , _use_dci_name (true)
94         , _dcp_content_type (Config::instance()->default_dcp_content_type ())
95         , _container (Config::instance()->default_container ())
96         , _scaler (Scaler::from_id ("bicubic"))
97         , _ab (false)
98         , _with_subtitles (false)
99         , _subtitle_offset (0)
100         , _subtitle_scale (1)
101         , _colour_lut (0)
102         , _j2k_bandwidth (200000000)
103         , _dci_metadata (Config::instance()->default_dci_metadata ())
104         , _dcp_video_frame_rate (0)
105         , _dirty (false)
106 {
107         set_dci_date_today ();
108
109         _playlist->Changed.connect (bind (&Film::playlist_changed, this));
110         _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
111         
112         /* Make state.directory a complete path without ..s (where possible)
113            (Code swiped from Adam Bowen on stackoverflow)
114         */
115         
116         boost::filesystem::path p (boost::filesystem::system_complete (d));
117         boost::filesystem::path result;
118         for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
119                 if (*i == "..") {
120                         if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
121                                 result /= *i;
122                         } else {
123                                 result = result.parent_path ();
124                         }
125                 } else if (*i != ".") {
126                         result /= *i;
127                 }
128         }
129
130         set_directory (result.string ());
131         _log.reset (new FileLog (file ("log")));
132 }
133
134 Film::Film (Film const & o)
135         : boost::enable_shared_from_this<Film> (o)
136         /* note: the copied film shares the original's log */
137         , _log               (o._log)
138         , _playlist          (new Playlist (o._playlist))
139         , _directory         (o._directory)
140         , _name              (o._name)
141         , _use_dci_name      (o._use_dci_name)
142         , _dcp_content_type  (o._dcp_content_type)
143         , _container         (o._container)
144         , _filters           (o._filters)
145         , _scaler            (o._scaler)
146         , _ab                (o._ab)
147         , _with_subtitles    (o._with_subtitles)
148         , _subtitle_offset   (o._subtitle_offset)
149         , _subtitle_scale    (o._subtitle_scale)
150         , _colour_lut        (o._colour_lut)
151         , _j2k_bandwidth     (o._j2k_bandwidth)
152         , _dci_metadata      (o._dci_metadata)
153         , _dcp_video_frame_rate (o._dcp_video_frame_rate)
154         , _dci_date          (o._dci_date)
155         , _dirty             (o._dirty)
156 {
157         _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
158 }
159
160 string
161 Film::video_state_identifier () const
162 {
163         assert (container ());
164         LocaleGuard lg;
165
166         pair<string, string> f = Filter::ffmpeg_strings (filters());
167
168         stringstream s;
169         s << container()->id()
170           << "_" << _playlist->video_digest()
171           << "_" << _dcp_video_frame_rate
172           << "_" << f.first << "_" << f.second
173           << "_" << scaler()->id()
174           << "_" << j2k_bandwidth()
175           << "_" << boost::lexical_cast<int> (colour_lut());
176
177         if (ab()) {
178                 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
179                 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
180         }
181
182         return s.str ();
183 }
184           
185 /** @return The path to the directory to write video frame info files to */
186 string
187 Film::info_dir () const
188 {
189         boost::filesystem::path p;
190         p /= "info";
191         p /= video_state_identifier ();
192         return dir (p.string());
193 }
194
195 string
196 Film::internal_video_mxf_dir () const
197 {
198         boost::filesystem::path p;
199         return dir ("video");
200 }
201
202 string
203 Film::internal_video_mxf_filename () const
204 {
205         return video_state_identifier() + ".mxf";
206 }
207
208 string
209 Film::dcp_video_mxf_filename () const
210 {
211         return filename_safe_name() + "_video.mxf";
212 }
213
214 string
215 Film::dcp_audio_mxf_filename () const
216 {
217         return filename_safe_name() + "_audio.mxf";
218 }
219
220 string
221 Film::filename_safe_name () const
222 {
223         string const n = name ();
224         string o;
225         for (size_t i = 0; i < n.length(); ++i) {
226                 if (isalnum (n[i])) {
227                         o += n[i];
228                 } else {
229                         o += "_";
230                 }
231         }
232
233         return o;
234 }
235
236 string
237 Film::audio_analysis_path () const
238 {
239         boost::filesystem::path p;
240         p /= "analysis";
241         p /= _playlist->audio_digest();
242         return file (p.string ());
243 }
244
245 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
246 void
247 Film::make_dcp ()
248 {
249         set_dci_date_today ();
250         
251         if (dcp_name().find ("/") != string::npos) {
252                 throw BadSettingError (_("name"), _("cannot contain slashes"));
253         }
254         
255         log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
256
257         {
258                 char buffer[128];
259                 gethostname (buffer, sizeof (buffer));
260                 log()->log (String::compose ("Starting to make DCP on %1", buffer));
261         }
262         
263 //      log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
264 //      if (length()) {
265 //              log()->log (String::compose ("Content length %1", length().get()));
266 //      }
267 //      log()->log (String::compose ("Content digest %1", content_digest()));
268 //      log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
269         log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
270         log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
271 #ifdef DCPOMATIC_DEBUG
272         log()->log ("DCP-o-matic built in debug mode.");
273 #else
274         log()->log ("DCP-o-matic built in optimised mode.");
275 #endif
276 #ifdef LIBDCP_DEBUG
277         log()->log ("libdcp built in debug mode.");
278 #else
279         log()->log ("libdcp built in optimised mode.");
280 #endif
281         pair<string, int> const c = cpu_info ();
282         log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
283         
284         if (container() == 0) {
285                 throw MissingSettingError (_("container"));
286         }
287
288         if (_playlist->content().empty ()) {
289                 throw StringError (_("You must add some content to the DCP before creating it"));
290         }
291
292         if (dcp_content_type() == 0) {
293                 throw MissingSettingError (_("content type"));
294         }
295
296         if (name().empty()) {
297                 throw MissingSettingError (_("name"));
298         }
299
300         shared_ptr<Job> r;
301
302         if (ab()) {
303                 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this())));
304         } else {
305                 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
306         }
307 }
308
309 /** Start a job to analyse the audio in our Playlist */
310 void
311 Film::analyse_audio ()
312 {
313         if (_analyse_audio_job) {
314                 return;
315         }
316
317         _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
318         _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
319         JobManager::instance()->add (_analyse_audio_job);
320 }
321
322 /** Start a job to examine a piece of content */
323 void
324 Film::examine_content (shared_ptr<Content> c)
325 {
326         shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
327         JobManager::instance()->add (j);
328 }
329
330 void
331 Film::analyse_audio_finished ()
332 {
333         ensure_ui_thread ();
334
335         if (_analyse_audio_job->finished_ok ()) {
336                 AudioAnalysisSucceeded ();
337         }
338         
339         _analyse_audio_job.reset ();
340 }
341
342 /** Start a job to send our DCP to the configured TMS */
343 void
344 Film::send_dcp_to_tms ()
345 {
346         shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
347         JobManager::instance()->add (j);
348 }
349
350 /** Count the number of frames that have been encoded for this film.
351  *  @return frame count.
352  */
353 int
354 Film::encoded_frames () const
355 {
356         if (container() == 0) {
357                 return 0;
358         }
359
360         int N = 0;
361         for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
362                 ++N;
363                 boost::this_thread::interruption_point ();
364         }
365
366         return N;
367 }
368
369 /** Write state to our `metadata' file */
370 void
371 Film::write_metadata () const
372 {
373         if (!boost::filesystem::exists (directory())) {
374                 boost::filesystem::create_directory (directory());
375         }
376         
377         boost::mutex::scoped_lock lm (_state_mutex);
378         LocaleGuard lg;
379
380         boost::filesystem::create_directories (directory());
381
382         xmlpp::Document doc;
383         xmlpp::Element* root = doc.create_root_node ("Metadata");
384
385         root->add_child("Version")->add_child_text (boost::lexical_cast<string> (state_version));
386         root->add_child("Name")->add_child_text (_name);
387         root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
388
389         if (_dcp_content_type) {
390                 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
391         }
392
393         if (_container) {
394                 root->add_child("Container")->add_child_text (_container->id ());
395         }
396
397         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
398                 root->add_child("Filter")->add_child_text ((*i)->id ());
399         }
400         
401         root->add_child("Scaler")->add_child_text (_scaler->id ());
402         root->add_child("AB")->add_child_text (_ab ? "1" : "0");
403         root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
404         root->add_child("SubtitleOffset")->add_child_text (boost::lexical_cast<string> (_subtitle_offset));
405         root->add_child("SubtitleScale")->add_child_text (boost::lexical_cast<string> (_subtitle_scale));
406         root->add_child("ColourLUT")->add_child_text (boost::lexical_cast<string> (_colour_lut));
407         root->add_child("J2KBandwidth")->add_child_text (boost::lexical_cast<string> (_j2k_bandwidth));
408         _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
409         root->add_child("DCPVideoFrameRate")->add_child_text (boost::lexical_cast<string> (_dcp_video_frame_rate));
410         root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
411         _playlist->as_xml (root->add_child ("Playlist"));
412
413         doc.write_to_file_formatted (file ("metadata.xml"));
414         
415         _dirty = false;
416 }
417
418 /** Read state from our metadata file */
419 void
420 Film::read_metadata ()
421 {
422         boost::mutex::scoped_lock lm (_state_mutex);
423         LocaleGuard lg;
424
425         if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
426                 throw StringError (_("This film was created with an older version of DCP-o-matic, and unfortunately it cannot be loaded into this version.  You will need to create a new Film, re-add your content and set it up again.  Sorry!"));
427         }
428
429         cxml::File f (file ("metadata.xml"), "Metadata");
430         
431         _name = f.string_child ("Name");
432         _use_dci_name = f.bool_child ("UseDCIName");
433
434         {
435                 optional<string> c = f.optional_string_child ("DCPContentType");
436                 if (c) {
437                         _dcp_content_type = DCPContentType::from_dci_name (c.get ());
438                 }
439         }
440
441         {
442                 optional<string> c = f.optional_string_child ("Container");
443                 if (c) {
444                         _container = Container::from_id (c.get ());
445                 }
446         }
447
448         {
449                 list<shared_ptr<cxml::Node> > c = f.node_children ("Filter");
450                 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
451                         _filters.push_back (Filter::from_id ((*i)->content ()));
452                 }
453         }
454
455         _scaler = Scaler::from_id (f.string_child ("Scaler"));
456         _ab = f.bool_child ("AB");
457         _with_subtitles = f.bool_child ("WithSubtitles");
458         _subtitle_offset = f.number_child<float> ("SubtitleOffset");
459         _subtitle_scale = f.number_child<float> ("SubtitleScale");
460         _colour_lut = f.number_child<int> ("ColourLUT");
461         _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
462         _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
463         _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
464         _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
465
466         _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"));
467
468         _dirty = false;
469 }
470
471 /** Given a directory name, return its full path within the Film's directory.
472  *  The directory (and its parents) will be created if they do not exist.
473  */
474 string
475 Film::dir (string d) const
476 {
477         boost::mutex::scoped_lock lm (_directory_mutex);
478         
479         boost::filesystem::path p;
480         p /= _directory;
481         p /= d;
482         
483         boost::filesystem::create_directories (p);
484         
485         return p.string ();
486 }
487
488 /** Given a file or directory name, return its full path within the Film's directory.
489  *  _directory_mutex must not be locked on entry.
490  *  Any required parent directories will be created.
491  */
492 string
493 Film::file (string f) const
494 {
495         boost::mutex::scoped_lock lm (_directory_mutex);
496
497         boost::filesystem::path p;
498         p /= _directory;
499         p /= f;
500
501         boost::filesystem::create_directories (p.parent_path ());
502         
503         return p.string ();
504 }
505
506 /** @return a DCI-compliant name for a DCP of this film */
507 string
508 Film::dci_name (bool if_created_now) const
509 {
510         stringstream d;
511
512         string fixed_name = to_upper_copy (name());
513         for (size_t i = 0; i < fixed_name.length(); ++i) {
514                 if (fixed_name[i] == ' ') {
515                         fixed_name[i] = '-';
516                 }
517         }
518
519         /* Spec is that the name part should be maximum 14 characters, as I understand it */
520         if (fixed_name.length() > 14) {
521                 fixed_name = fixed_name.substr (0, 14);
522         }
523
524         d << fixed_name;
525
526         if (dcp_content_type()) {
527                 d << "_" << dcp_content_type()->dci_name();
528         }
529
530         if (container()) {
531                 d << "_" << container()->dci_name();
532         }
533
534         DCIMetadata const dm = dci_metadata ();
535
536         if (!dm.audio_language.empty ()) {
537                 d << "_" << dm.audio_language;
538                 if (!dm.subtitle_language.empty()) {
539                         d << "-" << dm.subtitle_language;
540                 } else {
541                         d << "-XX";
542                 }
543         }
544
545         if (!dm.territory.empty ()) {
546                 d << "_" << dm.territory;
547                 if (!dm.rating.empty ()) {
548                         d << "-" << dm.rating;
549                 }
550         }
551
552         d << "_51_2K";
553
554         if (!dm.studio.empty ()) {
555                 d << "_" << dm.studio;
556         }
557
558         if (if_created_now) {
559                 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
560         } else {
561                 d << "_" << boost::gregorian::to_iso_string (_dci_date);
562         }
563
564         if (!dm.facility.empty ()) {
565                 d << "_" << dm.facility;
566         }
567
568         if (!dm.package_type.empty ()) {
569                 d << "_" << dm.package_type;
570         }
571
572         return d.str ();
573 }
574
575 /** @return name to give the DCP */
576 string
577 Film::dcp_name (bool if_created_now) const
578 {
579         if (use_dci_name()) {
580                 return dci_name (if_created_now);
581         }
582
583         return name();
584 }
585
586
587 void
588 Film::set_directory (string d)
589 {
590         boost::mutex::scoped_lock lm (_state_mutex);
591         _directory = d;
592         _dirty = true;
593 }
594
595 void
596 Film::set_name (string n)
597 {
598         {
599                 boost::mutex::scoped_lock lm (_state_mutex);
600                 _name = n;
601         }
602         signal_changed (NAME);
603 }
604
605 void
606 Film::set_use_dci_name (bool u)
607 {
608         {
609                 boost::mutex::scoped_lock lm (_state_mutex);
610                 _use_dci_name = u;
611         }
612         signal_changed (USE_DCI_NAME);
613 }
614
615 void
616 Film::set_dcp_content_type (DCPContentType const * t)
617 {
618         {
619                 boost::mutex::scoped_lock lm (_state_mutex);
620                 _dcp_content_type = t;
621         }
622         signal_changed (DCP_CONTENT_TYPE);
623 }
624
625 void
626 Film::set_container (Container const * c)
627 {
628         {
629                 boost::mutex::scoped_lock lm (_state_mutex);
630                 _container = c;
631         }
632         signal_changed (CONTAINER);
633 }
634
635 void
636 Film::set_filters (vector<Filter const *> f)
637 {
638         {
639                 boost::mutex::scoped_lock lm (_state_mutex);
640                 _filters = f;
641         }
642         signal_changed (FILTERS);
643 }
644
645 void
646 Film::set_scaler (Scaler const * s)
647 {
648         {
649                 boost::mutex::scoped_lock lm (_state_mutex);
650                 _scaler = s;
651         }
652         signal_changed (SCALER);
653 }
654
655 void
656 Film::set_ab (bool a)
657 {
658         {
659                 boost::mutex::scoped_lock lm (_state_mutex);
660                 _ab = a;
661         }
662         signal_changed (AB);
663 }
664
665 void
666 Film::set_with_subtitles (bool w)
667 {
668         {
669                 boost::mutex::scoped_lock lm (_state_mutex);
670                 _with_subtitles = w;
671         }
672         signal_changed (WITH_SUBTITLES);
673 }
674
675 void
676 Film::set_subtitle_offset (int o)
677 {
678         {
679                 boost::mutex::scoped_lock lm (_state_mutex);
680                 _subtitle_offset = o;
681         }
682         signal_changed (SUBTITLE_OFFSET);
683 }
684
685 void
686 Film::set_subtitle_scale (float s)
687 {
688         {
689                 boost::mutex::scoped_lock lm (_state_mutex);
690                 _subtitle_scale = s;
691         }
692         signal_changed (SUBTITLE_SCALE);
693 }
694
695 void
696 Film::set_colour_lut (int i)
697 {
698         {
699                 boost::mutex::scoped_lock lm (_state_mutex);
700                 _colour_lut = i;
701         }
702         signal_changed (COLOUR_LUT);
703 }
704
705 void
706 Film::set_j2k_bandwidth (int b)
707 {
708         {
709                 boost::mutex::scoped_lock lm (_state_mutex);
710                 _j2k_bandwidth = b;
711         }
712         signal_changed (J2K_BANDWIDTH);
713 }
714
715 void
716 Film::set_dci_metadata (DCIMetadata m)
717 {
718         {
719                 boost::mutex::scoped_lock lm (_state_mutex);
720                 _dci_metadata = m;
721         }
722         signal_changed (DCI_METADATA);
723 }
724
725
726 void
727 Film::set_dcp_video_frame_rate (int f)
728 {
729         {
730                 boost::mutex::scoped_lock lm (_state_mutex);
731                 _dcp_video_frame_rate = f;
732         }
733         signal_changed (DCP_VIDEO_FRAME_RATE);
734 }
735
736 void
737 Film::signal_changed (Property p)
738 {
739         {
740                 boost::mutex::scoped_lock lm (_state_mutex);
741                 _dirty = true;
742         }
743
744         switch (p) {
745         case Film::CONTENT:
746                 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
747                 break;
748         default:
749                 break;
750         }
751
752         if (ui_signaller) {
753                 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
754         }
755 }
756
757 void
758 Film::set_dci_date_today ()
759 {
760         _dci_date = boost::gregorian::day_clock::local_day ();
761 }
762
763 string
764 Film::info_path (int f) const
765 {
766         boost::filesystem::path p;
767         p /= info_dir ();
768
769         stringstream s;
770         s.width (8);
771         s << setfill('0') << f << ".md5";
772
773         p /= s.str();
774
775         /* info_dir() will already have added any initial bit of the path,
776            so don't call file() on this.
777         */
778         return p.string ();
779 }
780
781 string
782 Film::j2c_path (int f, bool t) const
783 {
784         boost::filesystem::path p;
785         p /= "j2c";
786         p /= video_state_identifier ();
787
788         stringstream s;
789         s.width (8);
790         s << setfill('0') << f << ".j2c";
791
792         if (t) {
793                 s << ".tmp";
794         }
795
796         p /= s.str();
797         return file (p.string ());
798 }
799
800 /** Make an educated guess as to whether we have a complete DCP
801  *  or not.
802  *  @return true if we do.
803  */
804
805 bool
806 Film::have_dcp () const
807 {
808         try {
809                 libdcp::DCP dcp (dir (dcp_name()));
810                 dcp.read ();
811         } catch (...) {
812                 return false;
813         }
814
815         return true;
816 }
817
818 shared_ptr<Player>
819 Film::player () const
820 {
821         boost::mutex::scoped_lock lm (_state_mutex);
822         return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
823 }
824
825 shared_ptr<Playlist>
826 Film::playlist () const
827 {
828         boost::mutex::scoped_lock lm (_state_mutex);
829         return _playlist;
830 }
831
832 Playlist::ContentList
833 Film::content () const
834 {
835         return _playlist->content ();
836 }
837
838 void
839 Film::add_content (shared_ptr<Content> c)
840 {
841         _playlist->add (c);
842         examine_content (c);
843 }
844
845 void
846 Film::remove_content (shared_ptr<Content> c)
847 {
848         _playlist->remove (c);
849 }
850
851 Time
852 Film::length () const
853 {
854         return _playlist->length ();
855 }
856
857 bool
858 Film::has_subtitles () const
859 {
860         return _playlist->has_subtitles ();
861 }
862
863 OutputVideoFrame
864 Film::best_dcp_video_frame_rate () const
865 {
866         return _playlist->best_dcp_frame_rate ();
867 }
868
869 void
870 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
871 {
872         if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
873                 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
874         } 
875
876         if (ui_signaller) {
877                 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
878         }
879 }
880
881 void
882 Film::playlist_changed ()
883 {
884         signal_changed (CONTENT);
885 }       
886
887 int
888 Film::loop () const
889 {
890         return _playlist->loop ();
891 }
892
893 void
894 Film::set_loop (int c)
895 {
896         _playlist->set_loop (c);
897 }
898
899 OutputAudioFrame
900 Film::time_to_audio_frames (Time t) const
901 {
902         return t * dcp_audio_frame_rate () / TIME_HZ;
903 }
904
905 OutputVideoFrame
906 Film::time_to_video_frames (Time t) const
907 {
908         return t * dcp_video_frame_rate () / TIME_HZ;
909 }
910
911 Time
912 Film::audio_frames_to_time (OutputAudioFrame f) const
913 {
914         return f * TIME_HZ / dcp_audio_frame_rate ();
915 }
916
917 Time
918 Film::video_frames_to_time (OutputVideoFrame f) const
919 {
920         return f * TIME_HZ / dcp_video_frame_rate ();
921 }
922
923 OutputAudioFrame
924 Film::dcp_audio_frame_rate () const
925 {
926         /* XXX */
927         return 48000;
928 }