Clean up a bit by using Content::film() more.
[dcpomatic.git] / src / lib / content.h
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/lib/content.h
21  *  @brief Content class.
22  */
23
24 #ifndef DCPOMATIC_CONTENT_H
25 #define DCPOMATIC_CONTENT_H
26
27 #include "types.h"
28 #include "signaller.h"
29 #include "dcpomatic_time.h"
30 #include <libcxml/cxml.h>
31 #include <boost/filesystem.hpp>
32 #include <boost/signals2.hpp>
33 #include <boost/thread/mutex.hpp>
34 #include <boost/enable_shared_from_this.hpp>
35
36 namespace xmlpp {
37         class Node;
38 }
39
40 namespace cxml {
41         class Node;
42 }
43
44 class Job;
45 class Film;
46
47 class ContentProperty
48 {
49 public:
50         static int const PATH;
51         static int const POSITION;
52         static int const LENGTH;
53         static int const TRIM_START;
54         static int const TRIM_END;
55 };
56
57 /** @class Content
58  *  @brief A piece of content represented by one or more files on disk.
59  */
60 class Content : public boost::enable_shared_from_this<Content>, public Signaller, public boost::noncopyable
61 {
62 public:
63         Content (boost::shared_ptr<const Film>);
64         Content (boost::shared_ptr<const Film>, DCPTime);
65         Content (boost::shared_ptr<const Film>, boost::filesystem::path);
66         Content (boost::shared_ptr<const Film>, cxml::ConstNodePtr);
67         Content (boost::shared_ptr<const Film>, std::vector<boost::shared_ptr<Content> >);
68         virtual ~Content () {}
69
70         /** Examine the content to establish digest, frame rates and any other
71          *  useful metadata.
72          *  @param job Job to use to report progress, or 0.
73          */
74         virtual void examine (boost::shared_ptr<Job> job);
75
76         /** @return Quick one-line summary of the content, as will be presented in the
77          *  film editor.
78          */
79         virtual std::string summary () const = 0;
80
81         /** @return Technical details of this content; these are written to logs to
82          *  help with debugging.
83          */
84         virtual std::string technical_summary () const;
85
86         virtual void as_xml (xmlpp::Node *) const;
87         virtual DCPTime full_length () const = 0;
88         virtual std::string identifier () const;
89
90         std::list<std::pair<std::string, std::string> > properties () const;
91
92         boost::shared_ptr<Content> clone () const;
93
94         void set_path (boost::filesystem::path);
95
96         std::string path_summary () const;
97
98         std::vector<boost::filesystem::path> paths () const {
99                 boost::mutex::scoped_lock lm (_mutex);
100                 return _paths;
101         }
102
103         size_t number_of_paths () const {
104                 boost::mutex::scoped_lock lm (_mutex);
105                 return _paths.size ();
106         }
107
108         boost::filesystem::path path (size_t i) const {
109                 boost::mutex::scoped_lock lm (_mutex);
110                 return _paths[i];
111         }
112
113         bool paths_valid () const;
114
115        /** @return Digest of the content's file(s).  Note: this is
116         *  not a complete MD5-or-whatever hash, but a sort of poor
117         *  man's version (see comments in ::examine).
118         */
119         std::string digest () const {
120                 boost::mutex::scoped_lock lm (_mutex);
121                 return _digest;
122         }
123
124         void set_position (DCPTime);
125
126         /** DCPTime that this content starts; i.e. the time that the first
127          *  bit of the content (trimmed or not) will happen.
128          */
129         DCPTime position () const {
130                 boost::mutex::scoped_lock lm (_mutex);
131                 return _position;
132         }
133
134         void set_trim_start (ContentTime);
135
136         ContentTime trim_start () const {
137                 boost::mutex::scoped_lock lm (_mutex);
138                 return _trim_start;
139         }
140
141         void set_trim_end (ContentTime);
142
143         ContentTime trim_end () const {
144                 boost::mutex::scoped_lock lm (_mutex);
145                 return _trim_end;
146         }
147
148         /** @return Time immediately after the last thing in this content */
149         DCPTime end () const {
150                 return position() + length_after_trim();
151         }
152
153         DCPTime length_after_trim () const;
154
155         void set_change_signals_frequent (bool f) {
156                 _change_signals_frequent = f;
157         }
158
159         boost::shared_ptr<const Film> film () const;
160
161         boost::signals2::signal<void (boost::weak_ptr<Content>, int, bool)> Changed;
162
163 protected:
164         void signal_changed (int);
165         virtual void add_properties (std::list<std::pair<std::string, std::string> > &) const {}
166
167         boost::weak_ptr<const Film> _film;
168
169         /** _mutex which should be used to protect accesses, as examine
170          *  jobs can update content state in threads other than the main one.
171          */
172         mutable boost::mutex _mutex;
173
174         /** Paths of our data files */
175         std::vector<boost::filesystem::path> _paths;
176
177 private:
178         std::string _digest;
179         DCPTime _position;
180         ContentTime _trim_start;
181         ContentTime _trim_end;
182         bool _change_signals_frequent;
183 };
184
185 #endif