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