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