Include tidying src/lib/a-j*.h
[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         /* Default to no colour conversion for DCPs */
54         unset_colour_conversion (false);
55 }
56
57 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
58         : Content (film, node)
59         , VideoContent (film, node, version)
60         , SingleStreamAudioContent (film, node, version)
61         , SubtitleContent (film, node, version)
62 {
63         _name = node->string_child ("Name");
64         _has_subtitles = node->bool_child ("HasSubtitles");
65         _encrypted = node->bool_child ("Encrypted");
66         if (node->optional_node_child ("KDM")) {
67                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
68         }
69         _kdm_valid = node->bool_child ("KDMValid");
70 }
71
72 void
73 DCPContent::read_directory (boost::filesystem::path p)
74 {
75         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
76                 if (boost::filesystem::is_regular_file (i->path ())) {
77                         _paths.push_back (i->path ());
78                 } else if (boost::filesystem::is_directory (i->path ())) {
79                         read_directory (i->path ());
80                 }
81         }
82 }
83
84 void
85 DCPContent::examine (shared_ptr<Job> job)
86 {
87         bool const could_be_played = can_be_played ();
88
89         job->set_progress_unknown ();
90         Content::examine (job);
91
92         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
93         take_from_video_examiner (examiner);
94         take_from_audio_examiner (examiner);
95
96         {
97                 boost::mutex::scoped_lock lm (_mutex);
98                 _name = examiner->name ();
99                 _has_subtitles = examiner->has_subtitles ();
100                 _encrypted = examiner->encrypted ();
101                 _kdm_valid = examiner->kdm_valid ();
102         }
103
104         if (could_be_played != can_be_played ()) {
105                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
106         }
107 }
108
109 string
110 DCPContent::summary () const
111 {
112         boost::mutex::scoped_lock lm (_mutex);
113         return String::compose (_("%1 [DCP]"), _name);
114 }
115
116 string
117 DCPContent::technical_summary () const
118 {
119         return Content::technical_summary() + " - "
120                 + VideoContent::technical_summary() + " - "
121                 + AudioContent::technical_summary() + " - ";
122 }
123
124 void
125 DCPContent::as_xml (xmlpp::Node* node) const
126 {
127         node->add_child("Type")->add_child_text ("DCP");
128
129         Content::as_xml (node);
130         VideoContent::as_xml (node);
131         SingleStreamAudioContent::as_xml (node);
132         SubtitleContent::as_xml (node);
133
134         boost::mutex::scoped_lock lm (_mutex);
135         node->add_child("Name")->add_child_text (_name);
136         node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
137         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
138         if (_kdm) {
139                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
140         }
141         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
142 }
143
144 DCPTime
145 DCPContent::full_length () const
146 {
147         shared_ptr<const Film> film = _film.lock ();
148         DCPOMATIC_ASSERT (film);
149         FrameRateChange const frc (video_frame_rate (), film->video_frame_rate ());
150         return DCPTime::from_frames (llrint (video_length () * frc.factor ()), film->video_frame_rate ());
151 }
152
153 string
154 DCPContent::identifier () const
155 {
156         return SubtitleContent::identifier ();
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 }