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