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