Basic stuff to count successful DCP encodes.
[dcpomatic.git] / src / lib / transcode_job.cc
1 /*
2     Copyright (C) 2012-2018 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 "analytics.h"
27 #include "dcp_encoder.h"
28 #include "upload_job.h"
29 #include "job_manager.h"
30 #include "film.h"
31 #include "encoder.h"
32 #include "log.h"
33 #include "compose.hpp"
34 #include <iostream>
35 #include <iomanip>
36
37 #include "i18n.h"
38
39 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
40 #define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_GENERAL);
41 #define LOG_ERROR_NC(...)   _film->log()->log (__VA_ARGS__, LogEntry::TYPE_ERROR);
42
43 using std::string;
44 using std::fixed;
45 using std::setprecision;
46 using std::cout;
47 using boost::shared_ptr;
48 using boost::dynamic_pointer_cast;
49
50 /** @param film Film to use */
51 TranscodeJob::TranscodeJob (shared_ptr<const Film> film)
52         : Job (film)
53 {
54
55 }
56
57 string
58 TranscodeJob::name () const
59 {
60         return String::compose (_("Transcode %1"), _film->name());
61 }
62
63 string
64 TranscodeJob::json_name () const
65 {
66         return N_("transcode");
67 }
68
69 void
70 TranscodeJob::set_encoder (shared_ptr<Encoder> e)
71 {
72         _encoder = e;
73 }
74
75 void
76 TranscodeJob::run ()
77 {
78         try {
79                 struct timeval start;
80                 gettimeofday (&start, 0);
81                 LOG_GENERAL_NC (N_("Transcode job starting"));
82
83                 DCPOMATIC_ASSERT (_encoder);
84                 _encoder->go ();
85                 set_progress (1);
86                 set_state (FINISHED_OK);
87
88                 struct timeval finish;
89                 gettimeofday (&finish, 0);
90
91                 float fps = 0;
92                 if (finish.tv_sec != start.tv_sec) {
93                         fps = _encoder->frames_done() / (finish.tv_sec - start.tv_sec);
94                 }
95
96                 LOG_GENERAL (N_("Transcode job completed successfully: %1 fps"), fps);
97
98                 if (dynamic_pointer_cast<DCPEncoder>(_encoder)) {
99                         Analytics::instance()->successful_dcp_encode();
100                 }
101
102                 _encoder.reset ();
103
104                 /* XXX: this shouldn't be here */
105                 if (_film->upload_after_make_dcp ()) {
106                         shared_ptr<Job> job (new UploadJob (_film));
107                         JobManager::instance()->add (job);
108                 }
109
110         } catch (...) {
111                 _encoder.reset ();
112                 throw;
113         }
114 }
115
116 string
117 TranscodeJob::status () const
118 {
119         if (!_encoder) {
120                 return Job::status ();
121         }
122
123
124         char buffer[256];
125         if (finished() || _encoder->finishing()) {
126                 strncpy (buffer, Job::status().c_str(), 256);
127         } else {
128                 snprintf (
129                         buffer, sizeof(buffer), "%s; %" PRId64 "/%" PRId64 " frames",
130                         Job::status().c_str(),
131                         _encoder->frames_done(),
132                         _film->length().frames_round (_film->video_frame_rate ())
133                         );
134
135                 float const fps = _encoder->current_rate ();
136                 if (fps) {
137                         char fps_buffer[64];
138                         /// TRANSLATORS: fps here is an abbreviation for frames per second
139                         snprintf (fps_buffer, sizeof(fps_buffer), _("; %.1f fps"), fps);
140                         strncat (buffer, fps_buffer, strlen(buffer) - 1);
141                 }
142         }
143
144         return buffer;
145 }
146
147 /** @return Approximate remaining time in seconds */
148 int
149 TranscodeJob::remaining_time () const
150 {
151         /* _encoder might be destroyed by the job-runner thread */
152         shared_ptr<Encoder> e = _encoder;
153
154         if (!e || e->finishing()) {
155                 /* We aren't doing any actual encoding so just use the job's guess */
156                 return Job::remaining_time ();
157         }
158
159         /* We're encoding so guess based on the current encoding rate */
160
161         float fps = e->current_rate ();
162
163         if (fps == 0) {
164                 return 0;
165         }
166
167         /* Compute approximate proposed length here, as it's only here that we need it */
168         return (_film->length().frames_round (_film->video_frame_rate ()) - e->frames_done()) / fps;
169 }