Allow DCP content to store and serialise metadata.
[dcpomatic.git] / test / import_dcp_test.cc
1 /*
2     Copyright (C) 2014-2020 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/import_dcp_test.cc
22  *  @brief Test import of encrypted DCPs.
23  *  @ingroup specific
24  */
25
26 #include "test.h"
27 #include "lib/film.h"
28 #include "lib/screen.h"
29 #include "lib/dcp_subtitle_content.h"
30 #include "lib/ratio.h"
31 #include "lib/dcp_content_type.h"
32 #include "lib/dcp_content.h"
33 #include "lib/ffmpeg_content.h"
34 #include "lib/examine_content_job.h"
35 #include "lib/job_manager.h"
36 #include "lib/config.h"
37 #include "lib/cross.h"
38 #include "lib/video_content.h"
39 #include "lib/content_factory.h"
40 #include <dcp/cpl.h>
41 #include <boost/test/unit_test.hpp>
42
43 using std::vector;
44 using std::string;
45 using std::map;
46 using boost::shared_ptr;
47 using boost::dynamic_pointer_cast;
48
49 /** Make an encrypted DCP, import it and make a new unencrypted DCP */
50 BOOST_AUTO_TEST_CASE (import_dcp_test)
51 {
52         shared_ptr<Film> A = new_test_film ("import_dcp_test");
53         A->set_container (Ratio::from_id ("185"));
54         A->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
55         A->set_name ("frobozz");
56         A->set_interop (false);
57
58         shared_ptr<FFmpegContent> c (new FFmpegContent("test/data/test.mp4"));
59         A->examine_and_add_content (c);
60         A->set_encrypted (true);
61         BOOST_CHECK (!wait_for_jobs ());
62
63         A->make_dcp ();
64         BOOST_CHECK (!wait_for_jobs ());
65
66         dcp::DCP A_dcp ("build/test/import_dcp_test/" + A->dcp_name());
67         A_dcp.read ();
68
69         Config::instance()->set_decryption_chain (shared_ptr<dcp::CertificateChain> (new dcp::CertificateChain (openssl_path ())));
70
71         /* Dear future-carl: I suck!  I thought you wouldn't still be running these tests in 2030!  Sorry! */
72         dcp::EncryptedKDM kdm = A->make_kdm (
73                 Config::instance()->decryption_chain()->leaf (),
74                 vector<string>(),
75                 A_dcp.cpls().front()->file().get(),
76                 dcp::LocalTime ("2030-07-21T00:00:00+00:00"),
77                 dcp::LocalTime ("2031-07-21T00:00:00+00:00"),
78                 dcp::MODIFIED_TRANSITIONAL_1,
79                 true, 0
80                 );
81
82         shared_ptr<Film> B = new_test_film ("import_dcp_test2");
83         B->set_container (Ratio::from_id ("185"));
84         B->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
85         B->set_name ("frobozz");
86         B->set_interop (false);
87
88         shared_ptr<DCPContent> d (new DCPContent("build/test/import_dcp_test/" + A->dcp_name()));
89         B->examine_and_add_content (d);
90         BOOST_CHECK (!wait_for_jobs ());
91         d->add_kdm (kdm);
92         JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (B, d)));
93         BOOST_CHECK (!wait_for_jobs ());
94
95         B->make_dcp ();
96         BOOST_CHECK (!wait_for_jobs ());
97
98         /* Should be 1s red, 1s green, 1s blue */
99         check_dcp ("test/data/import_dcp_test2", "build/test/import_dcp_test2/" + B->dcp_name());
100 }
101
102
103 /** Check that DCP markers are imported correctly */
104 BOOST_AUTO_TEST_CASE (import_dcp_markers_test)
105 {
106         /* Make a DCP with some markers */
107         shared_ptr<Film> film = new_test_film2 ("import_dcp_markers_test");
108         shared_ptr<Content> content = content_factory("test/data/flat_red.png").front();
109         film->examine_and_add_content (content);
110         BOOST_REQUIRE (!wait_for_jobs());
111
112         content->video->set_length (24 * 60 * 10);
113
114         film->set_marker(dcp::FFOC, dcpomatic::DCPTime::from_seconds(1.91));
115         film->set_marker(dcp::FFMC, dcpomatic::DCPTime::from_seconds(9.4));
116         film->set_marker(dcp::LFMC, dcpomatic::DCPTime::from_seconds(9.99));
117
118         film->make_dcp ();
119         BOOST_REQUIRE (!wait_for_jobs());
120
121         /* Import the DCP to a new film and check the markers */
122         shared_ptr<Film> film2 = new_test_film2 ("import_dcp_markers_test2");
123         shared_ptr<DCPContent> imported (new DCPContent(film->dir(film->dcp_name())));
124         film2->examine_and_add_content (imported);
125         BOOST_REQUIRE (!wait_for_jobs());
126         film2->write_metadata ();
127
128         BOOST_CHECK_EQUAL (imported->markers().size(), 3);
129
130         map<dcp::Marker, dcpomatic::ContentTime> markers = imported->markers();
131         BOOST_REQUIRE(markers.find(dcp::FFOC) != markers.end());
132         BOOST_CHECK(markers[dcp::FFOC] == dcpomatic::ContentTime(184000));
133         BOOST_REQUIRE(markers.find(dcp::FFMC) != markers.end());
134         BOOST_CHECK(markers[dcp::FFMC] == dcpomatic::ContentTime(904000));
135         BOOST_REQUIRE(markers.find(dcp::LFMC) != markers.end());
136         BOOST_CHECK(markers[dcp::LFMC] == dcpomatic::ContentTime(960000));
137
138         /* Load that film and check that the markers have been loaded */
139         shared_ptr<Film> film3(new Film(boost::filesystem::path("build/test/import_dcp_markers_test2")));
140         film3->read_metadata ();
141         BOOST_REQUIRE (film3->content().size() == 1);
142         shared_ptr<DCPContent> reloaded = dynamic_pointer_cast<DCPContent>(film3->content().front());
143         BOOST_REQUIRE (reloaded);
144
145         BOOST_CHECK_EQUAL (reloaded->markers().size(), 3);
146
147         markers = reloaded->markers();
148         BOOST_REQUIRE(markers.find(dcp::FFOC) != markers.end());
149         BOOST_CHECK(markers[dcp::FFOC] == dcpomatic::ContentTime(184000));
150         BOOST_REQUIRE(markers.find(dcp::FFMC) != markers.end());
151         BOOST_CHECK(markers[dcp::FFMC] == dcpomatic::ContentTime(904000));
152         BOOST_REQUIRE(markers.find(dcp::LFMC) != markers.end());
153         BOOST_CHECK(markers[dcp::LFMC] == dcpomatic::ContentTime(960000));
154 }
155
156
157 /** Check that DCP metadata (ratings and content version) are imported correctly */
158 BOOST_AUTO_TEST_CASE (import_dcp_metadata_test)
159 {
160         /* Make a DCP with some ratings and a content version */
161         shared_ptr<Film> film = new_test_film2 ("import_dcp_metadata_test");
162         shared_ptr<Content> content = content_factory("test/data/flat_red.png").front();
163         film->examine_and_add_content (content);
164         BOOST_REQUIRE (!wait_for_jobs());
165
166         content->video->set_length (10);
167
168         std::vector<dcp::Rating> ratings;
169         ratings.push_back (dcp::Rating("BBFC", "15"));
170         ratings.push_back (dcp::Rating("MPAA", "NC-17"));
171         film->set_ratings (ratings);
172
173         film->set_content_version ("Fred");
174
175         film->make_dcp ();
176         BOOST_REQUIRE (!wait_for_jobs());
177
178         /* Import the DCP to a new film and check the metadata */
179         shared_ptr<Film> film2 = new_test_film2 ("import_dcp_metadata_test2");
180         shared_ptr<DCPContent> imported (new DCPContent(film->dir(film->dcp_name())));
181         film2->examine_and_add_content (imported);
182         BOOST_REQUIRE (!wait_for_jobs());
183         film2->write_metadata ();
184
185         BOOST_CHECK (imported->ratings() == ratings);
186         BOOST_CHECK_EQUAL (imported->content_version(), "Fred");
187
188         /* Load that film and check that the metadata has been loaded */
189         shared_ptr<Film> film3(new Film(boost::filesystem::path("build/test/import_dcp_metadata_test2")));
190         film3->read_metadata ();
191         BOOST_REQUIRE (film3->content().size() == 1);
192         shared_ptr<DCPContent> reloaded = dynamic_pointer_cast<DCPContent>(film3->content().front());
193         BOOST_REQUIRE (reloaded);
194
195         BOOST_CHECK (reloaded->ratings() == ratings);
196         BOOST_CHECK_EQUAL (reloaded->content_version(), "Fred");
197 }
198