2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file test/client_server_test.cc
22 * @brief Test the server class.
24 * Create a test image and then encode it using the standard mechanism
25 * and also using a EncodeServer object running on localhost. Compare the resulting
26 * encoded data to check that they are the same.
29 #include "lib/encode_server.h"
30 #include "lib/image.h"
31 #include "lib/cross.h"
32 #include "lib/dcp_video.h"
33 #include "lib/player_video.h"
34 #include "lib/raw_image_proxy.h"
35 #include "lib/j2k_image_proxy.h"
36 #include "lib/encode_server_description.h"
37 #include "lib/file_log.h"
38 #include <boost/test/unit_test.hpp>
39 #include <boost/thread.hpp>
40 #include <boost/make_shared.hpp>
43 using boost::shared_ptr;
44 using boost::make_shared;
46 using boost::optional;
50 do_remote_encode (shared_ptr<DCPVideo> frame, EncodeServerDescription description, Data locally_encoded)
52 Data remotely_encoded;
53 BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description, 60));
55 BOOST_CHECK_EQUAL (locally_encoded.size(), remotely_encoded.size());
56 BOOST_CHECK_EQUAL (memcmp (locally_encoded.data().get(), remotely_encoded.data().get(), locally_encoded.size()), 0);
59 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
61 shared_ptr<Image> image = make_shared<Image> (AV_PIX_FMT_RGB24, dcp::Size (1998, 1080), true);
62 uint8_t* p = image->data()[0];
64 for (int y = 0; y < 1080; ++y) {
66 for (int x = 0; x < 1998; ++x) {
71 p += image->stride()[0];
74 shared_ptr<Image> sub_image = make_shared<Image> (AV_PIX_FMT_RGBA, dcp::Size (100, 200), true);
75 p = sub_image->data()[0];
76 for (int y = 0; y < 200; ++y) {
78 for (int x = 0; x < 100; ++x) {
84 p += sub_image->stride()[0];
87 shared_ptr<FileLog> log = make_shared<FileLog> ("build/test/client_server_test_rgb.log");
89 shared_ptr<PlayerVideo> pvf (
91 make_shared<RawImageProxy> (image),
95 dcp::Size (1998, 1080),
96 dcp::Size (1998, 1080),
103 pvf->set_subtitle (PositionImage (sub_image, Position<int> (50, 60)));
105 shared_ptr<DCPVideo> frame (
116 Data locally_encoded = frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
118 EncodeServer* server = new EncodeServer (log, true, 2);
120 new thread (boost::bind (&EncodeServer::run, server));
122 /* Let the server get itself ready */
125 EncodeServerDescription description ("localhost", 2);
127 list<thread*> threads;
128 for (int i = 0; i < 8; ++i) {
129 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
132 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
136 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
143 BOOST_AUTO_TEST_CASE (client_server_test_yuv)
145 shared_ptr<Image> image = make_shared<Image> (AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), true);
147 for (int i = 0; i < image->planes(); ++i) {
148 uint8_t* p = image->data()[i];
149 for (int j = 0; j < image->line_size()[i]; ++j) {
154 shared_ptr<Image> sub_image = make_shared<Image> (AV_PIX_FMT_RGBA, dcp::Size (100, 200), true);
155 uint8_t* p = sub_image->data()[0];
156 for (int y = 0; y < 200; ++y) {
158 for (int x = 0; x < 100; ++x) {
161 *q++ = (x + y) % 256;
164 p += sub_image->stride()[0];
167 shared_ptr<FileLog> log = make_shared<FileLog> ("build/test/client_server_test_yuv.log");
169 shared_ptr<PlayerVideo> pvf (
171 make_shared<RawImageProxy> (image),
175 dcp::Size (1998, 1080),
176 dcp::Size (1998, 1080),
183 pvf->set_subtitle (PositionImage (sub_image, Position<int> (50, 60)));
185 shared_ptr<DCPVideo> frame (
196 Data locally_encoded = frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
198 EncodeServer* server = new EncodeServer (log, true, 2);
200 new thread (boost::bind (&EncodeServer::run, server));
202 /* Let the server get itself ready */
205 EncodeServerDescription description ("localhost", 2);
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)));
212 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
216 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
223 BOOST_AUTO_TEST_CASE (client_server_test_j2k)
225 shared_ptr<Image> image = make_shared<Image> (AV_PIX_FMT_YUV420P, dcp::Size (1998, 1080), true);
227 for (int i = 0; i < image->planes(); ++i) {
228 uint8_t* p = image->data()[i];
229 for (int j = 0; j < image->line_size()[i]; ++j) {
234 shared_ptr<FileLog> log = make_shared<FileLog> ("build/test/client_server_test_j2k.log");
236 shared_ptr<PlayerVideo> raw_pvf (
238 make_shared<RawImageProxy> (image),
242 dcp::Size (1998, 1080),
243 dcp::Size (1998, 1080),
250 shared_ptr<DCPVideo> raw_frame (
261 Data raw_locally_encoded = raw_frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
263 shared_ptr<PlayerVideo> j2k_pvf (
265 /* This J2KImageProxy constructor is private, so no make_shared */
266 shared_ptr<J2KImageProxy> (new J2KImageProxy (raw_locally_encoded, dcp::Size (1998, 1080))),
270 dcp::Size (1998, 1080),
271 dcp::Size (1998, 1080),
274 PresetColourConversion::all().front().conversion
278 shared_ptr<DCPVideo> j2k_frame (
289 Data j2k_locally_encoded = j2k_frame->encode_locally (boost::bind (&Log::dcp_log, log.get(), _1, _2));
291 EncodeServer* server = new EncodeServer (log, true, 2);
293 new thread (boost::bind (&EncodeServer::run, server));
295 /* Let the server get itself ready */
298 EncodeServerDescription description ("localhost", 2);
300 list<thread*> threads;
301 for (int i = 0; i < 8; ++i) {
302 threads.push_back (new thread (boost::bind (do_remote_encode, j2k_frame, description, j2k_locally_encoded)));
305 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
309 for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {