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