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