2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 /** @file test/client_server_test.cc
23 * @brief Test the remote encoding code.
28 #include "lib/content_factory.h"
29 #include "lib/cross.h"
30 #include "lib/dcp_video.h"
31 #include "lib/dcpomatic_log.h"
32 #include "lib/encode_server.h"
33 #include "lib/encode_server_description.h"
34 #include "lib/encode_server_finder.h"
35 #include "lib/file_log.h"
36 #include "lib/image.h"
37 #include "lib/j2k_image_proxy.h"
38 #include "lib/player_video.h"
39 #include "lib/raw_image_proxy.h"
41 #include <boost/test/unit_test.hpp>
42 #include <boost/thread.hpp>
46 using std::make_shared;
47 using std::shared_ptr;
50 using boost::optional;
52 using namespace dcpomatic;
56 do_remote_encode (shared_ptr<DCPVideo> frame, EncodeServerDescription description, ArrayData locally_encoded)
58 ArrayData remotely_encoded;
59 BOOST_REQUIRE_NO_THROW (remotely_encoded = frame->encode_remotely (description, 1200));
61 BOOST_REQUIRE_EQUAL (locally_encoded.size(), remotely_encoded.size());
62 BOOST_CHECK_EQUAL (memcmp (locally_encoded.data(), remotely_encoded.data(), locally_encoded.size()), 0);
66 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
68 auto image = make_shared<Image>(AV_PIX_FMT_RGB24, dcp::Size (1998, 1080), Image::Alignment::PADDED);
69 uint8_t* p = image->data()[0];
71 for (int y = 0; y < 1080; ++y) {
73 for (int x = 0; x < 1998; ++x) {
78 p += image->stride()[0];
81 auto sub_image = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (100, 200), Image::Alignment::PADDED);
82 p = sub_image->data()[0];
83 for (int y = 0; y < 200; ++y) {
85 for (int x = 0; x < 100; ++x) {
91 p += sub_image->stride()[0];
94 LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_rgb.log"));
96 auto pvf = std::make_shared<PlayerVideo>(
97 make_shared<RawImageProxy>(image),
100 dcp::Size (1998, 1080),
101 dcp::Size (1998, 1080),
107 optional<ContentTime>(),
111 pvf->set_text (PositionImage(sub_image, Position<int>(50, 60)));
113 auto frame = make_shared<DCPVideo> (
121 auto locally_encoded = frame->encode_locally ();
123 auto server = make_shared<EncodeServer>(true, 2);
125 thread server_thread(boost::bind(&EncodeServer::run, server));
127 /* Let the server get itself ready */
128 dcpomatic_sleep_seconds (1);
130 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
131 EncodeServerDescription description ("127.0.0.1", 1, SERVER_LINK_VERSION);
133 list<thread> threads;
134 for (int i = 0; i < 8; ++i) {
135 threads.push_back(thread(boost::bind(do_remote_encode, frame, description, locally_encoded)));
138 for (auto& i: threads) {
145 server_thread.join();
149 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
151 auto image = make_shared<Image>(AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), Image::Alignment::PADDED);
153 for (int i = 0; i < image->planes(); ++i) {
154 uint8_t* p = image->data()[i];
155 for (int j = 0; j < image->line_size()[i]; ++j) {
160 auto sub_image = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (100, 200), Image::Alignment::PADDED);
161 uint8_t* p = sub_image->data()[0];
162 for (int y = 0; y < 200; ++y) {
164 for (int x = 0; x < 100; ++x) {
167 *q++ = (x + y) % 256;
170 p += sub_image->stride()[0];
173 LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_yuv.log"));
175 auto pvf = std::make_shared<PlayerVideo>(
176 std::make_shared<RawImageProxy>(image),
179 dcp::Size(1998, 1080),
180 dcp::Size(1998, 1080),
186 optional<ContentTime>(),
190 pvf->set_text (PositionImage(sub_image, Position<int>(50, 60)));
192 auto frame = make_shared<DCPVideo>(
200 auto locally_encoded = frame->encode_locally ();
202 auto server = make_shared<EncodeServer>(true, 2);
204 thread server_thread(boost::bind(&EncodeServer::run, server));
206 /* Let the server get itself ready */
207 dcpomatic_sleep_seconds (1);
209 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
210 EncodeServerDescription description ("127.0.0.1", 2, SERVER_LINK_VERSION);
212 list<thread> threads;
213 for (int i = 0; i < 8; ++i) {
214 threads.push_back(thread(boost::bind(do_remote_encode, frame, description, locally_encoded)));
217 for (auto& i: threads) {
224 server_thread.join();
228 BOOST_AUTO_TEST_CASE (client_server_test_j2k)
230 auto image = make_shared<Image>(AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), Image::Alignment::PADDED);
232 for (int i = 0; i < image->planes(); ++i) {
233 uint8_t* p = image->data()[i];
234 for (int j = 0; j < image->line_size()[i]; ++j) {
239 LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_j2k.log"));
241 auto raw_pvf = std::make_shared<PlayerVideo> (
242 std::make_shared<RawImageProxy>(image),
245 dcp::Size(1998, 1080),
246 dcp::Size(1998, 1080),
252 optional<ContentTime>(),
256 auto raw_frame = make_shared<DCPVideo> (
264 auto raw_locally_encoded = raw_frame->encode_locally ();
266 auto j2k_pvf = std::make_shared<PlayerVideo> (
267 std::make_shared<J2KImageProxy>(raw_locally_encoded, dcp::Size(1998, 1080), AV_PIX_FMT_XYZ12LE),
270 dcp::Size(1998, 1080),
271 dcp::Size(1998, 1080),
274 PresetColourConversion::all().front().conversion,
277 optional<ContentTime>(),
281 auto j2k_frame = make_shared<DCPVideo> (
289 auto j2k_locally_encoded = j2k_frame->encode_locally ();
291 auto server = make_shared<EncodeServer>(true, 2);
293 thread server_thread(boost::bind (&EncodeServer::run, server));
295 /* Let the server get itself ready */
296 dcpomatic_sleep_seconds (1);
298 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
299 EncodeServerDescription description ("127.0.0.1", 2, SERVER_LINK_VERSION);
301 list<thread> threads;
302 for (int i = 0; i < 8; ++i) {
303 threads.push_back(thread(boost::bind(do_remote_encode, j2k_frame, description, j2k_locally_encoded)));
306 for (auto& i: threads) {
313 server_thread.join();
317 BOOST_AUTO_TEST_CASE(real_encode_with_server)
319 auto content = content_factory(TestPaths::private_data() / "dolby_aurora.vob");
320 auto film = new_test_film2("real_encode_with_server", content);
322 EncodeServerFinder::instance();
324 EncodeServer server(true, 4);
325 thread server_thread(boost::bind(&EncodeServer::run, &server));
327 make_and_verify_dcp(film);
330 server_thread.join();
332 BOOST_CHECK(server.frames_encoded() > 0);
333 EncodeServerFinder::drop();