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 "imagemagick_decoder.h"
24 #include "sndfile_decoder.h"
30 using boost::shared_ptr;
31 using boost::weak_ptr;
32 using boost::dynamic_pointer_cast;
34 Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
40 , _have_valid_decoders (false)
41 , _ffmpeg_decoder_done (false)
44 _playlist->Changed.connect (bind (&Player::playlist_changed, this));
45 _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
49 Player::disable_video ()
55 Player::disable_audio ()
61 Player::disable_subtitles ()
69 if (!_have_valid_decoders) {
71 _have_valid_decoders = true;
76 if (_playlist->video_from() == Playlist::VIDEO_FFMPEG || _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
77 if (!_ffmpeg_decoder_done) {
78 if (_ffmpeg_decoder->pass ()) {
79 _ffmpeg_decoder_done = true;
86 if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
87 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
88 if ((*_imagemagick_decoder)->pass ()) {
89 _imagemagick_decoder++;
92 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
104 Player::set_progress (shared_ptr<Job> job)
106 /* Assume progress can be divined from how far through the video we are */
107 switch (_playlist->video_from ()) {
108 case Playlist::VIDEO_NONE:
110 case Playlist::VIDEO_FFMPEG:
111 if (_playlist->video_length ()) {
112 job->set_progress (float(_ffmpeg_decoder->video_frame()) / _playlist->video_length ());
115 case Playlist::VIDEO_IMAGEMAGICK:
118 for (list<shared_ptr<ImageMagickDecoder> >::iterator i = _imagemagick_decoders.begin(); i != _imagemagick_decoders.end(); ++i) {
119 if (_imagemagick_decoder == i) {
120 job->set_progress (float (n) / _imagemagick_decoders.size ());
130 Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
136 Player::process_audio (shared_ptr<AudioBuffers> b)
141 /** @return true on error */
143 Player::seek (double t)
145 if (!_have_valid_decoders) {
147 _have_valid_decoders = true;
152 switch (_playlist->video_from()) {
153 case Playlist::VIDEO_NONE:
155 case Playlist::VIDEO_FFMPEG:
156 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek (t)) {
159 /* We're seeking, so all `all done' bets are off */
160 _ffmpeg_decoder_done = false;
162 case Playlist::VIDEO_IMAGEMAGICK:
163 /* Find the decoder that contains this position */
164 _imagemagick_decoder = _imagemagick_decoders.begin ();
165 while (_imagemagick_decoder != _imagemagick_decoders.end ()) {
166 double const this_length = (*_imagemagick_decoder)->video_length() / _film->video_frame_rate ();
167 if (t < this_length) {
171 ++_imagemagick_decoder;
174 if (_imagemagick_decoder != _imagemagick_decoders.end()) {
175 (*_imagemagick_decoder)->seek (t);
182 /* XXX: don't seek audio because we don't need to... */
188 Player::seek_to_last ()
190 if (!_have_valid_decoders) {
192 _have_valid_decoders = true;
197 switch (_playlist->video_from ()) {
198 case Playlist::VIDEO_NONE:
200 case Playlist::VIDEO_FFMPEG:
201 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek_to_last ()) {
205 /* We're seeking, so all `all done' bets are off */
206 _ffmpeg_decoder_done = false;
208 case Playlist::VIDEO_IMAGEMAGICK:
209 if ((*_imagemagick_decoder)->seek_to_last ()) {
215 /* XXX: don't seek audio because we don't need to... */
221 Player::setup_decoders ()
223 if ((_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) || (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG)) {
224 _ffmpeg_decoder.reset (
228 _video && _playlist->video_from() == Playlist::VIDEO_FFMPEG,
229 _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
230 _subtitles && _film->with_subtitles(),
236 if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
237 _ffmpeg_decoder->connect_video (shared_from_this ());
240 if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
241 _ffmpeg_decoder->connect_audio (shared_from_this ());
244 if (_video && _playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
245 list<shared_ptr<const ImageMagickContent> > ic = _playlist->imagemagick ();
246 for (list<shared_ptr<const ImageMagickContent> >::iterator i = ic.begin(); i != ic.end(); ++i) {
247 shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
248 _imagemagick_decoders.push_back (d);
249 d->connect_video (shared_from_this ());
252 _imagemagick_decoder = _imagemagick_decoders.begin ();
255 if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
256 list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
257 for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
258 shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
259 _sndfile_decoders.push_back (d);
260 d->connect_audio (shared_from_this ());
266 Player::disable_video_sync ()
272 Player::last_video_time () const
274 switch (_playlist->video_from ()) {
275 case Playlist::VIDEO_NONE:
277 case Playlist::VIDEO_FFMPEG:
278 return _ffmpeg_decoder->last_source_time ();
279 case Playlist::VIDEO_IMAGEMAGICK:
280 return (*_imagemagick_decoder)->last_source_time ();
287 Player::content_changed (weak_ptr<Content> w, int p)
289 shared_ptr<Content> c = w.lock ();
294 if (p == VideoContentProperty::VIDEO_LENGTH) {
295 if (dynamic_pointer_cast<FFmpegContent> (c)) {
296 _have_valid_decoders = false;
302 Player::playlist_changed ()
304 _have_valid_decoders = false;