Rename video/audio/subtitle part methods.
[dcpomatic.git] / test / srt_subtitle_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 /** @file  test/subtitle_write_test.cc
21  *  @brief Test writing DCPs with XML subtitles.
22  */
23
24 #include "lib/film.h"
25 #include "lib/text_subtitle_content.h"
26 #include "lib/dcp_content_type.h"
27 #include "lib/font.h"
28 #include "lib/ratio.h"
29 #include "lib/subtitle_content.h"
30 #include "test.h"
31 #include <boost/test/unit_test.hpp>
32 #include <boost/algorithm/string.hpp>
33 #include <list>
34
35 using std::string;
36 using std::list;
37 using boost::shared_ptr;
38
39 /** Make a very short DCP with a single subtitle from .srt with no specified fonts */
40 BOOST_AUTO_TEST_CASE (srt_subtitle_test)
41 {
42         shared_ptr<Film> film = new_test_film ("srt_subtitle_test");
43         film->set_container (Ratio::from_id ("185"));
44         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
45         film->set_name ("frobozz");
46         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
47         film->examine_and_add_content (content);
48         wait_for_jobs ();
49
50         content->subtitle->set_use (true);
51         content->subtitle->set_burn (false);
52         film->make_dcp ();
53         wait_for_jobs ();
54
55         /* Should be blank video with a subtitle MXF */
56         check_dcp ("test/data/srt_subtitle_test", film->dir (film->dcp_name ()));
57 }
58
59 /** Same again but with a `font' specified */
60 BOOST_AUTO_TEST_CASE (srt_subtitle_test2)
61 {
62         shared_ptr<Film> film = new_test_film ("srt_subtitle_test2");
63         film->set_container (Ratio::from_id ("185"));
64         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
65         film->set_name ("frobozz");
66         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
67         film->examine_and_add_content (content);
68         wait_for_jobs ();
69
70         content->subtitle->set_use (true);
71         content->subtitle->set_burn (false);
72         /* Use test/data/subrip2.srt as if it were a font file  */
73         content->subtitle->fonts().front()->set_file (FontFiles::NORMAL, "test/data/subrip2.srt");
74
75         film->make_dcp ();
76         wait_for_jobs ();
77
78         /* Should be blank video with a subtitle MXF */
79         check_dcp ("test/data/srt_subtitle_test2", film->dir (film->dcp_name ()));
80 }
81
82 /** Make another DCP with a longer .srt file */
83 BOOST_AUTO_TEST_CASE (srt_subtitle_test3)
84 {
85         shared_ptr<Film> film = new_test_film ("srt_subtitle_test3");
86
87         film->set_container (Ratio::from_id ("185"));
88         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
89         film->set_name ("frobozz");
90         film->set_interop (true);
91         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, private_data / "Ankoemmling.srt"));
92         film->examine_and_add_content (content);
93         wait_for_jobs ();
94
95         content->subtitle->set_use (true);
96         content->subtitle->set_burn (false);
97
98         film->make_dcp ();
99         wait_for_jobs ();
100
101         /* Find the subtitle file and check it */
102         for (
103                 boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (film->directory() / film->dcp_name (false));
104                 i != boost::filesystem::directory_iterator ();
105                 ++i) {
106
107                 if (boost::filesystem::is_directory (i->path ())) {
108                         for (
109                                 boost::filesystem::directory_iterator j = boost::filesystem::directory_iterator (i->path ());
110                                 j != boost::filesystem::directory_iterator ();
111                                 ++j) {
112
113                                 if (boost::algorithm::starts_with (j->path().leaf().string(), "sub_")) {
114                                         list<string> ignore;
115                                         ignore.push_back ("SubtitleID");
116                                         check_xml (*j, private_data / "Ankoemmling.xml", ignore);
117                                 }
118                         }
119                 }
120         }
121 }
122
123 #if 0
124 /* XXX: this is disabled; there is some difference in font rendering
125    between the test machine and others.
126 */
127
128 /** Test rendering of a SubRip subtitle */
129 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
130 {
131         shared_ptr<Film> film = new_test_film ("subrip_render_test");
132         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip.srt"));
133         content->examine (shared_ptr<Job> (), true);
134         BOOST_CHECK_EQUAL (content->full_length(), DCPTime::from_seconds ((3 * 60) + 56.471));
135
136         shared_ptr<SubRipDecoder> decoder (new SubRipDecoder (content));
137         list<ContentTextSubtitle> cts = decoder->get_text_subtitles (
138                 ContentTimePeriod (
139                         ContentTime::from_seconds (109), ContentTime::from_seconds (110)
140                         ), false
141                 );
142         BOOST_CHECK_EQUAL (cts.size(), 1);
143
144         PositionImage image = render_subtitles (cts.front().subs, dcp::Size (1998, 1080));
145         write_image (image.image, "build/test/subrip_render_test.png");
146         check_file ("build/test/subrip_render_test.png", "test/data/subrip_render_test.png");
147 }
148 #endif