Add some logging to the KDM creator.
[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 BOOST_AUTO_TEST_CASE (file_naming_test)
66 {
67         Keep k;
68         Config::instance()->set_dcp_asset_filename_format (dcp::NameFormat("%c"));
69
70         auto film = new_test_film ("file_naming_test");
71         film->set_name ("file_naming_test");
72         film->set_video_frame_rate (24);
73         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
74         auto r = make_shared<FFmpegContent>("test/data/flat_red.png");
75         film->examine_and_add_content (r);
76         auto g = make_shared<FFmpegContent>("test/data/flat_green.png");
77         film->examine_and_add_content (g);
78         auto b = make_shared<FFmpegContent>("test/data/flat_blue.png");
79         film->examine_and_add_content (b);
80         BOOST_REQUIRE (!wait_for_jobs());
81
82         r->set_position (film, dcpomatic::DCPTime::from_seconds(0));
83         r->set_video_frame_rate (24);
84         r->video->set_length (24);
85         g->set_position (film, dcpomatic::DCPTime::from_seconds(1));
86         g->set_video_frame_rate (24);
87         g->video->set_length (24);
88         b->set_position (film, dcpomatic::DCPTime::from_seconds(2));
89         b->set_video_frame_rate (24);
90         b->video->set_length (24);
91
92         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
93         film->write_metadata ();
94         make_and_verify_dcp (
95                 film,
96                 {
97                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
98                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
99                 });
100
101         int got[3] = { 0, 0, 0 };
102         for (auto i: boost::filesystem::directory_iterator(film->file(film->dcp_name()))) {
103                 if (boost::regex_match(i.path().string(), boost::regex(".*flat_red\\.png_.*\\.mxf"))) {
104                         ++got[0];
105                 } else if (boost::regex_match(i.path().string(), boost::regex(".*flat_green\\.png_.*\\.mxf"))) {
106                         ++got[1];
107                 } else if (boost::regex_match(i.path().string(), boost::regex(".*flat_blue\\.png_.*\\.mxf"))) {
108                         ++got[2];
109                 }
110         }
111
112         for (int i = 0; i < 3; ++i) {
113                 BOOST_CHECK (got[i] == 2);
114         }
115 }
116
117
118 BOOST_AUTO_TEST_CASE (file_naming_test2)
119 {
120         Keep k;
121         Config::instance()->set_dcp_asset_filename_format (dcp::NameFormat ("%c"));
122
123         auto film = new_test_film ("file_naming_test2");
124         film->set_name ("file_naming_test2");
125         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
126
127 #ifdef DCPOMATIC_WINDOWS
128         /* This is necessary so that the UTF8 string constant below gets converted properly */
129         std::locale::global(boost::locale::generator().generate(""));
130         boost::filesystem::path::imbue(std::locale());
131 #endif
132
133         auto r = make_shared<FFmpegContent>("test/data/flät_red.png");
134         film->examine_and_add_content (r);
135         auto g = make_shared<FFmpegContent>("test/data/flat_green.png");
136         film->examine_and_add_content (g);
137         auto b = make_shared<FFmpegContent>("test/data/flat_blue.png");
138         film->examine_and_add_content (b);
139         BOOST_REQUIRE (!wait_for_jobs());
140
141         r->set_position (film, dcpomatic::DCPTime::from_seconds(0));
142         r->set_video_frame_rate (24);
143         r->video->set_length (24);
144         g->set_position (film, dcpomatic::DCPTime::from_seconds(1));
145         g->set_video_frame_rate (24);
146         g->video->set_length (24);
147         b->set_position (film, dcpomatic::DCPTime::from_seconds(2));
148         b->set_video_frame_rate (24);
149         b->video->set_length (24);
150
151         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
152         make_and_verify_dcp (
153                 film,
154                 {
155                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
156                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
157                 });
158
159         int got[3] = { 0, 0, 0 };
160         for (auto i: boost::filesystem::directory_iterator (film->file(film->dcp_name()))) {
161                 if (boost::regex_match(i.path().string(), boost::regex(".*flat_red\\.png_.*\\.mxf"))) {
162                         ++got[0];
163                 } else if (boost::regex_match(i.path().string(), boost::regex(".*flat_green\\.png_.*\\.mxf"))) {
164                         ++got[1];
165                 } else if (boost::regex_match(i.path().string(), boost::regex(".*flat_blue\\.png_.*\\.mxf"))) {
166                         ++got[2];
167                 }
168         }
169
170         for (int i = 0; i < 3; ++i) {
171                 BOOST_CHECK (got[i] == 2);
172         }
173 }
174
175
176 BOOST_AUTO_TEST_CASE (subtitle_file_naming)
177 {
178         Keep keep;
179
180         Config::instance()->set_dcp_asset_filename_format(dcp::NameFormat("%t ostrabagalous %c"));
181
182         auto content = content_factory("test/data/15s.srt");
183         auto film = new_test_film2("subtitle_file_naming", content);
184         film->set_interop(false);
185
186         make_and_verify_dcp (
187                 film,
188                 {
189                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
190                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
191                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
192                 });
193
194         int got = 0;
195
196         for (auto i: boost::filesystem::directory_iterator(film->file(film->dcp_name()))) {
197                 if (boost::regex_match(i.path().filename().string(), boost::regex("sub_ostrabagalous_15s.*\\.mxf"))) {
198                         ++got;
199                 }
200         }
201
202         BOOST_CHECK_EQUAL(got, 1);
203 }
204