Make terminate_threads() less likely to leave _threads containing invalid pointers.
[dcpomatic.git] / src / lib / film.h
1 /*
2     Copyright (C) 2012-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/film.h
22  *  @brief A representation of some audio and video content, and details of
23  *  how they should be presented in a DCP.
24  */
25
26 #ifndef DCPOMATIC_FILM_H
27 #define DCPOMATIC_FILM_H
28
29 #include "util.h"
30 #include "types.h"
31 #include "isdcf_metadata.h"
32 #include "frame_rate_change.h"
33 #include "signaller.h"
34 #include "dcp_text_track.h"
35 #include <dcp/key.h>
36 #include <dcp/encrypted_kdm.h>
37 #include <boost/signals2.hpp>
38 #include <boost/enable_shared_from_this.hpp>
39 #include <boost/thread.hpp>
40 #include <boost/filesystem.hpp>
41 #include <boost/thread/mutex.hpp>
42 #include <string>
43 #include <vector>
44 #include <inttypes.h>
45
46 namespace xmlpp {
47         class Document;
48 }
49
50 class DCPContentType;
51 class Log;
52 class Content;
53 class Playlist;
54 class AudioContent;
55 class Screen;
56 class AudioProcessor;
57 class AudioMapping;
58 class Ratio;
59 class Job;
60 class ScreenKDM;
61 class Film;
62 struct isdcf_name_test;
63
64 class InfoFileHandle
65 {
66 public:
67         ~InfoFileHandle ();
68
69         FILE* get () const {
70                 return _handle;
71         }
72
73         boost::filesystem::path file () const {
74                 return _file;
75         }
76
77 private:
78         friend class Film;
79
80         InfoFileHandle (boost::mutex& mutex, boost::filesystem::path file, bool read);
81
82         boost::mutex::scoped_lock _lock;
83         FILE* _handle;
84         boost::filesystem::path _file;
85 };
86
87 /** @class Film
88  *
89  *  @brief A representation of some audio and video content, and details of
90  *  how they should be presented in a DCP.
91  *
92  *  The content of a Film is held in a Playlist (created and managed by the Film).
93  */
94 class Film : public boost::enable_shared_from_this<Film>, public Signaller, public boost::noncopyable
95 {
96 public:
97         explicit Film (boost::optional<boost::filesystem::path> dir);
98         ~Film ();
99
100         boost::shared_ptr<InfoFileHandle> info_file_handle (DCPTimePeriod period, bool read) const;
101         boost::filesystem::path j2c_path (int, Frame, Eyes, bool) const;
102         boost::filesystem::path internal_video_asset_dir () const;
103         boost::filesystem::path internal_video_asset_filename (DCPTimePeriod p) const;
104
105         boost::filesystem::path audio_analysis_path (boost::shared_ptr<const Playlist>) const;
106
107         void send_dcp_to_tms ();
108         void make_dcp ();
109
110         /** @return Logger.
111          *  It is safe to call this from any thread.
112          */
113         boost::shared_ptr<Log> log () const {
114                 return _log;
115         }
116
117         boost::filesystem::path file (boost::filesystem::path f) const;
118         boost::filesystem::path dir (boost::filesystem::path d, bool create = true) const;
119
120         void use_template (std::string name);
121         std::list<std::string> read_metadata (boost::optional<boost::filesystem::path> path = boost::optional<boost::filesystem::path> ());
122         void write_metadata () const;
123         void write_metadata (boost::filesystem::path path) const;
124         void write_template (boost::filesystem::path path) const;
125         boost::shared_ptr<xmlpp::Document> metadata (bool with_content_paths = true) const;
126
127         void copy_from (boost::shared_ptr<const Film> film);
128
129         std::string isdcf_name (bool if_created_now) const;
130         std::string dcp_name (bool if_created_now = false) const;
131
132         /** @return true if our state has changed since we last saved it */
133         bool dirty () const {
134                 return _dirty;
135         }
136
137         dcp::Size full_frame () const;
138         dcp::Size frame_size () const;
139
140         std::vector<CPLSummary> cpls () const;
141
142         int audio_frame_rate () const;
143
144         std::list<DCPTextTrack> closed_caption_tracks () const;
145
146         uint64_t required_disk_space () const;
147         bool should_be_enough_disk_space (double& required, double& available, bool& can_hard_link) const;
148
149         /* Proxies for some Playlist methods */
150
151         ContentList content () const;
152         DCPTime length () const;
153         int best_video_frame_rate () const;
154         FrameRateChange active_frame_rate_change (DCPTime) const;
155         std::pair<double, double> speed_up_range (int dcp_frame_rate) const;
156
157         dcp::EncryptedKDM make_kdm (
158                 dcp::Certificate recipient,
159                 std::vector<std::string> trusted_devices,
160                 boost::filesystem::path cpl_file,
161                 dcp::LocalTime from,
162                 dcp::LocalTime until,
163                 dcp::Formulation formulation,
164                 bool disable_forensic_marking_picture,
165                 boost::optional<int> disable_forensic_marking_audio
166                 ) const;
167
168         std::list<ScreenKDM> make_kdms (
169                 std::list<boost::shared_ptr<Screen> > screens,
170                 boost::filesystem::path cpl_file,
171                 boost::posix_time::ptime from,
172                 boost::posix_time::ptime until,
173                 dcp::Formulation formulation,
174                 bool disable_forensic_marking_picture,
175                 boost::optional<int> disable_forensic_marking_audio
176                 ) const;
177
178         int state_version () const {
179                 return _state_version;
180         }
181
182         std::string subtitle_language () const;
183
184         std::vector<std::string> audio_output_names () const;
185
186         void repeat_content (ContentList, int);
187
188         boost::shared_ptr<const Playlist> playlist () const {
189                 return _playlist;
190         }
191
192         std::list<DCPTimePeriod> reels () const;
193         std::list<int> mapped_audio_channels () const;
194
195         std::string content_summary (DCPTimePeriod period) const;
196
197         bool references_dcp_video () const;
198         bool references_dcp_audio () const;
199
200         /** Identifiers for the parts of our state;
201             used for signalling changes.
202         */
203         enum Property {
204                 NONE,
205                 NAME,
206                 USE_ISDCF_NAME,
207                 /** The playlist's content list has changed (i.e. content has been added or removed) */
208                 CONTENT,
209                 /** The order of content in the playlist has changed */
210                 CONTENT_ORDER,
211                 DCP_CONTENT_TYPE,
212                 CONTAINER,
213                 RESOLUTION,
214                 SIGNED,
215                 ENCRYPTED,
216                 KEY,
217                 J2K_BANDWIDTH,
218                 ISDCF_METADATA,
219                 VIDEO_FRAME_RATE,
220                 AUDIO_CHANNELS,
221                 /** The setting of _three_d has changed */
222                 THREE_D,
223                 SEQUENCE,
224                 INTEROP,
225                 AUDIO_PROCESSOR,
226                 REEL_TYPE,
227                 REEL_LENGTH,
228                 UPLOAD_AFTER_MAKE_DCP,
229                 REENCODE_J2K
230         };
231
232
233         /* GET */
234
235         boost::optional<boost::filesystem::path> directory () const {
236                 return _directory;
237         }
238
239         std::string name () const {
240                 return _name;
241         }
242
243         bool use_isdcf_name () const {
244                 return _use_isdcf_name;
245         }
246
247         DCPContentType const * dcp_content_type () const {
248                 return _dcp_content_type;
249         }
250
251         Ratio const * container () const {
252                 return _container;
253         }
254
255         Resolution resolution () const {
256                 return _resolution;
257         }
258
259         /* signed is a reserved word */
260         bool is_signed () const {
261                 return _signed;
262         }
263
264         bool encrypted () const {
265                 return _encrypted;
266         }
267
268         dcp::Key key () const {
269                 return _key;
270         }
271
272         int j2k_bandwidth () const {
273                 return _j2k_bandwidth;
274         }
275
276         ISDCFMetadata isdcf_metadata () const {
277                 return _isdcf_metadata;
278         }
279
280         /** @return The frame rate of the DCP */
281         int video_frame_rate () const {
282                 return _video_frame_rate;
283         }
284
285         int audio_channels () const {
286                 return _audio_channels;
287         }
288
289         bool three_d () const {
290                 return _three_d;
291         }
292
293         bool sequence () const {
294                 return _sequence;
295         }
296
297         bool interop () const {
298                 return _interop;
299         }
300
301         AudioProcessor const * audio_processor () const {
302                 return _audio_processor;
303         }
304
305         ReelType reel_type () const {
306                 return _reel_type;
307         }
308
309         int64_t reel_length () const {
310                 return _reel_length;
311         }
312
313         bool upload_after_make_dcp () const {
314                 return _upload_after_make_dcp;
315         }
316
317         std::string context_id () const {
318                 return _context_id;
319         }
320
321         bool reencode_j2k () const {
322                 return _reencode_j2k;
323         }
324
325
326         /* SET */
327
328         void set_directory (boost::filesystem::path);
329         void set_name (std::string);
330         void set_use_isdcf_name (bool);
331         void examine_and_add_content (boost::shared_ptr<Content> content, bool disable_audio_analysis = false);
332         void add_content (boost::shared_ptr<Content>);
333         void remove_content (boost::shared_ptr<Content>);
334         void remove_content (ContentList);
335         void move_content_earlier (boost::shared_ptr<Content>);
336         void move_content_later (boost::shared_ptr<Content>);
337         void set_dcp_content_type (DCPContentType const *);
338         void set_container (Ratio const *);
339         void set_resolution (Resolution);
340         void set_signed (bool);
341         void set_encrypted (bool);
342         void set_key (dcp::Key key);
343         void set_j2k_bandwidth (int);
344         void set_isdcf_metadata (ISDCFMetadata);
345         void set_video_frame_rate (int rate, bool user_explicit = false);
346         void set_audio_channels (int);
347         void set_three_d (bool);
348         void set_isdcf_date_today ();
349         void set_sequence (bool);
350         void set_interop (bool);
351         void set_audio_processor (AudioProcessor const * processor);
352         void set_reel_type (ReelType);
353         void set_reel_length (int64_t);
354         void set_upload_after_make_dcp (bool);
355         void set_reencode_j2k (bool);
356
357         /** Emitted when some property has of the Film is about to change or has changed */
358         mutable boost::signals2::signal<void (ChangeType, Property)> Change;
359
360         /** Emitted when some property of our content has changed */
361         mutable boost::signals2::signal<void (ChangeType, boost::weak_ptr<Content>, int, bool)> ContentChange;
362
363         /** Emitted when we have something important to tell the user */
364         boost::signals2::signal<void (std::string)> Message;
365
366         /** Current version number of the state file */
367         static int const current_state_version;
368
369 private:
370
371         friend struct ::isdcf_name_test;
372         template <typename> friend class ChangeSignaller;
373
374         boost::filesystem::path info_file (DCPTimePeriod p) const;
375
376         void signal_change (ChangeType, Property);
377         void signal_change (ChangeType, int);
378         std::string video_identifier () const;
379         void playlist_change (ChangeType);
380         void playlist_order_changed ();
381         void playlist_content_change (ChangeType type, boost::weak_ptr<Content>, int, bool frequent);
382         void maybe_add_content (boost::weak_ptr<Job>, boost::weak_ptr<Content>, bool disable_audio_analysis);
383         void audio_analysis_finished ();
384
385         static std::string const metadata_file;
386
387         /** Log to write to */
388         boost::shared_ptr<Log> _log;
389         boost::shared_ptr<Playlist> _playlist;
390
391         /** Complete path to directory containing the film metadata;
392          *  must not be relative.
393          */
394         boost::optional<boost::filesystem::path> _directory;
395
396         /** Name for DCP-o-matic */
397         std::string _name;
398         /** True if a auto-generated ISDCF-compliant name should be used for our DCP */
399         bool _use_isdcf_name;
400         /** The type of content that this Film represents (feature, trailer etc.) */
401         DCPContentType const * _dcp_content_type;
402         /** The container to put this Film in (flat, scope, etc.) */
403         Ratio const * _container;
404         /** DCP resolution (2K or 4K) */
405         Resolution _resolution;
406         bool _signed;
407         bool _encrypted;
408         dcp::Key _key;
409         /** context ID used when encrypting picture assets; we keep it so that we can
410          *  re-start picture MXF encodes.
411          */
412         std::string _context_id;
413         /** bandwidth for J2K files in bits per second */
414         int _j2k_bandwidth;
415         /** ISDCF naming stuff */
416         ISDCFMetadata _isdcf_metadata;
417         /** Frames per second to run our DCP at */
418         int _video_frame_rate;
419         /** The date that we should use in a ISDCF name */
420         boost::gregorian::date _isdcf_date;
421         /** Number of audio channels requested for the DCP */
422         int _audio_channels;
423         /** If true, the DCP will be written in 3D mode; otherwise in 2D.
424             This will be regardless of what content is on the playlist.
425         */
426         bool _three_d;
427         bool _sequence;
428         bool _interop;
429         AudioProcessor const * _audio_processor;
430         ReelType _reel_type;
431         /** Desired reel length in bytes, if _reel_type == REELTYPE_BY_LENGTH */
432         int64_t _reel_length;
433         bool _upload_after_make_dcp;
434         bool _reencode_j2k;
435         /** true if the user has ever explicitly set the video frame rate of this film */
436         bool _user_explicit_video_frame_rate;
437
438         int _state_version;
439
440         /** true if our state has changed since we last saved it */
441         mutable bool _dirty;
442         /** film being used as a template, or 0 */
443         boost::shared_ptr<Film> _template_film;
444
445
446         mutable boost::mutex _info_file_mutex;
447
448         boost::signals2::scoped_connection _playlist_change_connection;
449         boost::signals2::scoped_connection _playlist_order_changed_connection;
450         boost::signals2::scoped_connection _playlist_content_change_connection;
451         std::list<boost::signals2::connection> _job_connections;
452         std::list<boost::signals2::connection> _audio_analysis_connections;
453
454         friend struct paths_test;
455         friend struct film_metadata_test;
456 };
457
458 #endif