Supporters update.
[dcpomatic.git] / src / lib / content.h
1 /*
2     Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 /** @file  src/lib/content.h
23  *  @brief Content class.
24  */
25
26
27 #ifndef DCPOMATIC_CONTENT_H
28 #define DCPOMATIC_CONTENT_H
29
30
31 #include "change_signaller.h"
32 #include "dcpomatic_time.h"
33 #include "signaller.h"
34 #include "types.h"
35 #include "user_property.h"
36 #include <libcxml/cxml.h>
37 #include <boost/filesystem.hpp>
38 #include <boost/signals2.hpp>
39 #include <boost/thread/mutex.hpp>
40
41
42 namespace xmlpp {
43         class Node;
44 }
45
46 namespace cxml {
47         class Node;
48 }
49
50 class Job;
51 class Film;
52 class AtmosContent;
53
54 class ContentProperty
55 {
56 public:
57         static int const PATH;
58         static int const POSITION;
59         static int const LENGTH;
60         static int const TRIM_START;
61         static int const TRIM_END;
62         static int const VIDEO_FRAME_RATE;
63 };
64
65
66 /** @class Content
67  *  @brief A piece of content represented by one or more files on disk.
68  */
69 class Content : public std::enable_shared_from_this<Content>, public Signaller
70 {
71 public:
72         explicit Content ();
73         Content (dcpomatic::DCPTime);
74         Content (boost::filesystem::path);
75         Content (cxml::ConstNodePtr);
76         Content (std::vector<std::shared_ptr<Content>>);
77         virtual ~Content () {}
78
79         Content (Content const&) = delete;
80         Content& operator= (Content const&) = delete;
81
82         /** Examine the content to establish digest, frame rates and any other
83          *  useful metadata.
84          *  @param job Job to use to report progress, or 0.
85          */
86         virtual void examine (std::shared_ptr<const Film> film, std::shared_ptr<Job> job);
87
88         virtual void take_settings_from (std::shared_ptr<const Content> c);
89
90         /** @return Quick one-line summary of the content, as will be presented in the
91          *  film editor.
92          */
93         virtual std::string summary () const = 0;
94
95         /** @return Technical details of this content; these are written to logs to
96          *  help with debugging.
97          */
98         virtual std::string technical_summary () const;
99
100         virtual void as_xml (xmlpp::Node *, bool with_paths) const;
101         virtual dcpomatic::DCPTime full_length (std::shared_ptr<const Film>) const = 0;
102         virtual dcpomatic::DCPTime approximate_length () const = 0;
103         virtual std::string identifier () const;
104         /** @return points at which to split this content when
105          *  REELTYPE_BY_VIDEO_CONTENT is in use.
106          */
107         virtual std::list<dcpomatic::DCPTime> reel_split_points (std::shared_ptr<const Film>) const;
108
109         std::shared_ptr<Content> clone () const;
110
111         void set_paths (std::vector<boost::filesystem::path> paths);
112
113         std::string path_summary () const;
114
115         std::vector<boost::filesystem::path> paths () const {
116                 boost::mutex::scoped_lock lm (_mutex);
117                 return _paths;
118         }
119
120         size_t number_of_paths () const {
121                 boost::mutex::scoped_lock lm (_mutex);
122                 return _paths.size ();
123         }
124
125         boost::filesystem::path path (size_t i) const {
126                 boost::mutex::scoped_lock lm (_mutex);
127                 return _paths[i];
128         }
129
130         std::time_t last_write_time (size_t i) const {
131                 boost::mutex::scoped_lock lm (_mutex);
132                 return _last_write_times[i];
133         }
134
135         bool paths_valid () const;
136
137         /** @return Digest of the content's file(s).  Note: this is
138          *  not a complete MD5-or-whatever hash, but a sort of poor
139          *  man's version (see comments in examine()).
140          */
141         std::string digest () const {
142                 boost::mutex::scoped_lock lm (_mutex);
143                 return _digest;
144         }
145
146         void set_position (std::shared_ptr<const Film> film, dcpomatic::DCPTime, bool force_emit = false);
147
148         /** dcpomatic::DCPTime that this content starts; i.e. the time that the first
149          *  bit of the content (trimmed or not) will happen.
150          */
151         dcpomatic::DCPTime position () const {
152                 boost::mutex::scoped_lock lm (_mutex);
153                 return _position;
154         }
155
156         void set_trim_start (dcpomatic::ContentTime);
157
158         dcpomatic::ContentTime trim_start () const {
159                 boost::mutex::scoped_lock lm (_mutex);
160                 return _trim_start;
161         }
162
163         void set_trim_end (dcpomatic::ContentTime);
164
165         dcpomatic::ContentTime trim_end () const {
166                 boost::mutex::scoped_lock lm (_mutex);
167                 return _trim_end;
168         }
169
170         /** @return Time immediately after the last thing in this content */
171         dcpomatic::DCPTime end (std::shared_ptr<const Film> film) const {
172                 return position() + length_after_trim(film);
173         }
174
175         dcpomatic::DCPTime length_after_trim (std::shared_ptr<const Film> film) const;
176
177         boost::optional<double> video_frame_rate () const {
178                 boost::mutex::scoped_lock lm (_mutex);
179                 return _video_frame_rate;
180         }
181
182         void set_video_frame_rate (double r);
183         void unset_video_frame_rate ();
184
185         double active_video_frame_rate (std::shared_ptr<const Film> film) const;
186
187         void set_change_signals_frequent (bool f) {
188                 _change_signals_frequent = f;
189         }
190
191         std::list<UserProperty> user_properties (std::shared_ptr<const Film> film) const;
192
193         std::string calculate_digest () const;
194
195         virtual bool can_be_played () const {
196                 return true;
197         }
198
199         /* ChangeType::PENDING and ChangeType::CANCELLED may be emitted from any thread; ChangeType::DONE always from GUI thread */
200         boost::signals2::signal<void (ChangeType, std::weak_ptr<Content>, int, bool)> Change;
201
202         std::shared_ptr<VideoContent> video;
203         std::shared_ptr<AudioContent> audio;
204         std::list<std::shared_ptr<TextContent>> text;
205         std::shared_ptr<AtmosContent> atmos;
206
207         std::shared_ptr<TextContent> only_text () const;
208         std::shared_ptr<TextContent> text_of_original_type (TextType type) const;
209
210         /** @return true if this content has changed since it was last examined */
211         bool changed () const;
212
213 protected:
214
215         virtual void add_properties (std::shared_ptr<const Film> film, std::list<UserProperty> &) const;
216
217         /** _mutex which should be used to protect accesses, as examine
218          *  jobs can update content state in threads other than the main one.
219          */
220         mutable boost::mutex _mutex;
221
222         void add_path (boost::filesystem::path p);
223
224 private:
225         friend struct ffmpeg_pts_offset_test;
226         friend struct best_dcp_frame_rate_test_single;
227         friend struct best_dcp_frame_rate_test_double;
228         friend struct audio_sampling_rate_test;
229         friend struct subtitle_font_id_change_test2;
230         template<class, class> friend class ChangeSignaller;
231
232         void signal_change (ChangeType, int);
233
234         /** Paths of our data files */
235         std::vector<boost::filesystem::path> _paths;
236         std::vector<std::time_t> _last_write_times;
237
238         std::string _digest;
239         dcpomatic::DCPTime _position;
240         dcpomatic::ContentTime _trim_start;
241         dcpomatic::ContentTime _trim_end;
242         /** The video frame rate that this content is or was prepared to be used with,
243          *  or empty if the effective rate of this content should be dictated by something
244          *  else (either some video happening at the same time, or the rate of the DCP).
245          */
246         boost::optional<double> _video_frame_rate;
247         bool _change_signals_frequent;
248 };
249
250
251 typedef ChangeSignaller<Content, int> ContentChangeSignaller;
252
253
254 #endif