Run all tests with lots of encoding threads.
[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 boost::shared_ptr;
43
44 /* Check that DCPDecoder reuses old data when it should */
45 BOOST_AUTO_TEST_CASE (check_reuse_old_data_test)
46 {
47         /* Make some DCPs */
48
49         shared_ptr<Film> ov = new_test_film2 ("check_reuse_old_data_ov");
50         ov->examine_and_add_content (content_factory("test/data/flat_red.png").front());
51         BOOST_REQUIRE (!wait_for_jobs());
52         ov->make_dcp ();
53         BOOST_REQUIRE (!wait_for_jobs());
54
55         shared_ptr<Film> vf = new_test_film2 ("check_reuse_old_data_vf");
56         shared_ptr<DCPContent> ov_content(new DCPContent(ov->dir(ov->dcp_name(false))));
57         vf->examine_and_add_content (ov_content);
58         vf->examine_and_add_content (content_factory("test/data/L.wav").front());
59         BOOST_REQUIRE (!wait_for_jobs());
60         ov_content->set_reference_video (true);
61         vf->make_dcp ();
62         BOOST_REQUIRE (!wait_for_jobs());
63
64         shared_ptr<Film> encrypted = new_test_film2 ("check_reuse_old_data_decrypted");
65         encrypted->examine_and_add_content (content_factory("test/data/flat_red.png").front());
66         BOOST_REQUIRE (!wait_for_jobs());
67         encrypted->set_encrypted (true);
68         encrypted->make_dcp ();
69         BOOST_REQUIRE (!wait_for_jobs());
70
71         dcp::DCP encrypted_dcp (encrypted->dir(encrypted->dcp_name()));
72         encrypted_dcp.read ();
73
74         dcp::EncryptedKDM kdm = encrypted->make_kdm (
75                 Config::instance()->decryption_chain()->leaf(),
76                 vector<string>(),
77                 encrypted_dcp.cpls().front()->file().get(),
78                 dcp::LocalTime ("2030-07-21T00:00:00+00:00"),
79                 dcp::LocalTime ("2031-07-21T00:00:00+00:00"),
80                 dcp::MODIFIED_TRANSITIONAL_1,
81                 true, 0
82                 );
83
84
85         /* Add just the OV to a new project, move it around a bit and check that
86            the _reels get reused.
87         */
88         shared_ptr<Film> test = new_test_film2 ("check_reuse_old_data_test1");
89         ov_content.reset (new DCPContent(ov->dir(ov->dcp_name(false))));
90         test->examine_and_add_content (ov_content);
91         BOOST_REQUIRE (!wait_for_jobs());
92         shared_ptr<Player> player (new Player(test));
93
94         shared_ptr<DCPDecoder> decoder = boost::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
95         BOOST_REQUIRE (decoder);
96         list<shared_ptr<dcp::Reel> > reels = decoder->reels();
97
98         ov_content->set_position (test, dcpomatic::DCPTime(96000));
99         decoder = boost::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
100         BOOST_REQUIRE (decoder);
101         BOOST_REQUIRE (reels == decoder->reels());
102
103         /* Add the VF to a new project, then add the OV and check that the
104            _reels did not get reused.
105         */
106         test = new_test_film2 ("check_reuse_old_data_test2");
107         shared_ptr<DCPContent> vf_content (new DCPContent(vf->dir(vf->dcp_name(false))));
108         test->examine_and_add_content (vf_content);
109         BOOST_REQUIRE (!wait_for_jobs());
110         player.reset (new Player(test));
111
112         decoder = boost::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
113         BOOST_REQUIRE (decoder);
114         reels = decoder->reels();
115
116         vf_content->add_ov (ov->dir(ov->dcp_name(false)));
117         JobManager::instance()->add (shared_ptr<Job>(new ExamineContentJob(test, vf_content)));
118         BOOST_REQUIRE (!wait_for_jobs());
119         decoder = boost::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
120         BOOST_REQUIRE (decoder);
121         BOOST_REQUIRE (reels != decoder->reels());
122
123         /* Add a KDM to an encrypted DCP and check that the _reels did not get reused */
124         test = new_test_film2 ("check_reuse_old_data_test3");
125         shared_ptr<DCPContent> encrypted_content (new DCPContent(encrypted->dir(encrypted->dcp_name(false))));
126         test->examine_and_add_content (encrypted_content);
127         BOOST_REQUIRE (!wait_for_jobs());
128         player.reset (new Player(test));
129
130         decoder = boost::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
131         BOOST_REQUIRE (decoder);
132         reels = decoder->reels();
133
134         encrypted_content->add_kdm (kdm);
135         JobManager::instance()->add (shared_ptr<Job>(new ExamineContentJob(test, encrypted_content)));
136         BOOST_REQUIRE (!wait_for_jobs());
137         decoder = boost::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
138         BOOST_REQUIRE (decoder);
139         BOOST_REQUIRE (reels != decoder->reels());
140 }