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});
212 /** Map an OV and a VF into a single DCP */
213 BOOST_AUTO_TEST_CASE(map_ov_vf_copy)
215 string const name = "map_ov_vf_copy";
216 string const out = String::compose("build/test/%1_out", name);
218 auto ov_content = content_factory("test/data/flat_red.png");
219 auto ov_film = new_test_film2(name + "_ov", ov_content);
220 make_and_verify_dcp(ov_film);
222 auto const ov_dir = ov_film->dir(ov_film->dcp_name());
224 auto vf_ov = make_shared<DCPContent>(ov_dir);
225 auto vf_sound = content_factory("test/data/sine_440.wav").front();
226 auto vf_film = new_test_film2(name + "_vf", { vf_ov, vf_sound });
227 vf_ov->set_reference_video(true);
228 make_and_verify_dcp(vf_film, {dcp::VerificationNote::Code::EXTERNAL_ASSET});
230 auto const vf_dir = vf_film->dir(vf_film->dcp_name());
232 vector<string> const args = {
235 "-d", ov_dir.string(),
236 "-d", vf_dir.string(),
237 find_cpl(vf_dir).string()
240 boost::filesystem::remove_all(out);
242 vector<string> output_messages;
243 auto error = run(args, output_messages);
248 check_file(find_file(out, "cpl_"), find_file(vf_dir, "cpl_"));
249 check_file(find_file(out, "j2c_"), find_file(ov_dir, "j2c_"));
250 check_file(find_file(out, "pcm_"), find_file(vf_dir, "pcm_"));
254 /** Map a single DCP into a new DCP using the rename option */
255 BOOST_AUTO_TEST_CASE(map_simple_dcp_copy_with_rename)
258 Config::instance()->set_dcp_asset_filename_format(dcp::NameFormat("hello%c"));
259 string const name = "map_simple_dcp_copy_with_rename";
260 string const out = String::compose("build/test/%1_out", name);
262 auto content = content_factory("test/data/flat_red.png");
263 auto film = new_test_film2(name + "_in", content);
264 make_and_verify_dcp(film);
266 vector<string> const args = {
269 "-d", film->dir(film->dcp_name()).string(),
271 find_cpl(film->dir(film->dcp_name())).string()
274 boost::filesystem::remove_all(out);
276 vector<string> output_messages;
277 auto error = run(args, output_messages);
282 dcp::DCP out_dcp(out);
285 BOOST_REQUIRE_EQUAL(out_dcp.cpls().size(), 1U);
286 auto const cpl = out_dcp.cpls()[0];
287 BOOST_REQUIRE_EQUAL(cpl->reels().size(), 1U);
288 auto const reel = cpl->reels()[0];
289 BOOST_REQUIRE(reel->main_picture());
290 BOOST_REQUIRE(reel->main_sound());
291 auto const picture = reel->main_picture()->asset();
292 BOOST_REQUIRE(picture);
293 auto const sound = reel->main_sound()->asset();
294 BOOST_REQUIRE(sound);
296 BOOST_REQUIRE(picture->file());
297 BOOST_CHECK(picture->file().get().filename() == picture->id() + ".mxf");
299 BOOST_REQUIRE(sound->file());
300 BOOST_CHECK(sound->file().get().filename() == sound->id() + ".mxf");
306 test_two_cpls_each_with_subs(string name, bool interop)
308 string const out = String::compose("build/test/%1_out", name);
310 vector<dcp::VerificationNote::Code> acceptable_errors;
312 acceptable_errors.push_back(dcp::VerificationNote::Code::INVALID_STANDARD);
314 acceptable_errors.push_back(dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE);
315 acceptable_errors.push_back(dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME);
318 shared_ptr<Film> films[2];
319 for (auto i = 0; i < 2; ++i) {
320 auto picture = content_factory("test/data/flat_red.png").front();
321 auto subs = content_factory("test/data/15s.srt").front();
322 films[i] = new_test_film2(String::compose("%1_%2_in", name, i), { picture, subs });
323 films[i]->set_interop(interop);
324 make_and_verify_dcp(films[i], acceptable_errors);
327 vector<string> const args = {
330 "-d", films[0]->dir(films[0]->dcp_name()).string(),
331 "-d", films[1]->dir(films[1]->dcp_name()).string(),
332 find_cpl(films[0]->dir(films[0]->dcp_name())).string(),
333 find_cpl(films[1]->dir(films[1]->dcp_name())).string()
336 boost::filesystem::remove_all(out);
338 vector<string> output_messages;
339 auto error = run(args, output_messages);
342 verify_dcp(out, acceptable_errors);
346 BOOST_AUTO_TEST_CASE(map_two_interop_cpls_each_with_subs)
348 test_two_cpls_each_with_subs("map_two_interop_cpls_each_with_subs", true);
352 BOOST_AUTO_TEST_CASE(map_two_smpte_cpls_each_with_subs)
354 test_two_cpls_each_with_subs("map_two_smpte_cpls_each_with_subs", false);