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>(film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false);
237 butler->disable_audio();
239 for (int i = 0; i < 10; ++i) {
240 auto t = DCPTime::from_frames (i, 24);
241 butler->seek (t, true);
242 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
243 BOOST_CHECK_EQUAL(video.second.get(), t.get());
244 write_image(video.first->image(bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test_%1.png", i));
245 /* This 14.08 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
246 (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
249 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);
254 /** Test some more seeks towards the start of a DCP with awkward subtitles */
255 BOOST_AUTO_TEST_CASE (player_seek_test2)
257 auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
258 auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs2");
259 film->examine_and_add_content (dcp, true);
260 BOOST_REQUIRE (!wait_for_jobs ());
261 dcp->only_text()->set_use (true);
263 auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
265 player->set_always_burn_open_subtitles ();
266 player->set_play_referenced ();
268 auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false);
269 butler->disable_audio();
271 butler->seek(DCPTime::from_seconds(5), true);
273 for (int i = 0; i < 10; ++i) {
274 auto t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
275 butler->seek (t, true);
276 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
277 BOOST_CHECK_EQUAL(video.second.get(), t.get());
279 video.first->image(bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test2_%1.png", i)
281 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);
286 /** Test a bug when trimmed content follows other content */
287 BOOST_AUTO_TEST_CASE (player_trim_test)
289 auto film = new_test_film2 ("player_trim_test");
290 auto A = content_factory("test/data/flat_red.png").front();
291 film->examine_and_add_content (A);
292 BOOST_REQUIRE (!wait_for_jobs ());
293 A->video->set_length (10 * 24);
294 auto B = content_factory("test/data/flat_red.png").front();
295 film->examine_and_add_content (B);
296 BOOST_REQUIRE (!wait_for_jobs ());
297 B->video->set_length (10 * 24);
298 B->set_position (film, DCPTime::from_seconds(10));
299 B->set_trim_start (ContentTime::from_seconds (2));
301 make_and_verify_dcp (film);
308 optional<DCPTextTrack> track;
309 DCPTimePeriod period;
314 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
325 /** Test ignoring both video and audio */
326 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
328 auto film = new_test_film2 ("player_ignore_video_and_audio_test");
329 auto ff = content_factory(TestPaths::private_data() / "boon_telly.mkv").front();
330 film->examine_and_add_content (ff);
331 auto text = content_factory("test/data/subrip.srt").front();
332 film->examine_and_add_content (text);
333 BOOST_REQUIRE (!wait_for_jobs());
334 text->only_text()->set_type (TextType::CLOSED_CAPTION);
335 text->only_text()->set_use (true);
337 auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
338 player->set_ignore_video ();
339 player->set_ignore_audio ();
342 player->Text.connect (bind (&store, &out, _1, _2, _3, _4));
343 while (!player->pass ()) {}
345 BOOST_CHECK_EQUAL (out.size(), 6U);
349 /** Trigger a crash due to the assertion failure in Player::emit_audio */
350 BOOST_AUTO_TEST_CASE (player_trim_crash)
352 auto film = new_test_film2 ("player_trim_crash");
353 auto boon = content_factory(TestPaths::private_data() / "boon_telly.mkv").front();
354 film->examine_and_add_content (boon);
355 BOOST_REQUIRE (!wait_for_jobs());
357 auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
359 auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 6, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::COMPACT, true, false);
361 /* Wait for the butler to fill */
362 dcpomatic_sleep_seconds (5);
364 boon->set_trim_start (ContentTime::from_seconds(5));
366 butler->seek (DCPTime(), true);
368 /* Wait for the butler to refill */
369 dcpomatic_sleep_seconds (5);
375 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
376 BOOST_AUTO_TEST_CASE (player_silence_crash)
378 auto film = new_test_film2 ("player_silence_crash");
379 auto sine = content_factory("test/data/impulse_train.wav").front();
380 film->examine_and_add_content (sine);
381 BOOST_REQUIRE (!wait_for_jobs());
383 sine->set_video_frame_rate (23.976);
384 film->write_metadata ();
385 make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
389 /** Test a crash when processing a 3D DCP */
390 BOOST_AUTO_TEST_CASE (player_3d_test_1)
392 auto film = new_test_film2 ("player_3d_test_1a");
393 auto left = content_factory("test/data/flat_red.png").front();
394 film->examine_and_add_content (left);
395 auto right = content_factory("test/data/flat_blue.png").front();
396 film->examine_and_add_content (right);
397 BOOST_REQUIRE (!wait_for_jobs());
399 left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
400 left->set_position (film, DCPTime());
401 right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
402 right->set_position (film, DCPTime());
403 film->set_three_d (true);
405 make_and_verify_dcp (film);
407 auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
408 auto film2 = new_test_film2 ("player_3d_test_1b", {dcp});
410 film2->set_three_d (true);
411 make_and_verify_dcp (film2);
415 /** Test a crash when processing a 3D DCP as content in a 2D project */
416 BOOST_AUTO_TEST_CASE (player_3d_test_2)
418 auto left = content_factory("test/data/flat_red.png").front();
419 auto right = content_factory("test/data/flat_blue.png").front();
420 auto film = new_test_film2 ("player_3d_test_2a", {left, right});
422 left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
423 left->set_position (film, DCPTime());
424 right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
425 right->set_position (film, DCPTime());
426 film->set_three_d (true);
428 make_and_verify_dcp (film);
430 auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
431 auto film2 = new_test_film2 ("player_3d_test_2b", {dcp});
433 make_and_verify_dcp (film2);
437 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
440 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
442 /* 25fps DCP with some audio */
443 auto content1 = content_factory("test/data/flat_red.png").front();
444 auto film1 = new_test_film2 ("player_silence_at_end_crash_1", {content1});
445 content1->video->set_length (25);
446 film1->set_video_frame_rate (25);
447 make_and_verify_dcp (film1);
449 /* Make another project importing this DCP */
450 auto content2 = std::make_shared<DCPContent>(film1->dir(film1->dcp_name()));
451 auto film2 = new_test_film2 ("player_silence_at_end_crash_2", {content2});
453 /* and importing just the video MXF on its own at the end */
454 optional<boost::filesystem::path> video;
455 for (auto i: boost::filesystem::directory_iterator(film1->dir(film1->dcp_name()))) {
456 if (boost::starts_with(i.path().filename().string(), "j2c_")) {
461 BOOST_REQUIRE (video);
462 auto content3 = content_factory(*video).front();
463 film2->examine_and_add_content (content3);
464 BOOST_REQUIRE (!wait_for_jobs());
465 content3->set_position (film2, DCPTime::from_seconds(1.5));
466 film2->set_video_frame_rate (24);
467 make_and_verify_dcp (film2);