2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
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.
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.
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.
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"
31 #include "null_content.h"
32 #include "black_decoder.h"
33 #include "silence_decoder.h"
40 using boost::shared_ptr;
41 using boost::weak_ptr;
42 using boost::dynamic_pointer_cast;
46 Piece (shared_ptr<Content> c, shared_ptr<Decoder> d)
51 shared_ptr<Content> content;
52 shared_ptr<Decoder> decoder;
55 Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
61 , _have_valid_pieces (false)
63 , _audio_buffers (f->dcp_audio_channels(), 0)
66 _playlist->Changed.connect (bind (&Player::playlist_changed, this));
67 _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
71 Player::disable_video ()
77 Player::disable_audio ()
83 Player::disable_subtitles ()
91 if (!_have_valid_pieces) {
93 _have_valid_pieces = true;
96 /* Here we are just finding the active decoder with the earliest last emission time, then
100 Time earliest_t = TIME_MAX;
101 shared_ptr<Piece> earliest;
103 for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
104 cout << "check " << (*i)->content->file()
105 << " start=" << (*i)->content->start()
106 << ", next=" << (*i)->decoder->next()
107 << ", end=" << (*i)->content->end() << "\n";
109 if ((*i)->decoder->done ()) {
113 if (!_audio && dynamic_pointer_cast<AudioDecoder> ((*i)->decoder) && !dynamic_pointer_cast<VideoDecoder> ((*i)->decoder)) {
117 Time const t = (*i)->content->start() + (*i)->decoder->next();
118 if (t < earliest_t) {
119 cout << "\t candidate; " << t << " " << (t / TIME_HZ) << ".\n";
131 cout << "\tpass " << earliest->content->file() << " ";
132 if (dynamic_pointer_cast<FFmpegContent> (earliest->content)) {
133 cout << " FFmpeg.\n";
134 } else if (dynamic_pointer_cast<ImageMagickContent> (earliest->content)) {
135 cout << " ImageMagickContent.\n";
136 } else if (dynamic_pointer_cast<SndfileContent> (earliest->content)) {
137 cout << " SndfileContent.\n";
138 } else if (dynamic_pointer_cast<BlackDecoder> (earliest->decoder)) {
140 } else if (dynamic_pointer_cast<SilenceDecoder> (earliest->decoder)) {
141 cout << " Silence.\n";
144 earliest->decoder->pass ();
145 _position = earliest->content->start() + earliest->decoder->next ();
146 cout << "\tpassed to " << _position << " " << (_position / TIME_HZ) << "\n";
152 Player::process_video (weak_ptr<Content> weak_content, shared_ptr<const Image> image, bool same, Time time)
154 shared_ptr<Content> content = weak_content.lock ();
159 time += content->start ();
161 Video (image, same, time);
165 Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuffers> audio, Time time)
167 shared_ptr<Content> content = weak_content.lock ();
172 /* The time of this audio may indicate that some of our buffered audio is not going to
173 be added to any more, so it can be emitted.
176 time += content->start ();
178 if (time > _next_audio) {
179 /* We can emit some audio from our buffers */
180 OutputAudioFrame const N = _film->time_to_audio_frames (time - _next_audio);
181 assert (N <= _audio_buffers.frames());
182 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
183 emit->copy_from (&_audio_buffers, N, 0, 0);
184 Audio (emit, _next_audio);
185 _next_audio += _film->audio_frames_to_time (N);
187 /* And remove it from our buffers */
188 if (_audio_buffers.frames() > N) {
189 _audio_buffers.move (N, 0, _audio_buffers.frames() - N);
191 _audio_buffers.set_frames (_audio_buffers.frames() - N);
194 /* Now accumulate the new audio into our buffers */
195 _audio_buffers.ensure_size (_audio_buffers.frames() + audio->frames());
196 _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ());
197 _audio_buffers.set_frames (_audio_buffers.frames() + audio->frames());
203 if (_audio_buffers.frames() > 0) {
204 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), _audio_buffers.frames()));
205 emit->copy_from (&_audio_buffers, _audio_buffers.frames(), 0, 0);
206 Audio (emit, _next_audio);
207 _next_audio += _film->audio_frames_to_time (_audio_buffers.frames ());
208 _audio_buffers.set_frames (0);
212 /** @return true on error */
214 Player::seek (Time t)
216 if (!_have_valid_pieces) {
218 _have_valid_pieces = true;
221 if (_pieces.empty ()) {
225 // cout << "seek to " << t << " " << (t / TIME_HZ) << "\n";
227 for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
228 Time s = t - (*i)->content->start ();
229 s = max (static_cast<Time> (0), s);
230 s = min ((*i)->content->length(), s);
231 // cout << "seek [" << (*i)->content->file() << "," << (*i)->content->start() << "," << (*i)->content->end() << "] to " << s << "\n";
232 (*i)->decoder->seek (s);
235 /* XXX: don't seek audio because we don't need to... */
246 Player::seek_forward ()
252 Player::add_black_piece (Time s, Time len)
254 shared_ptr<NullContent> nc (new NullContent (_film, s, len));
255 nc->set_ratio (_film->container ());
256 shared_ptr<BlackDecoder> bd (new BlackDecoder (_film, nc));
257 bd->Video.connect (bind (&Player::process_video, this, nc, _1, _2, _3));
258 _pieces.push_back (shared_ptr<Piece> (new Piece (nc, bd)));
259 cout << "\tblack @ " << s << " -- " << (s + len) << "\n";
263 Player::add_silent_piece (Time s, Time len)
265 shared_ptr<NullContent> nc (new NullContent (_film, s, len));
266 shared_ptr<SilenceDecoder> sd (new SilenceDecoder (_film, nc));
267 sd->Audio.connect (bind (&Player::process_audio, this, nc, _1, _2));
268 _pieces.push_back (shared_ptr<Piece> (new Piece (nc, sd)));
269 cout << "\tsilence @ " << s << " -- " << (s + len) << "\n";
274 Player::setup_pieces ()
276 cout << "----- Player SETUP PIECES.\n";
278 list<shared_ptr<Piece> > old_pieces = _pieces;
282 Playlist::ContentList content = _playlist->content ();
283 sort (content.begin(), content.end(), ContentSorter ());
285 for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
287 shared_ptr<Decoder> decoder;
289 /* XXX: into content? */
291 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
293 shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio, _subtitles));
295 fd->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3));
296 fd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
299 cout << "\tFFmpeg @ " << fc->start() << " -- " << fc->end() << "\n";
302 shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
304 shared_ptr<ImageMagickDecoder> id;
306 /* See if we can re-use an old ImageMagickDecoder */
307 for (list<shared_ptr<Piece> >::const_iterator j = old_pieces.begin(); j != old_pieces.end(); ++j) {
308 shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> ((*j)->decoder);
309 if (imd && imd->content() == ic) {
315 id.reset (new ImageMagickDecoder (_film, ic));
316 id->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3));
320 cout << "\tImageMagick @ " << ic->start() << " -- " << ic->end() << "\n";
323 shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
325 shared_ptr<AudioDecoder> sd (new SndfileDecoder (_film, sc));
326 sd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
329 cout << "\tSndfile @ " << sc->start() << " -- " << sc->end() << "\n";
332 _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder)));
335 /* Fill in visual gaps with black and audio gaps with silence */
339 list<shared_ptr<Piece> > pieces_copy = _pieces;
340 for (list<shared_ptr<Piece> >::iterator i = pieces_copy.begin(); i != pieces_copy.end(); ++i) {
341 if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
342 Time const diff = (*i)->content->start() - video_pos;
344 add_black_piece (video_pos, diff);
346 video_pos = (*i)->content->end();
349 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> ((*i)->content);
350 if (ac && ac->audio_channels()) {
351 Time const diff = (*i)->content->start() - audio_pos;
353 add_silent_piece (video_pos, diff);
355 audio_pos = (*i)->content->end();
359 if (video_pos < audio_pos) {
360 add_black_piece (video_pos, audio_pos - video_pos);
361 } else if (audio_pos < video_pos) {
362 add_silent_piece (audio_pos, video_pos - audio_pos);
367 Player::content_changed (weak_ptr<Content> w, int p)
369 shared_ptr<Content> c = w.lock ();
374 if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
375 _have_valid_pieces = false;
380 Player::playlist_changed ()
382 _have_valid_pieces = false;