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