No-op; fix GPL address and use the explicit-program-name version.
[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 boost::shared_ptr;
44 using dcp::Data;
45
46 static shared_ptr<Film> film;
47 static EncodeServerDescription* server;
48 static shared_ptr<FileLog> log_ (new FileLog ("servomatictest.log"));
49 static int frame_count = 0;
50
51 void
52 process_video (shared_ptr<PlayerVideo> pvf)
53 {
54         shared_ptr<DCPVideo> local  (new DCPVideo (pvf, frame_count, film->video_frame_rate(), 250000000, RESOLUTION_2K, log_));
55         shared_ptr<DCPVideo> remote (new DCPVideo (pvf, frame_count, film->video_frame_rate(), 250000000, RESOLUTION_2K, log_));
56
57         cout << "Frame " << frame_count << ": ";
58         cout.flush ();
59
60         ++frame_count;
61
62         Data local_encoded = local->encode_locally (boost::bind (&Log::dcp_log, log_.get(), _1, _2));
63         Data remote_encoded;
64
65         string remote_error;
66         try {
67                 remote_encoded = remote->encode_remotely (*server);
68         } catch (NetworkError& e) {
69                 remote_error = e.what ();
70         }
71
72         if (!remote_error.empty ()) {
73                 cout << "\033[0;31mnetwork problem: " << remote_error << "\033[0m\n";
74                 return;
75         }
76
77         if (local_encoded.size() != remote_encoded.size()) {
78                 cout << "\033[0;31msizes differ\033[0m\n";
79                 return;
80         }
81
82         uint8_t* p = local_encoded.data().get ();
83         uint8_t* q = remote_encoded.data().get ();
84         for (int i = 0; i < local_encoded.size(); ++i) {
85                 if (*p++ != *q++) {
86                         cout << "\033[0;31mdata differ\033[0m at byte " << i << "\n";
87                         return;
88                 }
89         }
90
91         cout << "\033[0;32mgood\033[0m\n";
92 }
93
94 static void
95 help (string n)
96 {
97         cerr << "Syntax: " << n << " [--help] --film <film> --server <host>\n";
98         exit (EXIT_FAILURE);
99 }
100
101 int
102 main (int argc, char* argv[])
103 {
104         string film_dir;
105         string server_host;
106
107         while (true) {
108                 static struct option long_options[] = {
109                         { "help", no_argument, 0, 'h'},
110                         { "server", required_argument, 0, 's'},
111                         { "film", required_argument, 0, 'f'},
112                         { 0, 0, 0, 0 }
113                 };
114
115                 int option_index = 0;
116                 int c = getopt_long (argc, argv, "hs:f:", long_options, &option_index);
117
118                 if (c == -1) {
119                         break;
120                 }
121
122                 switch (c) {
123                 case 'h':
124                         help (argv[0]);
125                         exit (EXIT_SUCCESS);
126                 case 's':
127                         server_host = optarg;
128                         break;
129                 case 'f':
130                         film_dir = optarg;
131                         break;
132                 }
133         }
134
135         if (server_host.empty() || film_dir.empty()) {
136                 help (argv[0]);
137                 exit (EXIT_FAILURE);
138         }
139
140         dcpomatic_setup ();
141
142         try {
143                 server = new EncodeServerDescription (server_host, 1);
144                 film.reset (new Film (film_dir));
145                 film->read_metadata ();
146
147                 shared_ptr<Player> player (new Player (film, film->playlist ()));
148
149                 DCPTime const frame = DCPTime::from_frames (1, film->video_frame_rate ());
150                 for (DCPTime t; t < film->length(); t += frame) {
151                         process_video (player->get_video(t, true).front ());
152                 }
153         } catch (std::exception& e) {
154                 cerr << "Error: " << e.what() << "\n";
155         }
156
157         return 0;
158 }