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"
29 using boost::shared_ptr;
31 Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
37 , _have_setup_decoders (false)
38 , _ffmpeg_decoder_done (false)
45 Player::disable_video ()
51 Player::disable_audio ()
57 Player::disable_subtitles ()
65 if (!_have_setup_decoders) {
67 _have_setup_decoders = true;
72 if (_playlist->video_from() == Playlist::VIDEO_FFMPEG || _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
73 if (!_ffmpeg_decoder_done) {
74 if (_ffmpeg_decoder->pass ()) {
75 _ffmpeg_decoder_done = true;
82 if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
83 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
84 if ((*_imagemagick_decoder)->pass ()) {
85 _imagemagick_decoder++;
88 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
100 Player::set_progress (shared_ptr<Job> job)
102 /* Assume progress can be divined from how far through the video we are */
103 switch (_playlist->video_from ()) {
104 case Playlist::VIDEO_NONE:
106 case Playlist::VIDEO_FFMPEG:
107 if (_playlist->video_length ()) {
108 job->set_progress (float(_ffmpeg_decoder->video_frame()) / _playlist->video_length ());
111 case Playlist::VIDEO_IMAGEMAGICK:
114 for (list<shared_ptr<ImageMagickDecoder> >::iterator i = _imagemagick_decoders.begin(); i != _imagemagick_decoders.end(); ++i) {
115 if (_imagemagick_decoder == i) {
116 job->set_progress (float (n) / _imagemagick_decoders.size ());
126 Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
132 Player::process_audio (shared_ptr<AudioBuffers> b)
137 /** @return true on error */
139 Player::seek (double t)
141 if (!_have_setup_decoders) {
143 _have_setup_decoders = true;
148 switch (_playlist->video_from()) {
149 case Playlist::VIDEO_NONE:
151 case Playlist::VIDEO_FFMPEG:
152 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek (t)) {
155 /* We're seeking, so all `all done' bets are off */
156 _ffmpeg_decoder_done = false;
158 case Playlist::VIDEO_IMAGEMAGICK:
159 /* Find the decoder that contains this position */
160 _imagemagick_decoder = _imagemagick_decoders.begin ();
161 while (_imagemagick_decoder != _imagemagick_decoders.end ()) {
162 double const this_length = (*_imagemagick_decoder)->video_length() / _film->video_frame_rate ();
163 if (t < this_length) {
167 ++_imagemagick_decoder;
170 if (_imagemagick_decoder != _imagemagick_decoders.end()) {
171 (*_imagemagick_decoder)->seek (t);
178 /* XXX: don't seek audio because we don't need to... */
184 Player::seek_to_last ()
186 if (!_have_setup_decoders) {
188 _have_setup_decoders = true;
193 switch (_playlist->video_from ()) {
194 case Playlist::VIDEO_NONE:
196 case Playlist::VIDEO_FFMPEG:
197 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek_to_last ()) {
201 /* We're seeking, so all `all done' bets are off */
202 _ffmpeg_decoder_done = false;
204 case Playlist::VIDEO_IMAGEMAGICK:
205 if ((*_imagemagick_decoder)->seek_to_last ()) {
211 /* XXX: don't seek audio because we don't need to... */
217 Player::setup_decoders ()
219 if ((_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) || (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG)) {
220 _ffmpeg_decoder.reset (
224 _video && _playlist->video_from() == Playlist::VIDEO_FFMPEG,
225 _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
226 _subtitles && _film->with_subtitles(),
232 if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
233 _ffmpeg_decoder->connect_video (shared_from_this ());
236 if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
237 _ffmpeg_decoder->connect_audio (shared_from_this ());
240 if (_video && _playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
241 list<shared_ptr<const ImageMagickContent> > ic = _playlist->imagemagick ();
242 for (list<shared_ptr<const ImageMagickContent> >::iterator i = ic.begin(); i != ic.end(); ++i) {
243 shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
244 _imagemagick_decoders.push_back (d);
245 d->connect_video (shared_from_this ());
248 _imagemagick_decoder = _imagemagick_decoders.begin ();
251 if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
252 list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
253 for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
254 shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
255 _sndfile_decoders.push_back (d);
256 d->connect_audio (shared_from_this ());
262 Player::disable_video_sync ()
268 Player::last_video_time () const
270 switch (_playlist->video_from ()) {
271 case Playlist::VIDEO_NONE:
273 case Playlist::VIDEO_FFMPEG:
274 return _ffmpeg_decoder->last_source_time ();
275 case Playlist::VIDEO_IMAGEMAGICK:
276 return (*_imagemagick_decoder)->last_source_time ();