/* Copyright (C) 2013 Carl Hetherington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "player.h" #include "film.h" #include "ffmpeg_decoder.h" #include "ffmpeg_content.h" #include "imagemagick_decoder.h" #include "imagemagick_content.h" #include "sndfile_decoder.h" #include "sndfile_content.h" #include "playlist.h" #include "job.h" #include "image.h" #include "null_content.h" #include "black_decoder.h" #include "silence_decoder.h" using std::list; using std::cout; using std::min; using std::max; using std::vector; using boost::shared_ptr; using boost::weak_ptr; using boost::dynamic_pointer_cast; struct Piece { Piece (shared_ptr c, shared_ptr d) : content (c) , decoder (d) {} shared_ptr content; shared_ptr decoder; }; Player::Player (shared_ptr f, shared_ptr p) : _film (f) , _playlist (p) , _video (true) , _audio (true) , _subtitles (true) , _have_valid_pieces (false) , _position (0) , _audio_buffers (f->dcp_audio_channels(), 0) , _next_audio (0) { _playlist->Changed.connect (bind (&Player::playlist_changed, this)); _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2)); } void Player::disable_video () { _video = false; } void Player::disable_audio () { _audio = false; } void Player::disable_subtitles () { _subtitles = false; } bool Player::pass () { if (!_have_valid_pieces) { setup_pieces (); _have_valid_pieces = true; } /* Here we are just finding the active decoder with the earliest last emission time, then calling pass on it. */ Time earliest_t = TIME_MAX; shared_ptr earliest; for (list >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) { cout << "check " << (*i)->content->file() << " start=" << (*i)->content->start() << ", next=" << (*i)->decoder->next() << ", end=" << (*i)->content->end() << "\n"; if ((*i)->decoder->done ()) { continue; } if (!_audio && dynamic_pointer_cast ((*i)->decoder) && !dynamic_pointer_cast ((*i)->decoder)) { continue; } Time const t = (*i)->content->start() + (*i)->decoder->next(); if (t < earliest_t) { cout << "\t candidate; " << t << " " << (t / TIME_HZ) << ".\n"; earliest_t = t; earliest = *i; } } if (!earliest) { flush (); return true; } cout << "PASS:\n"; cout << "\tpass " << earliest->content->file() << " "; if (dynamic_pointer_cast (earliest->content)) { cout << " FFmpeg.\n"; } else if (dynamic_pointer_cast (earliest->content)) { cout << " ImageMagickContent.\n"; } else if (dynamic_pointer_cast (earliest->content)) { cout << " SndfileContent.\n"; } else if (dynamic_pointer_cast (earliest->decoder)) { cout << " Black.\n"; } else if (dynamic_pointer_cast (earliest->decoder)) { cout << " Silence.\n"; } earliest->decoder->pass (); _position = earliest->content->start() + earliest->decoder->next (); cout << "\tpassed to " << _position << " " << (_position / TIME_HZ) << "\n"; return false; } void Player::process_video (weak_ptr weak_content, shared_ptr image, bool same, Time time) { shared_ptr content = weak_content.lock (); if (!content) { return; } time += content->start (); Video (image, same, time); } void Player::process_audio (weak_ptr weak_content, shared_ptr audio, Time time) { shared_ptr content = weak_content.lock (); if (!content) { return; } /* The time of this audio may indicate that some of our buffered audio is not going to be added to any more, so it can be emitted. */ time += content->start (); if (time > _next_audio) { /* We can emit some audio from our buffers */ OutputAudioFrame const N = _film->time_to_audio_frames (time - _next_audio); assert (N <= _audio_buffers.frames()); shared_ptr emit (new AudioBuffers (_audio_buffers.channels(), N)); emit->copy_from (&_audio_buffers, N, 0, 0); Audio (emit, _next_audio); _next_audio += _film->audio_frames_to_time (N); /* And remove it from our buffers */ if (_audio_buffers.frames() > N) { _audio_buffers.move (N, 0, _audio_buffers.frames() - N); } _audio_buffers.set_frames (_audio_buffers.frames() - N); } /* Now accumulate the new audio into our buffers */ _audio_buffers.ensure_size (_audio_buffers.frames() + audio->frames()); _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ()); _audio_buffers.set_frames (_audio_buffers.frames() + audio->frames()); } void Player::flush () { if (_audio_buffers.frames() > 0) { shared_ptr emit (new AudioBuffers (_audio_buffers.channels(), _audio_buffers.frames())); emit->copy_from (&_audio_buffers, _audio_buffers.frames(), 0, 0); Audio (emit, _next_audio); _next_audio += _film->audio_frames_to_time (_audio_buffers.frames ()); _audio_buffers.set_frames (0); } } /** @return true on error */ void Player::seek (Time t) { if (!_have_valid_pieces) { setup_pieces (); _have_valid_pieces = true; } if (_pieces.empty ()) { return; } // cout << "seek to " << t << " " << (t / TIME_HZ) << "\n"; for (list >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) { Time s = t - (*i)->content->start (); s = max (static_cast