Allow J2K encode backends to accept more than one frame at once.
[dcpomatic.git] / src / lib / j2k_encoder_remote_backend.cc
1 /*
2     Copyright (C) 2021 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
22 #include "config.h"
23 #include "cross.h"
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"
29 #include "warnings.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>
35
36
37 #include "i18n.h"
38
39
40 using std::make_shared;
41 using std::shared_ptr;
42 using std::string;
43 using std::unique_ptr;
44 using std::vector;
45 using boost::optional;
46 using dcp::raw_convert;
47
48
49 J2KEncoderRemoteBackend::J2KEncoderRemoteBackend (J2KEncoderRemoteBackend&& other)
50         : _server (other._server)
51         , _backoff (other._backoff)
52 {
53
54 }
55
56
57 vector<dcp::ArrayData>
58 J2KEncoderRemoteBackend::encode (vector<DCPVideo> const& all_video)
59 {
60         DCPOMATIC_ASSERT (all_video.size() == 1);
61         auto video = all_video.front();
62
63         try {
64                 boost::asio::io_service io_service;
65                 boost::asio::ip::tcp::resolver resolver (io_service);
66                 boost::asio::ip::tcp::resolver::query query (_server.host_name(), raw_convert<string>(ENCODE_FRAME_PORT));
67                 auto endpoint_iterator = resolver.resolve (query);
68
69                 auto socket = make_shared<Socket>(_timeout);
70
71                 socket->connect (*endpoint_iterator);
72
73                 /* Collect all XML metadata */
74                 xmlpp::Document doc;
75                 auto root = doc.create_root_node ("EncodingRequest");
76                 root->add_child("Version")->add_child_text(raw_convert<string>(SERVER_LINK_VERSION));
77                 video.add_metadata (root);
78
79                 LOG_DEBUG_ENCODE (N_("Sending frame %1 to remote"), video.index());
80
81                 {
82                         Socket::WriteDigestScope ds (socket);
83
84                         /* Send XML metadata */
85                         auto xml = doc.write_to_string ("UTF-8");
86                         socket->write (xml.length() + 1);
87                         socket->write (reinterpret_cast<uint8_t const*>(xml.c_str()), xml.bytes() + 1);
88
89                         /* Send binary data */
90                         LOG_TIMING("start-remote-send thread=%1", thread_id());
91                         video.frame()->write_to_socket(socket);
92                 }
93
94                 /* Read the response (JPEG2000-encoded data); this blocks until the data
95                    is ready and sent back.
96                 */
97                 Socket::ReadDigestScope ds (socket);
98                 LOG_TIMING("start-remote-encode thread=%1", thread_id());
99                 dcp::ArrayData enc(socket->read_uint32());
100                 LOG_TIMING("start-remote-receive thread=%1", thread_id());
101                 socket->read (enc.data(), enc.size());
102                 LOG_TIMING("finish-remote-receive thread=%1", thread_id());
103                 if (!ds.check()) {
104                         throw NetworkError ("Checksums do not match");
105                 }
106
107                 LOG_DEBUG_ENCODE (N_("Finished remotely-encoded frame %1"), video.index());
108
109                 _backoff = 0;
110                 return { enc };
111
112         } catch (std::exception& e) {
113                 if (_backoff < 60) {
114                         /* back off more */
115                         _backoff += 10;
116                 }
117                 LOG_ERROR (
118                         N_("Remote encode of %1 on %2 failed (%3); thread sleeping for %4s"),
119                         video.index(), _server.host_name(), e.what(), _backoff
120                         );
121                 return {};
122         }
123
124 }
125