6f7a435690a8645b29a02ed7bbcdb9370248f29f
[dcpomatic.git] / src / tools / server_test.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21 #include <iomanip>
22 #include <exception>
23 #include <getopt.h>
24 #include "lib/ratio.h"
25 #include "lib/film.h"
26 #include "lib/filter.h"
27 #include "lib/util.h"
28 #include "lib/server.h"
29 #include "lib/dcp_video.h"
30 #include "lib/decoder.h"
31 #include "lib/exceptions.h"
32 #include "lib/log.h"
33 #include "lib/video_decoder.h"
34 #include "lib/player.h"
35 #include "lib/player_video.h"
36 #include "lib/data.h"
37
38 using std::cout;
39 using std::cerr;
40 using std::string;
41 using std::pair;
42 using boost::shared_ptr;
43
44 static shared_ptr<Film> film;
45 static ServerDescription* server;
46 static shared_ptr<FileLog> log_ (new FileLog ("servomatictest.log"));
47 static int frame_count = 0;
48
49 void
50 process_video (shared_ptr<PlayerVideo> pvf)
51 {
52         shared_ptr<DCPVideo> local  (new DCPVideo (pvf, frame_count, film->video_frame_rate(), 250000000, RESOLUTION_2K, true, log_));
53         shared_ptr<DCPVideo> remote (new DCPVideo (pvf, frame_count, film->video_frame_rate(), 250000000, RESOLUTION_2K, true, log_));
54
55         cout << "Frame " << frame_count << ": ";
56         cout.flush ();
57
58         ++frame_count;
59
60         Data local_encoded = local->encode_locally (boost::bind (&Log::dcp_log, log_.get(), _1, _2));
61         Data remote_encoded;
62
63         string remote_error;
64         try {
65                 remote_encoded = remote->encode_remotely (*server);
66         } catch (NetworkError& e) {
67                 remote_error = e.what ();
68         }
69
70         if (!remote_error.empty ()) {
71                 cout << "\033[0;31mnetwork problem: " << remote_error << "\033[0m\n";
72                 return;
73         }
74
75         if (local_encoded.size() != remote_encoded.size()) {
76                 cout << "\033[0;31msizes differ\033[0m\n";
77                 return;
78         }
79                 
80         uint8_t* p = local_encoded.data().get ();
81         uint8_t* q = remote_encoded.data().get ();
82         for (int i = 0; i < local_encoded.size(); ++i) {
83                 if (*p++ != *q++) {
84                         cout << "\033[0;31mdata differ\033[0m at byte " << i << "\n";
85                         return;
86                 }
87         }
88
89         cout << "\033[0;32mgood\033[0m\n";
90 }
91
92 static void
93 help (string n)
94 {
95         cerr << "Syntax: " << n << " [--help] --film <film> --server <host>\n";
96         exit (EXIT_FAILURE);
97 }
98
99 int
100 main (int argc, char* argv[])
101 {
102         string film_dir;
103         string server_host;
104
105         while (true) {
106                 static struct option long_options[] = {
107                         { "help", no_argument, 0, 'h'},
108                         { "server", required_argument, 0, 's'},
109                         { "film", required_argument, 0, 'f'},
110                         { 0, 0, 0, 0 }
111                 };
112
113                 int option_index = 0;
114                 int c = getopt_long (argc, argv, "hs:f:", long_options, &option_index);
115
116                 if (c == -1) {
117                         break;
118                 }
119
120                 switch (c) {
121                 case 'h':
122                         help (argv[0]);
123                         exit (EXIT_SUCCESS);
124                 case 's':
125                         server_host = optarg;
126                         break;
127                 case 'f':
128                         film_dir = optarg;
129                         break;
130                 }
131         }
132         
133         if (server_host.empty() || film_dir.empty()) {
134                 help (argv[0]);
135                 exit (EXIT_FAILURE);
136         }
137
138         dcpomatic_setup ();
139
140         try {
141                 server = new ServerDescription (server_host, 1);
142                 film.reset (new Film (film_dir));
143                 film->read_metadata ();
144                 
145                 shared_ptr<Player> player (new Player (film));
146
147                 DCPTime const frame = DCPTime::from_frames (1, film->video_frame_rate ());
148                 for (DCPTime t; t < film->length(); t += frame) {
149                         process_video (player->get_video(t, true).front ());
150                 }
151         } catch (std::exception& e) {
152                 cerr << "Error: " << e.what() << "\n";
153         }
154
155         return 0;
156 }