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"
43 using std::stringstream;
46 using boost::shared_ptr;
47 using boost::algorithm::is_any_of;
48 using boost::algorithm::split;
51 using boost::scoped_array;
52 using boost::optional;
55 ServerDescription::ServerDescription (shared_ptr<const cxml::Node> node)
57 _host_name = node->string_child ("HostName");
58 _threads = node->number_child<int> ("Threads");
62 ServerDescription::as_xml (xmlpp::Node* root) const
64 root->add_child("HostName")->add_child_text (_host_name);
65 root->add_child("Threads")->add_child_text (boost::lexical_cast<string> (_threads));
68 /** Create a server description from a string of metadata returned from as_metadata().
70 * @return ServerDescription, or 0.
72 optional<ServerDescription>
73 ServerDescription::create_from_metadata (string v)
76 split (b, v, is_any_of (N_(" ")));
79 return optional<ServerDescription> ();
82 return ServerDescription (b[0], atoi (b[1].c_str ()));
85 Server::Server (shared_ptr<Log> log)
92 Server::process (shared_ptr<Socket> socket)
94 uint32_t length = socket->read_uint32 ();
95 scoped_array<char> buffer (new char[length]);
96 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
98 stringstream s (buffer.get());
99 shared_ptr<cxml::Document> xml (new cxml::Document ("EncodingRequest"));
100 xml->read_stream (s);
101 if (xml->number_child<int> ("Version") != SERVER_LINK_VERSION) {
102 _log->log ("Mismatched server/client versions");
107 xml->number_child<int> ("Width"), xml->number_child<int> ("Height")
110 shared_ptr<Image> image (new Image (PIX_FMT_RGB24, size, true));
112 image->read_from_socket (socket);
113 DCPVideoFrame dcp_video_frame (image, xml, _log);
115 shared_ptr<EncodedData> encoded = dcp_video_frame.encode_locally ();
117 encoded->send (socket);
118 } catch (std::exception& e) {
119 _log->log (String::compose (
120 "Send failed; frame %1, data size %2, pixel format %3, image size %4x%5, %6 components",
121 dcp_video_frame.frame(), encoded->size(), image->pixel_format(), image->size().width, image->size().height, image->components()
127 return dcp_video_frame.frame ();
131 Server::worker_thread ()
134 boost::mutex::scoped_lock lock (_worker_mutex);
135 while (_queue.empty ()) {
136 _worker_condition.wait (lock);
139 shared_ptr<Socket> socket = _queue.front ();
146 struct timeval start;
147 gettimeofday (&start, 0);
150 frame = process (socket);
151 } catch (std::exception& e) {
152 _log->log (String::compose (N_("Error: %1"), e.what()));
161 gettimeofday (&end, 0);
162 _log->log (String::compose (N_("Encoded frame %1 in %2"), frame, seconds (end) - seconds (start)));
165 _worker_condition.notify_all ();
170 Server::run (int num_threads)
172 _log->log (String::compose (N_("Server starting with %1 threads"), num_threads));
174 for (int i = 0; i < num_threads; ++i) {
175 _worker_threads.push_back (new thread (bind (&Server::worker_thread, this)));
178 boost::asio::io_service io_service;
179 boost::asio::ip::tcp::acceptor acceptor (io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), Config::instance()->server_port ()));
181 shared_ptr<Socket> socket (new Socket);
182 acceptor.accept (socket->socket ());
184 boost::mutex::scoped_lock lock (_worker_mutex);
186 /* Wait until the queue has gone down a bit */
187 while (int (_queue.size()) >= num_threads * 2) {
188 _worker_condition.wait (lock);
191 _queue.push_back (socket);
192 _worker_condition.notify_all ();