2 Copyright (C) 2014-2021 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 /** @file test/player_test.cc
23 * @brief Test Player class.
24 * @ingroup selfcontained
28 #include "lib/audio_buffers.h"
29 #include "lib/butler.h"
30 #include "lib/compose.hpp"
31 #include "lib/config.h"
32 #include "lib/content_factory.h"
33 #include "lib/cross.h"
34 #include "lib/dcp_content.h"
35 #include "lib/dcp_content_type.h"
36 #include "lib/dcpomatic_log.h"
37 #include "lib/ffmpeg_content.h"
39 #include "lib/image_content.h"
40 #include "lib/player.h"
41 #include "lib/ratio.h"
42 #include "lib/string_text_file_content.h"
43 #include "lib/text_content.h"
44 #include "lib/video_content.h"
46 #include <boost/test/unit_test.hpp>
47 #include <boost/algorithm/string.hpp>
53 using std::shared_ptr;
54 using std::make_shared;
56 using boost::optional;
57 #if BOOST_VERSION >= 106100
58 using namespace boost::placeholders;
60 using namespace dcpomatic;
63 static shared_ptr<AudioBuffers> accumulated;
67 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
69 BOOST_REQUIRE (accumulated);
70 accumulated->append (audio);
74 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
75 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
77 auto film = new_test_film ("player_silence_padding_test");
78 film->set_name ("player_silence_padding_test");
79 auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
80 film->set_container (Ratio::from_id ("185"));
81 film->set_audio_channels (6);
83 film->examine_and_add_content (c);
84 BOOST_REQUIRE (!wait_for_jobs());
86 accumulated = std::make_shared<AudioBuffers>(film->audio_channels(), 0);
88 Player player(film, Image::Alignment::COMPACT);
89 player.Audio.connect(bind(&accumulate, _1, _2));
90 while (!player.pass()) {}
91 BOOST_REQUIRE (accumulated->frames() >= 48000);
92 BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
94 for (int i = 0; i < 48000; ++i) {
95 for (int c = 0; c < accumulated->channels(); ++c) {
96 BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
102 /* Test insertion of black frames between separate bits of video content */
103 BOOST_AUTO_TEST_CASE (player_black_fill_test)
105 auto film = new_test_film ("black_fill_test");
106 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
107 film->set_name ("black_fill_test");
108 film->set_container (Ratio::from_id ("185"));
109 film->set_sequence (false);
110 film->set_interop (false);
111 auto contentA = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
112 auto contentB = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
114 film->examine_and_add_content (contentA);
115 film->examine_and_add_content (contentB);
116 BOOST_REQUIRE (!wait_for_jobs());
118 contentA->video->set_length (3);
119 contentA->set_position (film, DCPTime::from_frames(2, film->video_frame_rate()));
120 contentA->video->set_custom_ratio (1.85);
121 contentB->video->set_length (1);
122 contentB->set_position (film, DCPTime::from_frames(7, film->video_frame_rate()));
123 contentB->video->set_custom_ratio (1.85);
125 make_and_verify_dcp (
128 dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
129 dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
132 boost::filesystem::path ref;
135 ref /= "black_fill_test";
137 boost::filesystem::path check;
140 check /= "black_fill_test";
141 check /= film->dcp_name();
143 check_dcp (ref.string(), check.string());
147 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
148 BOOST_AUTO_TEST_CASE (player_subframe_test)
150 auto film = new_test_film ("reels_test7");
151 film->set_name ("reels_test7");
152 film->set_container (Ratio::from_id("185"));
153 film->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
154 auto A = content_factory("test/data/flat_red.png")[0];
155 film->examine_and_add_content (A);
156 BOOST_REQUIRE (!wait_for_jobs());
157 auto B = content_factory("test/data/awkward_length.wav")[0];
158 film->examine_and_add_content (B);
159 BOOST_REQUIRE (!wait_for_jobs());
160 film->set_video_frame_rate (24);
161 A->video->set_length (3 * 24);
163 BOOST_CHECK (A->full_length(film) == DCPTime::from_frames(3 * 24, 24));
164 BOOST_CHECK (B->full_length(film) == DCPTime(289920));
165 /* Length should be rounded up from B's length to the next video frame */
166 BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
168 Player player(film, Image::Alignment::COMPACT);
169 player.setup_pieces();
170 BOOST_REQUIRE_EQUAL(player._black._periods.size(), 1U);
171 BOOST_CHECK(player._black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
172 BOOST_REQUIRE_EQUAL(player._silent._periods.size(), 1U);
173 BOOST_CHECK(player._silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
177 static Frame video_frames;
178 static Frame audio_frames;
182 video (shared_ptr<PlayerVideo>, DCPTime)
188 audio (shared_ptr<AudioBuffers> audio, DCPTime)
190 audio_frames += audio->frames();
194 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
195 BOOST_AUTO_TEST_CASE (player_interleave_test)
197 auto film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
198 film->set_name ("ffmpeg_transcoder_basic_test");
199 film->set_container (Ratio::from_id ("185"));
200 film->set_audio_channels (6);
202 auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
203 film->examine_and_add_content (c);
204 BOOST_REQUIRE (!wait_for_jobs ());
206 auto s = std::make_shared<StringTextFileContent>("test/data/subrip.srt");
207 film->examine_and_add_content (s);
208 BOOST_REQUIRE (!wait_for_jobs ());
210 Player player(film, Image::Alignment::COMPACT);
211 player.Video.connect(bind(&video, _1, _2));
212 player.Audio.connect(bind(&audio, _1, _2));
213 video_frames = audio_frames = 0;
214 while (!player.pass()) {
215 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) <= 8);
220 /** Test some seeks towards the start of a DCP with awkward subtitles; see mantis #1085
221 * and a number of others. I thought this was a player seek bug but in fact it was
222 * caused by the subtitle starting just after the start of the video frame and hence
225 BOOST_AUTO_TEST_CASE (player_seek_test)
227 auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
228 auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs");
229 film->examine_and_add_content (dcp, true);
230 BOOST_REQUIRE (!wait_for_jobs ());
231 dcp->only_text()->set_use (true);
233 Player player(film, Image::Alignment::COMPACT);
235 player.set_always_burn_open_subtitles();
236 player.set_play_referenced();
238 auto butler = std::make_shared<Butler>(
239 film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
242 for (int i = 0; i < 10; ++i) {
243 auto t = DCPTime::from_frames (i, 24);
244 butler->seek (t, true);
245 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
246 BOOST_CHECK_EQUAL(video.second.get(), t.get());
247 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));
248 /* This 14.08 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
249 (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
252 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);
257 /** Test some more seeks towards the start of a DCP with awkward subtitles */
258 BOOST_AUTO_TEST_CASE (player_seek_test2)
260 auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
261 auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs2");
262 film->examine_and_add_content (dcp, true);
263 BOOST_REQUIRE (!wait_for_jobs ());
264 dcp->only_text()->set_use (true);
266 Player player(film, Image::Alignment::COMPACT);
268 player.set_always_burn_open_subtitles();
269 player.set_play_referenced();
271 auto butler = std::make_shared<Butler>
272 (film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
275 butler->seek(DCPTime::from_seconds(5), true);
277 for (int i = 0; i < 10; ++i) {
278 auto t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
279 butler->seek (t, true);
280 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
281 BOOST_CHECK_EQUAL(video.second.get(), t.get());
283 video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test2_%1.png", i)
285 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);
290 /** Test a bug when trimmed content follows other content */
291 BOOST_AUTO_TEST_CASE (player_trim_test)
293 auto film = new_test_film2 ("player_trim_test");
294 auto A = content_factory("test/data/flat_red.png")[0];
295 film->examine_and_add_content (A);
296 BOOST_REQUIRE (!wait_for_jobs ());
297 A->video->set_length (10 * 24);
298 auto B = content_factory("test/data/flat_red.png")[0];
299 film->examine_and_add_content (B);
300 BOOST_REQUIRE (!wait_for_jobs ());
301 B->video->set_length (10 * 24);
302 B->set_position (film, DCPTime::from_seconds(10));
303 B->set_trim_start (ContentTime::from_seconds (2));
305 make_and_verify_dcp (film);
312 optional<DCPTextTrack> track;
313 DCPTimePeriod period;
318 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
329 /** Test ignoring both video and audio */
330 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
332 auto film = new_test_film2 ("player_ignore_video_and_audio_test");
333 auto ff = content_factory(TestPaths::private_data() / "boon_telly.mkv")[0];
334 film->examine_and_add_content (ff);
335 auto text = content_factory("test/data/subrip.srt")[0];
336 film->examine_and_add_content (text);
337 BOOST_REQUIRE (!wait_for_jobs());
338 text->only_text()->set_type (TextType::CLOSED_CAPTION);
339 text->only_text()->set_use (true);
341 Player player(film, Image::Alignment::COMPACT);
342 player.set_ignore_video();
343 player.set_ignore_audio();
346 player.Text.connect(bind (&store, &out, _1, _2, _3, _4));
347 while (!player.pass()) {}
349 BOOST_CHECK_EQUAL (out.size(), 6U);
353 /** Trigger a crash due to the assertion failure in Player::emit_audio */
354 BOOST_AUTO_TEST_CASE (player_trim_crash)
356 auto film = new_test_film2 ("player_trim_crash");
357 auto boon = content_factory(TestPaths::private_data() / "boon_telly.mkv")[0];
358 film->examine_and_add_content (boon);
359 BOOST_REQUIRE (!wait_for_jobs());
361 Player player(film, Image::Alignment::COMPACT);
363 auto butler = std::make_shared<Butler>(
364 film, player, AudioMapping(), 6, bind(&PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::COMPACT, true, false, Butler::Audio::ENABLED
367 /* Wait for the butler to fill */
368 dcpomatic_sleep_seconds (5);
370 boon->set_trim_start (ContentTime::from_seconds(5));
372 butler->seek (DCPTime(), true);
374 /* Wait for the butler to refill */
375 dcpomatic_sleep_seconds (5);
381 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
382 BOOST_AUTO_TEST_CASE (player_silence_crash)
384 auto film = new_test_film2 ("player_silence_crash");
385 auto sine = content_factory("test/data/impulse_train.wav")[0];
386 film->examine_and_add_content (sine);
387 BOOST_REQUIRE (!wait_for_jobs());
389 sine->set_video_frame_rate (23.976);
390 film->write_metadata ();
391 make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
395 /** Test a crash when processing a 3D DCP */
396 BOOST_AUTO_TEST_CASE (player_3d_test_1)
398 auto film = new_test_film2 ("player_3d_test_1a");
399 auto left = content_factory("test/data/flat_red.png")[0];
400 film->examine_and_add_content (left);
401 auto right = content_factory("test/data/flat_blue.png")[0];
402 film->examine_and_add_content (right);
403 BOOST_REQUIRE (!wait_for_jobs());
405 left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
406 left->set_position (film, DCPTime());
407 right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
408 right->set_position (film, DCPTime());
409 film->set_three_d (true);
411 make_and_verify_dcp (film);
413 auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
414 auto film2 = new_test_film2 ("player_3d_test_1b", {dcp});
416 film2->set_three_d (true);
417 make_and_verify_dcp (film2);
421 /** Test a crash when processing a 3D DCP as content in a 2D project */
422 BOOST_AUTO_TEST_CASE (player_3d_test_2)
424 auto left = content_factory("test/data/flat_red.png")[0];
425 auto right = content_factory("test/data/flat_blue.png")[0];
426 auto film = new_test_film2 ("player_3d_test_2a", {left, right});
428 left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
429 left->set_position (film, DCPTime());
430 right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
431 right->set_position (film, DCPTime());
432 film->set_three_d (true);
434 make_and_verify_dcp (film);
436 auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
437 auto film2 = new_test_film2 ("player_3d_test_2b", {dcp});
439 make_and_verify_dcp (film2);
443 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
446 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
448 /* 25fps DCP with some audio */
449 auto content1 = content_factory("test/data/flat_red.png")[0];
450 auto film1 = new_test_film2 ("player_silence_at_end_crash_1", {content1});
451 content1->video->set_length (25);
452 film1->set_video_frame_rate (25);
453 make_and_verify_dcp (film1);
455 /* Make another project importing this DCP */
456 auto content2 = std::make_shared<DCPContent>(film1->dir(film1->dcp_name()));
457 auto film2 = new_test_film2 ("player_silence_at_end_crash_2", {content2});
459 /* and importing just the video MXF on its own at the end */
460 optional<boost::filesystem::path> video;
461 for (auto i: boost::filesystem::directory_iterator(film1->dir(film1->dcp_name()))) {
462 if (boost::starts_with(i.path().filename().string(), "j2c_")) {
467 BOOST_REQUIRE (video);
468 auto content3 = content_factory(*video)[0];
469 film2->examine_and_add_content (content3);
470 BOOST_REQUIRE (!wait_for_jobs());
471 content3->set_position (film2, DCPTime::from_seconds(1.5));
472 film2->set_video_frame_rate (24);
473 make_and_verify_dcp (film2);
478 BOOST_AUTO_TEST_CASE (encrypted_dcp_with_no_kdm_gives_no_butler_error)
480 auto content = content_factory("test/data/flat_red.png")[0];
481 auto film = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error", { content });
482 int constexpr length = 24 * 25;
483 content->video->set_length(length);
484 film->set_encrypted (true);
485 make_and_verify_dcp (
488 dcp::VerificationNote::Code::MISSING_CPL_METADATA,
491 auto content2 = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
492 auto film2 = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error2", { content2 });
494 Player player(film, Image::Alignment::COMPACT);
495 Butler butler(film2, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::ENABLED);
497 float buffer[2000 * 6];
498 for (int i = 0; i < length; ++i) {
499 butler.get_video(Butler::Behaviour::BLOCKING, 0);
500 butler.get_audio(Butler::Behaviour::BLOCKING, buffer, 2000);
503 BOOST_CHECK_NO_THROW(butler.rethrow());
507 BOOST_AUTO_TEST_CASE (interleaved_subtitle_are_emitted_correctly)
509 boost::filesystem::path paths[2] = {
510 "build/test/interleaved_subtitle_are_emitted_correctly1.srt",
511 "build/test/interleaved_subtitle_are_emitted_correctly2.srt"
514 dcp::File subs_file[2] = { dcp::File(paths[0], "w"), dcp::File(paths[1], "w") };
516 fprintf(subs_file[0].get(), "1\n00:00:01,000 -> 00:00:02,000\nSub 1/1\n\n");
517 fprintf(subs_file[0].get(), "2\n00:00:05,000 -> 00:00:06,000\nSub 1/2\n\n");
519 fprintf(subs_file[1].get(), "1\n00:00:00,500 -> 00:00:01,500\nSub 2/1\n\n");
520 fprintf(subs_file[1].get(), "2\n00:00:02,000 -> 00:00:03,000\nSub 2/2\n\n");
522 subs_file[0].close();
523 subs_file[1].close();
525 auto subs1 = content_factory(paths[0])[0];
526 auto subs2 = content_factory(paths[1])[0];
527 auto film = new_test_film2("interleaved_subtitle_are_emitted_correctly", { subs1, subs2 });
528 film->set_sequence(false);
529 subs1->set_position(film, DCPTime());
530 subs2->set_position(film, DCPTime());
532 Player player(film, Image::Alignment::COMPACT);
534 player.Text.connect([&last](PlayerText text, TextType, optional<DCPTextTrack>, dcpomatic::DCPTimePeriod) {
535 for (auto sub: text.string) {
536 BOOST_CHECK(sub.in() >= last);
540 while (!player.pass()) {}
544 BOOST_AUTO_TEST_CASE(multiple_sound_files_bug)
546 Config::instance()->set_log_types(Config::instance()->log_types() | LogEntry::TYPE_DEBUG_PLAYER);
548 auto A = content_factory(TestPaths::private_data() / "kook" / "1.wav").front();
549 auto B = content_factory(TestPaths::private_data() / "kook" / "2.wav").front();
550 auto C = content_factory(TestPaths::private_data() / "kook" / "3.wav").front();
552 auto film = new_test_film2("multiple_sound_files_bug", { A, B, C });
553 C->set_position(film, DCPTime(3840000));
555 make_and_verify_dcp(film, { dcp::VerificationNote::Code::MISSING_CPL_METADATA });
557 check_mxf_audio_file(TestPaths::private_data() / "kook" / "reference.mxf", dcp_file(film, "pcm_"));