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 "user_property.h"
35 #include "text_type.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 class AudioContent;
54 class TextContent;
55 class VideoContent;
56
57 class ContentProperty
58 {
59 public:
60         static int const PATH;
61         static int const POSITION;
62         static int const LENGTH;
63         static int const TRIM_START;
64         static int const TRIM_END;
65         static int const VIDEO_FRAME_RATE;
66 };
67
68
69 /** @class Content
70  *  @brief A piece of content represented by one or more files on disk.
71  */
72 class Content : public std::enable_shared_from_this<Content>, public Signaller
73 {
74 public:
75         explicit Content ();
76         Content (dcpomatic::DCPTime);
77         Content (boost::filesystem::path);
78         Content (cxml::ConstNodePtr);
79         Content (std::vector<std::shared_ptr<Content>>);
80         virtual ~Content () {}
81
82         Content (Content const&) = delete;
83         Content& operator= (Content const&) = delete;
84
85         /** Examine the content to establish digest, frame rates and any other
86          *  useful metadata.
87          *  @param job Job to use to report progress, or 0.
88          */
89         virtual void examine (std::shared_ptr<const Film> film, std::shared_ptr<Job> job);
90
91         virtual void take_settings_from (std::shared_ptr<const Content> c);
92
93         /** @return Quick one-line summary of the content, as will be presented in the
94          *  film editor.
95          */
96         virtual std::string summary () const = 0;
97
98         /** @return Technical details of this content; these are written to logs to
99          *  help with debugging.
100          */
101         virtual std::string technical_summary () const;
102
103         virtual void as_xml (xmlpp::Node *, bool with_paths) const;
104         virtual dcpomatic::DCPTime full_length (std::shared_ptr<const Film>) const = 0;
105         virtual dcpomatic::DCPTime approximate_length () const = 0;
106         virtual std::string identifier () const;
107         /** @return points at which to split this content when
108          *  REELTYPE_BY_VIDEO_CONTENT is in use.
109          */
110         virtual std::list<dcpomatic::DCPTime> reel_split_points (std::shared_ptr<const Film>) const;
111
112         std::shared_ptr<Content> clone () const;
113
114         void set_paths (std::vector<boost::filesystem::path> paths);
115
116         std::string path_summary () const;
117
118         std::vector<boost::filesystem::path> paths () const {
119                 boost::mutex::scoped_lock lm (_mutex);
120                 return _paths;
121         }
122
123         size_t number_of_paths () const {
124                 boost::mutex::scoped_lock lm (_mutex);
125                 return _paths.size ();
126         }
127
128         boost::filesystem::path path (size_t i) const {
129                 boost::mutex::scoped_lock lm (_mutex);
130                 return _paths[i];
131         }
132
133         std::time_t last_write_time (size_t i) const {
134                 boost::mutex::scoped_lock lm (_mutex);
135                 return _last_write_times[i];
136         }
137
138         bool paths_valid () const;
139
140         /** @return Digest of the content's file(s).  Note: this is
141          *  not a complete MD5-or-whatever hash, but a sort of poor
142          *  man's version (see comments in examine()).
143          */
144         std::string digest () const {
145                 boost::mutex::scoped_lock lm (_mutex);
146                 return _digest;
147         }
148
149         void set_position (std::shared_ptr<const Film> film, dcpomatic::DCPTime, bool force_emit = false);
150
151         /** dcpomatic::DCPTime that this content starts; i.e. the time that the first
152          *  bit of the content (trimmed or not) will happen.
153          */
154         dcpomatic::DCPTime position () const {
155                 boost::mutex::scoped_lock lm (_mutex);
156                 return _position;
157         }
158
159         void set_trim_start(std::shared_ptr<const Film> film, dcpomatic::ContentTime);
160
161         dcpomatic::ContentTime trim_start () const {
162                 boost::mutex::scoped_lock lm (_mutex);
163                 return _trim_start;
164         }
165
166         void set_trim_end (dcpomatic::ContentTime);
167
168         dcpomatic::ContentTime trim_end () const {
169                 boost::mutex::scoped_lock lm (_mutex);
170                 return _trim_end;
171         }
172
173         /** @return Time immediately after the last thing in this content */
174         dcpomatic::DCPTime end (std::shared_ptr<const Film> film) const {
175                 return position() + length_after_trim(film);
176         }
177
178         dcpomatic::DCPTimePeriod period(std::shared_ptr<const Film> film) const {
179                 return { position(), end(film) };
180         }
181
182         dcpomatic::DCPTime length_after_trim (std::shared_ptr<const Film> film) const;
183
184         boost::optional<double> video_frame_rate () const {
185                 boost::mutex::scoped_lock lm (_mutex);
186                 return _video_frame_rate;
187         }
188
189         void set_video_frame_rate(std::shared_ptr<const Film> film, double r);
190         void unset_video_frame_rate ();
191
192         double active_video_frame_rate (std::shared_ptr<const Film> film) const;
193
194         void set_change_signals_frequent (bool f) {
195                 _change_signals_frequent = f;
196         }
197
198         std::list<UserProperty> user_properties (std::shared_ptr<const Film> film) const;
199
200         std::string calculate_digest () const;
201
202         virtual bool can_be_played () const {
203                 return true;
204         }
205
206         /* ChangeType::PENDING and ChangeType::CANCELLED may be emitted from any thread; ChangeType::DONE always from GUI thread */
207         boost::signals2::signal<void (ChangeType, std::weak_ptr<Content>, int, bool)> Change;
208
209         std::shared_ptr<VideoContent> video;
210         std::shared_ptr<AudioContent> audio;
211         std::vector<std::shared_ptr<TextContent>> text;
212         std::shared_ptr<AtmosContent> atmos;
213
214         std::shared_ptr<TextContent> only_text () const;
215         std::shared_ptr<TextContent> text_of_original_type (TextType type) const;
216
217         /** @return true if this content has changed since it was last examined */
218         bool changed () const;
219
220 protected:
221
222         virtual void add_properties (std::shared_ptr<const Film> film, std::list<UserProperty> &) const;
223
224         /** _mutex which should be used to protect accesses, as examine
225          *  jobs can update content state in threads other than the main one.
226          */
227         mutable boost::mutex _mutex;
228
229         void add_path (boost::filesystem::path p);
230
231 private:
232         friend struct ffmpeg_pts_offset_test;
233         friend struct best_dcp_frame_rate_test_single;
234         friend struct best_dcp_frame_rate_test_double;
235         friend struct audio_sampling_rate_test;
236         friend struct subtitle_font_id_change_test2;
237         template<class, class> friend class ChangeSignalDespatcher;
238
239         void signal_change (ChangeType, int);
240
241         /** Paths of our data files */
242         std::vector<boost::filesystem::path> _paths;
243         std::vector<std::time_t> _last_write_times;
244
245         std::string _digest;
246         dcpomatic::DCPTime _position;
247         dcpomatic::ContentTime _trim_start;
248         dcpomatic::ContentTime _trim_end;
249         /** The video frame rate that this content is or was prepared to be used with,
250          *  or empty if the effective rate of this content should be dictated by something
251          *  else (either some video happening at the same time, or the rate of the DCP).
252          */
253         boost::optional<double> _video_frame_rate;
254         bool _change_signals_frequent = false;
255 };
256
257
258 typedef ChangeSignaller<Content, int> ContentChangeSignaller;
259 typedef ChangeSignalDespatcher<Content, int> ContentChangeSignalDespatcher;
260
261
262 #endif