Make Film state thread-safe.
[dcpomatic.git] / src / lib / film.h
1 /*
2     Copyright (C) 2012 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/film.h
21  *  @brief A representation of a piece of video (with sound), including naming,
22  *  the source content file, and how it should be presented in a DCP.
23  */
24
25 #ifndef DVDOMATIC_FILM_H
26 #define DVDOMATIC_FILM_H
27
28 #include <string>
29 #include <vector>
30 #include <inttypes.h>
31 #include <boost/thread/mutex.hpp>
32 #include <boost/thread.hpp>
33 extern "C" {
34 #include <libavcodec/avcodec.h>
35 }
36 #include "dcp_content_type.h"
37 #include "util.h"
38 #include "stream.h"
39 #include "trim_action.h"
40
41 class Format;
42 class Job;
43 class Filter;
44 class Log;
45 class ExamineContentJob;
46
47 /** @class Film
48  *  @brief A representation of a video with sound.
49  *
50  *  A representation of a piece of video (with sound), including naming,
51  *  the source content file, and how it should be presented in a DCP.
52  */
53 class Film : public boost::enable_shared_from_this<Film>
54 {
55 public:
56         Film (std::string d, bool must_exist = true);
57         Film (Film const &);
58         ~Film ();
59
60         std::string j2k_dir () const;
61         std::vector<std::string> audio_files () const;
62         std::pair<Position, std::string> thumb_subtitle (int) const;
63
64         void copy_from_dvd_post_gui ();
65         void examine_content ();
66         void examine_content_post_gui ();
67         void send_dcp_to_tms ();
68         void copy_from_dvd ();
69
70         void make_dcp (bool);
71
72         /** @return Logger.
73          *  It is safe to call this from any thread.
74          */
75         Log* log () const {
76                 return _log;
77         }
78
79         int encoded_frames () const;
80         
81         std::string file (std::string f) const;
82         std::string dir (std::string d) const;
83
84         std::string content_path () const;
85         ContentType content_type () const;
86         
87         bool content_is_dvd () const;
88
89         std::string thumb_file (int) const;
90         std::string thumb_base (int) const;
91         int thumb_frame (int) const;
92
93         int target_audio_sample_rate () const;
94         
95         void write_metadata () const;
96         void read_metadata ();
97
98         Size cropped_size (Size) const;
99         int dcp_length () const;
100         std::string dci_name () const;
101         std::string dcp_name () const;
102
103         bool dirty () const {
104                 return _dirty;
105         }
106
107         int audio_channels () const;
108
109         enum Property {
110                 NONE,
111                 NAME,
112                 USE_DCI_NAME,
113                 CONTENT,
114                 DCP_CONTENT_TYPE,
115                 FORMAT,
116                 CROP,
117                 FILTERS,
118                 SCALER,
119                 DCP_FRAMES,
120                 DCP_TRIM_ACTION,
121                 DCP_AB,
122                 AUDIO_STREAM,
123                 AUDIO_GAIN,
124                 AUDIO_DELAY,
125                 STILL_DURATION,
126                 SUBTITLE_STREAM,
127                 WITH_SUBTITLES,
128                 SUBTITLE_OFFSET,
129                 SUBTITLE_SCALE,
130                 DCI_METADATA,
131                 THUMBS,
132                 SIZE,
133                 LENGTH,
134                 AUDIO_SAMPLE_RATE,
135                 HAS_SUBTITLES,
136                 AUDIO_STREAMS,
137                 SUBTITLE_STREAMS,
138                 FRAMES_PER_SECOND,
139         };
140
141
142         /* GET */
143
144         std::string directory () const {
145                 boost::mutex::scoped_lock lm (_state_mutex);
146                 return _directory;
147         }
148
149         std::string name () const {
150                 boost::mutex::scoped_lock lm (_state_mutex);
151                 return _name;
152         }
153
154         bool use_dci_name () const {
155                 boost::mutex::scoped_lock lm (_state_mutex);
156                 return _use_dci_name;
157         }
158
159         std::string content () const {
160                 boost::mutex::scoped_lock lm (_state_mutex);
161                 return _content;
162         }
163
164         DCPContentType const * dcp_content_type () const {
165                 boost::mutex::scoped_lock lm (_state_mutex);
166                 return _dcp_content_type;
167         }
168
169         Format const * format () const {
170                 boost::mutex::scoped_lock lm (_state_mutex);
171                 return _format;
172         }
173
174         Crop crop () const {
175                 boost::mutex::scoped_lock lm (_state_mutex);
176                 return _crop;
177         }
178
179         std::vector<Filter const *> filters () const {
180                 boost::mutex::scoped_lock lm (_state_mutex);
181                 return _filters;
182         }
183
184         Scaler const * scaler () const {
185                 boost::mutex::scoped_lock lm (_state_mutex);
186                 return _scaler;
187         }
188
189         int dcp_frames () const {
190                 boost::mutex::scoped_lock lm (_state_mutex);
191                 return _dcp_frames;
192         }
193
194         TrimAction dcp_trim_action () const {
195                 boost::mutex::scoped_lock lm (_state_mutex);
196                 return _dcp_trim_action;
197         }
198
199         bool dcp_ab () const {
200                 boost::mutex::scoped_lock lm (_state_mutex);
201                 return _dcp_ab;
202         }
203
204         int audio_stream_index () const {
205                 boost::mutex::scoped_lock lm (_state_mutex);
206                 return _audio_stream;
207         }
208
209         AudioStream audio_stream () const {
210                 boost::mutex::scoped_lock lm (_state_mutex);
211                 assert (_audio_stream < int (_audio_streams.size()));
212                 return _audio_streams[_audio_stream];
213         }
214         
215         float audio_gain () const {
216                 boost::mutex::scoped_lock lm (_state_mutex);
217                 return _audio_gain;
218         }
219
220         int audio_delay () const {
221                 boost::mutex::scoped_lock lm (_state_mutex);
222                 return _audio_delay;
223         }
224
225         int still_duration () const {
226                 boost::mutex::scoped_lock lm (_state_mutex);
227                 return _still_duration;
228         }
229
230         int subtitle_stream_index () const {
231                 boost::mutex::scoped_lock lm (_state_mutex);
232                 return _subtitle_stream;
233         }
234
235         SubtitleStream subtitle_stream () const {
236                 boost::mutex::scoped_lock lm (_state_mutex);
237                 assert (_subtitle_stream < int (_subtitle_streams.size()));
238                 return _subtitle_streams[_subtitle_stream];
239         }
240
241         bool with_subtitles () const {
242                 boost::mutex::scoped_lock lm (_state_mutex);
243                 return _with_subtitles;
244         }
245
246         int subtitle_offset () const {
247                 boost::mutex::scoped_lock lm (_state_mutex);
248                 return _subtitle_offset;
249         }
250
251         float subtitle_scale () const {
252                 boost::mutex::scoped_lock lm (_state_mutex);
253                 return _subtitle_scale;
254         }
255
256         std::string audio_language () const {
257                 boost::mutex::scoped_lock lm (_state_mutex);
258                 return _audio_language;
259         }
260         
261         std::string subtitle_language () const {
262                 boost::mutex::scoped_lock lm (_state_mutex);
263                 return _subtitle_language;
264         }
265         
266         std::string territory () const {
267                 boost::mutex::scoped_lock lm (_state_mutex);
268                 return _territory;
269         }
270         
271         std::string rating () const {
272                 boost::mutex::scoped_lock lm (_state_mutex);
273                 return _rating;
274         }
275         
276         std::string studio () const {
277                 boost::mutex::scoped_lock lm (_state_mutex);
278                 return _studio;
279         }
280         
281         std::string facility () const {
282                 boost::mutex::scoped_lock lm (_state_mutex);
283                 return _facility;
284         }
285         
286         std::string package_type () const {
287                 boost::mutex::scoped_lock lm (_state_mutex);
288                 return _package_type;
289         }
290
291         std::vector<int> thumbs () const {
292                 boost::mutex::scoped_lock lm (_state_mutex);
293                 return _thumbs;
294         }
295         
296         Size size () const {
297                 boost::mutex::scoped_lock lm (_state_mutex);
298                 return _size;
299         }
300
301         int length () const {
302                 boost::mutex::scoped_lock lm (_state_mutex);
303                 return _length;
304         }
305
306         int audio_sample_rate () const {
307                 boost::mutex::scoped_lock lm (_state_mutex);
308                 return _audio_sample_rate;
309         }
310         
311         std::string content_digest () const {
312                 boost::mutex::scoped_lock lm (_state_mutex);
313                 return _content_digest;
314         }
315         
316         bool has_subtitles () const {
317                 boost::mutex::scoped_lock lm (_state_mutex);
318                 return _has_subtitles;
319         }
320
321         std::vector<AudioStream> audio_streams () const {
322                 boost::mutex::scoped_lock lm (_state_mutex);
323                 return _audio_streams;
324         }
325
326         std::vector<SubtitleStream> subtitle_streams () const {
327                 boost::mutex::scoped_lock lm (_state_mutex);
328                 return _subtitle_streams;
329         }
330         
331         float frames_per_second () const {
332                 boost::mutex::scoped_lock lm (_state_mutex);
333                 return _frames_per_second;
334         }
335
336
337         /* SET */
338
339         void set_directory (std::string);
340         void set_name (std::string);
341         void set_use_dci_name (bool);
342         virtual void set_content (std::string);
343         void set_dcp_content_type (DCPContentType const *);
344         void set_format (Format const *);
345         void set_crop (Crop);
346         void set_left_crop (int);
347         void set_right_crop (int);
348         void set_top_crop (int);
349         void set_bottom_crop (int);
350         void set_filters (std::vector<Filter const *>);
351         void set_scaler (Scaler const *);
352         void set_dcp_frames (int);
353         void set_dcp_trim_action (TrimAction);
354         void set_dcp_ab (bool);
355         void set_audio_stream (int);
356         void set_audio_gain (float);
357         void set_audio_delay (int);
358         void set_still_duration (int);
359         void set_subtitle_stream (int);
360         void set_with_subtitles (bool);
361         void set_subtitle_offset (int);
362         void set_subtitle_scale (float);
363         void set_audio_language (std::string);
364         void set_subtitle_language (std::string);
365         void set_territory (std::string);
366         void set_rating (std::string);
367         void set_studio (std::string);
368         void set_facility (std::string);
369         void set_package_type (std::string);
370         void set_thumbs (std::vector<int>);
371         void set_size (Size);
372         void set_length (int);
373         void set_audio_sample_rate (int);
374         void set_content_digest (std::string);
375         void set_has_subtitles (bool);
376         void set_audio_streams (std::vector<AudioStream>);
377         void set_subtitle_streams (std::vector<SubtitleStream>);
378         void set_frames_per_second (float);
379
380         /** Emitted when some property has changed */
381         mutable sigc::signal1<void, Property> Changed;
382         
383 private:
384         
385         /** Log to write to */
386         Log* _log;
387
388         /** Any running ExamineContentJob, or 0 */
389         boost::shared_ptr<ExamineContentJob> _examine_content_job;
390
391         std::string thumb_file_for_frame (int) const;
392         std::string thumb_base_for_frame (int) const;
393         void signal_changed (Property);
394         std::string file_locked (std::string) const;
395         
396         /** Complete path to directory containing the film metadata;
397          *  must not be relative.
398          */
399         std::string _directory;
400         /** Name for DVD-o-matic */
401         std::string _name;
402         /** True if a auto-generated DCI-compliant name should be used for our DCP */
403         bool _use_dci_name;
404         /** File or directory containing content; may be relative to our directory
405          *  or an absolute path.
406          */
407         std::string _content;
408         /** The type of content that this Film represents (feature, trailer etc.) */
409         DCPContentType const * _dcp_content_type;
410         /** The format to present this Film in (flat, scope, etc.) */
411         Format const * _format;
412         /** The crop to apply to the source */
413         Crop _crop;
414         /** Video filters that should be used when generating DCPs */
415         std::vector<Filter const *> _filters;
416         /** Scaler algorithm to use */
417         Scaler const * _scaler;
418         /** Number of frames to put in the DCP, or 0 for all */
419         int _dcp_frames;
420         /** What to do with audio when trimming DCPs */
421         TrimAction _dcp_trim_action;
422         /** true to create an A/B comparison DCP, where the left half of the image
423             is the video without any filters or post-processing, and the right half
424             has the specified filters and post-processing.
425         */
426         bool _dcp_ab;
427         /** An index into our _audio_streams vector for the stream to use for audio, or -1 if there is none */
428         int _audio_stream;
429         /** Gain to apply to audio in dB */
430         float _audio_gain;
431         /** Delay to apply to audio (positive moves audio later) in milliseconds */
432         int _audio_delay;
433         /** Duration to make still-sourced films (in seconds) */
434         int _still_duration;
435         /** An index into our _subtitle_streams vector for the stream to use for subtitles, or -1 if there is none */
436         int _subtitle_stream;
437         /** True if subtitles should be shown for this film */
438         bool _with_subtitles;
439         /** y offset for placing subtitles, in source pixels; +ve is further down
440             the frame, -ve is further up.
441         */
442         int _subtitle_offset;
443         /** scale factor to apply to subtitles */
444         float _subtitle_scale;
445
446         /* DCI naming stuff */
447         std::string _audio_language;
448         std::string _subtitle_language;
449         std::string _territory;
450         std::string _rating;
451         std::string _studio;
452         std::string _facility;
453         std::string _package_type;
454
455         /* Data which are cached to speed things up */
456
457         /** Vector of frame indices for each of our `thumbnails' */
458         std::vector<int> _thumbs;
459         /** Size, in pixels, of the source (ignoring cropping) */
460         Size _size;
461         /** Length of the source in frames */
462         int _length;
463         /** Sample rate of the source audio, in Hz */
464         int _audio_sample_rate;
465         /** MD5 digest of our content file */
466         std::string _content_digest;
467         /** true if the source has subtitles */
468         bool _has_subtitles;
469         /** the audio streams that the source has */
470         std::vector<AudioStream> _audio_streams;
471         /** the subtitle streams that the source has */
472         std::vector<SubtitleStream> _subtitle_streams;
473         /** Frames per second of the source */
474         float _frames_per_second;
475
476         mutable bool _dirty;
477
478         mutable boost::mutex _state_mutex;
479
480         friend class paths_test;
481 };
482
483 #endif