Merge master.
[dcpomatic.git] / src / lib / content.h
1 /*
2     Copyright (C) 2013-2014 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 <set>
28 #include <boost/filesystem.hpp>
29 #include <boost/signals2.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <boost/enable_shared_from_this.hpp>
32 #include <libxml++/libxml++.h>
33 #include <libcxml/cxml.h>
34 #include "types.h"
35 #include "dcpomatic_time.h"
36
37 namespace cxml {
38         class Node;
39 }
40
41 class Job;
42 class Film;
43
44 class ContentProperty
45 {
46 public:
47         static int const PATH;
48         static int const POSITION;
49         static int const LENGTH;
50         static int const TRIM_START;
51         static int const TRIM_END;
52 };
53
54 /** @class Content
55  *  @brief A piece of content represented by one or more files on disk.
56  */
57 class Content : public boost::enable_shared_from_this<Content>, public boost::noncopyable
58 {
59 public:
60         Content (boost::shared_ptr<const Film>);
61         Content (boost::shared_ptr<const Film>, DCPTime);
62         Content (boost::shared_ptr<const Film>, boost::filesystem::path);
63         Content (boost::shared_ptr<const Film>, cxml::ConstNodePtr);
64         Content (boost::shared_ptr<const Film>, std::vector<boost::shared_ptr<Content> >);
65         virtual ~Content () {}
66         
67         virtual void examine (boost::shared_ptr<Job>);
68         virtual std::string summary () const = 0;
69         /** @return Technical details of this content; these are written to logs to
70          *  help with debugging.
71          */
72         virtual std::string technical_summary () const;
73         virtual std::string information () const = 0;
74         virtual void as_xml (xmlpp::Node *) const;
75         virtual DCPTime full_length () const = 0;
76         virtual std::string identifier () const;
77
78         boost::shared_ptr<Content> clone () const;
79
80         void set_path (boost::filesystem::path);
81
82         std::string path_summary () const;
83
84         std::vector<boost::filesystem::path> paths () const {
85                 boost::mutex::scoped_lock lm (_mutex);
86                 return _paths;
87         }
88
89         size_t number_of_paths () const {
90                 boost::mutex::scoped_lock lm (_mutex);
91                 return _paths.size ();
92         }
93
94         boost::filesystem::path path (size_t i) const {
95                 boost::mutex::scoped_lock lm (_mutex);
96                 return _paths[i];
97         }
98         
99         bool paths_valid () const;
100
101         /** @return MD5 digest of the content's file(s) */
102         std::string digest () const {
103                 boost::mutex::scoped_lock lm (_mutex);
104                 return _digest;
105         }
106
107         void set_position (DCPTime);
108
109         /** DCPTime that this content starts; i.e. the time that the first
110          *  bit of the content (trimmed or not) will happen.
111          */
112         DCPTime position () const {
113                 boost::mutex::scoped_lock lm (_mutex);
114                 return _position;
115         }
116
117         void set_trim_start (DCPTime);
118
119         DCPTime trim_start () const {
120                 boost::mutex::scoped_lock lm (_mutex);
121                 return _trim_start;
122         }
123
124         void set_trim_end (DCPTime);
125         
126         DCPTime trim_end () const {
127                 boost::mutex::scoped_lock lm (_mutex);
128                 return _trim_end;
129         }
130         
131         DCPTime end () const {
132                 return position() + length_after_trim();
133         }
134
135         DCPTime length_after_trim () const;
136         
137         void set_change_signals_frequent (bool f) {
138                 _change_signals_frequent = f;
139         }
140
141         boost::shared_ptr<const Film> film () const {
142                 return _film.lock ();
143         }
144
145         boost::signals2::signal<void (boost::weak_ptr<Content>, int, bool)> Changed;
146
147 protected:
148         void signal_changed (int);
149
150         boost::weak_ptr<const Film> _film;
151
152         /** _mutex which should be used to protect accesses, as examine
153          *  jobs can update content state in threads other than the main one.
154          */
155         mutable boost::mutex _mutex;
156
157         /** Paths of our data files */
158         std::vector<boost::filesystem::path> _paths;
159         
160 private:
161         std::string _digest;
162         DCPTime _position;
163         DCPTime _trim_start;
164         DCPTime _trim_end;
165         bool _change_signals_frequent;
166 };
167
168 #endif