Merge branch 'master' into 12bit
[dcpomatic.git] / src / lib / content.cc
1 /*
2     Copyright (C) 2013 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 <boost/thread/mutex.hpp>
21 #include <libxml++/libxml++.h>
22 #include <libcxml/cxml.h>
23 #include <libdcp/raw_convert.h>
24 #include "content.h"
25 #include "util.h"
26 #include "content_factory.h"
27 #include "ui_signaller.h"
28 #include "exceptions.h"
29 #include "film.h"
30 #include "safe_stringstream.h"
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::set;
36 using std::list;
37 using std::cout;
38 using std::vector;
39 using std::max;
40 using boost::shared_ptr;
41 using libdcp::raw_convert;
42
43 int const ContentProperty::PATH = 400;
44 int const ContentProperty::POSITION = 401;
45 int const ContentProperty::LENGTH = 402;
46 int const ContentProperty::TRIM_START = 403;
47 int const ContentProperty::TRIM_END = 404;
48
49 Content::Content (shared_ptr<const Film> f)
50         : _film (f)
51         , _position (0)
52         , _trim_start (0)
53         , _trim_end (0)
54         , _change_signals_frequent (false)
55 {
56
57 }
58
59 Content::Content (shared_ptr<const Film> f, Time p)
60         : _film (f)
61         , _position (p)
62         , _trim_start (0)
63         , _trim_end (0)
64         , _change_signals_frequent (false)
65 {
66
67 }
68
69 Content::Content (shared_ptr<const Film> f, boost::filesystem::path p)
70         : _film (f)
71         , _position (0)
72         , _trim_start (0)
73         , _trim_end (0)
74         , _change_signals_frequent (false)
75 {
76         _paths.push_back (p);
77 }
78
79 Content::Content (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
80         : _film (f)
81         , _change_signals_frequent (false)
82 {
83         list<cxml::NodePtr> path_children = node->node_children ("Path");
84         for (list<cxml::NodePtr>::const_iterator i = path_children.begin(); i != path_children.end(); ++i) {
85                 _paths.push_back ((*i)->content ());
86         }
87         _digest = node->string_child ("Digest");
88         _position = node->number_child<Time> ("Position");
89         _trim_start = node->number_child<Time> ("TrimStart");
90         _trim_end = node->number_child<Time> ("TrimEnd");
91 }
92
93 Content::Content (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
94         : _film (f)
95         , _position (c.front()->position ())
96         , _trim_start (c.front()->trim_start ())
97         , _trim_end (c.back()->trim_end ())
98         , _change_signals_frequent (false)
99 {
100         for (size_t i = 0; i < c.size(); ++i) {
101                 if (i > 0 && c[i]->trim_start ()) {
102                         throw JoinError (_("Only the first piece of content to be joined can have a start trim."));
103                 }
104
105                 if (i < (c.size() - 1) && c[i]->trim_end ()) {
106                         throw JoinError (_("Only the last piece of content to be joined can have an end trim."));
107                 }
108
109                 for (size_t j = 0; j < c[i]->number_of_paths(); ++j) {
110                         _paths.push_back (c[i]->path (j));
111                 }
112         }
113 }
114
115 void
116 Content::as_xml (xmlpp::Node* node) const
117 {
118         boost::mutex::scoped_lock lm (_mutex);
119
120         for (vector<boost::filesystem::path>::const_iterator i = _paths.begin(); i != _paths.end(); ++i) {
121                 node->add_child("Path")->add_child_text (i->string ());
122         }
123         node->add_child("Digest")->add_child_text (_digest);
124         node->add_child("Position")->add_child_text (raw_convert<string> (_position));
125         node->add_child("TrimStart")->add_child_text (raw_convert<string> (_trim_start));
126         node->add_child("TrimEnd")->add_child_text (raw_convert<string> (_trim_end));
127 }
128
129 void
130 Content::examine (shared_ptr<Job> job)
131 {
132         boost::mutex::scoped_lock lm (_mutex);
133         vector<boost::filesystem::path> p = _paths;
134         lm.unlock ();
135         
136         string const d = md5_digest (p, job);
137
138         lm.lock ();
139         _digest = d;
140 }
141
142 void
143 Content::signal_changed (int p)
144 {
145         if (ui_signaller) {
146                 ui_signaller->emit (boost::bind (boost::ref (Changed), shared_from_this (), p, _change_signals_frequent));
147         }
148 }
149
150 void
151 Content::set_position (Time p)
152 {
153         {
154                 boost::mutex::scoped_lock lm (_mutex);
155                 if (p == _position) {
156                         return;
157                 }
158                 
159                 _position = p;
160         }
161
162         signal_changed (ContentProperty::POSITION);
163 }
164
165 void
166 Content::set_trim_start (Time t)
167 {
168         {
169                 boost::mutex::scoped_lock lm (_mutex);
170                 _trim_start = t;
171         }
172
173         signal_changed (ContentProperty::TRIM_START);
174 }
175
176 void
177 Content::set_trim_end (Time t)
178 {
179         {
180                 boost::mutex::scoped_lock lm (_mutex);
181                 _trim_end = t;
182         }
183
184         signal_changed (ContentProperty::TRIM_END);
185 }
186
187
188 shared_ptr<Content>
189 Content::clone () const
190 {
191         shared_ptr<const Film> film = _film.lock ();
192         if (!film) {
193                 return shared_ptr<Content> ();
194         }
195         
196         /* This is a bit naughty, but I can't think of a compelling reason not to do it ... */
197         xmlpp::Document doc;
198         xmlpp::Node* node = doc.create_root_node ("Content");
199         as_xml (node);
200
201         /* notes is unused here (we assume) */
202         list<string> notes;
203         return content_factory (film, cxml::NodePtr (new cxml::Node (node)), Film::current_state_version, notes);
204 }
205
206 string
207 Content::technical_summary () const
208 {
209         return String::compose ("%1 %2 %3", path_summary(), digest(), position());
210 }
211
212 Time
213 Content::length_after_trim () const
214 {
215         return max (int64_t (0), full_length() - trim_start() - trim_end());
216 }
217
218 /** @param t A time relative to the start of this content (not the position).
219  *  @return true if this time is trimmed by our trim settings.
220  */
221 bool
222 Content::trimmed (Time t) const
223 {
224         return (t < trim_start() || t > (full_length() - 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()
237           << "_" << trim_start()
238           << "_" << trim_end();
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         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 }