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