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>
34 #include "dcp_video_frame.h"
38 using namespace boost;
40 /** Create a server description from a string of metadata returned from as_metadata().
42 * @return ServerDescription, or 0.
45 ServerDescription::create_from_metadata (string v)
48 split (b, v, is_any_of (" "));
54 return new ServerDescription (b[0], atoi (b[1].c_str ()));
57 /** @return Description of this server as text */
59 ServerDescription::as_metadata () const
62 s << _host_name << " " << _threads;
66 Server::Server (Log* log)
73 Server::process (shared_ptr<DeadlineWrapper> wrapper)
76 wrapper->read_indefinite ((uint8_t *) buffer, sizeof (buffer), 30);
77 wrapper->consume (strlen (buffer) + 1);
79 stringstream s (buffer);
83 if (command != "encode") {
93 float frames_per_second;
98 s >> in_size.width >> in_size.height
100 >> out_size.width >> out_size.height
109 PixelFormat pixel_format = (PixelFormat) pixel_format_int;
110 Scaler const * scaler = Scaler::from_id (scaler_id);
111 if (post_process == "none") {
115 shared_ptr<SimpleImage> image (new SimpleImage (pixel_format, in_size));
117 for (int i = 0; i < image->components(); ++i) {
120 image->set_line_size (i, line_size);
123 for (int i = 0; i < image->components(); ++i) {
124 wrapper->read_definite_and_consume (image->data()[i], image->line_size()[i] * image->lines(i), 30);
128 image->hash ("Image for encoding (as received by server)");
131 DCPVideoFrame dcp_video_frame (image, out_size, padding, scaler, frame, frames_per_second, post_process, colour_lut_index, j2k_bandwidth, _log);
132 shared_ptr<EncodedData> encoded = dcp_video_frame.encode_locally ();
133 encoded->send (wrapper);
136 encoded->hash ("Encoded image (as made by server and as sent back)");
143 Server::worker_thread ()
146 mutex::scoped_lock lock (_worker_mutex);
147 while (_queue.empty ()) {
148 _worker_condition.wait (lock);
151 shared_ptr<DeadlineWrapper> wrapper = _queue.front ();
158 struct timeval start;
159 gettimeofday (&start, 0);
162 frame = process (wrapper);
163 } catch (std::exception& e) {
164 cerr << "Error: " << e.what() << "\n";
173 gettimeofday (&end, 0);
175 s << "Encoded frame " << frame << " in " << (seconds (end) - seconds (start));
176 _log->log (s.str ());
179 _worker_condition.notify_all ();
184 Server::run (int num_threads)
187 s << "Server starting with " << num_threads << " threads.";
188 _log->log (s.str ());
190 for (int i = 0; i < num_threads; ++i) {
191 _worker_threads.push_back (new thread (bind (&Server::worker_thread, this)));
194 asio::io_service io_service;
195 asio::ip::tcp::acceptor acceptor (io_service, asio::ip::tcp::endpoint (asio::ip::tcp::v4(), Config::instance()->server_port ()));
197 shared_ptr<DeadlineWrapper> wrapper (new DeadlineWrapper);
198 acceptor.accept (wrapper->socket ());
200 mutex::scoped_lock lock (_worker_mutex);
202 /* Wait until the queue has gone down a bit */
203 while (int (_queue.size()) >= num_threads * 2) {
204 _worker_condition.wait (lock);
207 _queue.push_back (wrapper);
208 _worker_condition.notify_all ();