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>
36 #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;
54 /** Create a server description from a string of metadata returned from as_metadata().
56 * @return ServerDescription, or 0.
59 ServerDescription::create_from_metadata (string v)
62 split (b, v, is_any_of (N_(" ")));
68 return new ServerDescription (b[0], atoi (b[1].c_str ()));
71 /** @return Description of this server as text */
73 ServerDescription::as_metadata () const
76 s << _host_name << N_(" ") << _threads;
80 Server::Server (shared_ptr<Log> log)
87 Server::process (shared_ptr<Socket> socket)
89 uint32_t length = socket->read_uint32 ();
90 scoped_array<char> buffer (new char[length]);
91 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
93 stringstream s (buffer.get());
94 multimap<string, string> kv = read_key_value (s);
96 if (get_required_string (kv, N_("encode")) != N_("please")) {
100 libdcp::Size in_size (get_required_int (kv, N_("input_width")), get_required_int (kv, N_("input_height")));
101 int pixel_format_int = get_required_int (kv, N_("input_pixel_format"));
102 libdcp::Size out_size (get_required_int (kv, N_("output_width")), get_required_int (kv, N_("output_height")));
103 int padding = get_required_int (kv, N_("padding"));
104 int subtitle_offset = get_required_int (kv, N_("subtitle_offset"));
105 float subtitle_scale = get_required_float (kv, N_("subtitle_scale"));
106 string scaler_id = get_required_string (kv, N_("scaler"));
107 int frame = get_required_int (kv, N_("frame"));
108 int frames_per_second = get_required_int (kv, N_("frames_per_second"));
109 string post_process = get_optional_string (kv, N_("post_process"));
110 int colour_lut_index = get_required_int (kv, N_("colour_lut"));
111 int j2k_bandwidth = get_required_int (kv, N_("j2k_bandwidth"));
112 Position subtitle_position (get_optional_int (kv, N_("subtitle_x")), get_optional_int (kv, N_("subtitle_y")));
113 libdcp::Size subtitle_size (get_optional_int (kv, N_("subtitle_width")), get_optional_int (kv, N_("subtitle_height")));
115 /* This checks that colour_lut_index is within range */
116 colour_lut_index_to_name (colour_lut_index);
118 PixelFormat pixel_format = (PixelFormat) pixel_format_int;
119 Scaler const * scaler = Scaler::from_id (scaler_id);
121 shared_ptr<Image> image (new SimpleImage (pixel_format, in_size, true));
123 image->read_from_socket (socket);
125 shared_ptr<Subtitle> sub;
126 if (subtitle_size.width && subtitle_size.height) {
127 shared_ptr<Image> subtitle_image (new SimpleImage (PIX_FMT_RGBA, subtitle_size, true));
128 subtitle_image->read_from_socket (socket);
129 sub.reset (new Subtitle (subtitle_position, subtitle_image));
132 DCPVideoFrame dcp_video_frame (
133 image, sub, out_size, padding, subtitle_offset, subtitle_scale,
134 scaler, frame, frames_per_second, post_process, colour_lut_index, j2k_bandwidth, _log
137 shared_ptr<EncodedData> encoded = dcp_video_frame.encode_locally ();
139 encoded->send (socket);
140 } catch (std::exception& e) {
141 _log->log (String::compose (
142 N_("Send failed; frame %1, data size %2, pixel format %3, image size %4x%5, %6 components"),
143 frame, encoded->size(), image->pixel_format(), image->size().width, image->size().height, image->components()
153 Server::worker_thread ()
156 boost::mutex::scoped_lock lock (_worker_mutex);
157 while (_queue.empty ()) {
158 _worker_condition.wait (lock);
161 shared_ptr<Socket> socket = _queue.front ();
168 struct timeval start;
169 gettimeofday (&start, 0);
172 frame = process (socket);
173 } catch (std::exception& e) {
174 _log->log (String::compose (N_("Error: %1"), e.what()));
183 gettimeofday (&end, 0);
184 _log->log (String::compose (N_("Encoded frame %1 in %2"), frame, seconds (end) - seconds (start)));
187 _worker_condition.notify_all ();
192 Server::run (int num_threads)
194 _log->log (String::compose (N_("Server starting with %1 threads"), num_threads));
196 for (int i = 0; i < num_threads; ++i) {
197 _worker_threads.push_back (new thread (bind (&Server::worker_thread, this)));
200 boost::asio::io_service io_service;
201 boost::asio::ip::tcp::acceptor acceptor (io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), Config::instance()->server_port ()));
203 shared_ptr<Socket> socket (new Socket);
204 acceptor.accept (socket->socket ());
206 boost::mutex::scoped_lock lock (_worker_mutex);
208 /* Wait until the queue has gone down a bit */
209 while (int (_queue.size()) >= num_threads * 2) {
210 _worker_condition.wait (lock);
213 _queue.push_back (socket);
214 _worker_condition.notify_all ();