Fix merging of audio in various circumstances.
[dcpomatic.git] / test / vf_test.cc
1 /*
2     Copyright (C) 2015-2016 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 #include "lib/film.h"
22 #include "lib/dcp_content.h"
23 #include "lib/ffmpeg_content.h"
24 #include "lib/content_factory.h"
25 #include "lib/dcp_content_type.h"
26 #include "lib/video_content.h"
27 #include "test.h"
28 #include <dcp/cpl.h>
29 #include <dcp/reel.h>
30 #include <dcp/reel_picture_asset.h>
31 #include <dcp/reel_sound_asset.h>
32 #include <boost/test/unit_test.hpp>
33 #include <boost/foreach.hpp>
34
35 using std::list;
36 using std::string;
37 using boost::shared_ptr;
38 using boost::dynamic_pointer_cast;
39
40 /** Test the logic which decides whether a DCP can be referenced or not */
41 BOOST_AUTO_TEST_CASE (vf_test1)
42 {
43         shared_ptr<Film> film = new_test_film ("vf_test1");
44         shared_ptr<DCPContent> dcp (new DCPContent (film, "test/data/reels_test2"));
45         film->examine_and_add_content (dcp);
46         wait_for_jobs ();
47
48         /* Multi-reel DCP can't be referenced if we are using a single reel for the project */
49         film->set_reel_type (REELTYPE_SINGLE);
50         list<string> why_not;
51         BOOST_CHECK (!dcp->can_reference_video(why_not));
52         BOOST_CHECK (!dcp->can_reference_audio(why_not));
53         BOOST_CHECK (!dcp->can_reference_subtitle(why_not));
54
55         /* Multi-reel DCP can be referenced if we are using by-video-content */
56         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
57         BOOST_CHECK (dcp->can_reference_video(why_not));
58         BOOST_CHECK (dcp->can_reference_audio(why_not));
59         /* (but reels_test2 has no subtitles to reference) */
60         BOOST_CHECK (!dcp->can_reference_subtitle(why_not));
61
62         shared_ptr<FFmpegContent> other (new FFmpegContent (film, "test/data/test.mp4"));
63         film->examine_and_add_content (other);
64         wait_for_jobs ();
65
66         /* Not possible if there is overlap */
67         other->set_position (DCPTime (0));
68         BOOST_CHECK (!dcp->can_reference_video(why_not));
69         BOOST_CHECK (!dcp->can_reference_audio(why_not));
70         BOOST_CHECK (!dcp->can_reference_subtitle(why_not));
71
72         /* This should not be considered an overlap */
73         other->set_position (dcp->end ());
74         BOOST_CHECK (dcp->can_reference_video(why_not));
75         BOOST_CHECK (dcp->can_reference_audio(why_not));
76         /* (reels_test2 has no subtitles to reference) */
77         BOOST_CHECK (!dcp->can_reference_subtitle(why_not));
78 }
79
80 /** Make a OV with video and audio and a VF referencing the OV and adding subs */
81 BOOST_AUTO_TEST_CASE (vf_test2)
82 {
83         /* Make the OV */
84         shared_ptr<Film> ov = new_test_film ("vf_test2_ov");
85         ov->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
86         ov->set_name ("vf_test2_ov");
87         shared_ptr<Content> video = content_factory (ov, "test/data/flat_red.png").front();
88         ov->examine_and_add_content (video);
89         wait_for_jobs ();
90         video->video->set_length (24 * 5);
91         shared_ptr<Content> audio = content_factory(ov, "test/data/white.wav").front();
92         ov->examine_and_add_content (audio);
93         wait_for_jobs ();
94         ov->make_dcp ();
95         wait_for_jobs ();
96
97         std::cout << "incoming vf.\n";
98
99         /* Make the VF */
100         shared_ptr<Film> vf = new_test_film ("vf_test2_vf");
101         vf->set_name ("vf_test2_vf");
102         vf->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
103         vf->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
104         shared_ptr<DCPContent> dcp (new DCPContent (vf, ov->dir (ov->dcp_name ())));
105         BOOST_REQUIRE (dcp);
106         vf->examine_and_add_content (dcp);
107         wait_for_jobs ();
108         dcp->set_reference_video (true);
109         dcp->set_reference_audio (true);
110         shared_ptr<Content> sub = content_factory(vf, "test/data/subrip4.srt").front();
111         vf->examine_and_add_content (sub);
112         vf->make_dcp ();
113         wait_for_jobs ();
114         vf->write_metadata ();
115
116         dcp::DCP ov_c (ov->dir (ov->dcp_name ()));
117         ov_c.read ();
118         BOOST_REQUIRE_EQUAL (ov_c.cpls().size(), 1);
119         BOOST_REQUIRE_EQUAL (ov_c.cpls().front()->reels().size(), 1);
120         BOOST_REQUIRE (ov_c.cpls().front()->reels().front()->main_picture());
121         string const pic_id = ov_c.cpls().front()->reels().front()->main_picture()->id();
122         BOOST_REQUIRE (ov_c.cpls().front()->reels().front()->main_sound());
123         string const sound_id = ov_c.cpls().front()->reels().front()->main_sound()->id();
124         BOOST_REQUIRE (!ov_c.cpls().front()->reels().front()->main_subtitle());
125
126         dcp::DCP vf_c (vf->dir (vf->dcp_name ()));
127         vf_c.read ();
128         BOOST_REQUIRE_EQUAL (vf_c.cpls().size(), 1);
129         BOOST_REQUIRE_EQUAL (vf_c.cpls().front()->reels().size(), 1);
130         BOOST_REQUIRE (vf_c.cpls().front()->reels().front()->main_picture());
131         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_picture()->id(), pic_id);
132         BOOST_REQUIRE (vf_c.cpls().front()->reels().front()->main_sound());
133         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_sound()->id(), sound_id);
134         BOOST_REQUIRE (vf_c.cpls().front()->reels().front()->main_subtitle());
135 }
136
137 /** Test creation of a VF using a trimmed OV; the output should have entry point /
138  *  duration altered to effect the trimming.
139  */
140 BOOST_AUTO_TEST_CASE (vf_test3)
141 {
142         /* Make the OV */
143         shared_ptr<Film> ov = new_test_film ("vf_test3_ov");
144         ov->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
145         ov->set_name ("vf_test3_ov");
146         shared_ptr<Content> video = content_factory(ov, "test/data/flat_red.png").front();
147         ov->examine_and_add_content (video);
148         wait_for_jobs ();
149         video->video->set_length (24 * 5);
150         shared_ptr<Content> audio = content_factory(ov, "test/data/white.wav").front();
151         ov->examine_and_add_content (audio);
152         wait_for_jobs ();
153         ov->make_dcp ();
154         wait_for_jobs ();
155
156         /* Make the VF */
157         shared_ptr<Film> vf = new_test_film ("vf_test3_vf");
158         vf->set_name ("vf_test3_vf");
159         vf->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
160         vf->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
161         shared_ptr<DCPContent> dcp (new DCPContent (vf, ov->dir (ov->dcp_name ())));
162         BOOST_REQUIRE (dcp);
163         dcp->set_trim_start (ContentTime::from_seconds (1));
164         dcp->set_trim_end (ContentTime::from_seconds (1));
165         vf->examine_and_add_content (dcp);
166         wait_for_jobs ();
167         dcp->set_reference_video (true);
168         dcp->set_reference_audio (true);
169         vf->make_dcp ();
170         wait_for_jobs ();
171         vf->write_metadata ();
172
173         dcp::DCP vf_c (vf->dir (vf->dcp_name ()));
174         vf_c.read ();
175         BOOST_REQUIRE_EQUAL (vf_c.cpls().size(), 1);
176         BOOST_REQUIRE_EQUAL (vf_c.cpls().front()->reels().size(), 1);
177         BOOST_REQUIRE (vf_c.cpls().front()->reels().front()->main_picture());
178         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_picture()->entry_point(), 24);
179         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_picture()->duration(), 72);
180         BOOST_REQUIRE (vf_c.cpls().front()->reels().front()->main_sound());
181         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_sound()->entry_point(), 24);
182         BOOST_CHECK_EQUAL (vf_c.cpls().front()->reels().front()->main_sound()->duration(), 72);
183 }