2 Copyright (C) 2014-2019 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/>.
21 /** @file test/player_test.cc
22 * @brief Test Player class.
23 * @ingroup selfcontained
27 #include "lib/ffmpeg_content.h"
28 #include "lib/dcp_content_type.h"
29 #include "lib/ratio.h"
30 #include "lib/audio_buffers.h"
31 #include "lib/player.h"
32 #include "lib/video_content.h"
33 #include "lib/image_content.h"
34 #include "lib/string_text_file_content.h"
35 #include "lib/content_factory.h"
36 #include "lib/dcp_content.h"
37 #include "lib/text_content.h"
38 #include "lib/butler.h"
39 #include "lib/compose.hpp"
40 #include "lib/cross.h"
42 #include <boost/test/unit_test.hpp>
43 #include <boost/algorithm/string.hpp>
49 using std::shared_ptr;
51 using boost::optional;
52 #if BOOST_VERSION >= 106100
53 using namespace boost::placeholders;
55 using namespace dcpomatic;
57 static shared_ptr<AudioBuffers> accumulated;
60 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
62 BOOST_REQUIRE (accumulated);
63 accumulated->append (audio);
66 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
67 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
69 shared_ptr<Film> film = new_test_film ("player_silence_padding_test");
70 film->set_name ("player_silence_padding_test");
71 shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
72 film->set_container (Ratio::from_id ("185"));
73 film->set_audio_channels (6);
75 film->examine_and_add_content (c);
76 BOOST_REQUIRE (!wait_for_jobs());
78 accumulated.reset (new AudioBuffers (film->audio_channels(), 0));
80 shared_ptr<Player> player (new Player(film));
81 player->Audio.connect (bind (&accumulate, _1, _2));
82 while (!player->pass ()) {}
83 BOOST_REQUIRE (accumulated->frames() >= 48000);
84 BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
86 for (int i = 0; i < 48000; ++i) {
87 for (int c = 0; c < accumulated->channels(); ++c) {
88 BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
93 /* Test insertion of black frames between separate bits of video content */
94 BOOST_AUTO_TEST_CASE (player_black_fill_test)
96 shared_ptr<Film> film = new_test_film ("black_fill_test");
97 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
98 film->set_name ("black_fill_test");
99 film->set_container (Ratio::from_id ("185"));
100 film->set_sequence (false);
101 film->set_interop (false);
102 shared_ptr<ImageContent> contentA (new ImageContent("test/data/simple_testcard_640x480.png"));
103 shared_ptr<ImageContent> contentB (new ImageContent("test/data/simple_testcard_640x480.png"));
105 film->examine_and_add_content (contentA);
106 film->examine_and_add_content (contentB);
107 BOOST_REQUIRE (!wait_for_jobs());
109 contentA->video->set_length (3);
110 contentA->set_position (film, DCPTime::from_frames(2, film->video_frame_rate()));
111 contentA->video->set_custom_ratio (1.85);
112 contentB->video->set_length (1);
113 contentB->set_position (film, DCPTime::from_frames(7, film->video_frame_rate()));
114 contentB->video->set_custom_ratio (1.85);
118 BOOST_REQUIRE (!wait_for_jobs());
120 boost::filesystem::path ref;
123 ref /= "black_fill_test";
125 boost::filesystem::path check;
128 check /= "black_fill_test";
129 check /= film->dcp_name();
131 check_dcp (ref.string(), check.string());
134 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
135 BOOST_AUTO_TEST_CASE (player_subframe_test)
137 shared_ptr<Film> film = new_test_film ("reels_test7");
138 film->set_name ("reels_test7");
139 film->set_container (Ratio::from_id ("185"));
140 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
141 shared_ptr<Content> A = content_factory("test/data/flat_red.png").front();
142 film->examine_and_add_content (A);
143 BOOST_REQUIRE (!wait_for_jobs ());
144 shared_ptr<Content> B = content_factory("test/data/awkward_length.wav").front();
145 film->examine_and_add_content (B);
146 BOOST_REQUIRE (!wait_for_jobs ());
147 film->set_video_frame_rate (24);
148 A->video->set_length (3 * 24);
150 BOOST_CHECK (A->full_length(film) == DCPTime::from_frames(3 * 24, 24));
151 BOOST_CHECK (B->full_length(film) == DCPTime(289920));
152 /* Length should be rounded up from B's length to the next video frame */
153 BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
155 shared_ptr<Player> player (new Player(film));
156 player->setup_pieces ();
157 BOOST_REQUIRE_EQUAL (player->_black._periods.size(), 1U);
158 BOOST_CHECK (player->_black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
159 BOOST_REQUIRE_EQUAL (player->_silent._periods.size(), 1U);
160 BOOST_CHECK (player->_silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
163 static Frame video_frames;
164 static Frame audio_frames;
167 video (shared_ptr<PlayerVideo>, DCPTime)
173 audio (shared_ptr<AudioBuffers> audio, DCPTime)
175 audio_frames += audio->frames();
178 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
179 BOOST_AUTO_TEST_CASE (player_interleave_test)
181 shared_ptr<Film> film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
182 film->set_name ("ffmpeg_transcoder_basic_test");
183 film->set_container (Ratio::from_id ("185"));
184 film->set_audio_channels (6);
186 shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
187 film->examine_and_add_content (c);
188 BOOST_REQUIRE (!wait_for_jobs ());
190 shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip.srt"));
191 film->examine_and_add_content (s);
192 BOOST_REQUIRE (!wait_for_jobs ());
194 shared_ptr<Player> player (new Player(film));
195 player->Video.connect (bind (&video, _1, _2));
196 player->Audio.connect (bind (&audio, _1, _2));
197 video_frames = audio_frames = 0;
198 while (!player->pass ()) {
199 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) < 8);
203 /** Test some seeks towards the start of a DCP with awkward subtitles; see mantis #1085
204 * and a number of others. I thought this was a player seek bug but in fact it was
205 * caused by the subtitle starting just after the start of the video frame and hence
208 BOOST_AUTO_TEST_CASE (player_seek_test)
210 shared_ptr<Film> film (new Film (optional<boost::filesystem::path>()));
211 shared_ptr<DCPContent> dcp (new DCPContent(TestPaths::private_data() / "awkward_subs"));
212 film->examine_and_add_content (dcp, true);
213 BOOST_REQUIRE (!wait_for_jobs ());
214 dcp->only_text()->set_use (true);
216 shared_ptr<Player> player (new Player(film));
218 player->set_always_burn_open_subtitles ();
219 player->set_play_referenced ();
221 shared_ptr<Butler> butler (new Butler (film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true));
222 butler->disable_audio();
224 for (int i = 0; i < 10; ++i) {
225 DCPTime t = DCPTime::from_frames (i, 24);
226 butler->seek (t, true);
227 pair<shared_ptr<PlayerVideo>, DCPTime> video = butler->get_video(true, 0);
228 BOOST_CHECK_EQUAL(video.second.get(), t.get());
229 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));
230 /* This 14.08 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
231 (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
234 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);
238 /** Test some more seeks towards the start of a DCP with awkward subtitles */
239 BOOST_AUTO_TEST_CASE (player_seek_test2)
241 shared_ptr<Film> film (new Film (optional<boost::filesystem::path>()));
242 shared_ptr<DCPContent> dcp (new DCPContent(TestPaths::private_data() / "awkward_subs2"));
243 film->examine_and_add_content (dcp, true);
244 BOOST_REQUIRE (!wait_for_jobs ());
245 dcp->only_text()->set_use (true);
247 shared_ptr<Player> player (new Player(film));
249 player->set_always_burn_open_subtitles ();
250 player->set_play_referenced ();
252 shared_ptr<Butler> butler (new Butler(film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true));
253 butler->disable_audio();
255 butler->seek(DCPTime::from_seconds(5), true);
257 for (int i = 0; i < 10; ++i) {
258 DCPTime t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
259 butler->seek (t, true);
260 pair<shared_ptr<PlayerVideo>, DCPTime> video = butler->get_video(true, 0);
261 BOOST_CHECK_EQUAL(video.second.get(), t.get());
263 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)
265 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);
269 /** Test a bug when trimmed content follows other content */
270 BOOST_AUTO_TEST_CASE (player_trim_test)
272 shared_ptr<Film> film = new_test_film2 ("player_trim_test");
273 shared_ptr<Content> A = content_factory("test/data/flat_red.png").front();
274 film->examine_and_add_content (A);
275 BOOST_REQUIRE (!wait_for_jobs ());
276 A->video->set_length (10 * 24);
277 shared_ptr<Content> B = content_factory("test/data/flat_red.png").front();
278 film->examine_and_add_content (B);
279 BOOST_REQUIRE (!wait_for_jobs ());
280 B->video->set_length (10 * 24);
281 B->set_position (film, DCPTime::from_seconds(10));
282 B->set_trim_start (ContentTime::from_seconds (2));
285 BOOST_REQUIRE (!wait_for_jobs ());
291 optional<DCPTextTrack> track;
292 DCPTimePeriod period;
296 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
306 /** Test ignoring both video and audio */
307 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
309 shared_ptr<Film> film = new_test_film2 ("player_ignore_video_and_audio_test");
310 shared_ptr<Content> ff = content_factory(TestPaths::private_data() / "boon_telly.mkv").front();
311 film->examine_and_add_content (ff);
312 shared_ptr<Content> text = content_factory("test/data/subrip.srt").front();
313 film->examine_and_add_content (text);
314 BOOST_REQUIRE (!wait_for_jobs());
315 text->only_text()->set_type (TextType::CLOSED_CAPTION);
316 text->only_text()->set_use (true);
318 shared_ptr<Player> player (new Player(film));
319 player->set_ignore_video ();
320 player->set_ignore_audio ();
323 player->Text.connect (bind (&store, &out, _1, _2, _3, _4));
324 while (!player->pass ()) {}
326 BOOST_CHECK_EQUAL (out.size(), 6U);
329 /** Trigger a crash due to the assertion failure in Player::emit_audio */
330 BOOST_AUTO_TEST_CASE (player_trim_crash)
332 shared_ptr<Film> film = new_test_film2 ("player_trim_crash");
333 shared_ptr<Content> boon = content_factory(TestPaths::private_data() / "boon_telly.mkv").front();
334 film->examine_and_add_content (boon);
335 BOOST_REQUIRE (!wait_for_jobs());
337 shared_ptr<Player> player (new Player(film));
339 shared_ptr<Butler> butler (new Butler(film, player, AudioMapping(), 6, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true));
341 /* Wait for the butler to fill */
342 dcpomatic_sleep_seconds (5);
344 boon->set_trim_start (ContentTime::from_seconds(5));
346 butler->seek (DCPTime(), true);
348 /* Wait for the butler to refill */
349 dcpomatic_sleep_seconds (5);
354 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
355 BOOST_AUTO_TEST_CASE (player_silence_crash)
357 shared_ptr<Film> film = new_test_film2 ("player_silence_crash");
358 shared_ptr<Content> sine = content_factory("test/data/impulse_train.wav").front();
359 film->examine_and_add_content (sine);
360 BOOST_REQUIRE (!wait_for_jobs());
362 sine->set_video_frame_rate (23.976);
363 film->write_metadata ();
365 BOOST_REQUIRE (!wait_for_jobs());
368 /** Test a crash when processing a 3D DCP */
369 BOOST_AUTO_TEST_CASE (player_3d_test_1)
371 shared_ptr<Film> film = new_test_film2 ("player_3d_test_1a");
372 shared_ptr<Content> left = content_factory("test/data/flat_red.png").front();
373 film->examine_and_add_content (left);
374 shared_ptr<Content> right = content_factory("test/data/flat_blue.png").front();
375 film->examine_and_add_content (right);
376 BOOST_REQUIRE (!wait_for_jobs());
378 left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
379 left->set_position (film, DCPTime());
380 right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
381 right->set_position (film, DCPTime());
382 film->set_three_d (true);
385 BOOST_REQUIRE (!wait_for_jobs());
387 shared_ptr<Film> film2 = new_test_film2 ("player_3d_test_1b");
388 shared_ptr<Content> dcp(new DCPContent(film->dir(film->dcp_name())));
389 film2->examine_and_add_content (dcp);
390 BOOST_REQUIRE (!wait_for_jobs());
392 film2->set_three_d (true);
394 BOOST_REQUIRE (!wait_for_jobs());
397 /** Test a crash when processing a 3D DCP as content in a 2D project */
398 BOOST_AUTO_TEST_CASE (player_3d_test_2)
400 shared_ptr<Film> film = new_test_film2 ("player_3d_test_2a");
401 shared_ptr<Content> left = content_factory("test/data/flat_red.png").front();
402 film->examine_and_add_content (left);
403 shared_ptr<Content> right = content_factory("test/data/flat_blue.png").front();
404 film->examine_and_add_content (right);
405 BOOST_REQUIRE (!wait_for_jobs());
407 left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
408 left->set_position (film, DCPTime());
409 right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
410 right->set_position (film, DCPTime());
411 film->set_three_d (true);
414 BOOST_REQUIRE (!wait_for_jobs());
416 shared_ptr<Film> film2 = new_test_film2 ("player_3d_test_2b");
417 shared_ptr<Content> dcp(new DCPContent(film->dir(film->dcp_name())));
418 film2->examine_and_add_content (dcp);
419 BOOST_REQUIRE (!wait_for_jobs());
422 BOOST_REQUIRE (!wait_for_jobs());
425 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
428 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
430 /* 25fps DCP with some audio */
431 shared_ptr<Film> film1 = new_test_film2 ("player_silence_at_end_crash_1");
432 shared_ptr<Content> content1 = content_factory("test/data/flat_red.png").front();
433 film1->examine_and_add_content (content1);
434 BOOST_REQUIRE (!wait_for_jobs());
435 content1->video->set_length (25);
436 film1->set_video_frame_rate (25);
438 BOOST_REQUIRE (!wait_for_jobs());
440 /* Make another project importing this DCP */
441 shared_ptr<Film> film2 = new_test_film2 ("player_silence_at_end_crash_2");
442 shared_ptr<Content> content2(new DCPContent(film1->dir(film1->dcp_name())));
443 film2->examine_and_add_content (content2);
444 BOOST_REQUIRE (!wait_for_jobs());
446 /* and importing just the video MXF on its own at the end */
447 optional<boost::filesystem::path> video;
448 for (boost::filesystem::directory_iterator i(film1->dir(film1->dcp_name())); i != boost::filesystem::directory_iterator(); ++i) {
449 if (boost::starts_with(i->path().filename().string(), "j2c_")) {
454 BOOST_REQUIRE (video);
455 shared_ptr<Content> content3 = content_factory(*video).front();
456 film2->examine_and_add_content (content3);
457 BOOST_REQUIRE (!wait_for_jobs());
458 content3->set_position (film2, DCPTime::from_seconds(1.5));
459 film2->set_video_frame_rate (24);
461 BOOST_REQUIRE (!wait_for_jobs());