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