Rename SafeStringStream -> locked_stringstream. Bump deps for removal of stringstream.
[dcpomatic.git] / src / lib / content.h
1 /*
2     Copyright (C) 2013-2016 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 "raw_convert.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         Content (boost::shared_ptr<const Film>);
68         Content (boost::shared_ptr<const Film>, DCPTime);
69         Content (boost::shared_ptr<const Film>, boost::filesystem::path);
70         Content (boost::shared_ptr<const Film>, cxml::ConstNodePtr);
71         Content (boost::shared_ptr<const Film>, 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<Job> job);
79
80         /** @return Quick one-line summary of the content, as will be presented in the
81          *  film editor.
82          */
83         virtual std::string summary () const = 0;
84
85         /** @return Technical details of this content; these are written to logs to
86          *  help with debugging.
87          */
88         virtual std::string technical_summary () const;
89
90         virtual void as_xml (xmlpp::Node *) const;
91         virtual DCPTime full_length () const = 0;
92         virtual std::string identifier () const;
93         /** @return points at which to split this content when
94          *  REELTYPE_BY_VIDEO_CONTENT is in use.
95          */
96         virtual std::list<DCPTime> reel_split_points () const;
97
98         boost::shared_ptr<Content> clone () const;
99
100         void set_path (boost::filesystem::path);
101
102         std::string path_summary () const;
103
104         std::vector<boost::filesystem::path> paths () const {
105                 boost::mutex::scoped_lock lm (_mutex);
106                 return _paths;
107         }
108
109         size_t number_of_paths () const {
110                 boost::mutex::scoped_lock lm (_mutex);
111                 return _paths.size ();
112         }
113
114         boost::filesystem::path path (size_t i) const {
115                 boost::mutex::scoped_lock lm (_mutex);
116                 return _paths[i];
117         }
118
119         bool paths_valid () const;
120
121         /** @return Digest of the content's file(s).  Note: this is
122          *  not a complete MD5-or-whatever hash, but a sort of poor
123          *  man's version (see comments in ::examine).
124          */
125         std::string digest () const {
126                 boost::mutex::scoped_lock lm (_mutex);
127                 return _digest;
128         }
129
130         void set_position (DCPTime);
131
132         /** DCPTime that this content starts; i.e. the time that the first
133          *  bit of the content (trimmed or not) will happen.
134          */
135         DCPTime position () const {
136                 boost::mutex::scoped_lock lm (_mutex);
137                 return _position;
138         }
139
140         void set_trim_start (ContentTime);
141
142         ContentTime trim_start () const {
143                 boost::mutex::scoped_lock lm (_mutex);
144                 return _trim_start;
145         }
146
147         void set_trim_end (ContentTime);
148
149         ContentTime trim_end () const {
150                 boost::mutex::scoped_lock lm (_mutex);
151                 return _trim_end;
152         }
153
154         /** @return Time immediately after the last thing in this content */
155         DCPTime end () const {
156                 return position() + length_after_trim();
157         }
158
159         DCPTime length_after_trim () const;
160
161         boost::optional<double> video_frame_rate () const {
162                 boost::mutex::scoped_lock lm (_mutex);
163                 return _video_frame_rate;
164         }
165
166         void set_video_frame_rate (double r);
167
168         double active_video_frame_rate () const;
169
170         void set_change_signals_frequent (bool f) {
171                 _change_signals_frequent = f;
172         }
173
174         boost::shared_ptr<const Film> film () const;
175
176         std::list<UserProperty> user_properties () const;
177
178         boost::signals2::signal<void (boost::weak_ptr<Content>, int, bool)> Changed;
179
180         boost::shared_ptr<VideoContent> video;
181         boost::shared_ptr<AudioContent> audio;
182         boost::shared_ptr<SubtitleContent> subtitle;
183
184         void signal_changed (int);
185
186 protected:
187
188         virtual void add_properties (std::list<UserProperty> &) const;
189
190         boost::weak_ptr<const Film> _film;
191
192         /** _mutex which should be used to protect accesses, as examine
193          *  jobs can update content state in threads other than the main one.
194          */
195         mutable boost::mutex _mutex;
196
197         /** Paths of our data files */
198         std::vector<boost::filesystem::path> _paths;
199
200 private:
201         friend struct ffmpeg_pts_offset_test;
202         friend struct best_dcp_frame_rate_test_single;
203         friend struct best_dcp_frame_rate_test_double;
204         friend struct audio_sampling_rate_test;
205
206         std::string _digest;
207         DCPTime _position;
208         ContentTime _trim_start;
209         ContentTime _trim_end;
210         /** The video frame rate that this content is or was prepared to be used with,
211          *  or empty if the effective rate of this content should be dictated by something
212          *  else (either some video happening at the same time, or the rate of the DCP).
213          */
214         boost::optional<double> _video_frame_rate;
215         bool _change_signals_frequent;
216 };
217
218 #endif