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