0c89685313337b69e1584df648240bd0c7c3d71f
[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> f, boost::filesystem::path p)
41         : Content (f)
42         , VideoContent (f)
43         , SingleStreamAudioContent (f)
44         , SubtitleContent (f)
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> f, cxml::ConstNodePtr node, int version)
55         : Content (f, node)
56         , VideoContent (f, node, version)
57         , SingleStreamAudioContent (f, node, version)
58         , SubtitleContent (f, 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         boost::mutex::scoped_lock lm (_mutex);
94         _name = examiner->name ();
95         _has_subtitles = examiner->has_subtitles ();
96         _encrypted = examiner->encrypted ();
97         _kdm_valid = examiner->kdm_valid ();
98
99         if (could_be_played != can_be_played ()) {
100                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
101         }
102 }
103
104 string
105 DCPContent::summary () const
106 {
107         boost::mutex::scoped_lock lm (_mutex);
108         return String::compose (_("%1 [DCP]"), _name);
109 }
110
111 string
112 DCPContent::technical_summary () const
113 {
114         return Content::technical_summary() + " - "
115                 + VideoContent::technical_summary() + " - "
116                 + AudioContent::technical_summary() + " - ";
117 }
118
119 void
120 DCPContent::as_xml (xmlpp::Node* node) const
121 {
122         node->add_child("Type")->add_child_text ("DCP");
123
124         Content::as_xml (node);
125         VideoContent::as_xml (node);
126         SingleStreamAudioContent::as_xml (node);
127         SubtitleContent::as_xml (node);
128
129         boost::mutex::scoped_lock lm (_mutex);
130         node->add_child("Name")->add_child_text (_name);
131         node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
132         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
133         if (_kdm) {
134                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
135         }
136         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
137 }
138
139 DCPTime
140 DCPContent::full_length () const
141 {
142         shared_ptr<const Film> film = _film.lock ();
143         DCPOMATIC_ASSERT (film);
144         FrameRateChange const frc (video_frame_rate (), film->video_frame_rate ());
145         return DCPTime::from_frames (rint (video_length () * frc.factor ()), film->video_frame_rate ());
146 }
147
148 string
149 DCPContent::identifier () const
150 {
151         return SubtitleContent::identifier ();
152 }
153
154 void
155 DCPContent::add_kdm (dcp::EncryptedKDM k)
156 {
157         _kdm = k;
158 }
159
160 bool
161 DCPContent::can_be_played () const
162 {
163         return !_encrypted || _kdm_valid;
164 }
165
166 boost::filesystem::path
167 DCPContent::directory () const
168 {
169         optional<size_t> smallest;
170         boost::filesystem::path dir;
171         for (size_t i = 0; i < number_of_paths(); ++i) {
172                 boost::filesystem::path const p = path (i).parent_path ();
173                 size_t const d = distance (p.begin(), p.end());
174                 if (!smallest || d < smallest.get ()) {
175                         dir = p;
176                 }
177         }
178
179         return dir;
180 }