Add more video properties to content properties dialog.
[dcpomatic.git] / src / lib / content.cc
1 /*
2     Copyright (C) 2013-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 /** @file  src/lib/content.cc
21  *  @brief Content class.
22  */
23
24 #include "content.h"
25 #include "util.h"
26 #include "content_factory.h"
27 #include "exceptions.h"
28 #include "film.h"
29 #include "safe_stringstream.h"
30 #include "job.h"
31 #include "raw_convert.h"
32 #include <libcxml/cxml.h>
33 #include <libxml++/libxml++.h>
34 #include <boost/thread/mutex.hpp>
35
36 #include "i18n.h"
37
38 using std::string;
39 using std::list;
40 using std::cout;
41 using std::vector;
42 using std::max;
43 using std::pair;
44 using boost::shared_ptr;
45
46 int const ContentProperty::PATH = 400;
47 int const ContentProperty::POSITION = 401;
48 int const ContentProperty::LENGTH = 402;
49 int const ContentProperty::TRIM_START = 403;
50 int const ContentProperty::TRIM_END = 404;
51
52 Content::Content (shared_ptr<const Film> film)
53         : _film (film)
54         , _position (0)
55         , _trim_start (0)
56         , _trim_end (0)
57         , _change_signals_frequent (false)
58 {
59
60 }
61
62 Content::Content (shared_ptr<const Film> film, DCPTime p)
63         : _film (film)
64         , _position (p)
65         , _trim_start (0)
66         , _trim_end (0)
67         , _change_signals_frequent (false)
68 {
69
70 }
71
72 Content::Content (shared_ptr<const Film> film, boost::filesystem::path p)
73         : _film (film)
74         , _position (0)
75         , _trim_start (0)
76         , _trim_end (0)
77         , _change_signals_frequent (false)
78 {
79         _paths.push_back (p);
80 }
81
82 Content::Content (shared_ptr<const Film> film, cxml::ConstNodePtr node)
83         : _film (film)
84         , _change_signals_frequent (false)
85 {
86         list<cxml::NodePtr> path_children = node->node_children ("Path");
87         for (list<cxml::NodePtr>::const_iterator i = path_children.begin(); i != path_children.end(); ++i) {
88                 _paths.push_back ((*i)->content ());
89         }
90         _digest = node->optional_string_child ("Digest").get_value_or ("X");
91         _position = DCPTime (node->number_child<double> ("Position"));
92         _trim_start = DCPTime (node->number_child<double> ("TrimStart"));
93         _trim_end = DCPTime (node->number_child<double> ("TrimEnd"));
94 }
95
96 Content::Content (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
97         : _film (film)
98         , _position (c.front()->position ())
99         , _trim_start (c.front()->trim_start ())
100         , _trim_end (c.back()->trim_end ())
101         , _change_signals_frequent (false)
102 {
103         for (size_t i = 0; i < c.size(); ++i) {
104                 if (i > 0 && c[i]->trim_start() > DCPTime()) {
105                         throw JoinError (_("Only the first piece of content to be joined can have a start trim."));
106                 }
107
108                 if (i < (c.size() - 1) && c[i]->trim_end () > DCPTime()) {
109                         throw JoinError (_("Only the last piece of content to be joined can have an end trim."));
110                 }
111
112                 for (size_t j = 0; j < c[i]->number_of_paths(); ++j) {
113                         _paths.push_back (c[i]->path (j));
114                 }
115         }
116 }
117
118 void
119 Content::as_xml (xmlpp::Node* node) const
120 {
121         boost::mutex::scoped_lock lm (_mutex);
122
123         for (vector<boost::filesystem::path>::const_iterator i = _paths.begin(); i != _paths.end(); ++i) {
124                 node->add_child("Path")->add_child_text (i->string ());
125         }
126         node->add_child("Digest")->add_child_text (_digest);
127         node->add_child("Position")->add_child_text (raw_convert<string> (_position.get ()));
128         node->add_child("TrimStart")->add_child_text (raw_convert<string> (_trim_start.get ()));
129         node->add_child("TrimEnd")->add_child_text (raw_convert<string> (_trim_end.get ()));
130 }
131
132 void
133 Content::examine (shared_ptr<Job> job)
134 {
135         if (job) {
136                 job->sub (_("Computing digest"));
137         }
138
139         boost::mutex::scoped_lock lm (_mutex);
140         vector<boost::filesystem::path> p = _paths;
141         lm.unlock ();
142
143         /* Some content files are very big, so we use a poor man's
144            digest here: a MD5 of the first and last 1e6 bytes with the
145            size of the first file tacked on the end as a string.
146         */
147         string const d = md5_digest_head_tail (p, 1000000) + raw_convert<string> (boost::filesystem::file_size (p.front ()));
148
149         lm.lock ();
150         _digest = d;
151 }
152
153 void
154 Content::signal_changed (int p)
155 {
156         emit (boost::bind (boost::ref (Changed), shared_from_this (), p, _change_signals_frequent));
157 }
158
159 void
160 Content::set_position (DCPTime p)
161 {
162         {
163                 boost::mutex::scoped_lock lm (_mutex);
164                 if (p == _position) {
165                         return;
166                 }
167
168                 _position = p;
169         }
170
171         signal_changed (ContentProperty::POSITION);
172 }
173
174 void
175 Content::set_trim_start (DCPTime t)
176 {
177         {
178                 boost::mutex::scoped_lock lm (_mutex);
179                 _trim_start = t;
180         }
181
182         signal_changed (ContentProperty::TRIM_START);
183 }
184
185 void
186 Content::set_trim_end (DCPTime t)
187 {
188         {
189                 boost::mutex::scoped_lock lm (_mutex);
190                 _trim_end = t;
191         }
192
193         signal_changed (ContentProperty::TRIM_END);
194 }
195
196
197 shared_ptr<Content>
198 Content::clone () const
199 {
200         shared_ptr<const Film> film = _film.lock ();
201         if (!film) {
202                 return shared_ptr<Content> ();
203         }
204
205         /* This is a bit naughty, but I can't think of a compelling reason not to do it ... */
206         xmlpp::Document doc;
207         xmlpp::Node* node = doc.create_root_node ("Content");
208         as_xml (node);
209
210         /* notes is unused here (we assume) */
211         list<string> notes;
212         return content_factory (film, cxml::NodePtr (new cxml::Node (node)), Film::current_state_version, notes);
213 }
214
215 string
216 Content::technical_summary () const
217 {
218         return String::compose ("%1 %2 %3", path_summary(), digest(), position().seconds());
219 }
220
221 DCPTime
222 Content::length_after_trim () const
223 {
224         return max (DCPTime (), full_length() - trim_start() - trim_end());
225 }
226
227 /** @return string which includes everything about how this content affects
228  *  its playlist.
229  */
230 string
231 Content::identifier () const
232 {
233         SafeStringStream s;
234
235         s << Content::digest()
236           << "_" << position().get()
237           << "_" << trim_start().get()
238           << "_" << trim_end().get();
239
240         return s.str ();
241 }
242
243 bool
244 Content::paths_valid () const
245 {
246         for (vector<boost::filesystem::path>::const_iterator i = _paths.begin(); i != _paths.end(); ++i) {
247                 if (!boost::filesystem::exists (*i)) {
248                         return false;
249                 }
250         }
251
252         return true;
253 }
254
255 void
256 Content::set_path (boost::filesystem::path path)
257 {
258         _paths.clear ();
259         _paths.push_back (path);
260         signal_changed (ContentProperty::PATH);
261 }
262
263 string
264 Content::path_summary () const
265 {
266         /* XXX: should handle multiple paths more gracefully */
267
268         DCPOMATIC_ASSERT (number_of_paths ());
269
270         string s = path(0).filename().string ();
271         if (number_of_paths() > 1) {
272                 s += " ...";
273         }
274
275         return s;
276 }
277
278 /** @return a list of properties that might be interesting to the user; first string is the property name,
279  *  second is the value.
280  */
281 list<pair<string, string> >
282 Content::properties () const
283 {
284         list<pair<string, string> > p;
285         add_properties (p);
286         return p;
287 }