Merge.
[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 #include <boost/test/unit_test.hpp>
21 #include <boost/thread.hpp>
22 #include "lib/server.h"
23 #include "lib/image.h"
24 #include "lib/cross.h"
25 #include "lib/dcp_video_frame.h"
26 #include "lib/scaler.h"
27 #include "lib/player_video_frame.h"
28 #include "lib/image_proxy.h"
29
30 using std::list;
31 using boost::shared_ptr;
32 using boost::thread;
33
34 void
35 do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription description, shared_ptr<EncodedData> locally_encoded)
36 {
37         shared_ptr<EncodedData> remotely_encoded;
38         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
39         BOOST_CHECK (remotely_encoded);
40         
41         BOOST_CHECK_EQUAL (locally_encoded->size(), remotely_encoded->size());
42         BOOST_CHECK (memcmp (locally_encoded->data(), remotely_encoded->data(), locally_encoded->size()) == 0);
43 }
44
45 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
46 {
47         shared_ptr<Image> image (new Image (PIX_FMT_RGB24, libdcp::Size (1998, 1080), true));
48         uint8_t* p = image->data()[0];
49         
50         for (int y = 0; y < 1080; ++y) {
51                 uint8_t* q = p;
52                 for (int x = 0; x < 1998; ++x) {
53                         *q++ = x % 256;
54                         *q++ = y % 256;
55                         *q++ = (x + y) % 256;
56                 }
57                 p += image->stride()[0];
58         }
59
60         shared_ptr<Image> sub_image (new Image (PIX_FMT_RGBA, libdcp::Size (100, 200), true));
61         p = sub_image->data()[0];
62         for (int y = 0; y < 200; ++y) {
63                 uint8_t* q = p;
64                 for (int x = 0; x < 100; ++x) {
65                         *q++ = y % 256;
66                         *q++ = x % 256;
67                         *q++ = (x + y) % 256;
68                         *q++ = 1;
69                 }
70                 p += sub_image->stride()[0];
71         }
72
73         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_rgb.log"));
74
75         shared_ptr<PlayerVideoFrame> pvf (
76                 new PlayerVideoFrame (
77                         shared_ptr<ImageProxy> (new RawImageProxy (image, log)),
78                         Crop (),
79                         libdcp::Size (1998, 1080),
80                         libdcp::Size (1998, 1080),
81                         Scaler::from_id ("bicubic"),
82                         EYES_BOTH,
83                         PART_WHOLE,
84                         ColourConversion ()
85                         )
86                 );
87
88         pvf->set_subtitle (sub_image, Position<int> (50, 60));
89
90         shared_ptr<DCPVideoFrame> frame (
91                 new DCPVideoFrame (
92                         pvf,
93                         0,
94                         24,
95                         200000000,
96                         RESOLUTION_2K,
97                         log
98                         )
99                 );
100
101         shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
102         BOOST_ASSERT (locally_encoded);
103         
104         Server* server = new Server (log, true);
105
106         new thread (boost::bind (&Server::run, server, 2));
107
108         /* Let the server get itself ready */
109         dcpomatic_sleep (1);
110
111         ServerDescription description ("localhost", 2);
112
113         list<thread*> threads;
114         for (int i = 0; i < 8; ++i) {
115                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
116         }
117
118         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
119                 (*i)->join ();
120         }
121
122         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
123                 delete *i;
124         }
125 }
126
127 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
128 {
129         shared_ptr<Image> image (new Image (PIX_FMT_YUV420P, libdcp::Size (1998, 1080), true));
130         uint8_t* p = image->data()[0];
131
132         for (int i = 0; i < image->components(); ++i) {
133                 uint8_t* p = image->data()[i];
134                 for (int j = 0; j < image->line_size()[i]; ++j) {
135                         *p++ = j % 256;
136                 }
137         }
138
139         shared_ptr<Image> sub_image (new Image (PIX_FMT_RGBA, libdcp::Size (100, 200), true));
140         p = sub_image->data()[0];
141         for (int y = 0; y < 200; ++y) {
142                 uint8_t* q = p;
143                 for (int x = 0; x < 100; ++x) {
144                         *q++ = y % 256;
145                         *q++ = x % 256;
146                         *q++ = (x + y) % 256;
147                         *q++ = 1;
148                 }
149                 p += sub_image->stride()[0];
150         }
151
152         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test_yuv.log"));
153
154         shared_ptr<PlayerVideoFrame> pvf (
155                 new PlayerVideoFrame (
156                         shared_ptr<ImageProxy> (new RawImageProxy (image, log)),
157                         Crop (),
158                         libdcp::Size (1998, 1080),
159                         libdcp::Size (1998, 1080),
160                         Scaler::from_id ("bicubic"),
161                         EYES_BOTH,
162                         PART_WHOLE,
163                         ColourConversion ()
164                         )
165                 );
166
167         pvf->set_subtitle (sub_image, Position<int> (50, 60));
168
169         shared_ptr<DCPVideoFrame> frame (
170                 new DCPVideoFrame (
171                         pvf,
172                         0,
173                         24,
174                         200000000,
175                         RESOLUTION_2K,
176                         log
177                         )
178                 );
179
180         shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
181         BOOST_ASSERT (locally_encoded);
182         
183         Server* server = new Server (log, true);
184
185         new thread (boost::bind (&Server::run, server, 2));
186
187         /* Let the server get itself ready */
188         dcpomatic_sleep (1);
189
190         ServerDescription description ("localhost", 2);
191
192         list<thread*> threads;
193         for (int i = 0; i < 8; ++i) {
194                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
195         }
196
197         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
198                 (*i)->join ();
199         }
200
201         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
202                 delete *i;
203         }
204 }
205