Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / transcode_job.cc
1 /*
2     Copyright (C) 2012-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/transcode_job.cc
21  *  @brief A job which transcodes from one format to another.
22  */
23
24 #include "transcode_job.h"
25 #include "film.h"
26 #include "transcoder.h"
27 #include "log.h"
28 #include "safe_stringstream.h"
29 #include "compose.hpp"
30 #include <iostream>
31 #include <iomanip>
32
33 #include "i18n.h"
34
35 #define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_GENERAL);
36 #define LOG_ERROR_NC(...)   _film->log()->log (__VA_ARGS__, LogEntry::TYPE_ERROR);
37
38 using std::string;
39 using std::fixed;
40 using std::setprecision;
41 using std::cout;
42 using boost::shared_ptr;
43
44 /** @param s Film to use.
45  */
46 TranscodeJob::TranscodeJob (shared_ptr<const Film> film)
47         : Job (film)
48 {
49
50 }
51
52 string
53 TranscodeJob::name () const
54 {
55         return String::compose (_("Transcode %1"), _film->name());
56 }
57
58 string
59 TranscodeJob::json_name () const
60 {
61         return N_("transcode");
62 }
63
64 void
65 TranscodeJob::run ()
66 {
67         try {
68
69                 LOG_GENERAL_NC (N_("Transcode job starting"));
70
71                 _transcoder.reset (new Transcoder (_film, shared_from_this ()));
72                 _transcoder->go ();
73                 set_progress (1);
74                 set_state (FINISHED_OK);
75
76                 LOG_GENERAL_NC (N_("Transcode job completed successfully"));
77                 _transcoder.reset ();
78
79         } catch (...) {
80                 _transcoder.reset ();
81                 throw;
82         }
83 }
84
85 string
86 TranscodeJob::status () const
87 {
88         if (!_transcoder) {
89                 return Job::status ();
90         }
91
92         float const fps = _transcoder->current_encoding_rate ();
93         if (fps == 0) {
94                 return Job::status ();
95         }
96
97         SafeStringStream s;
98
99         s << Job::status ();
100
101         if (!finished () && !_transcoder->finishing ()) {
102                 /// TRANSLATORS: fps here is an abbreviation for frames per second
103                 s << "; " << _transcoder->video_frames_out() << "/"
104                   << _film->length().frames_round (_film->video_frame_rate ()) << " " << _("frames") << "; "
105                   << fixed << setprecision (1) << fps << " " << _("fps");
106         }
107
108         return s.str ();
109 }
110
111 /** @return Approximate remaining time in seconds */
112 int
113 TranscodeJob::remaining_time () const
114 {
115         /* _transcoder might be destroyed by the job-runner thread */
116         shared_ptr<Transcoder> t = _transcoder;
117
118         if (!t) {
119                 return 0;
120         }
121
122         float fps = t->current_encoding_rate ();
123
124         if (fps == 0) {
125                 return 0;
126         }
127
128         /* Compute approximate proposed length here, as it's only here that we need it */
129         return (_film->length().frames_round (_film->video_frame_rate ()) - t->video_frames_out()) / fps;
130 }