Fix save/load of Atmos asset lengths.
[dcpomatic.git] / src / lib / atmos_mxf_content.cc
1 /*
2     Copyright (C) 2016-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "atmos_mxf_content.h"
22 #include "job.h"
23 #include "film.h"
24 #include "compose.hpp"
25 #include <asdcp/KM_log.h>
26 #include <dcp/atmos_asset.h>
27 #include <dcp/exceptions.h>
28 #include <dcp/raw_convert.h>
29 #include <libxml++/libxml++.h>
30
31 #include "i18n.h"
32
33 using std::list;
34 using std::string;
35 using boost::shared_ptr;
36 using namespace dcpomatic;
37
38 AtmosMXFContent::AtmosMXFContent (boost::filesystem::path path)
39         : Content (path)
40 {
41
42 }
43
44 AtmosMXFContent::AtmosMXFContent (cxml::ConstNodePtr node, int)
45         : Content (node)
46 {
47         /* This was mistakenly left out for a while, so make sure we at least don't
48          * crash if an old Film is loaded.
49          */
50         _length = node->optional_number_child<Frame>("Length").get_value_or(0);
51 }
52
53 bool
54 AtmosMXFContent::valid_mxf (boost::filesystem::path path)
55 {
56         Kumu::DefaultLogSink().UnsetFilterFlag(Kumu::LOG_ALLOW_ALL);
57
58         try {
59                 shared_ptr<dcp::AtmosAsset> a (new dcp::AtmosAsset (path));
60                 return true;
61         } catch (dcp::MXFFileError& e) {
62
63         } catch (dcp::ReadError& e) {
64
65         }
66
67         Kumu::DefaultLogSink().SetFilterFlag(Kumu::LOG_ALLOW_ALL);
68
69         return false;
70 }
71
72 void
73 AtmosMXFContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
74 {
75         job->set_progress_unknown ();
76         Content::examine (film, job);
77         shared_ptr<dcp::AtmosAsset> a (new dcp::AtmosAsset (path(0)));
78
79         {
80                 boost::mutex::scoped_lock lm (_mutex);
81                 _length = a->intrinsic_duration ();
82         }
83 }
84
85 string
86 AtmosMXFContent::summary () const
87 {
88         return String::compose (_("%1 [Atmos]"), path_summary());
89 }
90
91 void
92 AtmosMXFContent::as_xml (xmlpp::Node* node, bool with_paths) const
93 {
94         node->add_child("Type")->add_child_text ("AtmosMXF");
95         Content::as_xml (node, with_paths);
96         node->add_child("Length")->add_child_text(dcp::raw_convert<string>(_length));
97 }
98
99 DCPTime
100 AtmosMXFContent::full_length (shared_ptr<const Film> film) const
101 {
102         FrameRateChange const frc (film, shared_from_this());
103         return DCPTime::from_frames (llrint (_length * frc.factor()), film->video_frame_rate());
104 }
105
106 DCPTime
107 AtmosMXFContent::approximate_length () const
108 {
109         return DCPTime::from_frames (_length, 24);
110 }