Separate out local/remote encode code from DCPVideo.
[dcpomatic.git] / src / tools / server_test.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "lib/dcp_video.h"
23 #include "lib/decoder.h"
24 #include "lib/encode_server.h"
25 #include "lib/encode_server_description.h"
26 #include "lib/exceptions.h"
27 #include "lib/file_log.h"
28 #include "lib/film.h"
29 #include "lib/filter.h"
30 #include "lib/player.h"
31 #include "lib/player_video.h"
32 #include "lib/j2k_encoder_cpu_backend.h"
33 #include "lib/j2k_encoder_remote_backend.h"
34 #include "lib/ratio.h"
35 #include "lib/util.h"
36 #include "lib/video_decoder.h"
37 #include <getopt.h>
38 #include <exception>
39 #include <iomanip>
40 #include <iostream>
41
42
43 using std::cerr;
44 using std::cout;
45 using std::make_shared;
46 using std::shared_ptr;
47 using std::string;
48 using boost::optional;
49 using boost::bind;
50 #if BOOST_VERSION >= 106100
51 using namespace boost::placeholders;
52 #endif
53 using dcp::ArrayData;
54 using namespace dcpomatic;
55
56
57 static shared_ptr<Film> film;
58 static EncodeServerDescription* server;
59 static int frame_count = 0;
60
61
62 void
63 process_video (shared_ptr<PlayerVideo> pvf)
64 {
65         DCPVideo local(pvf, frame_count, film->video_frame_rate(), 250000000, Resolution::TWO_K);
66         DCPVideo remote(pvf, frame_count, film->video_frame_rate(), 250000000, Resolution::TWO_K);
67
68         cout << "Frame " << frame_count << ": ";
69         cout.flush ();
70
71         ++frame_count;
72
73         J2KEncoderCPUBackend cpu_backend;
74         auto local_encoded = cpu_backend.encode(local);
75         optional<ArrayData> remote_encoded;
76
77         string remote_error;
78         J2KEncoderRemoteBackend remote_backend(*server);
79         try {
80                 remote_encoded = remote_backend.encode(remote);
81         } catch (NetworkError& e) {
82                 remote_error = e.what ();
83         }
84
85         DCPOMATIC_ASSERT (remote_encoded);
86
87         if (!remote_error.empty()) {
88                 cout << "\033[0;31mnetwork problem: " << remote_error << "\033[0m\n";
89                 return;
90         }
91
92         if (local_encoded->size() != remote_encoded->size()) {
93                 cout << "\033[0;31msizes differ\033[0m\n";
94                 return;
95         }
96
97         auto p = local_encoded->data();
98         auto q = remote_encoded->data();
99         for (int i = 0; i < local_encoded->size(); ++i) {
100                 if (*p++ != *q++) {
101                         cout << "\033[0;31mdata differ\033[0m at byte " << i << "\n";
102                         return;
103                 }
104         }
105
106         cout << "\033[0;32mgood\033[0m\n";
107 }
108
109
110 static void
111 help (string n)
112 {
113         cerr << "Syntax: " << n << " [--help] --film <film> --server <host>\n";
114         exit (EXIT_FAILURE);
115 }
116
117
118 int
119 main (int argc, char* argv[])
120 {
121         boost::filesystem::path film_dir;
122         string server_host;
123
124         while (true) {
125                 static struct option long_options[] = {
126                         { "help", no_argument, 0, 'h'},
127                         { "server", required_argument, 0, 's'},
128                         { "film", required_argument, 0, 'f'},
129                         { 0, 0, 0, 0 }
130                 };
131
132                 int option_index = 0;
133                 int c = getopt_long (argc, argv, "hs:f:", long_options, &option_index);
134
135                 if (c == -1) {
136                         break;
137                 }
138
139                 switch (c) {
140                 case 'h':
141                         help (argv[0]);
142                         exit (EXIT_SUCCESS);
143                 case 's':
144                         server_host = optarg;
145                         break;
146                 case 'f':
147                         film_dir = optarg;
148                         break;
149                 }
150         }
151
152         if (server_host.empty() || film_dir.string().empty()) {
153                 help (argv[0]);
154                 exit (EXIT_FAILURE);
155         }
156
157         dcpomatic_setup ();
158
159         try {
160                 server = new EncodeServerDescription (server_host, 1, SERVER_LINK_VERSION);
161                 film = make_shared<Film>(film_dir);
162                 film->read_metadata ();
163
164                 auto player = make_shared<Player>(film, Image::Alignment::COMPACT);
165                 player->Video.connect (bind(&process_video, _1));
166                 while (!player->pass ()) {}
167         } catch (std::exception& e) {
168                 cerr << "Error: " << e.what() << "\n";
169         }
170
171         return 0;
172 }