test/data updates.
[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/encoded_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, shared_ptr<EncodedData> locally_encoded)
45 {
46         shared_ptr<EncodedData> remotely_encoded;
47         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
48         BOOST_CHECK (remotely_encoded);
49         
50         BOOST_CHECK_EQUAL (locally_encoded->size(), remotely_encoded->size());
51         BOOST_CHECK_EQUAL (memcmp (locally_encoded->data(), remotely_encoded->data(), locally_encoded->size()), 0);
52 }
53
54 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
55 {
56         shared_ptr<Image> image (new Image (PIX_FMT_RGB24, dcp::Size (1998, 1080), true));
57         uint8_t* p = image->data()[0];
58         
59         for (int y = 0; y < 1080; ++y) {
60                 uint8_t* q = p;
61                 for (int x = 0; x < 1998; ++x) {
62                         *q++ = x % 256;
63                         *q++ = y % 256;
64                         *q++ = (x + y) % 256;
65                 }
66                 p += image->stride()[0];
67         }
68
69         shared_ptr<Image> sub_image (new Image (PIX_FMT_RGBA, dcp::Size (100, 200), true));
70         p = sub_image->data()[0];
71         for (int y = 0; y < 200; ++y) {
72                 uint8_t* q = p;
73                 for (int x = 0; x < 100; ++x) {
74                         *q++ = y % 256;
75                         *q++ = x % 256;
76                         *q++ = (x + y) % 256;
77                         *q++ = 1;
78                 }
79                 p += sub_image->stride()[0];
80         }
81
82         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_rgb.log"));
83
84         shared_ptr<PlayerVideo> pvf (
85                 new PlayerVideo (
86                         shared_ptr<ImageProxy> (new RawImageProxy (image)),
87                         DCPTime (),
88                         Crop (),
89                         optional<float> (),
90                         dcp::Size (1998, 1080),
91                         dcp::Size (1998, 1080),
92                         EYES_BOTH,
93                         PART_WHOLE,
94                         ColourConversion ()
95                         )
96                 );
97
98         pvf->set_subtitle (PositionImage (sub_image, Position<int> (50, 60)));
99
100         shared_ptr<DCPVideo> frame (
101                 new DCPVideo (
102                         pvf,
103                         0,
104                         24,
105                         200000000,
106                         RESOLUTION_2K,
107                         true,
108                         log
109                         )
110                 );
111
112         shared_ptr<EncodedData> locally_encoded = frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
113         BOOST_ASSERT (locally_encoded);
114         
115         Server* server = new Server (log, true);
116
117         new thread (boost::bind (&Server::run, server, 2));
118
119         /* Let the server get itself ready */
120         dcpomatic_sleep (1);
121
122         ServerDescription 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 (PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
143         uint8_t* p = image->data()[0];
144
145         for (int i = 0; i < image->components(); ++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 (PIX_FMT_RGBA, dcp::Size (100, 200), true));
153         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<float> (),
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                         true,
191                         log
192                         )
193                 );
194
195         shared_ptr<EncodedData> locally_encoded = frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
196         BOOST_ASSERT (locally_encoded);
197         
198         Server* server = new Server (log, true);
199
200         new thread (boost::bind (&Server::run, server, 2));
201
202         /* Let the server get itself ready */
203         dcpomatic_sleep (1);
204
205         ServerDescription description ("localhost", 2);
206
207         list<thread*> threads;
208         for (int i = 0; i < 8; ++i) {
209                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
210         }
211
212         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
213                 (*i)->join ();
214         }
215
216         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
217                 delete *i;
218         }
219
220         delete server;
221 }
222