Attempt to tidy up internal APIs slightly.
[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                         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, 2);
116
117         new thread (boost::bind (&EncodeServer::run, server));
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                         Crop (),
170                         optional<double> (),
171                         dcp::Size (1998, 1080),
172                         dcp::Size (1998, 1080),
173                         EYES_BOTH,
174                         PART_WHOLE,
175                         ColourConversion ()
176                         )
177                 );
178
179         pvf->set_subtitle (PositionImage (sub_image, Position<int> (50, 60)));
180
181         shared_ptr<DCPVideo> frame (
182                 new DCPVideo (
183                         pvf,
184                         0,
185                         24,
186                         200000000,
187                         RESOLUTION_2K,
188                         log
189                         )
190                 );
191
192         Data locally_encoded = frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
193
194         EncodeServer* server = new EncodeServer (log, true, 2);
195
196         new thread (boost::bind (&EncodeServer::run, server));
197
198         /* Let the server get itself ready */
199         dcpomatic_sleep (1);
200
201         EncodeServerDescription description ("localhost", 2);
202
203         list<thread*> threads;
204         for (int i = 0; i < 8; ++i) {
205                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
206         }
207
208         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
209                 (*i)->join ();
210         }
211
212         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
213                 delete *i;
214         }
215
216         delete server;
217 }
218
219 BOOST_AUTO_TEST_CASE (client_server_test_j2k)
220 {
221         shared_ptr<Image> image (new Image (AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
222
223         for (int i = 0; i < image->planes(); ++i) {
224                 uint8_t* p = image->data()[i];
225                 for (int j = 0; j < image->line_size()[i]; ++j) {
226                         *p++ = j % 256;
227                 }
228         }
229
230         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_j2k.log"));
231
232         shared_ptr<PlayerVideo> raw_pvf (
233                 new PlayerVideo (
234                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
235                         Crop (),
236                         optional<double> (),
237                         dcp::Size (1998, 1080),
238                         dcp::Size (1998, 1080),
239                         EYES_BOTH,
240                         PART_WHOLE,
241                         ColourConversion ()
242                         )
243                 );
244
245         shared_ptr<DCPVideo> raw_frame (
246                 new DCPVideo (
247                         raw_pvf,
248                         0,
249                         24,
250                         200000000,
251                         RESOLUTION_2K,
252                         log
253                         )
254                 );
255
256         Data raw_locally_encoded = raw_frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
257
258         shared_ptr<PlayerVideo> j2k_pvf (
259                 new PlayerVideo (
260                         shared_ptr<ImageProxy> (new J2KImageProxy (raw_locally_encoded, dcp::Size (1998, 1080), AV_PIX_FMT_XYZ12LE)),
261                         Crop (),
262                         optional<double> (),
263                         dcp::Size (1998, 1080),
264                         dcp::Size (1998, 1080),
265                         EYES_BOTH,
266                         PART_WHOLE,
267                         PresetColourConversion::all().front().conversion
268                         )
269                 );
270
271         shared_ptr<DCPVideo> j2k_frame (
272                 new DCPVideo (
273                         j2k_pvf,
274                         0,
275                         24,
276                         200000000,
277                         RESOLUTION_2K,
278                         log
279                         )
280                 );
281
282         Data j2k_locally_encoded = j2k_frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
283
284         EncodeServer* server = new EncodeServer (log, true, 2);
285
286         new thread (boost::bind (&EncodeServer::run, server));
287
288         /* Let the server get itself ready */
289         dcpomatic_sleep (1);
290
291         EncodeServerDescription description ("localhost", 2);
292
293         list<thread*> threads;
294         for (int i = 0; i < 8; ++i) {
295                 threads.push_back (new thread (boost::bind (do_remote_encode, j2k_frame, description, j2k_locally_encoded)));
296         }
297
298         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
299                 (*i)->join ();
300         }
301
302         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
303                 delete *i;
304         }
305
306         delete server;
307 }