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