2355c08c09ea98b995ebf278dd7a5f9966ac9960
[dcpomatic.git] / test / client_server_test.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 /** @file  test/client_server_test.cc
23  *  @brief Test the server class.
24  *  @ingroup feature
25  *
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.
29  */
30
31
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"
44 #include "test.h"
45 #include <boost/test/unit_test.hpp>
46 #include <boost/thread.hpp>
47
48
49 using std::list;
50 using std::make_shared;
51 using std::shared_ptr;
52 using std::weak_ptr;
53 using boost::thread;
54 using boost::optional;
55 using dcp::ArrayData;
56 using namespace dcpomatic;
57
58
59 void
60 do_remote_encode (DCPVideo frame, EncodeServerDescription description, ArrayData locally_encoded)
61 {
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));
66
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);
69 }
70
71
72 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
73 {
74         auto image = make_shared<Image>(AV_PIX_FMT_RGB24, dcp::Size (1998, 1080), Image::Alignment::PADDED);
75         uint8_t* p = image->data()[0];
76
77         for (int y = 0; y < 1080; ++y) {
78                 uint8_t* q = p;
79                 for (int x = 0; x < 1998; ++x) {
80                         *q++ = x % 256;
81                         *q++ = y % 256;
82                         *q++ = (x + y) % 256;
83                 }
84                 p += image->stride()[0];
85         }
86
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) {
90                 uint8_t* q = p;
91                 for (int x = 0; x < 100; ++x) {
92                         *q++ = y % 256;
93                         *q++ = x % 256;
94                         *q++ = (x + y) % 256;
95                         *q++ = 1;
96                 }
97                 p += sub_image->stride()[0];
98         }
99
100         LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_rgb.log"));
101
102         auto pvf = std::make_shared<PlayerVideo>(
103                 make_shared<RawImageProxy>(image),
104                 Crop (),
105                 optional<double> (),
106                 dcp::Size (1998, 1080),
107                 dcp::Size (1998, 1080),
108                 Eyes::BOTH,
109                 Part::WHOLE,
110                 ColourConversion(),
111                 VideoRange::FULL,
112                 weak_ptr<Content>(),
113                 optional<Frame>(),
114                 false
115                 );
116
117         pvf->set_text (PositionImage(sub_image, Position<int>(50, 60)));
118
119         DCPVideo frame(
120                 pvf,
121                 0,
122                 24,
123                 200000000,
124                 Resolution::TWO_K
125                 );
126
127         J2KEncoderCPUBackend cpu;
128         auto locally_encoded = cpu.encode (frame);
129         BOOST_REQUIRE (static_cast<bool>(locally_encoded));
130
131         auto server = new EncodeServer (true, 2);
132
133         auto server_thread = new thread (boost::bind(&EncodeServer::run, server));
134
135         /* Let the server get itself ready */
136         dcpomatic_sleep_seconds (1);
137
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);
140
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)));
144         }
145
146         for (auto i: threads) {
147                 i->join ();
148         }
149
150         for (auto i: threads) {
151                 delete i;
152         }
153
154         server->stop ();
155         server_thread->join ();
156         delete server_thread;
157         delete server;
158 }
159
160
161 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
162 {
163         auto image = make_shared<Image>(AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), Image::Alignment::PADDED);
164
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) {
168                         *p++ = j % 256;
169                 }
170         }
171
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) {
175                 uint8_t* q = p;
176                 for (int x = 0; x < 100; ++x) {
177                         *q++ = y % 256;
178                         *q++ = x % 256;
179                         *q++ = (x + y) % 256;
180                         *q++ = 1;
181                 }
182                 p += sub_image->stride()[0];
183         }
184
185         LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_yuv.log"));
186
187         auto pvf = std::make_shared<PlayerVideo>(
188                 std::make_shared<RawImageProxy>(image),
189                 Crop(),
190                 optional<double>(),
191                 dcp::Size(1998, 1080),
192                 dcp::Size(1998, 1080),
193                 Eyes::BOTH,
194                 Part::WHOLE,
195                 ColourConversion(),
196                 VideoRange::FULL,
197                 weak_ptr<Content>(),
198                 optional<Frame>(),
199                 false
200                 );
201
202         pvf->set_text (PositionImage(sub_image, Position<int>(50, 60)));
203
204         DCPVideo frame(
205                 pvf,
206                 0,
207                 24,
208                 200000000,
209                 Resolution::TWO_K
210                 );
211
212         J2KEncoderCPUBackend cpu;
213         auto locally_encoded = cpu.encode (frame);
214         BOOST_REQUIRE (static_cast<bool>(locally_encoded));
215
216         auto server = new EncodeServer (true, 2);
217
218         auto server_thread = new thread(boost::bind(&EncodeServer::run, server));
219
220         /* Let the server get itself ready */
221         dcpomatic_sleep_seconds (1);
222
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);
225
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)));
229         }
230
231         for (auto i: threads) {
232                 i->join ();
233         }
234
235         for (auto i: threads) {
236                 delete i;
237         }
238
239         server->stop ();
240         server_thread->join ();
241         delete server_thread;
242         delete server;
243 }
244
245
246 BOOST_AUTO_TEST_CASE (client_server_test_j2k)
247 {
248         auto image = make_shared<Image>(AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), Image::Alignment::PADDED);
249
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) {
253                         *p++ = j % 256;
254                 }
255         }
256
257         LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_j2k.log"));
258
259         auto raw_pvf = std::make_shared<PlayerVideo> (
260                 std::make_shared<RawImageProxy>(image),
261                 Crop(),
262                 optional<double>(),
263                 dcp::Size(1998, 1080),
264                 dcp::Size(1998, 1080),
265                 Eyes::BOTH,
266                 Part::WHOLE,
267                 ColourConversion(),
268                 VideoRange::FULL,
269                 weak_ptr<Content>(),
270                 optional<Frame>(),
271                 false
272                 );
273
274         DCPVideo raw_frame(
275                 raw_pvf,
276                 0,
277                 24,
278                 200000000,
279                 Resolution::TWO_K
280                 );
281
282         J2KEncoderCPUBackend cpu;
283         auto raw_locally_encoded = cpu.encode (raw_frame);
284         BOOST_REQUIRE (static_cast<bool>(raw_locally_encoded));
285
286         auto j2k_pvf = std::make_shared<PlayerVideo> (
287                 std::make_shared<J2KImageProxy>(*raw_locally_encoded, dcp::Size(1998, 1080), AV_PIX_FMT_XYZ12LE),
288                 Crop(),
289                 optional<double>(),
290                 dcp::Size(1998, 1080),
291                 dcp::Size(1998, 1080),
292                 Eyes::BOTH,
293                 Part::WHOLE,
294                 PresetColourConversion::all().front().conversion,
295                 VideoRange::FULL,
296                 weak_ptr<Content>(),
297                 optional<Frame>(),
298                 false
299                 );
300
301         DCPVideo j2k_frame(
302                 j2k_pvf,
303                 0,
304                 24,
305                 200000000,
306                 Resolution::TWO_K
307                 );
308
309         auto j2k_locally_encoded = cpu.encode(j2k_frame);
310         BOOST_REQUIRE (static_cast<bool>(j2k_locally_encoded));
311
312         auto server = new EncodeServer (true, 2);
313
314         auto server_thread = new thread (boost::bind (&EncodeServer::run, server));
315
316         /* Let the server get itself ready */
317         dcpomatic_sleep_seconds (1);
318
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);
321
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)));
325         }
326
327         for (auto i: threads) {
328                 i->join ();
329         }
330
331         for (auto i: threads) {
332                 delete i;
333         }
334
335         server->stop ();
336         server_thread->join ();
337         delete server_thread;
338         delete server;
339 }