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