Add a OV/VF test; tidy up a bit.
[dcpomatic.git] / test / vf_test.cc
1 /*
2     Copyright (C) 2015-2017 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/vf_Test.cc
22  *  @brief Various VF-related tests.
23  *  @ingroup specific
24  */
25
26 #include "lib/film.h"
27 #include "lib/dcp_content.h"
28 #include "lib/ffmpeg_content.h"
29 #include "lib/content_factory.h"
30 #include "lib/dcp_content_type.h"
31 #include "lib/video_content.h"
32 #include "test.h"
33 #include <dcp/cpl.h>
34 #include <dcp/reel.h>
35 #include <dcp/reel_picture_asset.h>
36 #include <dcp/reel_sound_asset.h>
37 #include <boost/test/unit_test.hpp>
38 #include <boost/foreach.hpp>
39
40 using std::list;
41 using std::string;
42 using std::cout;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45
46 /** Test the logic which decides whether a DCP can be referenced or not */
47 BOOST_AUTO_TEST_CASE (vf_test1)
48 {
49         shared_ptr<Film> film = new_test_film ("vf_test1");
50         shared_ptr<DCPContent> dcp (new DCPContent (film, "test/data/reels_test2"));
51         film->examine_and_add_content (dcp);
52         wait_for_jobs ();
53
54         /* Multi-reel DCP can't be referenced if we are using a single reel for the project */
55         film->set_reel_type (REELTYPE_SINGLE);
56         list<string> why_not;
57         BOOST_CHECK (!dcp->can_reference_video(why_not));
58         BOOST_CHECK (!dcp->can_reference_audio(why_not));
59         BOOST_CHECK (!dcp->can_reference_subtitle(why_not));
60
61         /* Multi-reel DCP can be referenced if we are using by-video-content */
62         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
63         BOOST_CHECK (dcp->can_reference_video(why_not));
64         BOOST_CHECK (dcp->can_reference_audio(why_not));
65         /* (but reels_test2 has no subtitles to reference) */
66         BOOST_CHECK (!dcp->can_reference_subtitle(why_not));
67
68         shared_ptr<FFmpegContent> other (new FFmpegContent (film, "test/data/test.mp4"));
69         film->examine_and_add_content (other);
70         wait_for_jobs ();
71
72         /* Not possible if there is overlap */
73         other->set_position (DCPTime (0));
74         BOOST_CHECK (!dcp->can_reference_video(why_not));
75         BOOST_CHECK (!dcp->can_reference_audio(why_not));
76         BOOST_CHECK (!dcp->can_reference_subtitle(why_not));
77
78         /* This should not be considered an overlap */
79         other->set_position (dcp->end ());
80         BOOST_CHECK (dcp->can_reference_video(why_not));
81         BOOST_CHECK (dcp->can_reference_audio(why_not));
82         /* (reels_test2 has no subtitles to reference) */
83         BOOST_CHECK (!dcp->can_reference_subtitle(why_not));
84 }
85
86 /** Make a OV with video and audio and a VF referencing the OV and adding subs */
87 BOOST_AUTO_TEST_CASE (vf_test2)
88 {
89         /* Make the OV */
90         shared_ptr<Film> ov = new_test_film ("vf_test2_ov");
91         ov->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
92         ov->set_name ("vf_test2_ov");
93         shared_ptr<Content> video = content_factory (ov, "test/data/flat_red.png").front();
94         ov->examine_and_add_content (video);
95         wait_for_jobs ();
96         video->video->set_length (24 * 5);
97         shared_ptr<Content> audio = content_factory(ov, "test/data/white.wav").front();
98         ov->examine_and_add_content (audio);
99         wait_for_jobs ();
100         ov->make_dcp ();
101         wait_for_jobs ();
102
103         /* Make the VF */
104         shared_ptr<Film> vf = new_test_film ("vf_test2_vf");
105         vf->set_name ("vf_test2_vf");
106         vf->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
107         vf->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
108         shared_ptr<DCPContent> dcp (new DCPContent (vf, ov->dir (ov->dcp_name ())));
109         BOOST_REQUIRE (dcp);
110         vf->examine_and_add_content (dcp);
111         wait_for_jobs ();
112         dcp->set_reference_video (true);
113         dcp->set_reference_audio (true);
114         shared_ptr<Content> sub = content_factory(vf, "test/data/subrip4.srt").front();
115         vf->examine_and_add_content (sub);
116         DCPOMATIC_ASSERT (!wait_for_jobs ());
117         vf->make_dcp ();
118         DCPOMATIC_ASSERT (!wait_for_jobs ());
119         vf->write_metadata ();
120
121         dcp::DCP ov_c (ov->dir (ov->dcp_name ()));
122         ov_c.read ();
123         BOOST_REQUIRE_EQUAL (ov_c.cpls().size(), 1);
124         BOOST_REQUIRE_EQUAL (ov_c.cpls().front()->reels().size(), 1);
125         BOOST_REQUIRE (ov_c.cpls().front()->reels().front()->main_picture());
126         string const pic_id = ov_c.cpls().front()->reels().front()->main_picture()->id();
127         BOOST_REQUIRE (ov_c.cpls().front()->reels().front()->main_sound());
128         string const sound_id = ov_c.cpls().front()->reels().front()->main_sound()->id();
129         BOOST_REQUIRE (!ov_c.cpls().front()->reels().front()->main_subtitle());
130
131         dcp::DCP vf_c (vf->dir (vf->dcp_name ()));
132         vf_c.read ();
133         BOOST_REQUIRE_EQUAL (vf_c.cpls().size(), 1);
134         BOOST_REQUIRE_EQUAL (vf_c.cpls().front()->reels().size(), 1);
135         BOOST_REQUIRE (vf_c.cpls().front()->reels().front()->main_picture());
136         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_picture()->id(), pic_id);
137         BOOST_REQUIRE (vf_c.cpls().front()->reels().front()->main_sound());
138         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_sound()->id(), sound_id);
139         BOOST_REQUIRE (vf_c.cpls().front()->reels().front()->main_subtitle());
140 }
141
142 /** Test creation of a VF using a trimmed OV; the output should have entry point /
143  *  duration altered to effect the trimming.
144  */
145 BOOST_AUTO_TEST_CASE (vf_test3)
146 {
147         /* Make the OV */
148         shared_ptr<Film> ov = new_test_film ("vf_test3_ov");
149         ov->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
150         ov->set_name ("vf_test3_ov");
151         shared_ptr<Content> video = content_factory(ov, "test/data/flat_red.png").front();
152         ov->examine_and_add_content (video);
153         wait_for_jobs ();
154         video->video->set_length (24 * 5);
155         shared_ptr<Content> audio = content_factory(ov, "test/data/white.wav").front();
156         ov->examine_and_add_content (audio);
157         wait_for_jobs ();
158         ov->make_dcp ();
159         wait_for_jobs ();
160
161         /* Make the VF */
162         shared_ptr<Film> vf = new_test_film ("vf_test3_vf");
163         vf->set_name ("vf_test3_vf");
164         vf->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
165         vf->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
166         shared_ptr<DCPContent> dcp (new DCPContent (vf, ov->dir (ov->dcp_name ())));
167         BOOST_REQUIRE (dcp);
168         dcp->set_trim_start (ContentTime::from_seconds (1));
169         dcp->set_trim_end (ContentTime::from_seconds (1));
170         vf->examine_and_add_content (dcp);
171         wait_for_jobs ();
172         dcp->set_reference_video (true);
173         dcp->set_reference_audio (true);
174         vf->make_dcp ();
175         wait_for_jobs ();
176         vf->write_metadata ();
177
178         dcp::DCP vf_c (vf->dir (vf->dcp_name ()));
179         vf_c.read ();
180         BOOST_REQUIRE_EQUAL (vf_c.cpls().size(), 1);
181         BOOST_REQUIRE_EQUAL (vf_c.cpls().front()->reels().size(), 1);
182         BOOST_REQUIRE (vf_c.cpls().front()->reels().front()->main_picture());
183         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_picture()->entry_point(), 24);
184         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_picture()->duration(), 72);
185         BOOST_REQUIRE (vf_c.cpls().front()->reels().front()->main_sound());
186         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_sound()->entry_point(), 24);
187         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_sound()->duration(), 72);
188 }
189
190 /** Make a OV with video and audio and a VF referencing the OV and adding some more video */
191 BOOST_AUTO_TEST_CASE (vf_test4)
192 {
193         /* Make the OV */
194         shared_ptr<Film> ov = new_test_film ("vf_test4_ov");
195         ov->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
196         ov->set_name ("vf_test4_ov");
197         shared_ptr<Content> video = content_factory (ov, "test/data/flat_red.png").front();
198         ov->examine_and_add_content (video);
199         wait_for_jobs ();
200         video->video->set_length (24 * 5);
201         shared_ptr<Content> audio = content_factory(ov, "test/data/white.wav").front();
202         ov->examine_and_add_content (audio);
203         wait_for_jobs ();
204         ov->make_dcp ();
205         wait_for_jobs ();
206
207         /* Make the VF */
208         shared_ptr<Film> vf = new_test_film ("vf_test4_vf");
209         vf->set_name ("vf_test4_vf");
210         vf->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
211         vf->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
212         vf->set_sequence (false);
213         shared_ptr<DCPContent> dcp (new DCPContent (vf, ov->dir (ov->dcp_name ())));
214         BOOST_REQUIRE (dcp);
215         vf->examine_and_add_content (dcp);
216         wait_for_jobs ();
217         dcp->set_position(DCPTime::from_seconds(10));
218         dcp->set_reference_video (true);
219         dcp->set_reference_audio (true);
220         shared_ptr<Content> more_video = content_factory(vf, "test/data/flat_red.png").front();
221         vf->examine_and_add_content (more_video);
222         DCPOMATIC_ASSERT (!wait_for_jobs ());
223         more_video->set_position (DCPTime ());
224         vf->write_metadata ();
225         vf->make_dcp ();
226         DCPOMATIC_ASSERT (!wait_for_jobs ());
227
228         dcp::DCP ov_c (ov->dir (ov->dcp_name ()));
229         ov_c.read ();
230         BOOST_REQUIRE_EQUAL (ov_c.cpls().size(), 1);
231         BOOST_REQUIRE_EQUAL (ov_c.cpls().front()->reels().size(), 1);
232         BOOST_REQUIRE (ov_c.cpls().front()->reels().front()->main_picture());
233         string const pic_id = ov_c.cpls().front()->reels().front()->main_picture()->id();
234         BOOST_REQUIRE (ov_c.cpls().front()->reels().front()->main_sound());
235         string const sound_id = ov_c.cpls().front()->reels().front()->main_sound()->id();
236         BOOST_REQUIRE (!ov_c.cpls().front()->reels().front()->main_subtitle());
237
238         dcp::DCP vf_c (vf->dir (vf->dcp_name ()));
239         vf_c.read ();
240         BOOST_REQUIRE_EQUAL (vf_c.cpls().size(), 1);
241         BOOST_REQUIRE_EQUAL (vf_c.cpls().front()->reels().size(), 2);
242         BOOST_REQUIRE (vf_c.cpls().front()->reels().back()->main_picture());
243         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().back()->main_picture()->id(), pic_id);
244         BOOST_REQUIRE (vf_c.cpls().front()->reels().back()->main_sound());
245         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().back()->main_sound()->id(), sound_id);
246 }