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