d2be1be7c10ea022359167ca1e149155721aee7a
[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_image_proxy.h"
40 #include "lib/player_video.h"
41 #include "lib/raw_image_proxy.h"
42 #include "test.h"
43 #include <boost/test/unit_test.hpp>
44 #include <boost/thread.hpp>
45
46
47 using std::list;
48 using std::make_shared;
49 using std::shared_ptr;
50 using std::weak_ptr;
51 using boost::thread;
52 using boost::optional;
53 using dcp::ArrayData;
54 using namespace dcpomatic;
55
56
57 void
58 do_remote_encode (shared_ptr<DCPVideo> frame, EncodeServerDescription description, ArrayData locally_encoded)
59 {
60         ArrayData remotely_encoded;
61         BOOST_REQUIRE_NO_THROW (remotely_encoded = frame->encode_remotely (description, 1200));
62
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);
65 }
66
67
68 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
69 {
70         auto image = make_shared<Image>(AV_PIX_FMT_RGB24, dcp::Size (1998, 1080), Image::Alignment::PADDED);
71         uint8_t* p = image->data()[0];
72
73         for (int y = 0; y < 1080; ++y) {
74                 uint8_t* q = p;
75                 for (int x = 0; x < 1998; ++x) {
76                         *q++ = x % 256;
77                         *q++ = y % 256;
78                         *q++ = (x + y) % 256;
79                 }
80                 p += image->stride()[0];
81         }
82
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) {
86                 uint8_t* q = p;
87                 for (int x = 0; x < 100; ++x) {
88                         *q++ = y % 256;
89                         *q++ = x % 256;
90                         *q++ = (x + y) % 256;
91                         *q++ = 1;
92                 }
93                 p += sub_image->stride()[0];
94         }
95
96         LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_rgb.log"));
97
98         auto pvf = std::make_shared<PlayerVideo>(
99                 make_shared<RawImageProxy>(image),
100                 Crop (),
101                 optional<double> (),
102                 dcp::Size (1998, 1080),
103                 dcp::Size (1998, 1080),
104                 VideoType::MAIN,
105                 Eyes::BOTH,
106                 Part::WHOLE,
107                 ColourConversion(),
108                 VideoRange::FULL,
109                 weak_ptr<Content>(),
110                 optional<ContentTime>(),
111                 false
112                 );
113
114         pvf->set_text (PositionImage(sub_image, Position<int>(50, 60)));
115
116         auto frame = make_shared<DCPVideo> (
117                 pvf,
118                 0,
119                 24,
120                 200000000,
121                 Resolution::TWO_K
122                 );
123
124         auto locally_encoded = frame->encode_locally ();
125
126         auto server = new EncodeServer (true, 2);
127
128         auto server_thread = new thread (boost::bind(&EncodeServer::run, server));
129
130         /* Let the server get itself ready */
131         dcpomatic_sleep_seconds (1);
132
133         /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
134         EncodeServerDescription description ("127.0.0.1", 1, SERVER_LINK_VERSION);
135
136         list<thread*> threads;
137         for (int i = 0; i < 8; ++i) {
138                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
139         }
140
141         for (auto i: threads) {
142                 i->join ();
143         }
144
145         for (auto i: threads) {
146                 delete i;
147         }
148
149         server->stop ();
150         server_thread->join ();
151         delete server_thread;
152         delete server;
153 }
154
155
156 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
157 {
158         auto image = make_shared<Image>(AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), Image::Alignment::PADDED);
159
160         for (int i = 0; i < image->planes(); ++i) {
161                 uint8_t* p = image->data()[i];
162                 for (int j = 0; j < image->line_size()[i]; ++j) {
163                         *p++ = j % 256;
164                 }
165         }
166
167         auto sub_image = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (100, 200), Image::Alignment::PADDED);
168         uint8_t* p = sub_image->data()[0];
169         for (int y = 0; y < 200; ++y) {
170                 uint8_t* q = p;
171                 for (int x = 0; x < 100; ++x) {
172                         *q++ = y % 256;
173                         *q++ = x % 256;
174                         *q++ = (x + y) % 256;
175                         *q++ = 1;
176                 }
177                 p += sub_image->stride()[0];
178         }
179
180         LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_yuv.log"));
181
182         auto pvf = std::make_shared<PlayerVideo>(
183                 std::make_shared<RawImageProxy>(image),
184                 Crop(),
185                 optional<double>(),
186                 dcp::Size(1998, 1080),
187                 dcp::Size(1998, 1080),
188                 VideoType::MAIN,
189                 Eyes::BOTH,
190                 Part::WHOLE,
191                 ColourConversion(),
192                 VideoRange::FULL,
193                 weak_ptr<Content>(),
194                 optional<ContentTime>(),
195                 false
196                 );
197
198         pvf->set_text (PositionImage(sub_image, Position<int>(50, 60)));
199
200         auto frame = make_shared<DCPVideo>(
201                 pvf,
202                 0,
203                 24,
204                 200000000,
205                 Resolution::TWO_K
206                 );
207
208         auto locally_encoded = frame->encode_locally ();
209
210         auto server = new EncodeServer (true, 2);
211
212         auto server_thread = new thread(boost::bind(&EncodeServer::run, server));
213
214         /* Let the server get itself ready */
215         dcpomatic_sleep_seconds (1);
216
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);
219
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)));
223         }
224
225         for (auto i: threads) {
226                 i->join ();
227         }
228
229         for (auto i: threads) {
230                 delete i;
231         }
232
233         server->stop ();
234         server_thread->join ();
235         delete server_thread;
236         delete server;
237 }
238
239
240 BOOST_AUTO_TEST_CASE (client_server_test_j2k)
241 {
242         auto image = make_shared<Image>(AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), Image::Alignment::PADDED);
243
244         for (int i = 0; i < image->planes(); ++i) {
245                 uint8_t* p = image->data()[i];
246                 for (int j = 0; j < image->line_size()[i]; ++j) {
247                         *p++ = j % 256;
248                 }
249         }
250
251         LogSwitcher ls (make_shared<FileLog>("build/test/client_server_test_j2k.log"));
252
253         auto raw_pvf = std::make_shared<PlayerVideo> (
254                 std::make_shared<RawImageProxy>(image),
255                 Crop(),
256                 optional<double>(),
257                 dcp::Size(1998, 1080),
258                 dcp::Size(1998, 1080),
259                 VideoType::MAIN,
260                 Eyes::BOTH,
261                 Part::WHOLE,
262                 ColourConversion(),
263                 VideoRange::FULL,
264                 weak_ptr<Content>(),
265                 optional<ContentTime>(),
266                 false
267                 );
268
269         auto raw_frame = make_shared<DCPVideo> (
270                 raw_pvf,
271                 0,
272                 24,
273                 200000000,
274                 Resolution::TWO_K
275                 );
276
277         auto raw_locally_encoded = raw_frame->encode_locally ();
278
279         auto j2k_pvf = std::make_shared<PlayerVideo> (
280                 std::make_shared<J2KImageProxy>(raw_locally_encoded, dcp::Size(1998, 1080), AV_PIX_FMT_XYZ12LE),
281                 Crop(),
282                 optional<double>(),
283                 dcp::Size(1998, 1080),
284                 dcp::Size(1998, 1080),
285                 VideoType::MAIN,
286                 Eyes::BOTH,
287                 Part::WHOLE,
288                 PresetColourConversion::all().front().conversion,
289                 VideoRange::FULL,
290                 weak_ptr<Content>(),
291                 optional<ContentTime>(),
292                 false
293                 );
294
295         auto j2k_frame = make_shared<DCPVideo> (
296                 j2k_pvf,
297                 0,
298                 24,
299                 200000000,
300                 Resolution::TWO_K
301                 );
302
303         auto j2k_locally_encoded = j2k_frame->encode_locally ();
304
305         auto server = new EncodeServer (true, 2);
306
307         auto server_thread = new thread (boost::bind (&EncodeServer::run, server));
308
309         /* Let the server get itself ready */
310         dcpomatic_sleep_seconds (1);
311
312         /* "localhost" rather than "127.0.0.1" here fails on docker; go figure */
313         EncodeServerDescription description ("127.0.0.1", 2, SERVER_LINK_VERSION);
314
315         list<thread*> threads;
316         for (int i = 0; i < 8; ++i) {
317                 threads.push_back (new thread(boost::bind(do_remote_encode, j2k_frame, description, j2k_locally_encoded)));
318         }
319
320         for (auto i: threads) {
321                 i->join ();
322         }
323
324         for (auto i: threads) {
325                 delete i;
326         }
327
328         server->stop ();
329         server_thread->join ();
330         delete server_thread;
331         delete server;
332 }