Fix test to reflect the fact that reel lengths are rounded to frame boundaries.
[dcpomatic.git] / test / reels_test.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/film.h"
21 #include "lib/ratio.h"
22 #include "lib/ffmpeg_content.h"
23 #include "lib/image_content.h"
24 #include "lib/dcp_content_type.h"
25 #include "lib/dcp_content.h"
26 #include "lib/subrip_content.h"
27 #include "test.h"
28 #include <boost/test/unit_test.hpp>
29 #include <boost/foreach.hpp>
30
31 using std::list;
32 using boost::shared_ptr;
33
34 /** Test Film::reels() */
35 BOOST_AUTO_TEST_CASE (reels_test1)
36 {
37         shared_ptr<Film> film = new_test_film ("reels_test1");
38         film->set_container (Ratio::from_id ("185"));
39         shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test.mp4"));
40         film->examine_and_add_content (A);
41         shared_ptr<FFmpegContent> B (new FFmpegContent (film, "test/data/test.mp4"));
42         film->examine_and_add_content (B);
43         wait_for_jobs ();
44         BOOST_CHECK_EQUAL (A->full_length(), DCPTime (288000));
45
46         film->set_reel_type (REELTYPE_SINGLE);
47         list<DCPTimePeriod> r = film->reels ();
48         BOOST_CHECK_EQUAL (r.size(), 1);
49         BOOST_CHECK_EQUAL (r.front().from, DCPTime (0));
50         BOOST_CHECK_EQUAL (r.front().to, DCPTime (288000 * 2));
51
52         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
53         r = film->reels ();
54         BOOST_CHECK_EQUAL (r.size(), 2);
55         BOOST_CHECK_EQUAL (r.front().from, DCPTime (0));
56         BOOST_CHECK_EQUAL (r.front().to, DCPTime (288000));
57         BOOST_CHECK_EQUAL (r.back().from, DCPTime (288000));
58         BOOST_CHECK_EQUAL (r.back().to, DCPTime (288000 * 2));
59
60         film->set_j2k_bandwidth (100000000);
61         film->set_reel_type (REELTYPE_BY_LENGTH);
62         /* This is just over 2.5s at 100Mbit/s; should correspond to 60 frames */
63         film->set_reel_length (31253154);
64         r = film->reels ();
65         BOOST_CHECK_EQUAL (r.size(), 3);
66         list<DCPTimePeriod>::const_iterator i = r.begin ();
67         BOOST_CHECK_EQUAL (i->from, DCPTime (0));
68         BOOST_CHECK_EQUAL (i->to, DCPTime::from_frames (60, 24));
69         ++i;
70         BOOST_CHECK_EQUAL (i->from, DCPTime::from_frames (60, 24));
71         BOOST_CHECK_EQUAL (i->to, DCPTime::from_frames (120, 24));
72         ++i;
73         BOOST_CHECK_EQUAL (i->from, DCPTime::from_frames (120, 24));
74         BOOST_CHECK_EQUAL (i->to, DCPTime::from_frames (144, 24));
75 }
76
77 /** Make a short DCP with multi reels split by video content, then import
78  *  this into a new project and make a new DCP referencing it.
79  */
80 BOOST_AUTO_TEST_CASE (reels_test2)
81 {
82         shared_ptr<Film> film = new_test_film ("reels_test2");
83         film->set_name ("reels_test2");
84         film->set_container (Ratio::from_id ("185"));
85         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
86
87         {
88                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_red.png"));
89                 film->examine_and_add_content (c);
90                 wait_for_jobs ();
91                 c->set_video_length (24);
92         }
93
94         {
95                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_green.png"));
96                 film->examine_and_add_content (c);
97                 wait_for_jobs ();
98                 c->set_video_length (24);
99         }
100
101         {
102                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_blue.png"));
103                 film->examine_and_add_content (c);
104                 wait_for_jobs ();
105                 c->set_video_length (24);
106         }
107
108         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
109         wait_for_jobs ();
110
111         film->make_dcp ();
112         wait_for_jobs ();
113
114         check_dcp ("test/data/reels_test2", film->dir (film->dcp_name()));
115
116         shared_ptr<Film> film2 = new_test_film ("reels_test2b");
117         film2->set_name ("reels_test2b");
118         film2->set_container (Ratio::from_id ("185"));
119         film2->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
120         film2->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
121
122         shared_ptr<DCPContent> c (new DCPContent (film2, film->dir (film->dcp_name ())));
123         film2->examine_and_add_content (c);
124         wait_for_jobs ();
125
126         list<DCPTimePeriod> r = film2->reels ();
127         BOOST_CHECK_EQUAL (r.size(), 3);
128         list<DCPTimePeriod>::const_iterator i = r.begin ();
129         BOOST_CHECK_EQUAL (i->from, DCPTime (0));
130         BOOST_CHECK_EQUAL (i->to, DCPTime (96000));
131         ++i;
132         BOOST_CHECK_EQUAL (i->from, DCPTime (96000));
133         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 2));
134         ++i;
135         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 2));
136         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 3));
137
138         c->set_reference_video (true);
139         c->set_reference_audio (true);
140
141         film2->make_dcp ();
142         wait_for_jobs ();
143 }
144
145 /** Check that REELTYPE_BY_VIDEO_CONTENT adds an extra reel, if necessary, at the end
146  *  of all the video content to mop up anything afterward.
147  */
148 BOOST_AUTO_TEST_CASE (reels_test3)
149 {
150         shared_ptr<Film> film = new_test_film ("reels_test3");
151         film->set_name ("reels_test3");
152         film->set_container (Ratio::from_id ("185"));
153         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
154         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
155
156         shared_ptr<Content> dcp (new DCPContent (film, "test/data/reels_test2"));
157         film->examine_and_add_content (dcp);
158         shared_ptr<Content> sub (new SubRipContent (film, "test/data/subrip.srt"));
159         film->examine_and_add_content (sub);
160         wait_for_jobs ();
161
162         std::cout << dcp->position() << " " << dcp->full_length() << "\n";
163         std::cout << sub->position() << " " << sub->full_length() << "\n";
164
165         list<DCPTimePeriod> reels = film->reels();
166         BOOST_REQUIRE_EQUAL (reels.size(), 4);
167         list<DCPTimePeriod>::const_iterator i = reels.begin ();
168         BOOST_CHECK_EQUAL (i->from, DCPTime (0));
169         BOOST_CHECK_EQUAL (i->to, DCPTime (96000));
170         ++i;
171         BOOST_CHECK_EQUAL (i->from, DCPTime (96000));
172         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 2));
173         ++i;
174         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 2));
175         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 3));
176         ++i;
177         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 3));
178         BOOST_CHECK_EQUAL (i->to, sub->full_length().round_up (film->video_frame_rate()));
179 }