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