Try to not start jobs if a dependant fails.
[dcpomatic.git] / test / test.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 <iostream>
22 #include <boost/filesystem.hpp>
23 #include <boost/algorithm/string/predicate.hpp>
24 #include "format.h"
25 #include "film.h"
26 #include "filter.h"
27 #include "job_manager.h"
28 #include "util.h"
29 #include "exceptions.h"
30 #include "dvd.h"
31 #include "delay_line.h"
32 #include "image.h"
33 #include "log.h"
34 #include "dcp_video_frame.h"
35 #include "config.h"
36 #include "server.h"
37 #include "cross.h"
38 #include "job.h"
39 #define BOOST_TEST_DYN_LINK
40 #define BOOST_TEST_MODULE dvdomatic_test
41 #include <boost/test/unit_test.hpp>
42
43 using namespace std;
44 using namespace boost;
45
46 BOOST_AUTO_TEST_CASE (film_metadata_test)
47 {
48         dvdomatic_setup ();
49         
50         string const test_film = "build/test/film";
51         
52         if (boost::filesystem::exists (test_film)) {
53                 boost::filesystem::remove_all (test_film);
54         }
55
56         BOOST_CHECK_THROW (new Film ("build/test/film", true), OpenFileError);
57         
58         Film f (test_film, false);
59         BOOST_CHECK (f.format() == 0);
60         BOOST_CHECK (f.dcp_content_type() == 0);
61         BOOST_CHECK (f.filters ().empty());
62
63         f.set_name ("fred");
64         BOOST_CHECK_THROW (f.set_content ("jim"), OpenFileError);
65         f.set_dcp_content_type (DCPContentType::from_pretty_name ("Short"));
66         f.set_format (Format::from_nickname ("Flat"));
67         f.set_left_crop (1);
68         f.set_right_crop (2);
69         f.set_top_crop (3);
70         f.set_bottom_crop (4);
71         vector<Filter const *> f_filters;
72         f_filters.push_back (Filter::from_id ("pphb"));
73         f_filters.push_back (Filter::from_id ("unsharp"));
74         f.set_filters (f_filters);
75         f.set_dcp_frames (42);
76         f.set_dcp_ab (true);
77         f.write_metadata ();
78
79         stringstream s;
80         s << "diff -u test/metadata.ref " << test_film << "/metadata";
81         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
82
83         Film g (test_film, true);
84
85         BOOST_CHECK_EQUAL (g.name(), "fred");
86         BOOST_CHECK_EQUAL (g.dcp_content_type(), DCPContentType::from_pretty_name ("Short"));
87         BOOST_CHECK_EQUAL (g.format(), Format::from_nickname ("Flat"));
88         BOOST_CHECK_EQUAL (g.crop().left, 1);
89         BOOST_CHECK_EQUAL (g.crop().right, 2);
90         BOOST_CHECK_EQUAL (g.crop().top, 3);
91         BOOST_CHECK_EQUAL (g.crop().bottom, 4);
92         vector<Filter const *> g_filters = g.filters ();
93         BOOST_CHECK_EQUAL (g_filters.size(), 2);
94         BOOST_CHECK_EQUAL (g_filters.front(), Filter::from_id ("pphb"));
95         BOOST_CHECK_EQUAL (g_filters.back(), Filter::from_id ("unsharp"));
96         BOOST_CHECK_EQUAL (g.dcp_frames(), 42);
97         BOOST_CHECK_EQUAL (g.dcp_ab(), true);
98         
99         g.write_metadata ();
100         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
101 }
102
103 BOOST_AUTO_TEST_CASE (format_test)
104 {
105         Format::setup_formats ();
106         
107         Format const * f = Format::from_nickname ("Flat");
108         BOOST_CHECK (f);
109         BOOST_CHECK_EQUAL (f->ratio_as_integer(0), 185);
110         
111         f = Format::from_nickname ("Scope");
112         BOOST_CHECK (f);
113         BOOST_CHECK_EQUAL (f->ratio_as_integer(0), 239);
114 }
115
116 BOOST_AUTO_TEST_CASE (util_test)
117 {
118         string t = "Hello this is a string \"with quotes\" and indeed without them";
119         vector<string> b = split_at_spaces_considering_quotes (t);
120         vector<string>::iterator i = b.begin ();
121         BOOST_CHECK_EQUAL (*i++, "Hello");
122         BOOST_CHECK_EQUAL (*i++, "this");
123         BOOST_CHECK_EQUAL (*i++, "is");
124         BOOST_CHECK_EQUAL (*i++, "a");
125         BOOST_CHECK_EQUAL (*i++, "string");
126         BOOST_CHECK_EQUAL (*i++, "with quotes");
127         BOOST_CHECK_EQUAL (*i++, "and");
128         BOOST_CHECK_EQUAL (*i++, "indeed");
129         BOOST_CHECK_EQUAL (*i++, "without");
130         BOOST_CHECK_EQUAL (*i++, "them");
131 }
132
133 BOOST_AUTO_TEST_CASE (dvd_test)
134 {
135         list<DVDTitle> const t = dvd_titles ("test/dvd");
136         BOOST_CHECK_EQUAL (t.size(), 3);
137         list<DVDTitle>::const_iterator i = t.begin ();
138         
139         BOOST_CHECK_EQUAL (i->number, 1);
140         BOOST_CHECK_EQUAL (i->size, 0);
141         ++i;
142         
143         BOOST_CHECK_EQUAL (i->number, 2);
144         BOOST_CHECK_EQUAL (i->size, 14);
145         ++i;
146         
147         BOOST_CHECK_EQUAL (i->number, 3);
148         BOOST_CHECK_EQUAL (i->size, 7);
149 }
150
151 void
152 do_positive_delay_line_test (int delay_length, int block_length)
153 {
154         DelayLine d (delay_length);
155         uint8_t data[block_length];
156
157         int in = 0;
158         int out = 0;
159         int returned = 0;
160         int zeros = 0;
161         
162         for (int i = 0; i < 64; ++i) {
163                 for (int j = 0; j < block_length; ++j) {
164                         data[j] = in;
165                         ++in;
166                 }
167
168                 int const a = d.feed (data, block_length);
169                 returned += a;
170
171                 for (int j = 0; j < a; ++j) {
172                         if (zeros < delay_length) {
173                                 BOOST_CHECK_EQUAL (data[j], 0);
174                                 ++zeros;
175                         } else {
176                                 BOOST_CHECK_EQUAL (data[j], out & 0xff);
177                                 ++out;
178                         }
179                 }
180         }
181
182         BOOST_CHECK_EQUAL (returned, 64 * block_length);
183 }
184
185 void
186 do_negative_delay_line_test (int delay_length, int block_length)
187 {
188         DelayLine d (delay_length);
189         uint8_t data[block_length];
190
191         int in = 0;
192         int out = -delay_length;
193         int returned = 0;
194         
195         for (int i = 0; i < 256; ++i) {
196                 for (int j = 0; j < block_length; ++j) {
197                         data[j] = in;
198                         ++in;
199                 }
200
201                 int const a = d.feed (data, block_length);
202                 returned += a;
203
204                 for (int j = 0; j < a; ++j) {
205                         BOOST_CHECK_EQUAL (data[j], out & 0xff);
206                         ++out;
207                 }
208         }
209
210         uint8_t remainder[-delay_length];
211         d.get_remaining (remainder);
212         returned += -delay_length;
213
214         for (int i = 0; i < -delay_length; ++i) {
215                 BOOST_CHECK_EQUAL (remainder[i], 0);
216                 ++out;
217         }
218
219         BOOST_CHECK_EQUAL (returned, 256 * block_length);
220         
221 }
222
223 BOOST_AUTO_TEST_CASE (delay_line_test)
224 {
225         do_positive_delay_line_test (64, 128);
226         do_positive_delay_line_test (128, 64);
227         do_positive_delay_line_test (3, 512);
228         do_positive_delay_line_test (512, 3);
229
230         do_positive_delay_line_test (0, 64);
231
232         do_negative_delay_line_test (-64, 128);
233         do_negative_delay_line_test (-128, 64);
234         do_negative_delay_line_test (-3, 512);
235         do_negative_delay_line_test (-512, 3);
236 }
237
238 BOOST_AUTO_TEST_CASE (md5_digest_test)
239 {
240         string const t = md5_digest ("test/md5.test");
241         BOOST_CHECK_EQUAL (t, "15058685ba99decdc4398c7634796eb0");
242
243         BOOST_CHECK_THROW (md5_digest ("foobar"), OpenFileError);
244 }
245
246 BOOST_AUTO_TEST_CASE (paths_test)
247 {
248         FilmState s;
249         s.directory = "build/test/a/b/c/d/e";
250         s.thumbs.push_back (42);
251         BOOST_CHECK_EQUAL (s.thumb_file (0), "build/test/a/b/c/d/e/thumbs/00000042.tiff");
252
253         s.content = "/foo/bar/baz";
254         BOOST_CHECK_EQUAL (s.content_path(), "/foo/bar/baz");
255         s.content = "foo/bar/baz";
256         BOOST_CHECK_EQUAL (s.content_path(), "build/test/a/b/c/d/e/foo/bar/baz");
257 }
258
259 void
260 do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription* description, shared_ptr<EncodedData> locally_encoded)
261 {
262         shared_ptr<EncodedData> remotely_encoded;
263         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
264         BOOST_CHECK (remotely_encoded);
265         
266         BOOST_CHECK_EQUAL (locally_encoded->size(), remotely_encoded->size());
267         BOOST_CHECK (memcmp (locally_encoded->data(), remotely_encoded->data(), locally_encoded->size()) == 0);
268 }
269
270 BOOST_AUTO_TEST_CASE (client_server_test)
271 {
272         shared_ptr<SimpleImage> image (new SimpleImage (PIX_FMT_RGB24, Size (1998, 1080)));
273         image->set_line_size (0, 1998 * 3);
274
275         uint8_t* p = image->data()[0];
276         
277         for (int y = 0; y < 1080; ++y) {
278                 for (int x = 0; x < 1998; ++x) {
279                         *p++ = x % 256;
280                         *p++ = y % 256;
281                         *p++ = (x + y) % 256;
282                 }
283         }
284
285         FileLog log ("build/test/client_server_test.log");
286
287         shared_ptr<DCPVideoFrame> frame (
288                 new DCPVideoFrame (
289                         image,
290                         Size (1998, 1080),
291                         0,
292                         Scaler::from_id ("bicubic"),
293                         0,
294                         24,
295                         "",
296                         0,
297                         200000000,
298                         &log
299                         )
300                 );
301
302         shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
303         
304         Config::instance()->set_server_port (61920);
305         Server* server = new Server (&log);
306
307         new thread (boost::bind (&Server::run, server, 2));
308
309         /* Let the server get itself ready */
310         dvdomatic_sleep (1);
311
312         ServerDescription description ("localhost", 2);
313
314         list<thread*> threads;
315         for (int i = 0; i < 8; ++i) {
316                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, &description, locally_encoded)));
317         }
318
319         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
320                 (*i)->join ();
321         }
322 }
323
324 BOOST_AUTO_TEST_CASE (make_dcp_test)
325 {
326         string const test_film = "build/test/film2";
327         
328         if (boost::filesystem::exists (test_film)) {
329                 boost::filesystem::remove_all (test_film);
330         }
331         
332         Film film (test_film, false);
333         film.set_name ("test_film");
334         film.set_content ("../../../test/test.mp4");
335         film.examine_content ();
336         film.set_format (Format::from_nickname ("Flat"));
337         film.set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
338         film.make_dcp (true);
339
340         while (JobManager::instance()->work_to_do ()) {
341                 dvdomatic_sleep (1);
342         }
343         
344         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
345 }
346
347 BOOST_AUTO_TEST_CASE (make_dcp_with_range_test)
348 {
349         string const test_film = "build/test/film3";
350         
351         if (boost::filesystem::exists (test_film)) {
352                 boost::filesystem::remove_all (test_film);
353         }
354         
355         Film film (test_film, false);
356         film.set_name ("test_film");
357         film.set_content ("../../../test/test.mp4");
358         film.examine_content ();
359         film.set_format (Format::from_nickname ("Flat"));
360         film.set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
361         film.set_dcp_frames (42);
362         film.make_dcp (true);
363
364         while (JobManager::instance()->work_to_do() && !JobManager::instance()->errors()) {
365                 dvdomatic_sleep (1);
366         }
367
368         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
369 }
370
371 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
372 {
373         FilmState fs;
374         fs.frames_per_second = 24;
375
376         fs.audio_sample_rate = 48000;
377         BOOST_CHECK_EQUAL (fs.target_sample_rate(), 48000);
378
379         fs.audio_sample_rate = 44100;
380         BOOST_CHECK_EQUAL (fs.target_sample_rate(), 48000);
381
382         fs.audio_sample_rate = 80000;
383         BOOST_CHECK_EQUAL (fs.target_sample_rate(), 96000);
384
385         fs.frames_per_second = 23.976;
386         fs.audio_sample_rate = 48000;
387         BOOST_CHECK_EQUAL (fs.target_sample_rate(), 47952);
388
389         fs.frames_per_second = 29.97;
390         fs.audio_sample_rate = 48000;
391         BOOST_CHECK_EQUAL (fs.target_sample_rate(), 47952);
392 }
393
394 class TestJob : public Job
395 {
396 public:
397         TestJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l, shared_ptr<Job> req)
398                 : Job (s, o, l, req)
399         {
400
401         }
402
403         void set_finished_ok () {
404                 set_state (FINISHED_OK);
405         }
406
407         void set_finished_error () {
408                 set_state (FINISHED_ERROR);
409         }
410
411         void run ()
412         {
413                 while (1) {
414                         if (finished ()) {
415                                 return;
416                         }
417                 }
418         }
419
420         string name () const {
421                 return "";
422         }
423 };
424
425 BOOST_AUTO_TEST_CASE (job_manager_test)
426 {
427         shared_ptr<const FilmState> s;
428         shared_ptr<const Options> o;
429         FileLog log ("build/test/job_manager_test.log");
430
431         /* Single job, no dependency */
432         shared_ptr<TestJob> a (new TestJob (s, o, &log, shared_ptr<Job> ()));
433
434         JobManager::instance()->add (a);
435         dvdomatic_sleep (1);
436         BOOST_CHECK_EQUAL (a->running (), true);
437         a->set_finished_ok ();
438         dvdomatic_sleep (2);
439         BOOST_CHECK_EQUAL (a->finished_ok(), true);
440
441         /* Two jobs, no dependency */
442         a.reset (new TestJob (s, o, &log, shared_ptr<Job> ()));
443         shared_ptr<TestJob> b (new TestJob (s, o, &log, shared_ptr<Job> ()));
444
445         JobManager::instance()->add (a);
446         JobManager::instance()->add (b);
447         dvdomatic_sleep (2);
448         BOOST_CHECK_EQUAL (a->running (), true);
449         BOOST_CHECK_EQUAL (b->running (), true);
450         a->set_finished_ok ();
451         b->set_finished_ok ();
452         dvdomatic_sleep (2);
453         BOOST_CHECK_EQUAL (a->finished_ok (), true);
454         BOOST_CHECK_EQUAL (b->finished_ok (), true);
455
456         /* Two jobs, dependency */
457         a.reset (new TestJob (s, o, &log, shared_ptr<Job> ()));
458         b.reset (new TestJob (s, o, &log, a));
459
460         JobManager::instance()->add (a);
461         JobManager::instance()->add (b);
462         dvdomatic_sleep (2);
463         BOOST_CHECK_EQUAL (a->running(), true);
464         BOOST_CHECK_EQUAL (b->running(), false);
465         a->set_finished_ok ();
466         dvdomatic_sleep (2);
467         BOOST_CHECK_EQUAL (a->finished_ok(), true);
468         BOOST_CHECK_EQUAL (b->running(), true);
469         b->set_finished_ok ();
470         dvdomatic_sleep (2);
471         BOOST_CHECK_EQUAL (b->finished_ok(), true);
472
473         /* Two jobs, dependency, first fails */
474         a.reset (new TestJob (s, o, &log, shared_ptr<Job> ()));
475         b.reset (new TestJob (s, o, &log, a));
476
477         JobManager::instance()->add (a);
478         JobManager::instance()->add (b);
479         dvdomatic_sleep (2);
480         BOOST_CHECK_EQUAL (a->running(), true);
481         BOOST_CHECK_EQUAL (b->running(), false);
482         a->set_finished_error ();
483         dvdomatic_sleep (2);
484         BOOST_CHECK_EQUAL (a->finished_in_error(), true);
485         BOOST_CHECK_EQUAL (b->running(), false);
486 }