Missing library from test link list.
[dcpomatic.git] / test / player_test.cc
1 /*
2     Copyright (C) 2014-2019 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 /** @file  test/player_test.cc
22  *  @brief Test Player class.
23  *  @ingroup selfcontained
24  */
25
26 #include "lib/film.h"
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"
41 #include "test.h"
42 #include <boost/test/unit_test.hpp>
43 #include <boost/algorithm/string.hpp>
44 #include <iostream>
45
46 using std::cout;
47 using std::list;
48 using std::pair;
49 using boost::shared_ptr;
50 using boost::bind;
51 using boost::optional;
52 using namespace dcpomatic;
53
54 static shared_ptr<AudioBuffers> accumulated;
55
56 static void
57 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
58 {
59         BOOST_REQUIRE (accumulated);
60         accumulated->append (audio);
61 }
62
63 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
64 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
65 {
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);
71
72         film->examine_and_add_content (c);
73         BOOST_REQUIRE (!wait_for_jobs());
74
75         accumulated.reset (new AudioBuffers (film->audio_channels(), 0));
76
77         shared_ptr<Player> player (new Player(film));
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 ());
82
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);
86                 }
87         }
88 }
89
90 /* Test insertion of black frames between separate bits of video content */
91 BOOST_AUTO_TEST_CASE (player_black_fill_test)
92 {
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"));
101
102         film->examine_and_add_content (contentA);
103         film->examine_and_add_content (contentB);
104         BOOST_REQUIRE (!wait_for_jobs());
105
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()));
112
113         film->make_dcp ();
114
115         BOOST_REQUIRE (!wait_for_jobs());
116
117         boost::filesystem::path ref;
118         ref = "test";
119         ref /= "data";
120         ref /= "black_fill_test";
121
122         boost::filesystem::path check;
123         check = "build";
124         check /= "test";
125         check /= "black_fill_test";
126         check /= film->dcp_name();
127
128         check_dcp (ref.string(), check.string());
129 }
130
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)
133 {
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);
146
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));
151
152         shared_ptr<Player> player (new Player(film));
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)));
158 }
159
160 static Frame video_frames;
161 static Frame audio_frames;
162
163 static void
164 video (shared_ptr<PlayerVideo>, DCPTime)
165 {
166         ++video_frames;
167 }
168
169 static void
170 audio (shared_ptr<AudioBuffers> audio, DCPTime)
171 {
172         audio_frames += audio->frames();
173 }
174
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)
177 {
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);
182
183         shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
184         film->examine_and_add_content (c);
185         BOOST_REQUIRE (!wait_for_jobs ());
186
187         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip.srt"));
188         film->examine_and_add_content (s);
189         BOOST_REQUIRE (!wait_for_jobs ());
190
191         shared_ptr<Player> player (new Player(film));
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);
197         }
198 }
199
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
203  *  being faded out.
204  */
205 BOOST_AUTO_TEST_CASE (player_seek_test)
206 {
207         shared_ptr<Film> film (new Film (optional<boost::filesystem::path>()));
208         shared_ptr<DCPContent> dcp (new DCPContent(TestPaths::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);
212
213         shared_ptr<Player> player (new Player(film));
214         player->set_fast ();
215         player->set_always_burn_open_subtitles ();
216         player->set_play_referenced ();
217
218         shared_ptr<Butler> butler (new Butler (player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
219         butler->disable_audio();
220
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
229                    text altogether.
230                 */
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);
232         }
233 }
234
235 /** Test some more seeks towards the start of a DCP with awkward subtitles */
236 BOOST_AUTO_TEST_CASE (player_seek_test2)
237 {
238         shared_ptr<Film> film (new Film (optional<boost::filesystem::path>()));
239         shared_ptr<DCPContent> dcp (new DCPContent(TestPaths::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);
243
244         shared_ptr<Player> player (new Player(film));
245         player->set_fast ();
246         player->set_always_burn_open_subtitles ();
247         player->set_play_referenced ();
248
249         shared_ptr<Butler> butler (new Butler(player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
250         butler->disable_audio();
251
252         butler->seek(DCPTime::from_seconds(5), true);
253
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);
261         }
262 }
263
264 /** Test a bug when trimmed content follows other content */
265 BOOST_AUTO_TEST_CASE (player_trim_test)
266 {
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));
278
279        film->make_dcp ();
280        BOOST_REQUIRE (!wait_for_jobs ());
281 }
282
283 struct Sub {
284         PlayerText text;
285         TextType type;
286         optional<DCPTextTrack> track;
287         DCPTimePeriod period;
288 };
289
290 static void
291 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
292 {
293         Sub s;
294         s.text = text;
295         s.type = type;
296         s.track = track;
297         s.period = period;
298         out->push_back (s);
299 }
300
301 /** Test ignoring both video and audio */
302 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
303 {
304         shared_ptr<Film> film = new_test_film2 ("player_ignore_video_and_audio_test");
305         shared_ptr<Content> ff = content_factory(TestPaths::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);
312
313         shared_ptr<Player> player (new Player(film));
314         player->set_ignore_video ();
315         player->set_ignore_audio ();
316
317         list<Sub> out;
318         player->Text.connect (bind (&store, &out, _1, _2, _3, _4));
319         while (!player->pass ()) {}
320
321         BOOST_CHECK_EQUAL (out.size(), 6);
322 }
323
324 /** Trigger a crash due to the assertion failure in Player::emit_audio */
325 BOOST_AUTO_TEST_CASE (player_trim_crash)
326 {
327         shared_ptr<Film> film = new_test_film2 ("player_trim_crash");
328         shared_ptr<Content> boon = content_factory(TestPaths::private_data / "boon_telly.mkv").front();
329         film->examine_and_add_content (boon);
330         BOOST_REQUIRE (!wait_for_jobs());
331
332         shared_ptr<Player> player (new Player(film));
333         player->set_fast ();
334         shared_ptr<Butler> butler (new Butler(player, AudioMapping(), 6, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
335
336         /* Wait for the butler to fill */
337         dcpomatic_sleep_seconds (5);
338
339         boon->set_trim_start (ContentTime::from_seconds(5));
340
341         butler->seek (DCPTime(), true);
342
343         /* Wait for the butler to refill */
344         dcpomatic_sleep_seconds (5);
345
346         butler->rethrow ();
347 }
348
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)
351 {
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());
356
357         sine->set_video_frame_rate (23.976);
358         film->write_metadata ();
359         film->make_dcp ();
360         BOOST_REQUIRE (!wait_for_jobs());
361 }
362
363 /** Test a crash when processing a 3D DCP */
364 BOOST_AUTO_TEST_CASE (player_3d_test_1)
365 {
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());
372
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);
378
379         film->make_dcp ();
380         BOOST_REQUIRE (!wait_for_jobs());
381
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());
386
387         film2->set_three_d (true);
388         film2->make_dcp ();
389         BOOST_REQUIRE (!wait_for_jobs());
390 }
391
392 /** Test a crash when processing a 3D DCP as content in a 2D project */
393 BOOST_AUTO_TEST_CASE (player_3d_test_2)
394 {
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());
401
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);
407
408         film->make_dcp ();
409         BOOST_REQUIRE (!wait_for_jobs());
410
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());
415
416         film2->make_dcp ();
417         BOOST_REQUIRE (!wait_for_jobs());
418 }
419
420 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
421  *  #1691.
422  */
423 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
424 {
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);
432         film1->make_dcp ();
433         BOOST_REQUIRE (!wait_for_jobs());
434
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());
440
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_")) {
445                         video = i->path();
446                 }
447         }
448
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";
456         film2->make_dcp ();
457         BOOST_REQUIRE (!wait_for_jobs());
458 }