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