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.
28 #include <boost/algorithm/string.hpp>
33 #include "dcp_video_frame.h"
37 using namespace boost;
39 /** Create a server description from a string of metadata returned from as_metadata().
41 * @return ServerDescription, or 0.
44 ServerDescription::create_from_metadata (string v)
47 split (b, v, is_any_of (" "));
53 return new ServerDescription (b[0], atoi (b[1].c_str ()));
56 /** @return Description of this server as text */
58 ServerDescription::as_metadata () const
61 s << _host_name << " " << _threads;
66 : _log ("servomatic.log")
72 Server::process (shared_ptr<asio::ip::tcp::socket> socket)
74 SocketReader reader (socket);
77 reader.read_indefinite ((uint8_t *) buffer, sizeof (buffer));
78 reader.consume (strlen (buffer) + 1);
80 stringstream s (buffer);
84 if (command != "encode") {
94 float frames_per_second;
99 s >> in_size.width >> in_size.height
101 >> out_size.width >> out_size.height
110 PixelFormat pixel_format = (PixelFormat) pixel_format_int;
111 Scaler const * scaler = Scaler::from_id (scaler_id);
112 if (post_process == "none") {
116 shared_ptr<SimpleImage> image (new SimpleImage (pixel_format, in_size));
118 for (int i = 0; i < image->components(); ++i) {
121 image->set_line_size (i, line_size);
124 for (int i = 0; i < image->components(); ++i) {
125 reader.read_definite_and_consume (image->data()[i], image->line_size()[i] * image->lines(i));
129 image->hash ("Image for encoding (as received by server)");
132 DCPVideoFrame dcp_video_frame (image, out_size, padding, scaler, frame, frames_per_second, post_process, colour_lut_index, j2k_bandwidth, &_log);
133 shared_ptr<EncodedData> encoded = dcp_video_frame.encode_locally ();
134 encoded->send (socket);
137 encoded->hash ("Encoded image (as made by server and as sent back)");
144 Server::worker_thread ()
147 mutex::scoped_lock lock (_worker_mutex);
148 while (_queue.empty ()) {
149 _worker_condition.wait (lock);
152 shared_ptr<asio::ip::tcp::socket> socket = _queue.front ();
159 struct timeval start;
160 gettimeofday (&start, 0);
163 frame = process (socket);
164 } catch (std::exception& e) {
165 cerr << "Error: " << e.what() << "\n";
174 gettimeofday (&end, 0);
175 cout << "Encoded frame " << frame << " in " << (seconds (end) - seconds (start)) << "\n";
178 _worker_condition.notify_all ();
185 int const num_threads = Config::instance()->num_local_encoding_threads ();
187 for (int i = 0; i < num_threads; ++i) {
188 _worker_threads.push_back (new thread (bind (&Server::worker_thread, this)));
191 asio::io_service io_service;
192 asio::ip::tcp::acceptor acceptor (io_service, asio::ip::tcp::endpoint (asio::ip::tcp::v4(), Config::instance()->server_port ()));
194 shared_ptr<asio::ip::tcp::socket> socket (new asio::ip::tcp::socket (io_service));
195 acceptor.accept (*socket);
197 mutex::scoped_lock lock (_worker_mutex);
199 /* Wait until the queue has gone down a bit */
200 while (int (_queue.size()) >= num_threads * 2) {
201 _worker_condition.wait (lock);
204 _queue.push_back (socket);
205 _worker_condition.notify_all ();