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