7175e209ab2a81195530da655bba3e9fc48f1e96
[dcpomatic.git] / test / file_naming_test.cc
1 /*
2     Copyright (C) 2016-2021 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
22 /** @file  test/file_naming_test.cc
23  *  @brief Test how files in DCPs are named.
24  *  @ingroup feature
25  */
26
27
28 #include "test.h"
29 #include "lib/config.h"
30 #include "lib/content_factory.h"
31 #include "lib/dcp_content_type.h"
32 #include "lib/ffmpeg_content.h"
33 #include "lib/film.h"
34 #include "lib/video_content.h"
35 #ifdef DCPOMATIC_WINDOWS
36 #include <boost/locale.hpp>
37 #endif
38 #include <boost/test/unit_test.hpp>
39 #include <boost/regex.hpp>
40
41
42 using std::make_shared;
43 using std::shared_ptr;
44 using std::string;
45
46
47 class Keep
48 {
49 public:
50         Keep ()
51         {
52                 _format = Config::instance()->dcp_asset_filename_format ();
53         }
54
55         ~Keep ()
56         {
57                 Config::instance()->set_dcp_asset_filename_format (_format);
58         }
59
60 private:
61         dcp::NameFormat _format;
62 };
63
64
65 static
66 string
67 mxf_regex(string part) {
68 #ifdef DCPOMATIC_WINDOWS
69         /* Windows replaces . in filenames with _ */
70         return String::compose(".*flat_%1_png_.*\\.mxf", part);
71 #else
72         return String::compose(".*flat_%1\\.png_.*\\.mxf", part);
73 #endif
74 };
75
76
77
78 BOOST_AUTO_TEST_CASE (file_naming_test)
79 {
80         Keep k;
81         Config::instance()->set_dcp_asset_filename_format (dcp::NameFormat("%c"));
82
83         auto film = new_test_film ("file_naming_test");
84         film->set_name ("file_naming_test");
85         film->set_video_frame_rate (24);
86         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
87         auto r = make_shared<FFmpegContent>("test/data/flat_red.png");
88         film->examine_and_add_content (r);
89         auto g = make_shared<FFmpegContent>("test/data/flat_green.png");
90         film->examine_and_add_content (g);
91         auto b = make_shared<FFmpegContent>("test/data/flat_blue.png");
92         film->examine_and_add_content (b);
93         BOOST_REQUIRE (!wait_for_jobs());
94
95         r->set_position (film, dcpomatic::DCPTime::from_seconds(0));
96         r->set_video_frame_rate(film, 24);
97         r->video->set_length (24);
98         g->set_position (film, dcpomatic::DCPTime::from_seconds(1));
99         g->set_video_frame_rate(film, 24);
100         g->video->set_length (24);
101         b->set_position (film, dcpomatic::DCPTime::from_seconds(2));
102         b->set_video_frame_rate(film, 24);
103         b->video->set_length (24);
104
105         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
106         film->write_metadata ();
107         make_and_verify_dcp (
108                 film,
109                 {
110                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
111                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
112                 });
113
114         int got[3] = { 0, 0, 0 };
115         for (auto i: boost::filesystem::directory_iterator(film->file(film->dcp_name()))) {
116                 if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("red")))) {
117                         ++got[0];
118                 } else if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("green")))) {
119                         ++got[1];
120                 } else if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("blue")))) {
121                         ++got[2];
122                 }
123         }
124
125         for (int i = 0; i < 3; ++i) {
126                 BOOST_CHECK (got[i] == 2);
127         }
128 }
129
130
131 BOOST_AUTO_TEST_CASE (file_naming_test2)
132 {
133         Keep k;
134         Config::instance()->set_dcp_asset_filename_format (dcp::NameFormat ("%c"));
135
136         auto film = new_test_film ("file_naming_test2");
137         film->set_name ("file_naming_test2");
138         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
139
140 #ifdef DCPOMATIC_WINDOWS
141         /* This is necessary so that the UTF8 string constant below gets converted properly */
142         std::locale::global(boost::locale::generator().generate(""));
143         boost::filesystem::path::imbue(std::locale());
144 #endif
145
146         auto r = make_shared<FFmpegContent>("test/data/flät_red.png");
147         film->examine_and_add_content (r);
148         auto g = make_shared<FFmpegContent>("test/data/flat_green.png");
149         film->examine_and_add_content (g);
150         auto b = make_shared<FFmpegContent>("test/data/flat_blue.png");
151         film->examine_and_add_content (b);
152         BOOST_REQUIRE (!wait_for_jobs());
153
154         r->set_position (film, dcpomatic::DCPTime::from_seconds(0));
155         r->set_video_frame_rate(film, 24);
156         r->video->set_length (24);
157         g->set_position (film, dcpomatic::DCPTime::from_seconds(1));
158         g->set_video_frame_rate(film, 24);
159         g->video->set_length (24);
160         b->set_position (film, dcpomatic::DCPTime::from_seconds(2));
161         b->set_video_frame_rate(film, 24);
162         b->video->set_length (24);
163
164         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
165         make_and_verify_dcp (
166                 film,
167                 {
168                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
169                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
170                 });
171
172         int got[3] = { 0, 0, 0 };
173         for (auto i: boost::filesystem::directory_iterator (film->file(film->dcp_name()))) {
174                 if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("red")))) {
175                         ++got[0];
176                 } else if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("green")))) {
177                         ++got[1];
178                 } else if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("blue")))) {
179                         ++got[2];
180                 }
181         }
182
183         for (int i = 0; i < 3; ++i) {
184                 BOOST_CHECK (got[i] == 2);
185         }
186 }
187
188
189 BOOST_AUTO_TEST_CASE (subtitle_file_naming)
190 {
191         Keep keep;
192
193         Config::instance()->set_dcp_asset_filename_format(dcp::NameFormat("%t ostrabagalous %c"));
194
195         auto content = content_factory("test/data/15s.srt");
196         auto film = new_test_film2("subtitle_file_naming", content);
197         film->set_interop(false);
198
199         make_and_verify_dcp (
200                 film,
201                 {
202                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
203                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
204                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
205                 });
206
207         int got = 0;
208
209         for (auto i: boost::filesystem::directory_iterator(film->file(film->dcp_name()))) {
210                 if (boost::regex_match(i.path().filename().string(), boost::regex("sub_ostrabagalous_15s.*\\.mxf"))) {
211                         ++got;
212                 }
213         }
214
215         BOOST_CHECK_EQUAL(got, 1);
216 }
217