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