No-op: remove all trailing whitespace.
[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 Server object running on localhost.  Compare the resulting
25  *  encoded data to check that they are the same.
26  */
27
28 #include <boost/test/unit_test.hpp>
29 #include <boost/thread.hpp>
30 #include "lib/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/data.h"
37
38 using std::list;
39 using boost::shared_ptr;
40 using boost::thread;
41 using boost::optional;
42
43 void
44 do_remote_encode (shared_ptr<DCPVideo> frame, ServerDescription description, Data locally_encoded)
45 {
46         Data remotely_encoded;
47         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
48
49         BOOST_CHECK_EQUAL (locally_encoded.size(), remotely_encoded.size());
50         BOOST_CHECK_EQUAL (memcmp (locally_encoded.data().get(), remotely_encoded.data().get(), locally_encoded.size()), 0);
51 }
52
53 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
54 {
55         shared_ptr<Image> image (new Image (PIX_FMT_RGB24, dcp::Size (1998, 1080), true));
56         uint8_t* p = image->data()[0];
57
58         for (int y = 0; y < 1080; ++y) {
59                 uint8_t* q = p;
60                 for (int x = 0; x < 1998; ++x) {
61                         *q++ = x % 256;
62                         *q++ = y % 256;
63                         *q++ = (x + y) % 256;
64                 }
65                 p += image->stride()[0];
66         }
67
68         shared_ptr<Image> sub_image (new Image (PIX_FMT_RGBA, dcp::Size (100, 200), true));
69         p = sub_image->data()[0];
70         for (int y = 0; y < 200; ++y) {
71                 uint8_t* q = p;
72                 for (int x = 0; x < 100; ++x) {
73                         *q++ = y % 256;
74                         *q++ = x % 256;
75                         *q++ = (x + y) % 256;
76                         *q++ = 1;
77                 }
78                 p += sub_image->stride()[0];
79         }
80
81         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_rgb.log"));
82
83         shared_ptr<PlayerVideo> pvf (
84                 new PlayerVideo (
85                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
86                         DCPTime (),
87                         Crop (),
88                         optional<float> (),
89                         dcp::Size (1998, 1080),
90                         dcp::Size (1998, 1080),
91                         EYES_BOTH,
92                         PART_WHOLE,
93                         ColourConversion ()
94                         )
95                 );
96
97         pvf->set_subtitle (PositionImage (sub_image, Position<int> (50, 60)));
98
99         shared_ptr<DCPVideo> frame (
100                 new DCPVideo (
101                         pvf,
102                         0,
103                         24,
104                         200000000,
105                         RESOLUTION_2K,
106                         true,
107                         log
108                         )
109                 );
110
111         Data locally_encoded = frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
112
113         Server* server = new Server (log, true);
114
115         new thread (boost::bind (&Server::run, server, 2));
116
117         /* Let the server get itself ready */
118         dcpomatic_sleep (1);
119
120         ServerDescription description ("localhost", 2);
121
122         list<thread*> threads;
123         for (int i = 0; i < 8; ++i) {
124                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
125         }
126
127         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
128                 (*i)->join ();
129         }
130
131         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
132                 delete *i;
133         }
134
135         delete server;
136 }
137
138 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
139 {
140         shared_ptr<Image> image (new Image (PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
141         uint8_t* p = image->data()[0];
142
143         for (int i = 0; i < image->components(); ++i) {
144                 uint8_t* p = image->data()[i];
145                 for (int j = 0; j < image->line_size()[i]; ++j) {
146                         *p++ = j % 256;
147                 }
148         }
149
150         shared_ptr<Image> sub_image (new Image (PIX_FMT_RGBA, dcp::Size (100, 200), true));
151         p = sub_image->data()[0];
152         for (int y = 0; y < 200; ++y) {
153                 uint8_t* q = p;
154                 for (int x = 0; x < 100; ++x) {
155                         *q++ = y % 256;
156                         *q++ = x % 256;
157                         *q++ = (x + y) % 256;
158                         *q++ = 1;
159                 }
160                 p += sub_image->stride()[0];
161         }
162
163         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_yuv.log"));
164
165         shared_ptr<PlayerVideo> pvf (
166                 new PlayerVideo (
167                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
168                         DCPTime (),
169                         Crop (),
170                         optional<float> (),
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                         true,
189                         log
190                         )
191                 );
192
193         Data locally_encoded = frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
194
195         Server* server = new Server (log, true);
196
197         new thread (boost::bind (&Server::run, server, 2));
198
199         /* Let the server get itself ready */
200         dcpomatic_sleep (1);
201
202         ServerDescription 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