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