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