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