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