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