2 Copyright (C) 2012-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.
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string.hpp>
30 #include <boost/lexical_cast.hpp>
31 #include <boost/date_time.hpp>
32 #include <libxml++/libxml++.h>
33 #include <libcxml/cxml.h>
37 #include "job_manager.h"
38 #include "transcode_job.h"
39 #include "scp_dcp_job.h"
41 #include "exceptions.h"
42 #include "examine_content_job.h"
46 #include "ui_signaller.h"
49 #include "dcp_content_type.h"
56 using std::stringstream;
69 using boost::shared_ptr;
70 using boost::weak_ptr;
71 using boost::lexical_cast;
72 using boost::dynamic_pointer_cast;
73 using boost::to_upper_copy;
74 using boost::ends_with;
75 using boost::starts_with;
76 using boost::optional;
79 int const Film::state_version = 4;
81 /** Construct a Film object in a given directory.
83 * @param d Film directory.
87 : _playlist (new Playlist)
88 , _use_dci_name (true)
89 , _dcp_content_type (Config::instance()->default_dcp_content_type ())
90 , _container (Config::instance()->default_container ())
91 , _resolution (RESOLUTION_2K)
92 , _scaler (Scaler::from_id ("bicubic"))
93 , _with_subtitles (false)
94 , _j2k_bandwidth (Config::instance()->default_j2k_bandwidth ())
95 , _dci_metadata (Config::instance()->default_dci_metadata ())
96 , _dcp_video_frame_rate (24)
97 , _dcp_audio_channels (MAX_AUDIO_CHANNELS)
98 , _sequence_video (true)
101 set_dci_date_today ();
103 _playlist->Changed.connect (bind (&Film::playlist_changed, this));
104 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
106 /* Make state.directory a complete path without ..s (where possible)
107 (Code swiped from Adam Bowen on stackoverflow)
110 boost::filesystem::path p (boost::filesystem::system_complete (d));
111 boost::filesystem::path result;
112 for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
114 if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
117 result = result.parent_path ();
119 } else if (*i != ".") {
124 set_directory (result.string ());
125 _log.reset (new FileLog ("log"));
127 _playlist->set_sequence_video (_sequence_video);
131 Film::video_identifier () const
133 assert (container ());
137 s << container()->id()
138 << "_" << resolution_to_string (_resolution)
139 << "_" << _playlist->video_identifier()
140 << "_" << _dcp_video_frame_rate
141 << "_" << scaler()->id()
142 << "_" << j2k_bandwidth();
147 /** @return The path to the directory to write video frame info files to */
149 Film::info_dir () const
151 boost::filesystem::path p;
153 p /= video_identifier ();
154 return dir (p.string());
158 Film::internal_video_mxf_dir () const
160 return dir ("video");
164 Film::internal_video_mxf_filename () const
166 return video_identifier() + ".mxf";
170 Film::dcp_video_mxf_filename () const
172 return filename_safe_name() + "_video.mxf";
176 Film::dcp_audio_mxf_filename () const
178 return filename_safe_name() + "_audio.mxf";
182 Film::filename_safe_name () const
184 string const n = name ();
186 for (size_t i = 0; i < n.length(); ++i) {
187 if (isalnum (n[i])) {
197 boost::filesystem::path
198 Film::audio_analysis_path (shared_ptr<const AudioContent> c) const
200 boost::filesystem::path p = dir ("analysis");
205 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
209 set_dci_date_today ();
211 if (dcp_name().find ("/") != string::npos) {
212 throw BadSettingError (_("name"), _("cannot contain slashes"));
215 log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
219 gethostname (buffer, sizeof (buffer));
220 log()->log (String::compose ("Starting to make DCP on %1", buffer));
223 // log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
225 // log()->log (String::compose ("Content length %1", length().get()));
227 // log()->log (String::compose ("Content digest %1", content_digest()));
228 // log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
229 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
230 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
231 #ifdef DCPOMATIC_DEBUG
232 log()->log ("DCP-o-matic built in debug mode.");
234 log()->log ("DCP-o-matic built in optimised mode.");
237 log()->log ("libdcp built in debug mode.");
239 log()->log ("libdcp built in optimised mode.");
241 pair<string, int> const c = cpu_info ();
242 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
243 list<pair<string, string> > const m = mount_info ();
244 for (list<pair<string, string> >::const_iterator i = m.begin(); i != m.end(); ++i) {
245 log()->log (String::compose ("Mount: %1 %2", i->first, i->second));
248 if (container() == 0) {
249 throw MissingSettingError (_("container"));
252 if (content().empty()) {
253 throw StringError (_("You must add some content to the DCP before creating it"));
256 if (dcp_content_type() == 0) {
257 throw MissingSettingError (_("content type"));
260 if (name().empty()) {
261 throw MissingSettingError (_("name"));
264 JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
267 /** Start a job to send our DCP to the configured TMS */
269 Film::send_dcp_to_tms ()
271 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
272 JobManager::instance()->add (j);
275 /** Count the number of frames that have been encoded for this film.
276 * @return frame count.
279 Film::encoded_frames () const
281 if (container() == 0) {
286 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
288 boost::this_thread::interruption_point ();
294 /** Write state to our `metadata' file */
296 Film::write_metadata () const
298 if (!boost::filesystem::exists (directory())) {
299 boost::filesystem::create_directory (directory());
302 boost::mutex::scoped_lock lm (_state_mutex);
305 boost::filesystem::create_directories (directory());
308 xmlpp::Element* root = doc.create_root_node ("Metadata");
310 root->add_child("Version")->add_child_text (lexical_cast<string> (state_version));
311 root->add_child("Name")->add_child_text (_name);
312 root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
314 if (_dcp_content_type) {
315 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
319 root->add_child("Container")->add_child_text (_container->id ());
322 root->add_child("Resolution")->add_child_text (resolution_to_string (_resolution));
323 root->add_child("Scaler")->add_child_text (_scaler->id ());
324 root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
325 root->add_child("J2KBandwidth")->add_child_text (lexical_cast<string> (_j2k_bandwidth));
326 _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
327 root->add_child("DCPVideoFrameRate")->add_child_text (lexical_cast<string> (_dcp_video_frame_rate));
328 root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
329 root->add_child("DCPAudioChannels")->add_child_text (lexical_cast<string> (_dcp_audio_channels));
330 root->add_child("SequenceVideo")->add_child_text (_sequence_video ? "1" : "0");
331 _playlist->as_xml (root->add_child ("Playlist"));
333 doc.write_to_file_formatted (file ("metadata.xml"));
338 /** Read state from our metadata file */
340 Film::read_metadata ()
342 boost::mutex::scoped_lock lm (_state_mutex);
345 if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
346 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!"));
349 cxml::File f (file ("metadata.xml"), "Metadata");
351 _name = f.string_child ("Name");
352 _use_dci_name = f.bool_child ("UseDCIName");
355 optional<string> c = f.optional_string_child ("DCPContentType");
357 _dcp_content_type = DCPContentType::from_dci_name (c.get ());
362 optional<string> c = f.optional_string_child ("Container");
364 _container = Ratio::from_id (c.get ());
368 _resolution = string_to_resolution (f.string_child ("Resolution"));
369 _scaler = Scaler::from_id (f.string_child ("Scaler"));
370 _with_subtitles = f.bool_child ("WithSubtitles");
371 _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
372 _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
373 _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
374 _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
375 _dcp_audio_channels = f.number_child<int> ("DCPAudioChannels");
376 _sequence_video = f.bool_child ("SequenceVideo");
378 _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"));
383 /** Given a directory name, return its full path within the Film's directory.
384 * The directory (and its parents) will be created if they do not exist.
387 Film::dir (string d) const
389 boost::mutex::scoped_lock lm (_directory_mutex);
391 boost::filesystem::path p;
395 boost::filesystem::create_directories (p);
400 /** Given a file or directory name, return its full path within the Film's directory.
401 * _directory_mutex must not be locked on entry.
402 * Any required parent directories will be created.
405 Film::file (string f) const
407 boost::mutex::scoped_lock lm (_directory_mutex);
409 boost::filesystem::path p;
413 boost::filesystem::create_directories (p.parent_path ());
418 /** @return a DCI-compliant name for a DCP of this film */
420 Film::dci_name (bool if_created_now) const
424 string fixed_name = to_upper_copy (name());
425 for (size_t i = 0; i < fixed_name.length(); ++i) {
426 if (fixed_name[i] == ' ') {
431 /* Spec is that the name part should be maximum 14 characters, as I understand it */
432 if (fixed_name.length() > 14) {
433 fixed_name = fixed_name.substr (0, 14);
438 if (dcp_content_type()) {
439 d << "_" << dcp_content_type()->dci_name();
443 d << "_" << container()->dci_name();
446 DCIMetadata const dm = dci_metadata ();
448 if (!dm.audio_language.empty ()) {
449 d << "_" << dm.audio_language;
450 if (!dm.subtitle_language.empty() && with_subtitles()) {
451 d << "-" << dm.subtitle_language;
457 if (!dm.territory.empty ()) {
458 d << "_" << dm.territory;
459 if (!dm.rating.empty ()) {
460 d << "-" << dm.rating;
464 switch (dcp_audio_channels ()) {
485 d << "_" << resolution_to_string (_resolution);
487 if (!dm.studio.empty ()) {
488 d << "_" << dm.studio;
491 if (if_created_now) {
492 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
494 d << "_" << boost::gregorian::to_iso_string (_dci_date);
497 if (!dm.facility.empty ()) {
498 d << "_" << dm.facility;
501 if (!dm.package_type.empty ()) {
502 d << "_" << dm.package_type;
508 /** @return name to give the DCP */
510 Film::dcp_name (bool if_created_now) const
512 if (use_dci_name()) {
513 return dci_name (if_created_now);
521 Film::set_directory (string d)
523 boost::mutex::scoped_lock lm (_state_mutex);
529 Film::set_name (string n)
532 boost::mutex::scoped_lock lm (_state_mutex);
535 signal_changed (NAME);
539 Film::set_use_dci_name (bool u)
542 boost::mutex::scoped_lock lm (_state_mutex);
545 signal_changed (USE_DCI_NAME);
549 Film::set_dcp_content_type (DCPContentType const * t)
552 boost::mutex::scoped_lock lm (_state_mutex);
553 _dcp_content_type = t;
555 signal_changed (DCP_CONTENT_TYPE);
559 Film::set_container (Ratio const * c)
562 boost::mutex::scoped_lock lm (_state_mutex);
565 signal_changed (CONTAINER);
569 Film::set_resolution (Resolution r)
572 boost::mutex::scoped_lock lm (_state_mutex);
575 signal_changed (RESOLUTION);
579 Film::set_scaler (Scaler const * s)
582 boost::mutex::scoped_lock lm (_state_mutex);
585 signal_changed (SCALER);
589 Film::set_with_subtitles (bool w)
592 boost::mutex::scoped_lock lm (_state_mutex);
595 signal_changed (WITH_SUBTITLES);
599 Film::set_j2k_bandwidth (int b)
602 boost::mutex::scoped_lock lm (_state_mutex);
605 signal_changed (J2K_BANDWIDTH);
609 Film::set_dci_metadata (DCIMetadata m)
612 boost::mutex::scoped_lock lm (_state_mutex);
615 signal_changed (DCI_METADATA);
619 Film::set_dcp_video_frame_rate (int f)
622 boost::mutex::scoped_lock lm (_state_mutex);
623 _dcp_video_frame_rate = f;
625 signal_changed (DCP_VIDEO_FRAME_RATE);
629 Film::set_dcp_audio_channels (int c)
632 boost::mutex::scoped_lock lm (_state_mutex);
633 _dcp_audio_channels = c;
635 signal_changed (DCP_AUDIO_CHANNELS);
639 Film::signal_changed (Property p)
642 boost::mutex::scoped_lock lm (_state_mutex);
648 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
650 case Film::DCP_VIDEO_FRAME_RATE:
651 case Film::SEQUENCE_VIDEO:
652 _playlist->maybe_sequence_video ();
659 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
664 Film::set_dci_date_today ()
666 _dci_date = boost::gregorian::day_clock::local_day ();
670 Film::info_path (int f) const
672 boost::filesystem::path p;
677 s << setfill('0') << f << ".md5";
681 /* info_dir() will already have added any initial bit of the path,
682 so don't call file() on this.
688 Film::j2c_path (int f, bool t) const
690 boost::filesystem::path p;
692 p /= video_identifier ();
696 s << setfill('0') << f << ".j2c";
703 return file (p.string ());
706 /** Make an educated guess as to whether we have a complete DCP
708 * @return true if we do.
712 Film::have_dcp () const
715 libdcp::DCP dcp (dir (dcp_name()));
725 Film::make_player () const
727 return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
731 Film::playlist () const
733 boost::mutex::scoped_lock lm (_state_mutex);
738 Film::content () const
740 return _playlist->content ();
744 Film::examine_and_add_content (shared_ptr<Content> c)
746 shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
747 j->Finished.connect (bind (&Film::maybe_add_content, this, boost::weak_ptr<Job> (j), boost::weak_ptr<Content> (c)));
748 JobManager::instance()->add (j);
752 Film::maybe_add_content (weak_ptr<Job> j, weak_ptr<Content> c)
754 shared_ptr<Job> job = j.lock ();
755 if (!job || !job->finished_ok ()) {
759 shared_ptr<Content> content = c.lock ();
761 add_content (content);
766 Film::add_content (shared_ptr<Content> c)
768 /* Add video content after any existing content */
769 if (dynamic_pointer_cast<VideoContent> (c)) {
770 c->set_start (_playlist->video_end ());
777 Film::remove_content (shared_ptr<Content> c)
779 _playlist->remove (c);
783 Film::length () const
785 return _playlist->length ();
789 Film::has_subtitles () const
791 return _playlist->has_subtitles ();
795 Film::best_dcp_video_frame_rate () const
797 return _playlist->best_dcp_frame_rate ();
801 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
803 if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
804 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
808 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
813 Film::playlist_changed ()
815 signal_changed (CONTENT);
819 Film::time_to_audio_frames (Time t) const
821 return t * dcp_audio_frame_rate () / TIME_HZ;
825 Film::time_to_video_frames (Time t) const
827 return t * dcp_video_frame_rate () / TIME_HZ;
831 Film::audio_frames_to_time (OutputAudioFrame f) const
833 return f * TIME_HZ / dcp_audio_frame_rate ();
837 Film::video_frames_to_time (OutputVideoFrame f) const
839 return f * TIME_HZ / dcp_video_frame_rate ();
843 Film::dcp_audio_frame_rate () const
850 Film::set_sequence_video (bool s)
853 boost::mutex::scoped_lock lm (_state_mutex);
855 _playlist->set_sequence_video (s);
858 signal_changed (SEQUENCE_VIDEO);
862 Film::full_frame () const
864 switch (_resolution) {
866 return libdcp::Size (2048, 1080);
868 return libdcp::Size (4096, 2160);
872 return libdcp::Size ();