2 Copyright (C) 2023 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/config.h"
23 #include "lib/content.h"
24 #include "lib/dcp_content.h"
25 #include "lib/content_factory.h"
27 #include "lib/map_cli.h"
32 #include <dcp/reel_picture_asset.h>
33 #include <dcp/reel_sound_asset.h>
34 #include <boost/algorithm/string.hpp>
35 #include <boost/filesystem.hpp>
36 #include <boost/optional.hpp>
37 #include <boost/test/unit_test.hpp>
40 using std::dynamic_pointer_cast;
41 using std::make_shared;
42 using std::shared_ptr;
45 using boost::optional;
50 run(vector<string> const& args, vector<string>& output)
52 vector<char*> argv(args.size() + 1);
53 for (auto i = 0U; i < args.size(); ++i) {
54 argv[i] = const_cast<char*>(args[i].c_str());
56 argv[args.size()] = nullptr;
58 auto error = map_cli(args.size(), argv.data(), [&output](string s) { output.push_back(s); });
60 std::cout << *error << "\n";
68 boost::filesystem::path
69 find_prefix(boost::filesystem::path dir, string prefix)
71 auto iter = std::find_if(boost::filesystem::directory_iterator(dir), boost::filesystem::directory_iterator(), [prefix](boost::filesystem::path const& p) {
72 return boost::starts_with(p.filename().string(), prefix);
75 BOOST_REQUIRE(iter != boost::filesystem::directory_iterator());
81 boost::filesystem::path
82 find_cpl(boost::filesystem::path dir)
84 return find_prefix(dir, "cpl_");
88 /** Map a single DCP into a new DCP */
89 BOOST_AUTO_TEST_CASE(map_simple_dcp_copy)
91 string const name = "map_simple_dcp_copy";
92 string const out = String::compose("build/test/%1_out", name);
94 auto content = content_factory("test/data/flat_red.png");
95 auto film = new_test_film2(name + "_in", content);
96 make_and_verify_dcp(film);
98 vector<string> const args = {
101 "-d", film->dir(film->dcp_name()).string(),
102 find_cpl(film->dir(film->dcp_name())).string()
105 boost::filesystem::remove_all(out);
107 vector<string> output_messages;
108 auto error = run(args, output_messages);
113 BOOST_CHECK(boost::filesystem::is_regular_file(find_prefix(out, "j2c_")));
114 BOOST_CHECK(boost::filesystem::is_regular_file(find_prefix(out, "pcm_")));
118 /** Map a single DCP into a new DCP using the symlink option */
119 BOOST_AUTO_TEST_CASE(map_simple_dcp_copy_with_symlinks)
121 string const name = "map_simple_dcp_copy_with_symlinks";
122 string const out = String::compose("build/test/%1_out", name);
124 auto content = content_factory("test/data/flat_red.png");
125 auto film = new_test_film2(name + "_in", content);
126 make_and_verify_dcp(film);
128 vector<string> const args = {
131 "-d", film->dir(film->dcp_name()).string(),
133 find_cpl(film->dir(film->dcp_name())).string()
136 boost::filesystem::remove_all(out);
138 vector<string> output_messages;
139 auto error = run(args, output_messages);
142 /* We can't verify this DCP because the symlinks will make it fail
143 * (as it should be, I think).
146 BOOST_CHECK(boost::filesystem::is_symlink(find_prefix(out, "j2c_")));
147 BOOST_CHECK(boost::filesystem::is_symlink(find_prefix(out, "pcm_")));
151 /** Map a single DCP into a new DCP using the hardlink option */
152 BOOST_AUTO_TEST_CASE(map_simple_dcp_copy_with_hardlinks)
154 string const name = "map_simple_dcp_copy_with_hardlinks";
155 string const out = String::compose("build/test/%1_out", name);
157 auto content = content_factory("test/data/flat_red.png");
158 auto film = new_test_film2(name + "_in", content);
159 make_and_verify_dcp(film);
161 vector<string> const args = {
164 "-d", film->dir(film->dcp_name()).string(),
166 find_cpl(film->dir(film->dcp_name())).string()
169 boost::filesystem::remove_all(out);
171 vector<string> output_messages;
172 auto error = run(args, output_messages);
177 /* The video file will have 3 links because DoM also makes a link into the video directory */
178 BOOST_CHECK_EQUAL(boost::filesystem::hard_link_count(find_prefix(out, "j2c_")), 3U);
179 BOOST_CHECK_EQUAL(boost::filesystem::hard_link_count(find_prefix(out, "pcm_")), 2U);
183 /** Map a single Interop DCP with subs into a new DCP */
184 BOOST_AUTO_TEST_CASE(map_simple_interop_dcp_with_subs)
186 string const name = "map_simple_interop_dcp_with_subs";
187 string const out = String::compose("build/test/%1_out", name);
189 auto picture = content_factory("test/data/flat_red.png").front();
190 auto subs = content_factory("test/data/15s.srt").front();
191 auto film = new_test_film2(name + "_in", { picture, subs });
192 film->set_interop(true);
193 make_and_verify_dcp(film, {dcp::VerificationNote::Code::INVALID_STANDARD});
195 vector<string> const args = {
198 "-d", film->dir(film->dcp_name()).string(),
199 find_cpl(film->dir(film->dcp_name())).string()
202 boost::filesystem::remove_all(out);
204 vector<string> output_messages;
205 auto error = run(args, output_messages);
208 verify_dcp(out, {dcp::VerificationNote::Code::INVALID_STANDARD});
214 test_map_ov_vf_copy(vector<string> extra_args = {})
216 string const name = "map_ov_vf_copy";
217 string const out = String::compose("build/test/%1_out", name);
219 auto ov_content = content_factory("test/data/flat_red.png");
220 auto ov_film = new_test_film2(name + "_ov", ov_content);
221 make_and_verify_dcp(ov_film);
223 auto const ov_dir = ov_film->dir(ov_film->dcp_name());
225 auto vf_ov = make_shared<DCPContent>(ov_dir);
226 auto vf_sound = content_factory("test/data/sine_440.wav").front();
227 auto vf_film = new_test_film2(name + "_vf", { vf_ov, vf_sound });
228 vf_ov->set_reference_video(true);
229 make_and_verify_dcp(vf_film, {dcp::VerificationNote::Code::EXTERNAL_ASSET});
231 auto const vf_dir = vf_film->dir(vf_film->dcp_name());
233 vector<string> args = {
236 "-d", ov_dir.string(),
237 "-d", vf_dir.string(),
238 find_cpl(vf_dir).string()
241 args.insert(std::end(args), std::begin(extra_args), std::end(extra_args));
243 boost::filesystem::remove_all(out);
245 vector<string> output_messages;
246 auto error = run(args, output_messages);
251 check_file(find_file(out, "cpl_"), find_file(vf_dir, "cpl_"));
252 check_file(find_file(out, "j2c_"), find_file(ov_dir, "j2c_"));
253 check_file(find_file(out, "pcm_"), find_file(vf_dir, "pcm_"));
257 /** Map an OV and a VF into a single DCP */
258 BOOST_AUTO_TEST_CASE(map_ov_vf_copy)
260 test_map_ov_vf_copy();
261 test_map_ov_vf_copy({"-l"});
265 /** Map an OV and VF into a single DCP, where the VF refers to the OV's assets multiple times */
266 BOOST_AUTO_TEST_CASE(map_ov_vf_copy_multiple_reference)
268 string const name = "map_ov_vf_copy_multiple_reference";
269 string const out = String::compose("build/test/%1_out", name);
271 auto ov_content = content_factory("test/data/flat_red.png");
272 auto ov_film = new_test_film2(name + "_ov", ov_content);
273 make_and_verify_dcp(ov_film);
275 auto const ov_dir = ov_film->dir(ov_film->dcp_name());
277 auto vf_ov1 = make_shared<DCPContent>(ov_dir);
278 auto vf_ov2 = make_shared<DCPContent>(ov_dir);
279 auto vf_sound = content_factory("test/data/sine_440.wav").front();
280 auto vf_film = new_test_film2(name + "_vf", { vf_ov1, vf_ov2, vf_sound });
281 vf_film->set_reel_type(ReelType::BY_VIDEO_CONTENT);
282 vf_ov2->set_position(vf_film, vf_ov1->end(vf_film));
283 vf_ov1->set_reference_video(true);
284 vf_ov2->set_reference_video(true);
285 make_and_verify_dcp(vf_film, {dcp::VerificationNote::Code::EXTERNAL_ASSET});
287 auto const vf_dir = vf_film->dir(vf_film->dcp_name());
289 vector<string> const args = {
292 "-d", ov_dir.string(),
293 "-d", vf_dir.string(),
295 find_cpl(vf_dir).string()
298 boost::filesystem::remove_all(out);
300 vector<string> output_messages;
301 auto error = run(args, output_messages);
306 check_file(find_file(out, "cpl_"), find_file(vf_dir, "cpl_"));
307 check_file(find_file(out, "j2c_"), find_file(ov_dir, "j2c_"));
311 /** Map a single DCP into a new DCP using the rename option */
312 BOOST_AUTO_TEST_CASE(map_simple_dcp_copy_with_rename)
315 Config::instance()->set_dcp_asset_filename_format(dcp::NameFormat("hello%c"));
316 string const name = "map_simple_dcp_copy_with_rename";
317 string const out = String::compose("build/test/%1_out", name);
319 auto content = content_factory("test/data/flat_red.png");
320 auto film = new_test_film2(name + "_in", content);
321 make_and_verify_dcp(film);
323 vector<string> const args = {
326 "-d", film->dir(film->dcp_name()).string(),
328 find_cpl(film->dir(film->dcp_name())).string()
331 boost::filesystem::remove_all(out);
333 vector<string> output_messages;
334 auto error = run(args, output_messages);
339 dcp::DCP out_dcp(out);
342 BOOST_REQUIRE_EQUAL(out_dcp.cpls().size(), 1U);
343 auto const cpl = out_dcp.cpls()[0];
344 BOOST_REQUIRE_EQUAL(cpl->reels().size(), 1U);
345 auto const reel = cpl->reels()[0];
346 BOOST_REQUIRE(reel->main_picture());
347 BOOST_REQUIRE(reel->main_sound());
348 auto const picture = reel->main_picture()->asset();
349 BOOST_REQUIRE(picture);
350 auto const sound = reel->main_sound()->asset();
351 BOOST_REQUIRE(sound);
353 BOOST_REQUIRE(picture->file());
354 BOOST_CHECK(picture->file().get().filename() == picture->id() + ".mxf");
356 BOOST_REQUIRE(sound->file());
357 BOOST_CHECK(sound->file().get().filename() == sound->id() + ".mxf");
363 test_two_cpls_each_with_subs(string name, bool interop)
365 string const out = String::compose("build/test/%1_out", name);
367 vector<dcp::VerificationNote::Code> acceptable_errors;
369 acceptable_errors.push_back(dcp::VerificationNote::Code::INVALID_STANDARD);
371 acceptable_errors.push_back(dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE);
372 acceptable_errors.push_back(dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME);
375 shared_ptr<Film> films[2];
376 for (auto i = 0; i < 2; ++i) {
377 auto picture = content_factory("test/data/flat_red.png").front();
378 auto subs = content_factory("test/data/15s.srt").front();
379 films[i] = new_test_film2(String::compose("%1_%2_in", name, i), { picture, subs });
380 films[i]->set_interop(interop);
381 make_and_verify_dcp(films[i], acceptable_errors);
384 vector<string> const args = {
387 "-d", films[0]->dir(films[0]->dcp_name()).string(),
388 "-d", films[1]->dir(films[1]->dcp_name()).string(),
389 find_cpl(films[0]->dir(films[0]->dcp_name())).string(),
390 find_cpl(films[1]->dir(films[1]->dcp_name())).string()
393 boost::filesystem::remove_all(out);
395 vector<string> output_messages;
396 auto error = run(args, output_messages);
399 verify_dcp(out, acceptable_errors);
403 BOOST_AUTO_TEST_CASE(map_two_interop_cpls_each_with_subs)
405 test_two_cpls_each_with_subs("map_two_interop_cpls_each_with_subs", true);
409 BOOST_AUTO_TEST_CASE(map_two_smpte_cpls_each_with_subs)
411 test_two_cpls_each_with_subs("map_two_smpte_cpls_each_with_subs", false);
415 BOOST_AUTO_TEST_CASE(map_with_given_config)
417 string const name = "map_with_given_config";
418 string const out = String::compose("build/test/%1_out", name);
420 auto content = content_factory("test/data/flat_red.png");
421 auto film = new_test_film2(name + "_in", content);
422 make_and_verify_dcp(film);
424 vector<string> const args = {
427 "-d", film->dir(film->dcp_name()).string(),
428 "--config", "test/data/map_with_given_config",
429 find_cpl(film->dir(film->dcp_name())).string()
432 boost::filesystem::remove_all(out);
434 Config::instance()->drop();
435 vector<string> output_messages;
436 auto error = run(args, output_messages);
439 /* It should be signed by the key in test/data/map_with_given_config, not the one in test/data/signer_key */
440 BOOST_CHECK(dcp::file_to_string(find_file(out, "cpl_")).find("dnQualifier=\\+uOcNN2lPuxpxgd/5vNkkBER0GE=,CN=CS.dcpomatic.smpte-430-2.LEAF,OU=dcpomatic.com,O=dcpomatic.com") != std::string::npos);