Adapt for libdcp use of enum class.
[dcpomatic.git] / test / dcp_decoder_test.cc
1 /*
2     Copyright (C) 2019 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/dcp_decoder_test.cc
22  *  @brief Test DCPDecoder class.
23  *  @ingroup selfcontained
24  */
25
26 #include "lib/film.h"
27 #include "lib/dcp_content.h"
28 #include "lib/dcp_decoder.h"
29 #include "lib/content_factory.h"
30 #include "lib/player.h"
31 #include "lib/examine_content_job.h"
32 #include "lib/job_manager.h"
33 #include "lib/config.h"
34 #include "test.h"
35 #include <dcp/dcp.h>
36 #include <boost/test/unit_test.hpp>
37 #include <iostream>
38
39 using std::list;
40 using std::string;
41 using std::vector;
42 using std::make_shared;
43 using std::shared_ptr;
44
45 /* Check that DCPDecoder reuses old data when it should */
46 BOOST_AUTO_TEST_CASE (check_reuse_old_data_test)
47 {
48         /* Make some DCPs */
49
50         auto ov = new_test_film2 ("check_reuse_old_data_ov");
51         ov->examine_and_add_content (content_factory("test/data/flat_red.png").front());
52         BOOST_REQUIRE (!wait_for_jobs());
53         ov->make_dcp ();
54         BOOST_REQUIRE (!wait_for_jobs());
55
56         auto vf = new_test_film2 ("check_reuse_old_data_vf");
57         auto ov_content = make_shared<DCPContent>(ov->dir(ov->dcp_name(false)));
58         vf->examine_and_add_content (ov_content);
59         vf->examine_and_add_content (content_factory("test/data/L.wav").front());
60         BOOST_REQUIRE (!wait_for_jobs());
61         ov_content->set_reference_video (true);
62         vf->make_dcp ();
63         BOOST_REQUIRE (!wait_for_jobs());
64
65         auto encrypted = new_test_film2 ("check_reuse_old_data_decrypted");
66         encrypted->examine_and_add_content (content_factory("test/data/flat_red.png").front());
67         BOOST_REQUIRE (!wait_for_jobs());
68         encrypted->set_encrypted (true);
69         encrypted->make_dcp ();
70         BOOST_REQUIRE (!wait_for_jobs());
71
72         dcp::DCP encrypted_dcp (encrypted->dir(encrypted->dcp_name()));
73         encrypted_dcp.read ();
74
75         auto kdm = encrypted->make_kdm (
76                 Config::instance()->decryption_chain()->leaf(),
77                 vector<string>(),
78                 encrypted_dcp.cpls().front()->file().get(),
79                 dcp::LocalTime ("2030-07-21T00:00:00+00:00"),
80                 dcp::LocalTime ("2031-07-21T00:00:00+00:00"),
81                 dcp::Formulation::MODIFIED_TRANSITIONAL_1,
82                 true, 0
83                 );
84
85
86         /* Add just the OV to a new project, move it around a bit and check that
87            the _reels get reused.
88         */
89         auto test = new_test_film2 ("check_reuse_old_data_test1");
90         ov_content = make_shared<DCPContent>(ov->dir(ov->dcp_name(false)));
91         test->examine_and_add_content (ov_content);
92         BOOST_REQUIRE (!wait_for_jobs());
93         auto player = make_shared<Player>(test);
94
95         auto decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
96         BOOST_REQUIRE (decoder);
97         auto reels = decoder->reels();
98
99         ov_content->set_position (test, dcpomatic::DCPTime(96000));
100         decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
101         BOOST_REQUIRE (decoder);
102         BOOST_REQUIRE (reels == decoder->reels());
103
104         /* Add the VF to a new project, then add the OV and check that the
105            _reels did not get reused.
106         */
107         test = new_test_film2 ("check_reuse_old_data_test2");
108         auto vf_content = make_shared<DCPContent>(vf->dir(vf->dcp_name(false)));
109         test->examine_and_add_content (vf_content);
110         BOOST_REQUIRE (!wait_for_jobs());
111         player.reset (new Player(test));
112
113         decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
114         BOOST_REQUIRE (decoder);
115         reels = decoder->reels();
116
117         vf_content->add_ov (ov->dir(ov->dcp_name(false)));
118         JobManager::instance()->add (make_shared<ExamineContentJob>(test, vf_content));
119         BOOST_REQUIRE (!wait_for_jobs());
120         decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
121         BOOST_REQUIRE (decoder);
122         BOOST_REQUIRE (reels != decoder->reels());
123
124         /* Add a KDM to an encrypted DCP and check that the _reels did not get reused */
125         test = new_test_film2 ("check_reuse_old_data_test3");
126         auto encrypted_content = make_shared<DCPContent>(encrypted->dir(encrypted->dcp_name(false)));
127         test->examine_and_add_content (encrypted_content);
128         BOOST_REQUIRE (!wait_for_jobs());
129         player = make_shared<Player>(test);
130
131         decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
132         BOOST_REQUIRE (decoder);
133         reels = decoder->reels();
134
135         encrypted_content->add_kdm (kdm);
136         JobManager::instance()->add (make_shared<ExamineContentJob>(test, encrypted_content));
137         BOOST_REQUIRE (!wait_for_jobs());
138         decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
139         BOOST_REQUIRE (decoder);
140         BOOST_REQUIRE (reels != decoder->reels());
141 }