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