Rearrange encoder threading.
[dcpomatic.git] / test / j2k_encode_threading_test.cc
1 /*
2     Copyright (C) 2023 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 #include "lib/config.h"
23 #include "lib/content_factory.h"
24 #include "lib/dcp_encoder.h"
25 #include "lib/dcp_transcode_job.h"
26 #include "lib/encode_server_description.h"
27 #include "lib/film.h"
28 #include "lib/j2k_encoder.h"
29 #include "lib/job_manager.h"
30 #include "lib/make_dcp.h"
31 #include "lib/transcode_job.h"
32 #include "test.h"
33 #include <dcp/cpl.h>
34 #include <dcp/dcp.h>
35 #include <dcp/reel.h>
36 #include <dcp/reel_picture_asset.h>
37 #include <boost/test/unit_test.hpp>
38
39
40 using std::dynamic_pointer_cast;
41 using std::list;
42
43
44 BOOST_AUTO_TEST_CASE(local_threads_created_and_destroyed)
45 {
46         auto film = new_test_film2("local_threads_created_and_destroyed", {});
47         Writer writer(film, {});
48         J2KEncoder encoder(film, writer);
49
50         encoder.remake_threads(32, 0, {});
51         BOOST_CHECK_EQUAL(encoder._threads.size(), 32U);
52
53         encoder.remake_threads(9, 0, {});
54         BOOST_CHECK_EQUAL(encoder._threads.size(), 9U);
55
56         encoder.end();
57         BOOST_CHECK_EQUAL(encoder._threads.size(), 0U);
58 }
59
60
61 BOOST_AUTO_TEST_CASE(remote_threads_created_and_destroyed)
62 {
63         auto film = new_test_film2("remote_threads_created_and_destroyed", {});
64         Writer writer(film, {});
65         J2KEncoder encoder(film, writer);
66
67         list<EncodeServerDescription> servers = {
68                 { "fred", 7, SERVER_LINK_VERSION },
69                 { "jim", 2, SERVER_LINK_VERSION },
70                 { "sheila", 14, SERVER_LINK_VERSION },
71         };
72
73         encoder.remake_threads(0, 0, servers);
74         BOOST_CHECK_EQUAL(encoder._threads.size(), 7U + 2U + 14U);
75
76         servers = {
77                 { "fred", 7, SERVER_LINK_VERSION },
78                 { "jim", 5, SERVER_LINK_VERSION },
79                 { "sheila", 14, SERVER_LINK_VERSION },
80         };
81
82         encoder.remake_threads(0, 0, servers);
83         BOOST_CHECK_EQUAL(encoder._threads.size(), 7U + 5U + 14U);
84
85         servers = {
86                 { "fred", 0, SERVER_LINK_VERSION },
87                 { "jim", 0, SERVER_LINK_VERSION },
88                 { "sheila", 11, SERVER_LINK_VERSION },
89         };
90
91         encoder.remake_threads(0, 0, servers);
92         BOOST_CHECK_EQUAL(encoder._threads.size(), 11U);
93 }
94
95
96 BOOST_AUTO_TEST_CASE(frames_not_lost_when_threads_disappear)
97 {
98         auto content = content_factory(TestPaths::private_data() / "clapperboard.mp4");
99         auto film = new_test_film2("frames_not_lost", content);
100         film->write_metadata();
101         auto job = make_dcp(film, TranscodeJob::ChangedBehaviour::IGNORE);
102         auto& encoder = dynamic_pointer_cast<DCPEncoder>(job->_encoder)->_j2k_encoder;
103
104         while (JobManager::instance()->work_to_do()) {
105                 encoder.remake_threads(rand() % 8, 0, {});
106                 dcpomatic_sleep_seconds(1);
107         }
108
109         BOOST_CHECK(!JobManager::instance()->errors());
110
111         dcp::DCP dcp(film->dir(film->dcp_name()));
112         dcp.read();
113         BOOST_REQUIRE_EQUAL(dcp.cpls().size(), 1U);
114         BOOST_REQUIRE_EQUAL(dcp.cpls()[0]->reels().size(), 1U);
115         BOOST_REQUIRE_EQUAL(dcp.cpls()[0]->reels()[0]->main_picture()->intrinsic_duration(), 423U);
116 }
117