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 boost::shared_ptr;
51 using boost::optional;
52 using namespace dcpomatic;
54 static shared_ptr<AudioBuffers> accumulated;
57 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
59 BOOST_REQUIRE (accumulated);
60 accumulated->append (audio);
63 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
64 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
66 shared_ptr<Film> film = new_test_film ("player_silence_padding_test");
67 film->set_name ("player_silence_padding_test");
68 shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
69 film->set_container (Ratio::from_id ("185"));
70 film->set_audio_channels (6);
72 film->examine_and_add_content (c);
73 BOOST_REQUIRE (!wait_for_jobs());
75 accumulated.reset (new AudioBuffers (film->audio_channels(), 0));
77 shared_ptr<Player> player (new Player (film, film->playlist ()));
78 player->Audio.connect (bind (&accumulate, _1, _2));
79 while (!player->pass ()) {}
80 BOOST_REQUIRE (accumulated->frames() >= 48000);
81 BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
83 for (int i = 0; i < 48000; ++i) {
84 for (int c = 0; c < accumulated->channels(); ++c) {
85 BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
90 /* Test insertion of black frames between separate bits of video content */
91 BOOST_AUTO_TEST_CASE (player_black_fill_test)
93 shared_ptr<Film> film = new_test_film ("black_fill_test");
94 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
95 film->set_name ("black_fill_test");
96 film->set_container (Ratio::from_id ("185"));
97 film->set_sequence (false);
98 film->set_interop (false);
99 shared_ptr<ImageContent> contentA (new ImageContent("test/data/simple_testcard_640x480.png"));
100 shared_ptr<ImageContent> contentB (new ImageContent("test/data/simple_testcard_640x480.png"));
102 film->examine_and_add_content (contentA);
103 film->examine_and_add_content (contentB);
104 BOOST_REQUIRE (!wait_for_jobs());
106 contentA->video->set_scale (VideoContentScale (Ratio::from_id ("185")));
107 contentA->video->set_length (3);
108 contentA->set_position (film, DCPTime::from_frames(2, film->video_frame_rate()));
109 contentB->video->set_scale (VideoContentScale (Ratio::from_id ("185")));
110 contentB->video->set_length (1);
111 contentB->set_position (film, DCPTime::from_frames(7, film->video_frame_rate()));
115 BOOST_REQUIRE (!wait_for_jobs());
117 boost::filesystem::path ref;
120 ref /= "black_fill_test";
122 boost::filesystem::path check;
125 check /= "black_fill_test";
126 check /= film->dcp_name();
128 check_dcp (ref.string(), check.string());
131 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
132 BOOST_AUTO_TEST_CASE (player_subframe_test)
134 shared_ptr<Film> film = new_test_film ("reels_test7");
135 film->set_name ("reels_test7");
136 film->set_container (Ratio::from_id ("185"));
137 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
138 shared_ptr<Content> A = content_factory("test/data/flat_red.png").front();
139 film->examine_and_add_content (A);
140 BOOST_REQUIRE (!wait_for_jobs ());
141 shared_ptr<Content> B = content_factory("test/data/awkward_length.wav").front();
142 film->examine_and_add_content (B);
143 BOOST_REQUIRE (!wait_for_jobs ());
144 film->set_video_frame_rate (24);
145 A->video->set_length (3 * 24);
147 BOOST_CHECK (A->full_length(film) == DCPTime::from_frames(3 * 24, 24));
148 BOOST_CHECK (B->full_length(film) == DCPTime(289920));
149 /* Length should be rounded up from B's length to the next video frame */
150 BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
152 shared_ptr<Player> player (new Player (film, film->playlist ()));
153 player->setup_pieces ();
154 BOOST_REQUIRE_EQUAL (player->_black._periods.size(), 1);
155 BOOST_CHECK (player->_black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
156 BOOST_REQUIRE_EQUAL (player->_silent._periods.size(), 1);
157 BOOST_CHECK (player->_silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
160 static Frame video_frames;
161 static Frame audio_frames;
164 video (shared_ptr<PlayerVideo>, DCPTime)
170 audio (shared_ptr<AudioBuffers> audio, DCPTime)
172 audio_frames += audio->frames();
175 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
176 BOOST_AUTO_TEST_CASE (player_interleave_test)
178 shared_ptr<Film> film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
179 film->set_name ("ffmpeg_transcoder_basic_test");
180 film->set_container (Ratio::from_id ("185"));
181 film->set_audio_channels (6);
183 shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
184 film->examine_and_add_content (c);
185 BOOST_REQUIRE (!wait_for_jobs ());
187 shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip.srt"));
188 film->examine_and_add_content (s);
189 BOOST_REQUIRE (!wait_for_jobs ());
191 shared_ptr<Player> player (new Player(film, film->playlist()));
192 player->Video.connect (bind (&video, _1, _2));
193 player->Audio.connect (bind (&audio, _1, _2));
194 video_frames = audio_frames = 0;
195 while (!player->pass ()) {
196 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) < 8);
200 /** Test some seeks towards the start of a DCP with awkward subtitles; see mantis #1085
201 * and a number of others. I thought this was a player seek bug but in fact it was
202 * caused by the subtitle starting just after the start of the video frame and hence
205 BOOST_AUTO_TEST_CASE (player_seek_test)
207 shared_ptr<Film> film (new Film (optional<boost::filesystem::path>()));
208 shared_ptr<DCPContent> dcp (new DCPContent(private_data / "awkward_subs"));
209 film->examine_and_add_content (dcp, true);
210 BOOST_REQUIRE (!wait_for_jobs ());
211 dcp->only_text()->set_use (true);
213 shared_ptr<Player> player (new Player (film, film->playlist()));
215 player->set_always_burn_open_subtitles ();
216 player->set_play_referenced ();
218 shared_ptr<Butler> butler (new Butler (player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
219 butler->disable_audio();
221 for (int i = 0; i < 10; ++i) {
222 DCPTime t = DCPTime::from_frames (i, 24);
223 butler->seek (t, true);
224 pair<shared_ptr<PlayerVideo>, DCPTime> video = butler->get_video(true, 0);
225 BOOST_CHECK_EQUAL(video.second.get(), t.get());
226 write_image(video.first->image(bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true), String::compose("build/test/player_seek_test_%1.png", i), "RGB");
227 /* This 0.055 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
228 (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
231 check_image(String::compose("test/data/player_seek_test_%1.png", i), String::compose("build/test/player_seek_test_%1.png", i), 0.055);
235 /** Test some more seeks towards the start of a DCP with awkward subtitles */
236 BOOST_AUTO_TEST_CASE (player_seek_test2)
238 shared_ptr<Film> film (new Film (optional<boost::filesystem::path>()));
239 shared_ptr<DCPContent> dcp (new DCPContent(private_data / "awkward_subs2"));
240 film->examine_and_add_content (dcp, true);
241 BOOST_REQUIRE (!wait_for_jobs ());
242 dcp->only_text()->set_use (true);
244 shared_ptr<Player> player (new Player (film, film->playlist()));
246 player->set_always_burn_open_subtitles ();
247 player->set_play_referenced ();
249 shared_ptr<Butler> butler (new Butler(player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
250 butler->disable_audio();
252 butler->seek(DCPTime::from_seconds(5), true);
254 for (int i = 0; i < 10; ++i) {
255 DCPTime t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
256 butler->seek (t, true);
257 pair<shared_ptr<PlayerVideo>, DCPTime> video = butler->get_video(true, 0);
258 BOOST_CHECK_EQUAL(video.second.get(), t.get());
259 write_image(video.first->image(bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true), String::compose("build/test/player_seek_test2_%1.png", i), "RGB");
260 check_image(String::compose("test/data/player_seek_test2_%1.png", i), String::compose("build/test/player_seek_test2_%1.png", i), 0.055);
264 /** Test a bug when trimmed content follows other content */
265 BOOST_AUTO_TEST_CASE (player_trim_test)
267 shared_ptr<Film> film = new_test_film2 ("player_trim_test");
268 shared_ptr<Content> A = content_factory("test/data/flat_red.png").front();
269 film->examine_and_add_content (A);
270 BOOST_REQUIRE (!wait_for_jobs ());
271 A->video->set_length (10 * 24);
272 shared_ptr<Content> B = content_factory("test/data/flat_red.png").front();
273 film->examine_and_add_content (B);
274 BOOST_REQUIRE (!wait_for_jobs ());
275 B->video->set_length (10 * 24);
276 B->set_position (film, DCPTime::from_seconds(10));
277 B->set_trim_start (ContentTime::from_seconds (2));
280 BOOST_REQUIRE (!wait_for_jobs ());
286 optional<DCPTextTrack> track;
287 DCPTimePeriod period;
291 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
301 /** Test ignoring both video and audio */
302 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
304 shared_ptr<Film> film = new_test_film2 ("player_ignore_video_and_audio_test");
305 shared_ptr<Content> ff = content_factory(private_data / "boon_telly.mkv").front();
306 film->examine_and_add_content (ff);
307 shared_ptr<Content> text = content_factory("test/data/subrip.srt").front();
308 film->examine_and_add_content (text);
309 BOOST_REQUIRE (!wait_for_jobs());
310 text->only_text()->set_type (TEXT_CLOSED_CAPTION);
311 text->only_text()->set_use (true);
313 shared_ptr<Player> player (new Player(film, film->playlist()));
314 player->set_ignore_video ();
315 player->set_ignore_audio ();
318 player->Text.connect (bind (&store, &out, _1, _2, _3, _4));
319 while (!player->pass ()) {}
321 BOOST_CHECK_EQUAL (out.size(), 6);
324 /** Trigger a crash due to the assertion failure in Player::emit_audio */
325 BOOST_AUTO_TEST_CASE (player_trim_crash)
327 shared_ptr<Film> film = new_test_film2 ("player_trim_crash");
328 shared_ptr<Content> boon = content_factory(private_data / "boon_telly.mkv").front();
329 film->examine_and_add_content (boon);
330 BOOST_REQUIRE (!wait_for_jobs());
332 shared_ptr<Player> player (new Player(film, film->playlist()));
334 shared_ptr<Butler> butler (new Butler(player, AudioMapping(), 6, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
336 /* Wait for the butler to fill */
337 dcpomatic_sleep_seconds (5);
339 boon->set_trim_start (ContentTime::from_seconds(5));
341 butler->seek (DCPTime(), true);
343 /* Wait for the butler to refill */
344 dcpomatic_sleep_seconds (5);
349 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
350 BOOST_AUTO_TEST_CASE (player_silence_crash)
352 shared_ptr<Film> film = new_test_film2 ("player_silence_crash");
353 shared_ptr<Content> sine = content_factory("test/data/impulse_train.wav").front();
354 film->examine_and_add_content (sine);
355 BOOST_REQUIRE (!wait_for_jobs());
357 sine->set_video_frame_rate (23.976);
358 film->write_metadata ();
360 BOOST_REQUIRE (!wait_for_jobs());
363 /** Test a crash when processing a 3D DCP */
364 BOOST_AUTO_TEST_CASE (player_3d_test_1)
366 shared_ptr<Film> film = new_test_film2 ("player_3d_test_1a");
367 shared_ptr<Content> left = content_factory("test/data/flat_red.png").front();
368 film->examine_and_add_content (left);
369 shared_ptr<Content> right = content_factory("test/data/flat_blue.png").front();
370 film->examine_and_add_content (right);
371 BOOST_REQUIRE (!wait_for_jobs());
373 left->video->set_frame_type (VIDEO_FRAME_TYPE_3D_LEFT);
374 left->set_position (film, DCPTime());
375 right->video->set_frame_type (VIDEO_FRAME_TYPE_3D_RIGHT);
376 right->set_position (film, DCPTime());
377 film->set_three_d (true);
380 BOOST_REQUIRE (!wait_for_jobs());
382 shared_ptr<Film> film2 = new_test_film2 ("player_3d_test_1b");
383 shared_ptr<Content> dcp(new DCPContent(film->dir(film->dcp_name())));
384 film2->examine_and_add_content (dcp);
385 BOOST_REQUIRE (!wait_for_jobs());
387 film2->set_three_d (true);
389 BOOST_REQUIRE (!wait_for_jobs());
392 /** Test a crash when processing a 3D DCP as content in a 2D project */
393 BOOST_AUTO_TEST_CASE (player_3d_test_2)
395 shared_ptr<Film> film = new_test_film2 ("player_3d_test_2a");
396 shared_ptr<Content> left = content_factory("test/data/flat_red.png").front();
397 film->examine_and_add_content (left);
398 shared_ptr<Content> right = content_factory("test/data/flat_blue.png").front();
399 film->examine_and_add_content (right);
400 BOOST_REQUIRE (!wait_for_jobs());
402 left->video->set_frame_type (VIDEO_FRAME_TYPE_3D_LEFT);
403 left->set_position (film, DCPTime());
404 right->video->set_frame_type (VIDEO_FRAME_TYPE_3D_RIGHT);
405 right->set_position (film, DCPTime());
406 film->set_three_d (true);
409 BOOST_REQUIRE (!wait_for_jobs());
411 shared_ptr<Film> film2 = new_test_film2 ("player_3d_test_2b");
412 shared_ptr<Content> dcp(new DCPContent(film->dir(film->dcp_name())));
413 film2->examine_and_add_content (dcp);
414 BOOST_REQUIRE (!wait_for_jobs());
417 BOOST_REQUIRE (!wait_for_jobs());
420 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
423 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
425 /* 25fps DCP with some audio */
426 shared_ptr<Film> film1 = new_test_film2 ("player_silence_at_end_crash_1");
427 shared_ptr<Content> content1 = content_factory("test/data/flat_red.png").front();
428 film1->examine_and_add_content (content1);
429 BOOST_REQUIRE (!wait_for_jobs());
430 content1->video->set_length (25);
431 film1->set_video_frame_rate (25);
433 BOOST_REQUIRE (!wait_for_jobs());
435 /* Make another project importing this DCP */
436 shared_ptr<Film> film2 = new_test_film2 ("player_silence_at_end_crash_2");
437 shared_ptr<Content> content2(new DCPContent(film1->dir(film1->dcp_name())));
438 film2->examine_and_add_content (content2);
439 BOOST_REQUIRE (!wait_for_jobs());
441 /* and importing just the video MXF on its own at the end */
442 optional<boost::filesystem::path> video;
443 for (boost::filesystem::directory_iterator i(film1->dir(film1->dcp_name())); i != boost::filesystem::directory_iterator(); ++i) {
444 if (boost::starts_with(i->path().filename().string(), "j2c_")) {
449 BOOST_REQUIRE (video);
450 shared_ptr<Content> content3 = content_factory(*video).front();
451 film2->examine_and_add_content (content3);
452 BOOST_REQUIRE (!wait_for_jobs());
453 content3->set_position (film2, DCPTime::from_seconds(1.5));
454 film2->set_video_frame_rate (24);
455 std::cout << "Here we go.\n";
457 BOOST_REQUIRE (!wait_for_jobs());