Add dcpomatic2_map tool (#2445).
[dcpomatic.git] / test / map_cli_test.cc
1 /*
2     Copyright (C) 2023 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "lib/config.h"
23 #include "lib/content.h"
24 #include "lib/dcp_content.h"
25 #include "lib/content_factory.h"
26 #include "lib/film.h"
27 #include "lib/map_cli.h"
28 #include "test.h"
29 #include <dcp/cpl.h>
30 #include <dcp/dcp.h>
31 #include <dcp/reel.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>
38
39
40 using std::dynamic_pointer_cast;
41 using std::make_shared;
42 using std::shared_ptr;
43 using std::string;
44 using std::vector;
45 using boost::optional;
46
47
48 static
49 optional<string>
50 run(vector<string> const& args, vector<string>& output)
51 {
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());
55         }
56         argv[args.size()] = nullptr;
57
58         auto error = map_cli(args.size(), argv.data(), [&output](string s) { output.push_back(s); });
59         if (error) {
60                 std::cout << *error << "\n";
61         }
62
63         return error;
64 }
65
66
67 static
68 boost::filesystem::path
69 find_prefix(boost::filesystem::path dir, string prefix)
70 {
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);
73         });
74
75         BOOST_REQUIRE(iter != boost::filesystem::directory_iterator());
76         return iter->path();
77 }
78
79
80 static
81 boost::filesystem::path
82 find_cpl(boost::filesystem::path dir)
83 {
84         return find_prefix(dir, "cpl_");
85 }
86
87
88 /** Map a single DCP into a new DCP */
89 BOOST_AUTO_TEST_CASE(map_simple_dcp_copy)
90 {
91         string const name = "map_simple_dcp_copy";
92         string const out = String::compose("build/test/%1_out", name);
93
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);
97
98         vector<string> const args = {
99                 "map_cli",
100                 "-o", out,
101                 "-d", film->dir(film->dcp_name()).string(),
102                 find_cpl(film->dir(film->dcp_name())).string()
103         };
104
105         boost::filesystem::remove_all(out);
106
107         vector<string> output_messages;
108         auto error = run(args, output_messages);
109         BOOST_CHECK(!error);
110
111         verify_dcp(out, {});
112
113         BOOST_CHECK(boost::filesystem::is_regular_file(find_prefix(out, "j2c_")));
114         BOOST_CHECK(boost::filesystem::is_regular_file(find_prefix(out, "pcm_")));
115 }
116
117
118 /** Map a single DCP into a new DCP using the symlink option */
119 BOOST_AUTO_TEST_CASE(map_simple_dcp_copy_with_symlinks)
120 {
121         string const name = "map_simple_dcp_copy_with_symlinks";
122         string const out = String::compose("build/test/%1_out", name);
123
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);
127
128         vector<string> const args = {
129                 "map_cli",
130                 "-o", out,
131                 "-d", film->dir(film->dcp_name()).string(),
132                 "-s",
133                 find_cpl(film->dir(film->dcp_name())).string()
134         };
135
136         boost::filesystem::remove_all(out);
137
138         vector<string> output_messages;
139         auto error = run(args, output_messages);
140         BOOST_CHECK(!error);
141
142         /* We can't verify this DCP because the symlinks will make it fail
143          * (as it should be, I think).
144          */
145
146         BOOST_CHECK(boost::filesystem::is_symlink(find_prefix(out, "j2c_")));
147         BOOST_CHECK(boost::filesystem::is_symlink(find_prefix(out, "pcm_")));
148 }
149
150
151 /** Map a single DCP into a new DCP using the hardlink option */
152 BOOST_AUTO_TEST_CASE(map_simple_dcp_copy_with_hardlinks)
153 {
154         string const name = "map_simple_dcp_copy_with_hardlinks";
155         string const out = String::compose("build/test/%1_out", name);
156
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);
160
161         vector<string> const args = {
162                 "map_cli",
163                 "-o", out,
164                 "-d", film->dir(film->dcp_name()).string(),
165                 "-l",
166                 find_cpl(film->dir(film->dcp_name())).string()
167         };
168
169         boost::filesystem::remove_all(out);
170
171         vector<string> output_messages;
172         auto error = run(args, output_messages);
173         BOOST_CHECK(!error);
174
175         verify_dcp(out, {});
176
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);
180 }
181
182
183 /** Map a single Interop DCP with subs into a new DCP */
184 BOOST_AUTO_TEST_CASE(map_simple_interop_dcp_with_subs)
185 {
186         string const name = "map_simple_interop_dcp_with_subs";
187         string const out = String::compose("build/test/%1_out", name);
188
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});
194
195         vector<string> const args = {
196                 "map_cli",
197                 "-o", out,
198                 "-d", film->dir(film->dcp_name()).string(),
199                 find_cpl(film->dir(film->dcp_name())).string()
200         };
201
202         boost::filesystem::remove_all(out);
203
204         vector<string> output_messages;
205         auto error = run(args, output_messages);
206         BOOST_CHECK(!error);
207
208         verify_dcp(out, {dcp::VerificationNote::Code::INVALID_STANDARD});
209 }
210
211
212 /** Map an OV and a VF into a single DCP */
213 BOOST_AUTO_TEST_CASE(map_ov_vf_copy)
214 {
215         string const name = "map_ov_vf_copy";
216         string const out = String::compose("build/test/%1_out", name);
217
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);
221
222         auto const ov_dir = ov_film->dir(ov_film->dcp_name());
223
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});
229
230         auto const vf_dir = vf_film->dir(vf_film->dcp_name());
231
232         vector<string> const args = {
233                 "map_cli",
234                 "-o", out,
235                 "-d", ov_dir.string(),
236                 "-d", vf_dir.string(),
237                 find_cpl(vf_dir).string()
238         };
239
240         boost::filesystem::remove_all(out);
241
242         vector<string> output_messages;
243         auto error = run(args, output_messages);
244         BOOST_CHECK(!error);
245
246         verify_dcp(out, {});
247
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_"));
251 }
252
253
254 /** Map a single DCP into a new DCP using the rename option */
255 BOOST_AUTO_TEST_CASE(map_simple_dcp_copy_with_rename)
256 {
257         ConfigRestorer cr;
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);
261
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);
265
266         vector<string> const args = {
267                 "map_cli",
268                 "-o", out,
269                 "-d", film->dir(film->dcp_name()).string(),
270                 "-r",
271                 find_cpl(film->dir(film->dcp_name())).string()
272         };
273
274         boost::filesystem::remove_all(out);
275
276         vector<string> output_messages;
277         auto error = run(args, output_messages);
278         BOOST_CHECK(!error);
279
280         verify_dcp(out, {});
281
282         dcp::DCP out_dcp(out);
283         out_dcp.read();
284
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);
295
296         BOOST_REQUIRE(picture->file());
297         BOOST_CHECK(picture->file().get().filename() == picture->id() + ".mxf");
298
299         BOOST_REQUIRE(sound->file());
300         BOOST_CHECK(sound->file().get().filename() == sound->id() + ".mxf");
301 }
302
303
304 static
305 void
306 test_two_cpls_each_with_subs(string name, bool interop)
307 {
308         string const out = String::compose("build/test/%1_out", name);
309
310         vector<dcp::VerificationNote::Code> acceptable_errors;
311         if (interop) {
312                 acceptable_errors.push_back(dcp::VerificationNote::Code::INVALID_STANDARD);
313         } else {
314                 acceptable_errors.push_back(dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE);
315                 acceptable_errors.push_back(dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME);
316         }
317
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);
325         }
326
327         vector<string> const args = {
328                 "map_cli",
329                 "-o", out,
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()
334         };
335
336         boost::filesystem::remove_all(out);
337
338         vector<string> output_messages;
339         auto error = run(args, output_messages);
340         BOOST_CHECK(!error);
341
342         verify_dcp(out, acceptable_errors);
343 }
344
345
346 BOOST_AUTO_TEST_CASE(map_two_interop_cpls_each_with_subs)
347 {
348         test_two_cpls_each_with_subs("map_two_interop_cpls_each_with_subs", true);
349 }
350
351
352 BOOST_AUTO_TEST_CASE(map_two_smpte_cpls_each_with_subs)
353 {
354         test_two_cpls_each_with_subs("map_two_smpte_cpls_each_with_subs", false);
355 }