Rename Server -> EncodeServer, ServerFinder -> EncodeServerFinder, ServerDescription...
[dcpomatic.git] / test / client_server_test.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  test/client_server_test.cc
21  *  @brief Test the server class.
22  *
23  *  Create a test image and then encode it using the standard mechanism
24  *  and also using a EncodeServer object running on localhost.  Compare the resulting
25  *  encoded data to check that they are the same.
26  */
27
28 #include "lib/encode_server.h"
29 #include "lib/image.h"
30 #include "lib/cross.h"
31 #include "lib/dcp_video.h"
32 #include "lib/player_video.h"
33 #include "lib/raw_image_proxy.h"
34 #include "lib/j2k_image_proxy.h"
35 #include "lib/encode_server_description.h"
36 #include "lib/file_log.h"
37 #include <boost/test/unit_test.hpp>
38 #include <boost/thread.hpp>
39
40 using std::list;
41 using boost::shared_ptr;
42 using boost::thread;
43 using boost::optional;
44 using dcp::Data;
45
46 void
47 do_remote_encode (shared_ptr<DCPVideo> frame, EncodeServerDescription description, Data locally_encoded)
48 {
49         Data remotely_encoded;
50         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description, 60));
51
52         BOOST_CHECK_EQUAL (locally_encoded.size(), remotely_encoded.size());
53         BOOST_CHECK_EQUAL (memcmp (locally_encoded.data().get(), remotely_encoded.data().get(), locally_encoded.size()), 0);
54 }
55
56 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
57 {
58         shared_ptr<Image> image (new Image (AV_PIX_FMT_RGB24, dcp::Size (1998, 1080), true));
59         uint8_t* p = image->data()[0];
60
61         for (int y = 0; y < 1080; ++y) {
62                 uint8_t* q = p;
63                 for (int x = 0; x < 1998; ++x) {
64                         *q++ = x % 256;
65                         *q++ = y % 256;
66                         *q++ = (x + y) % 256;
67                 }
68                 p += image->stride()[0];
69         }
70
71         shared_ptr<Image> sub_image (new Image (AV_PIX_FMT_RGBA, dcp::Size (100, 200), true));
72         p = sub_image->data()[0];
73         for (int y = 0; y < 200; ++y) {
74                 uint8_t* q = p;
75                 for (int x = 0; x < 100; ++x) {
76                         *q++ = y % 256;
77                         *q++ = x % 256;
78                         *q++ = (x + y) % 256;
79                         *q++ = 1;
80                 }
81                 p += sub_image->stride()[0];
82         }
83
84         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_rgb.log"));
85
86         shared_ptr<PlayerVideo> pvf (
87                 new PlayerVideo (
88                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
89                         DCPTime (),
90                         Crop (),
91                         optional<double> (),
92                         dcp::Size (1998, 1080),
93                         dcp::Size (1998, 1080),
94                         EYES_BOTH,
95                         PART_WHOLE,
96                         ColourConversion ()
97                         )
98                 );
99
100         pvf->set_subtitle (PositionImage (sub_image, Position<int> (50, 60)));
101
102         shared_ptr<DCPVideo> frame (
103                 new DCPVideo (
104                         pvf,
105                         0,
106                         24,
107                         200000000,
108                         RESOLUTION_2K,
109                         log
110                         )
111                 );
112
113         Data locally_encoded = frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
114
115         EncodeServer* server = new EncodeServer (log, true);
116
117         new thread (boost::bind (&EncodeServer::run, server, 2));
118
119         /* Let the server get itself ready */
120         dcpomatic_sleep (1);
121
122         EncodeServerDescription description ("localhost", 2);
123
124         list<thread*> threads;
125         for (int i = 0; i < 8; ++i) {
126                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
127         }
128
129         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
130                 (*i)->join ();
131         }
132
133         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
134                 delete *i;
135         }
136
137         delete server;
138 }
139
140 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
141 {
142         shared_ptr<Image> image (new Image (AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
143
144         for (int i = 0; i < image->planes(); ++i) {
145                 uint8_t* p = image->data()[i];
146                 for (int j = 0; j < image->line_size()[i]; ++j) {
147                         *p++ = j % 256;
148                 }
149         }
150
151         shared_ptr<Image> sub_image (new Image (AV_PIX_FMT_RGBA, dcp::Size (100, 200), true));
152         uint8_t* p = sub_image->data()[0];
153         for (int y = 0; y < 200; ++y) {
154                 uint8_t* q = p;
155                 for (int x = 0; x < 100; ++x) {
156                         *q++ = y % 256;
157                         *q++ = x % 256;
158                         *q++ = (x + y) % 256;
159                         *q++ = 1;
160                 }
161                 p += sub_image->stride()[0];
162         }
163
164         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_yuv.log"));
165
166         shared_ptr<PlayerVideo> pvf (
167                 new PlayerVideo (
168                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
169                         DCPTime (),
170                         Crop (),
171                         optional<double> (),
172                         dcp::Size (1998, 1080),
173                         dcp::Size (1998, 1080),
174                         EYES_BOTH,
175                         PART_WHOLE,
176                         ColourConversion ()
177                         )
178                 );
179
180         pvf->set_subtitle (PositionImage (sub_image, Position<int> (50, 60)));
181
182         shared_ptr<DCPVideo> frame (
183                 new DCPVideo (
184                         pvf,
185                         0,
186                         24,
187                         200000000,
188                         RESOLUTION_2K,
189                         log
190                         )
191                 );
192
193         Data locally_encoded = frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
194
195         EncodeServer* server = new EncodeServer (log, true);
196
197         new thread (boost::bind (&EncodeServer::run, server, 2));
198
199         /* Let the server get itself ready */
200         dcpomatic_sleep (1);
201
202         EncodeServerDescription description ("localhost", 2);
203
204         list<thread*> threads;
205         for (int i = 0; i < 8; ++i) {
206                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
207         }
208
209         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
210                 (*i)->join ();
211         }
212
213         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
214                 delete *i;
215         }
216
217         delete server;
218 }
219
220 BOOST_AUTO_TEST_CASE (client_server_test_j2k)
221 {
222         shared_ptr<Image> image (new Image (AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
223
224         for (int i = 0; i < image->planes(); ++i) {
225                 uint8_t* p = image->data()[i];
226                 for (int j = 0; j < image->line_size()[i]; ++j) {
227                         *p++ = j % 256;
228                 }
229         }
230
231         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_j2k.log"));
232
233         shared_ptr<PlayerVideo> raw_pvf (
234                 new PlayerVideo (
235                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
236                         DCPTime (),
237                         Crop (),
238                         optional<double> (),
239                         dcp::Size (1998, 1080),
240                         dcp::Size (1998, 1080),
241                         EYES_BOTH,
242                         PART_WHOLE,
243                         ColourConversion ()
244                         )
245                 );
246
247         shared_ptr<DCPVideo> raw_frame (
248                 new DCPVideo (
249                         raw_pvf,
250                         0,
251                         24,
252                         200000000,
253                         RESOLUTION_2K,
254                         log
255                         )
256                 );
257
258         Data raw_locally_encoded = raw_frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
259
260         shared_ptr<PlayerVideo> j2k_pvf (
261                 new PlayerVideo (
262                         shared_ptr<ImageProxy> (new J2KImageProxy (raw_locally_encoded, dcp::Size (1998, 1080))),
263                         DCPTime (),
264                         Crop (),
265                         optional<double> (),
266                         dcp::Size (1998, 1080),
267                         dcp::Size (1998, 1080),
268                         EYES_BOTH,
269                         PART_WHOLE,
270                         PresetColourConversion::all().front().conversion
271                         )
272                 );
273
274         shared_ptr<DCPVideo> j2k_frame (
275                 new DCPVideo (
276                         j2k_pvf,
277                         0,
278                         24,
279                         200000000,
280                         RESOLUTION_2K,
281                         log
282                         )
283                 );
284
285         Data j2k_locally_encoded = j2k_frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
286
287         EncodeServer* server = new EncodeServer (log, true);
288
289         new thread (boost::bind (&EncodeServer::run, server, 2));
290
291         /* Let the server get itself ready */
292         dcpomatic_sleep (1);
293
294         EncodeServerDescription description ("localhost", 2);
295
296         list<thread*> threads;
297         for (int i = 0; i < 8; ++i) {
298                 threads.push_back (new thread (boost::bind (do_remote_encode, j2k_frame, description, j2k_locally_encoded)));
299         }
300
301         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
302                 (*i)->join ();
303         }
304
305         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
306                 delete *i;
307         }
308
309         delete server;
310 }