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