Fix silent stereo mixdown exports when the project audio channel count is > 6.
[dcpomatic.git] / test / hints_test.cc
1 /*
2     Copyright (C) 2020-2022 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/audio_content.h"
23 #include "lib/config.h"
24 #include "lib/constants.h"
25 #include "lib/content.h"
26 #include "lib/content_factory.h"
27 #include "lib/cross.h"
28 #include "lib/film.h"
29 #include "lib/font.h"
30 #include "lib/hints.h"
31 #include "lib/text_content.h"
32 #include "test.h"
33 #include <boost/test/unit_test.hpp>
34
35
36 using std::make_shared;
37 using std::shared_ptr;
38 using std::string;
39 using std::vector;
40 using boost::optional;
41
42
43 vector<string> current_hints;
44
45
46 static
47 void
48 collect_hint (string hint)
49 {
50         current_hints.push_back (hint);
51 }
52
53
54 static
55 vector<string>
56 get_hints (shared_ptr<Film> film)
57 {
58         current_hints.clear ();
59         Hints hints (film);
60         /* None of our tests need the audio analysis, and it is quite time-consuming */
61         hints.disable_audio_analysis ();
62         hints.Hint.connect (collect_hint);
63         hints.start ();
64         hints.join ();
65         while (signal_manager->ui_idle()) {}
66         hints.rethrow();
67         return current_hints;
68 }
69
70
71 static
72 void
73 check (TextType type, string name, optional<string> expected_hint = optional<string>())
74 {
75         auto film = new_test_film2 (name);
76         auto content = content_factory("test/data/" + name + ".srt")[0];
77         content->text.front()->set_type (type);
78         content->text.front()->set_language (dcp::LanguageTag("en-US"));
79         film->examine_and_add_content (content);
80         BOOST_REQUIRE (!wait_for_jobs());
81         auto hints = get_hints (film);
82
83         if (expected_hint) {
84                 BOOST_REQUIRE_EQUAL (hints.size(), 1U);
85                 BOOST_CHECK_EQUAL (hints[0], *expected_hint);
86         } else {
87                 string message;
88                 for (auto hint: hints) {
89                         message += hint + "\n";
90                 }
91                 BOOST_CHECK_MESSAGE(hints.empty(), "Found: " << message);
92         }
93 }
94
95
96 BOOST_AUTO_TEST_CASE (hint_closed_caption_too_long)
97 {
98         check (
99                 TextType::CLOSED_CAPTION,
100                 "hint_closed_caption_too_long",
101                 String::compose("At least one of your closed caption lines has more than %1 characters.  It is advisable to make each line %1 characters at most in length.", MAX_CLOSED_CAPTION_LENGTH, MAX_CLOSED_CAPTION_LENGTH)
102               );
103 }
104
105
106 BOOST_AUTO_TEST_CASE (hint_many_closed_caption_lines)
107 {
108         check (
109                 TextType::CLOSED_CAPTION,
110                 "hint_many_closed_caption_lines",
111                 String::compose("Some of your closed captions span more than %1 lines, so they will be truncated.", MAX_CLOSED_CAPTION_LINES)
112               );
113 }
114
115
116 BOOST_AUTO_TEST_CASE (hint_subtitle_too_early)
117 {
118         check (
119                 TextType::OPEN_SUBTITLE,
120                 "hint_subtitle_too_early",
121                 string("It is advisable to put your first subtitle at least 4 seconds after the start of the DCP to make sure it is seen.")
122                 );
123 }
124
125
126 BOOST_AUTO_TEST_CASE (hint_short_subtitles)
127 {
128         check (
129                 TextType::OPEN_SUBTITLE,
130                 "hint_short_subtitles",
131                 string("At least one of your subtitles lasts less than 15 frames.  It is advisable to make each subtitle at least 15 frames long.")
132                 );
133 }
134
135
136 BOOST_AUTO_TEST_CASE (hint_subtitles_too_close)
137 {
138         check (
139                 TextType::OPEN_SUBTITLE,
140                 "hint_subtitles_too_close",
141                 string("At least one of your subtitles starts less than 2 frames after the previous one.  It is advisable to make the gap between subtitles at least 2 frames.")
142               );
143 }
144
145
146 BOOST_AUTO_TEST_CASE (hint_many_subtitle_lines)
147 {
148         check (
149                 TextType::OPEN_SUBTITLE,
150                 "hint_many_subtitle_lines",
151                 string("At least one of your subtitles has more than 3 lines.  It is advisable to use no more than 3 lines.")
152               );
153 }
154
155
156 BOOST_AUTO_TEST_CASE(hint_many_subtitle_lines2)
157 {
158         check(TextType::OPEN_SUBTITLE, "hint_many_subtitle_lines2");
159 }
160
161
162 BOOST_AUTO_TEST_CASE (hint_subtitle_too_long)
163 {
164         check (
165                 TextType::OPEN_SUBTITLE,
166                 "hint_subtitle_too_long",
167                 string("At least one of your subtitle lines has more than 52 characters.  It is recommended to make each line 52 characters at most in length.")
168               );
169 }
170
171
172 BOOST_AUTO_TEST_CASE (hint_subtitle_much_too_long)
173 {
174         check (
175                 TextType::OPEN_SUBTITLE,
176                 "hint_subtitle_much_too_long",
177                 string("At least one of your subtitle lines has more than 79 characters.  You should make each line 79 characters at most in length.")
178               );
179 }
180
181
182 BOOST_AUTO_TEST_CASE (hint_subtitle_mxf_too_big)
183 {
184         string const name = "hint_subtitle_mxf_too_big";
185
186         auto film = new_test_film2 (name);
187
188         for (int i = 0; i < 4; ++i) {
189                 dcp::File fake_font("build/test/hint_subtitle_mxf_too_big.ttf", "w");
190                 for (int i = 0; i < 512; ++i) {
191                         std::vector<uint8_t> rubbish(65536);
192                         fake_font.write(rubbish.data(), 1, rubbish.size());
193                 }
194                 fake_font.close();
195
196                 auto content = content_factory(String::compose("test/data/%1%2.xml", name, i))[0];
197                 content->text[0]->set_type(TextType::OPEN_SUBTITLE);
198                 content->text[0]->set_language(dcp::LanguageTag("en-US"));
199                 film->examine_and_add_content(content);
200                 BOOST_REQUIRE (!wait_for_jobs());
201                 auto const font = content->text[0]->get_font(String::compose("0_font_%1", i));
202                 BOOST_REQUIRE(font);
203                 font->set_file("build/test/hint_subtitle_mxf_too_big.ttf");
204         }
205
206         auto hints = get_hints (film);
207
208         BOOST_REQUIRE_EQUAL (hints.size(), 1U);
209         BOOST_CHECK_EQUAL (
210                 hints[0],
211                 "At least one of your subtitle files is larger than " MAX_TEXT_MXF_SIZE_TEXT " in total.  "
212                 "You should divide the DCP into shorter reels."
213                 );
214 }
215
216
217 BOOST_AUTO_TEST_CASE (hint_closed_caption_xml_too_big)
218 {
219         string const name = "hint_closed_caption_xml_too_big";
220
221         auto film = new_test_film2 (name);
222
223         dcp::File ccap(String::compose("build/test/%1.srt", name), "w");
224         BOOST_REQUIRE (ccap);
225         for (int i = 0; i < 2048; ++i) {
226                 fprintf(ccap.get(), "%d\n", i + 1);
227                 int second = i * 2;
228                 int minute = second % 60;
229                 fprintf(ccap.get(), "00:%02d:%02d,000 --> 00:%02d:%02d,000\n", minute, second, minute, second + 1);
230                 fprintf(ccap.get(), "Here are some closed captions.\n\n");
231         }
232         ccap.close();
233
234         auto content = content_factory("build/test/" + name + ".srt")[0];
235         content->text.front()->set_type (TextType::CLOSED_CAPTION);
236         content->text.front()->set_language (dcp::LanguageTag("en-US"));
237         film->examine_and_add_content (content);
238         BOOST_REQUIRE (!wait_for_jobs());
239         auto hints = get_hints (film);
240
241         BOOST_REQUIRE_EQUAL (hints.size(), 1U);
242         BOOST_CHECK_EQUAL (
243                 hints[0],
244                 "At least one of your closed caption files' XML part is larger than " MAX_CLOSED_CAPTION_XML_SIZE_TEXT ".  "
245                 "You should divide the DCP into shorter reels."
246                 );
247 }
248
249
250 BOOST_AUTO_TEST_CASE (hints_destroyed_while_running)
251 {
252         auto film = new_test_film2 ("hints_destroyed_while_running");
253         auto content = content_factory(TestPaths::private_data() / "boon_telly.mkv")[0];
254         film->examine_and_add_content (content);
255         BOOST_REQUIRE (!wait_for_jobs());
256
257         auto hints = make_shared<Hints>(film);
258         hints->start ();
259         dcpomatic_sleep_seconds (1);
260         hints.reset ();
261         dcpomatic_sleep_seconds (1);
262 }
263
264
265 BOOST_AUTO_TEST_CASE (hints_audio_with_no_language)
266 {
267         auto content = content_factory("test/data/sine_440.wav")[0];
268         auto film = new_test_film2 ("hints_audio_with_no_language", { content });
269         content->audio->set_gain (-6);
270
271         auto hints = get_hints (film);
272         BOOST_REQUIRE_EQUAL (hints.size(), 1U);
273         BOOST_CHECK_EQUAL (
274                 hints[0],
275                 "Some of your content has audio but you have not set the audio language.  It is advisable to set the audio language "
276                 "in the \"DCP\" tab unless your audio has no spoken parts."
277                 );
278 }
279
280
281 BOOST_AUTO_TEST_CASE (hints_certificate_validity)
282 {
283         ConfigRestorer cr;
284
285         Config::instance()->set_signer_chain(make_shared<dcp::CertificateChain>(openssl_path(), 40 * 365));
286
287         auto film = new_test_film2 ("hints_certificate_validity");
288         auto hints = get_hints (film);
289         BOOST_REQUIRE_EQUAL (hints.size(), 1U);
290         BOOST_CHECK_EQUAL (
291                 hints[0],
292                 "The certificate chain that DCP-o-matic uses for signing DCPs and KDMs has a validity period "
293                 "that is too long.  This will cause problems playing back DCPs on some systems. "
294                 "It is advisable to re-create the signing certificate chain by clicking the "
295                 "\"Re-make certificates and key...\" button in the Keys page of Preferences."
296                 );
297 }
298