Doxygen tweaks.
[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 <iostream>
27 #include <boost/test/unit_test.hpp>
28 #include "lib/film.h"
29 #include "lib/ffmpeg_content.h"
30 #include "lib/dcp_content_type.h"
31 #include "lib/ratio.h"
32 #include "lib/audio_buffers.h"
33 #include "lib/player.h"
34 #include "lib/video_content.h"
35 #include "lib/image_content.h"
36 #include "test.h"
37
38 using std::cout;
39 using std::list;
40 using boost::shared_ptr;
41 using boost::bind;
42
43 static bool
44 valid (Content const *)
45 {
46         return true;
47 }
48
49 /** Player::overlaps */
50 BOOST_AUTO_TEST_CASE (player_overlaps_test)
51 {
52         shared_ptr<Film> film = new_test_film ("player_overlaps_test");
53         film->set_container (Ratio::from_id ("185"));
54
55         /* This content is 3s long */
56         shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test.mp4"));
57         shared_ptr<FFmpegContent> B (new FFmpegContent (film, "test/data/test.mp4"));
58         shared_ptr<FFmpegContent> C (new FFmpegContent (film, "test/data/test.mp4"));
59
60         film->examine_and_add_content (A);
61         film->examine_and_add_content (B);
62         film->examine_and_add_content (C);
63         wait_for_jobs ();
64
65         BOOST_CHECK_EQUAL (A->full_length().get(), 288000);
66
67         A->set_position (DCPTime::from_seconds (0));
68         B->set_position (DCPTime::from_seconds (10));
69         C->set_position (DCPTime::from_seconds (20));
70
71         shared_ptr<Player> player (new Player (film, film->playlist ()));
72
73         list<shared_ptr<Piece> > o = player->overlaps (DCPTime::from_seconds (0), DCPTime::from_seconds (5), &valid);
74         BOOST_CHECK_EQUAL (o.size(), 1U);
75         BOOST_CHECK_EQUAL (o.front()->content, A);
76
77         o = player->overlaps (DCPTime::from_seconds (5), DCPTime::from_seconds (8), &valid);
78         BOOST_CHECK_EQUAL (o.size(), 0U);
79
80         o = player->overlaps (DCPTime::from_seconds (8), DCPTime::from_seconds (12), &valid);
81         BOOST_CHECK_EQUAL (o.size(), 1U);
82         BOOST_CHECK_EQUAL (o.front()->content, B);
83
84         o = player->overlaps (DCPTime::from_seconds (2), DCPTime::from_seconds (12), &valid);
85         BOOST_CHECK_EQUAL (o.size(), 2U);
86         BOOST_CHECK_EQUAL (o.front()->content, A);
87         BOOST_CHECK_EQUAL (o.back()->content, B);
88
89         o = player->overlaps (DCPTime::from_seconds (8), DCPTime::from_seconds (11), &valid);
90         BOOST_CHECK_EQUAL (o.size(), 1U);
91         BOOST_CHECK_EQUAL (o.front()->content, B);
92 }
93
94 static shared_ptr<AudioBuffers> accumulated;
95
96 static void
97 accumulate (shared_ptr<AudioBuffers> audio, DCPTime t)
98 {
99         cout << "got " << audio->frames() << " @ " << to_string(t) << "\n";
100         BOOST_REQUIRE (accumulated);
101         accumulated->append (audio);
102 }
103
104 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
105 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
106 {
107         shared_ptr<Film> film = new_test_film ("player_silence_padding_test");
108         film->set_name ("player_silence_padding_test");
109         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/test.mp4"));
110         film->set_container (Ratio::from_id ("185"));
111         film->set_audio_channels (6);
112
113         film->examine_and_add_content (c);
114         wait_for_jobs ();
115
116         accumulated.reset (new AudioBuffers (film->audio_channels(), 0));
117
118         shared_ptr<Player> player (new Player (film, film->playlist ()));
119         player->Audio.connect (bind (&accumulate, _1, _2));
120         while (!player->pass ()) {}
121         BOOST_REQUIRE (accumulated->frames() >= 48000);
122         BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
123
124         for (int i = 0; i < 48000; ++i) {
125                 for (int c = 0; c < accumulated->channels(); ++c) {
126                         BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
127                 }
128         }
129 }
130
131 /* Test insertion of black frames between separate bits of video content */
132 BOOST_AUTO_TEST_CASE (player_black_fill_test)
133 {
134         shared_ptr<Film> film = new_test_film ("black_fill_test");
135         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
136         film->set_name ("black_fill_test");
137         film->set_container (Ratio::from_id ("185"));
138         film->set_sequence (false);
139         shared_ptr<ImageContent> contentA (new ImageContent (film, "test/data/simple_testcard_640x480.png"));
140         shared_ptr<ImageContent> contentB (new ImageContent (film, "test/data/simple_testcard_640x480.png"));
141
142         film->examine_and_add_content (contentA);
143         film->examine_and_add_content (contentB);
144         wait_for_jobs ();
145
146         contentA->video->set_scale (VideoContentScale (Ratio::from_id ("185")));
147         contentA->video->set_length (3);
148         contentA->set_position (DCPTime::from_frames (2, film->video_frame_rate ()));
149         contentB->video->set_scale (VideoContentScale (Ratio::from_id ("185")));
150         contentB->video->set_length (1);
151         contentB->set_position (DCPTime::from_frames (7, film->video_frame_rate ()));
152
153         film->make_dcp ();
154
155         wait_for_jobs ();
156
157         boost::filesystem::path ref;
158         ref = "test";
159         ref /= "data";
160         ref /= "black_fill_test";
161
162         boost::filesystem::path check;
163         check = "build";
164         check /= "test";
165         check /= "black_fill_test";
166         check /= film->dcp_name();
167
168         check_dcp (ref.string(), check.string());
169 }