Merge branch 'master' of /home/carl/git/dvdomatic
[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 <boost/lexical_cast.hpp>
31 #include "server.h"
32 #include "util.h"
33 #include "scaler.h"
34 #include "image.h"
35 #include "dcp_video_frame.h"
36 #include "config.h"
37 #include "subtitle.h"
38
39 using namespace std;
40 using namespace boost;
41
42 /** Create a server description from a string of metadata returned from as_metadata().
43  *  @param v Metadata.
44  *  @return ServerDescription, or 0.
45  */
46 ServerDescription *
47 ServerDescription::create_from_metadata (string v)
48 {
49         vector<string> b;
50         split (b, v, is_any_of (" "));
51
52         if (b.size() != 2) {
53                 return 0;
54         }
55
56         return new ServerDescription (b[0], atoi (b[1].c_str ()));
57 }
58
59 /** @return Description of this server as text */
60 string
61 ServerDescription::as_metadata () const
62 {
63         stringstream s;
64         s << _host_name << " " << _threads;
65         return s.str ();
66 }
67
68 Server::Server (Log* log)
69         : _log (log)
70 {
71
72 }
73
74 int
75 Server::process (shared_ptr<Socket> socket)
76 {
77         char buffer[512];
78         socket->read_indefinite ((uint8_t *) buffer, sizeof (buffer), 30);
79         socket->consume (strlen (buffer) + 1);
80         
81         stringstream s (buffer);
82         multimap<string, string> kv = read_key_value (s);
83
84         if (get_required_string (kv, "encode") != "please") {
85                 return -1;
86         }
87
88         Size in_size (get_required_int (kv, "input_width"), get_required_int (kv, "input_height"));
89         int pixel_format_int = get_required_int (kv, "input_pixel_format");
90         Size out_size (get_required_int (kv, "output_width"), get_required_int (kv, "output_height"));
91         int padding = get_required_int (kv, "padding");
92         int subtitle_offset = get_required_int (kv, "subtitle_offset");
93         float subtitle_scale = get_required_float (kv, "subtitle_scale");
94         string scaler_id = get_required_string (kv, "scaler");
95         int frame = get_required_int (kv, "frame");
96         int frames_per_second = get_required_int (kv, "frames_per_second");
97         string post_process = get_optional_string (kv, "post_process");
98         int colour_lut_index = get_required_int (kv, "colour_lut");
99         int j2k_bandwidth = get_required_int (kv, "j2k_bandwidth");
100         Position subtitle_position (get_optional_int (kv, "subtitle_x"), get_optional_int (kv, "subtitle_y"));
101         Size subtitle_size (get_optional_int (kv, "subtitle_width"), get_optional_int (kv, "subtitle_height"));
102
103         /* This checks that colour_lut_index is within range */
104         colour_lut_index_to_name (colour_lut_index);
105
106         PixelFormat pixel_format = (PixelFormat) pixel_format_int;
107         Scaler const * scaler = Scaler::from_id (scaler_id);
108         
109         shared_ptr<Image> image (new AlignedImage (pixel_format, in_size));
110
111         image->read_from_socket (socket);
112
113         shared_ptr<Subtitle> sub;
114         if (subtitle_size.width && subtitle_size.height) {
115                 shared_ptr<Image> subtitle_image (new AlignedImage (PIX_FMT_RGBA, subtitle_size));
116                 subtitle_image->read_from_socket (socket);
117                 sub.reset (new Subtitle (subtitle_position, subtitle_image));
118         }
119
120         DCPVideoFrame dcp_video_frame (
121                 image, sub, out_size, padding, subtitle_offset, subtitle_scale,
122                 scaler, frame, frames_per_second, post_process, colour_lut_index, j2k_bandwidth, _log
123                 );
124         
125         shared_ptr<EncodedData> encoded = dcp_video_frame.encode_locally ();
126         encoded->send (socket);
127
128         return frame;
129 }
130
131 void
132 Server::worker_thread ()
133 {
134         while (1) {
135                 mutex::scoped_lock lock (_worker_mutex);
136                 while (_queue.empty ()) {
137                         _worker_condition.wait (lock);
138                 }
139
140                 shared_ptr<Socket> socket = _queue.front ();
141                 _queue.pop_front ();
142                 
143                 lock.unlock ();
144
145                 int frame = -1;
146
147                 struct timeval start;
148                 gettimeofday (&start, 0);
149                 
150                 try {
151                         frame = process (socket);
152                 } catch (std::exception& e) {
153                         cerr << "Error: " << e.what() << "\n";
154                 }
155                 
156                 socket.reset ();
157                 
158                 lock.lock ();
159
160                 if (frame >= 0) {
161                         struct timeval end;
162                         gettimeofday (&end, 0);
163                         _log->log (String::compose ("Encoded frame %1 in %2", frame, seconds (end) - seconds (start)));
164                 }
165                 
166                 _worker_condition.notify_all ();
167         }
168 }
169
170 void
171 Server::run (int num_threads)
172 {
173         _log->log (String::compose ("Server starting with %1 threads", num_threads));
174         
175         for (int i = 0; i < num_threads; ++i) {
176                 _worker_threads.push_back (new thread (bind (&Server::worker_thread, this)));
177         }
178
179         asio::io_service io_service;
180         asio::ip::tcp::acceptor acceptor (io_service, asio::ip::tcp::endpoint (asio::ip::tcp::v4(), Config::instance()->server_port ()));
181         while (1) {
182                 shared_ptr<Socket> socket (new Socket);
183                 acceptor.accept (socket->socket ());
184
185                 mutex::scoped_lock lock (_worker_mutex);
186                 
187                 /* Wait until the queue has gone down a bit */
188                 while (int (_queue.size()) >= num_threads * 2) {
189                         _worker_condition.wait (lock);
190                 }
191                 
192                 _queue.push_back (socket);
193                 _worker_condition.notify_all ();
194         }
195 }