Bump version
[dcpomatic.git] / src / tools / servomatictest.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 "format.h"
25 #include "film.h"
26 #include "filter.h"
27 #include "util.h"
28 #include "scaler.h"
29 #include "server.h"
30 #include "dcp_video_frame.h"
31 #include "options.h"
32 #include "decoder.h"
33 #include "exceptions.h"
34 #include "scaler.h"
35 #include "log.h"
36 #include "decoder_factory.h"
37 #include "video_decoder.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 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<Image> image, bool, shared_ptr<Subtitle> sub)
51 {
52         shared_ptr<DCPVideoFrame> local (
53                 new DCPVideoFrame (
54                         image, sub,
55                         libdcp::Size (1024, 1024), 0, 0, 0,
56                         Scaler::from_id ("bicubic"), frame, 24, "", 0, 250000000, log_)
57                 );
58         
59         shared_ptr<DCPVideoFrame> remote (
60                 new DCPVideoFrame (
61                         image, sub,
62                         libdcp::Size (1024, 1024), 0, 0, 0,
63                         Scaler::from_id ("bicubic"), frame, 24, "", 0, 250000000, log_)
64                 );
65
66         cout << "Frame " << frame << ": ";
67         cout.flush ();
68
69         ++frame;
70
71         shared_ptr<EncodedData> local_encoded = local->encode_locally ();
72         shared_ptr<EncodedData> remote_encoded;
73
74         string remote_error;
75         try {
76                 remote_encoded = remote->encode_remotely (server);
77         } catch (NetworkError& e) {
78                 remote_error = e.what ();
79         }
80
81         if (!remote_error.empty ()) {
82                 cout << "\033[0;31mnetwork problem: " << remote_error << "\033[0m\n";
83                 return;
84         }
85
86         if (local_encoded->size() != remote_encoded->size()) {
87                 cout << "\033[0;31msizes differ\033[0m\n";
88                 return;
89         }
90                 
91         uint8_t* p = local_encoded->data();
92         uint8_t* q = remote_encoded->data();
93         for (int i = 0; i < local_encoded->size(); ++i) {
94                 if (*p++ != *q++) {
95                         cout << "\033[0;31mdata differ\033[0m at byte " << i << "\n";
96                         return;
97                 }
98         }
99
100         cout << "\033[0;32mgood\033[0m\n";
101 }
102
103 static void
104 help (string n)
105 {
106         cerr << "Syntax: " << n << " [--help] --film <film> --server <host>\n";
107         exit (EXIT_FAILURE);
108 }
109
110 int
111 main (int argc, char* argv[])
112 {
113         string film_dir;
114         string server_host;
115
116         while (1) {
117                 static struct option long_options[] = {
118                         { "help", no_argument, 0, 'h'},
119                         { "server", required_argument, 0, 's'},
120                         { "film", required_argument, 0, 'f'},
121                         { 0, 0, 0, 0 }
122                 };
123
124                 int option_index = 0;
125                 int c = getopt_long (argc, argv, "hs:f:", long_options, &option_index);
126
127                 if (c == -1) {
128                         break;
129                 }
130
131                 switch (c) {
132                 case 'h':
133                         help (argv[0]);
134                         exit (EXIT_SUCCESS);
135                 case 's':
136                         server_host = optarg;
137                         break;
138                 case 'f':
139                         film_dir = optarg;
140                         break;
141                 }
142         }
143         
144         if (server_host.empty() || film_dir.empty()) {
145                 help (argv[0]);
146                 exit (EXIT_FAILURE);
147         }
148
149         dvdomatic_setup ();
150
151         server = new ServerDescription (server_host, 1);
152         shared_ptr<Film> film (new Film (film_dir, true));
153
154         DecodeOptions opt;
155         opt.decode_audio = false;
156         opt.decode_subtitles = true;
157         opt.video_sync = true;
158
159         Decoders decoders = decoder_factory (film, opt);
160         try {
161                 decoders.video->Video.connect (boost::bind (process_video, _1, _2, _3));
162                 bool done = false;
163                 while (!done) {
164                         done = decoders.video->pass ();
165                 }
166         } catch (std::exception& e) {
167                 cerr << "Error: " << e.what() << "\n";
168         }
169
170         return 0;
171 }