Use log rather the cout in server.
[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<asio::ip::tcp::socket> socket)
74 {
75         DeadlineWrapper wrapper (_io_service);
76         wrapper.set_socket (socket);
77         
78         char buffer[128];
79         wrapper.read_indefinite ((uint8_t *) buffer, sizeof (buffer), 30);
80         wrapper.consume (strlen (buffer) + 1);
81         
82         stringstream s (buffer);
83         
84         string command;
85         s >> command;
86         if (command != "encode") {
87                 return -1;
88         }
89         
90         Size in_size;
91         int pixel_format_int;
92         Size out_size;
93         int padding;
94         string scaler_id;
95         int frame;
96         float frames_per_second;
97         string post_process;
98         int colour_lut_index;
99         int j2k_bandwidth;
100         
101         s >> in_size.width >> in_size.height
102           >> pixel_format_int
103           >> out_size.width >> out_size.height
104           >> padding
105           >> scaler_id
106           >> frame
107           >> frames_per_second
108           >> post_process
109           >> colour_lut_index
110           >> j2k_bandwidth;
111         
112         PixelFormat pixel_format = (PixelFormat) pixel_format_int;
113         Scaler const * scaler = Scaler::from_id (scaler_id);
114         if (post_process == "none") {
115                 post_process = "";
116         }
117         
118         shared_ptr<SimpleImage> image (new SimpleImage (pixel_format, in_size));
119         
120         for (int i = 0; i < image->components(); ++i) {
121                 int line_size;
122                 s >> line_size;
123                 image->set_line_size (i, line_size);
124         }
125         
126         for (int i = 0; i < image->components(); ++i) {
127                 wrapper.read_definite_and_consume (image->data()[i], image->line_size()[i] * image->lines(i), 30);
128         }
129         
130 #ifdef DEBUG_HASH
131         image->hash ("Image for encoding (as received by server)");
132 #endif          
133         
134         DCPVideoFrame dcp_video_frame (image, out_size, padding, scaler, frame, frames_per_second, post_process, colour_lut_index, j2k_bandwidth, _log);
135         shared_ptr<EncodedData> encoded = dcp_video_frame.encode_locally ();
136         encoded->send (socket);
137
138 #ifdef DEBUG_HASH
139         encoded->hash ("Encoded image (as made by server and as sent back)");
140 #endif          
141         
142         return frame;
143 }
144
145 void
146 Server::worker_thread ()
147 {
148         while (1) {
149                 mutex::scoped_lock lock (_worker_mutex);
150                 while (_queue.empty ()) {
151                         _worker_condition.wait (lock);
152                 }
153
154                 shared_ptr<asio::ip::tcp::socket> socket = _queue.front ();
155                 _queue.pop_front ();
156                 
157                 lock.unlock ();
158
159                 int frame = -1;
160
161                 struct timeval start;
162                 gettimeofday (&start, 0);
163                 
164                 try {
165                         frame = process (socket);
166                 } catch (std::exception& e) {
167                         cerr << "Error: " << e.what() << "\n";
168                 }
169                 
170                 socket.reset ();
171                 
172                 lock.lock ();
173
174                 if (frame >= 0) {
175                         struct timeval end;
176                         gettimeofday (&end, 0);
177                         stringstream s;
178                         s << "Encoded frame " << frame << " in " << (seconds (end) - seconds (start));
179                         _log->log (s.str ());
180                 }
181                 
182                 _worker_condition.notify_all ();
183         }
184 }
185
186 void
187 Server::run (int num_threads)
188 {
189         stringstream s;
190         s << "Server starting with " << num_threads << " threads.";
191         _log->log (s.str ());
192         
193         for (int i = 0; i < num_threads; ++i) {
194                 _worker_threads.push_back (new thread (bind (&Server::worker_thread, this)));
195         }
196         
197         asio::ip::tcp::acceptor acceptor (_io_service, asio::ip::tcp::endpoint (asio::ip::tcp::v4(), Config::instance()->server_port ()));
198         while (1) {
199                 shared_ptr<asio::ip::tcp::socket> socket (new asio::ip::tcp::socket (_io_service));
200                 acceptor.accept (*socket);
201
202                 mutex::scoped_lock lock (_worker_mutex);
203                 
204                 /* Wait until the queue has gone down a bit */
205                 while (int (_queue.size()) >= num_threads * 2) {
206                         _worker_condition.wait (lock);
207                 }
208                 
209                 _queue.push_back (socket);
210                 _worker_condition.notify_all ();
211         }
212 }