730aa8d3e2a21dca5e0cf691a5f3f352f2787a92
[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.directory = "build/test/a/b/c/d/e";
262         s.thumbs.push_back (42);
263         BOOST_CHECK_EQUAL (s.thumb_file (0), "build/test/a/b/c/d/e/thumbs/00000042.png");
264
265         s.content = "/foo/bar/baz";
266         BOOST_CHECK_EQUAL (s.content_path(), "/foo/bar/baz");
267         s.content = "foo/bar/baz";
268         BOOST_CHECK_EQUAL (s.content_path(), "build/test/a/b/c/d/e/foo/bar/baz");
269 }
270
271 void
272 do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription* description, shared_ptr<EncodedData> locally_encoded, int N)
273 {
274         shared_ptr<EncodedData> remotely_encoded;
275         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
276         BOOST_CHECK (remotely_encoded);
277         
278         BOOST_CHECK_EQUAL (locally_encoded->size(), remotely_encoded->size());
279         BOOST_CHECK (memcmp (locally_encoded->data(), remotely_encoded->data(), locally_encoded->size()) == 0);
280 }
281
282 BOOST_AUTO_TEST_CASE (client_server_test)
283 {
284         shared_ptr<Image> image (new CompactImage (PIX_FMT_RGB24, Size (1998, 1080)));
285         uint8_t* p = image->data()[0];
286         
287         for (int y = 0; y < 1080; ++y) {
288                 for (int x = 0; x < 1998; ++x) {
289                         *p++ = x % 256;
290                         *p++ = y % 256;
291                         *p++ = (x + y) % 256;
292                 }
293         }
294
295         shared_ptr<Image> sub_image (new CompactImage (PIX_FMT_RGBA, Size (100, 200)));
296         p = sub_image->data()[0];
297         for (int y = 0; y < 200; ++y) {
298                 for (int x = 0; x < 100; ++x) {
299                         *p++ = y % 256;
300                         *p++ = x % 256;
301                         *p++ = (x + y) % 256;
302                         *p++ = 1;
303                 }
304         }
305
306         shared_ptr<Subtitle> subtitle (new Subtitle (Position (50, 60), sub_image));
307
308         FileLog log ("build/test/client_server_test.log");
309
310         shared_ptr<DCPVideoFrame> frame (
311                 new DCPVideoFrame (
312                         image,
313                         subtitle,
314                         Size (1998, 1080),
315                         0,
316                         0,
317                         1,
318                         Scaler::from_id ("bicubic"),
319                         0,
320                         24,
321                         "",
322                         0,
323                         200000000,
324                         &log
325                         )
326                 );
327
328         shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
329         BOOST_ASSERT (locally_encoded);
330         
331         Server* server = new Server (&log);
332
333         new thread (boost::bind (&Server::run, server, 2));
334
335         /* Let the server get itself ready */
336         dvdomatic_sleep (1);
337
338         ServerDescription description ("localhost", 2);
339
340         list<thread*> threads;
341         for (int i = 0; i < 8; ++i) {
342                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, &description, locally_encoded, i)));
343         }
344
345         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
346                 (*i)->join ();
347         }
348
349         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
350                 delete *i;
351         }
352 }
353
354 BOOST_AUTO_TEST_CASE (make_dcp_test)
355 {
356         string const test_film = "build/test/film2";
357         
358         if (boost::filesystem::exists (test_film)) {
359                 boost::filesystem::remove_all (test_film);
360         }
361         
362         Film film (test_film, false);
363         film.set_name ("test_film");
364         film.set_content ("../../../test/test.mp4");
365         film.examine_content ();
366         film.set_format (Format::from_nickname ("Flat"));
367         film.set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
368         film.make_dcp (true);
369
370         while (JobManager::instance()->work_to_do ()) {
371                 dvdomatic_sleep (1);
372         }
373         
374         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
375 }
376
377 BOOST_AUTO_TEST_CASE (make_dcp_with_range_test)
378 {
379         string const test_film = "build/test/film3";
380         
381         if (boost::filesystem::exists (test_film)) {
382                 boost::filesystem::remove_all (test_film);
383         }
384         
385         Film film (test_film, false);
386         film.set_name ("test_film");
387         film.set_content ("../../../test/test.mp4");
388         film.examine_content ();
389         film.set_format (Format::from_nickname ("Flat"));
390         film.set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
391         film.set_dcp_frames (42);
392         film.make_dcp (true);
393
394         while (JobManager::instance()->work_to_do() && !JobManager::instance()->errors()) {
395                 dvdomatic_sleep (1);
396         }
397
398         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
399 }
400
401 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
402 {
403         FilmState fs;
404         fs.frames_per_second = 24;
405
406         fs.audio_sample_rate = 48000;
407         BOOST_CHECK_EQUAL (fs.target_sample_rate(), 48000);
408
409         fs.audio_sample_rate = 44100;
410         BOOST_CHECK_EQUAL (fs.target_sample_rate(), 48000);
411
412         fs.audio_sample_rate = 80000;
413         BOOST_CHECK_EQUAL (fs.target_sample_rate(), 96000);
414
415         fs.frames_per_second = 23.976;
416         fs.audio_sample_rate = 48000;
417         BOOST_CHECK_EQUAL (fs.target_sample_rate(), 47952);
418
419         fs.frames_per_second = 29.97;
420         fs.audio_sample_rate = 48000;
421         BOOST_CHECK_EQUAL (fs.target_sample_rate(), 47952);
422 }
423
424 class TestJob : public Job
425 {
426 public:
427         TestJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l, shared_ptr<Job> req)
428                 : Job (s, o, l, req)
429         {
430
431         }
432
433         void set_finished_ok () {
434                 set_state (FINISHED_OK);
435         }
436
437         void set_finished_error () {
438                 set_state (FINISHED_ERROR);
439         }
440
441         void run ()
442         {
443                 while (1) {
444                         if (finished ()) {
445                                 return;
446                         }
447                 }
448         }
449
450         string name () const {
451                 return "";
452         }
453 };
454
455 BOOST_AUTO_TEST_CASE (job_manager_test)
456 {
457         shared_ptr<const FilmState> s;
458         shared_ptr<const Options> o;
459         FileLog log ("build/test/job_manager_test.log");
460
461         /* Single job, no dependency */
462         shared_ptr<TestJob> a (new TestJob (s, o, &log, shared_ptr<Job> ()));
463
464         JobManager::instance()->add (a);
465         dvdomatic_sleep (1);
466         BOOST_CHECK_EQUAL (a->running (), true);
467         a->set_finished_ok ();
468         dvdomatic_sleep (2);
469         BOOST_CHECK_EQUAL (a->finished_ok(), true);
470
471         /* Two jobs, dependency */
472         a.reset (new TestJob (s, o, &log, shared_ptr<Job> ()));
473         shared_ptr<TestJob> b (new TestJob (s, o, &log, a));
474
475         JobManager::instance()->add (a);
476         JobManager::instance()->add (b);
477         dvdomatic_sleep (2);
478         BOOST_CHECK_EQUAL (a->running(), true);
479         BOOST_CHECK_EQUAL (b->running(), false);
480         a->set_finished_ok ();
481         dvdomatic_sleep (2);
482         BOOST_CHECK_EQUAL (a->finished_ok(), true);
483         BOOST_CHECK_EQUAL (b->running(), true);
484         b->set_finished_ok ();
485         dvdomatic_sleep (2);
486         BOOST_CHECK_EQUAL (b->finished_ok(), true);
487
488         /* Two jobs, dependency, first fails */
489         a.reset (new TestJob (s, o, &log, shared_ptr<Job> ()));
490         b.reset (new TestJob (s, o, &log, a));
491
492         JobManager::instance()->add (a);
493         JobManager::instance()->add (b);
494         dvdomatic_sleep (2);
495         BOOST_CHECK_EQUAL (a->running(), true);
496         BOOST_CHECK_EQUAL (b->running(), false);
497         a->set_finished_error ();
498         dvdomatic_sleep (2);
499         BOOST_CHECK_EQUAL (a->finished_in_error(), true);
500         BOOST_CHECK_EQUAL (b->running(), false);
501 }