Improve transcode job progress reporting.
[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
30 using namespace std;
31 using namespace boost;
32
33 /** @param s FilmState for the film that we are operating on.
34  *  @param o Options.
35  *  @param l A log that we can write to.
36  */
37 Job::Job (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
38         : _fs (s)
39         , _opt (o)
40         , _log (l)
41         , _state (NEW)
42         , _start_time (0)
43         , _progress_unknown (false)
44 {
45         assert (_log);
46         
47         descend (1);
48 }
49
50 /** Start the job in a separate thread, returning immediately */
51 void
52 Job::start ()
53 {
54         set_state (RUNNING);
55         _start_time = time (0);
56         boost::thread (boost::bind (&Job::run_wrapper, this));
57 }
58
59 /** A wrapper for the ::run() method to catch exceptions */
60 void
61 Job::run_wrapper ()
62 {
63         try {
64
65                 run ();
66
67         } catch (libdcp::FileError& e) {
68                 
69                 set_progress (1);
70                 set_state (FINISHED_ERROR);
71                 stringstream s;
72                 s << e.what() << "(" << filesystem::path (e.filename()).leaf() << ")";
73                 set_error (s.str ());
74                 
75         } catch (std::exception& e) {
76
77                 set_progress (1);
78                 set_state (FINISHED_ERROR);
79                 set_error (e.what ());
80
81         }
82 }
83
84 /** @return true if the job is running */
85 bool
86 Job::running () const
87 {
88         boost::mutex::scoped_lock lm (_state_mutex);
89         return _state == RUNNING;
90 }
91
92 /** @return true if the job has finished (either successfully or unsuccessfully) */
93 bool
94 Job::finished () const
95 {
96         boost::mutex::scoped_lock lm (_state_mutex);
97         return _state == FINISHED_OK || _state == FINISHED_ERROR;
98 }
99
100 /** @return true if the job has finished successfully */
101 bool
102 Job::finished_ok () const
103 {
104         boost::mutex::scoped_lock lm (_state_mutex);
105         return _state == FINISHED_OK;
106 }
107
108 /** @return true if the job has finished unsuccessfully */
109 bool
110 Job::finished_in_error () const
111 {
112         boost::mutex::scoped_lock lm (_state_mutex);
113         return _state == FINISHED_ERROR;
114 }
115
116 /** Set the state of this job.
117  *  @param s New state.
118  */
119 void
120 Job::set_state (State s)
121 {
122         boost::mutex::scoped_lock lm (_state_mutex);
123         _state = s;
124 }
125
126 /** A hack to work around our lack of cross-thread
127  *  signalling; this emits Finished, and listeners
128  *  assume that it will be emitted in the GUI thread,
129  *  so this method must be called from the GUI thread.
130  */
131 void
132 Job::emit_finished ()
133 {
134         Finished ();
135 }
136
137 /** @return Time (in seconds) that this job has been running */
138 int
139 Job::elapsed_time () const
140 {
141         if (_start_time == 0) {
142                 return 0;
143         }
144         
145         return time (0) - _start_time;
146 }
147
148 /** Set the progress of the current part of the job.
149  *  @param p Progress (from 0 to 1)
150  */
151 void
152 Job::set_progress (float p)
153 {
154         boost::mutex::scoped_lock lm (_progress_mutex);
155         _stack.back().normalised = p;
156 }
157
158 /** @return fractional overall progress, or -1 if not known */
159 float
160 Job::overall_progress () const
161 {
162         boost::mutex::scoped_lock lm (_progress_mutex);
163         if (_progress_unknown) {
164                 return -1;
165         }
166
167         float overall = 0;
168         float factor = 1;
169         for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
170                 factor *= i->allocation;
171                 overall += i->normalised * factor;
172         }
173
174         if (overall > 1) {
175                 overall = 1;
176         }
177         
178         return overall;
179 }
180
181 /** Ascend up one level in terms of progress reporting; see descend() */
182 void
183 Job::ascend ()
184 {
185         boost::mutex::scoped_lock lm (_progress_mutex);
186         
187         assert (!_stack.empty ());
188         float const a = _stack.back().allocation;
189         _stack.pop_back ();
190         _stack.back().normalised += a;
191 }
192
193 /** Descend down one level in terms of progress reporting; e.g. if
194  *  there is a task which is split up into N subtasks, each of which
195  *  report their progress from 0 to 100%, call descend() before executing
196  *  each subtask, and ascend() afterwards to ensure that overall progress
197  *  is reported correctly.
198  *
199  *  @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
200  */
201 void
202 Job::descend (float a)
203 {
204         boost::mutex::scoped_lock lm (_progress_mutex);
205         _stack.push_back (Level (a));
206 }
207
208 /** @return Any error string that the job has generated */
209 string
210 Job::error () const
211 {
212         boost::mutex::scoped_lock lm (_state_mutex);
213         return _error;
214 }
215
216 /** Set the current error string.
217  *  @param e New error string.
218  */
219 void
220 Job::set_error (string e)
221 {
222         boost::mutex::scoped_lock lm (_state_mutex);
223         _error = e;
224 }
225
226 /** Set that this job's progress will always be unknown */
227 void
228 Job::set_progress_unknown ()
229 {
230         boost::mutex::scoped_lock lm (_progress_mutex);
231         _progress_unknown = true;
232 }
233
234 string
235 Job::status () const
236 {
237         float const p = overall_progress ();
238         int const t = elapsed_time ();
239         int const r = remaining_time ();
240         
241         stringstream s;
242         if (!finished () && p >= 0 && t > 10 && r > 0) {
243                 s << rint (p * 100) << "%; " << seconds_to_approximate_hms (r) << " remaining";
244         } else if (!finished () && (t <= 10 || r == 0)) {
245                 s << rint (p * 100) << "%";
246         } else if (finished_ok ()) {
247                 s << "OK (ran for " << seconds_to_hms (t) << ")";
248         } else if (finished_in_error ()) {
249                 s << "Error (" << error() << ")";
250         }
251
252         return s.str ();
253 }
254
255 int
256 Job::remaining_time () const
257 {
258         return elapsed_time() / overall_progress() - elapsed_time();
259 }