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