Use cxml::ConstNodePtr.
[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_frame.h"
34
35 using std::list;
36 using boost::shared_ptr;
37 using boost::thread;
38
39 void
40 do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription description, shared_ptr<EncodedData> locally_encoded)
41 {
42         shared_ptr<EncodedData> remotely_encoded;
43         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
44         BOOST_CHECK (remotely_encoded);
45         
46         BOOST_CHECK_EQUAL (locally_encoded->size(), remotely_encoded->size());
47         BOOST_CHECK_EQUAL (memcmp (locally_encoded->data(), remotely_encoded->data(), locally_encoded->size()), 0);
48 }
49
50 BOOST_AUTO_TEST_CASE (client_server_test)
51 {
52         shared_ptr<Image> image (new Image (PIX_FMT_RGB24, dcp::Size (1998, 1080), true));
53         uint8_t* p = image->data()[0];
54         
55         for (int y = 0; y < 1080; ++y) {
56                 uint8_t* q = p;
57                 for (int x = 0; x < 1998; ++x) {
58                         *q++ = x % 256;
59                         *q++ = y % 256;
60                         *q++ = (x + y) % 256;
61                 }
62                 p += image->stride()[0];
63         }
64
65         shared_ptr<Image> sub_image (new Image (PIX_FMT_RGBA, dcp::Size (100, 200), true));
66         p = sub_image->data()[0];
67         for (int y = 0; y < 200; ++y) {
68                 uint8_t* q = p;
69                 for (int x = 0; x < 100; ++x) {
70                         *q++ = y % 256;
71                         *q++ = x % 256;
72                         *q++ = (x + y) % 256;
73                         *q++ = 1;
74                 }
75                 p += sub_image->stride()[0];
76         }
77
78         /* XXX */
79 //      shared_ptr<Subtitle> subtitle (new Subtitle (Position<int> (50, 60), sub_image));
80
81         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test.log"));
82
83         shared_ptr<DCPVideoFrame> frame (
84                 new DCPVideoFrame (
85                         image,
86                         0,
87                         EYES_BOTH,
88                         ColourConversion (),
89                         24,
90                         200000000,
91                         RESOLUTION_2K,
92                         log
93                         )
94                 );
95
96         shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
97         BOOST_ASSERT (locally_encoded);
98         
99         Server* server = new Server (log, true);
100
101         new thread (boost::bind (&Server::run, server, 2));
102
103         /* Let the server get itself ready */
104         dcpomatic_sleep (1);
105
106         ServerDescription description ("localhost", 2);
107
108         list<thread*> threads;
109         for (int i = 0; i < 8; ++i) {
110                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, description, locally_encoded)));
111         }
112
113         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
114                 (*i)->join ();
115         }
116
117         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
118                 delete *i;
119         }
120 }
121