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/audio_content.h"
30 #include "lib/butler.h"
31 #include "lib/compose.hpp"
32 #include "lib/config.h"
33 #include "lib/content_factory.h"
34 #include "lib/cross.h"
35 #include "lib/dcp_content.h"
36 #include "lib/dcp_content_type.h"
37 #include "lib/dcpomatic_log.h"
38 #include "lib/ffmpeg_content.h"
40 #include "lib/image_content.h"
41 #include "lib/player.h"
42 #include "lib/ratio.h"
43 #include "lib/string_text_file_content.h"
44 #include "lib/text_content.h"
45 #include "lib/video_content.h"
47 #include <boost/test/unit_test.hpp>
48 #include <boost/algorithm/string.hpp>
54 using std::shared_ptr;
55 using std::make_shared;
57 using boost::optional;
58 #if BOOST_VERSION >= 106100
59 using namespace boost::placeholders;
61 using namespace dcpomatic;
64 static shared_ptr<AudioBuffers> accumulated;
68 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
70 BOOST_REQUIRE (accumulated);
71 accumulated->append (audio);
75 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
76 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
78 auto film = new_test_film ("player_silence_padding_test");
79 film->set_name ("player_silence_padding_test");
80 auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
81 film->set_container (Ratio::from_id ("185"));
82 film->set_audio_channels (6);
84 film->examine_and_add_content (c);
85 BOOST_REQUIRE (!wait_for_jobs());
87 accumulated = std::make_shared<AudioBuffers>(film->audio_channels(), 0);
89 Player player(film, Image::Alignment::COMPACT);
90 player.Audio.connect(bind(&accumulate, _1, _2));
91 while (!player.pass()) {}
92 BOOST_REQUIRE (accumulated->frames() >= 48000);
93 BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
95 for (int i = 0; i < 48000; ++i) {
96 for (int c = 0; c < accumulated->channels(); ++c) {
97 BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
103 /* Test insertion of black frames between separate bits of video content */
104 BOOST_AUTO_TEST_CASE (player_black_fill_test)
106 auto film = new_test_film ("black_fill_test");
107 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
108 film->set_name ("black_fill_test");
109 film->set_container (Ratio::from_id ("185"));
110 film->set_sequence (false);
111 film->set_interop (false);
112 auto contentA = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
113 auto contentB = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
115 film->examine_and_add_content (contentA);
116 film->examine_and_add_content (contentB);
117 BOOST_REQUIRE (!wait_for_jobs());
119 contentA->video->set_length (3);
120 contentA->set_position (film, DCPTime::from_frames(2, film->video_frame_rate()));
121 contentA->video->set_custom_ratio (1.85);
122 contentB->video->set_length (1);
123 contentB->set_position (film, DCPTime::from_frames(7, film->video_frame_rate()));
124 contentB->video->set_custom_ratio (1.85);
126 make_and_verify_dcp (
129 dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
130 dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
133 boost::filesystem::path ref;
136 ref /= "black_fill_test";
138 boost::filesystem::path check;
141 check /= "black_fill_test";
142 check /= film->dcp_name();
144 check_dcp (ref.string(), check.string());
148 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
149 BOOST_AUTO_TEST_CASE (player_subframe_test)
151 auto film = new_test_film ("reels_test7");
152 film->set_name ("reels_test7");
153 film->set_container (Ratio::from_id("185"));
154 film->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
155 auto A = content_factory("test/data/flat_red.png")[0];
156 film->examine_and_add_content (A);
157 BOOST_REQUIRE (!wait_for_jobs());
158 auto B = content_factory("test/data/awkward_length.wav")[0];
159 film->examine_and_add_content (B);
160 BOOST_REQUIRE (!wait_for_jobs());
161 film->set_video_frame_rate (24);
162 A->video->set_length (3 * 24);
164 BOOST_CHECK (A->full_length(film) == DCPTime::from_frames(3 * 24, 24));
165 BOOST_CHECK (B->full_length(film) == DCPTime(289920));
166 /* Length should be rounded up from B's length to the next video frame */
167 BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
169 Player player(film, Image::Alignment::COMPACT);
170 player.setup_pieces();
171 BOOST_REQUIRE_EQUAL(player._black._periods.size(), 1U);
172 BOOST_CHECK(player._black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
173 BOOST_REQUIRE_EQUAL(player._silent._periods.size(), 1U);
174 BOOST_CHECK(player._silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
178 static Frame video_frames;
179 static Frame audio_frames;
183 video (shared_ptr<PlayerVideo>, DCPTime)
189 audio (shared_ptr<AudioBuffers> audio, DCPTime)
191 audio_frames += audio->frames();
195 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
196 BOOST_AUTO_TEST_CASE (player_interleave_test)
198 auto film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
199 film->set_name ("ffmpeg_transcoder_basic_test");
200 film->set_container (Ratio::from_id ("185"));
201 film->set_audio_channels (6);
203 auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
204 film->examine_and_add_content (c);
205 BOOST_REQUIRE (!wait_for_jobs ());
207 auto s = std::make_shared<StringTextFileContent>("test/data/subrip.srt");
208 film->examine_and_add_content (s);
209 BOOST_REQUIRE (!wait_for_jobs ());
211 Player player(film, Image::Alignment::COMPACT);
212 player.Video.connect(bind(&video, _1, _2));
213 player.Audio.connect(bind(&audio, _1, _2));
214 video_frames = audio_frames = 0;
215 while (!player.pass()) {
216 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) <= 8);
221 /** Test some seeks towards the start of a DCP with awkward subtitles; see mantis #1085
222 * and a number of others. I thought this was a player seek bug but in fact it was
223 * caused by the subtitle starting just after the start of the video frame and hence
226 BOOST_AUTO_TEST_CASE (player_seek_test)
228 auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
229 auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs");
230 film->examine_and_add_content (dcp, true);
231 BOOST_REQUIRE (!wait_for_jobs ());
232 dcp->only_text()->set_use (true);
234 Player player(film, Image::Alignment::COMPACT);
236 player.set_always_burn_open_subtitles();
237 player.set_play_referenced();
239 auto butler = std::make_shared<Butler>(
240 film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
243 for (int i = 0; i < 10; ++i) {
244 auto t = DCPTime::from_frames (i, 24);
245 butler->seek (t, true);
246 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
247 BOOST_CHECK_EQUAL(video.second.get(), t.get());
248 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));
249 /* This 14.08 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
250 (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
253 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);
258 /** Test some more seeks towards the start of a DCP with awkward subtitles */
259 BOOST_AUTO_TEST_CASE (player_seek_test2)
261 auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
262 auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs2");
263 film->examine_and_add_content (dcp, true);
264 BOOST_REQUIRE (!wait_for_jobs ());
265 dcp->only_text()->set_use (true);
267 Player player(film, Image::Alignment::COMPACT);
269 player.set_always_burn_open_subtitles();
270 player.set_play_referenced();
272 auto butler = std::make_shared<Butler>
273 (film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
276 butler->seek(DCPTime::from_seconds(5), true);
278 for (int i = 0; i < 10; ++i) {
279 auto t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
280 butler->seek (t, true);
281 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
282 BOOST_CHECK_EQUAL(video.second.get(), t.get());
284 video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test2_%1.png", i)
286 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);
291 /** Test a bug when trimmed content follows other content */
292 BOOST_AUTO_TEST_CASE (player_trim_test)
294 auto film = new_test_film2 ("player_trim_test");
295 auto A = content_factory("test/data/flat_red.png")[0];
296 film->examine_and_add_content (A);
297 BOOST_REQUIRE (!wait_for_jobs ());
298 A->video->set_length (10 * 24);
299 auto B = content_factory("test/data/flat_red.png")[0];
300 film->examine_and_add_content (B);
301 BOOST_REQUIRE (!wait_for_jobs ());
302 B->video->set_length (10 * 24);
303 B->set_position (film, DCPTime::from_seconds(10));
304 B->set_trim_start (ContentTime::from_seconds (2));
306 make_and_verify_dcp (film);
313 optional<DCPTextTrack> track;
314 DCPTimePeriod period;
319 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
330 /** Test ignoring both video and audio */
331 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
333 auto film = new_test_film2 ("player_ignore_video_and_audio_test");
334 auto ff = content_factory(TestPaths::private_data() / "boon_telly.mkv")[0];
335 film->examine_and_add_content (ff);
336 auto text = content_factory("test/data/subrip.srt")[0];
337 film->examine_and_add_content (text);
338 BOOST_REQUIRE (!wait_for_jobs());
339 text->only_text()->set_type (TextType::CLOSED_CAPTION);
340 text->only_text()->set_use (true);
342 Player player(film, Image::Alignment::COMPACT);
343 player.set_ignore_video();
344 player.set_ignore_audio();
347 player.Text.connect(bind (&store, &out, _1, _2, _3, _4));
348 while (!player.pass()) {}
350 BOOST_CHECK_EQUAL (out.size(), 6U);
354 /** Trigger a crash due to the assertion failure in Player::emit_audio */
355 BOOST_AUTO_TEST_CASE (player_trim_crash)
357 auto film = new_test_film2 ("player_trim_crash");
358 auto boon = content_factory(TestPaths::private_data() / "boon_telly.mkv")[0];
359 film->examine_and_add_content (boon);
360 BOOST_REQUIRE (!wait_for_jobs());
362 Player player(film, Image::Alignment::COMPACT);
364 auto butler = std::make_shared<Butler>(
365 film, player, AudioMapping(), 6, bind(&PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::COMPACT, true, false, Butler::Audio::ENABLED
368 /* Wait for the butler to fill */
369 dcpomatic_sleep_seconds (5);
371 boon->set_trim_start (ContentTime::from_seconds(5));
373 butler->seek (DCPTime(), true);
375 /* Wait for the butler to refill */
376 dcpomatic_sleep_seconds (5);
382 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
383 BOOST_AUTO_TEST_CASE (player_silence_crash)
385 auto film = new_test_film2 ("player_silence_crash");
386 auto sine = content_factory("test/data/impulse_train.wav")[0];
387 film->examine_and_add_content (sine);
388 BOOST_REQUIRE (!wait_for_jobs());
390 sine->set_video_frame_rate (23.976);
391 film->write_metadata ();
392 make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
396 /** Test a crash when processing a 3D DCP */
397 BOOST_AUTO_TEST_CASE (player_3d_test_1)
399 auto film = new_test_film2 ("player_3d_test_1a");
400 auto left = content_factory("test/data/flat_red.png")[0];
401 film->examine_and_add_content (left);
402 auto right = content_factory("test/data/flat_blue.png")[0];
403 film->examine_and_add_content (right);
404 BOOST_REQUIRE (!wait_for_jobs());
406 left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
407 left->set_position (film, DCPTime());
408 right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
409 right->set_position (film, DCPTime());
410 film->set_three_d (true);
412 make_and_verify_dcp (film);
414 auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
415 auto film2 = new_test_film2 ("player_3d_test_1b", {dcp});
417 film2->set_three_d (true);
418 make_and_verify_dcp (film2);
422 /** Test a crash when processing a 3D DCP as content in a 2D project */
423 BOOST_AUTO_TEST_CASE (player_3d_test_2)
425 auto left = content_factory("test/data/flat_red.png")[0];
426 auto right = content_factory("test/data/flat_blue.png")[0];
427 auto film = new_test_film2 ("player_3d_test_2a", {left, right});
429 left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
430 left->set_position (film, DCPTime());
431 right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
432 right->set_position (film, DCPTime());
433 film->set_three_d (true);
435 make_and_verify_dcp (film);
437 auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
438 auto film2 = new_test_film2 ("player_3d_test_2b", {dcp});
440 make_and_verify_dcp (film2);
444 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
447 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
449 /* 25fps DCP with some audio */
450 auto content1 = content_factory("test/data/flat_red.png")[0];
451 auto film1 = new_test_film2 ("player_silence_at_end_crash_1", {content1});
452 content1->video->set_length (25);
453 film1->set_video_frame_rate (25);
454 make_and_verify_dcp (film1);
456 /* Make another project importing this DCP */
457 auto content2 = std::make_shared<DCPContent>(film1->dir(film1->dcp_name()));
458 auto film2 = new_test_film2 ("player_silence_at_end_crash_2", {content2});
460 /* and importing just the video MXF on its own at the end */
461 optional<boost::filesystem::path> video;
462 for (auto i: boost::filesystem::directory_iterator(film1->dir(film1->dcp_name()))) {
463 if (boost::starts_with(i.path().filename().string(), "j2c_")) {
468 BOOST_REQUIRE (video);
469 auto content3 = content_factory(*video)[0];
470 film2->examine_and_add_content (content3);
471 BOOST_REQUIRE (!wait_for_jobs());
472 content3->set_position (film2, DCPTime::from_seconds(1.5));
473 film2->set_video_frame_rate (24);
474 make_and_verify_dcp (film2);
479 BOOST_AUTO_TEST_CASE (encrypted_dcp_with_no_kdm_gives_no_butler_error)
481 auto content = content_factory("test/data/flat_red.png")[0];
482 auto film = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error", { content });
483 int constexpr length = 24 * 25;
484 content->video->set_length(length);
485 film->set_encrypted (true);
486 make_and_verify_dcp (
489 dcp::VerificationNote::Code::MISSING_CPL_METADATA,
492 auto content2 = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
493 auto film2 = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error2", { content2 });
495 Player player(film, Image::Alignment::COMPACT);
496 Butler butler(film2, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::ENABLED);
498 float buffer[2000 * 6];
499 for (int i = 0; i < length; ++i) {
500 butler.get_video(Butler::Behaviour::BLOCKING, 0);
501 butler.get_audio(Butler::Behaviour::BLOCKING, buffer, 2000);
504 BOOST_CHECK_NO_THROW(butler.rethrow());
508 BOOST_AUTO_TEST_CASE (interleaved_subtitle_are_emitted_correctly)
510 boost::filesystem::path paths[2] = {
511 "build/test/interleaved_subtitle_are_emitted_correctly1.srt",
512 "build/test/interleaved_subtitle_are_emitted_correctly2.srt"
515 dcp::File subs_file[2] = { dcp::File(paths[0], "w"), dcp::File(paths[1], "w") };
517 fprintf(subs_file[0].get(), "1\n00:00:01,000 -> 00:00:02,000\nSub 1/1\n\n");
518 fprintf(subs_file[0].get(), "2\n00:00:05,000 -> 00:00:06,000\nSub 1/2\n\n");
520 fprintf(subs_file[1].get(), "1\n00:00:00,500 -> 00:00:01,500\nSub 2/1\n\n");
521 fprintf(subs_file[1].get(), "2\n00:00:02,000 -> 00:00:03,000\nSub 2/2\n\n");
523 subs_file[0].close();
524 subs_file[1].close();
526 auto subs1 = content_factory(paths[0])[0];
527 auto subs2 = content_factory(paths[1])[0];
528 auto film = new_test_film2("interleaved_subtitle_are_emitted_correctly", { subs1, subs2 });
529 film->set_sequence(false);
530 subs1->set_position(film, DCPTime());
531 subs2->set_position(film, DCPTime());
533 Player player(film, Image::Alignment::COMPACT);
535 player.Text.connect([&last](PlayerText text, TextType, optional<DCPTextTrack>, dcpomatic::DCPTimePeriod) {
536 for (auto sub: text.string) {
537 BOOST_CHECK(sub.in() >= last);
541 while (!player.pass()) {}
545 BOOST_AUTO_TEST_CASE(multiple_sound_files_bug)
547 Config::instance()->set_log_types(Config::instance()->log_types() | LogEntry::TYPE_DEBUG_PLAYER);
549 auto A = content_factory(TestPaths::private_data() / "kook" / "1.wav").front();
550 auto B = content_factory(TestPaths::private_data() / "kook" / "2.wav").front();
551 auto C = content_factory(TestPaths::private_data() / "kook" / "3.wav").front();
553 auto film = new_test_film2("multiple_sound_files_bug", { A, B, C });
554 C->set_position(film, DCPTime(3840000));
556 make_and_verify_dcp(film, { dcp::VerificationNote::Code::MISSING_CPL_METADATA });
558 check_mxf_audio_file(TestPaths::private_data() / "kook" / "reference.mxf", dcp_file(film, "pcm_"));
562 BOOST_AUTO_TEST_CASE(trimmed_sound_mix_bug_13)
564 auto A = content_factory("test/data/sine_16_48_440_10.wav").front();
565 auto B = content_factory("test/data/sine_16_44.1_440_10.wav").front();
566 auto film = new_test_film2("trimmed_sound_mix_bug_13", { A, B });
568 A->set_position(film, DCPTime());
569 A->audio->set_gain(-12);
570 B->set_position(film, DCPTime());
571 B->audio->set_gain(-12);
572 B->set_trim_start(ContentTime(13));
574 make_and_verify_dcp(film, { dcp::VerificationNote::Code::MISSING_CPL_METADATA });
575 check_mxf_audio_file("test/data/trimmed_sound_mix_bug_13.mxf", dcp_file(film, "pcm_"));