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