1cab293f17d47462f1956dada63d73c57faaa56c
[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/audio_buffers.h"
29 #include "lib/butler.h"
30 #include "lib/compose.hpp"
31 #include "lib/config.h"
32 #include "lib/content_factory.h"
33 #include "lib/cross.h"
34 #include "lib/dcp_content.h"
35 #include "lib/dcp_content_type.h"
36 #include "lib/dcpomatic_log.h"
37 #include "lib/ffmpeg_content.h"
38 #include "lib/film.h"
39 #include "lib/image_content.h"
40 #include "lib/player.h"
41 #include "lib/ratio.h"
42 #include "lib/string_text_file_content.h"
43 #include "lib/text_content.h"
44 #include "lib/video_content.h"
45 #include "test.h"
46 #include <boost/test/unit_test.hpp>
47 #include <boost/algorithm/string.hpp>
48 #include <iostream>
49
50
51 using std::cout;
52 using std::list;
53 using std::shared_ptr;
54 using std::make_shared;
55 using boost::bind;
56 using boost::optional;
57 #if BOOST_VERSION >= 106100
58 using namespace boost::placeholders;
59 #endif
60 using namespace dcpomatic;
61
62
63 static shared_ptr<AudioBuffers> accumulated;
64
65
66 static void
67 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
68 {
69         BOOST_REQUIRE (accumulated);
70         accumulated->append (audio);
71 }
72
73
74 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
75 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
76 {
77         auto film = new_test_film ("player_silence_padding_test");
78         film->set_name ("player_silence_padding_test");
79         auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
80         film->set_container (Ratio::from_id ("185"));
81         film->set_audio_channels (6);
82
83         film->examine_and_add_content (c);
84         BOOST_REQUIRE (!wait_for_jobs());
85
86         accumulated = std::make_shared<AudioBuffers>(film->audio_channels(), 0);
87
88         Player player(film, Image::Alignment::COMPACT);
89         player.Audio.connect(bind(&accumulate, _1, _2));
90         while (!player.pass()) {}
91         BOOST_REQUIRE (accumulated->frames() >= 48000);
92         BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
93
94         for (int i = 0; i < 48000; ++i) {
95                 for (int c = 0; c < accumulated->channels(); ++c) {
96                         BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
97                 }
98         }
99 }
100
101
102 /* Test insertion of black frames between separate bits of video content */
103 BOOST_AUTO_TEST_CASE (player_black_fill_test)
104 {
105         auto film = new_test_film ("black_fill_test");
106         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
107         film->set_name ("black_fill_test");
108         film->set_container (Ratio::from_id ("185"));
109         film->set_sequence (false);
110         film->set_interop (false);
111         auto contentA = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
112         auto contentB = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
113
114         film->examine_and_add_content (contentA);
115         film->examine_and_add_content (contentB);
116         BOOST_REQUIRE (!wait_for_jobs());
117
118         contentA->video->set_length (3);
119         contentA->set_position (film, DCPTime::from_frames(2, film->video_frame_rate()));
120         contentA->video->set_custom_ratio (1.85);
121         contentB->video->set_length (1);
122         contentB->set_position (film, DCPTime::from_frames(7, film->video_frame_rate()));
123         contentB->video->set_custom_ratio (1.85);
124
125         make_and_verify_dcp (
126                 film,
127                 {
128                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
129                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
130                 });
131
132         boost::filesystem::path ref;
133         ref = "test";
134         ref /= "data";
135         ref /= "black_fill_test";
136
137         boost::filesystem::path check;
138         check = "build";
139         check /= "test";
140         check /= "black_fill_test";
141         check /= film->dcp_name();
142
143         check_dcp (ref.string(), check.string());
144 }
145
146
147 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
148 BOOST_AUTO_TEST_CASE (player_subframe_test)
149 {
150         auto film = new_test_film ("reels_test7");
151         film->set_name ("reels_test7");
152         film->set_container (Ratio::from_id("185"));
153         film->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
154         auto A = content_factory("test/data/flat_red.png")[0];
155         film->examine_and_add_content (A);
156         BOOST_REQUIRE (!wait_for_jobs());
157         auto B = content_factory("test/data/awkward_length.wav")[0];
158         film->examine_and_add_content (B);
159         BOOST_REQUIRE (!wait_for_jobs());
160         film->set_video_frame_rate (24);
161         A->video->set_length (3 * 24);
162
163         BOOST_CHECK (A->full_length(film) == DCPTime::from_frames(3 * 24, 24));
164         BOOST_CHECK (B->full_length(film) == DCPTime(289920));
165         /* Length should be rounded up from B's length to the next video frame */
166         BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
167
168         Player player(film, Image::Alignment::COMPACT);
169         player.setup_pieces();
170         BOOST_REQUIRE_EQUAL(player._black._periods.size(), 1U);
171         BOOST_CHECK(player._black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
172         BOOST_REQUIRE_EQUAL(player._silent._periods.size(), 1U);
173         BOOST_CHECK(player._silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
174 }
175
176
177 static Frame video_frames;
178 static Frame audio_frames;
179
180
181 static void
182 video (shared_ptr<PlayerVideo>, DCPTime)
183 {
184         ++video_frames;
185 }
186
187 static void
188 audio (shared_ptr<AudioBuffers> audio, DCPTime)
189 {
190         audio_frames += audio->frames();
191 }
192
193
194 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
195 BOOST_AUTO_TEST_CASE (player_interleave_test)
196 {
197         auto film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
198         film->set_name ("ffmpeg_transcoder_basic_test");
199         film->set_container (Ratio::from_id ("185"));
200         film->set_audio_channels (6);
201
202         auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
203         film->examine_and_add_content (c);
204         BOOST_REQUIRE (!wait_for_jobs ());
205
206         auto s = std::make_shared<StringTextFileContent>("test/data/subrip.srt");
207         film->examine_and_add_content (s);
208         BOOST_REQUIRE (!wait_for_jobs ());
209
210         Player player(film, Image::Alignment::COMPACT);
211         player.Video.connect(bind(&video, _1, _2));
212         player.Audio.connect(bind(&audio, _1, _2));
213         video_frames = audio_frames = 0;
214         while (!player.pass()) {
215                 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) <= 8);
216         }
217 }
218
219
220 /** Test some seeks towards the start of a DCP with awkward subtitles; see mantis #1085
221  *  and a number of others.  I thought this was a player seek bug but in fact it was
222  *  caused by the subtitle starting just after the start of the video frame and hence
223  *  being faded out.
224  */
225 BOOST_AUTO_TEST_CASE (player_seek_test)
226 {
227         auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
228         auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs");
229         film->examine_and_add_content (dcp, true);
230         BOOST_REQUIRE (!wait_for_jobs ());
231         dcp->only_text()->set_use (true);
232
233         Player player(film, Image::Alignment::COMPACT);
234         player.set_fast();
235         player.set_always_burn_open_subtitles();
236         player.set_play_referenced();
237
238         auto butler = std::make_shared<Butler>(
239                 film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
240                 );
241
242         for (int i = 0; i < 10; ++i) {
243                 auto t = DCPTime::from_frames (i, 24);
244                 butler->seek (t, true);
245                 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
246                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
247                 write_image(video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test_%1.png", i));
248                 /* This 14.08 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
249                    (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
250                    text altogether.
251                 */
252                 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);
253         }
254 }
255
256
257 /** Test some more seeks towards the start of a DCP with awkward subtitles */
258 BOOST_AUTO_TEST_CASE (player_seek_test2)
259 {
260         auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
261         auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs2");
262         film->examine_and_add_content (dcp, true);
263         BOOST_REQUIRE (!wait_for_jobs ());
264         dcp->only_text()->set_use (true);
265
266         Player player(film, Image::Alignment::COMPACT);
267         player.set_fast();
268         player.set_always_burn_open_subtitles();
269         player.set_play_referenced();
270
271         auto butler = std::make_shared<Butler>
272                 (film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
273                  );
274
275         butler->seek(DCPTime::from_seconds(5), true);
276
277         for (int i = 0; i < 10; ++i) {
278                 auto t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
279                 butler->seek (t, true);
280                 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
281                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
282                 write_image(
283                         video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test2_%1.png", i)
284                         );
285                 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         }
287 }
288
289
290 /** Test a bug when trimmed content follows other content */
291 BOOST_AUTO_TEST_CASE (player_trim_test)
292 {
293        auto film = new_test_film2 ("player_trim_test");
294        auto A = content_factory("test/data/flat_red.png")[0];
295        film->examine_and_add_content (A);
296        BOOST_REQUIRE (!wait_for_jobs ());
297        A->video->set_length (10 * 24);
298        auto B = content_factory("test/data/flat_red.png")[0];
299        film->examine_and_add_content (B);
300        BOOST_REQUIRE (!wait_for_jobs ());
301        B->video->set_length (10 * 24);
302        B->set_position (film, DCPTime::from_seconds(10));
303        B->set_trim_start (ContentTime::from_seconds (2));
304
305        make_and_verify_dcp (film);
306 }
307
308
309 struct Sub {
310         PlayerText text;
311         TextType type;
312         optional<DCPTextTrack> track;
313         DCPTimePeriod period;
314 };
315
316
317 static void
318 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
319 {
320         Sub s;
321         s.text = text;
322         s.type = type;
323         s.track = track;
324         s.period = period;
325         out->push_back (s);
326 }
327
328
329 /** Test ignoring both video and audio */
330 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
331 {
332         auto film = new_test_film2 ("player_ignore_video_and_audio_test");
333         auto ff = content_factory(TestPaths::private_data() / "boon_telly.mkv")[0];
334         film->examine_and_add_content (ff);
335         auto text = content_factory("test/data/subrip.srt")[0];
336         film->examine_and_add_content (text);
337         BOOST_REQUIRE (!wait_for_jobs());
338         text->only_text()->set_type (TextType::CLOSED_CAPTION);
339         text->only_text()->set_use (true);
340
341         Player player(film, Image::Alignment::COMPACT);
342         player.set_ignore_video();
343         player.set_ignore_audio();
344
345         list<Sub> out;
346         player.Text.connect(bind (&store, &out, _1, _2, _3, _4));
347         while (!player.pass()) {}
348
349         BOOST_CHECK_EQUAL (out.size(), 6U);
350 }
351
352
353 /** Trigger a crash due to the assertion failure in Player::emit_audio */
354 BOOST_AUTO_TEST_CASE (player_trim_crash)
355 {
356         auto film = new_test_film2 ("player_trim_crash");
357         auto boon = content_factory(TestPaths::private_data() / "boon_telly.mkv")[0];
358         film->examine_and_add_content (boon);
359         BOOST_REQUIRE (!wait_for_jobs());
360
361         Player player(film, Image::Alignment::COMPACT);
362         player.set_fast();
363         auto butler = std::make_shared<Butler>(
364                 film, player, AudioMapping(), 6, bind(&PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::COMPACT, true, false, Butler::Audio::ENABLED
365                 );
366
367         /* Wait for the butler to fill */
368         dcpomatic_sleep_seconds (5);
369
370         boon->set_trim_start (ContentTime::from_seconds(5));
371
372         butler->seek (DCPTime(), true);
373
374         /* Wait for the butler to refill */
375         dcpomatic_sleep_seconds (5);
376
377         butler->rethrow ();
378 }
379
380
381 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
382 BOOST_AUTO_TEST_CASE (player_silence_crash)
383 {
384         auto film = new_test_film2 ("player_silence_crash");
385         auto sine = content_factory("test/data/impulse_train.wav")[0];
386         film->examine_and_add_content (sine);
387         BOOST_REQUIRE (!wait_for_jobs());
388
389         sine->set_video_frame_rate (23.976);
390         film->write_metadata ();
391         make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
392 }
393
394
395 /** Test a crash when processing a 3D DCP */
396 BOOST_AUTO_TEST_CASE (player_3d_test_1)
397 {
398         auto film = new_test_film2 ("player_3d_test_1a");
399         auto left = content_factory("test/data/flat_red.png")[0];
400         film->examine_and_add_content (left);
401         auto right = content_factory("test/data/flat_blue.png")[0];
402         film->examine_and_add_content (right);
403         BOOST_REQUIRE (!wait_for_jobs());
404
405         left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
406         left->set_position (film, DCPTime());
407         right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
408         right->set_position (film, DCPTime());
409         film->set_three_d (true);
410
411         make_and_verify_dcp (film);
412
413         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
414         auto film2 = new_test_film2 ("player_3d_test_1b", {dcp});
415
416         film2->set_three_d (true);
417         make_and_verify_dcp (film2);
418 }
419
420
421 /** Test a crash when processing a 3D DCP as content in a 2D project */
422 BOOST_AUTO_TEST_CASE (player_3d_test_2)
423 {
424         auto left = content_factory("test/data/flat_red.png")[0];
425         auto right = content_factory("test/data/flat_blue.png")[0];
426         auto film = new_test_film2 ("player_3d_test_2a", {left, right});
427
428         left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
429         left->set_position (film, DCPTime());
430         right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
431         right->set_position (film, DCPTime());
432         film->set_three_d (true);
433
434         make_and_verify_dcp (film);
435
436         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
437         auto film2 = new_test_film2 ("player_3d_test_2b", {dcp});
438
439         make_and_verify_dcp (film2);
440 }
441
442
443 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
444  *  #1691.
445  */
446 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
447 {
448         /* 25fps DCP with some audio */
449         auto content1 = content_factory("test/data/flat_red.png")[0];
450         auto film1 = new_test_film2 ("player_silence_at_end_crash_1", {content1});
451         content1->video->set_length (25);
452         film1->set_video_frame_rate (25);
453         make_and_verify_dcp (film1);
454
455         /* Make another project importing this DCP */
456         auto content2 = std::make_shared<DCPContent>(film1->dir(film1->dcp_name()));
457         auto film2 = new_test_film2 ("player_silence_at_end_crash_2", {content2});
458
459         /* and importing just the video MXF on its own at the end */
460         optional<boost::filesystem::path> video;
461         for (auto i: boost::filesystem::directory_iterator(film1->dir(film1->dcp_name()))) {
462                 if (boost::starts_with(i.path().filename().string(), "j2c_")) {
463                         video = i.path();
464                 }
465         }
466
467         BOOST_REQUIRE (video);
468         auto content3 = content_factory(*video)[0];
469         film2->examine_and_add_content (content3);
470         BOOST_REQUIRE (!wait_for_jobs());
471         content3->set_position (film2, DCPTime::from_seconds(1.5));
472         film2->set_video_frame_rate (24);
473         make_and_verify_dcp (film2);
474 }
475
476
477 /** #2257 */
478 BOOST_AUTO_TEST_CASE (encrypted_dcp_with_no_kdm_gives_no_butler_error)
479 {
480         auto content = content_factory("test/data/flat_red.png")[0];
481         auto film = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error", { content });
482         int constexpr length = 24 * 25;
483         content->video->set_length(length);
484         film->set_encrypted (true);
485         make_and_verify_dcp (
486                 film,
487                 {
488                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
489                 });
490
491         auto content2 = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
492         auto film2 = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error2", { content2 });
493
494         Player player(film, Image::Alignment::COMPACT);
495         Butler butler(film2, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::ENABLED);
496
497         float buffer[2000 * 6];
498         for (int i = 0; i < length; ++i) {
499                 butler.get_video(Butler::Behaviour::BLOCKING, 0);
500                 butler.get_audio(Butler::Behaviour::BLOCKING, buffer, 2000);
501         }
502
503         BOOST_CHECK_NO_THROW(butler.rethrow());
504 }
505
506
507 BOOST_AUTO_TEST_CASE (interleaved_subtitle_are_emitted_correctly)
508 {
509         boost::filesystem::path paths[2] = {
510                 "build/test/interleaved_subtitle_are_emitted_correctly1.srt",
511                 "build/test/interleaved_subtitle_are_emitted_correctly2.srt"
512         };
513
514         dcp::File subs_file[2] = { dcp::File(paths[0], "w"), dcp::File(paths[1], "w") };
515
516         fprintf(subs_file[0].get(), "1\n00:00:01,000 -> 00:00:02,000\nSub 1/1\n\n");
517         fprintf(subs_file[0].get(), "2\n00:00:05,000 -> 00:00:06,000\nSub 1/2\n\n");
518
519         fprintf(subs_file[1].get(), "1\n00:00:00,500 -> 00:00:01,500\nSub 2/1\n\n");
520         fprintf(subs_file[1].get(), "2\n00:00:02,000 -> 00:00:03,000\nSub 2/2\n\n");
521
522         subs_file[0].close();
523         subs_file[1].close();
524
525         auto subs1 = content_factory(paths[0])[0];
526         auto subs2 = content_factory(paths[1])[0];
527         auto film = new_test_film2("interleaved_subtitle_are_emitted_correctly", { subs1, subs2 });
528         film->set_sequence(false);
529         subs1->set_position(film, DCPTime());
530         subs2->set_position(film, DCPTime());
531
532         Player player(film, Image::Alignment::COMPACT);
533         dcp::Time last;
534         player.Text.connect([&last](PlayerText text, TextType, optional<DCPTextTrack>, dcpomatic::DCPTimePeriod) {
535                 for (auto sub: text.string) {
536                         BOOST_CHECK(sub.in() >= last);
537                         last = sub.in();
538                 }
539         });
540         while (!player.pass()) {}
541 }
542
543
544 BOOST_AUTO_TEST_CASE(multiple_sound_files_bug)
545 {
546         Config::instance()->set_log_types(Config::instance()->log_types() | LogEntry::TYPE_DEBUG_PLAYER);
547
548         auto A = content_factory(TestPaths::private_data() / "kook" / "1.wav").front();
549         auto B = content_factory(TestPaths::private_data() / "kook" / "2.wav").front();
550         auto C = content_factory(TestPaths::private_data() / "kook" / "3.wav").front();
551
552         auto film = new_test_film2("multiple_sound_files_bug", { A, B, C });
553         C->set_position(film, DCPTime(3840000));
554
555         make_and_verify_dcp(film, { dcp::VerificationNote::Code::MISSING_CPL_METADATA });
556
557         check_mxf_audio_file(TestPaths::private_data() / "kook" / "reference.mxf", dcp_file(film, "pcm_"));
558 }
559