2 Copyright (C) 2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
24 #include "dcp_video.h"
25 #include "dcpomatic_log.h"
26 #include "dcpomatic_socket.h"
27 #include "j2k_encoder_remote_backend.h"
28 #include "player_video.h"
30 #include <dcp/raw_convert.h>
31 DCPOMATIC_DISABLE_WARNINGS
32 #include <libxml++/libxml++.h>
33 DCPOMATIC_ENABLE_WARNINGS
34 #include <boost/asio.hpp>
40 using std::make_shared;
43 using dcp::raw_convert;
46 vector<dcp::ArrayData>
47 J2KEncoderRemoteBackend::encode (vector<DCPVideo> const& all_video)
49 DCPOMATIC_ASSERT (all_video.size() == 1);
50 auto video = all_video.front();
53 boost::asio::io_service io_service;
54 boost::asio::ip::tcp::resolver resolver (io_service);
55 boost::asio::ip::tcp::resolver::query query (_server.host_name(), raw_convert<string>(ENCODE_FRAME_PORT));
56 auto endpoint_iterator = resolver.resolve (query);
58 auto socket = make_shared<Socket>(_timeout);
60 socket->connect (*endpoint_iterator);
62 /* Collect all XML metadata */
64 auto root = doc.create_root_node ("EncodingRequest");
65 root->add_child("Version")->add_child_text(raw_convert<string>(SERVER_LINK_VERSION));
66 video.add_metadata (root);
68 LOG_DEBUG_ENCODE (N_("Sending frame %1 to remote"), video.index());
71 Socket::WriteDigestScope ds (socket);
73 /* Send XML metadata */
74 auto xml = doc.write_to_string ("UTF-8");
75 socket->write (xml.length() + 1);
76 socket->write (reinterpret_cast<uint8_t const*>(xml.c_str()), xml.bytes() + 1);
78 /* Send binary data */
79 LOG_TIMING("start-remote-send thread=%1", thread_id());
80 video.frame()->write_to_socket(socket);
83 /* Read the response (JPEG2000-encoded data); this blocks until the data
84 is ready and sent back.
86 Socket::ReadDigestScope ds (socket);
87 LOG_TIMING("start-remote-encode thread=%1", thread_id());
88 dcp::ArrayData enc(socket->read_uint32());
89 LOG_TIMING("start-remote-receive thread=%1", thread_id());
90 socket->read (enc.data(), enc.size());
91 LOG_TIMING("finish-remote-receive thread=%1", thread_id());
93 throw NetworkError ("Checksums do not match");
96 LOG_DEBUG_ENCODE (N_("Finished remotely-encoded frame %1"), video.index());
101 } catch (std::exception& e) {
107 N_("Remote encode of %1 on %2 failed (%3); thread sleeping for %4s"),
108 video.index(), _server.host_name(), e.what(), _backoff