Missed update to private test repo version.
[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 string
152 TranscodeJob::status () const
153 {
154         if (!_encoder) {
155                 return Job::status ();
156         }
157
158         if (finished() || _encoder->finishing()) {
159                 return Job::status();
160         }
161
162         auto status = String::compose(_("%1; %2/%3 frames"), Job::status(), _encoder->frames_done(), _film->length().frames_round(_film->video_frame_rate()));
163         if (auto const fps = _encoder->current_rate()) {
164                 /// TRANSLATORS: fps here is an abbreviation for frames per second
165                 status += String::compose(_("; %1 fps"), dcp::locale_convert<string>(*fps, 1, true));
166         }
167
168         return status;
169 }
170
171
172 /** @return Approximate remaining time in seconds */
173 int
174 TranscodeJob::remaining_time () const
175 {
176         /* _encoder might be destroyed by the job-runner thread */
177         auto e = _encoder;
178
179         if (!e || e->finishing()) {
180                 /* We aren't doing any actual encoding so just use the job's guess */
181                 return Job::remaining_time ();
182         }
183
184         /* We're encoding so guess based on the current encoding rate */
185
186         auto fps = e->current_rate ();
187
188         if (!fps) {
189                 return 0;
190         }
191
192         /* Compute approximate proposed length here, as it's only here that we need it */
193         return (_film->length().frames_round(_film->video_frame_rate()) - e->frames_done()) / *fps;
194 }
195
196
197 float
198 TranscodeJob::frames_per_second() const
199 {
200         if (_finish_time != _start_time) {
201                 return _encoder->frames_done() / (_finish_time - _start_time);
202         } else {
203                 return 0;
204         }
205 }
206