Merge remote-tracking branch 'origin/main' into v2.17.x
[dcpomatic.git] / src / lib / transcode_job.cc
1 /*
2     Copyright (C) 2012-2021 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
22 /** @file src/transcode_job.cc
23  *  @brief A job which transcodes from one format to another.
24  */
25
26
27 #include "analytics.h"
28 #include "compose.hpp"
29 #include "content.h"
30 #include "config.h"
31 #include "dcp_encoder.h"
32 #include "dcpomatic_log.h"
33 #include "encoder.h"
34 #include "examine_content_job.h"
35 #include "film.h"
36 #include "job_manager.h"
37 #include "log.h"
38 #include "transcode_job.h"
39 #include "upload_job.h"
40 #include <iomanip>
41 #include <iostream>
42
43 #include "i18n.h"
44
45
46 using std::cout;
47 using std::fixed;
48 using std::make_shared;
49 using std::setprecision;
50 using std::shared_ptr;
51 using std::string;
52 using boost::optional;
53 using std::dynamic_pointer_cast;
54
55
56 /** @param film Film to use */
57 TranscodeJob::TranscodeJob (shared_ptr<const Film> film, ChangedBehaviour changed)
58         : Job (film)
59         , _changed (changed)
60 {
61
62 }
63
64
65 TranscodeJob::~TranscodeJob ()
66 {
67         stop_thread ();
68 }
69
70
71 string
72 TranscodeJob::name () const
73 {
74         return String::compose (_("Transcoding %1"), _film->name());
75 }
76
77
78 string
79 TranscodeJob::json_name () const
80 {
81         return N_("transcode");
82 }
83
84
85 void
86 TranscodeJob::set_encoder (shared_ptr<Encoder> e)
87 {
88         _encoder = e;
89 }
90
91
92 void
93 TranscodeJob::run ()
94 {
95         try {
96                 auto content = _film->content();
97                 std::vector<shared_ptr<Content>> changed;
98                 std::copy_if (content.begin(), content.end(), std::back_inserter(changed), [](shared_ptr<Content> c) { return c->changed(); });
99
100                 if (!changed.empty()) {
101                         switch (_changed) {
102                         case ChangedBehaviour::EXAMINE_THEN_STOP:
103                                 for (auto i: changed) {
104                                         JobManager::instance()->add(make_shared<ExamineContentJob>(_film, i));
105                                 }
106                                 set_progress (1);
107                                 set_message (_("Some files have been changed since they were added to the project.\n\nThese files will now be re-examined, so you may need to check their settings before trying again."));
108                                 set_error (_("Files have changed since they were added to the project."), _("Check their new settings, then try again."));
109                                 set_state (FINISHED_ERROR);
110                                 return;
111                         case ChangedBehaviour::STOP:
112                                 set_progress (1);
113                                 set_error (_("Files have changed since they were added to the project."), _("Open the project in DCP-o-matic, check the settings, then save it before trying again."));
114                                 set_state (FINISHED_ERROR);
115                                 return;
116                         default:
117                                 LOG_GENERAL_NC (_("Some files have been changed since they were added to the project."));
118                                 break;
119                         }
120                 }
121
122                 LOG_GENERAL_NC (N_("Transcode job starting"));
123
124                 DCPOMATIC_ASSERT (_encoder);
125                 _encoder->go ();
126
127                 set_progress (1);
128                 set_state (FINISHED_OK);
129
130                 LOG_GENERAL(N_("Transcode job completed successfully: %1 fps"), dcp::locale_convert<string>(frames_per_second(), 2, true));
131
132                 if (dynamic_pointer_cast<DCPEncoder>(_encoder)) {
133                         try {
134                                 Analytics::instance()->successful_dcp_encode();
135                         } catch (FileError& e) {
136                                 LOG_WARNING (N_("Failed to write analytics (%1)"), e.what());
137                         }
138                 }
139
140                 post_transcode ();
141
142                 _encoder.reset ();
143
144         } catch (...) {
145                 _encoder.reset ();
146                 throw;
147         }
148 }
149
150
151 void
152 TranscodeJob::pause()
153 {
154         _encoder->pause();
155 }
156
157
158 void TranscodeJob::resume()
159 {
160         _encoder->resume();
161         Job::resume();
162 }
163
164
165 string
166 TranscodeJob::status () const
167 {
168         if (!_encoder) {
169                 return Job::status ();
170         }
171
172         if (finished() || _encoder->finishing()) {
173                 return Job::status();
174         }
175
176         auto status = String::compose(_("%1; %2/%3 frames"), Job::status(), _encoder->frames_done(), _film->length().frames_round(_film->video_frame_rate()));
177         if (auto const fps = _encoder->current_rate()) {
178                 /// TRANSLATORS: fps here is an abbreviation for frames per second
179                 status += String::compose(_("; %1 fps"), dcp::locale_convert<string>(*fps, 1, true));
180         }
181
182         return status;
183 }
184
185
186 /** @return Approximate remaining time in seconds */
187 int
188 TranscodeJob::remaining_time () const
189 {
190         /* _encoder might be destroyed by the job-runner thread */
191         auto e = _encoder;
192
193         if (!e || e->finishing()) {
194                 /* We aren't doing any actual encoding so just use the job's guess */
195                 return Job::remaining_time ();
196         }
197
198         /* We're encoding so guess based on the current encoding rate */
199
200         auto fps = e->current_rate ();
201
202         if (!fps) {
203                 return 0;
204         }
205
206         /* Compute approximate proposed length here, as it's only here that we need it */
207         return (_film->length().frames_round(_film->video_frame_rate()) - e->frames_done()) / *fps;
208 }
209
210
211 float
212 TranscodeJob::frames_per_second() const
213 {
214         if (_finish_time != _start_time) {
215                 return _encoder->frames_done() / (_finish_time - _start_time);
216         } else {
217                 return 0;
218         }
219 }
220