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