Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / transcode_job.cc
1 /*
2     Copyright (C) 2012-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file src/transcode_job.cc
22  *  @brief A job which transcodes from one format to another.
23  */
24
25 #include "transcode_job.h"
26 #include "dcp_encoder.h"
27 #include "upload_job.h"
28 #include "job_manager.h"
29 #include "film.h"
30 #include "encoder.h"
31 #include "log.h"
32 #include "dcpomatic_log.h"
33 #include "compose.hpp"
34 #include "analytics.h"
35 #include <iostream>
36 #include <iomanip>
37
38 #include "i18n.h"
39
40 using std::string;
41 using std::fixed;
42 using std::setprecision;
43 using std::cout;
44 using boost::shared_ptr;
45 using boost::optional;
46 using boost::dynamic_pointer_cast;
47
48 /** @param film Film to use */
49 TranscodeJob::TranscodeJob (shared_ptr<const Film> film)
50         : Job (film)
51 {
52
53 }
54
55 TranscodeJob::~TranscodeJob ()
56 {
57         stop_thread ();
58 }
59
60 string
61 TranscodeJob::name () const
62 {
63         return String::compose (_("Transcoding %1"), _film->name());
64 }
65
66 string
67 TranscodeJob::json_name () const
68 {
69         return N_("transcode");
70 }
71
72 void
73 TranscodeJob::set_encoder (shared_ptr<Encoder> e)
74 {
75         _encoder = e;
76 }
77
78 void
79 TranscodeJob::run ()
80 {
81         try {
82                 struct timeval start;
83                 gettimeofday (&start, 0);
84                 LOG_GENERAL_NC (N_("Transcode job starting"));
85
86                 DCPOMATIC_ASSERT (_encoder);
87                 _encoder->go ();
88
89                 struct timeval finish;
90                 gettimeofday (&finish, 0);
91
92                 float fps = 0;
93                 if (finish.tv_sec != start.tv_sec) {
94                         fps = _encoder->frames_done() / (finish.tv_sec - start.tv_sec);
95                 }
96
97                 LOG_GENERAL (N_("Transcode job completed successfully: %1 fps"), fps);
98
99                 if (dynamic_pointer_cast<DCPEncoder>(_encoder)) {
100                         Analytics::instance()->successful_dcp_encode();
101                 }
102
103                 /* XXX: this shouldn't be here */
104                 if (_film->upload_after_make_dcp() && dynamic_pointer_cast<DCPEncoder>(_encoder)) {
105                         shared_ptr<Job> job (new UploadJob (_film));
106                         JobManager::instance()->add (job);
107                 }
108
109                 _encoder.reset ();
110
111                 set_progress (1);
112                 set_state (FINISHED_OK);
113
114         } catch (...) {
115                 _encoder.reset ();
116                 throw;
117         }
118 }
119
120 string
121 TranscodeJob::status () const
122 {
123         if (!_encoder) {
124                 return Job::status ();
125         }
126
127
128         char buffer[256];
129         if (finished() || _encoder->finishing()) {
130                 strncpy (buffer, Job::status().c_str(), 255);
131                 buffer[255] = '\0';
132         } else {
133                 snprintf (
134                         buffer, sizeof(buffer), "%s; %" PRId64 "/%" PRId64 " frames",
135                         Job::status().c_str(),
136                         _encoder->frames_done(),
137                         _film->length().frames_round (_film->video_frame_rate ())
138                         );
139
140                 optional<float> const fps = _encoder->current_rate ();
141                 if (fps) {
142                         char fps_buffer[64];
143                         /// TRANSLATORS: fps here is an abbreviation for frames per second
144                         snprintf (fps_buffer, sizeof(fps_buffer), _("; %.1f fps"), *fps);
145                         strncat (buffer, fps_buffer, strlen(buffer) - 1);
146                 }
147         }
148
149         return buffer;
150 }
151
152 /** @return Approximate remaining time in seconds */
153 int
154 TranscodeJob::remaining_time () const
155 {
156         /* _encoder might be destroyed by the job-runner thread */
157         shared_ptr<Encoder> e = _encoder;
158
159         if (!e || e->finishing()) {
160                 /* We aren't doing any actual encoding so just use the job's guess */
161                 return Job::remaining_time ();
162         }
163
164         /* We're encoding so guess based on the current encoding rate */
165
166         optional<float> fps = e->current_rate ();
167
168         if (!fps) {
169                 return 0;
170         }
171
172         /* Compute approximate proposed length here, as it's only here that we need it */
173         return (_film->length().frames_round(_film->video_frame_rate()) - e->frames_done()) / *fps;
174 }