Add a new test.
[dcpomatic.git] / test / player_test.cc
1 /*
2     Copyright (C) 2014 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/text_subtitle_content.h"
35 #include "lib/content_factory.h"
36 #include "test.h"
37 #include <boost/test/unit_test.hpp>
38 #include <iostream>
39
40 using std::cout;
41 using std::list;
42 using boost::shared_ptr;
43 using boost::bind;
44
45 static shared_ptr<AudioBuffers> accumulated;
46
47 static void
48 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
49 {
50         BOOST_REQUIRE (accumulated);
51         accumulated->append (audio);
52 }
53
54 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
55 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
56 {
57         shared_ptr<Film> film = new_test_film ("player_silence_padding_test");
58         film->set_name ("player_silence_padding_test");
59         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/test.mp4"));
60         film->set_container (Ratio::from_id ("185"));
61         film->set_audio_channels (6);
62
63         film->examine_and_add_content (c);
64         wait_for_jobs ();
65
66         accumulated.reset (new AudioBuffers (film->audio_channels(), 0));
67
68         shared_ptr<Player> player (new Player (film, film->playlist ()));
69         player->Audio.connect (bind (&accumulate, _1, _2));
70         while (!player->pass ()) {}
71         BOOST_REQUIRE (accumulated->frames() >= 48000);
72         BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
73
74         for (int i = 0; i < 48000; ++i) {
75                 for (int c = 0; c < accumulated->channels(); ++c) {
76                         BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
77                 }
78         }
79 }
80
81 /* Test insertion of black frames between separate bits of video content */
82 BOOST_AUTO_TEST_CASE (player_black_fill_test)
83 {
84         shared_ptr<Film> film = new_test_film ("black_fill_test");
85         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
86         film->set_name ("black_fill_test");
87         film->set_container (Ratio::from_id ("185"));
88         film->set_sequence (false);
89         shared_ptr<ImageContent> contentA (new ImageContent (film, "test/data/simple_testcard_640x480.png"));
90         shared_ptr<ImageContent> contentB (new ImageContent (film, "test/data/simple_testcard_640x480.png"));
91
92         film->examine_and_add_content (contentA);
93         film->examine_and_add_content (contentB);
94         wait_for_jobs ();
95
96         contentA->video->set_scale (VideoContentScale (Ratio::from_id ("185")));
97         contentA->video->set_length (3);
98         contentA->set_position (DCPTime::from_frames (2, film->video_frame_rate ()));
99         contentB->video->set_scale (VideoContentScale (Ratio::from_id ("185")));
100         contentB->video->set_length (1);
101         contentB->set_position (DCPTime::from_frames (7, film->video_frame_rate ()));
102
103         film->make_dcp ();
104
105         wait_for_jobs ();
106
107         boost::filesystem::path ref;
108         ref = "test";
109         ref /= "data";
110         ref /= "black_fill_test";
111
112         boost::filesystem::path check;
113         check = "build";
114         check /= "test";
115         check /= "black_fill_test";
116         check /= film->dcp_name();
117
118         check_dcp (ref.string(), check.string());
119 }
120
121 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
122 BOOST_AUTO_TEST_CASE (player_subframe_test)
123 {
124         shared_ptr<Film> film = new_test_film ("reels_test7");
125         film->set_name ("reels_test7");
126         film->set_container (Ratio::from_id ("185"));
127         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
128         shared_ptr<Content> A = content_factory(film, "test/data/flat_red.png").front();
129         film->examine_and_add_content (A);
130         BOOST_REQUIRE (!wait_for_jobs ());
131         shared_ptr<Content> B = content_factory(film, "test/data/awkward_length.wav").front();
132         film->examine_and_add_content (B);
133         BOOST_REQUIRE (!wait_for_jobs ());
134         film->set_video_frame_rate (24);
135         A->video->set_length (3 * 24);
136
137         BOOST_CHECK (A->full_length() == DCPTime::from_frames(3 * 24, 24));
138         BOOST_CHECK (B->full_length() == DCPTime(289920));
139         /* Length should be rounded up from B's length to the next video frame */
140         BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
141
142         shared_ptr<Player> player (new Player (film, film->playlist ()));
143         player->setup_pieces ();
144         BOOST_REQUIRE_EQUAL (player->_black._periods.size(), 1);
145         BOOST_CHECK (player->_black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
146         BOOST_REQUIRE_EQUAL (player->_silent._periods.size(), 1);
147         BOOST_CHECK (player->_silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
148 }
149
150 static Frame video_frames;
151 static Frame audio_frames;
152
153 static void
154 video (shared_ptr<PlayerVideo>, DCPTime)
155 {
156         ++video_frames;
157 }
158
159 static void
160 audio (shared_ptr<AudioBuffers> audio, DCPTime)
161 {
162         audio_frames += audio->frames();
163 }
164
165 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
166 BOOST_AUTO_TEST_CASE (player_interleave_test)
167 {
168         shared_ptr<Film> film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
169         film->set_name ("ffmpeg_transcoder_basic_test");
170         film->set_container (Ratio::from_id ("185"));
171         film->set_audio_channels (6);
172
173         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/test.mp4"));
174         film->examine_and_add_content (c);
175         BOOST_REQUIRE (!wait_for_jobs ());
176
177         shared_ptr<TextSubtitleContent> s (new TextSubtitleContent (film, "test/data/subrip.srt"));
178         film->examine_and_add_content (s);
179         BOOST_REQUIRE (!wait_for_jobs ());
180
181         shared_ptr<Player> player (new Player(film, film->playlist()));
182         player->Video.connect (bind (&video, _1, _2));
183         player->Audio.connect (bind (&audio, _1, _2));
184         video_frames = audio_frames = 0;
185         while (!player->pass ()) {
186                 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) < 8);
187         }
188 }