C++11 tidying.
[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/ratio.h"
33 #include "lib/util.h"
34 #include "lib/video_decoder.h"
35 #include <getopt.h>
36 #include <exception>
37 #include <iomanip>
38 #include <iostream>
39
40
41 using std::cerr;
42 using std::cout;
43 using std::make_shared;
44 using std::pair;
45 using std::shared_ptr;
46 using std::string;
47 using boost::optional;
48 using boost::bind;
49 #if BOOST_VERSION >= 106100
50 using namespace boost::placeholders;
51 #endif
52 using dcp::ArrayData;
53
54
55 static shared_ptr<Film> film;
56 static EncodeServerDescription* server;
57 static int frame_count = 0;
58
59
60 void
61 process_video (shared_ptr<PlayerVideo> pvf)
62 {
63         auto local = make_shared<DCPVideo>(pvf, frame_count, film->video_frame_rate(), 250000000, Resolution::TWO_K);
64         auto remote = make_shared<DCPVideo>(pvf, frame_count, film->video_frame_rate(), 250000000, Resolution::TWO_K);
65
66         cout << "Frame " << frame_count << ": ";
67         cout.flush ();
68
69         ++frame_count;
70
71         auto local_encoded = local->encode_locally ();
72         ArrayData remote_encoded;
73
74         string remote_error;
75         try {
76                 remote_encoded = remote->encode_remotely (*server);
77         } catch (NetworkError& e) {
78                 remote_error = e.what ();
79         }
80
81         if (!remote_error.empty()) {
82                 cout << "\033[0;31mnetwork problem: " << remote_error << "\033[0m\n";
83                 return;
84         }
85
86         if (local_encoded.size() != remote_encoded.size()) {
87                 cout << "\033[0;31msizes differ\033[0m\n";
88                 return;
89         }
90
91         auto p = local_encoded.data();
92         auto q = remote_encoded.data();
93         for (int i = 0; i < local_encoded.size(); ++i) {
94                 if (*p++ != *q++) {
95                         cout << "\033[0;31mdata differ\033[0m at byte " << i << "\n";
96                         return;
97                 }
98         }
99
100         cout << "\033[0;32mgood\033[0m\n";
101 }
102
103
104 static void
105 help (string n)
106 {
107         cerr << "Syntax: " << n << " [--help] --film <film> --server <host>\n";
108         exit (EXIT_FAILURE);
109 }
110
111
112 int
113 main (int argc, char* argv[])
114 {
115         boost::filesystem::path film_dir;
116         string server_host;
117
118         while (true) {
119                 static struct option long_options[] = {
120                         { "help", no_argument, 0, 'h'},
121                         { "server", required_argument, 0, 's'},
122                         { "film", required_argument, 0, 'f'},
123                         { 0, 0, 0, 0 }
124                 };
125
126                 int option_index = 0;
127                 int c = getopt_long (argc, argv, "hs:f:", long_options, &option_index);
128
129                 if (c == -1) {
130                         break;
131                 }
132
133                 switch (c) {
134                 case 'h':
135                         help (argv[0]);
136                         exit (EXIT_SUCCESS);
137                 case 's':
138                         server_host = optarg;
139                         break;
140                 case 'f':
141                         film_dir = optarg;
142                         break;
143                 }
144         }
145
146         if (server_host.empty() || film_dir.string().empty()) {
147                 help (argv[0]);
148                 exit (EXIT_FAILURE);
149         }
150
151         dcpomatic_setup ();
152
153         try {
154                 server = new EncodeServerDescription (server_host, 1, SERVER_LINK_VERSION);
155                 film = make_shared<Film>(film_dir);
156                 film->read_metadata ();
157
158                 auto player = make_shared<Player>(film);
159                 player->Video.connect (bind(&process_video, _1));
160                 while (!player->pass ()) {}
161         } catch (std::exception& e) {
162                 cerr << "Error: " << e.what() << "\n";
163         }
164
165         return 0;
166 }