Various fixes to make audio analysis sort-of work.
[dcpomatic.git] / src / lib / job.cc
1 /*
2     Copyright (C) 2012 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/job.cc
21  *  @brief A parent class to represent long-running tasks which are run in their own thread.
22  */
23
24 #include <boost/thread.hpp>
25 #include <boost/filesystem.hpp>
26 #include <libdcp/exceptions.h>
27 #include "job.h"
28 #include "util.h"
29 #include "cross.h"
30
31 #include "i18n.h"
32
33 using std::string;
34 using std::list;
35 using std::stringstream;
36 using boost::shared_ptr;
37
38 Job::Job (shared_ptr<const Film> f)
39         : _film (f)
40         , _thread (0)
41         , _state (NEW)
42         , _start_time (0)
43         , _progress_unknown (false)
44         , _ran_for (0)
45 {
46         descend (1);
47 }
48
49 /** Start the job in a separate thread, returning immediately */
50 void
51 Job::start ()
52 {
53         set_state (RUNNING);
54         _start_time = time (0);
55         _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
56 }
57
58 /** A wrapper for the ::run() method to catch exceptions */
59 void
60 Job::run_wrapper ()
61 {
62         try {
63
64                 run ();
65
66         } catch (libdcp::FileError& e) {
67                 
68                 set_progress (1);
69                 set_state (FINISHED_ERROR);
70                 
71                 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
72
73                 try {
74                         boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
75                         if (s.available < pow (1024, 3)) {
76                                 m += N_("\n\n");
77                                 m += _("The drive that the film is stored on is low in disc space.  Free some more space and try again.");
78                         }
79                 } catch (...) {
80
81                 }
82
83                 set_error (e.what(), m);
84
85         } catch (boost::thread_interrupted &) {
86
87                 set_state (FINISHED_CANCELLED);
88                 
89         } catch (std::exception& e) {
90
91                 set_progress (1);
92                 set_state (FINISHED_ERROR);
93                 set_error (
94                         e.what (),
95                         _("It is not known what caused this error.  The best idea is to report the problem to the DCP-o-matic mailing list (dcpomatic@carlh.net)")
96                         );
97
98         } catch (...) {
99
100                 set_progress (1);
101                 set_state (FINISHED_ERROR);
102                 set_error (
103                         _("Unknown error"),
104                         _("It is not known what caused this error.  The best idea is to report the problem to the DCP-o-matic mailing list (dcpomatic@carlh.net)")
105                         );
106
107         }
108 }
109
110 /** @return true if this job is new (ie has not started running) */
111 bool
112 Job::is_new () const
113 {
114         boost::mutex::scoped_lock lm (_state_mutex);
115         return _state == NEW;
116 }
117
118 /** @return true if the job is running */
119 bool
120 Job::running () const
121 {
122         boost::mutex::scoped_lock lm (_state_mutex);
123         return _state == RUNNING;
124 }
125
126 /** @return true if the job has finished (either successfully or unsuccessfully) */
127 bool
128 Job::finished () const
129 {
130         boost::mutex::scoped_lock lm (_state_mutex);
131         return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
132 }
133
134 /** @return true if the job has finished successfully */
135 bool
136 Job::finished_ok () const
137 {
138         boost::mutex::scoped_lock lm (_state_mutex);
139         return _state == FINISHED_OK;
140 }
141
142 /** @return true if the job has finished unsuccessfully */
143 bool
144 Job::finished_in_error () const
145 {
146         boost::mutex::scoped_lock lm (_state_mutex);
147         return _state == FINISHED_ERROR;
148 }
149
150 bool
151 Job::finished_cancelled () const
152 {
153         boost::mutex::scoped_lock lm (_state_mutex);
154         return _state == FINISHED_CANCELLED;
155 }
156
157 bool
158 Job::paused () const
159 {
160         boost::mutex::scoped_lock lm (_state_mutex);
161         return _state == PAUSED;
162 }
163         
164 /** Set the state of this job.
165  *  @param s New state.
166  */
167 void
168 Job::set_state (State s)
169 {
170         boost::mutex::scoped_lock lm (_state_mutex);
171         _state = s;
172
173         if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
174                 _ran_for = elapsed_time ();
175         }
176 }
177
178 /** @return Time (in seconds) that this job has been running */
179 int
180 Job::elapsed_time () const
181 {
182         if (_start_time == 0) {
183                 return 0;
184         }
185         
186         return time (0) - _start_time;
187 }
188
189 /** Set the progress of the current part of the job.
190  *  @param p Progress (from 0 to 1)
191  */
192 void
193 Job::set_progress (float p)
194 {
195         boost::mutex::scoped_lock lm (_progress_mutex);
196         _progress_unknown = false;
197         _stack.back().normalised = p;
198         boost::this_thread::interruption_point ();
199
200         if (paused ()) {
201                 dcpomatic_sleep (1);
202         }
203 }
204
205 /** @return fractional overall progress, or -1 if not known */
206 float
207 Job::overall_progress () const
208 {
209         boost::mutex::scoped_lock lm (_progress_mutex);
210         if (_progress_unknown) {
211                 return -1;
212         }
213
214         float overall = 0;
215         float factor = 1;
216         for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
217                 factor *= i->allocation;
218                 overall += i->normalised * factor;
219         }
220
221         if (overall > 1) {
222                 overall = 1;
223         }
224         
225         return overall;
226 }
227
228 /** Ascend up one level in terms of progress reporting; see descend() */
229 void
230 Job::ascend ()
231 {
232         boost::mutex::scoped_lock lm (_progress_mutex);
233         
234         assert (!_stack.empty ());
235         float const a = _stack.back().allocation;
236         _stack.pop_back ();
237         _stack.back().normalised += a;
238 }
239
240 /** Descend down one level in terms of progress reporting; e.g. if
241  *  there is a task which is split up into N subtasks, each of which
242  *  report their progress from 0 to 100%, call descend() before executing
243  *  each subtask, and ascend() afterwards to ensure that overall progress
244  *  is reported correctly.
245  *
246  *  @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
247  */
248 void
249 Job::descend (float a)
250 {
251         boost::mutex::scoped_lock lm (_progress_mutex);
252         _stack.push_back (Level (a));
253 }
254
255 string
256 Job::error_details () const
257 {
258         boost::mutex::scoped_lock lm (_state_mutex);
259         return _error_details;
260 }
261
262 /** @return A summary of any error that the job has generated */
263 string
264 Job::error_summary () const
265 {
266         boost::mutex::scoped_lock lm (_state_mutex);
267         return _error_summary;
268 }
269
270 /** Set the current error string.
271  *  @param e New error string.
272  */
273 void
274 Job::set_error (string s, string d)
275 {
276         boost::mutex::scoped_lock lm (_state_mutex);
277         _error_summary = s;
278         _error_details = d;
279 }
280
281 /** Say that this job's progress will be unknown until further notice */
282 void
283 Job::set_progress_unknown ()
284 {
285         boost::mutex::scoped_lock lm (_progress_mutex);
286         _progress_unknown = true;
287 }
288
289 /** @return Human-readable status of this job */
290 string
291 Job::status () const
292 {
293         float const p = overall_progress ();
294         int const t = elapsed_time ();
295         int const r = remaining_time ();
296
297         int pc = rint (p * 100);
298         if (pc == 100) {
299                 /* 100% makes it sound like we've finished when we haven't */
300                 pc = 99;
301         }
302
303         stringstream s;
304         if (!finished ()) {
305                 s << pc << N_("%");
306                 if (p >= 0 && t > 10 && r > 0) {
307                         /// TRANSLATORS: remaining here follows an amount of time that is remaining
308                         /// on an operation.
309                         s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
310                 }
311         } else if (finished_ok ()) {
312                 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
313         } else if (finished_in_error ()) {
314                 s << String::compose (_("Error (%1)"), error_summary());
315         } else if (finished_cancelled ()) {
316                 s << _("Cancelled");
317         }
318
319         return s.str ();
320 }
321
322 /** @return An estimate of the remaining time for this job, in seconds */
323 int
324 Job::remaining_time () const
325 {
326         return elapsed_time() / overall_progress() - elapsed_time();
327 }
328
329 void
330 Job::cancel ()
331 {
332         if (!_thread) {
333                 return;
334         }
335
336         _thread->interrupt ();
337         _thread->join ();
338 }
339
340 void
341 Job::pause ()
342 {
343         if (running ()) {
344                 set_state (PAUSED);
345         }
346 }
347
348 void
349 Job::resume ()
350 {
351         if (paused ()) {
352                 set_state (RUNNING);
353         }
354 }