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