Fix build.
[dcpomatic.git] / src / tools / server_test.cc
1 /*
2     Copyright (C) 2012 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/scaler.h"
29 #include "lib/server.h"
30 #include "lib/dcp_video_frame.h"
31 #include "lib/decoder.h"
32 #include "lib/exceptions.h"
33 #include "lib/scaler.h"
34 #include "lib/log.h"
35 #include "lib/video_decoder.h"
36 #include "lib/player.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 = 0;
48
49 void
50 process_video (shared_ptr<PlayerImage> image, Eyes eyes, ColourConversion conversion, DCPTime)
51 {
52         shared_ptr<DCPVideoFrame> local  (
53                 new DCPVideoFrame (image->image (PIX_FMT_RGB24, false), frame, eyes, conversion, film->video_frame_rate(), 250000000, RESOLUTION_2K, log_)
54                 );
55         
56         shared_ptr<DCPVideoFrame> remote (
57                 new DCPVideoFrame (image->image (PIX_FMT_RGB24, false), frame, eyes, conversion, film->video_frame_rate(), 250000000, RESOLUTION_2K, log_)
58                 );
59
60         cout << "Frame " << frame << ": ";
61         cout.flush ();
62
63         ++frame;
64
65         shared_ptr<EncodedData> local_encoded = local->encode_locally ();
66         shared_ptr<EncodedData> remote_encoded;
67
68         string remote_error;
69         try {
70                 remote_encoded = remote->encode_remotely (*server);
71         } catch (NetworkError& e) {
72                 remote_error = e.what ();
73         }
74
75         if (!remote_error.empty ()) {
76                 cout << "\033[0;31mnetwork problem: " << remote_error << "\033[0m\n";
77                 return;
78         }
79
80         if (local_encoded->size() != remote_encoded->size()) {
81                 cout << "\033[0;31msizes differ\033[0m\n";
82                 return;
83         }
84                 
85         uint8_t* p = local_encoded->data();
86         uint8_t* q = remote_encoded->data();
87         for (int i = 0; i < local_encoded->size(); ++i) {
88                 if (*p++ != *q++) {
89                         cout << "\033[0;31mdata differ\033[0m at byte " << i << "\n";
90                         return;
91                 }
92         }
93
94         cout << "\033[0;32mgood\033[0m\n";
95 }
96
97 static void
98 help (string n)
99 {
100         cerr << "Syntax: " << n << " [--help] --film <film> --server <host>\n";
101         exit (EXIT_FAILURE);
102 }
103
104 int
105 main (int argc, char* argv[])
106 {
107         string film_dir;
108         string server_host;
109
110         while (1) {
111                 static struct option long_options[] = {
112                         { "help", no_argument, 0, 'h'},
113                         { "server", required_argument, 0, 's'},
114                         { "film", required_argument, 0, 'f'},
115                         { 0, 0, 0, 0 }
116                 };
117
118                 int option_index = 0;
119                 int c = getopt_long (argc, argv, "hs:f:", long_options, &option_index);
120
121                 if (c == -1) {
122                         break;
123                 }
124
125                 switch (c) {
126                 case 'h':
127                         help (argv[0]);
128                         exit (EXIT_SUCCESS);
129                 case 's':
130                         server_host = optarg;
131                         break;
132                 case 'f':
133                         film_dir = optarg;
134                         break;
135                 }
136         }
137         
138         if (server_host.empty() || film_dir.empty()) {
139                 help (argv[0]);
140                 exit (EXIT_FAILURE);
141         }
142
143         dcpomatic_setup ();
144
145         try {
146                 server = new ServerDescription (server_host, 1);
147                 film.reset (new Film (film_dir));
148                 film->read_metadata ();
149                 
150                 shared_ptr<Player> player = film->make_player ();
151                 player->disable_audio ();
152
153                 player->Video.connect (boost::bind (process_video, _1, _2, _3, _5));
154                 bool done = false;
155                 while (!done) {
156                         done = player->pass ();
157                 }
158         } catch (std::exception& e) {
159                 cerr << "Error: " << e.what() << "\n";
160         }
161
162         return 0;
163 }