Fix recover test digest since colour conversion digests changed again.
[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/scaler.h"
35 #include "lib/player_video.h"
36 #include "lib/raw_image_proxy.h"
37 #include "lib/encoded_data.h"
38
39 using std::list;
40 using boost::shared_ptr;
41 using boost::thread;
42 using boost::optional;
43
44 void
45 do_remote_encode (shared_ptr<DCPVideo> frame, ServerDescription description, shared_ptr<EncodedData> locally_encoded)
46 {
47         shared_ptr<EncodedData> remotely_encoded;
48         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
49         BOOST_CHECK (remotely_encoded);
50         
51         BOOST_CHECK_EQUAL (locally_encoded->size(), remotely_encoded->size());
52         BOOST_CHECK_EQUAL (memcmp (locally_encoded->data(), remotely_encoded->data(), locally_encoded->size()), 0);
53 }
54
55 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
56 {
57         shared_ptr<Image> image (new Image (PIX_FMT_RGB24, dcp::Size (1998, 1080), true));
58         uint8_t* p = image->data()[0];
59         
60         for (int y = 0; y < 1080; ++y) {
61                 uint8_t* q = p;
62                 for (int x = 0; x < 1998; ++x) {
63                         *q++ = x % 256;
64                         *q++ = y % 256;
65                         *q++ = (x + y) % 256;
66                 }
67                 p += image->stride()[0];
68         }
69
70         shared_ptr<Image> sub_image (new Image (PIX_FMT_RGBA, dcp::Size (100, 200), true));
71         p = sub_image->data()[0];
72         for (int y = 0; y < 200; ++y) {
73                 uint8_t* q = p;
74                 for (int x = 0; x < 100; ++x) {
75                         *q++ = y % 256;
76                         *q++ = x % 256;
77                         *q++ = (x + y) % 256;
78                         *q++ = 1;
79                 }
80                 p += sub_image->stride()[0];
81         }
82
83         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_rgb.log"));
84
85         shared_ptr<PlayerVideo> pvf (
86                 new PlayerVideo (
87                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
88                         DCPTime (),
89                         Crop (),
90                         optional<float> (),
91                         dcp::Size (1998, 1080),
92                         dcp::Size (1998, 1080),
93                         Scaler::from_id ("bicubic"),
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                         true,
110                         log
111                         )
112                 );
113
114         shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
115         BOOST_ASSERT (locally_encoded);
116         
117         Server* server = new Server (log, true);
118
119         new thread (boost::bind (&Server::run, server, 2));
120
121         /* Let the server get itself ready */
122         dcpomatic_sleep (1);
123
124         ServerDescription description ("localhost", 2);
125
126         list<thread*> threads;
127         for (int i = 0; i < 8; ++i) {
128                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
129         }
130
131         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
132                 (*i)->join ();
133         }
134
135         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
136                 delete *i;
137         }
138
139         delete server;
140 }
141
142 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
143 {
144         shared_ptr<Image> image (new Image (PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
145         uint8_t* p = image->data()[0];
146
147         for (int i = 0; i < image->components(); ++i) {
148                 uint8_t* p = image->data()[i];
149                 for (int j = 0; j < image->line_size()[i]; ++j) {
150                         *p++ = j % 256;
151                 }
152         }
153
154         shared_ptr<Image> sub_image (new Image (PIX_FMT_RGBA, dcp::Size (100, 200), true));
155         p = sub_image->data()[0];
156         for (int y = 0; y < 200; ++y) {
157                 uint8_t* q = p;
158                 for (int x = 0; x < 100; ++x) {
159                         *q++ = y % 256;
160                         *q++ = x % 256;
161                         *q++ = (x + y) % 256;
162                         *q++ = 1;
163                 }
164                 p += sub_image->stride()[0];
165         }
166
167         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_yuv.log"));
168
169         shared_ptr<PlayerVideo> pvf (
170                 new PlayerVideo (
171                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
172                         DCPTime (),
173                         Crop (),
174                         optional<float> (),
175                         dcp::Size (1998, 1080),
176                         dcp::Size (1998, 1080),
177                         Scaler::from_id ("bicubic"),
178                         EYES_BOTH,
179                         PART_WHOLE,
180                         ColourConversion ()
181                         )
182                 );
183
184         pvf->set_subtitle (PositionImage (sub_image, Position<int> (50, 60)));
185
186         shared_ptr<DCPVideo> frame (
187                 new DCPVideo (
188                         pvf,
189                         0,
190                         24,
191                         200000000,
192                         RESOLUTION_2K,
193                         true,
194                         log
195                         )
196                 );
197
198         shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
199         BOOST_ASSERT (locally_encoded);
200         
201         Server* server = new Server (log, true);
202
203         new thread (boost::bind (&Server::run, server, 2));
204
205         /* Let the server get itself ready */
206         dcpomatic_sleep (1);
207
208         ServerDescription description ("localhost", 2);
209
210         list<thread*> threads;
211         for (int i = 0; i < 8; ++i) {
212                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
213         }
214
215         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
216                 (*i)->join ();
217         }
218
219         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
220                 delete *i;
221         }
222
223         delete server;
224 }
225