Hand-apply 6a3cd511559433554ab40ed72ff94b7d8dc2c5bd from master;
[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/dcp.h>
21 #include <dcp/exceptions.h>
22 #include "dcp_content.h"
23 #include "dcp_examiner.h"
24 #include "job.h"
25 #include "film.h"
26 #include "config.h"
27 #include "compose.hpp"
28
29 #include "i18n.h"
30
31 using std::string;
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::optional;
35
36 int const DCPContentProperty::CAN_BE_PLAYED = 600;
37
38 DCPContent::DCPContent (shared_ptr<const Film> f, boost::filesystem::path p)
39         : Content (f)
40         , VideoContent (f)
41         , SingleStreamAudioContent (f)
42         , SubtitleContent (f)
43         , _has_subtitles (false)
44         , _encrypted (false)
45         , _directory (p)
46         , _kdm_valid (false)
47 {
48         read_directory (p);
49 }
50
51 DCPContent::DCPContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
52         : Content (f, node)
53         , VideoContent (f, node, version)
54         , SingleStreamAudioContent (f, node, version)
55         , SubtitleContent (f, node, version)
56 {
57         _name = node->string_child ("Name");
58         _has_subtitles = node->bool_child ("HasSubtitles");
59         _directory = node->string_child ("Directory");
60         _encrypted = node->bool_child ("Encrypted");
61         if (node->optional_node_child ("KDM")) {
62                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
63         }
64         _kdm_valid = node->bool_child ("KDMValid");
65 }
66
67 void
68 DCPContent::read_directory (boost::filesystem::path p)
69 {
70         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
71                 if (boost::filesystem::is_regular_file (i->path ())) {
72                         _paths.push_back (i->path ());
73                 } else if (boost::filesystem::is_directory (i->path ())) {
74                         read_directory (i->path ());
75                 }
76         }
77 }
78
79 void
80 DCPContent::examine (shared_ptr<Job> job, bool calculate_digest)
81 {
82         bool const could_be_played = can_be_played ();
83                 
84         job->set_progress_unknown ();
85         Content::examine (job, calculate_digest);
86         
87         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
88         take_from_video_examiner (examiner);
89         take_from_audio_examiner (examiner);
90
91         boost::mutex::scoped_lock lm (_mutex);
92         _name = examiner->name ();
93         _has_subtitles = examiner->has_subtitles ();
94         _encrypted = examiner->encrypted ();
95         _kdm_valid = examiner->kdm_valid ();
96
97         if (could_be_played != can_be_played ()) {
98                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
99         }
100 }
101
102 string
103 DCPContent::summary () const
104 {
105         boost::mutex::scoped_lock lm (_mutex);
106         return String::compose (_("%1 [DCP]"), _name);
107 }
108
109 string
110 DCPContent::technical_summary () const
111 {
112         return Content::technical_summary() + " - "
113                 + VideoContent::technical_summary() + " - "
114                 + AudioContent::technical_summary() + " - ";
115 }
116
117 void
118 DCPContent::as_xml (xmlpp::Node* node) const
119 {
120         node->add_child("Type")->add_child_text ("DCP");
121
122         Content::as_xml (node);
123         VideoContent::as_xml (node);
124         SingleStreamAudioContent::as_xml (node);
125         SubtitleContent::as_xml (node);
126
127         boost::mutex::scoped_lock lm (_mutex);
128         node->add_child("Name")->add_child_text (_name);
129         node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
130         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
131         node->add_child("Directory")->add_child_text (_directory.string ());
132         if (_kdm) {
133                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
134         }
135         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
136 }
137
138 DCPTime
139 DCPContent::full_length () const
140 {
141         shared_ptr<const Film> film = _film.lock ();
142         assert (film);
143         return DCPTime (video_length (), FrameRateChange (video_frame_rate (), film->video_frame_rate ()));
144 }
145
146 string
147 DCPContent::identifier () const
148 {
149         return SubtitleContent::identifier ();
150 }
151
152 void
153 DCPContent::add_kdm (dcp::EncryptedKDM k)
154 {
155         _kdm = k;
156 }
157
158 bool
159 DCPContent::can_be_played () const
160 {
161         return !_encrypted || _kdm_valid;
162 }