Fix incorrect extension on interop subtitle files (#2270).
[dcpomatic.git] / test / player_test.cc
1 /*
2     Copyright (C) 2014-2021 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 /** @file  test/player_test.cc
23  *  @brief Test Player class.
24  *  @ingroup selfcontained
25  */
26
27
28 #include "lib/audio_buffers.h"
29 #include "lib/butler.h"
30 #include "lib/compose.hpp"
31 #include "lib/content_factory.h"
32 #include "lib/cross.h"
33 #include "lib/dcp_content.h"
34 #include "lib/dcp_content_type.h"
35 #include "lib/ffmpeg_content.h"
36 #include "lib/film.h"
37 #include "lib/image_content.h"
38 #include "lib/player.h"
39 #include "lib/ratio.h"
40 #include "lib/string_text_file_content.h"
41 #include "lib/text_content.h"
42 #include "lib/video_content.h"
43 #include "test.h"
44 #include <boost/test/unit_test.hpp>
45 #include <boost/algorithm/string.hpp>
46 #include <iostream>
47
48
49 using std::cout;
50 using std::list;
51 using std::shared_ptr;
52 using std::make_shared;
53 using boost::bind;
54 using boost::optional;
55 #if BOOST_VERSION >= 106100
56 using namespace boost::placeholders;
57 #endif
58 using namespace dcpomatic;
59
60
61 static shared_ptr<AudioBuffers> accumulated;
62
63
64 static void
65 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
66 {
67         BOOST_REQUIRE (accumulated);
68         accumulated->append (audio);
69 }
70
71
72 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
73 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
74 {
75         auto film = new_test_film ("player_silence_padding_test");
76         film->set_name ("player_silence_padding_test");
77         auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
78         film->set_container (Ratio::from_id ("185"));
79         film->set_audio_channels (6);
80
81         film->examine_and_add_content (c);
82         BOOST_REQUIRE (!wait_for_jobs());
83
84         accumulated = std::make_shared<AudioBuffers>(film->audio_channels(), 0);
85
86         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
87         player->Audio.connect (bind (&accumulate, _1, _2));
88         while (!player->pass ()) {}
89         BOOST_REQUIRE (accumulated->frames() >= 48000);
90         BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
91
92         for (int i = 0; i < 48000; ++i) {
93                 for (int c = 0; c < accumulated->channels(); ++c) {
94                         BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
95                 }
96         }
97 }
98
99
100 /* Test insertion of black frames between separate bits of video content */
101 BOOST_AUTO_TEST_CASE (player_black_fill_test)
102 {
103         auto film = new_test_film ("black_fill_test");
104         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
105         film->set_name ("black_fill_test");
106         film->set_container (Ratio::from_id ("185"));
107         film->set_sequence (false);
108         film->set_interop (false);
109         auto contentA = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
110         auto contentB = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
111
112         film->examine_and_add_content (contentA);
113         film->examine_and_add_content (contentB);
114         BOOST_REQUIRE (!wait_for_jobs());
115
116         contentA->video->set_length (3);
117         contentA->set_position (film, DCPTime::from_frames(2, film->video_frame_rate()));
118         contentA->video->set_custom_ratio (1.85);
119         contentB->video->set_length (1);
120         contentB->set_position (film, DCPTime::from_frames(7, film->video_frame_rate()));
121         contentB->video->set_custom_ratio (1.85);
122
123         make_and_verify_dcp (
124                 film,
125                 {
126                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
127                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
128                 });
129
130         boost::filesystem::path ref;
131         ref = "test";
132         ref /= "data";
133         ref /= "black_fill_test";
134
135         boost::filesystem::path check;
136         check = "build";
137         check /= "test";
138         check /= "black_fill_test";
139         check /= film->dcp_name();
140
141         check_dcp (ref.string(), check.string());
142 }
143
144
145 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
146 BOOST_AUTO_TEST_CASE (player_subframe_test)
147 {
148         auto film = new_test_film ("reels_test7");
149         film->set_name ("reels_test7");
150         film->set_container (Ratio::from_id("185"));
151         film->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
152         auto A = content_factory("test/data/flat_red.png").front();
153         film->examine_and_add_content (A);
154         BOOST_REQUIRE (!wait_for_jobs());
155         auto B = content_factory("test/data/awkward_length.wav").front();
156         film->examine_and_add_content (B);
157         BOOST_REQUIRE (!wait_for_jobs());
158         film->set_video_frame_rate (24);
159         A->video->set_length (3 * 24);
160
161         BOOST_CHECK (A->full_length(film) == DCPTime::from_frames(3 * 24, 24));
162         BOOST_CHECK (B->full_length(film) == DCPTime(289920));
163         /* Length should be rounded up from B's length to the next video frame */
164         BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
165
166         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
167         player->setup_pieces ();
168         BOOST_REQUIRE_EQUAL (player->_black._periods.size(), 1U);
169         BOOST_CHECK (player->_black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
170         BOOST_REQUIRE_EQUAL (player->_silent._periods.size(), 1U);
171         BOOST_CHECK (player->_silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
172 }
173
174
175 static Frame video_frames;
176 static Frame audio_frames;
177
178
179 static void
180 video (shared_ptr<PlayerVideo>, DCPTime)
181 {
182         ++video_frames;
183 }
184
185 static void
186 audio (shared_ptr<AudioBuffers> audio, DCPTime)
187 {
188         audio_frames += audio->frames();
189 }
190
191
192 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
193 BOOST_AUTO_TEST_CASE (player_interleave_test)
194 {
195         auto film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
196         film->set_name ("ffmpeg_transcoder_basic_test");
197         film->set_container (Ratio::from_id ("185"));
198         film->set_audio_channels (6);
199
200         auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
201         film->examine_and_add_content (c);
202         BOOST_REQUIRE (!wait_for_jobs ());
203
204         auto s = std::make_shared<StringTextFileContent>("test/data/subrip.srt");
205         film->examine_and_add_content (s);
206         BOOST_REQUIRE (!wait_for_jobs ());
207
208         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
209         player->Video.connect (bind (&video, _1, _2));
210         player->Audio.connect (bind (&audio, _1, _2));
211         video_frames = audio_frames = 0;
212         while (!player->pass ()) {
213                 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) <= 8);
214         }
215 }
216
217
218 /** Test some seeks towards the start of a DCP with awkward subtitles; see mantis #1085
219  *  and a number of others.  I thought this was a player seek bug but in fact it was
220  *  caused by the subtitle starting just after the start of the video frame and hence
221  *  being faded out.
222  */
223 BOOST_AUTO_TEST_CASE (player_seek_test)
224 {
225         auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
226         auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs");
227         film->examine_and_add_content (dcp, true);
228         BOOST_REQUIRE (!wait_for_jobs ());
229         dcp->only_text()->set_use (true);
230
231         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
232         player->set_fast ();
233         player->set_always_burn_open_subtitles ();
234         player->set_play_referenced ();
235
236         auto butler = std::make_shared<Butler>(
237                 film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
238                 );
239
240         for (int i = 0; i < 10; ++i) {
241                 auto t = DCPTime::from_frames (i, 24);
242                 butler->seek (t, true);
243                 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
244                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
245                 write_image(video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test_%1.png", i));
246                 /* This 14.08 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
247                    (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
248                    text altogether.
249                 */
250                 check_image(TestPaths::private_data() / String::compose("player_seek_test_%1.png", i), String::compose("build/test/player_seek_test_%1.png", i), 14.08);
251         }
252 }
253
254
255 /** Test some more seeks towards the start of a DCP with awkward subtitles */
256 BOOST_AUTO_TEST_CASE (player_seek_test2)
257 {
258         auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
259         auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs2");
260         film->examine_and_add_content (dcp, true);
261         BOOST_REQUIRE (!wait_for_jobs ());
262         dcp->only_text()->set_use (true);
263
264         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
265         player->set_fast ();
266         player->set_always_burn_open_subtitles ();
267         player->set_play_referenced ();
268
269         auto butler = std::make_shared<Butler>
270                 (film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
271                  );
272
273         butler->seek(DCPTime::from_seconds(5), true);
274
275         for (int i = 0; i < 10; ++i) {
276                 auto t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
277                 butler->seek (t, true);
278                 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
279                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
280                 write_image(
281                         video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test2_%1.png", i)
282                         );
283                 check_image(TestPaths::private_data() / String::compose("player_seek_test2_%1.png", i), String::compose("build/test/player_seek_test2_%1.png", i), 14.08);
284         }
285 }
286
287
288 /** Test a bug when trimmed content follows other content */
289 BOOST_AUTO_TEST_CASE (player_trim_test)
290 {
291        auto film = new_test_film2 ("player_trim_test");
292        auto A = content_factory("test/data/flat_red.png").front();
293        film->examine_and_add_content (A);
294        BOOST_REQUIRE (!wait_for_jobs ());
295        A->video->set_length (10 * 24);
296        auto B = content_factory("test/data/flat_red.png").front();
297        film->examine_and_add_content (B);
298        BOOST_REQUIRE (!wait_for_jobs ());
299        B->video->set_length (10 * 24);
300        B->set_position (film, DCPTime::from_seconds(10));
301        B->set_trim_start (ContentTime::from_seconds (2));
302
303        make_and_verify_dcp (film);
304 }
305
306
307 struct Sub {
308         PlayerText text;
309         TextType type;
310         optional<DCPTextTrack> track;
311         DCPTimePeriod period;
312 };
313
314
315 static void
316 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
317 {
318         Sub s;
319         s.text = text;
320         s.type = type;
321         s.track = track;
322         s.period = period;
323         out->push_back (s);
324 }
325
326
327 /** Test ignoring both video and audio */
328 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
329 {
330         auto film = new_test_film2 ("player_ignore_video_and_audio_test");
331         auto ff = content_factory(TestPaths::private_data() / "boon_telly.mkv").front();
332         film->examine_and_add_content (ff);
333         auto text = content_factory("test/data/subrip.srt").front();
334         film->examine_and_add_content (text);
335         BOOST_REQUIRE (!wait_for_jobs());
336         text->only_text()->set_type (TextType::CLOSED_CAPTION);
337         text->only_text()->set_use (true);
338
339         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
340         player->set_ignore_video ();
341         player->set_ignore_audio ();
342
343         list<Sub> out;
344         player->Text.connect (bind (&store, &out, _1, _2, _3, _4));
345         while (!player->pass ()) {}
346
347         BOOST_CHECK_EQUAL (out.size(), 6U);
348 }
349
350
351 /** Trigger a crash due to the assertion failure in Player::emit_audio */
352 BOOST_AUTO_TEST_CASE (player_trim_crash)
353 {
354         auto film = new_test_film2 ("player_trim_crash");
355         auto boon = content_factory(TestPaths::private_data() / "boon_telly.mkv").front();
356         film->examine_and_add_content (boon);
357         BOOST_REQUIRE (!wait_for_jobs());
358
359         auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
360         player->set_fast ();
361         auto butler = std::make_shared<Butler>(
362                 film, player, AudioMapping(), 6, bind(&PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::COMPACT, true, false, Butler::Audio::ENABLED
363                 );
364
365         /* Wait for the butler to fill */
366         dcpomatic_sleep_seconds (5);
367
368         boon->set_trim_start (ContentTime::from_seconds(5));
369
370         butler->seek (DCPTime(), true);
371
372         /* Wait for the butler to refill */
373         dcpomatic_sleep_seconds (5);
374
375         butler->rethrow ();
376 }
377
378
379 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
380 BOOST_AUTO_TEST_CASE (player_silence_crash)
381 {
382         auto film = new_test_film2 ("player_silence_crash");
383         auto sine = content_factory("test/data/impulse_train.wav").front();
384         film->examine_and_add_content (sine);
385         BOOST_REQUIRE (!wait_for_jobs());
386
387         sine->set_video_frame_rate (23.976);
388         film->write_metadata ();
389         make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
390 }
391
392
393 /** Test a crash when processing a 3D DCP */
394 BOOST_AUTO_TEST_CASE (player_3d_test_1)
395 {
396         auto film = new_test_film2 ("player_3d_test_1a");
397         auto left = content_factory("test/data/flat_red.png").front();
398         film->examine_and_add_content (left);
399         auto right = content_factory("test/data/flat_blue.png").front();
400         film->examine_and_add_content (right);
401         BOOST_REQUIRE (!wait_for_jobs());
402
403         left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
404         left->set_position (film, DCPTime());
405         right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
406         right->set_position (film, DCPTime());
407         film->set_three_d (true);
408
409         make_and_verify_dcp (film);
410
411         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
412         auto film2 = new_test_film2 ("player_3d_test_1b", {dcp});
413
414         film2->set_three_d (true);
415         make_and_verify_dcp (film2);
416 }
417
418
419 /** Test a crash when processing a 3D DCP as content in a 2D project */
420 BOOST_AUTO_TEST_CASE (player_3d_test_2)
421 {
422         auto left = content_factory("test/data/flat_red.png").front();
423         auto right = content_factory("test/data/flat_blue.png").front();
424         auto film = new_test_film2 ("player_3d_test_2a", {left, right});
425
426         left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
427         left->set_position (film, DCPTime());
428         right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
429         right->set_position (film, DCPTime());
430         film->set_three_d (true);
431
432         make_and_verify_dcp (film);
433
434         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
435         auto film2 = new_test_film2 ("player_3d_test_2b", {dcp});
436
437         make_and_verify_dcp (film2);
438 }
439
440
441 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
442  *  #1691.
443  */
444 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
445 {
446         /* 25fps DCP with some audio */
447         auto content1 = content_factory("test/data/flat_red.png").front();
448         auto film1 = new_test_film2 ("player_silence_at_end_crash_1", {content1});
449         content1->video->set_length (25);
450         film1->set_video_frame_rate (25);
451         make_and_verify_dcp (film1);
452
453         /* Make another project importing this DCP */
454         auto content2 = std::make_shared<DCPContent>(film1->dir(film1->dcp_name()));
455         auto film2 = new_test_film2 ("player_silence_at_end_crash_2", {content2});
456
457         /* and importing just the video MXF on its own at the end */
458         optional<boost::filesystem::path> video;
459         for (auto i: boost::filesystem::directory_iterator(film1->dir(film1->dcp_name()))) {
460                 if (boost::starts_with(i.path().filename().string(), "j2c_")) {
461                         video = i.path();
462                 }
463         }
464
465         BOOST_REQUIRE (video);
466         auto content3 = content_factory(*video).front();
467         film2->examine_and_add_content (content3);
468         BOOST_REQUIRE (!wait_for_jobs());
469         content3->set_position (film2, DCPTime::from_seconds(1.5));
470         film2->set_video_frame_rate (24);
471         make_and_verify_dcp (film2);
472 }
473
474
475 /** #2257 */
476 BOOST_AUTO_TEST_CASE (encrypted_dcp_with_no_kdm_gives_no_butler_error)
477 {
478         auto content = content_factory("test/data/flat_red.png").front();
479         auto film = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error", { content });
480         int constexpr length = 24 * 25;
481         content->video->set_length(length);
482         film->set_encrypted (true);
483         make_and_verify_dcp (
484                 film,
485                 {
486                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
487                 });
488
489         auto content2 = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
490         auto film2 = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error2", { content2 });
491
492         auto player = std::make_shared<Player>(film2, Image::Alignment::COMPACT);
493         Butler butler(film2, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::ENABLED);
494
495         float buffer[2000 * 6];
496         for (int i = 0; i < length; ++i) {
497                 butler.get_video(Butler::Behaviour::BLOCKING, 0);
498                 butler.get_audio(Butler::Behaviour::BLOCKING, buffer, 2000);
499         }
500
501         BOOST_CHECK_NO_THROW(butler.rethrow());
502 }
503