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