Add interface to set up still image lengths.
[dcpomatic.git] / src / lib / player.cc
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 #include "player.h"
21 #include "film.h"
22 #include "ffmpeg_decoder.h"
23 #include "imagemagick_decoder.h"
24 #include "sndfile_decoder.h"
25 #include "playlist.h"
26 #include "job.h"
27
28 using std::list;
29 using boost::shared_ptr;
30 using boost::weak_ptr;
31 using boost::dynamic_pointer_cast;
32
33 Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
34         : _film (f)
35         , _playlist (p)
36         , _video (true)
37         , _audio (true)
38         , _subtitles (true)
39         , _have_valid_decoders (false)
40         , _ffmpeg_decoder_done (false)
41         , _video_sync (true)
42 {
43         _playlist->Changed.connect (bind (&Player::playlist_changed, this));
44         _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
45 }
46
47 void
48 Player::disable_video ()
49 {
50         _video = false;
51 }
52
53 void
54 Player::disable_audio ()
55 {
56         _audio = false;
57 }
58
59 void
60 Player::disable_subtitles ()
61 {
62         _subtitles = false;
63 }
64
65 bool
66 Player::pass ()
67 {
68         if (!_have_valid_decoders) {
69                 setup_decoders ();
70                 _have_valid_decoders = true;
71         }
72         
73         bool done = true;
74         
75         if (_playlist->video_from() == Playlist::VIDEO_FFMPEG || _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
76                 if (!_ffmpeg_decoder_done) {
77                         if (_ffmpeg_decoder->pass ()) {
78                                 _ffmpeg_decoder_done = true;
79                         } else {
80                                 done = false;
81                         }
82                 }
83         }
84
85         if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
86                 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
87                         if ((*_imagemagick_decoder)->pass ()) {
88                                 _imagemagick_decoder++;
89                         }
90
91                         if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
92                                 done = false;
93                         }
94                 }
95         }
96
97         /* XXX: sndfile */
98
99         return done;
100 }
101
102 void
103 Player::set_progress (shared_ptr<Job> job)
104 {
105         /* Assume progress can be divined from how far through the video we are */
106         switch (_playlist->video_from ()) {
107         case Playlist::VIDEO_NONE:
108                 break;
109         case Playlist::VIDEO_FFMPEG:
110                 if (_playlist->video_length ()) {
111                         job->set_progress (float(_ffmpeg_decoder->video_frame()) / _playlist->video_length ());
112                 }
113                 break;
114         case Playlist::VIDEO_IMAGEMAGICK:
115         {
116                 int n = 0;
117                 for (list<shared_ptr<ImageMagickDecoder> >::iterator i = _imagemagick_decoders.begin(); i != _imagemagick_decoders.end(); ++i) {
118                         if (_imagemagick_decoder == i) {
119                                 job->set_progress (float (n) / _imagemagick_decoders.size ());
120                         }
121                         ++n;
122                 }
123                 break;
124         }
125         }
126 }
127
128 void
129 Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
130 {
131         Video (i, same, s);
132 }
133
134 void
135 Player::process_audio (shared_ptr<AudioBuffers> b)
136 {
137         Audio (b);
138 }
139
140 /** @return true on error */
141 bool
142 Player::seek (double t)
143 {
144         if (!_have_valid_decoders) {
145                 setup_decoders ();
146                 _have_valid_decoders = true;
147         }
148         
149         bool r = false;
150
151         switch (_playlist->video_from()) {
152         case Playlist::VIDEO_NONE:
153                 break;
154         case Playlist::VIDEO_FFMPEG:
155                 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek (t)) {
156                         r = true;
157                 }
158                 /* We're seeking, so all `all done' bets are off */
159                 _ffmpeg_decoder_done = false;
160                 break;
161         case Playlist::VIDEO_IMAGEMAGICK:
162                 /* Find the decoder that contains this position */
163                 _imagemagick_decoder = _imagemagick_decoders.begin ();
164                 while (_imagemagick_decoder != _imagemagick_decoders.end ()) {
165                         double const this_length = (*_imagemagick_decoder)->video_length() / _film->video_frame_rate ();
166                         if (t < this_length) {
167                                 break;
168                         }
169                         t -= this_length;
170                         ++_imagemagick_decoder;
171                 }
172
173                 if (_imagemagick_decoder != _imagemagick_decoders.end()) {
174                         (*_imagemagick_decoder)->seek (t);
175                 } else {
176                         r = true;
177                 }
178                 break;
179         }
180
181         /* XXX: don't seek audio because we don't need to... */
182
183         return r;
184 }
185
186 bool
187 Player::seek_to_last ()
188 {
189         if (!_have_valid_decoders) {
190                 setup_decoders ();
191                 _have_valid_decoders = true;
192         }
193
194         bool r = false;
195         
196         switch (_playlist->video_from ()) {
197         case Playlist::VIDEO_NONE:
198                 break;
199         case Playlist::VIDEO_FFMPEG:
200                 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek_to_last ()) {
201                         r = true;
202                 }
203
204                 /* We're seeking, so all `all done' bets are off */
205                 _ffmpeg_decoder_done = false;
206                 break;
207         case Playlist::VIDEO_IMAGEMAGICK:
208                 if ((*_imagemagick_decoder)->seek_to_last ()) {
209                         r = true;
210                 }
211                 break;
212         }
213
214         /* XXX: don't seek audio because we don't need to... */
215
216         return r;
217 }
218
219 void
220 Player::setup_decoders ()
221 {
222         if ((_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) || (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG)) {
223                 _ffmpeg_decoder.reset (
224                         new FFmpegDecoder (
225                                 _film,
226                                 _playlist->ffmpeg(),
227                                 _video && _playlist->video_from() == Playlist::VIDEO_FFMPEG,
228                                 _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
229                                 _subtitles && _film->with_subtitles(),
230                                 _video_sync
231                                 )
232                         );
233         }
234         
235         if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
236                 _ffmpeg_decoder->connect_video (shared_from_this ());
237         }
238
239         if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
240                 _ffmpeg_decoder->connect_audio (shared_from_this ());
241         }
242
243         if (_video && _playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
244                 list<shared_ptr<const ImageMagickContent> > ic = _playlist->imagemagick ();
245                 for (list<shared_ptr<const ImageMagickContent> >::iterator i = ic.begin(); i != ic.end(); ++i) {
246                         shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
247                         _imagemagick_decoders.push_back (d);
248                         d->connect_video (shared_from_this ());
249                 }
250
251                 _imagemagick_decoder = _imagemagick_decoders.begin ();
252         }
253
254         if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
255                 list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
256                 for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
257                         shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
258                         _sndfile_decoders.push_back (d);
259                         d->connect_audio (shared_from_this ());
260                 }
261         }
262 }
263
264 void
265 Player::disable_video_sync ()
266 {
267         _video_sync = false;
268 }
269
270 double
271 Player::last_video_time () const
272 {
273         switch (_playlist->video_from ()) {
274         case Playlist::VIDEO_NONE:
275                 return 0;
276         case Playlist::VIDEO_FFMPEG:
277                 return _ffmpeg_decoder->last_source_time ();
278         case Playlist::VIDEO_IMAGEMAGICK:
279                 return (*_imagemagick_decoder)->last_source_time ();
280         }
281
282         return 0;
283 }
284
285 void
286 Player::content_changed (weak_ptr<Content> w, int p)
287 {
288         shared_ptr<Content> c = w.lock ();
289         if (!c) {
290                 return;
291         }
292
293         if (p == VideoContentProperty::VIDEO_LENGTH) {
294                 if (dynamic_pointer_cast<FFmpegContent> (c)) {
295                         _have_valid_decoders = false;
296                 }
297         }
298 }
299
300 void
301 Player::playlist_changed ()
302 {
303         _have_valid_decoders = false;
304 }