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