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"
41 #include <boost/test/unit_test.hpp>
42 #include <boost/thread.hpp>
45 using boost::shared_ptr;
47 using boost::optional;
48 using boost::weak_ptr;
50 using namespace dcpomatic;
54 do_remote_encode (shared_ptr<DCPVideo> frame, EncodeServerDescription description, Data locally_encoded)
56 Data remotely_encoded;
57 BOOST_REQUIRE_NO_THROW (remotely_encoded = frame->encode_remotely (description, 1200));
59 BOOST_REQUIRE_EQUAL (locally_encoded.size(), remotely_encoded.size());
60 BOOST_CHECK_EQUAL (memcmp (locally_encoded.data().get(), remotely_encoded.data().get(), locally_encoded.size()), 0);
63 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
65 shared_ptr<Image> image (new Image (AV_PIX_FMT_RGB24, dcp::Size (1998, 1080), true));
66 uint8_t* p = image->data()[0];
68 for (int y = 0; y < 1080; ++y) {
70 for (int x = 0; x < 1998; ++x) {
75 p += image->stride()[0];
78 shared_ptr<Image> sub_image (new Image (AV_PIX_FMT_BGRA, dcp::Size (100, 200), true));
79 p = sub_image->data()[0];
80 for (int y = 0; y < 200; ++y) {
82 for (int x = 0; x < 100; ++x) {
88 p += sub_image->stride()[0];
91 LogSwitcher ls (shared_ptr<Log>(new FileLog("build/test/client_server_test_rgb.log")));
93 shared_ptr<PlayerVideo> pvf (
95 shared_ptr<ImageProxy> (new RawImageProxy (image)),
98 dcp::Size (1998, 1080),
99 dcp::Size (1998, 1080),
110 pvf->set_text (PositionImage (sub_image, Position<int> (50, 60)));
112 shared_ptr<DCPVideo> frame (
122 Data locally_encoded = frame->encode_locally ();
124 EncodeServer* server = new EncodeServer (true, 2);
126 thread* server_thread = new thread (boost::bind (&EncodeServer::run, server));
128 /* Let the server get itself ready */
129 dcpomatic_sleep_seconds (1);
131 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
132 EncodeServerDescription description ("127.0.0.1", 1, SERVER_LINK_VERSION);
134 list<thread*> threads;
135 for (int i = 0; i < 8; ++i) {
136 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
139 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
143 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
148 server_thread->join ();
149 delete server_thread;
153 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
155 shared_ptr<Image> image (new Image (AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
157 for (int i = 0; i < image->planes(); ++i) {
158 uint8_t* p = image->data()[i];
159 for (int j = 0; j < image->line_size()[i]; ++j) {
164 shared_ptr<Image> sub_image (new Image (AV_PIX_FMT_BGRA, dcp::Size (100, 200), true));
165 uint8_t* p = sub_image->data()[0];
166 for (int y = 0; y < 200; ++y) {
168 for (int x = 0; x < 100; ++x) {
171 *q++ = (x + y) % 256;
174 p += sub_image->stride()[0];
177 LogSwitcher ls (shared_ptr<Log>(new FileLog("build/test/client_server_test_yuv.log")));
179 shared_ptr<PlayerVideo> pvf (
181 shared_ptr<ImageProxy> (new RawImageProxy (image)),
184 dcp::Size (1998, 1080),
185 dcp::Size (1998, 1080),
196 pvf->set_text (PositionImage (sub_image, Position<int> (50, 60)));
198 shared_ptr<DCPVideo> frame (
208 Data locally_encoded = frame->encode_locally ();
210 EncodeServer* server = new EncodeServer (true, 2);
212 thread* server_thread = new thread (boost::bind (&EncodeServer::run, server));
214 /* Let the server get itself ready */
215 dcpomatic_sleep_seconds (1);
217 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
218 EncodeServerDescription description ("127.0.0.1", 2, SERVER_LINK_VERSION);
220 list<thread*> threads;
221 for (int i = 0; i < 8; ++i) {
222 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
225 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
229 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
234 server_thread->join ();
235 delete server_thread;
239 BOOST_AUTO_TEST_CASE (client_server_test_j2k)
241 shared_ptr<Image> image (new Image (AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
243 for (int i = 0; i < image->planes(); ++i) {
244 uint8_t* p = image->data()[i];
245 for (int j = 0; j < image->line_size()[i]; ++j) {
250 LogSwitcher ls (shared_ptr<Log>(new FileLog("build/test/client_server_test_j2k.log")));
252 shared_ptr<PlayerVideo> raw_pvf (
254 shared_ptr<ImageProxy> (new RawImageProxy (image)),
257 dcp::Size (1998, 1080),
258 dcp::Size (1998, 1080),
269 shared_ptr<DCPVideo> raw_frame (
279 Data raw_locally_encoded = raw_frame->encode_locally ();
281 shared_ptr<PlayerVideo> j2k_pvf (
283 shared_ptr<ImageProxy> (new J2KImageProxy (raw_locally_encoded, dcp::Size (1998, 1080), AV_PIX_FMT_XYZ12LE)),
286 dcp::Size (1998, 1080),
287 dcp::Size (1998, 1080),
290 PresetColourConversion::all().front().conversion,
298 shared_ptr<DCPVideo> j2k_frame (
308 Data j2k_locally_encoded = j2k_frame->encode_locally ();
310 EncodeServer* server = new EncodeServer (true, 2);
312 thread* server_thread = new thread (boost::bind (&EncodeServer::run, server));
314 /* Let the server get itself ready */
315 dcpomatic_sleep_seconds (1);
317 /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
318 EncodeServerDescription description ("127.0.0.1", 2, SERVER_LINK_VERSION);
320 list<thread*> threads;
321 for (int i = 0; i < 8; ++i) {
322 threads.push_back (new thread (boost::bind (do_remote_encode, j2k_frame, description, j2k_locally_encoded)));
325 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
329 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
334 server_thread->join ();
335 delete server_thread;