summaryrefslogtreecommitdiff
path: root/test/j2k_encode_threading_test.cc
blob: e6f10ee073522182daa7dc43204bd030b3fe2213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
    Copyright (C) 2023 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "lib/config.h"
#include "lib/content_factory.h"
#include "lib/dcp_film_encoder.h"
#include "lib/dcp_transcode_job.h"
#include "lib/encode_server_description.h"
#include "lib/film.h"
#ifdef DCPOMATIC_GROK
#include "lib/grok/context.h"
#endif
#include "lib/j2k_encoder.h"
#include "lib/job_manager.h"
#include "lib/make_dcp.h"
#include "lib/transcode_job.h"
#include "test.h"
#include <dcp/cpl.h>
#include <dcp/dcp.h>
#include <dcp/reel.h>
#include <dcp/reel_picture_asset.h>
#include <boost/test/unit_test.hpp>


using std::dynamic_pointer_cast;
using std::list;


BOOST_AUTO_TEST_CASE(local_threads_created_and_destroyed)
{
	auto film = new_test_film("local_threads_created_and_destroyed", {});
	Writer writer(film, {}, "foo");
	J2KEncoder encoder(film, writer);

	encoder.remake_threads(32, 0, {});
	BOOST_CHECK_EQUAL(encoder._threads.size(), 32U);

	encoder.remake_threads(9, 0, {});
	BOOST_CHECK_EQUAL(encoder._threads.size(), 9U);

	encoder.end();
	BOOST_CHECK_EQUAL(encoder._threads.size(), 0U);
}


BOOST_AUTO_TEST_CASE(remote_threads_created_and_destroyed)
{
	auto film = new_test_film("remote_threads_created_and_destroyed", {});
	Writer writer(film, {}, "foo");
	J2KEncoder encoder(film, writer);

	list<EncodeServerDescription> servers = {
		{ "fred", 7, SERVER_LINK_VERSION },
		{ "jim", 2, SERVER_LINK_VERSION },
		{ "sheila", 14, SERVER_LINK_VERSION },
	};

	encoder.remake_threads(0, 0, servers);
	BOOST_CHECK_EQUAL(encoder._threads.size(), 7U + 2U + 14U);

	servers = {
		{ "fred", 7, SERVER_LINK_VERSION },
		{ "jim", 5, SERVER_LINK_VERSION },
		{ "sheila", 14, SERVER_LINK_VERSION },
	};

	encoder.remake_threads(0, 0, servers);
	BOOST_CHECK_EQUAL(encoder._threads.size(), 7U + 5U + 14U);

	servers = {
		{ "fred", 0, SERVER_LINK_VERSION },
		{ "jim", 0, SERVER_LINK_VERSION },
		{ "sheila", 11, SERVER_LINK_VERSION },
	};

	encoder.remake_threads(0, 0, servers);
	BOOST_CHECK_EQUAL(encoder._threads.size(), 11U);
}


BOOST_AUTO_TEST_CASE(frames_not_lost_when_threads_disappear)
{
	auto content = content_factory(TestPaths::private_data() / "clapperboard.mp4");
	auto film = new_test_film("frames_not_lost", content);
	film->write_metadata();
	auto job = make_dcp(film, TranscodeJob::ChangedBehaviour::IGNORE);

	while (JobManager::instance()->work_to_do()) {
		if (auto encoder = dynamic_cast<J2KEncoder*>(dynamic_pointer_cast<DCPFilmEncoder>(job->_encoder)->_encoder.get())) {
			encoder->remake_threads((rand() % 7) + 1, 0, {});
			dcpomatic_sleep_seconds(1);
		}
	}

	BOOST_CHECK(!JobManager::instance()->errors());

	dcp::DCP dcp(film->dir(film->dcp_name()));
	dcp.read();
	BOOST_REQUIRE_EQUAL(dcp.cpls().size(), 1U);
	BOOST_REQUIRE_EQUAL(dcp.cpls()[0]->reels().size(), 1U);
	BOOST_REQUIRE_EQUAL(dcp.cpls()[0]->reels()[0]->main_picture()->intrinsic_duration(), 423U);
}


#ifdef DCPOMATIC_GROK
BOOST_AUTO_TEST_CASE(transcode_stops_when_gpu_enabled_with_no_gpu)
{
	ConfigRestorer cr;

	grk_plugin::setMessengerLogger(new grk_plugin::GrokLogger("[GROK] "));

	Config::Grok grok;
	grok.enable = true;
	Config::instance()->set_grok(grok);

	auto content = content_factory(TestPaths::private_data() / "clapperboard.mp4");
	auto film = new_test_film("transcode_stops_when_gpu_enabled_with_no_gpu", content);
	film->write_metadata();
	auto job = make_dcp(film, TranscodeJob::ChangedBehaviour::IGNORE);

	int slept = 0;
	while (JobManager::instance()->work_to_do() && slept < 10) {
		dcpomatic_sleep_seconds(1);
		++slept;
	}

	BOOST_REQUIRE(slept < 10);

	JobManager::drop();
}
#endif