2 Copyright (C) 2012-2018 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/>.
21 /** @file test/client_server_test.cc
22 * @brief Test the server class.
25 * Create a test image and then encode it using the standard mechanism
26 * and also using a EncodeServer object running on localhost. Compare the resulting
27 * encoded data to check that they are the same.
30 #include "lib/encode_server.h"
31 #include "lib/image.h"
32 #include "lib/cross.h"
33 #include "lib/dcp_video.h"
34 #include "lib/player_video.h"
35 #include "lib/raw_image_proxy.h"
36 #include "lib/j2k_image_proxy.h"
37 #include "lib/encode_server_description.h"
38 #include "lib/file_log.h"
39 #include "lib/dcpomatic_log.h"
40 #include "lib/j2k_encoder_cpu_backend.h"
41 #include "lib/j2k_encoder_remote_backend.h"
43 #include <boost/test/unit_test.hpp>
44 #include <boost/thread.hpp>
48 using boost::shared_ptr;
50 using boost::optional;
51 using boost::weak_ptr;
53 using namespace dcpomatic;
57 do_remote_encode (shared_ptr<DCPVideo> frame, shared_ptr<J2KEncoderBackend> backend, Data locally_encoded)
59 Data remotely_encoded;
60 vector<shared_ptr<DCPVideo> > frames;
61 frames.push_back (frame);
62 BOOST_REQUIRE_NO_THROW (remotely_encoded = backend->encode(frames).front());
64 BOOST_REQUIRE_EQUAL (locally_encoded.size(), remotely_encoded.size());
65 BOOST_CHECK_EQUAL (memcmp (locally_encoded.data().get(), remotely_encoded.data().get(), locally_encoded.size()), 0);
69 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
71 shared_ptr<Image> image (new Image (AV_PIX_FMT_RGB24, dcp::Size (1998, 1080), true));
72 uint8_t* p = image->data()[0];
74 for (int y = 0; y < 1080; ++y) {
76 for (int x = 0; x < 1998; ++x) {
81 p += image->stride()[0];
84 shared_ptr<Image> sub_image (new Image (AV_PIX_FMT_BGRA, dcp::Size (100, 200), true));
85 p = sub_image->data()[0];
86 for (int y = 0; y < 200; ++y) {
88 for (int x = 0; x < 100; ++x) {
94 p += sub_image->stride()[0];
97 LogSwitcher ls (shared_ptr<Log>(new FileLog("build/test/client_server_test_rgb.log")));
99 shared_ptr<PlayerVideo> pvf (
101 shared_ptr<ImageProxy> (new RawImageProxy (image)),
104 dcp::Size (1998, 1080),
105 dcp::Size (1998, 1080),
116 pvf->set_text (PositionImage (sub_image, Position<int> (50, 60)));
118 shared_ptr<DCPVideo> frame (
128 vector<shared_ptr<DCPVideo> > frames;
129 frames.push_back (frame);
131 J2KEncoderCPUBackend cpu;
132 Data locally_encoded = cpu.encode(frames).front();
134 EncodeServer* server = new EncodeServer (true, 2);
136 thread* server_thread = new thread (boost::bind (&EncodeServer::run, server));
138 /* Let the server get itself ready */
139 dcpomatic_sleep_seconds (1);
141 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
142 EncodeServerDescription description ("127.0.0.1", 1, SERVER_LINK_VERSION);
144 list<thread*> threads;
145 for (int i = 0; i < 8; ++i) {
146 shared_ptr<J2KEncoderRemoteBackend> backend (new J2KEncoderRemoteBackend(description));
147 threads.push_back (new thread (boost::bind (do_remote_encode, frame, backend, locally_encoded)));
150 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
154 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
159 server_thread->join ();
160 delete server_thread;
164 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
166 shared_ptr<Image> image (new Image (AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
168 for (int i = 0; i < image->planes(); ++i) {
169 uint8_t* p = image->data()[i];
170 for (int j = 0; j < image->line_size()[i]; ++j) {
175 shared_ptr<Image> sub_image (new Image (AV_PIX_FMT_BGRA, dcp::Size (100, 200), true));
176 uint8_t* p = sub_image->data()[0];
177 for (int y = 0; y < 200; ++y) {
179 for (int x = 0; x < 100; ++x) {
182 *q++ = (x + y) % 256;
185 p += sub_image->stride()[0];
188 LogSwitcher ls (shared_ptr<Log>(new FileLog("build/test/client_server_test_yuv.log")));
190 shared_ptr<PlayerVideo> pvf (
192 shared_ptr<ImageProxy> (new RawImageProxy (image)),
195 dcp::Size (1998, 1080),
196 dcp::Size (1998, 1080),
207 pvf->set_text (PositionImage (sub_image, Position<int> (50, 60)));
209 shared_ptr<DCPVideo> frame (
219 vector<shared_ptr<DCPVideo> > frames;
220 frames.push_back (frame);
222 J2KEncoderCPUBackend cpu;
223 Data locally_encoded = cpu.encode(frames).front();
225 EncodeServer* server = new EncodeServer (true, 2);
227 thread* server_thread = new thread (boost::bind (&EncodeServer::run, server));
229 /* Let the server get itself ready */
230 dcpomatic_sleep_seconds (1);
232 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
233 EncodeServerDescription description ("127.0.0.1", 2, SERVER_LINK_VERSION);
235 list<thread*> threads;
236 for (int i = 0; i < 8; ++i) {
237 shared_ptr<J2KEncoderRemoteBackend> backend (new J2KEncoderRemoteBackend(description));
238 threads.push_back (new thread (boost::bind (do_remote_encode, frame, backend, locally_encoded)));
241 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
245 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
250 server_thread->join ();
251 delete server_thread;
255 BOOST_AUTO_TEST_CASE (client_server_test_j2k)
257 shared_ptr<Image> image (new Image (AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
259 for (int i = 0; i < image->planes(); ++i) {
260 uint8_t* p = image->data()[i];
261 for (int j = 0; j < image->line_size()[i]; ++j) {
266 LogSwitcher ls (shared_ptr<Log>(new FileLog("build/test/client_server_test_j2k.log")));
268 shared_ptr<PlayerVideo> raw_pvf (
270 shared_ptr<ImageProxy> (new RawImageProxy (image)),
273 dcp::Size (1998, 1080),
274 dcp::Size (1998, 1080),
285 shared_ptr<DCPVideo> raw_frame (
295 vector<shared_ptr<DCPVideo> > raw_frames;
296 raw_frames.push_back (raw_frame);
298 J2KEncoderCPUBackend cpu;
299 Data raw_locally_encoded = cpu.encode(raw_frames).front();
301 shared_ptr<PlayerVideo> j2k_pvf (
303 shared_ptr<ImageProxy> (new J2KImageProxy (raw_locally_encoded, dcp::Size (1998, 1080), AV_PIX_FMT_XYZ12LE)),
306 dcp::Size (1998, 1080),
307 dcp::Size (1998, 1080),
310 PresetColourConversion::all().front().conversion,
318 shared_ptr<DCPVideo> j2k_frame (
328 vector<shared_ptr<DCPVideo> > j2k_frames;
329 j2k_frames.push_back (j2k_frame);
331 Data j2k_locally_encoded = cpu.encode(j2k_frames).front();
333 EncodeServer* server = new EncodeServer (true, 2);
335 thread* server_thread = new thread (boost::bind (&EncodeServer::run, server));
337 /* Let the server get itself ready */
338 dcpomatic_sleep_seconds (1);
340 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
341 EncodeServerDescription description ("127.0.0.1", 2, SERVER_LINK_VERSION);
343 list<thread*> threads;
344 for (int i = 0; i < 8; ++i) {
345 shared_ptr<J2KEncoderRemoteBackend> backend (new J2KEncoderRemoteBackend(description));
346 threads.push_back (new thread (boost::bind (do_remote_encode, j2k_frame, backend, j2k_locally_encoded)));
349 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
353 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
358 server_thread->join ();
359 delete server_thread;