summaryrefslogtreecommitdiff
path: root/test/create_cli_test.cc
blob: 1209f93440817f29e2332bb3d68f392e3fd60861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
    Copyright (C) 2019-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "lib/config.h"
#include "lib/content.h"
#include "lib/create_cli.h"
#include "lib/dcp_content_type.h"
#include "lib/film.h"
#include "lib/ratio.h"
#include "lib/video_content.h"
#include "test.h"
#include <fmt/format.h>
#include <boost/test/unit_test.hpp>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <iostream>


using std::string;


static CreateCLI
run (string cmd)
{
	/* This approximates the logic which splits command lines up into argc/argv */

	boost::escaped_list_separator<char> els ("", " ", "\"\'");
	boost::tokenizer<boost::escaped_list_separator<char> > tok (cmd, els);

	std::vector<char*> argv(256);
	int argc = 0;

	for (boost::tokenizer<boost::escaped_list_separator<char> >::iterator i = tok.begin(); i != tok.end(); ++i) {
		argv[argc++] = strdup (i->c_str());
	}

	CreateCLI cc (argc, argv.data());

	for (int i = 0; i < argc; ++i) {
		free (argv[i]);
	}

	return cc;
}

BOOST_AUTO_TEST_CASE (create_cli_test)
{
	string collected_error;
	auto error = [&collected_error](string s) {
		collected_error += s;
	};

	CreateCLI cc = run ("dcpomatic2_create --version");
	BOOST_CHECK (!cc.error);
	BOOST_CHECK (cc.version);

	cc = run ("dcpomatic2_create --versionX");
	BOOST_REQUIRE (cc.error);
	BOOST_CHECK (boost::algorithm::starts_with(*cc.error, "dcpomatic2_create: unrecognised option '--versionX'"));

	cc = run ("dcpomatic2_create --help");
	BOOST_REQUIRE (cc.error);

	cc = run ("dcpomatic2_create -h");
	BOOST_REQUIRE (cc.error);
	BOOST_CHECK(collected_error.empty());

	cc = run ("dcpomatic2_create x --name frobozz --template bar");
	BOOST_CHECK (!cc.error);
	BOOST_CHECK_EQUAL(cc._name, "frobozz");
	BOOST_REQUIRE(cc._template_name);
	BOOST_CHECK_EQUAL(*cc._template_name, "bar");
	BOOST_CHECK(collected_error.empty());

	cc = run ("dcpomatic2_create x --dcp-content-type FTR");
	BOOST_CHECK (!cc.error);
	BOOST_CHECK_EQUAL(cc._dcp_content_type, DCPContentType::from_isdcf_name("FTR"));

	cc = run ("dcpomatic2_create x --dcp-frame-rate 30");
	BOOST_CHECK (!cc.error);
	BOOST_REQUIRE (cc.dcp_frame_rate);
	BOOST_CHECK_EQUAL (*cc.dcp_frame_rate, 30);

	cc = run ("dcpomatic2_create x --container-ratio 185");
	BOOST_CHECK (!cc.error);
	BOOST_CHECK_EQUAL(cc._container_ratio, Ratio::from_id("185"));

	cc = run ("dcpomatic2_create x --container-ratio XXX");
	BOOST_CHECK (cc.error);

	cc = run ("dcpomatic2_create x --still-length 42");
	BOOST_CHECK (!cc.error);
	BOOST_CHECK_EQUAL(cc.still_length.get_value_or(0), 42);

	cc = run ("dcpomatic2_create x --standard SMPTE");
	BOOST_CHECK (!cc.error);
	BOOST_REQUIRE(cc._standard);
	BOOST_CHECK_EQUAL(*cc._standard, dcp::Standard::SMPTE);

	cc = run ("dcpomatic2_create x --standard interop");
	BOOST_CHECK (!cc.error);
	BOOST_REQUIRE(cc._standard);
	BOOST_CHECK_EQUAL(*cc._standard, dcp::Standard::INTEROP);

	cc = run ("dcpomatic2_create x --standard SMPTEX");
	BOOST_CHECK (cc.error);

	cc = run("dcpomatic2_create x --no-encrypt");
	BOOST_CHECK(cc._no_encrypt);

	cc = run("dcpomatic2_create x --encrypt");
	BOOST_CHECK(cc._encrypt);

	cc = run("dcpomatic2_create x --no-encrypt --encrypt");
	BOOST_CHECK(cc.error);

	cc = run("dcpomatic2_create x --twod");
	BOOST_CHECK(cc._twod);

	cc = run("dcpomatic2_create x --threed");
	BOOST_CHECK(cc._threed);

	cc = run("dcpomatic2_create x --twod --threed");
	BOOST_CHECK(cc.error);

	cc = run ("dcpomatic2_create x --config foo/bar");
	BOOST_CHECK (!cc.error);
	BOOST_REQUIRE (cc.config_dir);
	BOOST_CHECK_EQUAL (*cc.config_dir, "foo/bar");

	cc = run ("dcpomatic2_create x --output fred/jim");
	BOOST_CHECK (!cc.error);
	BOOST_REQUIRE (cc.output_dir);
	BOOST_CHECK_EQUAL (*cc.output_dir, "fred/jim");

	cc = run ("dcpomatic2_create x --outputX fred/jim");
	BOOST_CHECK (cc.error);

	cc = run ("dcpomatic2_create --config foo/bar --still-length 42 --output flaps fred jim sheila");
	BOOST_CHECK (!cc.error);
	BOOST_REQUIRE (cc.config_dir);
	BOOST_CHECK_EQUAL (*cc.config_dir, "foo/bar");
	BOOST_CHECK_EQUAL(cc.still_length.get_value_or(0), 42);
	BOOST_REQUIRE (cc.output_dir);
	BOOST_CHECK_EQUAL (*cc.output_dir, "flaps");
	BOOST_REQUIRE_EQUAL (cc.content.size(), 3U);
	BOOST_CHECK_EQUAL (cc.content[0].path, "fred");
	BOOST_CHECK_EQUAL (cc.content[0].frame_type, VideoFrameType::TWO_D);
	BOOST_CHECK_EQUAL (cc.content[1].path, "jim");
	BOOST_CHECK_EQUAL (cc.content[1].frame_type, VideoFrameType::TWO_D);
	BOOST_CHECK_EQUAL (cc.content[2].path, "sheila");
	BOOST_CHECK_EQUAL (cc.content[2].frame_type, VideoFrameType::TWO_D);

	cc = run ("dcpomatic2_create --left-eye left.mp4 --right-eye right.mp4");
	BOOST_REQUIRE_EQUAL (cc.content.size(), 2U);
	BOOST_CHECK_EQUAL (cc.content[0].path, "left.mp4");
	BOOST_CHECK_EQUAL (cc.content[0].frame_type, VideoFrameType::THREE_D_LEFT);
	BOOST_CHECK_EQUAL (cc.content[1].path, "right.mp4");
	BOOST_CHECK_EQUAL (cc.content[1].frame_type, VideoFrameType::THREE_D_RIGHT);
	BOOST_CHECK_EQUAL(cc._fourk, false);

	cc = run ("dcpomatic2_create --colourspace rec1886 test/data/flat_red.png");
	BOOST_REQUIRE_EQUAL(cc.content.size(), 1U);
	BOOST_CHECK_EQUAL(cc.content[0].colour_conversion.get_value_or(""), "rec1886");
	BOOST_CHECK(!cc.error);
	auto film = cc.make_film(error);
	BOOST_REQUIRE_EQUAL(film->content().size(), 1U);
	BOOST_REQUIRE(static_cast<bool>(film->content()[0]->video->colour_conversion()));
	BOOST_REQUIRE(film->content()[0]->video->colour_conversion() == PresetColourConversion::from_id("rec1886").conversion);

	cc = run ("dcpomatic2_create --colourspace ostrobogulous foo.mp4");
	BOOST_CHECK_EQUAL(cc.error.get_value_or(""), "dcpomatic2_create: ostrobogulous is not a recognised colourspace");

	cc = run ("dcpomatic2_create --twok foo.mp4");
	BOOST_REQUIRE_EQUAL (cc.content.size(), 1U);
	BOOST_CHECK_EQUAL (cc.content[0].path, "foo.mp4");
	BOOST_CHECK_EQUAL(cc._twok, true);
	BOOST_CHECK (!cc.error);

	cc = run ("dcpomatic2_create --fourk foo.mp4");
	BOOST_REQUIRE_EQUAL (cc.content.size(), 1U);
	BOOST_CHECK_EQUAL (cc.content[0].path, "foo.mp4");
	BOOST_CHECK_EQUAL(cc._fourk, true);
	BOOST_CHECK (!cc.error);

	cc = run("dcpomatic2_create --auto-crop foo.mp4 bar.mp4 --auto-crop baz.mp4");
	BOOST_REQUIRE_EQUAL(cc.content.size(), 3U);
	BOOST_CHECK(cc.content[0].auto_crop);
	BOOST_CHECK(!cc.content[1].auto_crop);
	BOOST_CHECK(cc.content[2].auto_crop);

	cc = run("dcpomatic2_create --auto-crop foo.mp4 bar.mp4 --auto-crop baz.mp4");
	BOOST_REQUIRE_EQUAL(cc.content.size(), 3U);
	BOOST_CHECK(cc.content[0].auto_crop);
	BOOST_CHECK(!cc.content[1].auto_crop);
	BOOST_CHECK(cc.content[2].auto_crop);

	cc = run("dcpomatic2_create --auto-crop-threshold 42 --auto-crop foo.mp4 bar.mp4 --auto-crop baz.mp4");
	BOOST_REQUIRE_EQUAL(cc.content.size(), 3U);
	BOOST_CHECK(cc.content[0].auto_crop);
	BOOST_CHECK(!cc.content[1].auto_crop);
	BOOST_CHECK(cc.content[2].auto_crop);
	BOOST_CHECK_EQUAL(cc.auto_crop_threshold.get_value_or(0), 42);

	auto pillarbox = TestPaths::private_data() / "pillarbox.png";
	cc = run("dcpomatic2_create --auto-crop " + pillarbox.string());
	film = cc.make_film(error);
	BOOST_CHECK_EQUAL(film->content().size(), 1U);
	BOOST_CHECK(film->content()[0]->video->actual_crop() == Crop(113, 262, 0, 0));
	BOOST_CHECK_EQUAL(collected_error, fmt::format("Cropped {} to 113 left, 262 right, 0 top and 0 bottom", pillarbox.string()));
	collected_error = "";

	cc = run ("dcpomatic2_create --video-bit-rate 120 foo.mp4");
	BOOST_REQUIRE_EQUAL (cc.content.size(), 1U);
	BOOST_CHECK_EQUAL (cc.content[0].path, "foo.mp4");
	BOOST_REQUIRE(cc._video_bit_rate);
	BOOST_CHECK_EQUAL(*cc._video_bit_rate, 120000000);
	BOOST_CHECK (!cc.error);

	cc = run ("dcpomatic2_create --channel L test/data/L.wav --channel R test/data/R.wav test/data/Lfe.wav");
	BOOST_REQUIRE_EQUAL (cc.content.size(), 3U);
	BOOST_CHECK_EQUAL (cc.content[0].path, "test/data/L.wav");
	BOOST_CHECK (cc.content[0].channel);
	BOOST_CHECK (*cc.content[0].channel == dcp::Channel::LEFT);
	BOOST_CHECK_EQUAL (cc.content[1].path, "test/data/R.wav");
	BOOST_CHECK (cc.content[1].channel);
	BOOST_CHECK (*cc.content[1].channel == dcp::Channel::RIGHT);
	BOOST_CHECK_EQUAL (cc.content[2].path, "test/data/Lfe.wav");
	BOOST_CHECK (!cc.content[2].channel);
	film = cc.make_film(error);
	BOOST_CHECK_EQUAL(film->audio_channels(), 6);
	BOOST_CHECK(collected_error.empty());

	cc = run ("dcpomatic2_create --channel foo fred.wav");
	BOOST_REQUIRE (cc.error);
	BOOST_CHECK (boost::algorithm::starts_with(*cc.error, "dcpomatic2_create: foo is not valid for --channel"));

	cc = run ("dcpomatic2_create fred.wav --gain -6 jim.wav --gain 2 sheila.wav");
	BOOST_REQUIRE_EQUAL (cc.content.size(), 3U);
	BOOST_CHECK_EQUAL (cc.content[0].path, "fred.wav");
	BOOST_CHECK (!cc.content[0].gain);
	BOOST_CHECK_EQUAL (cc.content[1].path, "jim.wav");
	BOOST_CHECK_CLOSE (*cc.content[1].gain, -6, 0.001);
	BOOST_CHECK_EQUAL (cc.content[2].path, "sheila.wav");
	BOOST_CHECK_CLOSE (*cc.content[2].gain, 2, 0.001);

	cc = run("dcpomatic2_create --cpl 123456-789-0 dcp");
	BOOST_REQUIRE_EQUAL(cc.content.size(), 1U);
	BOOST_CHECK_EQUAL(cc.content[0].path, "dcp");
	BOOST_REQUIRE(static_cast<bool>(cc.content[0].cpl));
	BOOST_CHECK_EQUAL(*cc.content[0].cpl, "123456-789-0");

	cc = run("dcpomatic2_create -s SMPTE sheila.wav");
	BOOST_CHECK(!cc.still_length);
	BOOST_CHECK(cc.error);

	cc = run("dcpomatic2_create --channel L fred.wav --channel R jim.wav --channel C sheila.wav --audio-channels 2");
	BOOST_REQUIRE(cc.error);
	BOOST_CHECK_EQUAL(*cc.error, "dcpomatic2_create: cannot map audio as requested with only 2 channels");

	cc = run("dcpomatic2_create --channel L fred.wav --channel R jim.wav --channel C sheila.wav --audio-channels 3");
	BOOST_REQUIRE(cc.error);
	BOOST_CHECK_EQUAL(*cc.error, "dcpomatic2_create: audio channel count must be even");

	cc = run("dcpomatic2_create --channel L test/data/L.wav --channel R test/data/R.wav --channel C test/data/C.wav");
	BOOST_CHECK(!cc.error);
	film = cc.make_film(error);
	BOOST_CHECK_EQUAL(film->audio_channels(), 6);
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create --channel L test/data/L.wav --channel R test/data/R.wav --channel HI test/data/sine_440.wav");
	BOOST_CHECK(!cc.error);
	film = cc.make_film(error);
	BOOST_CHECK_EQUAL(film->audio_channels(), 8);
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create --channel L test/data/L.wav --channel R test/data/R.wav --channel C test/data/C.wav --audio-channels 16");
	BOOST_CHECK(!cc.error);
	film = cc.make_film(error);
	BOOST_CHECK_EQUAL(film->audio_channels(), 16);
	BOOST_CHECK(collected_error.empty());
}


BOOST_AUTO_TEST_CASE(create_cli_template_test)
{
	ConfigRestorer cr("test/data");

	string collected_error;
	auto error = [&collected_error](string s) {
		collected_error += s;
	};

	auto cc = run("dcpomatic2_create test/data/flat_red.png");
	auto film = cc.make_film(error);
	BOOST_CHECK(!film->three_d());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template 2d");
	film = cc.make_film(error);
	BOOST_CHECK(!film->three_d());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template 2d --threed");
	film = cc.make_film(error);
	BOOST_CHECK(film->three_d());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template 3d");
	film = cc.make_film(error);
	BOOST_CHECK(film->three_d());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template 3d --twod");
	film = cc.make_film(error);
	BOOST_CHECK(!film->three_d());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png");
	film = cc.make_film(error);
	BOOST_CHECK(!film->encrypted());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template unencrypted");
	film = cc.make_film(error);
	BOOST_CHECK(!film->encrypted());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template unencrypted --encrypt");
	film = cc.make_film(error);
	BOOST_CHECK(film->encrypted());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template encrypted");
	film = cc.make_film(error);
	BOOST_CHECK(film->encrypted());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template encrypted --no-encrypt");
	film = cc.make_film(error);
	BOOST_CHECK(!film->encrypted());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png");
	film = cc.make_film(error);
	BOOST_CHECK(!film->interop());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template interop");
	film = cc.make_film(error);
	BOOST_CHECK(film->interop());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template interop --standard SMPTE");
	film = cc.make_film(error);
	BOOST_CHECK(!film->interop());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template smpte");
	film = cc.make_film(error);
	BOOST_CHECK(!film->interop());
	BOOST_CHECK(collected_error.empty());

	cc = run("dcpomatic2_create test/data/flat_red.png --template smpte --standard interop");
	film = cc.make_film(error);
	BOOST_CHECK(film->interop());
	BOOST_CHECK(collected_error.empty());
}