More fixes for errors / crashes / misbehaviour with content changes
[dcpomatic.git] / src / lib / player.h
1 /*
2     Copyright (C) 2013-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 #ifndef DCPOMATIC_PLAYER_H
22 #define DCPOMATIC_PLAYER_H
23
24 #include "player_text.h"
25 #include "active_text.h"
26 #include "content_text.h"
27 #include "film.h"
28 #include "content.h"
29 #include "position_image.h"
30 #include "piece.h"
31 #include "content_video.h"
32 #include "content_audio.h"
33 #include "audio_stream.h"
34 #include "audio_merger.h"
35 #include "empty.h"
36 #include <boost/shared_ptr.hpp>
37 #include <boost/enable_shared_from_this.hpp>
38 #include <list>
39
40 namespace dcp {
41         class ReelAsset;
42 }
43
44 class PlayerVideo;
45 class Playlist;
46 class Font;
47 class AudioBuffers;
48 class ReferencedReelAsset;
49 class Shuffler;
50
51 class PlayerProperty
52 {
53 public:
54         static int const VIDEO_CONTAINER_SIZE;
55         static int const PLAYLIST;
56         static int const FILM_CONTAINER;
57         static int const FILM_VIDEO_FRAME_RATE;
58         static int const DCP_DECODE_REDUCTION;
59 };
60
61 /** @class Player
62  *  @brief A class which can play a Playlist.
63  */
64 class Player : public boost::enable_shared_from_this<Player>, public boost::noncopyable
65 {
66 public:
67         Player (boost::shared_ptr<const Film>, boost::shared_ptr<const Playlist> playlist);
68         ~Player ();
69
70         bool pass ();
71         void seek (DCPTime time, bool accurate);
72
73         std::list<boost::shared_ptr<Font> > get_subtitle_fonts ();
74         std::list<ReferencedReelAsset> get_reel_assets ();
75         dcp::Size video_container_size () const {
76                 boost::mutex::scoped_lock lm (_mutex);
77                 return _video_container_size;
78         }
79
80         void set_video_container_size (dcp::Size);
81         void set_ignore_video ();
82         void set_ignore_audio ();
83         void set_ignore_text ();
84         void set_always_burn_open_subtitles ();
85         void set_fast ();
86         void set_play_referenced ();
87         void set_dcp_decode_reduction (boost::optional<int> reduction);
88
89         boost::optional<DCPTime> content_time_to_dcp (boost::shared_ptr<Content> content, ContentTime t);
90
91         boost::signals2::signal<void ()> MayChange;
92
93         /** Emitted when something has changed such that if we went back and emitted
94          *  the last frame again it would look different.  This is not emitted after
95          *  a seek.
96          *
97          *  The first parameter is what changed.
98          *  The second parameter is true if these signals are currently likely to be frequent.
99          */
100         boost::signals2::signal<void (int, bool)> Changed;
101         boost::signals2::signal<void ()> NotChanged;
102
103         /** Emitted when a video frame is ready.  These emissions happen in the correct order. */
104         boost::signals2::signal<void (boost::shared_ptr<PlayerVideo>, DCPTime)> Video;
105         boost::signals2::signal<void (boost::shared_ptr<AudioBuffers>, DCPTime)> Audio;
106         /** Emitted when a text is ready.  This signal may be emitted considerably
107          *  after the corresponding Video.
108          */
109         boost::signals2::signal<void (PlayerText, TextType, DCPTimePeriod)> Text;
110
111 private:
112         friend class PlayerWrapper;
113         friend class Piece;
114         friend struct player_time_calculation_test1;
115         friend struct player_time_calculation_test2;
116         friend struct player_time_calculation_test3;
117         friend struct player_subframe_test;
118
119         void setup_pieces ();
120         void setup_pieces_unlocked ();
121         void flush ();
122         void film_changed (Film::Property);
123         void playlist_changed ();
124         void playlist_content_may_change ();
125         void playlist_content_changed (boost::weak_ptr<Content>, int, bool);
126         void playlist_content_not_changed ();
127         std::list<PositionImage> transform_bitmap_texts (std::list<BitmapText>) const;
128         Frame dcp_to_content_video (boost::shared_ptr<const Piece> piece, DCPTime t) const;
129         DCPTime content_video_to_dcp (boost::shared_ptr<const Piece> piece, Frame f) const;
130         Frame dcp_to_resampled_audio (boost::shared_ptr<const Piece> piece, DCPTime t) const;
131         DCPTime resampled_audio_to_dcp (boost::shared_ptr<const Piece> piece, Frame f) const;
132         ContentTime dcp_to_content_time (boost::shared_ptr<const Piece> piece, DCPTime t) const;
133         DCPTime content_time_to_dcp (boost::shared_ptr<const Piece> piece, ContentTime t) const;
134         boost::shared_ptr<PlayerVideo> black_player_video_frame (Eyes eyes) const;
135         void video (boost::weak_ptr<Piece>, ContentVideo);
136         void audio (boost::weak_ptr<Piece>, AudioStreamPtr, ContentAudio);
137         void bitmap_text_start (boost::weak_ptr<Piece>, boost::weak_ptr<const TextContent>, ContentBitmapText);
138         void plain_text_start (boost::weak_ptr<Piece>, boost::weak_ptr<const TextContent>, ContentStringText);
139         void subtitle_stop (boost::weak_ptr<Piece>, boost::weak_ptr<const TextContent>, ContentTime, TextType);
140         DCPTime one_video_frame () const;
141         void fill_audio (DCPTimePeriod period);
142         std::pair<boost::shared_ptr<AudioBuffers>, DCPTime> discard_audio (
143                 boost::shared_ptr<const AudioBuffers> audio, DCPTime time, DCPTime discard_to
144                 ) const;
145         boost::optional<PositionImage> open_subtitles_for_frame (DCPTime time) const;
146         void emit_video (boost::shared_ptr<PlayerVideo> pv, DCPTime time);
147         void do_emit_video (boost::shared_ptr<PlayerVideo> pv, DCPTime time);
148         void emit_audio (boost::shared_ptr<AudioBuffers> data, DCPTime time);
149
150         /** Mutex to protect the whole Player state.  When it's used for the preview we have
151             seek() and pass() called from the Butler thread and lots of other stuff called
152             from the GUI thread.
153         */
154         mutable boost::mutex _mutex;
155
156         boost::shared_ptr<const Film> _film;
157         boost::shared_ptr<const Playlist> _playlist;
158
159         /** We can pass() and seek() */
160         bool _can_run;
161         std::list<boost::shared_ptr<Piece> > _pieces;
162
163         /** Size of the image in the DCP (e.g. 1990x1080 for flat) */
164         dcp::Size _video_container_size;
165         boost::shared_ptr<Image> _black_image;
166
167         /** true if the player should ignore all video; i.e. never produce any */
168         bool _ignore_video;
169         bool _ignore_audio;
170         /** true if the player should ignore all text; i.e. never produce any */
171         bool _ignore_text;
172         bool _always_burn_open_subtitles;
173         /** true if we should try to be fast rather than high quality */
174         bool _fast;
175         /** true if we should `play' (i.e output) referenced DCP data (e.g. for preview) */
176         bool _play_referenced;
177
178         /** Time just after the last video frame we emitted, or the time of the last accurate seek */
179         boost::optional<DCPTime> _last_video_time;
180         boost::optional<Eyes> _last_video_eyes;
181         /** Time just after the last audio frame we emitted, or the time of the last accurate seek */
182         boost::optional<DCPTime> _last_audio_time;
183
184         boost::optional<int> _dcp_decode_reduction;
185
186         typedef std::map<boost::weak_ptr<Piece>, boost::shared_ptr<PlayerVideo> > LastVideoMap;
187         LastVideoMap _last_video;
188
189         AudioMerger _audio_merger;
190         Shuffler* _shuffler;
191         std::list<std::pair<boost::shared_ptr<PlayerVideo>, DCPTime> > _delay;
192
193         class StreamState
194         {
195         public:
196                 StreamState () {}
197
198                 StreamState (boost::shared_ptr<Piece> p, DCPTime l)
199                         : piece(p)
200                         , last_push_end(l)
201                 {}
202
203                 boost::shared_ptr<Piece> piece;
204                 DCPTime last_push_end;
205         };
206         std::map<AudioStreamPtr, StreamState> _stream_states;
207
208         Empty _black;
209         Empty _silent;
210
211         ActiveText _active_texts[TEXT_COUNT];
212         boost::shared_ptr<AudioProcessor> _audio_processor;
213
214         boost::signals2::scoped_connection _film_changed_connection;
215         boost::signals2::scoped_connection _playlist_changed_connection;
216         boost::signals2::scoped_connection _playlist_content_may_change_connection;
217         boost::signals2::scoped_connection _playlist_content_changed_connection;
218         boost::signals2::scoped_connection _playlist_content_not_changed_connection;
219 };
220
221 #endif