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 server class.
26 * Create a test image and then encode it using the standard mechanism
27 * and also using a EncodeServer object running on localhost. Compare the resulting
28 * encoded data to check that they are the same.
32 #include "lib/cross.h"
33 #include "lib/dcp_video.h"
34 #include "lib/dcpomatic_log.h"
35 #include "lib/encode_server.h"
36 #include "lib/encode_server_description.h"
37 #include "lib/file_log.h"
38 #include "lib/image.h"
39 #include "lib/j2k_encoder_cpu_backend.h"
40 #include "lib/j2k_encoder_remote_backend.h"
41 #include "lib/j2k_image_proxy.h"
42 #include "lib/player_video.h"
43 #include "lib/raw_image_proxy.h"
45 #include <boost/test/unit_test.hpp>
46 #include <boost/thread.hpp>
50 using std::make_shared;
51 using std::shared_ptr;
54 using boost::optional;
56 using namespace dcpomatic;
60 do_remote_encode (DCPVideo frame, EncodeServerDescription description, ArrayData locally_encoded)
62 optional<ArrayData> remotely_encoded;
63 J2KEncoderRemoteBackend backend (description, 1200);
64 BOOST_REQUIRE_NO_THROW (remotely_encoded = backend.encode(frame));
65 BOOST_REQUIRE (static_cast<bool>(remotely_encoded));
67 BOOST_REQUIRE_EQUAL (locally_encoded.size(), remotely_encoded->size());
68 BOOST_CHECK_EQUAL (memcmp (locally_encoded.data(), remotely_encoded->data(), locally_encoded.size()), 0);
72 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
74 auto image = make_shared<Image>(AV_PIX_FMT_RGB24, dcp::Size (1998, 1080), Image::Alignment::PADDED);
75 uint8_t* p = image->data()[0];
77 for (int y = 0; y < 1080; ++y) {
79 for (int x = 0; x < 1998; ++x) {
84 p += image->stride()[0];
87 auto sub_image = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (100, 200), Image::Alignment::PADDED);
88 p = sub_image->data()[0];
89 for (int y = 0; y < 200; ++y) {
91 for (int x = 0; x < 100; ++x) {
97 p += sub_image->stride()[0];
100 LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_rgb.log"));
102 auto pvf = std::make_shared<PlayerVideo>(
103 make_shared<RawImageProxy>(image),
106 dcp::Size (1998, 1080),
107 dcp::Size (1998, 1080),
117 pvf->set_text (PositionImage(sub_image, Position<int>(50, 60)));
127 J2KEncoderCPUBackend cpu;
128 auto locally_encoded = cpu.encode (frame);
129 BOOST_REQUIRE (static_cast<bool>(locally_encoded));
131 auto server = new EncodeServer (true, 2);
133 auto server_thread = new thread (boost::bind(&EncodeServer::run, server));
135 /* Let the server get itself ready */
136 dcpomatic_sleep_seconds (1);
138 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
139 EncodeServerDescription description ("127.0.0.1", 1, SERVER_LINK_VERSION);
141 list<thread*> threads;
142 for (int i = 0; i < 8; ++i) {
143 threads.push_back (new thread(boost::bind(do_remote_encode, frame, description, *locally_encoded)));
146 for (auto i: threads) {
150 for (auto i: threads) {
155 server_thread->join ();
156 delete server_thread;
161 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
163 auto image = make_shared<Image>(AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), Image::Alignment::PADDED);
165 for (int i = 0; i < image->planes(); ++i) {
166 uint8_t* p = image->data()[i];
167 for (int j = 0; j < image->line_size()[i]; ++j) {
172 auto sub_image = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (100, 200), Image::Alignment::PADDED);
173 uint8_t* p = sub_image->data()[0];
174 for (int y = 0; y < 200; ++y) {
176 for (int x = 0; x < 100; ++x) {
179 *q++ = (x + y) % 256;
182 p += sub_image->stride()[0];
185 LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_yuv.log"));
187 auto pvf = std::make_shared<PlayerVideo>(
188 std::make_shared<RawImageProxy>(image),
191 dcp::Size(1998, 1080),
192 dcp::Size(1998, 1080),
202 pvf->set_text (PositionImage(sub_image, Position<int>(50, 60)));
212 J2KEncoderCPUBackend cpu;
213 auto locally_encoded = cpu.encode (frame);
214 BOOST_REQUIRE (static_cast<bool>(locally_encoded));
216 auto server = new EncodeServer (true, 2);
218 auto server_thread = new thread(boost::bind(&EncodeServer::run, server));
220 /* Let the server get itself ready */
221 dcpomatic_sleep_seconds (1);
223 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
224 EncodeServerDescription description ("127.0.0.1", 2, SERVER_LINK_VERSION);
226 list<thread*> threads;
227 for (int i = 0; i < 8; ++i) {
228 threads.push_back (new thread(boost::bind(do_remote_encode, frame, description, *locally_encoded)));
231 for (auto i: threads) {
235 for (auto i: threads) {
240 server_thread->join ();
241 delete server_thread;
246 BOOST_AUTO_TEST_CASE (client_server_test_j2k)
248 auto image = make_shared<Image>(AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), Image::Alignment::PADDED);
250 for (int i = 0; i < image->planes(); ++i) {
251 uint8_t* p = image->data()[i];
252 for (int j = 0; j < image->line_size()[i]; ++j) {
257 LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_j2k.log"));
259 auto raw_pvf = std::make_shared<PlayerVideo> (
260 std::make_shared<RawImageProxy>(image),
263 dcp::Size(1998, 1080),
264 dcp::Size(1998, 1080),
282 J2KEncoderCPUBackend cpu;
283 auto raw_locally_encoded = cpu.encode (raw_frame);
284 BOOST_REQUIRE (static_cast<bool>(raw_locally_encoded));
286 auto j2k_pvf = std::make_shared<PlayerVideo> (
287 std::make_shared<J2KImageProxy>(*raw_locally_encoded, dcp::Size(1998, 1080), AV_PIX_FMT_XYZ12LE),
290 dcp::Size(1998, 1080),
291 dcp::Size(1998, 1080),
294 PresetColourConversion::all().front().conversion,
309 auto j2k_locally_encoded = cpu.encode(j2k_frame);
310 BOOST_REQUIRE (static_cast<bool>(j2k_locally_encoded));
312 auto server = new EncodeServer (true, 2);
314 auto server_thread = new thread (boost::bind (&EncodeServer::run, server));
316 /* Let the server get itself ready */
317 dcpomatic_sleep_seconds (1);
319 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
320 EncodeServerDescription description ("127.0.0.1", 2, SERVER_LINK_VERSION);
322 list<thread*> threads;
323 for (int i = 0; i < 8; ++i) {
324 threads.push_back (new thread(boost::bind(do_remote_encode, j2k_frame, description, *j2k_locally_encoded)));
327 for (auto i: threads) {
331 for (auto i: threads) {
336 server_thread->join ();
337 delete server_thread;