cf4e86b79c1e2038c8ff738e8233a66c022f94a7
[dcpomatic.git] / src / lib / check_hashes_job.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <fstream>
21 #include <boost/lexical_cast.hpp>
22 #include <boost/filesystem.hpp>
23 #include "check_hashes_job.h"
24 #include "options.h"
25 #include "log.h"
26 #include "job_manager.h"
27 #include "ab_transcode_job.h"
28 #include "transcode_job.h"
29 #include "film.h"
30 #include "exceptions.h"
31
32 using std::string;
33 using std::stringstream;
34 using std::ifstream;
35 using boost::shared_ptr;
36
37 CheckHashesJob::CheckHashesJob (shared_ptr<Film> f, DecodeOptions o, shared_ptr<Job> req)
38         : Job (f, req)
39         , _decode_opt (o)
40         , _bad (0)
41 {
42
43 }
44
45 string
46 CheckHashesJob::name () const
47 {
48         return String::compose ("Check hashes of %1", _film->name());
49 }
50
51 void
52 CheckHashesJob::run ()
53 {
54         _bad = 0;
55
56         if (!_film->dcp_length()) {
57                 throw EncodeError ("cannot check hashes of a DCP with unknown length");
58         }
59         
60         SourceFrame const N = _film->trim_start() + _film->dcp_length().get();
61         DCPFrameRate const dfr (_film->frames_per_second ());
62
63         int const inc = dfr.skip ? 2 : 1;
64         
65         for (SourceFrame i = _film->trim_start(); i < N; i += inc) {
66                 string const j2k_file = _film->frame_out_path (i, false);
67                 string const hash_file = _film->hash_out_path (i, false);
68
69                 if (!boost::filesystem::exists (j2k_file)) {
70                         _film->log()->log (String::compose ("Frame %1 has a missing J2K file.", i));
71                         boost::filesystem::remove (hash_file);
72                         ++_bad;
73                 } else if (!boost::filesystem::exists (hash_file)) {
74                         _film->log()->log (String::compose ("Frame %1 has a missing hash file.", i));
75                         boost::filesystem::remove (j2k_file);
76                         ++_bad;
77                 } else {
78                         ifstream ref (hash_file.c_str ());
79                         string hash;
80                         ref >> hash;
81                         if (hash != md5_digest (j2k_file)) {
82                                 _film->log()->log (String::compose ("Frame %1 has wrong hash; deleting.", i));
83                                 boost::filesystem::remove (j2k_file);
84                                 boost::filesystem::remove (hash_file);
85                                 ++_bad;
86                         }
87                 }
88
89                 set_progress (float (i) / N);
90         }
91
92         if (_bad) {
93                 shared_ptr<Job> tc;
94
95                 if (_film->dcp_ab()) {
96                         tc.reset (new ABTranscodeJob (_film, _decode_opt, shared_from_this()));
97                 } else {
98                         tc.reset (new TranscodeJob (_film, _decode_opt, shared_from_this()));
99                 }
100                 
101                 JobManager::instance()->add_after (shared_from_this(), tc);
102                 JobManager::instance()->add_after (tc, shared_ptr<Job> (new CheckHashesJob (_film, _decode_opt, tc)));
103         }
104                 
105         set_progress (1);
106         set_state (FINISHED_OK);
107 }
108
109 string
110 CheckHashesJob::status () const
111 {
112         stringstream s;
113         s << Job::status ();
114         if (overall_progress() > 0) {
115                 if (_bad == 0) {
116                         s << "; no bad frames found";
117                 } else if (_bad == 1) {
118                         s << "; 1 bad frame found";
119                 } else {
120                         s << "; " << _bad << " bad frames found";
121                 }
122         }
123         return s.str ();
124 }