2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/server.cc
21 * @brief Class to describe a server to which we can send
22 * encoding work, and a class to implement such a server.
29 #include <boost/algorithm/string.hpp>
30 #include <boost/lexical_cast.hpp>
31 #include <boost/scoped_array.hpp>
32 #include <libcxml/cxml.h>
37 #include "dcp_video_frame.h"
44 using std::stringstream;
50 using std::setprecision;
52 using boost::shared_ptr;
53 using boost::algorithm::is_any_of;
54 using boost::algorithm::split;
57 using boost::scoped_array;
58 using boost::optional;
59 using boost::lexical_cast;
62 ServerDescription::ServerDescription (shared_ptr<const cxml::Node> node)
64 _host_name = node->string_child ("HostName");
65 _threads = node->number_child<int> ("Threads");
69 ServerDescription::as_xml (xmlpp::Node* root) const
71 root->add_child("HostName")->add_child_text (_host_name);
72 root->add_child("Threads")->add_child_text (boost::lexical_cast<string> (_threads));
75 /** Create a server description from a string of metadata returned from as_metadata().
77 * @return ServerDescription, or 0.
79 optional<ServerDescription>
80 ServerDescription::create_from_metadata (string v)
83 split (b, v, is_any_of (" "));
86 return optional<ServerDescription> ();
89 return ServerDescription (b[0], atoi (b[1].c_str ()));
92 Server::Server (shared_ptr<Log> log, bool verbose)
99 /** @param after_read Filled in with gettimeofday() after reading the input from the network.
100 * @param after_encode Filled in with gettimeofday() after encoding the image.
103 Server::process (shared_ptr<Socket> socket, struct timeval& after_read, struct timeval& after_encode)
105 uint32_t length = socket->read_uint32 ();
106 scoped_array<char> buffer (new char[length]);
107 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
109 stringstream s (buffer.get());
110 shared_ptr<cxml::Document> xml (new cxml::Document ("EncodingRequest"));
111 xml->read_stream (s);
112 if (xml->number_child<int> ("Version") != SERVER_LINK_VERSION) {
113 cerr << "Mismatched server/client versions\n";
114 _log->log ("Mismatched server/client versions");
119 xml->number_child<int> ("Width"), xml->number_child<int> ("Height")
122 shared_ptr<Image> image (new Image (PIX_FMT_RGB24, size, true));
124 image->read_from_socket (socket);
125 DCPVideoFrame dcp_video_frame (image, xml, _log);
127 gettimeofday (&after_read, 0);
129 shared_ptr<EncodedData> encoded = dcp_video_frame.encode_locally ();
131 gettimeofday (&after_encode, 0);
134 encoded->send (socket);
135 } catch (std::exception& e) {
136 _log->log (String::compose (
137 "Send failed; frame %1, data size %2, pixel format %3, image size %4x%5, %6 components",
138 dcp_video_frame.frame(), encoded->size(), image->pixel_format(), image->size().width, image->size().height, image->components()
144 return dcp_video_frame.frame ();
148 Server::worker_thread ()
151 boost::mutex::scoped_lock lock (_worker_mutex);
152 while (_queue.empty ()) {
153 _worker_condition.wait (lock);
156 shared_ptr<Socket> socket = _queue.front ();
164 struct timeval start;
165 struct timeval after_read;
166 struct timeval after_encode;
169 gettimeofday (&start, 0);
172 frame = process (socket, after_read, after_encode);
173 ip = socket->socket().remote_endpoint().address().to_string();
174 } catch (std::exception& e) {
175 _log->log (String::compose ("Error: %1", e.what()));
178 gettimeofday (&end, 0);
186 gettimeofday (&end, 0);
188 stringstream message;
189 message.precision (2);
191 << "Encoded frame " << frame << " from " << ip << ": "
192 << "receive " << (seconds(after_read) - seconds(start)) << "s "
193 << "encode " << (seconds(after_encode) - seconds(after_read)) << "s "
194 << "send " << (seconds(end) - seconds(after_encode)) << "s.";
197 cout << message.str() << "\n";
200 _log->log (message.str ());
203 _worker_condition.notify_all ();
208 Server::run (int num_threads)
210 _log->log (String::compose ("Server starting with %1 threads", num_threads));
212 cout << "DCP-o-matic server starting with " << num_threads << " threads.\n";
215 for (int i = 0; i < num_threads; ++i) {
216 _worker_threads.push_back (new thread (bind (&Server::worker_thread, this)));
219 _broadcast.thread = new thread (bind (&Server::broadcast_thread, this));
221 boost::asio::io_service io_service;
223 boost::asio::ip::tcp::acceptor acceptor (
225 boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), Config::instance()->server_port_base ())
229 shared_ptr<Socket> socket (new Socket);
230 acceptor.accept (socket->socket ());
232 boost::mutex::scoped_lock lock (_worker_mutex);
234 /* Wait until the queue has gone down a bit */
235 while (int (_queue.size()) >= num_threads * 2) {
236 _worker_condition.wait (lock);
239 _queue.push_back (socket);
240 _worker_condition.notify_all ();
245 Server::broadcast_thread ()
248 boost::asio::io_service io_service;
250 boost::asio::ip::address address = boost::asio::ip::address_v4::any ();
251 boost::asio::ip::udp::endpoint listen_endpoint (address, Config::instance()->server_port_base() + 1);
253 _broadcast.socket = new boost::asio::ip::udp::socket (io_service);
254 _broadcast.socket->open (listen_endpoint.protocol ());
255 _broadcast.socket->bind (listen_endpoint);
257 _broadcast.socket->async_receive_from (
258 boost::asio::buffer (_broadcast.buffer, sizeof (_broadcast.buffer)),
259 _broadcast.send_endpoint,
260 boost::bind (&Server::broadcast_received, this)
271 Server::broadcast_received ()
273 _broadcast.buffer[sizeof(_broadcast.buffer) - 1] = '\0';
275 if (strcmp (_broadcast.buffer, DCPOMATIC_HELLO) == 0) {
276 /* Reply to the client saying what we can do */
278 xmlpp::Element* root = doc.create_root_node ("ServerAvailable");
279 root->add_child("Threads")->add_child_text (lexical_cast<string> (_worker_threads.size ()));
281 doc.write_to_stream (xml, "UTF-8");
283 shared_ptr<Socket> socket (new Socket);
285 socket->connect (boost::asio::ip::tcp::endpoint (_broadcast.send_endpoint.address(), Config::instance()->server_port_base() + 1));
286 socket->write (xml.str().length() + 1);
287 socket->write ((uint8_t *) xml.str().c_str(), xml.str().length() + 1);
293 _broadcast.socket->async_receive_from (
294 boost::asio::buffer (_broadcast.buffer, sizeof (_broadcast.buffer)),
295 _broadcast.send_endpoint, boost::bind (&Server::broadcast_received, this)