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.
20 #include <boost/shared_ptr.hpp>
22 #include "sndfile_content.h"
23 #include "sndfile_decoder.h"
24 #include "ffmpeg_content.h"
25 #include "ffmpeg_decoder.h"
26 #include "imagemagick_content.h"
27 #include "imagemagick_decoder.h"
30 using boost::shared_ptr;
31 using boost::dynamic_pointer_cast;
34 : _video_from (VIDEO_NONE)
35 , _audio_from (AUDIO_NONE)
41 Playlist::setup (list<shared_ptr<Content> > content)
43 _video_from = VIDEO_NONE;
44 _audio_from = AUDIO_NONE;
46 for (list<shared_ptr<Content> >::const_iterator i = content.begin(); i != content.end(); ++i) {
47 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
51 _video_from = VIDEO_FFMPEG;
52 if (_audio_from == AUDIO_NONE) {
53 _audio_from = AUDIO_FFMPEG;
57 shared_ptr<ImageMagickContent> ic = dynamic_pointer_cast<ImageMagickContent> (*i);
59 _imagemagick.push_back (ic);
60 if (_video_from == VIDEO_NONE) {
61 _video_from = VIDEO_IMAGEMAGICK;
65 shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
67 _sndfile.push_back (sc);
68 _audio_from = AUDIO_SNDFILE;
74 Playlist::audio_length () const
76 switch (_audio_from) {
80 return _ffmpeg->audio_length ();
83 ContentAudioFrame l = 0;
84 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
85 l += (*i)->audio_length ();
95 Playlist::audio_channels () const
97 switch (_audio_from) {
101 return _ffmpeg->audio_channels ();
105 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
106 c += (*i)->audio_channels ();
116 Playlist::audio_frame_rate () const
118 switch (_audio_from) {
122 return _ffmpeg->audio_frame_rate ();
124 return _sndfile.front()->audio_frame_rate ();
131 Playlist::audio_channel_layout () const
133 switch (_audio_from) {
137 return _ffmpeg->audio_channel_layout ();
147 Playlist::video_frame_rate () const
149 switch (_video_from) {
153 return _ffmpeg->video_frame_rate ();
154 case VIDEO_IMAGEMAGICK:
162 Playlist::video_size () const
164 switch (_video_from) {
166 return libdcp::Size ();
168 return _ffmpeg->video_size ();
169 case VIDEO_IMAGEMAGICK:
171 return _imagemagick.front()->video_size ();
174 return libdcp::Size ();
178 Playlist::video_length () const
180 switch (_video_from) {
184 return _ffmpeg->video_length ();
185 case VIDEO_IMAGEMAGICK:
187 ContentVideoFrame l = 0;
188 for (list<shared_ptr<const ImageMagickContent> >::const_iterator i = _imagemagick.begin(); i != _imagemagick.end(); ++i) {
189 l += (*i)->video_length ();
199 Playlist::has_audio () const
201 return _audio_from != AUDIO_NONE;
204 Player::Player (boost::shared_ptr<const Film> f, boost::shared_ptr<const Playlist> p)
210 , _have_setup_decoders (false)
211 , _ffmpeg_decoder_done (false)
218 Player::disable_video ()
224 Player::disable_audio ()
230 Player::disable_subtitles ()
238 if (!_have_setup_decoders) {
240 _have_setup_decoders = true;
245 if (_playlist->video_from() == Playlist::VIDEO_FFMPEG || _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
246 if (!_ffmpeg_decoder_done) {
247 if (_ffmpeg_decoder->pass ()) {
248 _ffmpeg_decoder_done = true;
255 if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
256 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
257 if ((*_imagemagick_decoder)->pass ()) {
258 _imagemagick_decoder++;
261 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
273 Player::set_progress (shared_ptr<Job> job)
279 Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
285 Player::process_audio (shared_ptr<AudioBuffers> b)
291 Player::seek (double t)
295 switch (_playlist->video_from()) {
296 case Playlist::VIDEO_NONE:
298 case Playlist::VIDEO_FFMPEG:
299 if (_ffmpeg_decoder->seek (t)) {
303 case Playlist::VIDEO_IMAGEMAGICK:
304 if ((*_imagemagick_decoder)->seek (t)) {
310 /* XXX: don't seek audio because we don't need to... */
316 Player::seek_to_last ()
320 switch (_playlist->video_from ()) {
321 case Playlist::VIDEO_NONE:
323 case Playlist::VIDEO_FFMPEG:
324 if (_ffmpeg_decoder->seek_to_last ()) {
328 case Playlist::VIDEO_IMAGEMAGICK:
329 if ((*_imagemagick_decoder)->seek_to_last ()) {
335 /* XXX: don't seek audio because we don't need to... */
341 Player::setup_decoders ()
343 if ((_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) || (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG)) {
344 _ffmpeg_decoder.reset (
348 _video && _playlist->video_from() == Playlist::VIDEO_FFMPEG,
349 _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
350 _subtitles && _film->with_subtitles(),
356 if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
357 _ffmpeg_decoder->connect_video (shared_from_this ());
360 if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
361 _ffmpeg_decoder->connect_audio (shared_from_this ());
364 if (_video && _playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
365 list<shared_ptr<const ImageMagickContent> > ic = _playlist->imagemagick ();
366 for (list<shared_ptr<const ImageMagickContent> >::iterator i = ic.begin(); i != ic.end(); ++i) {
367 shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
368 _imagemagick_decoders.push_back (d);
369 d->connect_video (shared_from_this ());
372 _imagemagick_decoder = _imagemagick_decoders.begin ();
375 if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
376 list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
377 for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
378 shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
379 _sndfile_decoders.push_back (d);
380 d->connect_audio (shared_from_this ());
386 Player::disable_video_sync ()
392 Player::last_video_time () const
394 switch (_playlist->video_from ()) {
395 case Playlist::VIDEO_NONE:
397 case Playlist::VIDEO_FFMPEG:
398 return _ffmpeg_decoder->last_source_time ();
399 case Playlist::VIDEO_IMAGEMAGICK:
400 return (*_imagemagick_decoder)->last_source_time ();