Back-end for very basic and hacky VF support for a DCP imported as content.
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014-2015 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 "dcp_content.h"
21 #include "dcp_examiner.h"
22 #include "job.h"
23 #include "film.h"
24 #include "config.h"
25 #include "compose.hpp"
26 #include <dcp/dcp.h>
27 #include <dcp/exceptions.h>
28 #include <libxml++/libxml++.h>
29 #include <iterator>
30 #include <iostream>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::cout;
36 using std::distance;
37 using std::pair;
38 using std::list;
39 using boost::shared_ptr;
40 using boost::optional;
41
42 int const DCPContentProperty::CAN_BE_PLAYED = 600;
43
44 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
45         : Content (film)
46         , VideoContent (film)
47         , SingleStreamAudioContent (film)
48         , SubtitleContent (film)
49         , _has_subtitles (false)
50         , _encrypted (false)
51         , _kdm_valid (false)
52         , _reference_video (false)
53         , _reference_audio (false)
54         , _reference_subtitle (false)
55 {
56         read_directory (p);
57 }
58
59 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
60         : Content (film, node)
61         , VideoContent (film, node, version)
62         , SingleStreamAudioContent (film, node, version)
63         , SubtitleContent (film, node, version)
64 {
65         _name = node->string_child ("Name");
66         _has_subtitles = node->bool_child ("HasSubtitles");
67         _encrypted = node->bool_child ("Encrypted");
68         if (node->optional_node_child ("KDM")) {
69                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
70         }
71         _kdm_valid = node->bool_child ("KDMValid");
72         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
73         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
74         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
75 }
76
77 void
78 DCPContent::read_directory (boost::filesystem::path p)
79 {
80         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
81                 if (boost::filesystem::is_regular_file (i->path ())) {
82                         _paths.push_back (i->path ());
83                 } else if (boost::filesystem::is_directory (i->path ())) {
84                         read_directory (i->path ());
85                 }
86         }
87 }
88
89 void
90 DCPContent::examine (shared_ptr<Job> job)
91 {
92         bool const could_be_played = can_be_played ();
93
94         job->set_progress_unknown ();
95         Content::examine (job);
96
97         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
98         take_from_video_examiner (examiner);
99         take_from_audio_examiner (examiner);
100
101         {
102                 boost::mutex::scoped_lock lm (_mutex);
103                 _name = examiner->name ();
104                 _has_subtitles = examiner->has_subtitles ();
105                 _encrypted = examiner->encrypted ();
106                 _kdm_valid = examiner->kdm_valid ();
107         }
108
109         if (could_be_played != can_be_played ()) {
110                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
111         }
112 }
113
114 string
115 DCPContent::summary () const
116 {
117         boost::mutex::scoped_lock lm (_mutex);
118         return String::compose (_("%1 [DCP]"), _name);
119 }
120
121 string
122 DCPContent::technical_summary () const
123 {
124         return Content::technical_summary() + " - "
125                 + VideoContent::technical_summary() + " - "
126                 + AudioContent::technical_summary() + " - ";
127 }
128
129 void
130 DCPContent::as_xml (xmlpp::Node* node) const
131 {
132         node->add_child("Type")->add_child_text ("DCP");
133
134         Content::as_xml (node);
135         VideoContent::as_xml (node);
136         SingleStreamAudioContent::as_xml (node);
137         SubtitleContent::as_xml (node);
138
139         boost::mutex::scoped_lock lm (_mutex);
140         node->add_child("Name")->add_child_text (_name);
141         node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
142         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
143         if (_kdm) {
144                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
145         }
146         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
147         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
148         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
149         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
150 }
151
152 DCPTime
153 DCPContent::full_length () const
154 {
155         shared_ptr<const Film> film = _film.lock ();
156         DCPOMATIC_ASSERT (film);
157         FrameRateChange const frc (video_frame_rate (), film->video_frame_rate ());
158         return DCPTime::from_frames (llrint (video_length () * frc.factor ()), film->video_frame_rate ());
159 }
160
161 string
162 DCPContent::identifier () const
163 {
164         SafeStringStream s;
165         s << VideoContent::identifier() << "_" << SubtitleContent::identifier () << " "
166           << (_reference_video ? "1" : "0")
167           << (_reference_subtitle ? "1" : "0");
168         return s.str ();
169 }
170
171 void
172 DCPContent::add_kdm (dcp::EncryptedKDM k)
173 {
174         _kdm = k;
175 }
176
177 bool
178 DCPContent::can_be_played () const
179 {
180         boost::mutex::scoped_lock lm (_mutex);
181         return !_encrypted || _kdm_valid;
182 }
183
184 boost::filesystem::path
185 DCPContent::directory () const
186 {
187         optional<size_t> smallest;
188         boost::filesystem::path dir;
189         for (size_t i = 0; i < number_of_paths(); ++i) {
190                 boost::filesystem::path const p = path (i).parent_path ();
191                 size_t const d = distance (p.begin(), p.end());
192                 if (!smallest || d < smallest.get ()) {
193                         dir = p;
194                 }
195         }
196
197         return dir;
198 }
199
200 void
201 DCPContent::add_properties (list<pair<string, string> >& p) const
202 {
203         SingleStreamAudioContent::add_properties (p);
204 }
205
206 void
207 DCPContent::set_default_colour_conversion ()
208 {
209         /* Default to no colour conversion for DCPs */
210         unset_colour_conversion ();
211 }