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