1f77f7f907cc718a0bcf61dd32b41881c9dd34c3
[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  *
24  *  Create a test image and then encode it using the standard mechanism
25  *  and also using a EncodeServer object running on localhost.  Compare the resulting
26  *  encoded data to check that they are the same.
27  */
28
29 #include "lib/encode_server.h"
30 #include "lib/image.h"
31 #include "lib/cross.h"
32 #include "lib/dcp_video.h"
33 #include "lib/player_video.h"
34 #include "lib/raw_image_proxy.h"
35 #include "lib/j2k_image_proxy.h"
36 #include "lib/encode_server_description.h"
37 #include "lib/file_log.h"
38 #include <boost/test/unit_test.hpp>
39 #include <boost/thread.hpp>
40
41 using std::list;
42 using boost::shared_ptr;
43 using boost::thread;
44 using boost::optional;
45 using dcp::Data;
46
47 void
48 do_remote_encode (shared_ptr<DCPVideo> frame, EncodeServerDescription description, Data locally_encoded)
49 {
50         Data remotely_encoded;
51         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description, 60));
52
53         BOOST_CHECK_EQUAL (locally_encoded.size(), remotely_encoded.size());
54         BOOST_CHECK_EQUAL (memcmp (locally_encoded.data().get(), remotely_encoded.data().get(), locally_encoded.size()), 0);
55 }
56
57 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
58 {
59         shared_ptr<Image> image (new Image (AV_PIX_FMT_RGB24, dcp::Size (1998, 1080), true));
60         uint8_t* p = image->data()[0];
61
62         for (int y = 0; y < 1080; ++y) {
63                 uint8_t* q = p;
64                 for (int x = 0; x < 1998; ++x) {
65                         *q++ = x % 256;
66                         *q++ = y % 256;
67                         *q++ = (x + y) % 256;
68                 }
69                 p += image->stride()[0];
70         }
71
72         shared_ptr<Image> sub_image (new Image (AV_PIX_FMT_RGBA, dcp::Size (100, 200), true));
73         p = sub_image->data()[0];
74         for (int y = 0; y < 200; ++y) {
75                 uint8_t* q = p;
76                 for (int x = 0; x < 100; ++x) {
77                         *q++ = y % 256;
78                         *q++ = x % 256;
79                         *q++ = (x + y) % 256;
80                         *q++ = 1;
81                 }
82                 p += sub_image->stride()[0];
83         }
84
85         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_rgb.log"));
86
87         shared_ptr<PlayerVideo> pvf (
88                 new PlayerVideo (
89                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
90                         DCPTime (),
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         new thread (boost::bind (&EncodeServer::run, server));
119
120         /* Let the server get itself ready */
121         dcpomatic_sleep (1);
122
123         EncodeServerDescription description ("localhost", 2);
124
125         list<thread*> threads;
126         for (int i = 0; i < 8; ++i) {
127                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
128         }
129
130         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
131                 (*i)->join ();
132         }
133
134         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
135                 delete *i;
136         }
137
138         delete server;
139 }
140
141 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
142 {
143         shared_ptr<Image> image (new Image (AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
144
145         for (int i = 0; i < image->planes(); ++i) {
146                 uint8_t* p = image->data()[i];
147                 for (int j = 0; j < image->line_size()[i]; ++j) {
148                         *p++ = j % 256;
149                 }
150         }
151
152         shared_ptr<Image> sub_image (new Image (AV_PIX_FMT_RGBA, dcp::Size (100, 200), true));
153         uint8_t* p = sub_image->data()[0];
154         for (int y = 0; y < 200; ++y) {
155                 uint8_t* q = p;
156                 for (int x = 0; x < 100; ++x) {
157                         *q++ = y % 256;
158                         *q++ = x % 256;
159                         *q++ = (x + y) % 256;
160                         *q++ = 1;
161                 }
162                 p += sub_image->stride()[0];
163         }
164
165         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_yuv.log"));
166
167         shared_ptr<PlayerVideo> pvf (
168                 new PlayerVideo (
169                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
170                         DCPTime (),
171                         Crop (),
172                         optional<double> (),
173                         dcp::Size (1998, 1080),
174                         dcp::Size (1998, 1080),
175                         EYES_BOTH,
176                         PART_WHOLE,
177                         ColourConversion ()
178                         )
179                 );
180
181         pvf->set_subtitle (PositionImage (sub_image, Position<int> (50, 60)));
182
183         shared_ptr<DCPVideo> frame (
184                 new DCPVideo (
185                         pvf,
186                         0,
187                         24,
188                         200000000,
189                         RESOLUTION_2K,
190                         log
191                         )
192                 );
193
194         Data locally_encoded = frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
195
196         EncodeServer* server = new EncodeServer (log, true, 2);
197
198         new thread (boost::bind (&EncodeServer::run, server));
199
200         /* Let the server get itself ready */
201         dcpomatic_sleep (1);
202
203         EncodeServerDescription description ("localhost", 2);
204
205         list<thread*> threads;
206         for (int i = 0; i < 8; ++i) {
207                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
208         }
209
210         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
211                 (*i)->join ();
212         }
213
214         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
215                 delete *i;
216         }
217
218         delete server;
219 }
220
221 BOOST_AUTO_TEST_CASE (client_server_test_j2k)
222 {
223         shared_ptr<Image> image (new Image (AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
224
225         for (int i = 0; i < image->planes(); ++i) {
226                 uint8_t* p = image->data()[i];
227                 for (int j = 0; j < image->line_size()[i]; ++j) {
228                         *p++ = j % 256;
229                 }
230         }
231
232         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_j2k.log"));
233
234         shared_ptr<PlayerVideo> raw_pvf (
235                 new PlayerVideo (
236                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
237                         DCPTime (),
238                         Crop (),
239                         optional<double> (),
240                         dcp::Size (1998, 1080),
241                         dcp::Size (1998, 1080),
242                         EYES_BOTH,
243                         PART_WHOLE,
244                         ColourConversion ()
245                         )
246                 );
247
248         shared_ptr<DCPVideo> raw_frame (
249                 new DCPVideo (
250                         raw_pvf,
251                         0,
252                         24,
253                         200000000,
254                         RESOLUTION_2K,
255                         log
256                         )
257                 );
258
259         Data raw_locally_encoded = raw_frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
260
261         shared_ptr<PlayerVideo> j2k_pvf (
262                 new PlayerVideo (
263                         shared_ptr<ImageProxy> (new J2KImageProxy (raw_locally_encoded, dcp::Size (1998, 1080))),
264                         DCPTime (),
265                         Crop (),
266                         optional<double> (),
267                         dcp::Size (1998, 1080),
268                         dcp::Size (1998, 1080),
269                         EYES_BOTH,
270                         PART_WHOLE,
271                         PresetColourConversion::all().front().conversion
272                         )
273                 );
274
275         shared_ptr<DCPVideo> j2k_frame (
276                 new DCPVideo (
277                         j2k_pvf,
278                         0,
279                         24,
280                         200000000,
281                         RESOLUTION_2K,
282                         log
283                         )
284                 );
285
286         Data j2k_locally_encoded = j2k_frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
287
288         EncodeServer* server = new EncodeServer (log, true, 2);
289
290         new thread (boost::bind (&EncodeServer::run, server));
291
292         /* Let the server get itself ready */
293         dcpomatic_sleep (1);
294
295         EncodeServerDescription description ("localhost", 2);
296
297         list<thread*> threads;
298         for (int i = 0; i < 8; ++i) {
299                 threads.push_back (new thread (boost::bind (do_remote_encode, j2k_frame, description, j2k_locally_encoded)));
300         }
301
302         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
303                 (*i)->join ();
304         }
305
306         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
307                 delete *i;
308         }
309
310         delete server;
311 }