Try to make client/server communications a little more robust.
[dcpomatic.git] / src / lib / util.h
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3     Copyright (C) 2000-2007 Paul Davis
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 /** @file src/util.h
22  *  @brief Some utility functions and classes.
23  */
24
25 #ifndef DVDOMATIC_UTIL_H
26 #define DVDOMATIC_UTIL_H
27
28 #include <string>
29 #include <vector>
30 #include <boost/shared_ptr.hpp>
31 #include <boost/asio.hpp>
32 extern "C" {
33 #include <libavcodec/avcodec.h>
34 #include <libavfilter/avfilter.h>
35 }
36 #include "compose.hpp"
37
38 #ifdef DVDOMATIC_DEBUG
39 #define TIMING(...) _log->microsecond_log (String::compose (__VA_ARGS__), Log::TIMING);
40 #else
41 #define TIMING(...)
42 #endif
43
44 class Scaler;
45
46 extern std::string seconds_to_hms (int);
47 extern std::string seconds_to_approximate_hms (int);
48 extern void stacktrace (std::ostream &, int);
49 extern std::string audio_sample_format_to_string (AVSampleFormat);
50 extern AVSampleFormat audio_sample_format_from_string (std::string);
51 extern std::string dependency_version_summary ();
52 extern double seconds (struct timeval);
53 extern void dvdomatic_setup ();
54 extern std::vector<std::string> split_at_spaces_considering_quotes (std::string);
55 extern std::string md5_digest (std::string);
56 extern std::string md5_digest (void const *, int);
57
58 enum ContentType {
59         STILL,
60         VIDEO
61 };
62
63 /** @class Size
64  *  @brief Representation of the size of something */
65 struct Size
66 {
67         /** Construct a zero Size */
68         Size ()
69                 : width (0)
70                 , height (0)
71         {}
72
73         /** @param w Width.
74          *  @param h Height.
75          */
76         Size (int w, int h)
77                 : width (w)
78                 , height (h)
79         {}
80
81         /** width */
82         int width;
83         /** height */
84         int height;
85 };
86
87 /** A description of the crop of an image or video. */
88 struct Crop
89 {
90         Crop () : left (0), right (0), top (0), bottom (0) {}
91
92         /** Number of pixels to remove from the left-hand side */
93         int left;
94         /** Number of pixels to remove from the right-hand side */
95         int right;
96         /** Number of pixels to remove from the top */
97         int top;
98         /** Number of pixels to remove from the bottom */
99         int bottom;
100 };
101
102 extern bool operator== (Crop const & a, Crop const & b);
103 extern bool operator!= (Crop const & a, Crop const & b);
104
105 /** A position */
106 struct Position
107 {
108         Position ()
109                 : x (0)
110                 , y (0)
111         {}
112
113         Position (int x_, int y_)
114                 : x (x_)
115                 , y (y_)
116         {}
117
118         /** x coordinate */
119         int x;
120         /** y coordinate */
121         int y;
122 };
123
124 /** A rectangle */
125 struct Rectangle
126 {
127         Rectangle ()
128                 : x (0)
129                 , y (0)
130                 , width (0)
131                 , height (0)
132         {}
133
134         Rectangle (int x_, int y_, int w_, int h_)
135                 : x (x_)
136                 , y (y_)
137                 , width (w_)
138                 , height (h_)
139         {}
140
141         int x;
142         int y;
143         int width;
144         int height;
145
146         Position position () const {
147                 return Position (x, y);
148         }
149
150         Size size () const {
151                 return Size (width, height);
152         }
153
154         Rectangle intersection (Rectangle const & other) const;
155 };
156
157 extern std::string crop_string (Position, Size);
158 extern int dcp_audio_sample_rate (int);
159 extern std::string colour_lut_index_to_name (int index);
160 extern int round_up (int, int);
161 extern std::multimap<std::string, std::string> read_key_value (std::istream& s);
162 extern int get_required_int (std::multimap<std::string, std::string> const & kv, std::string k);
163 extern std::string get_required_string (std::multimap<std::string, std::string> const & kv, std::string k);
164 extern int get_optional_int (std::multimap<std::string, std::string> const & kv, std::string k);
165 extern std::string get_optional_string (std::multimap<std::string, std::string> const & kv, std::string k);
166
167 /** @class Socket
168  *  @brief A class to wrap a boost::asio::ip::tcp::socket with some things
169  *  that are useful for DVD-o-matic.
170  *
171  *  This class wraps some things that I could not work out how to do with boost;
172  *  most notably, sync read/write calls with timeouts, and the ability to peak into
173  *  data being read.
174  */
175 class Socket
176 {
177 public:
178         Socket ();
179
180         /** @return Our underlying socket */
181         boost::asio::ip::tcp::socket& socket () {
182                 return _socket;
183         }
184
185         void connect (boost::asio::ip::basic_resolver_entry<boost::asio::ip::tcp> const & endpoint, int timeout);
186         void write (uint8_t const * data, int size, int timeout);
187         
188         void read_definite_and_consume (uint8_t* data, int size, int timeout);
189         void read_indefinite (uint8_t* data, int size, int timeout);
190         void consume (int amount);
191         
192 private:
193         void check ();
194         int read (uint8_t* data, int size, int timeout);
195
196         Socket (Socket const &);
197
198         boost::asio::io_service _io_service;
199         boost::asio::deadline_timer _deadline;
200         boost::asio::ip::tcp::socket _socket;
201         /** a buffer for small reads */
202         uint8_t _buffer[1024];
203         /** amount of valid data in the buffer */
204         int _buffer_data;
205 };
206
207 #endif
208