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