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
29 #include "lib/ffmpeg_content.h"
30 #include "lib/dcp_content_type.h"
31 #include "lib/ratio.h"
32 #include "lib/audio_buffers.h"
33 #include "lib/player.h"
34 #include "lib/video_content.h"
35 #include "lib/image_content.h"
36 #include "lib/string_text_file_content.h"
37 #include "lib/content_factory.h"
38 #include "lib/dcp_content.h"
39 #include "lib/text_content.h"
40 #include "lib/butler.h"
41 #include "lib/compose.hpp"
42 #include "lib/cross.h"
44 #include <boost/test/unit_test.hpp>
45 #include <boost/algorithm/string.hpp>
52 using std::shared_ptr;
53 using std::make_shared;
55 using boost::optional;
56 #if BOOST_VERSION >= 106100
57 using namespace boost::placeholders;
59 using namespace dcpomatic;
62 static shared_ptr<AudioBuffers> accumulated;
66 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
68 BOOST_REQUIRE (accumulated);
69 accumulated->append (audio);
73 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
74 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
76 auto film = new_test_film ("player_silence_padding_test");
77 film->set_name ("player_silence_padding_test");
78 auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
79 film->set_container (Ratio::from_id ("185"));
80 film->set_audio_channels (6);
82 film->examine_and_add_content (c);
83 BOOST_REQUIRE (!wait_for_jobs());
85 accumulated = std::make_shared<AudioBuffers>(film->audio_channels(), 0);
87 auto player = std::make_shared<Player>(film);
88 player->Audio.connect (bind (&accumulate, _1, _2));
89 while (!player->pass ()) {}
90 BOOST_REQUIRE (accumulated->frames() >= 48000);
91 BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
93 for (int i = 0; i < 48000; ++i) {
94 for (int c = 0; c < accumulated->channels(); ++c) {
95 BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
101 /* Test insertion of black frames between separate bits of video content */
102 BOOST_AUTO_TEST_CASE (player_black_fill_test)
104 auto film = new_test_film ("black_fill_test");
105 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
106 film->set_name ("black_fill_test");
107 film->set_container (Ratio::from_id ("185"));
108 film->set_sequence (false);
109 film->set_interop (false);
110 auto contentA = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
111 auto contentB = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
113 film->examine_and_add_content (contentA);
114 film->examine_and_add_content (contentB);
115 BOOST_REQUIRE (!wait_for_jobs());
117 contentA->video->set_length (3);
118 contentA->set_position (film, DCPTime::from_frames(2, film->video_frame_rate()));
119 contentA->video->set_custom_ratio (1.85);
120 contentB->video->set_length (1);
121 contentB->set_position (film, DCPTime::from_frames(7, film->video_frame_rate()));
122 contentB->video->set_custom_ratio (1.85);
124 make_and_verify_dcp (
127 dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
128 dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
131 boost::filesystem::path ref;
134 ref /= "black_fill_test";
136 boost::filesystem::path check;
139 check /= "black_fill_test";
140 check /= film->dcp_name();
142 check_dcp (ref.string(), check.string());
146 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
147 BOOST_AUTO_TEST_CASE (player_subframe_test)
149 auto film = new_test_film ("reels_test7");
150 film->set_name ("reels_test7");
151 film->set_container (Ratio::from_id("185"));
152 film->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
153 auto A = content_factory("test/data/flat_red.png").front();
154 film->examine_and_add_content (A);
155 BOOST_REQUIRE (!wait_for_jobs());
156 auto B = content_factory("test/data/awkward_length.wav").front();
157 film->examine_and_add_content (B);
158 BOOST_REQUIRE (!wait_for_jobs());
159 film->set_video_frame_rate (24);
160 A->video->set_length (3 * 24);
162 BOOST_CHECK (A->full_length(film) == DCPTime::from_frames(3 * 24, 24));
163 BOOST_CHECK (B->full_length(film) == DCPTime(289920));
164 /* Length should be rounded up from B's length to the next video frame */
165 BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
167 auto player = std::make_shared<Player>(film);
168 player->setup_pieces ();
169 BOOST_REQUIRE_EQUAL (player->_black._periods.size(), 1U);
170 BOOST_CHECK (player->_black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
171 BOOST_REQUIRE_EQUAL (player->_silent._periods.size(), 1U);
172 BOOST_CHECK (player->_silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
176 static Frame video_frames;
177 static Frame audio_frames;
181 video (shared_ptr<PlayerVideo>, DCPTime)
187 audio (shared_ptr<AudioBuffers> audio, DCPTime)
189 audio_frames += audio->frames();
193 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
194 BOOST_AUTO_TEST_CASE (player_interleave_test)
196 auto film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
197 film->set_name ("ffmpeg_transcoder_basic_test");
198 film->set_container (Ratio::from_id ("185"));
199 film->set_audio_channels (6);
201 auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
202 film->examine_and_add_content (c);
203 BOOST_REQUIRE (!wait_for_jobs ());
205 auto s = std::make_shared<StringTextFileContent>("test/data/subrip.srt");
206 film->examine_and_add_content (s);
207 BOOST_REQUIRE (!wait_for_jobs ());
209 auto player = std::make_shared<Player>(film);
210 player->Video.connect (bind (&video, _1, _2));
211 player->Audio.connect (bind (&audio, _1, _2));
212 video_frames = audio_frames = 0;
213 while (!player->pass ()) {
214 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) < 8);
219 /** Test some seeks towards the start of a DCP with awkward subtitles; see mantis #1085
220 * and a number of others. I thought this was a player seek bug but in fact it was
221 * caused by the subtitle starting just after the start of the video frame and hence
224 BOOST_AUTO_TEST_CASE (player_seek_test)
226 auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
227 auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs");
228 film->examine_and_add_content (dcp, true);
229 BOOST_REQUIRE (!wait_for_jobs ());
230 dcp->only_text()->set_use (true);
232 auto player = std::make_shared<Player>(film);
234 player->set_always_burn_open_subtitles ();
235 player->set_play_referenced ();
237 auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true);
238 butler->disable_audio();
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(true, 0);
244 BOOST_CHECK_EQUAL(video.second.get(), t.get());
245 write_image(video.first->image(bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, 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);
266 player->set_always_burn_open_subtitles ();
267 player->set_play_referenced ();
269 auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true);
270 butler->disable_audio();
272 butler->seek(DCPTime::from_seconds(5), true);
274 for (int i = 0; i < 10; ++i) {
275 auto t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
276 butler->seek (t, true);
277 auto video = butler->get_video(true, 0);
278 BOOST_CHECK_EQUAL(video.second.get(), t.get());
280 video.first->image(bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true), String::compose("build/test/player_seek_test2_%1.png", i)
282 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);
287 /** Test a bug when trimmed content follows other content */
288 BOOST_AUTO_TEST_CASE (player_trim_test)
290 auto film = new_test_film2 ("player_trim_test");
291 auto A = content_factory("test/data/flat_red.png").front();
292 film->examine_and_add_content (A);
293 BOOST_REQUIRE (!wait_for_jobs ());
294 A->video->set_length (10 * 24);
295 auto B = content_factory("test/data/flat_red.png").front();
296 film->examine_and_add_content (B);
297 BOOST_REQUIRE (!wait_for_jobs ());
298 B->video->set_length (10 * 24);
299 B->set_position (film, DCPTime::from_seconds(10));
300 B->set_trim_start (ContentTime::from_seconds (2));
302 make_and_verify_dcp (film);
309 optional<DCPTextTrack> track;
310 DCPTimePeriod period;
315 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
326 /** Test ignoring both video and audio */
327 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
329 auto film = new_test_film2 ("player_ignore_video_and_audio_test");
330 auto ff = content_factory(TestPaths::private_data() / "boon_telly.mkv").front();
331 film->examine_and_add_content (ff);
332 auto text = content_factory("test/data/subrip.srt").front();
333 film->examine_and_add_content (text);
334 BOOST_REQUIRE (!wait_for_jobs());
335 text->only_text()->set_type (TextType::CLOSED_CAPTION);
336 text->only_text()->set_use (true);
338 auto player = std::make_shared<Player>(film);
339 player->set_ignore_video ();
340 player->set_ignore_audio ();
343 player->Text.connect (bind (&store, &out, _1, _2, _3, _4));
344 while (!player->pass ()) {}
346 BOOST_CHECK_EQUAL (out.size(), 6U);
350 /** Trigger a crash due to the assertion failure in Player::emit_audio */
351 BOOST_AUTO_TEST_CASE (player_trim_crash)
353 auto film = new_test_film2 ("player_trim_crash");
354 auto boon = content_factory(TestPaths::private_data() / "boon_telly.mkv").front();
355 film->examine_and_add_content (boon);
356 BOOST_REQUIRE (!wait_for_jobs());
358 auto player = std::make_shared<Player>(film);
360 auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 6, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true);
362 /* Wait for the butler to fill */
363 dcpomatic_sleep_seconds (5);
365 boon->set_trim_start (ContentTime::from_seconds(5));
367 butler->seek (DCPTime(), true);
369 /* Wait for the butler to refill */
370 dcpomatic_sleep_seconds (5);
376 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
377 BOOST_AUTO_TEST_CASE (player_silence_crash)
379 auto film = new_test_film2 ("player_silence_crash");
380 auto sine = content_factory("test/data/impulse_train.wav").front();
381 film->examine_and_add_content (sine);
382 BOOST_REQUIRE (!wait_for_jobs());
384 sine->set_video_frame_rate (23.976);
385 film->write_metadata ();
386 make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
390 /** Test a crash when processing a 3D DCP */
391 BOOST_AUTO_TEST_CASE (player_3d_test_1)
393 auto film = new_test_film2 ("player_3d_test_1a");
394 auto left = content_factory("test/data/flat_red.png").front();
395 film->examine_and_add_content (left);
396 auto right = content_factory("test/data/flat_blue.png").front();
397 film->examine_and_add_content (right);
398 BOOST_REQUIRE (!wait_for_jobs());
400 left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
401 left->set_position (film, DCPTime());
402 right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
403 right->set_position (film, DCPTime());
404 film->set_three_d (true);
406 make_and_verify_dcp (film);
408 auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
409 auto film2 = new_test_film2 ("player_3d_test_1b", {dcp});
411 film2->set_three_d (true);
412 make_and_verify_dcp (film2);
416 /** Test a crash when processing a 3D DCP as content in a 2D project */
417 BOOST_AUTO_TEST_CASE (player_3d_test_2)
419 auto left = content_factory("test/data/flat_red.png").front();
420 auto right = content_factory("test/data/flat_blue.png").front();
421 auto film = new_test_film2 ("player_3d_test_2a", {left, right});
423 left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
424 left->set_position (film, DCPTime());
425 right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
426 right->set_position (film, DCPTime());
427 film->set_three_d (true);
429 make_and_verify_dcp (film);
431 auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
432 auto film2 = new_test_film2 ("player_3d_test_2b", {dcp});
434 make_and_verify_dcp (film2);
438 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
441 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
443 /* 25fps DCP with some audio */
444 auto content1 = content_factory("test/data/flat_red.png").front();
445 auto film1 = new_test_film2 ("player_silence_at_end_crash_1", {content1});
446 content1->video->set_length (25);
447 film1->set_video_frame_rate (25);
448 make_and_verify_dcp (film1);
450 /* Make another project importing this DCP */
451 auto content2 = std::make_shared<DCPContent>(film1->dir(film1->dcp_name()));
452 auto film2 = new_test_film2 ("player_silence_at_end_crash_2", {content2});
454 /* and importing just the video MXF on its own at the end */
455 optional<boost::filesystem::path> video;
456 for (auto i: boost::filesystem::directory_iterator(film1->dir(film1->dcp_name()))) {
457 if (boost::starts_with(i.path().filename().string(), "j2c_")) {
462 BOOST_REQUIRE (video);
463 auto content3 = content_factory(*video).front();
464 film2->examine_and_add_content (content3);
465 BOOST_REQUIRE (!wait_for_jobs());
466 content3->set_position (film2, DCPTime::from_seconds(1.5));
467 film2->set_video_frame_rate (24);
468 make_and_verify_dcp (film2);