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_image_proxy.h"
40 #include "lib/player_video.h"
41 #include "lib/raw_image_proxy.h"
43 #include <boost/test/unit_test.hpp>
44 #include <boost/thread.hpp>
48 using std::make_shared;
49 using std::shared_ptr;
52 using boost::optional;
54 using namespace dcpomatic;
58 do_remote_encode (shared_ptr<DCPVideo> frame, EncodeServerDescription description, ArrayData locally_encoded)
60 ArrayData remotely_encoded;
61 BOOST_REQUIRE_NO_THROW (remotely_encoded = frame->encode_remotely (description, 1200));
63 BOOST_REQUIRE_EQUAL (locally_encoded.size(), remotely_encoded.size());
64 BOOST_CHECK_EQUAL (memcmp (locally_encoded.data(), remotely_encoded.data(), locally_encoded.size()), 0);
68 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
70 auto image = make_shared<Image>(AV_PIX_FMT_RGB24, dcp::Size (1998, 1080), Image::Alignment::PADDED);
71 uint8_t* p = image->data()[0];
73 for (int y = 0; y < 1080; ++y) {
75 for (int x = 0; x < 1998; ++x) {
80 p += image->stride()[0];
83 auto sub_image = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (100, 200), Image::Alignment::PADDED);
84 p = sub_image->data()[0];
85 for (int y = 0; y < 200; ++y) {
87 for (int x = 0; x < 100; ++x) {
93 p += sub_image->stride()[0];
96 LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_rgb.log"));
98 auto pvf = std::make_shared<PlayerVideo>(
99 make_shared<RawImageProxy>(image),
102 dcp::Size (1998, 1080),
103 dcp::Size (1998, 1080),
109 optional<ContentTime>(),
113 pvf->set_text (PositionImage(sub_image, Position<int>(50, 60)));
115 auto frame = make_shared<DCPVideo> (
123 auto locally_encoded = frame->encode_locally ();
125 auto server = new EncodeServer (true, 2);
127 auto server_thread = new thread (boost::bind(&EncodeServer::run, server));
129 /* Let the server get itself ready */
130 dcpomatic_sleep_seconds (1);
132 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
133 EncodeServerDescription description ("127.0.0.1", 1, SERVER_LINK_VERSION);
135 list<thread*> threads;
136 for (int i = 0; i < 8; ++i) {
137 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
140 for (auto i: threads) {
144 for (auto i: threads) {
149 server_thread->join ();
150 delete server_thread;
155 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
157 auto image = make_shared<Image>(AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), Image::Alignment::PADDED);
159 for (int i = 0; i < image->planes(); ++i) {
160 uint8_t* p = image->data()[i];
161 for (int j = 0; j < image->line_size()[i]; ++j) {
166 auto sub_image = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (100, 200), Image::Alignment::PADDED);
167 uint8_t* p = sub_image->data()[0];
168 for (int y = 0; y < 200; ++y) {
170 for (int x = 0; x < 100; ++x) {
173 *q++ = (x + y) % 256;
176 p += sub_image->stride()[0];
179 LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_yuv.log"));
181 auto pvf = std::make_shared<PlayerVideo>(
182 std::make_shared<RawImageProxy>(image),
185 dcp::Size(1998, 1080),
186 dcp::Size(1998, 1080),
192 optional<ContentTime>(),
196 pvf->set_text (PositionImage(sub_image, Position<int>(50, 60)));
198 auto frame = make_shared<DCPVideo>(
206 auto locally_encoded = frame->encode_locally ();
208 auto server = new EncodeServer (true, 2);
210 auto server_thread = new thread(boost::bind(&EncodeServer::run, server));
212 /* Let the server get itself ready */
213 dcpomatic_sleep_seconds (1);
215 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
216 EncodeServerDescription description ("127.0.0.1", 2, SERVER_LINK_VERSION);
218 list<thread*> threads;
219 for (int i = 0; i < 8; ++i) {
220 threads.push_back (new thread(boost::bind(do_remote_encode, frame, description, locally_encoded)));
223 for (auto i: threads) {
227 for (auto i: threads) {
232 server_thread->join ();
233 delete server_thread;
238 BOOST_AUTO_TEST_CASE (client_server_test_j2k)
240 auto image = make_shared<Image>(AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), Image::Alignment::PADDED);
242 for (int i = 0; i < image->planes(); ++i) {
243 uint8_t* p = image->data()[i];
244 for (int j = 0; j < image->line_size()[i]; ++j) {
249 LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_j2k.log"));
251 auto raw_pvf = std::make_shared<PlayerVideo> (
252 std::make_shared<RawImageProxy>(image),
255 dcp::Size(1998, 1080),
256 dcp::Size(1998, 1080),
262 optional<ContentTime>(),
266 auto raw_frame = make_shared<DCPVideo> (
274 auto raw_locally_encoded = raw_frame->encode_locally ();
276 auto j2k_pvf = std::make_shared<PlayerVideo> (
277 std::make_shared<J2KImageProxy>(raw_locally_encoded, dcp::Size(1998, 1080), AV_PIX_FMT_XYZ12LE),
280 dcp::Size(1998, 1080),
281 dcp::Size(1998, 1080),
284 PresetColourConversion::all().front().conversion,
287 optional<ContentTime>(),
291 auto j2k_frame = make_shared<DCPVideo> (
299 auto j2k_locally_encoded = j2k_frame->encode_locally ();
301 auto server = new EncodeServer (true, 2);
303 auto server_thread = new thread (boost::bind (&EncodeServer::run, server));
305 /* Let the server get itself ready */
306 dcpomatic_sleep_seconds (1);
308 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
309 EncodeServerDescription description ("127.0.0.1", 2, SERVER_LINK_VERSION);
311 list<thread*> threads;
312 for (int i = 0; i < 8; ++i) {
313 threads.push_back (new thread(boost::bind(do_remote_encode, j2k_frame, description, j2k_locally_encoded)));
316 for (auto i: threads) {
320 for (auto i: threads) {
325 server_thread->join ();
326 delete server_thread;