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