Very basics of colour conversion configuration.
[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 <boost/scoped_array.hpp>
32 #include <libcxml/cxml.h>
33 #include "server.h"
34 #include "util.h"
35 #include "scaler.h"
36 #include "image.h"
37 #include "dcp_video_frame.h"
38 #include "config.h"
39
40 #include "i18n.h"
41
42 using std::string;
43 using std::stringstream;
44 using std::multimap;
45 using std::vector;
46 using boost::shared_ptr;
47 using boost::algorithm::is_any_of;
48 using boost::algorithm::split;
49 using boost::thread;
50 using boost::bind;
51 using boost::scoped_array;
52 using libdcp::Size;
53
54 ServerDescription::ServerDescription (shared_ptr<const cxml::Node> node)
55 {
56         _host_name = node->string_child ("HostName");
57         _threads = node->number_child<int> ("Threads");
58 }
59
60 void
61 ServerDescription::as_xml (xmlpp::Node* root) const
62 {
63         root->add_child("HostName")->add_child_text (_host_name);
64         root->add_child("Threads")->add_child_text (boost::lexical_cast<string> (_threads));
65 }
66
67 /** Create a server description from a string of metadata returned from as_metadata().
68  *  @param v Metadata.
69  *  @return ServerDescription, or 0.
70  */
71 shared_ptr<ServerDescription>
72 ServerDescription::create_from_metadata (string v)
73 {
74         vector<string> b;
75         split (b, v, is_any_of (N_(" ")));
76
77         if (b.size() != 2) {
78                 return shared_ptr<ServerDescription> ();
79         }
80
81         return shared_ptr<ServerDescription> (new ServerDescription (b[0], atoi (b[1].c_str ())));
82 }
83
84 Server::Server (shared_ptr<Log> log)
85         : _log (log)
86 {
87
88 }
89
90 int
91 Server::process (shared_ptr<Socket> socket)
92 {
93         uint32_t length = socket->read_uint32 ();
94         scoped_array<char> buffer (new char[length]);
95         socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
96         
97         stringstream s (buffer.get());
98         multimap<string, string> kv = read_key_value (s);
99
100         if (get_required_string (kv, "encode") != "please") {
101                 return -1;
102         }
103
104         libdcp::Size size (get_required_int (kv, "width"), get_required_int (kv, "height"));
105         int frame = get_required_int (kv, "frame");
106         int frames_per_second = get_required_int (kv, "frames_per_second");
107         int j2k_bandwidth = get_required_int (kv, "j2k_bandwidth");
108         Eyes eyes = static_cast<Eyes> (get_required_int (kv, "eyes"));
109
110         shared_ptr<Image> image (new Image (PIX_FMT_RGB24, size, true));
111
112         image->read_from_socket (socket);
113
114         DCPVideoFrame dcp_video_frame (
115                 image, frame, eyes, frames_per_second, j2k_bandwidth, _log
116                 );
117         
118         shared_ptr<EncodedData> encoded = dcp_video_frame.encode_locally ();
119         try {
120                 encoded->send (socket);
121         } catch (std::exception& e) {
122                 _log->log (String::compose (
123                                    N_("Send failed; frame %1, data size %2, pixel format %3, image size %4x%5, %6 components"),
124                                    frame, encoded->size(), image->pixel_format(), image->size().width, image->size().height, image->components()
125                                    )
126                         );
127                 throw;
128         }
129
130         return frame;
131 }
132
133 void
134 Server::worker_thread ()
135 {
136         while (1) {
137                 boost::mutex::scoped_lock lock (_worker_mutex);
138                 while (_queue.empty ()) {
139                         _worker_condition.wait (lock);
140                 }
141
142                 shared_ptr<Socket> socket = _queue.front ();
143                 _queue.pop_front ();
144                 
145                 lock.unlock ();
146
147                 int frame = -1;
148
149                 struct timeval start;
150                 gettimeofday (&start, 0);
151                 
152                 try {
153                         frame = process (socket);
154                 } catch (std::exception& e) {
155                         _log->log (String::compose (N_("Error: %1"), e.what()));
156                 }
157                 
158                 socket.reset ();
159                 
160                 lock.lock ();
161
162                 if (frame >= 0) {
163                         struct timeval end;
164                         gettimeofday (&end, 0);
165                         _log->log (String::compose (N_("Encoded frame %1 in %2"), frame, seconds (end) - seconds (start)));
166                 }
167                 
168                 _worker_condition.notify_all ();
169         }
170 }
171
172 void
173 Server::run (int num_threads)
174 {
175         _log->log (String::compose (N_("Server starting with %1 threads"), num_threads));
176         
177         for (int i = 0; i < num_threads; ++i) {
178                 _worker_threads.push_back (new thread (bind (&Server::worker_thread, this)));
179         }
180
181         boost::asio::io_service io_service;
182         boost::asio::ip::tcp::acceptor acceptor (io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), Config::instance()->server_port ()));
183         while (1) {
184                 shared_ptr<Socket> socket (new Socket);
185                 acceptor.accept (socket->socket ());
186
187                 boost::mutex::scoped_lock lock (_worker_mutex);
188                 
189                 /* Wait until the queue has gone down a bit */
190                 while (int (_queue.size()) >= num_threads * 2) {
191                         _worker_condition.wait (lock);
192                 }
193                 
194                 _queue.push_back (socket);
195                 _worker_condition.notify_all ();
196         }
197 }