2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <boost/lexical_cast.hpp>
22 #include <boost/filesystem.hpp>
23 #include "check_hashes_job.h"
26 #include "job_manager.h"
27 #include "ab_transcode_job.h"
28 #include "transcode_job.h"
30 #include "exceptions.h"
33 using std::stringstream;
35 using boost::shared_ptr;
37 CheckHashesJob::CheckHashesJob (shared_ptr<Film> f, DecodeOptions o, shared_ptr<Job> req)
46 CheckHashesJob::name () const
48 return String::compose ("Check hashes of %1", _film->name());
52 CheckHashesJob::run ()
56 if (!_film->dcp_intrinsic_duration()) {
57 throw EncodeError ("cannot check hashes of a DCP with unknown intrinsic duration");
60 int const N = _film->dcp_intrinsic_duration().get();
61 for (int i = 0; i < N; ++i) {
62 string const j2k_file = _film->frame_out_path (i, false);
63 string const hash_file = _film->hash_out_path (i, false);
65 if (!boost::filesystem::exists (j2k_file)) {
66 _film->log()->log (String::compose ("Frame %1 has a missing J2K file.", i));
67 boost::filesystem::remove (hash_file);
69 } else if (!boost::filesystem::exists (hash_file)) {
70 _film->log()->log (String::compose ("Frame %1 has a missing hash file.", i));
71 boost::filesystem::remove (j2k_file);
74 ifstream ref (hash_file.c_str ());
77 if (hash != md5_digest (j2k_file)) {
78 _film->log()->log (String::compose ("Frame %1 has wrong hash; deleting.", i));
79 boost::filesystem::remove (j2k_file);
80 boost::filesystem::remove (hash_file);
85 set_progress (float (i) / N);
91 if (_film->dcp_ab()) {
92 tc.reset (new ABTranscodeJob (_film, _decode_opt, shared_from_this()));
94 tc.reset (new TranscodeJob (_film, _decode_opt, shared_from_this()));
97 JobManager::instance()->add_after (shared_from_this(), tc);
98 JobManager::instance()->add_after (tc, shared_ptr<Job> (new CheckHashesJob (_film, _decode_opt, tc)));
102 set_state (FINISHED_OK);
106 CheckHashesJob::status () const
110 if (overall_progress() > 0) {
112 s << "; no bad frames found";
113 } else if (_bad == 1) {
114 s << "; 1 bad frame found";
116 s << "; " << _bad << " bad frames found";