2 Copyright (C) 2012 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 , _scaler (Scaler::from_id ("bicubic"))
92 , _with_subtitles (false)
93 , _j2k_bandwidth (Config::instance()->default_j2k_bandwidth ())
94 , _dci_metadata (Config::instance()->default_dci_metadata ())
95 , _dcp_video_frame_rate (24)
96 , _dcp_audio_channels (MAX_AUDIO_CHANNELS)
99 set_dci_date_today ();
101 _playlist->Changed.connect (bind (&Film::playlist_changed, this));
102 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
104 /* Make state.directory a complete path without ..s (where possible)
105 (Code swiped from Adam Bowen on stackoverflow)
108 boost::filesystem::path p (boost::filesystem::system_complete (d));
109 boost::filesystem::path result;
110 for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
112 if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
115 result = result.parent_path ();
117 } else if (*i != ".") {
122 set_directory (result.string ());
123 _log.reset (new FileLog ("log"));
127 Film::video_identifier () const
129 assert (container ());
133 s << container()->id()
134 << "_" << _playlist->video_identifier()
135 << "_" << _dcp_video_frame_rate
136 << "_" << scaler()->id()
137 << "_" << j2k_bandwidth();
142 /** @return The path to the directory to write video frame info files to */
144 Film::info_dir () const
146 boost::filesystem::path p;
148 p /= video_identifier ();
149 return dir (p.string());
153 Film::internal_video_mxf_dir () const
155 return dir ("video");
159 Film::internal_video_mxf_filename () const
161 return video_identifier() + ".mxf";
165 Film::dcp_video_mxf_filename () const
167 return filename_safe_name() + "_video.mxf";
171 Film::dcp_audio_mxf_filename () const
173 return filename_safe_name() + "_audio.mxf";
177 Film::filename_safe_name () const
179 string const n = name ();
181 for (size_t i = 0; i < n.length(); ++i) {
182 if (isalnum (n[i])) {
192 boost::filesystem::path
193 Film::audio_analysis_path (shared_ptr<const AudioContent> c) const
195 boost::filesystem::path p = dir ("analysis");
200 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
204 set_dci_date_today ();
206 if (dcp_name().find ("/") != string::npos) {
207 throw BadSettingError (_("name"), _("cannot contain slashes"));
210 log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
214 gethostname (buffer, sizeof (buffer));
215 log()->log (String::compose ("Starting to make DCP on %1", buffer));
218 // log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
220 // log()->log (String::compose ("Content length %1", length().get()));
222 // log()->log (String::compose ("Content digest %1", content_digest()));
223 // log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
224 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
225 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
226 #ifdef DCPOMATIC_DEBUG
227 log()->log ("DCP-o-matic built in debug mode.");
229 log()->log ("DCP-o-matic built in optimised mode.");
232 log()->log ("libdcp built in debug mode.");
234 log()->log ("libdcp built in optimised mode.");
236 pair<string, int> const c = cpu_info ();
237 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
238 list<pair<string, string> > const m = mount_info ();
239 for (list<pair<string, string> >::const_iterator i = m.begin(); i != m.end(); ++i) {
240 log()->log (String::compose ("Mount: %1 %2", i->first, i->second));
243 if (container() == 0) {
244 throw MissingSettingError (_("container"));
247 if (_playlist->content().empty ()) {
248 throw StringError (_("You must add some content to the DCP before creating it"));
251 if (dcp_content_type() == 0) {
252 throw MissingSettingError (_("content type"));
255 if (name().empty()) {
256 throw MissingSettingError (_("name"));
259 JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
262 /** Start a job to send our DCP to the configured TMS */
264 Film::send_dcp_to_tms ()
266 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
267 JobManager::instance()->add (j);
270 /** Count the number of frames that have been encoded for this film.
271 * @return frame count.
274 Film::encoded_frames () const
276 if (container() == 0) {
281 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
283 boost::this_thread::interruption_point ();
289 /** Write state to our `metadata' file */
291 Film::write_metadata () const
293 if (!boost::filesystem::exists (directory())) {
294 boost::filesystem::create_directory (directory());
297 boost::mutex::scoped_lock lm (_state_mutex);
300 boost::filesystem::create_directories (directory());
303 xmlpp::Element* root = doc.create_root_node ("Metadata");
305 root->add_child("Version")->add_child_text (lexical_cast<string> (state_version));
306 root->add_child("Name")->add_child_text (_name);
307 root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
309 if (_dcp_content_type) {
310 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
314 root->add_child("Container")->add_child_text (_container->id ());
317 root->add_child("Scaler")->add_child_text (_scaler->id ());
318 root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
319 root->add_child("J2KBandwidth")->add_child_text (lexical_cast<string> (_j2k_bandwidth));
320 _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
321 root->add_child("DCPVideoFrameRate")->add_child_text (lexical_cast<string> (_dcp_video_frame_rate));
322 root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
323 root->add_child("DCPAudioChannels")->add_child_text (lexical_cast<string> (_dcp_audio_channels));
324 _playlist->as_xml (root->add_child ("Playlist"));
326 doc.write_to_file_formatted (file ("metadata.xml"));
331 /** Read state from our metadata file */
333 Film::read_metadata ()
335 boost::mutex::scoped_lock lm (_state_mutex);
338 if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
339 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!"));
342 cxml::File f (file ("metadata.xml"), "Metadata");
344 _name = f.string_child ("Name");
345 _use_dci_name = f.bool_child ("UseDCIName");
348 optional<string> c = f.optional_string_child ("DCPContentType");
350 _dcp_content_type = DCPContentType::from_dci_name (c.get ());
355 optional<string> c = f.optional_string_child ("Container");
357 _container = Ratio::from_id (c.get ());
361 _scaler = Scaler::from_id (f.string_child ("Scaler"));
362 _with_subtitles = f.bool_child ("WithSubtitles");
363 _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
364 _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
365 _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
366 _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
367 _dcp_audio_channels = f.number_child<int> ("DCPAudioChannels");
369 _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"));
374 /** Given a directory name, return its full path within the Film's directory.
375 * The directory (and its parents) will be created if they do not exist.
378 Film::dir (string d) const
380 boost::mutex::scoped_lock lm (_directory_mutex);
382 boost::filesystem::path p;
386 boost::filesystem::create_directories (p);
391 /** Given a file or directory name, return its full path within the Film's directory.
392 * _directory_mutex must not be locked on entry.
393 * Any required parent directories will be created.
396 Film::file (string f) const
398 boost::mutex::scoped_lock lm (_directory_mutex);
400 boost::filesystem::path p;
404 boost::filesystem::create_directories (p.parent_path ());
409 /** @return a DCI-compliant name for a DCP of this film */
411 Film::dci_name (bool if_created_now) const
415 string fixed_name = to_upper_copy (name());
416 for (size_t i = 0; i < fixed_name.length(); ++i) {
417 if (fixed_name[i] == ' ') {
422 /* Spec is that the name part should be maximum 14 characters, as I understand it */
423 if (fixed_name.length() > 14) {
424 fixed_name = fixed_name.substr (0, 14);
429 if (dcp_content_type()) {
430 d << "_" << dcp_content_type()->dci_name();
434 d << "_" << container()->dci_name();
437 DCIMetadata const dm = dci_metadata ();
439 if (!dm.audio_language.empty ()) {
440 d << "_" << dm.audio_language;
441 if (!dm.subtitle_language.empty()) {
442 d << "-" << dm.subtitle_language;
448 if (!dm.territory.empty ()) {
449 d << "_" << dm.territory;
450 if (!dm.rating.empty ()) {
451 d << "-" << dm.rating;
457 if (!dm.studio.empty ()) {
458 d << "_" << dm.studio;
461 if (if_created_now) {
462 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
464 d << "_" << boost::gregorian::to_iso_string (_dci_date);
467 if (!dm.facility.empty ()) {
468 d << "_" << dm.facility;
471 if (!dm.package_type.empty ()) {
472 d << "_" << dm.package_type;
478 /** @return name to give the DCP */
480 Film::dcp_name (bool if_created_now) const
482 if (use_dci_name()) {
483 return dci_name (if_created_now);
491 Film::set_directory (string d)
493 boost::mutex::scoped_lock lm (_state_mutex);
499 Film::set_name (string n)
502 boost::mutex::scoped_lock lm (_state_mutex);
505 signal_changed (NAME);
509 Film::set_use_dci_name (bool u)
512 boost::mutex::scoped_lock lm (_state_mutex);
515 signal_changed (USE_DCI_NAME);
519 Film::set_dcp_content_type (DCPContentType const * t)
522 boost::mutex::scoped_lock lm (_state_mutex);
523 _dcp_content_type = t;
525 signal_changed (DCP_CONTENT_TYPE);
529 Film::set_container (Ratio const * c)
532 boost::mutex::scoped_lock lm (_state_mutex);
535 signal_changed (CONTAINER);
539 Film::set_scaler (Scaler const * s)
542 boost::mutex::scoped_lock lm (_state_mutex);
545 signal_changed (SCALER);
549 Film::set_with_subtitles (bool w)
552 boost::mutex::scoped_lock lm (_state_mutex);
555 signal_changed (WITH_SUBTITLES);
559 Film::set_j2k_bandwidth (int b)
562 boost::mutex::scoped_lock lm (_state_mutex);
565 signal_changed (J2K_BANDWIDTH);
569 Film::set_dci_metadata (DCIMetadata m)
572 boost::mutex::scoped_lock lm (_state_mutex);
575 signal_changed (DCI_METADATA);
579 Film::set_dcp_video_frame_rate (int f)
582 boost::mutex::scoped_lock lm (_state_mutex);
583 _dcp_video_frame_rate = f;
585 signal_changed (DCP_VIDEO_FRAME_RATE);
589 Film::set_dcp_audio_channels (int c)
592 boost::mutex::scoped_lock lm (_state_mutex);
593 _dcp_audio_channels = c;
595 signal_changed (DCP_AUDIO_CHANNELS);
599 Film::signal_changed (Property p)
602 boost::mutex::scoped_lock lm (_state_mutex);
608 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
610 case Film::DCP_VIDEO_FRAME_RATE:
611 _playlist->maybe_sequence_video ();
618 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
623 Film::set_dci_date_today ()
625 _dci_date = boost::gregorian::day_clock::local_day ();
629 Film::info_path (int f) const
631 boost::filesystem::path p;
636 s << setfill('0') << f << ".md5";
640 /* info_dir() will already have added any initial bit of the path,
641 so don't call file() on this.
647 Film::j2c_path (int f, bool t) const
649 boost::filesystem::path p;
651 p /= video_identifier ();
655 s << setfill('0') << f << ".j2c";
662 return file (p.string ());
665 /** Make an educated guess as to whether we have a complete DCP
667 * @return true if we do.
671 Film::have_dcp () const
674 libdcp::DCP dcp (dir (dcp_name()));
684 Film::make_player () const
686 return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
690 Film::playlist () const
692 boost::mutex::scoped_lock lm (_state_mutex);
696 Playlist::ContentList
697 Film::content () const
699 return _playlist->content ();
703 Film::examine_and_add_content (shared_ptr<Content> c)
705 shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
706 j->Finished.connect (bind (&Film::add_content_weak, this, boost::weak_ptr<Content> (c)));
707 JobManager::instance()->add (j);
711 Film::add_content_weak (weak_ptr<Content> c)
713 shared_ptr<Content> content = c.lock ();
715 add_content (content);
720 Film::add_content (shared_ptr<Content> c)
722 /* Add video content after any existing content */
723 if (dynamic_pointer_cast<VideoContent> (c)) {
724 c->set_start (_playlist->video_end ());
731 Film::remove_content (shared_ptr<Content> c)
733 _playlist->remove (c);
737 Film::length () const
739 return _playlist->length ();
743 Film::has_subtitles () const
745 return _playlist->has_subtitles ();
749 Film::best_dcp_video_frame_rate () const
751 return _playlist->best_dcp_frame_rate ();
755 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
757 if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
758 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
762 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
767 Film::playlist_changed ()
769 signal_changed (CONTENT);
775 return _playlist->loop ();
779 Film::set_loop (int c)
781 _playlist->set_loop (c);
785 Film::time_to_audio_frames (Time t) const
787 return t * dcp_audio_frame_rate () / TIME_HZ;
791 Film::time_to_video_frames (Time t) const
793 return t * dcp_video_frame_rate () / TIME_HZ;
797 Film::audio_frames_to_time (OutputAudioFrame f) const
799 return f * TIME_HZ / dcp_audio_frame_rate ();
803 Film::video_frames_to_time (OutputVideoFrame f) const
805 return f * TIME_HZ / dcp_video_frame_rate ();
809 Film::dcp_audio_frame_rate () const
816 Film::set_sequence_video (bool s)
818 _playlist->set_sequence_video (s);
822 Film::full_frame () const
824 return libdcp::Size (2048, 1080);