A number of unit test fixes.
[dcpomatic.git] / test / srt_subtitle_test.cc
1 /*
2     Copyright (C) 2015 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/srt_subtitle_test.cc
22  *  @brief Test writing DCPs with subtitles from .srt.
23  *  @ingroup specific
24  */
25
26 #include "lib/film.h"
27 #include "lib/text_subtitle_content.h"
28 #include "lib/dcp_content_type.h"
29 #include "lib/font.h"
30 #include "lib/ratio.h"
31 #include "lib/subtitle_content.h"
32 #include "test.h"
33 #include <boost/test/unit_test.hpp>
34 #include <boost/algorithm/string.hpp>
35 #include <list>
36
37 using std::string;
38 using std::list;
39 using boost::shared_ptr;
40
41 /** Make a very short DCP with a single subtitle from .srt with no specified fonts */
42 BOOST_AUTO_TEST_CASE (srt_subtitle_test)
43 {
44         shared_ptr<Film> film = new_test_film ("srt_subtitle_test");
45         film->set_container (Ratio::from_id ("185"));
46         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
47         film->set_name ("frobozz");
48         film->set_audio_channels (6);
49         film->set_interop (false);
50         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
51         film->examine_and_add_content (content);
52         wait_for_jobs ();
53
54         content->subtitle->set_use (true);
55         content->subtitle->set_burn (false);
56         film->make_dcp ();
57         wait_for_jobs ();
58
59         /* Should be blank video with a subtitle MXF */
60         check_dcp ("test/data/srt_subtitle_test", film->dir (film->dcp_name ()));
61 }
62
63 /** Same again but with a `font' specified */
64 BOOST_AUTO_TEST_CASE (srt_subtitle_test2)
65 {
66         shared_ptr<Film> film = new_test_film ("srt_subtitle_test2");
67         film->set_container (Ratio::from_id ("185"));
68         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
69         film->set_name ("frobozz");
70         film->set_audio_channels (6);
71         film->set_interop (false);
72         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
73         film->examine_and_add_content (content);
74         wait_for_jobs ();
75
76         content->subtitle->set_use (true);
77         content->subtitle->set_burn (false);
78         /* Use test/data/subrip2.srt as if it were a font file  */
79         content->subtitle->fonts().front()->set_file (FontFiles::NORMAL, "test/data/subrip2.srt");
80
81         film->make_dcp ();
82         wait_for_jobs ();
83
84         /* Should be blank video with a subtitle MXF */
85         check_dcp ("test/data/srt_subtitle_test2", film->dir (film->dcp_name ()));
86 }
87
88 static void
89 check_subtitle_file (shared_ptr<Film> film, boost::filesystem::path ref)
90 {
91         /* Find the subtitle file and check it */
92         for (
93                 boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (film->directory().get() / film->dcp_name (false));
94                 i != boost::filesystem::directory_iterator ();
95                 ++i) {
96
97                 if (boost::filesystem::is_directory (i->path ())) {
98                         for (
99                                 boost::filesystem::directory_iterator j = boost::filesystem::directory_iterator (i->path ());
100                                 j != boost::filesystem::directory_iterator ();
101                                 ++j) {
102
103                                 if (boost::algorithm::starts_with (j->path().leaf().string(), "sub_")) {
104                                         list<string> ignore;
105                                         ignore.push_back ("SubtitleID");
106                                         check_xml (*j, ref, ignore);
107                                 }
108                         }
109                 }
110         }
111 }
112
113 /** Make another DCP with a longer .srt file */
114 BOOST_AUTO_TEST_CASE (srt_subtitle_test3)
115 {
116         shared_ptr<Film> film = new_test_film ("srt_subtitle_test3");
117
118         film->set_container (Ratio::from_id ("185"));
119         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
120         film->set_name ("frobozz");
121         film->set_interop (true);
122         film->set_audio_channels (6);
123         film->set_interop (false);
124         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, private_data / "Ankoemmling_short.srt"));
125         film->examine_and_add_content (content);
126         wait_for_jobs ();
127
128         content->subtitle->set_use (true);
129         content->subtitle->set_burn (false);
130
131         film->make_dcp ();
132         wait_for_jobs ();
133
134         check_subtitle_file (film, private_data / "Ankoemmling_short.xml");
135 }
136
137 /** Build a small DCP with no picture and a single subtitle overlaid onto it */
138 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
139 {
140         shared_ptr<Film> film = new_test_film ("srt_subtitle_test4");
141         film->set_container (Ratio::from_id ("185"));
142         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
143         film->set_name ("frobozz");
144         film->set_interop (false);
145         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
146         content->subtitle->set_use (true);
147         content->subtitle->set_burn (false);
148         film->examine_and_add_content (content);
149         wait_for_jobs ();
150         film->make_dcp ();
151         wait_for_jobs ();
152
153         /* Should be blank video with MXF subtitles */
154         check_dcp ("test/data/xml_subtitle_test", film->dir (film->dcp_name ()));
155 }
156
157 /** Check the subtitle XML when there are two subtitle files in the project */
158 BOOST_AUTO_TEST_CASE (srt_subtitle_test5)
159 {
160         shared_ptr<Film> film = new_test_film ("srt_subtitle_test5");
161         film->set_container (Ratio::from_id ("185"));
162         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
163         film->set_name ("frobozz");
164         film->set_interop (true);
165         film->set_sequence (false);
166         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
167         content->subtitle->set_use (true);
168         content->subtitle->set_burn (false);
169         film->examine_and_add_content (content);
170         film->examine_and_add_content (content);
171         wait_for_jobs ();
172         content->set_position (DCPTime (0));
173         film->make_dcp ();
174         wait_for_jobs ();
175         film->write_metadata ();
176
177         check_dcp ("test/data/xml_subtitle_test2", film->dir (film->dcp_name ()));
178 }
179
180 #if 0
181 /* XXX: this is disabled; there is some difference in font rendering
182    between the test machine and others.
183 */
184
185 /** Test rendering of a SubRip subtitle */
186 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
187 {
188         shared_ptr<Film> film = new_test_film ("subrip_render_test");
189         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip.srt"));
190         content->examine (shared_ptr<Job> (), true);
191         BOOST_CHECK_EQUAL (content->full_length(), DCPTime::from_seconds ((3 * 60) + 56.471));
192
193         shared_ptr<SubRipDecoder> decoder (new SubRipDecoder (content));
194         list<ContentTextSubtitle> cts = decoder->get_text_subtitles (
195                 ContentTimePeriod (
196                         ContentTime::from_seconds (109), ContentTime::from_seconds (110)
197                         ), false
198                 );
199         BOOST_CHECK_EQUAL (cts.size(), 1);
200
201         PositionImage image = render_subtitles (cts.front().subs, dcp::Size (1998, 1080));
202         write_image (image.image, "build/test/subrip_render_test.png");
203         check_file ("build/test/subrip_render_test.png", "test/data/subrip_render_test.png");
204 }
205 #endif