28236e3e04804e39ccab9b5167e7d2de7101f193
[dcpomatic.git] / src / lib / server.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
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.
23  */
24
25 #include <string>
26 #include <vector>
27 #include <sstream>
28 #include <iostream>
29 #include <boost/algorithm/string.hpp>
30 #include "server.h"
31 #include "util.h"
32 #include "scaler.h"
33 #include "image.h"
34 #include "dcp_video_frame.h"
35 #include "config.h"
36
37 using namespace std;
38 using namespace boost;
39
40 /** Create a server description from a string of metadata returned from as_metadata().
41  *  @param v Metadata.
42  *  @return ServerDescription, or 0.
43  */
44 ServerDescription *
45 ServerDescription::create_from_metadata (string v)
46 {
47         vector<string> b;
48         split (b, v, is_any_of (" "));
49
50         if (b.size() != 2) {
51                 return 0;
52         }
53
54         return new ServerDescription (b[0], atoi (b[1].c_str ()));
55 }
56
57 /** @return Description of this server as text */
58 string
59 ServerDescription::as_metadata () const
60 {
61         stringstream s;
62         s << _host_name << " " << _threads;
63         return s.str ();
64 }
65
66 Server::Server (Log* log)
67         : _log (log)
68 {
69
70 }
71
72 int
73 Server::process (shared_ptr<Socket> socket)
74 {
75         char buffer[128];
76         socket->read_indefinite ((uint8_t *) buffer, sizeof (buffer), 30);
77         socket->consume (strlen (buffer) + 1);
78         
79         stringstream s (buffer);
80         
81         string command;
82         s >> command;
83         if (command != "encode") {
84                 return -1;
85         }
86         
87         Size in_size;
88         int pixel_format_int;
89         Size out_size;
90         int padding;
91         string scaler_id;
92         int frame;
93         float frames_per_second;
94         string post_process;
95         int colour_lut_index;
96         int j2k_bandwidth;
97         
98         s >> in_size.width >> in_size.height
99           >> pixel_format_int
100           >> out_size.width >> out_size.height
101           >> padding
102           >> scaler_id
103           >> frame
104           >> frames_per_second
105           >> post_process
106           >> colour_lut_index
107           >> j2k_bandwidth;
108         
109         PixelFormat pixel_format = (PixelFormat) pixel_format_int;
110         Scaler const * scaler = Scaler::from_id (scaler_id);
111         if (post_process == "none") {
112                 post_process = "";
113         }
114         
115         shared_ptr<SimpleImage> image (new SimpleImage (pixel_format, in_size));
116         
117         for (int i = 0; i < image->components(); ++i) {
118                 int line_size;
119                 s >> line_size;
120                 image->set_line_size (i, line_size);
121         }
122         
123         for (int i = 0; i < image->components(); ++i) {
124                 socket->read_definite_and_consume (image->data()[i], image->line_size()[i] * image->lines(i), 30);
125         }
126         
127         DCPVideoFrame dcp_video_frame (image, out_size, padding, scaler, frame, frames_per_second, post_process, colour_lut_index, j2k_bandwidth, _log);
128         shared_ptr<EncodedData> encoded = dcp_video_frame.encode_locally ();
129         encoded->send (socket);
130         
131         return frame;
132 }
133
134 void
135 Server::worker_thread ()
136 {
137         while (1) {
138                 mutex::scoped_lock lock (_worker_mutex);
139                 while (_queue.empty ()) {
140                         _worker_condition.wait (lock);
141                 }
142
143                 shared_ptr<Socket> socket = _queue.front ();
144                 _queue.pop_front ();
145                 
146                 lock.unlock ();
147
148                 int frame = -1;
149
150                 struct timeval start;
151                 gettimeofday (&start, 0);
152                 
153                 try {
154                         frame = process (socket);
155                 } catch (std::exception& e) {
156                         cerr << "Error: " << e.what() << "\n";
157                 }
158                 
159                 socket.reset ();
160                 
161                 lock.lock ();
162
163                 if (frame >= 0) {
164                         struct timeval end;
165                         gettimeofday (&end, 0);
166                         _log->log (String::compose ("Encoded frame %1 in %2", frame, seconds (end) - seconds (start)));
167                 }
168                 
169                 _worker_condition.notify_all ();
170         }
171 }
172
173 void
174 Server::run (int num_threads)
175 {
176         _log->log (String::compose ("Server starting with %1 threads", num_threads));
177         
178         for (int i = 0; i < num_threads; ++i) {
179                 _worker_threads.push_back (new thread (bind (&Server::worker_thread, this)));
180         }
181
182         asio::io_service io_service;
183         asio::ip::tcp::acceptor acceptor (io_service, asio::ip::tcp::endpoint (asio::ip::tcp::v4(), Config::instance()->server_port ()));
184         while (1) {
185                 shared_ptr<Socket> socket (new Socket);
186                 acceptor.accept (socket->socket ());
187
188                 mutex::scoped_lock lock (_worker_mutex);
189                 
190                 /* Wait until the queue has gone down a bit */
191                 while (int (_queue.size()) >= num_threads * 2) {
192                         _worker_condition.wait (lock);
193                 }
194                 
195                 _queue.push_back (socket);
196                 _worker_condition.notify_all ();
197         }
198 }