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