BOOST_FOREACH.
[dcpomatic.git] / test / subtitle_reel_test.cc
1 /*
2     Copyright (C) 2019-2020 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/content_factory.h"
22 #include "lib/film.h"
23 #include "lib/image_content.h"
24 #include "lib/dcp_subtitle_content.h"
25 #include "lib/text_content.h"
26 #include "lib/video_content.h"
27 #include "test.h"
28 #include <dcp/dcp.h>
29 #include <dcp/cpl.h>
30 #include <dcp/reel.h>
31 #include <dcp/interop_subtitle_asset.h>
32 #include <dcp/reel_closed_caption_asset.h>
33 #include <dcp/reel_subtitle_asset.h>
34 #include <boost/test/unit_test.hpp>
35
36
37 using std::list;
38 using std::string;
39 using boost::optional;
40 using std::shared_ptr;
41
42
43 /* Check that timings are done correctly for multi-reel DCPs with PNG subs */
44 BOOST_AUTO_TEST_CASE (subtitle_reel_test)
45 {
46         shared_ptr<Film> film = new_test_film2 ("subtitle_reel_test");
47         film->set_interop (true);
48         shared_ptr<ImageContent> red_a (new ImageContent("test/data/flat_red.png"));
49         shared_ptr<ImageContent> red_b (new ImageContent("test/data/flat_red.png"));
50         shared_ptr<DCPSubtitleContent> sub_a (new DCPSubtitleContent("test/data/png_subs/subs.xml"));
51         shared_ptr<DCPSubtitleContent> sub_b (new DCPSubtitleContent("test/data/png_subs/subs.xml"));
52
53         film->examine_and_add_content (red_a);
54         film->examine_and_add_content (red_b);
55         film->examine_and_add_content (sub_a);
56         film->examine_and_add_content (sub_b);
57
58         BOOST_REQUIRE (!wait_for_jobs());
59
60         red_a->set_position (film, dcpomatic::DCPTime());
61         red_a->video->set_length (240);
62         sub_a->set_position (film, dcpomatic::DCPTime());
63         red_b->set_position (film, dcpomatic::DCPTime::from_seconds(10));
64         red_b->video->set_length (240);
65         sub_b->set_position (film, dcpomatic::DCPTime::from_seconds(10));
66
67         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
68
69         film->make_dcp ();
70         BOOST_REQUIRE (!wait_for_jobs());
71
72         dcp::DCP dcp ("build/test/subtitle_reel_test/" + film->dcp_name());
73         dcp.read ();
74         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1U);
75         shared_ptr<dcp::CPL> cpl = dcp.cpls().front();
76
77         list<shared_ptr<dcp::Reel> > reels = cpl->reels ();
78         BOOST_REQUIRE_EQUAL (reels.size(), 2U);
79         list<shared_ptr<dcp::Reel> >::const_iterator i = reels.begin ();
80         BOOST_REQUIRE ((*i)->main_subtitle());
81         BOOST_REQUIRE ((*i)->main_subtitle()->asset());
82         shared_ptr<dcp::InteropSubtitleAsset> A = std::dynamic_pointer_cast<dcp::InteropSubtitleAsset>((*i)->main_subtitle()->asset());
83         BOOST_REQUIRE (A);
84         ++i;
85         BOOST_REQUIRE ((*i)->main_subtitle());
86         BOOST_REQUIRE ((*i)->main_subtitle()->asset());
87         shared_ptr<dcp::InteropSubtitleAsset> B = std::dynamic_pointer_cast<dcp::InteropSubtitleAsset>((*i)->main_subtitle()->asset());
88         BOOST_REQUIRE (B);
89
90         BOOST_REQUIRE_EQUAL (A->subtitles().size(), 1U);
91         BOOST_REQUIRE_EQUAL (B->subtitles().size(), 1U);
92
93         /* These times should be the same as they are should be offset from the start of the reel */
94         BOOST_CHECK (A->subtitles().front()->in() == B->subtitles().front()->in());
95 }
96
97
98
99 /** Check that with a SMPTE DCP if we have subtitles in one reel, all reels have a
100  *  SubtitleAsset (even if it's empty); SMPTE Bv2.1 section 8.3.1.
101  */
102 BOOST_AUTO_TEST_CASE (subtitle_in_all_reels_test)
103 {
104         shared_ptr<Film> film = new_test_film2 ("subtitle_in_all_reels_test");
105         film->set_interop (false);
106         film->set_sequence (false);
107         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
108         for (int i = 0; i < 3; ++i) {
109                 shared_ptr<Content> video = content_factory("test/data/flat_red.png").front();
110                 film->examine_and_add_content (video);
111                 BOOST_REQUIRE (!wait_for_jobs());
112                 video->video->set_length (15 * 24);
113                 video->set_position (film, dcpomatic::DCPTime::from_seconds(15 * i));
114         }
115         shared_ptr<Content> subs = content_factory("test/data/15s.srt").front();
116         film->examine_and_add_content (subs);
117         BOOST_REQUIRE (!wait_for_jobs());
118         film->make_dcp ();
119         BOOST_REQUIRE (!wait_for_jobs());
120
121         dcp::DCP dcp ("build/test/subtitle_in_all_reels_test/" + film->dcp_name());
122         dcp.read ();
123         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1U);
124         shared_ptr<dcp::CPL> cpl = dcp.cpls().front();
125         BOOST_REQUIRE_EQUAL (cpl->reels().size(), 3U);
126
127         for (auto i: cpl->reels()) {
128                 BOOST_CHECK (i->main_subtitle());
129         }
130 }
131
132
133 /** Check that with a SMPTE DCP if we have closed captions in one reel, all reels have a
134  *  ClosedCaptionAssets for the same set of tracks (even if they are empty); SMPTE Bv2.1 section 8.3.1.
135  */
136 BOOST_AUTO_TEST_CASE (closed_captions_in_all_reels_test)
137 {
138         shared_ptr<Film> film = new_test_film2 ("closed_captions_in_all_reels_test");
139         film->set_interop (false);
140         film->set_sequence (false);
141         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
142
143         for (int i = 0; i < 3; ++i) {
144                 shared_ptr<Content> video = content_factory("test/data/flat_red.png").front();
145                 film->examine_and_add_content (video);
146                 BOOST_REQUIRE (!wait_for_jobs());
147                 video->video->set_length (15 * 24);
148                 video->set_position (film, dcpomatic::DCPTime::from_seconds(15 * i));
149         }
150
151         shared_ptr<Content> ccap1 = content_factory("test/data/15s.srt").front();
152         film->examine_and_add_content (ccap1);
153         BOOST_REQUIRE (!wait_for_jobs());
154         ccap1->text.front()->set_type (TEXT_CLOSED_CAPTION);
155         ccap1->text.front()->set_dcp_track (DCPTextTrack("Test", "de-DE"));
156
157         shared_ptr<Content> ccap2 = content_factory("test/data/15s.srt").front();
158         film->examine_and_add_content (ccap2);
159         BOOST_REQUIRE (!wait_for_jobs());
160         ccap2->text.front()->set_type (TEXT_CLOSED_CAPTION);
161         ccap2->text.front()->set_dcp_track (DCPTextTrack("Other", "en-GB"));
162
163         film->make_dcp ();
164         BOOST_REQUIRE (!wait_for_jobs());
165
166         dcp::DCP dcp ("build/test/closed_captions_in_all_reels_test/" + film->dcp_name());
167         dcp.read ();
168         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1U);
169         shared_ptr<dcp::CPL> cpl = dcp.cpls().front();
170         BOOST_REQUIRE_EQUAL (cpl->reels().size(), 3U);
171
172         for (auto i: cpl->reels()) {
173                 BOOST_REQUIRE_EQUAL (i->closed_captions().size(), 2U);
174                 optional<string> first = i->closed_captions().front()->language();
175                 optional<string> second = i->closed_captions().back()->language();
176                 BOOST_REQUIRE (first);
177                 BOOST_REQUIRE (second);
178                 BOOST_CHECK (
179                         (*first == "en-GB" && *second == "de-DE") ||
180                         (*first == "de-DE" && *second == "en-GB")
181                         );
182         }
183 }