6812ddbb06a4bb72b4ed688b222eb66f4bd4da06
[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 <iostream>
44
45 using std::cout;
46 using std::list;
47 using std::pair;
48 using boost::shared_ptr;
49 using boost::bind;
50 using boost::optional;
51
52 static shared_ptr<AudioBuffers> accumulated;
53
54 static void
55 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
56 {
57         BOOST_REQUIRE (accumulated);
58         accumulated->append (audio);
59 }
60
61 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
62 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
63 {
64         shared_ptr<Film> film = new_test_film ("player_silence_padding_test");
65         film->set_name ("player_silence_padding_test");
66         shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
67         film->set_container (Ratio::from_id ("185"));
68         film->set_audio_channels (6);
69
70         film->examine_and_add_content (c);
71         BOOST_REQUIRE (!wait_for_jobs());
72
73         accumulated.reset (new AudioBuffers (film->audio_channels(), 0));
74
75         shared_ptr<Player> player (new Player (film, film->playlist ()));
76         player->Audio.connect (bind (&accumulate, _1, _2));
77         while (!player->pass ()) {}
78         BOOST_REQUIRE (accumulated->frames() >= 48000);
79         BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
80
81         for (int i = 0; i < 48000; ++i) {
82                 for (int c = 0; c < accumulated->channels(); ++c) {
83                         BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
84                 }
85         }
86 }
87
88 /* Test insertion of black frames between separate bits of video content */
89 BOOST_AUTO_TEST_CASE (player_black_fill_test)
90 {
91         shared_ptr<Film> film = new_test_film ("black_fill_test");
92         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
93         film->set_name ("black_fill_test");
94         film->set_container (Ratio::from_id ("185"));
95         film->set_sequence (false);
96         film->set_interop (false);
97         shared_ptr<ImageContent> contentA (new ImageContent("test/data/simple_testcard_640x480.png"));
98         shared_ptr<ImageContent> contentB (new ImageContent("test/data/simple_testcard_640x480.png"));
99
100         film->examine_and_add_content (contentA);
101         film->examine_and_add_content (contentB);
102         BOOST_REQUIRE (!wait_for_jobs());
103
104         contentA->video->set_scale (VideoContentScale (Ratio::from_id ("185")));
105         contentA->video->set_length (3);
106         contentA->set_position (film, DCPTime::from_frames(2, film->video_frame_rate()));
107         contentB->video->set_scale (VideoContentScale (Ratio::from_id ("185")));
108         contentB->video->set_length (1);
109         contentB->set_position (film, DCPTime::from_frames(7, film->video_frame_rate()));
110
111         film->make_dcp ();
112
113         BOOST_REQUIRE (!wait_for_jobs());
114
115         boost::filesystem::path ref;
116         ref = "test";
117         ref /= "data";
118         ref /= "black_fill_test";
119
120         boost::filesystem::path check;
121         check = "build";
122         check /= "test";
123         check /= "black_fill_test";
124         check /= film->dcp_name();
125
126         check_dcp (ref.string(), check.string());
127 }
128
129 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
130 BOOST_AUTO_TEST_CASE (player_subframe_test)
131 {
132         shared_ptr<Film> film = new_test_film ("reels_test7");
133         film->set_name ("reels_test7");
134         film->set_container (Ratio::from_id ("185"));
135         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
136         shared_ptr<Content> A = content_factory("test/data/flat_red.png").front();
137         film->examine_and_add_content (A);
138         BOOST_REQUIRE (!wait_for_jobs ());
139         shared_ptr<Content> B = content_factory("test/data/awkward_length.wav").front();
140         film->examine_and_add_content (B);
141         BOOST_REQUIRE (!wait_for_jobs ());
142         film->set_video_frame_rate (24);
143         A->video->set_length (3 * 24);
144
145         BOOST_CHECK (A->full_length(film) == DCPTime::from_frames(3 * 24, 24));
146         BOOST_CHECK (B->full_length(film) == DCPTime(289920));
147         /* Length should be rounded up from B's length to the next video frame */
148         BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
149
150         shared_ptr<Player> player (new Player (film, film->playlist ()));
151         player->setup_pieces ();
152         BOOST_REQUIRE_EQUAL (player->_black._periods.size(), 1);
153         BOOST_CHECK (player->_black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
154         BOOST_REQUIRE_EQUAL (player->_silent._periods.size(), 1);
155         BOOST_CHECK (player->_silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
156 }
157
158 static Frame video_frames;
159 static Frame audio_frames;
160
161 static void
162 video (shared_ptr<PlayerVideo>, DCPTime)
163 {
164         ++video_frames;
165 }
166
167 static void
168 audio (shared_ptr<AudioBuffers> audio, DCPTime)
169 {
170         audio_frames += audio->frames();
171 }
172
173 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
174 BOOST_AUTO_TEST_CASE (player_interleave_test)
175 {
176         shared_ptr<Film> film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
177         film->set_name ("ffmpeg_transcoder_basic_test");
178         film->set_container (Ratio::from_id ("185"));
179         film->set_audio_channels (6);
180
181         shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
182         film->examine_and_add_content (c);
183         BOOST_REQUIRE (!wait_for_jobs ());
184
185         shared_ptr<StringTextFileContent> s (new StringTextFileContent("test/data/subrip.srt"));
186         film->examine_and_add_content (s);
187         BOOST_REQUIRE (!wait_for_jobs ());
188
189         shared_ptr<Player> player (new Player(film, film->playlist()));
190         player->Video.connect (bind (&video, _1, _2));
191         player->Audio.connect (bind (&audio, _1, _2));
192         video_frames = audio_frames = 0;
193         while (!player->pass ()) {
194                 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) < 8);
195         }
196 }
197
198 /** Test some seeks towards the start of a DCP with awkward subtitles; see mantis #1085
199  *  and a number of others.  I thought this was a player seek bug but in fact it was
200  *  caused by the subtitle starting just after the start of the video frame and hence
201  *  being faded out.
202  */
203 BOOST_AUTO_TEST_CASE (player_seek_test)
204 {
205         shared_ptr<Film> film (new Film (optional<boost::filesystem::path>()));
206         shared_ptr<DCPContent> dcp (new DCPContent(private_data / "awkward_subs"));
207         film->examine_and_add_content (dcp, true);
208         BOOST_REQUIRE (!wait_for_jobs ());
209         dcp->only_text()->set_use (true);
210
211         shared_ptr<Player> player (new Player (film, film->playlist()));
212         player->set_fast ();
213         player->set_always_burn_open_subtitles ();
214         player->set_play_referenced ();
215
216         shared_ptr<Butler> butler (new Butler (player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
217         butler->disable_audio();
218
219         for (int i = 0; i < 10; ++i) {
220                 DCPTime t = DCPTime::from_frames (i, 24);
221                 butler->seek (t, true);
222                 pair<shared_ptr<PlayerVideo>, DCPTime> video = butler->get_video();
223                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
224                 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");
225                 /* This 0.055 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
226                    (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
227                    text altogether.
228                 */
229                 check_image(String::compose("test/data/player_seek_test_%1.png", i), String::compose("build/test/player_seek_test_%1.png", i), 0.055);
230         }
231 }
232
233 /** Test some more seeks towards the start of a DCP with awkward subtitles */
234 BOOST_AUTO_TEST_CASE (player_seek_test2)
235 {
236         shared_ptr<Film> film (new Film (optional<boost::filesystem::path>()));
237         shared_ptr<DCPContent> dcp (new DCPContent(private_data / "awkward_subs2"));
238         film->examine_and_add_content (dcp, true);
239         BOOST_REQUIRE (!wait_for_jobs ());
240         dcp->only_text()->set_use (true);
241
242         shared_ptr<Player> player (new Player (film, film->playlist()));
243         player->set_fast ();
244         player->set_always_burn_open_subtitles ();
245         player->set_play_referenced ();
246
247         shared_ptr<Butler> butler (new Butler(player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
248         butler->disable_audio();
249
250         butler->seek(DCPTime::from_seconds(5), true);
251
252         for (int i = 0; i < 10; ++i) {
253                 DCPTime t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
254                 butler->seek (t, true);
255                 pair<shared_ptr<PlayerVideo>, DCPTime> video = butler->get_video();
256                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
257                 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");
258                 check_image(String::compose("test/data/player_seek_test2_%1.png", i), String::compose("build/test/player_seek_test2_%1.png", i), 0.055);
259         }
260 }
261
262 /** Test a bug when trimmed content follows other content */
263 BOOST_AUTO_TEST_CASE (player_trim_test)
264 {
265        shared_ptr<Film> film = new_test_film2 ("player_trim_test");
266        shared_ptr<Content> A = content_factory("test/data/flat_red.png").front();
267        film->examine_and_add_content (A);
268        BOOST_REQUIRE (!wait_for_jobs ());
269        A->video->set_length (10 * 24);
270        shared_ptr<Content> B = content_factory("test/data/flat_red.png").front();
271        film->examine_and_add_content (B);
272        BOOST_REQUIRE (!wait_for_jobs ());
273        B->video->set_length (10 * 24);
274        B->set_position (film, DCPTime::from_seconds(10));
275        B->set_trim_start (ContentTime::from_seconds (2));
276
277        film->make_dcp ();
278        BOOST_REQUIRE (!wait_for_jobs ());
279 }
280
281 struct Sub {
282         PlayerText text;
283         TextType type;
284         optional<DCPTextTrack> track;
285         DCPTimePeriod period;
286 };
287
288 static void
289 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
290 {
291         Sub s;
292         s.text = text;
293         s.type = type;
294         s.track = track;
295         s.period = period;
296         out->push_back (s);
297 }
298
299 /** Test ignoring both video and audio */
300 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
301 {
302         shared_ptr<Film> film = new_test_film2 ("player_ignore_video_and_audio_test");
303         shared_ptr<Content> ff = content_factory(private_data / "boon_telly.mkv").front();
304         film->examine_and_add_content (ff);
305         shared_ptr<Content> text = content_factory("test/data/subrip.srt").front();
306         film->examine_and_add_content (text);
307         BOOST_REQUIRE (!wait_for_jobs());
308         text->only_text()->set_type (TEXT_CLOSED_CAPTION);
309         text->only_text()->set_use (true);
310
311         shared_ptr<Player> player (new Player(film, film->playlist()));
312         player->set_ignore_video ();
313         player->set_ignore_audio ();
314
315         list<Sub> out;
316         player->Text.connect (bind (&store, &out, _1, _2, _3, _4));
317         while (!player->pass ()) {}
318
319         BOOST_CHECK_EQUAL (out.size(), 6);
320 }
321
322 /** Trigger a crash due to the assertion failure in Player::emit_audio */
323 BOOST_AUTO_TEST_CASE (player_trim_crash)
324 {
325         shared_ptr<Film> film = new_test_film2 ("player_trim_crash");
326         shared_ptr<Content> boon = content_factory(private_data / "boon_telly.mkv").front();
327         film->examine_and_add_content (boon);
328         BOOST_REQUIRE (!wait_for_jobs());
329
330         shared_ptr<Player> player (new Player(film, film->playlist()));
331         player->set_fast ();
332         shared_ptr<Butler> butler (new Butler(player, AudioMapping(), 6, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
333
334         /* Wait for the butler to fill */
335         dcpomatic_sleep (5);
336
337         boon->set_trim_start (ContentTime::from_seconds(5));
338
339         butler->seek (DCPTime(), true);
340
341         /* Wait for the butler to refill */
342         dcpomatic_sleep (5);
343
344         butler->rethrow ();
345 }
346
347 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
348 BOOST_AUTO_TEST_CASE (player_silence_crash)
349 {
350         shared_ptr<Film> film = new_test_film2 ("player_silence_crash");
351         shared_ptr<Content> sine = content_factory("test/data/impulse_train.wav").front();
352         film->examine_and_add_content (sine);
353         BOOST_REQUIRE (!wait_for_jobs());
354
355         sine->set_video_frame_rate (23.976);
356         film->write_metadata ();
357         film->make_dcp ();
358         BOOST_REQUIRE (!wait_for_jobs());
359 }