Use make_shared<>.
[dcpomatic.git] / test / reels_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/ratio.h"
23 #include "lib/ffmpeg_content.h"
24 #include "lib/image_content.h"
25 #include "lib/dcp_content_type.h"
26 #include "lib/dcp_content.h"
27 #include "lib/video_content.h"
28 #include "lib/text_subtitle_content.h"
29 #include "test.h"
30 #include <boost/test/unit_test.hpp>
31 #include <boost/foreach.hpp>
32 #include <boost/make_shared.hpp>
33
34 using std::list;
35 using boost::shared_ptr;
36 using boost::make_shared;
37
38 /** Test Film::reels() */
39 BOOST_AUTO_TEST_CASE (reels_test1)
40 {
41         shared_ptr<Film> film = new_test_film ("reels_test1");
42         film->set_container (Ratio::from_id ("185"));
43         shared_ptr<FFmpegContent> A = make_shared<FFmpegContent> (film, "test/data/test.mp4");
44         film->examine_and_add_content (A);
45         shared_ptr<FFmpegContent> B = make_shared<FFmpegContent> (film, "test/data/test.mp4");
46         film->examine_and_add_content (B);
47         wait_for_jobs ();
48         BOOST_CHECK_EQUAL (A->full_length(), DCPTime (288000));
49
50         film->set_reel_type (REELTYPE_SINGLE);
51         list<DCPTimePeriod> r = film->reels ();
52         BOOST_CHECK_EQUAL (r.size(), 1);
53         BOOST_CHECK_EQUAL (r.front().from, DCPTime (0));
54         BOOST_CHECK_EQUAL (r.front().to, DCPTime (288000 * 2));
55
56         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
57         r = film->reels ();
58         BOOST_CHECK_EQUAL (r.size(), 2);
59         BOOST_CHECK_EQUAL (r.front().from, DCPTime (0));
60         BOOST_CHECK_EQUAL (r.front().to, DCPTime (288000));
61         BOOST_CHECK_EQUAL (r.back().from, DCPTime (288000));
62         BOOST_CHECK_EQUAL (r.back().to, DCPTime (288000 * 2));
63
64         film->set_j2k_bandwidth (100000000);
65         film->set_reel_type (REELTYPE_BY_LENGTH);
66         /* This is just over 2.5s at 100Mbit/s; should correspond to 60 frames */
67         film->set_reel_length (31253154);
68         r = film->reels ();
69         BOOST_CHECK_EQUAL (r.size(), 3);
70         list<DCPTimePeriod>::const_iterator i = r.begin ();
71         BOOST_CHECK_EQUAL (i->from, DCPTime (0));
72         BOOST_CHECK_EQUAL (i->to, DCPTime::from_frames (60, 24));
73         ++i;
74         BOOST_CHECK_EQUAL (i->from, DCPTime::from_frames (60, 24));
75         BOOST_CHECK_EQUAL (i->to, DCPTime::from_frames (120, 24));
76         ++i;
77         BOOST_CHECK_EQUAL (i->from, DCPTime::from_frames (120, 24));
78         BOOST_CHECK_EQUAL (i->to, DCPTime::from_frames (144, 24));
79 }
80
81 /** Make a short DCP with multi reels split by video content, then import
82  *  this into a new project and make a new DCP referencing it.
83  */
84 BOOST_AUTO_TEST_CASE (reels_test2)
85 {
86         shared_ptr<Film> film = new_test_film ("reels_test2");
87         film->set_name ("reels_test2");
88         film->set_container (Ratio::from_id ("185"));
89         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
90
91         {
92                 shared_ptr<ImageContent> c = make_shared<ImageContent> (film, "test/data/flat_red.png");
93                 film->examine_and_add_content (c);
94                 wait_for_jobs ();
95                 c->video->set_length (24);
96         }
97
98         {
99                 shared_ptr<ImageContent> c = make_shared<ImageContent> (film, "test/data/flat_green.png");
100                 film->examine_and_add_content (c);
101                 wait_for_jobs ();
102                 c->video->set_length (24);
103         }
104
105         {
106                 shared_ptr<ImageContent> c = make_shared<ImageContent> (film, "test/data/flat_blue.png");
107                 film->examine_and_add_content (c);
108                 wait_for_jobs ();
109                 c->video->set_length (24);
110         }
111
112         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
113         wait_for_jobs ();
114
115         film->make_dcp ();
116         wait_for_jobs ();
117
118         check_dcp ("test/data/reels_test2", film->dir (film->dcp_name()));
119
120         shared_ptr<Film> film2 = new_test_film ("reels_test2b");
121         film2->set_name ("reels_test2b");
122         film2->set_container (Ratio::from_id ("185"));
123         film2->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
124         film2->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
125
126         shared_ptr<DCPContent> c = make_shared<DCPContent> (film2, film->dir (film->dcp_name ()));
127         film2->examine_and_add_content (c);
128         wait_for_jobs ();
129
130         list<DCPTimePeriod> r = film2->reels ();
131         BOOST_CHECK_EQUAL (r.size(), 3);
132         list<DCPTimePeriod>::const_iterator i = r.begin ();
133         BOOST_CHECK_EQUAL (i->from, DCPTime (0));
134         BOOST_CHECK_EQUAL (i->to, DCPTime (96000));
135         ++i;
136         BOOST_CHECK_EQUAL (i->from, DCPTime (96000));
137         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 2));
138         ++i;
139         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 2));
140         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 3));
141
142         c->set_reference_video (true);
143         c->set_reference_audio (true);
144
145         film2->make_dcp ();
146         wait_for_jobs ();
147 }
148
149 /** Check that REELTYPE_BY_VIDEO_CONTENT adds an extra reel, if necessary, at the end
150  *  of all the video content to mop up anything afterward.
151  */
152 BOOST_AUTO_TEST_CASE (reels_test3)
153 {
154         shared_ptr<Film> film = new_test_film ("reels_test3");
155         film->set_name ("reels_test3");
156         film->set_container (Ratio::from_id ("185"));
157         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
158         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
159
160         shared_ptr<Content> dcp = make_shared<DCPContent> (film, "test/data/reels_test2");
161         film->examine_and_add_content (dcp);
162         shared_ptr<Content> sub = make_shared<TextSubtitleContent> (film, "test/data/subrip.srt");
163         film->examine_and_add_content (sub);
164         wait_for_jobs ();
165
166         list<DCPTimePeriod> reels = film->reels();
167         BOOST_REQUIRE_EQUAL (reels.size(), 4);
168         list<DCPTimePeriod>::const_iterator i = reels.begin ();
169         BOOST_CHECK_EQUAL (i->from, DCPTime (0));
170         BOOST_CHECK_EQUAL (i->to, DCPTime (96000));
171         ++i;
172         BOOST_CHECK_EQUAL (i->from, DCPTime (96000));
173         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 2));
174         ++i;
175         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 2));
176         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 3));
177         ++i;
178         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 3));
179         BOOST_CHECK_EQUAL (i->to, sub->full_length().round_up (film->video_frame_rate()));
180 }
181
182 /** Check creation of a multi-reel DCP with a single .srt subtitle file;
183  *  make sure that the reel subtitle timing is done right.
184  */
185 BOOST_AUTO_TEST_CASE (reels_test4)
186 {
187         shared_ptr<Film> film = new_test_film ("reels_test4");
188         film->set_name ("reels_test4");
189         film->set_container (Ratio::from_id ("185"));
190         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
191         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
192
193         /* 4 piece of 1s-long content */
194         shared_ptr<ImageContent> content[4];
195         for (int i = 0; i < 4; ++i) {
196                 content[i].reset (new ImageContent (film, "test/data/flat_green.png"));
197                 film->examine_and_add_content (content[i]);
198                 wait_for_jobs ();
199                 content[i]->video->set_length (24);
200         }
201
202         shared_ptr<TextSubtitleContent> subs = make_shared<TextSubtitleContent> (film, "test/data/subrip3.srt");
203         film->examine_and_add_content (subs);
204         wait_for_jobs ();
205
206         list<DCPTimePeriod> reels = film->reels();
207         BOOST_REQUIRE_EQUAL (reels.size(), 4);
208         list<DCPTimePeriod>::const_iterator i = reels.begin ();
209         BOOST_CHECK_EQUAL (i->from, DCPTime (0));
210         BOOST_CHECK_EQUAL (i->to, DCPTime (96000));
211         ++i;
212         BOOST_CHECK_EQUAL (i->from, DCPTime (96000));
213         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 2));
214         ++i;
215         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 2));
216         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 3));
217         ++i;
218         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 3));
219         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 4));
220
221         film->make_dcp ();
222         wait_for_jobs ();
223
224         check_dcp ("test/data/reels_test4", film->dir (film->dcp_name()));
225 }