Hand-apply bbfb370d7de28ec1e8f307865cc6253bb5d4366e from master; quicker digest calcu...
[dcpomatic.git] / src / lib / util.h
1 /*
2     Copyright (C) 2012-2015 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/util.h
21  *  @brief Some utility functions and classes.
22  */
23
24 #ifndef DCPOMATIC_UTIL_H
25 #define DCPOMATIC_UTIL_H
26
27 #include "compose.hpp"
28 #include "types.h"
29 #include "exceptions.h"
30 #include "dcpomatic_time.h"
31 #include <dcp/util.h>
32 extern "C" {
33 #include <libavcodec/avcodec.h>
34 #include <libavfilter/avfilter.h>
35 }
36 #include <boost/shared_ptr.hpp>
37 #include <boost/asio.hpp>
38 #include <boost/optional.hpp>
39 #include <boost/filesystem.hpp>
40 #include <string>
41 #include <vector>
42
43 #undef check
44
45 /** The maximum number of audio channels that we can have in a DCP */
46 #define MAX_DCP_AUDIO_CHANNELS 12
47 /** Message broadcast to find possible encoding servers */
48 #define DCPOMATIC_HELLO "Boys, you gotta learn not to talk to nuns that way"
49 /** Number of films to keep in history */
50 #define HISTORY_SIZE 10
51 #define REPORT_PROBLEM _("Please report this problem by using Help -> Report a problem or via email to carl@dcpomatic.com")
52
53 extern std::string program_name;
54
55 class Job;
56 struct AVSubtitle;
57
58 extern std::string seconds_to_hms (int);
59 extern std::string seconds_to_approximate_hms (int);
60 extern std::string dependency_version_summary ();
61 extern double seconds (struct timeval);
62 extern void dcpomatic_setup ();
63 extern void dcpomatic_setup_gettext_i18n (std::string);
64 extern std::string md5_digest_head_tail (std::vector<boost::filesystem::path>, boost::uintmax_t size);
65 extern void ensure_ui_thread ();
66 extern std::string audio_channel_name (int);
67 extern bool valid_image_file (boost::filesystem::path);
68 extern bool valid_j2k_file (boost::filesystem::path);
69 #ifdef DCPOMATIC_WINDOWS
70 extern boost::filesystem::path mo_path ();
71 #endif
72 extern std::string tidy_for_filename (std::string);
73 extern dcp::Size fit_ratio_within (float ratio, dcp::Size, int);
74 extern int dcp_audio_frame_rate (int);
75 extern int stride_round_up (int, int const *, int);
76 extern int round_to (float n, int r);
77 extern void* wrapped_av_malloc (size_t);
78 extern ContentTimePeriod subtitle_period (AVSubtitle const &);
79 extern void set_backtrace_file (boost::filesystem::path);
80
81 /** @class Socket
82  *  @brief A class to wrap a boost::asio::ip::tcp::socket with some things
83  *  that are useful for DCP-o-matic.
84  *
85  *  This class wraps some things that I could not work out how to do with boost;
86  *  most notably, sync read/write calls with timeouts.
87  */
88 class Socket
89 {
90 public:
91         Socket (int timeout = 30);
92         ~Socket ();
93
94         /** @return Our underlying socket */
95         boost::asio::ip::tcp::socket& socket () {
96                 return _socket;
97         }
98
99         void connect (boost::asio::ip::tcp::endpoint);
100         void accept (int);
101
102         void write (uint32_t n);
103         void write (uint8_t const * data, int size);
104         
105         void read (uint8_t* data, int size);
106         uint32_t read_uint32 ();
107         
108 private:
109         void check ();
110
111         Socket (Socket const &);
112
113         boost::asio::io_service _io_service;
114         boost::asio::deadline_timer _deadline;
115         boost::asio::ip::tcp::socket _socket;
116         boost::asio::ip::tcp::acceptor* _acceptor;
117         int _timeout;
118 };
119
120 extern int64_t video_frames_to_audio_frames (VideoFrame v, float audio_sample_rate, float frames_per_second);
121
122 #endif
123