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;
30 using boost::weak_ptr;
31 using boost::dynamic_pointer_cast;
33 Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
39 , _have_valid_decoders (false)
40 , _ffmpeg_decoder_done (false)
43 _playlist->Changed.connect (bind (&Player::playlist_changed, this));
44 _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
48 Player::disable_video ()
54 Player::disable_audio ()
60 Player::disable_subtitles ()
68 if (!_have_valid_decoders) {
70 _have_valid_decoders = true;
75 if (_playlist->video_from() == Playlist::VIDEO_FFMPEG || _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
76 if (!_ffmpeg_decoder_done) {
77 if (_ffmpeg_decoder->pass ()) {
78 _ffmpeg_decoder_done = true;
85 if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
86 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
87 if ((*_imagemagick_decoder)->pass ()) {
88 _imagemagick_decoder++;
91 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
103 Player::set_progress (shared_ptr<Job> job)
105 /* Assume progress can be divined from how far through the video we are */
106 switch (_playlist->video_from ()) {
107 case Playlist::VIDEO_NONE:
109 case Playlist::VIDEO_FFMPEG:
110 if (_playlist->video_length ()) {
111 job->set_progress (float(_ffmpeg_decoder->video_frame()) / _playlist->video_length ());
114 case Playlist::VIDEO_IMAGEMAGICK:
117 for (list<shared_ptr<ImageMagickDecoder> >::iterator i = _imagemagick_decoders.begin(); i != _imagemagick_decoders.end(); ++i) {
118 if (_imagemagick_decoder == i) {
119 job->set_progress (float (n) / _imagemagick_decoders.size ());
129 Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
135 Player::process_audio (shared_ptr<AudioBuffers> b)
140 /** @return true on error */
142 Player::seek (double t)
144 if (!_have_valid_decoders) {
146 _have_valid_decoders = true;
151 switch (_playlist->video_from()) {
152 case Playlist::VIDEO_NONE:
154 case Playlist::VIDEO_FFMPEG:
155 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek (t)) {
158 /* We're seeking, so all `all done' bets are off */
159 _ffmpeg_decoder_done = false;
161 case Playlist::VIDEO_IMAGEMAGICK:
162 /* Find the decoder that contains this position */
163 _imagemagick_decoder = _imagemagick_decoders.begin ();
164 while (_imagemagick_decoder != _imagemagick_decoders.end ()) {
165 double const this_length = (*_imagemagick_decoder)->video_length() / _film->video_frame_rate ();
166 if (t < this_length) {
170 ++_imagemagick_decoder;
173 if (_imagemagick_decoder != _imagemagick_decoders.end()) {
174 (*_imagemagick_decoder)->seek (t);
181 /* XXX: don't seek audio because we don't need to... */
187 Player::seek_to_last ()
189 if (!_have_valid_decoders) {
191 _have_valid_decoders = true;
196 switch (_playlist->video_from ()) {
197 case Playlist::VIDEO_NONE:
199 case Playlist::VIDEO_FFMPEG:
200 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek_to_last ()) {
204 /* We're seeking, so all `all done' bets are off */
205 _ffmpeg_decoder_done = false;
207 case Playlist::VIDEO_IMAGEMAGICK:
208 if ((*_imagemagick_decoder)->seek_to_last ()) {
214 /* XXX: don't seek audio because we don't need to... */
220 Player::setup_decoders ()
222 if ((_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) || (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG)) {
223 _ffmpeg_decoder.reset (
227 _video && _playlist->video_from() == Playlist::VIDEO_FFMPEG,
228 _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
229 _subtitles && _film->with_subtitles(),
235 if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
236 _ffmpeg_decoder->connect_video (shared_from_this ());
239 if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
240 _ffmpeg_decoder->connect_audio (shared_from_this ());
243 if (_video && _playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
244 list<shared_ptr<const ImageMagickContent> > ic = _playlist->imagemagick ();
245 for (list<shared_ptr<const ImageMagickContent> >::iterator i = ic.begin(); i != ic.end(); ++i) {
246 shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
247 _imagemagick_decoders.push_back (d);
248 d->connect_video (shared_from_this ());
251 _imagemagick_decoder = _imagemagick_decoders.begin ();
254 if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
255 list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
256 for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
257 shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
258 _sndfile_decoders.push_back (d);
259 d->connect_audio (shared_from_this ());
265 Player::disable_video_sync ()
271 Player::last_video_time () const
273 switch (_playlist->video_from ()) {
274 case Playlist::VIDEO_NONE:
276 case Playlist::VIDEO_FFMPEG:
277 return _ffmpeg_decoder->last_source_time ();
278 case Playlist::VIDEO_IMAGEMAGICK:
279 return (*_imagemagick_decoder)->last_source_time ();
286 Player::content_changed (weak_ptr<Content> w, int p)
288 shared_ptr<Content> c = w.lock ();
293 if (p == VideoContentProperty::VIDEO_LENGTH) {
294 if (dynamic_pointer_cast<FFmpegContent> (c)) {
295 _have_valid_decoders = false;
301 Player::playlist_changed ()
303 _have_valid_decoders = false;