Untested merge of master.
[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 "ffmpeg_content.h"
24 #include "imagemagick_decoder.h"
25 #include "imagemagick_content.h"
26 #include "sndfile_decoder.h"
27 #include "sndfile_content.h"
28 #include "playlist.h"
29 #include "job.h"
30
31 using std::list;
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35 using boost::dynamic_pointer_cast;
36
37 Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
38         : _film (f)
39         , _playlist (p)
40         , _video (true)
41         , _audio (true)
42         , _subtitles (true)
43         , _have_valid_decoders (false)
44 {
45         _playlist->Changed.connect (bind (&Player::playlist_changed, this));
46         _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
47 }
48
49 void
50 Player::disable_video ()
51 {
52         _video = false;
53 }
54
55 void
56 Player::disable_audio ()
57 {
58         _audio = false;
59 }
60
61 void
62 Player::disable_subtitles ()
63 {
64         _subtitles = false;
65 }
66
67 bool
68 Player::pass ()
69 {
70         if (!_have_valid_decoders) {
71                 setup_decoders ();
72                 _have_valid_decoders = true;
73         }
74         
75         bool done = true;
76         
77         if (_video_decoder != _video_decoders.end ()) {
78                 if ((*_video_decoder)->pass ()) {
79                         _video_decoder++;
80                 }
81                 
82                 if (_video_decoder != _video_decoders.end ()) {
83                         done = false;
84                 }
85         }
86
87         if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
88                 for (list<shared_ptr<SndfileDecoder> >::iterator i = _sndfile_decoders.begin(); i != _sndfile_decoders.end(); ++i) {
89                         if (!(*i)->pass ()) {
90                                 done = false;
91                         }
92                 }
93
94                 Audio (_audio_buffers, _audio_time.get());
95                 _audio_buffers.reset ();
96                 _audio_time = boost::none;
97         }
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
107         if (_video_decoder == _video_decoders.end() || !_playlist->video_length()) {
108                 return;
109         }
110         
111         ContentVideoFrame p = 0;
112         list<shared_ptr<VideoDecoder> >::iterator i = _video_decoders.begin ();
113         while (i != _video_decoders.end() && i != _video_decoder) {
114                 p += (*i)->video_length ();
115         }
116
117         job->set_progress (float ((*_video_decoder)->video_frame ()) / _playlist->video_length ());
118 }
119
120 void
121 Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s, double t)
122 {
123         /* XXX: this time will need mangling to add on the offset of the start of the content */
124         Video (i, same, s, t);
125 }
126
127 void
128 Player::process_audio (weak_ptr<const AudioContent> c, shared_ptr<AudioBuffers> b, double t)
129 {
130         /* XXX: this time will need mangling to add on the offset of the start of the content */
131         AudioMapping mapping = _film->audio_mapping ();
132         if (!_audio_buffers) {
133                 _audio_buffers.reset (new AudioBuffers (mapping.dcp_channels(), b->frames ()));
134                 _audio_buffers->make_silent ();
135                 _audio_time = t;
136         }
137
138         for (int i = 0; i < b->channels(); ++i) {
139                 list<libdcp::Channel> dcp = mapping.content_to_dcp (AudioMapping::Channel (c, i));
140                 for (list<libdcp::Channel>::iterator j = dcp.begin(); j != dcp.end(); ++j) {
141                         _audio_buffers->accumulate (b, i, static_cast<int> (*j));
142                 }
143         }
144
145         if (_playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
146                 /* We can just emit this audio now as it will all be here */
147                 Audio (_audio_buffers, t);
148                 _audio_buffers.reset ();
149                 _audio_time = boost::none;
150         }
151 }
152
153 /** @return true on error */
154 bool
155 Player::seek (double t)
156 {
157         if (!_have_valid_decoders) {
158                 setup_decoders ();
159                 _have_valid_decoders = true;
160         }
161
162         /* Find the decoder that contains this position */
163         _video_decoder = _video_decoders.begin ();
164         while (_video_decoder != _video_decoders.end ()) {
165                 double const this_length = double ((*_video_decoder)->video_length()) / _film->video_frame_rate ();
166                 if (t < this_length) {
167                         break;
168                 }
169                 t -= this_length;
170                 ++_video_decoder;
171         }
172         
173         if (_video_decoder != _video_decoders.end()) {
174                 (*_video_decoder)->seek (t);
175         } else {
176                 return true;
177         }
178
179         /* XXX: don't seek audio because we don't need to... */
180
181         return false;
182 }
183
184
185 void
186 Player::seek_back ()
187 {
188         /* XXX */
189 }
190
191 void
192 Player::seek_forward ()
193 {
194         /* XXX */
195 }
196
197
198 void
199 Player::setup_decoders ()
200 {
201         _video_decoders.clear ();
202         _video_decoder = _video_decoders.end ();
203         _sndfile_decoders.clear ();
204         
205         if (_video) {
206                 list<shared_ptr<const VideoContent> > vc = _playlist->video ();
207                 for (list<shared_ptr<const VideoContent> >::iterator i = vc.begin(); i != vc.end(); ++i) {
208
209                         shared_ptr<VideoDecoder> d;
210                         
211                         /* XXX: into content? */
212                         
213                         shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
214                         if (fc) {
215                                 shared_ptr<FFmpegDecoder> fd (
216                                         new FFmpegDecoder (
217                                                 _film, fc, _video,
218                                                 _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
219                                                 _subtitles
220                                                 )
221                                         );
222
223                                 if (_playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
224                                         fd->Audio.connect (bind (&Player::process_audio, this, fc, _1, _2));
225                                 }
226
227                                 d = fd;
228                         }
229
230                         shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
231                         if (ic) {
232                                 d.reset (new ImageMagickDecoder (_film, ic));
233                         }
234
235                         d->connect_video (shared_from_this ());
236                         _video_decoders.push_back (d);
237                 }
238
239                 _video_decoder = _video_decoders.begin ();
240         }
241
242         if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
243                 list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
244                 for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
245                         shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
246                         _sndfile_decoders.push_back (d);
247                         d->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
248                 }
249         }
250 }
251
252 double
253 Player::last_video_time () const
254 {
255         double t = 0;
256         for (list<shared_ptr<VideoDecoder> >::const_iterator i = _video_decoders.begin(); i != _video_decoder; ++i) {
257                 t += (*i)->video_length() / (*i)->video_frame_rate ();
258         }
259
260         return t + (*_video_decoder)->last_content_time ();
261 }
262
263 void
264 Player::content_changed (weak_ptr<Content> w, int p)
265 {
266         shared_ptr<Content> c = w.lock ();
267         if (!c) {
268                 return;
269         }
270
271         if (p == VideoContentProperty::VIDEO_LENGTH) {
272                 if (dynamic_pointer_cast<FFmpegContent> (c)) {
273                         /* FFmpeg content length changes are serious; we need new decoders */
274                         _have_valid_decoders = false;
275                 }
276         }
277 }
278
279 void
280 Player::playlist_changed ()
281 {
282         _have_valid_decoders = false;
283 }