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/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"
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"
44 #include <boost/test/unit_test.hpp>
45 #include <boost/algorithm/string.hpp>
51 using std::shared_ptr;
52 using std::make_shared;
54 using boost::optional;
55 #if BOOST_VERSION >= 106100
56 using namespace boost::placeholders;
58 using namespace dcpomatic;
61 static shared_ptr<AudioBuffers> accumulated;
65 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
67 BOOST_REQUIRE (accumulated);
68 accumulated->append (audio);
72 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
73 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
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);
81 film->examine_and_add_content (c);
82 BOOST_REQUIRE (!wait_for_jobs());
84 accumulated = std::make_shared<AudioBuffers>(film->audio_channels(), 0);
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 ());
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);
100 /* Test insertion of black frames between separate bits of video content */
101 BOOST_AUTO_TEST_CASE (player_black_fill_test)
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");
112 film->examine_and_add_content (contentA);
113 film->examine_and_add_content (contentB);
114 BOOST_REQUIRE (!wait_for_jobs());
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);
123 make_and_verify_dcp (
126 dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
127 dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
130 boost::filesystem::path ref;
133 ref /= "black_fill_test";
135 boost::filesystem::path check;
138 check /= "black_fill_test";
139 check /= film->dcp_name();
141 check_dcp (ref.string(), check.string());
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)
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);
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));
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)));
175 static Frame video_frames;
176 static Frame audio_frames;
180 video (shared_ptr<PlayerVideo>, DCPTime)
186 audio (shared_ptr<AudioBuffers> audio, DCPTime)
188 audio_frames += audio->frames();
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)
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);
200 auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
201 film->examine_and_add_content (c);
202 BOOST_REQUIRE (!wait_for_jobs ());
204 auto s = std::make_shared<StringTextFileContent>("test/data/subrip.srt");
205 film->examine_and_add_content (s);
206 BOOST_REQUIRE (!wait_for_jobs ());
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);
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
223 BOOST_AUTO_TEST_CASE (player_seek_test)
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);
231 auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
233 player->set_always_burn_open_subtitles ();
234 player->set_play_referenced ();
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
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
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);
255 /** Test some more seeks towards the start of a DCP with awkward subtitles */
256 BOOST_AUTO_TEST_CASE (player_seek_test2)
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);
264 auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
266 player->set_always_burn_open_subtitles ();
267 player->set_play_referenced ();
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
273 butler->seek(DCPTime::from_seconds(5), true);
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());
281 video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test2_%1.png", i)
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);
288 /** Test a bug when trimmed content follows other content */
289 BOOST_AUTO_TEST_CASE (player_trim_test)
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));
303 make_and_verify_dcp (film);
310 optional<DCPTextTrack> track;
311 DCPTimePeriod period;
316 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
327 /** Test ignoring both video and audio */
328 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
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);
339 auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
340 player->set_ignore_video ();
341 player->set_ignore_audio ();
344 player->Text.connect (bind (&store, &out, _1, _2, _3, _4));
345 while (!player->pass ()) {}
347 BOOST_CHECK_EQUAL (out.size(), 6U);
351 /** Trigger a crash due to the assertion failure in Player::emit_audio */
352 BOOST_AUTO_TEST_CASE (player_trim_crash)
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());
359 auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
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
365 /* Wait for the butler to fill */
366 dcpomatic_sleep_seconds (5);
368 boon->set_trim_start (ContentTime::from_seconds(5));
370 butler->seek (DCPTime(), true);
372 /* Wait for the butler to refill */
373 dcpomatic_sleep_seconds (5);
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)
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());
387 sine->set_video_frame_rate (23.976);
388 film->write_metadata ();
389 make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
393 /** Test a crash when processing a 3D DCP */
394 BOOST_AUTO_TEST_CASE (player_3d_test_1)
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());
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);
409 make_and_verify_dcp (film);
411 auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
412 auto film2 = new_test_film2 ("player_3d_test_1b", {dcp});
414 film2->set_three_d (true);
415 make_and_verify_dcp (film2);
419 /** Test a crash when processing a 3D DCP as content in a 2D project */
420 BOOST_AUTO_TEST_CASE (player_3d_test_2)
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});
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);
432 make_and_verify_dcp (film);
434 auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
435 auto film2 = new_test_film2 ("player_3d_test_2b", {dcp});
437 make_and_verify_dcp (film2);
441 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
444 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
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);
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});
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_")) {
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);
476 BOOST_AUTO_TEST_CASE (encrypted_dcp_with_no_kdm_gives_no_butler_error)
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 (
486 dcp::VerificationNote::Code::MISSING_CPL_METADATA,
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 });
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);
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);
501 BOOST_CHECK_NO_THROW(butler.rethrow());