std::shared_ptr
[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 std::shared_ptr;
50 using boost::bind;
51 using boost::optional;
52 #if BOOST_VERSION >= 106100
53 using namespace boost::placeholders;
54 #endif
55 using namespace dcpomatic;
56
57 static shared_ptr<AudioBuffers> accumulated;
58
59 static void
60 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
61 {
62         BOOST_REQUIRE (accumulated);
63         accumulated->append (audio);
64 }
65
66 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
67 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
68 {
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);
74
75         film->examine_and_add_content (c);
76         BOOST_REQUIRE (!wait_for_jobs());
77
78         accumulated.reset (new AudioBuffers (film->audio_channels(), 0));
79
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 ());
85
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);
89                 }
90         }
91 }
92
93 /* Test insertion of black frames between separate bits of video content */
94 BOOST_AUTO_TEST_CASE (player_black_fill_test)
95 {
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"));
104
105         film->examine_and_add_content (contentA);
106         film->examine_and_add_content (contentB);
107         BOOST_REQUIRE (!wait_for_jobs());
108
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);
115
116         film->make_dcp ();
117
118         BOOST_REQUIRE (!wait_for_jobs());
119
120         boost::filesystem::path ref;
121         ref = "test";
122         ref /= "data";
123         ref /= "black_fill_test";
124
125         boost::filesystem::path check;
126         check = "build";
127         check /= "test";
128         check /= "black_fill_test";
129         check /= film->dcp_name();
130
131         check_dcp (ref.string(), check.string());
132 }
133
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)
136 {
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);
149
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));
154
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)));
161 }
162
163 static Frame video_frames;
164 static Frame audio_frames;
165
166 static void
167 video (shared_ptr<PlayerVideo>, DCPTime)
168 {
169         ++video_frames;
170 }
171
172 static void
173 audio (shared_ptr<AudioBuffers> audio, DCPTime)
174 {
175         audio_frames += audio->frames();
176 }
177
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)
180 {
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);
185
186         shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
187         film->examine_and_add_content (c);
188         BOOST_REQUIRE (!wait_for_jobs ());
189
190         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip.srt"));
191         film->examine_and_add_content (s);
192         BOOST_REQUIRE (!wait_for_jobs ());
193
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);
200         }
201 }
202
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
206  *  being faded out.
207  */
208 BOOST_AUTO_TEST_CASE (player_seek_test)
209 {
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);
215
216         shared_ptr<Player> player (new Player(film));
217         player->set_fast ();
218         player->set_always_burn_open_subtitles ();
219         player->set_play_referenced ();
220
221         shared_ptr<Butler> butler (new Butler (film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VIDEO_RANGE_FULL, false, true));
222         butler->disable_audio();
223
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), VIDEO_RANGE_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
232                    text altogether.
233                 */
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);
235         }
236 }
237
238 /** Test some more seeks towards the start of a DCP with awkward subtitles */
239 BOOST_AUTO_TEST_CASE (player_seek_test2)
240 {
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);
246
247         shared_ptr<Player> player (new Player(film));
248         player->set_fast ();
249         player->set_always_burn_open_subtitles ();
250         player->set_play_referenced ();
251
252         shared_ptr<Butler> butler (new Butler(film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VIDEO_RANGE_FULL, false, true));
253         butler->disable_audio();
254
255         butler->seek(DCPTime::from_seconds(5), true);
256
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());
262                 write_image(
263                         video.first->image(bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VIDEO_RANGE_FULL, false, true), String::compose("build/test/player_seek_test2_%1.png", i)
264                         );
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);
266         }
267 }
268
269 /** Test a bug when trimmed content follows other content */
270 BOOST_AUTO_TEST_CASE (player_trim_test)
271 {
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));
283
284        film->make_dcp ();
285        BOOST_REQUIRE (!wait_for_jobs ());
286 }
287
288 struct Sub {
289         PlayerText text;
290         TextType type;
291         optional<DCPTextTrack> track;
292         DCPTimePeriod period;
293 };
294
295 static void
296 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
297 {
298         Sub s;
299         s.text = text;
300         s.type = type;
301         s.track = track;
302         s.period = period;
303         out->push_back (s);
304 }
305
306 /** Test ignoring both video and audio */
307 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
308 {
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 (TEXT_CLOSED_CAPTION);
316         text->only_text()->set_use (true);
317
318         shared_ptr<Player> player (new Player(film));
319         player->set_ignore_video ();
320         player->set_ignore_audio ();
321
322         list<Sub> out;
323         player->Text.connect (bind (&store, &out, _1, _2, _3, _4));
324         while (!player->pass ()) {}
325
326         BOOST_CHECK_EQUAL (out.size(), 6U);
327 }
328
329 /** Trigger a crash due to the assertion failure in Player::emit_audio */
330 BOOST_AUTO_TEST_CASE (player_trim_crash)
331 {
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());
336
337         shared_ptr<Player> player (new Player(film));
338         player->set_fast ();
339         shared_ptr<Butler> butler (new Butler(film, player, AudioMapping(), 6, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VIDEO_RANGE_FULL, false, true));
340
341         /* Wait for the butler to fill */
342         dcpomatic_sleep_seconds (5);
343
344         boon->set_trim_start (ContentTime::from_seconds(5));
345
346         butler->seek (DCPTime(), true);
347
348         /* Wait for the butler to refill */
349         dcpomatic_sleep_seconds (5);
350
351         butler->rethrow ();
352 }
353
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)
356 {
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());
361
362         sine->set_video_frame_rate (23.976);
363         film->write_metadata ();
364         film->make_dcp ();
365         BOOST_REQUIRE (!wait_for_jobs());
366 }
367
368 /** Test a crash when processing a 3D DCP */
369 BOOST_AUTO_TEST_CASE (player_3d_test_1)
370 {
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());
377
378         left->video->set_frame_type (VIDEO_FRAME_TYPE_3D_LEFT);
379         left->set_position (film, DCPTime());
380         right->video->set_frame_type (VIDEO_FRAME_TYPE_3D_RIGHT);
381         right->set_position (film, DCPTime());
382         film->set_three_d (true);
383
384         film->make_dcp ();
385         BOOST_REQUIRE (!wait_for_jobs());
386
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());
391
392         film2->set_three_d (true);
393         film2->make_dcp ();
394         BOOST_REQUIRE (!wait_for_jobs());
395 }
396
397 /** Test a crash when processing a 3D DCP as content in a 2D project */
398 BOOST_AUTO_TEST_CASE (player_3d_test_2)
399 {
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());
406
407         left->video->set_frame_type (VIDEO_FRAME_TYPE_3D_LEFT);
408         left->set_position (film, DCPTime());
409         right->video->set_frame_type (VIDEO_FRAME_TYPE_3D_RIGHT);
410         right->set_position (film, DCPTime());
411         film->set_three_d (true);
412
413         film->make_dcp ();
414         BOOST_REQUIRE (!wait_for_jobs());
415
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());
420
421         film2->make_dcp ();
422         BOOST_REQUIRE (!wait_for_jobs());
423 }
424
425 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
426  *  #1691.
427  */
428 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
429 {
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);
437         film1->make_dcp ();
438         BOOST_REQUIRE (!wait_for_jobs());
439
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());
445
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_")) {
450                         video = i->path();
451                 }
452         }
453
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);
460         film2->make_dcp ();
461         BOOST_REQUIRE (!wait_for_jobs());
462 }