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