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>
35 #include "dcp_video_frame.h"
40 using std::stringstream;
43 using boost::shared_ptr;
44 using boost::algorithm::is_any_of;
45 using boost::algorithm::split;
50 /** Create a server description from a string of metadata returned from as_metadata().
52 * @return ServerDescription, or 0.
55 ServerDescription::create_from_metadata (string v)
58 split (b, v, is_any_of (" "));
64 return new ServerDescription (b[0], atoi (b[1].c_str ()));
67 /** @return Description of this server as text */
69 ServerDescription::as_metadata () const
72 s << _host_name << " " << _threads;
76 Server::Server (Log* log)
83 Server::process (shared_ptr<Socket> socket)
86 socket->read_indefinite ((uint8_t *) buffer, sizeof (buffer), 30);
87 socket->consume (strlen (buffer) + 1);
89 stringstream s (buffer);
90 multimap<string, string> kv = read_key_value (s);
92 if (get_required_string (kv, "encode") != "please") {
96 libdcp::Size in_size (get_required_int (kv, "input_width"), get_required_int (kv, "input_height"));
97 int pixel_format_int = get_required_int (kv, "input_pixel_format");
98 libdcp::Size out_size (get_required_int (kv, "output_width"), get_required_int (kv, "output_height"));
99 int padding = get_required_int (kv, "padding");
100 int subtitle_offset = get_required_int (kv, "subtitle_offset");
101 float subtitle_scale = get_required_float (kv, "subtitle_scale");
102 string scaler_id = get_required_string (kv, "scaler");
103 int frame = get_required_int (kv, "frame");
104 int frames_per_second = get_required_int (kv, "frames_per_second");
105 string post_process = get_optional_string (kv, "post_process");
106 int colour_lut_index = get_required_int (kv, "colour_lut");
107 int j2k_bandwidth = get_required_int (kv, "j2k_bandwidth");
108 Position subtitle_position (get_optional_int (kv, "subtitle_x"), get_optional_int (kv, "subtitle_y"));
109 libdcp::Size subtitle_size (get_optional_int (kv, "subtitle_width"), get_optional_int (kv, "subtitle_height"));
111 /* This checks that colour_lut_index is within range */
112 colour_lut_index_to_name (colour_lut_index);
114 PixelFormat pixel_format = (PixelFormat) pixel_format_int;
115 Scaler const * scaler = Scaler::from_id (scaler_id);
117 shared_ptr<Image> image (new SimpleImage (pixel_format, in_size, true));
119 image->read_from_socket (socket);
121 shared_ptr<Subtitle> sub;
122 if (subtitle_size.width && subtitle_size.height) {
123 shared_ptr<Image> subtitle_image (new SimpleImage (PIX_FMT_RGBA, subtitle_size, true));
124 subtitle_image->read_from_socket (socket);
125 sub.reset (new Subtitle (subtitle_position, subtitle_image));
128 DCPVideoFrame dcp_video_frame (
129 image, sub, out_size, padding, subtitle_offset, subtitle_scale,
130 scaler, frame, frames_per_second, post_process, colour_lut_index, j2k_bandwidth, _log
133 shared_ptr<EncodedData> encoded = dcp_video_frame.encode_locally ();
135 encoded->send (socket);
136 } catch (std::exception& e) {
137 _log->log (String::compose (
138 "Send failed; frame %1, data size %2, pixel format %3, image size %4x%5, %6 components",
139 frame, encoded->size(), image->pixel_format(), image->size().width, image->size().height, image->components()
149 Server::worker_thread ()
152 boost::mutex::scoped_lock lock (_worker_mutex);
153 while (_queue.empty ()) {
154 _worker_condition.wait (lock);
157 shared_ptr<Socket> socket = _queue.front ();
164 struct timeval start;
165 gettimeofday (&start, 0);
168 frame = process (socket);
169 } catch (std::exception& e) {
170 _log->log (String::compose ("Error: %1", e.what()));
179 gettimeofday (&end, 0);
180 _log->log (String::compose ("Encoded frame %1 in %2", frame, seconds (end) - seconds (start)));
183 _worker_condition.notify_all ();
188 Server::run (int num_threads)
190 _log->log (String::compose ("Server starting with %1 threads", num_threads));
192 for (int i = 0; i < num_threads; ++i) {
193 _worker_threads.push_back (new thread (bind (&Server::worker_thread, this)));
196 boost::asio::io_service io_service;
197 boost::asio::ip::tcp::acceptor acceptor (io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), Config::instance()->server_port ()));
199 shared_ptr<Socket> socket (new Socket);
200 acceptor.accept (socket->socket ());
202 boost::mutex::scoped_lock lock (_worker_mutex);
204 /* Wait until the queue has gone down a bit */
205 while (int (_queue.size()) >= num_threads * 2) {
206 _worker_condition.wait (lock);
209 _queue.push_back (socket);
210 _worker_condition.notify_all ();