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