Merge master.
[dcpomatic.git] / src / lib / player.h
1 /*
2     Copyright (C) 2013 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 #ifndef DCPOMATIC_PLAYER_H
21 #define DCPOMATIC_PLAYER_H
22
23 #include <list>
24 #include <boost/shared_ptr.hpp>
25 #include <boost/enable_shared_from_this.hpp>
26 #include <libdcp/subtitle_asset.h>
27 #include "playlist.h"
28 #include "content.h"
29 #include "film.h"
30 #include "rect.h"
31 #include "audio_merger.h"
32 #include "audio_content.h"
33 #include "decoded.h"
34
35 class Job;
36 class Film;
37 class Playlist;
38 class AudioContent;
39 class Piece;
40 class Image;
41
42 /** @class PlayerImage
43  *  @brief A wrapper for an Image which contains some pending operations; these may
44  *  not be necessary if the receiver of the PlayerImage throws it away.
45  */
46 class PlayerImage
47 {
48 public:
49         PlayerImage (boost::shared_ptr<const Image>, Crop, libdcp::Size, libdcp::Size, Scaler const *);
50
51         void set_subtitle (boost::shared_ptr<const Image>, Position<int>);
52         
53         boost::shared_ptr<Image> image (AVPixelFormat, bool);
54
55 private:
56         boost::shared_ptr<const Image> _in;
57         Crop _crop;
58         libdcp::Size _inter_size;
59         libdcp::Size _out_size;
60         Scaler const * _scaler;
61         boost::shared_ptr<const Image> _subtitle_image;
62         Position<int> _subtitle_position;
63 };
64
65 class PlayerStatistics
66 {
67 public:
68         struct Video {
69                 Video ()
70                         : black (0)
71                         , repeat (0)
72                         , good (0)
73                         , skip (0)
74                 {}
75                 
76                 int black;
77                 int repeat;
78                 int good;
79                 int skip;
80         } video;
81
82         struct Audio {
83                 Audio ()
84                         : silence (0)
85                         , good (0)
86                         , skip (0)
87                 {}
88                 
89                 int64_t silence;
90                 int64_t good;
91                 int64_t skip;
92         } audio;
93
94         void dump (boost::shared_ptr<Log>) const;
95 };
96  
97 /** @class Player
98  *  @brief A class which can `play' a Playlist; emitting its audio and video.
99  */
100
101 class Player : public boost::enable_shared_from_this<Player>, public boost::noncopyable
102 {
103 public:
104         Player (boost::shared_ptr<const Film>, boost::shared_ptr<const Playlist>);
105
106         void disable_video ();
107         void disable_audio ();
108
109         bool pass ();
110         void seek (DCPTime, bool);
111
112         DCPTime video_position () const {
113                 return _video_position;
114         }
115
116         void set_video_container_size (libdcp::Size);
117         void set_approximate_size ();
118
119         bool repeat_last_video ();
120
121         PlayerStatistics const & statistics () const;
122         
123         /** Emitted when a video frame is ready.
124          *  First parameter is the video image.
125          *  Second parameter is the eye(s) that should see this image.
126          *  Third parameter is the colour conversion that should be used for this image.
127          *  Fourth parameter is true if the image is the same as the last one that was emitted.
128          *  Fifth parameter is the time.
129          */
130         boost::signals2::signal<void (boost::shared_ptr<PlayerImage>, Eyes, ColourConversion, bool, DCPTime)> Video;
131         
132         /** Emitted when some audio data is ready */
133         boost::signals2::signal<void (boost::shared_ptr<const AudioBuffers>, DCPTime)> Audio;
134
135         /** Emitted when something has changed such that if we went back and emitted
136          *  the last frame again it would look different.  This is not emitted after
137          *  a seek.
138          *
139          *  The parameter is true if these signals are currently likely to be frequent.
140          */
141         boost::signals2::signal<void (bool)> Changed;
142
143 private:
144         friend class PlayerWrapper;
145         friend class Piece;
146
147         void setup_pieces ();
148         void playlist_changed ();
149         void content_changed (boost::weak_ptr<Content>, int, bool);
150         void do_seek (DCPTime, bool);
151         void flush ();
152         void emit_black ();
153         void emit_silence (AudioFrame);
154         void film_changed (Film::Property);
155         void update_subtitle_from_image ();
156         void update_subtitle_from_text ();
157         void emit_video (boost::weak_ptr<Piece>, boost::shared_ptr<DecodedVideo>);
158         void emit_audio (boost::weak_ptr<Piece>, boost::shared_ptr<DecodedAudio>);
159         void step_video_position (boost::shared_ptr<DecodedVideo>);
160
161         boost::shared_ptr<const Film> _film;
162         boost::shared_ptr<const Playlist> _playlist;
163         
164         bool _video;
165         bool _audio;
166
167         /** Our pieces are ready to go; if this is false the pieces must be (re-)created before they are used */
168         bool _have_valid_pieces;
169         std::list<boost::shared_ptr<Piece> > _pieces;
170
171         /** The time after the last video that we emitted */
172         DCPTime _video_position;
173         /** The time after the last audio that we emitted */
174         DCPTime _audio_position;
175
176         AudioMerger<DCPTime, AudioFrame> _audio_merger;
177
178         libdcp::Size _video_container_size;
179         boost::shared_ptr<PlayerImage> _black_frame;
180
181         struct {
182                 boost::weak_ptr<Piece> piece;
183                 boost::shared_ptr<DecodedImageSubtitle> subtitle;
184         } _image_subtitle;
185
186         struct {
187                 boost::weak_ptr<Piece> piece;
188                 boost::shared_ptr<DecodedTextSubtitle> subtitle;
189         } _text_subtitle;
190         
191         struct {
192                 Position<int> position;
193                 boost::shared_ptr<Image> image;
194                 DCPTime from;
195                 DCPTime to;
196         } _out_subtitle;
197
198 #ifdef DCPOMATIC_DEBUG
199         boost::shared_ptr<Content> _last_video;
200 #endif
201
202         bool _last_emit_was_black;
203
204         struct {
205                 boost::weak_ptr<Piece> weak_piece;
206                 boost::shared_ptr<DecodedVideo> video;
207         } _last_incoming_video;
208
209         bool _just_did_inaccurate_seek;
210         bool _approximate_size;
211
212         PlayerStatistics _statistics;
213
214         boost::signals2::scoped_connection _playlist_changed_connection;
215         boost::signals2::scoped_connection _playlist_content_changed_connection;
216         boost::signals2::scoped_connection _film_changed_connection;
217 };
218
219 #endif