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