Fix typo if -> of (thanks to Uwe Dittes)
[dcpomatic.git] / test / player_test.cc
1 /*
2     Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 /** @file  test/player_test.cc
23  *  @brief Test Player class.
24  *  @ingroup selfcontained
25  */
26
27
28 #include "lib/film.h"
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"
43 #include "test.h"
44 #include <boost/test/unit_test.hpp>
45 #include <boost/algorithm/string.hpp>
46 #include <iostream>
47
48
49 using std::cout;
50 using std::list;
51 using std::pair;
52 using std::shared_ptr;
53 using std::make_shared;
54 using boost::bind;
55 using boost::optional;
56 #if BOOST_VERSION >= 106100
57 using namespace boost::placeholders;
58 #endif
59 using namespace dcpomatic;
60
61
62 static shared_ptr<AudioBuffers> accumulated;
63
64
65 static void
66 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
67 {
68         BOOST_REQUIRE (accumulated);
69         accumulated->append (audio);
70 }
71
72
73 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
74 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
75 {
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);
81
82         film->examine_and_add_content (c);
83         BOOST_REQUIRE (!wait_for_jobs());
84
85         accumulated = std::make_shared<AudioBuffers>(film->audio_channels(), 0);
86
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 ());
92
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);
96                 }
97         }
98 }
99
100
101 /* Test insertion of black frames between separate bits of video content */
102 BOOST_AUTO_TEST_CASE (player_black_fill_test)
103 {
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");
112
113         film->examine_and_add_content (contentA);
114         film->examine_and_add_content (contentB);
115         BOOST_REQUIRE (!wait_for_jobs());
116
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);
123
124         make_and_verify_dcp (
125                 film,
126                 {
127                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
128                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
129                 });
130
131         boost::filesystem::path ref;
132         ref = "test";
133         ref /= "data";
134         ref /= "black_fill_test";
135
136         boost::filesystem::path check;
137         check = "build";
138         check /= "test";
139         check /= "black_fill_test";
140         check /= film->dcp_name();
141
142         check_dcp (ref.string(), check.string());
143 }
144
145
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)
148 {
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);
161
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));
166
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)));
173 }
174
175
176 static Frame video_frames;
177 static Frame audio_frames;
178
179
180 static void
181 video (shared_ptr<PlayerVideo>, DCPTime)
182 {
183         ++video_frames;
184 }
185
186 static void
187 audio (shared_ptr<AudioBuffers> audio, DCPTime)
188 {
189         audio_frames += audio->frames();
190 }
191
192
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)
195 {
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);
200
201         auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
202         film->examine_and_add_content (c);
203         BOOST_REQUIRE (!wait_for_jobs ());
204
205         auto s = std::make_shared<StringTextFileContent>("test/data/subrip.srt");
206         film->examine_and_add_content (s);
207         BOOST_REQUIRE (!wait_for_jobs ());
208
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);
215         }
216 }
217
218
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
222  *  being faded out.
223  */
224 BOOST_AUTO_TEST_CASE (player_seek_test)
225 {
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);
231
232         auto player = std::make_shared<Player>(film);
233         player->set_fast ();
234         player->set_always_burn_open_subtitles ();
235         player->set_play_referenced ();
236
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();
239
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
248                    text altogether.
249                 */
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);
251         }
252 }
253
254
255 /** Test some more seeks towards the start of a DCP with awkward subtitles */
256 BOOST_AUTO_TEST_CASE (player_seek_test2)
257 {
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);
263
264         auto player = std::make_shared<Player>(film);
265         player->set_fast ();
266         player->set_always_burn_open_subtitles ();
267         player->set_play_referenced ();
268
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();
271
272         butler->seek(DCPTime::from_seconds(5), true);
273
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());
279                 write_image(
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)
281                         );
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);
283         }
284 }
285
286
287 /** Test a bug when trimmed content follows other content */
288 BOOST_AUTO_TEST_CASE (player_trim_test)
289 {
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));
301
302        make_and_verify_dcp (film);
303 }
304
305
306 struct Sub {
307         PlayerText text;
308         TextType type;
309         optional<DCPTextTrack> track;
310         DCPTimePeriod period;
311 };
312
313
314 static void
315 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
316 {
317         Sub s;
318         s.text = text;
319         s.type = type;
320         s.track = track;
321         s.period = period;
322         out->push_back (s);
323 }
324
325
326 /** Test ignoring both video and audio */
327 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
328 {
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);
337
338         auto player = std::make_shared<Player>(film);
339         player->set_ignore_video ();
340         player->set_ignore_audio ();
341
342         list<Sub> out;
343         player->Text.connect (bind (&store, &out, _1, _2, _3, _4));
344         while (!player->pass ()) {}
345
346         BOOST_CHECK_EQUAL (out.size(), 6U);
347 }
348
349
350 /** Trigger a crash due to the assertion failure in Player::emit_audio */
351 BOOST_AUTO_TEST_CASE (player_trim_crash)
352 {
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());
357
358         auto player = std::make_shared<Player>(film);
359         player->set_fast ();
360         auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 6, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true);
361
362         /* Wait for the butler to fill */
363         dcpomatic_sleep_seconds (5);
364
365         boon->set_trim_start (ContentTime::from_seconds(5));
366
367         butler->seek (DCPTime(), true);
368
369         /* Wait for the butler to refill */
370         dcpomatic_sleep_seconds (5);
371
372         butler->rethrow ();
373 }
374
375
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)
378 {
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());
383
384         sine->set_video_frame_rate (23.976);
385         film->write_metadata ();
386         make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
387 }
388
389
390 /** Test a crash when processing a 3D DCP */
391 BOOST_AUTO_TEST_CASE (player_3d_test_1)
392 {
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());
399
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);
405
406         make_and_verify_dcp (film);
407
408         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
409         auto film2 = new_test_film2 ("player_3d_test_1b", {dcp});
410
411         film2->set_three_d (true);
412         make_and_verify_dcp (film2);
413 }
414
415
416 /** Test a crash when processing a 3D DCP as content in a 2D project */
417 BOOST_AUTO_TEST_CASE (player_3d_test_2)
418 {
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});
422
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);
428
429         make_and_verify_dcp (film);
430
431         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
432         auto film2 = new_test_film2 ("player_3d_test_2b", {dcp});
433
434         make_and_verify_dcp (film2);
435 }
436
437
438 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
439  *  #1691.
440  */
441 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
442 {
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);
449
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});
453
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_")) {
458                         video = i.path();
459                 }
460         }
461
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);
469 }