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.
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <boost/algorithm/string.hpp>
31 #include <boost/thread.hpp>
32 #include <boost/thread/mutex.hpp>
33 #include <boost/thread/condition.hpp>
35 #include "dcp_video_frame.h"
36 #include "exceptions.h"
46 using namespace boost;
48 static vector<thread *> worker_threads;
50 static std::list<int> queue;
51 static mutex worker_mutex;
52 static condition worker_condition;
53 static Log log_ ("servomatic.log");
58 SocketReader reader (fd);
61 reader.read_indefinite ((uint8_t *) buffer, sizeof (buffer));
62 reader.consume (strlen (buffer) + 1);
64 stringstream s (buffer);
68 if (command != "encode") {
79 float frames_per_second;
84 s >> in_size.width >> in_size.height
86 >> out_size.width >> out_size.height
95 PixelFormat pixel_format = (PixelFormat) pixel_format_int;
96 Scaler const * scaler = Scaler::from_id (scaler_id);
97 if (post_process == "none") {
101 shared_ptr<SimpleImage> image (new SimpleImage (pixel_format, in_size));
103 for (int i = 0; i < image->components(); ++i) {
106 image->set_line_size (i, line_size);
109 for (int i = 0; i < image->components(); ++i) {
110 reader.read_definite_and_consume (image->data()[i], image->line_size()[i] * image->lines(i));
114 image->hash ("Image for encoding (as received by server)");
117 DCPVideoFrame dcp_video_frame (image, out_size, padding, scaler, frame, frames_per_second, post_process, colour_lut_index, j2k_bandwidth, &log_);
118 shared_ptr<EncodedData> encoded = dcp_video_frame.encode_locally ();
122 encoded->hash ("Encoded image (as made by server and as sent back)");
133 mutex::scoped_lock lock (worker_mutex);
134 while (queue.empty ()) {
135 worker_condition.wait (lock);
138 int fd = queue.front ();
145 struct timeval start;
146 gettimeofday (&start, 0);
149 frame = process (fd);
150 } catch (std::exception& e) {
151 cerr << "Error: " << e.what() << "\n";
160 gettimeofday (&end, 0);
161 cout << "Encoded frame " << frame << " in " << (seconds (end) - seconds (start)) << "\n";
164 worker_condition.notify_all ();
171 Scaler::setup_scalers ();
173 int const num_threads = Config::instance()->num_local_encoding_threads ();
175 for (int i = 0; i < num_threads; ++i) {
176 worker_threads.push_back (new thread (worker_thread));
179 int fd = socket (AF_INET, SOCK_STREAM, 0);
181 throw NetworkError ("could not open socket");
185 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &o, sizeof (o));
190 setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, (void *) &tv, sizeof (tv));
191 setsockopt (fd, SOL_SOCKET, SO_SNDTIMEO, (void *) &tv, sizeof (tv));
193 struct sockaddr_in server_address;
194 memset (&server_address, 0, sizeof (server_address));
195 server_address.sin_family = AF_INET;
196 server_address.sin_addr.s_addr = INADDR_ANY;
197 server_address.sin_port = htons (Config::instance()->server_port ());
198 if (::bind (fd, (struct sockaddr *) &server_address, sizeof (server_address)) < 0) {
200 s << "could not bind to port " << Config::instance()->server_port() << " (" << strerror (errno) << ")";
201 throw NetworkError (s.str());
204 listen (fd, BACKLOG);
207 struct sockaddr_in client_address;
208 socklen_t client_length = sizeof (client_address);
209 int new_fd = accept (fd, (struct sockaddr *) &client_address, &client_length);
211 if (errno != EAGAIN && errno != EWOULDBLOCK) {
212 throw NetworkError ("accept failed");
218 mutex::scoped_lock lock (worker_mutex);
220 /* Wait until the queue has gone down a bit */
221 while (int (queue.size()) >= num_threads * 2) {
222 worker_condition.wait (lock);
228 setsockopt (new_fd, SOL_SOCKET, SO_RCVTIMEO, (void *) &tv, sizeof (tv));
229 setsockopt (new_fd, SOL_SOCKET, SO_SNDTIMEO, (void *) &tv, sizeof (tv));
231 queue.push_back (new_fd);
232 worker_condition.notify_all ();