Add some tests; fix failure to make DCP when there is a bit of audio right at the...
[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/content_factory.h"
35 #include "test.h"
36 #include <boost/test/unit_test.hpp>
37 #include <iostream>
38
39 using std::cout;
40 using std::list;
41 using boost::shared_ptr;
42 using boost::bind;
43
44 static shared_ptr<AudioBuffers> accumulated;
45
46 static void
47 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
48 {
49         BOOST_REQUIRE (accumulated);
50         accumulated->append (audio);
51 }
52
53 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
54 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
55 {
56         shared_ptr<Film> film = new_test_film ("player_silence_padding_test");
57         film->set_name ("player_silence_padding_test");
58         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/test.mp4"));
59         film->set_container (Ratio::from_id ("185"));
60         film->set_audio_channels (6);
61
62         film->examine_and_add_content (c);
63         wait_for_jobs ();
64
65         accumulated.reset (new AudioBuffers (film->audio_channels(), 0));
66
67         shared_ptr<Player> player (new Player (film, film->playlist ()));
68         player->Audio.connect (bind (&accumulate, _1, _2));
69         while (!player->pass ()) {}
70         BOOST_REQUIRE (accumulated->frames() >= 48000);
71         BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
72
73         for (int i = 0; i < 48000; ++i) {
74                 for (int c = 0; c < accumulated->channels(); ++c) {
75                         BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
76                 }
77         }
78 }
79
80 /* Test insertion of black frames between separate bits of video content */
81 BOOST_AUTO_TEST_CASE (player_black_fill_test)
82 {
83         shared_ptr<Film> film = new_test_film ("black_fill_test");
84         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
85         film->set_name ("black_fill_test");
86         film->set_container (Ratio::from_id ("185"));
87         film->set_sequence (false);
88         shared_ptr<ImageContent> contentA (new ImageContent (film, "test/data/simple_testcard_640x480.png"));
89         shared_ptr<ImageContent> contentB (new ImageContent (film, "test/data/simple_testcard_640x480.png"));
90
91         film->examine_and_add_content (contentA);
92         film->examine_and_add_content (contentB);
93         wait_for_jobs ();
94
95         contentA->video->set_scale (VideoContentScale (Ratio::from_id ("185")));
96         contentA->video->set_length (3);
97         contentA->set_position (DCPTime::from_frames (2, film->video_frame_rate ()));
98         contentB->video->set_scale (VideoContentScale (Ratio::from_id ("185")));
99         contentB->video->set_length (1);
100         contentB->set_position (DCPTime::from_frames (7, film->video_frame_rate ()));
101
102         film->make_dcp ();
103
104         wait_for_jobs ();
105
106         boost::filesystem::path ref;
107         ref = "test";
108         ref /= "data";
109         ref /= "black_fill_test";
110
111         boost::filesystem::path check;
112         check = "build";
113         check /= "test";
114         check /= "black_fill_test";
115         check /= film->dcp_name();
116
117         check_dcp (ref.string(), check.string());
118 }
119
120 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
121 BOOST_AUTO_TEST_CASE (player_subframe_test)
122 {
123         shared_ptr<Film> film = new_test_film ("reels_test7");
124         film->set_name ("reels_test7");
125         film->set_container (Ratio::from_id ("185"));
126         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
127         shared_ptr<Content> A = content_factory(film, "test/data/flat_red.png").front();
128         film->examine_and_add_content (A);
129         BOOST_REQUIRE (!wait_for_jobs ());
130         shared_ptr<Content> B = content_factory(film, "test/data/awkward_length.wav").front();
131         film->examine_and_add_content (B);
132         BOOST_REQUIRE (!wait_for_jobs ());
133         film->set_video_frame_rate (24);
134         A->video->set_length (3 * 24);
135
136         BOOST_CHECK (A->full_length() == DCPTime::from_frames(3 * 24, 24));
137         BOOST_CHECK (B->full_length() == DCPTime(289920));
138         /* Length should be rounded up from B's length to the next video frame */
139         BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
140
141         shared_ptr<Player> player (new Player (film, film->playlist ()));
142         player->setup_pieces ();
143         BOOST_REQUIRE_EQUAL (player->_black._periods.size(), 1);
144         BOOST_CHECK (player->_black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
145         BOOST_REQUIRE_EQUAL (player->_silent._periods.size(), 1);
146         BOOST_CHECK (player->_silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
147 }