1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
4 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "ffmpeg_decoder.h"
25 #include "ffmpeg_content.h"
26 #include "imagemagick_decoder.h"
27 #include "imagemagick_content.h"
28 #include "sndfile_decoder.h"
29 #include "sndfile_content.h"
33 #include "null_content.h"
34 #include "black_decoder.h"
35 #include "silence_decoder.h"
42 using boost::shared_ptr;
43 using boost::weak_ptr;
44 using boost::dynamic_pointer_cast;
48 Piece (shared_ptr<Content> c, shared_ptr<Decoder> d)
53 shared_ptr<Content> content;
54 shared_ptr<Decoder> decoder;
57 Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
63 , _have_valid_pieces (false)
65 , _audio_buffers (MAX_AUDIO_CHANNELS, 0)
68 _playlist->Changed.connect (bind (&Player::playlist_changed, this));
69 _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
73 Player::disable_video ()
79 Player::disable_audio ()
85 Player::disable_subtitles ()
93 if (!_have_valid_pieces) {
95 _have_valid_pieces = true;
98 /* Here we are just finding the active decoder with the earliest last emission time, then
102 Time earliest_t = TIME_MAX;
103 shared_ptr<Piece> earliest;
105 for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
106 cout << "check " << (*i)->content->file() << " start=" << (*i)->content->start() << ", next=" << (*i)->decoder->next() << ", end=" << (*i)->content->end() << "\n";
107 if (((*i)->decoder->next() + (*i)->content->start()) >= (*i)->content->end()) {
111 Time const t = (*i)->content->start() + (*i)->decoder->next();
112 if (t < earliest_t) {
113 cout << "\t candidate; " << t << " " << (t / TIME_HZ) << ".\n";
124 cout << "\tpass " << earliest->content->file() << " ";
125 if (dynamic_pointer_cast<FFmpegContent> (earliest->content)) {
126 cout << " FFmpeg.\n";
127 } else if (dynamic_pointer_cast<ImageMagickContent> (earliest->content)) {
128 cout << " ImageMagickContent.\n";
129 } else if (dynamic_pointer_cast<SndfileContent> (earliest->content)) {
130 cout << " SndfileContent.\n";
131 } else if (dynamic_pointer_cast<BlackDecoder> (earliest->decoder)) {
133 } else if (dynamic_pointer_cast<SilenceDecoder> (earliest->decoder)) {
134 cout << " Silence.\n";
137 earliest->decoder->pass ();
138 _position = earliest->content->start() + earliest->decoder->next ();
139 cout << "\tpassed to " << _position << " " << (_position / TIME_HZ) << "\n";
145 Player::process_video (weak_ptr<Content> weak_content, shared_ptr<const Image> image, bool same, shared_ptr<Subtitle> sub, Time time)
149 shared_ptr<Content> content = weak_content.lock ();
154 time += content->start ();
156 Video (image, same, sub, time);
160 Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuffers> audio, Time time)
162 shared_ptr<Content> content = weak_content.lock ();
169 /* The time of this audio may indicate that some of our buffered audio is not going to
170 be added to any more, so it can be emitted.
173 time += content->start ();
175 if (time > _next_audio) {
176 /* We can emit some audio from our buffers */
177 OutputAudioFrame const N = min (_film->time_to_audio_frames (time - _next_audio), static_cast<OutputAudioFrame> (_audio_buffers.frames()));
178 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
179 emit->copy_from (&_audio_buffers, N, 0, 0);
180 Audio (emit, _next_audio);
181 _next_audio += _film->audio_frames_to_time (N);
183 /* And remove it from our buffers */
184 if (_audio_buffers.frames() > N) {
185 _audio_buffers.move (N, 0, _audio_buffers.frames() - N);
187 _audio_buffers.set_frames (_audio_buffers.frames() - N);
190 /* Now accumulate the new audio into our buffers */
191 _audio_buffers.ensure_size (time - _next_audio + audio->frames());
192 _audio_buffers.accumulate (audio.get(), 0, _film->time_to_audio_frames (time - _next_audio));
195 /** @return true on error */
197 Player::seek (Time t)
199 if (!_have_valid_pieces) {
201 _have_valid_pieces = true;
204 if (_pieces.empty ()) {
208 cout << "seek to " << t << " " << (t / TIME_HZ) << "\n";
210 for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
211 Time s = t - (*i)->content->start ();
212 s = max (static_cast<Time> (0), s);
213 s = min ((*i)->content->length(), s);
214 cout << "seek [" << (*i)->content->file() << "," << (*i)->content->start() << "," << (*i)->content->end() << "] to " << s << "\n";
215 (*i)->decoder->seek (s);
218 /* XXX: don't seek audio because we don't need to... */
229 Player::seek_forward ()
236 bool operator() (shared_ptr<Content> a, shared_ptr<Content> b)
238 return a->start() < b->start();
243 Player::setup_pieces ()
245 // cout << "----- Player SETUP PIECES.\n";
247 list<shared_ptr<Piece> > old_pieces = _pieces;
251 Playlist::ContentList content = _playlist->content ();
252 sort (content.begin(), content.end(), ContentSorter ());
254 for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
256 shared_ptr<Decoder> decoder;
258 /* XXX: into content? */
260 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
262 shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio, _subtitles));
264 fd->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3, _4));
265 fd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
268 // cout << "\tFFmpeg @ " << fc->start() << " -- " << fc->end() << "\n";
271 shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
273 shared_ptr<ImageMagickDecoder> id;
275 /* See if we can re-use an old ImageMagickDecoder */
276 for (list<shared_ptr<Piece> >::const_iterator j = old_pieces.begin(); j != old_pieces.end(); ++j) {
277 shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> ((*j)->decoder);
278 if (imd && imd->content() == ic) {
284 id.reset (new ImageMagickDecoder (_film, ic));
285 id->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3, _4));
289 // cout << "\tImageMagick @ " << ic->start() << " -- " << ic->end() << "\n";
292 shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
294 shared_ptr<AudioDecoder> sd (new SndfileDecoder (_film, sc));
295 sd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
298 // cout << "\tSndfile @ " << sc->start() << " -- " << sc->end() << "\n";
301 _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder)));
304 /* Fill in visual gaps with black and audio gaps with silence */
308 list<shared_ptr<Piece> > pieces_copy = _pieces;
309 for (list<shared_ptr<Piece> >::iterator i = pieces_copy.begin(); i != pieces_copy.end(); ++i) {
310 if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
311 Time const diff = (*i)->content->start() - video_pos;
313 shared_ptr<NullContent> nc (new NullContent (_film, video_pos, diff));
314 shared_ptr<BlackDecoder> bd (new BlackDecoder (_film, nc));
315 bd->Video.connect (bind (&Player::process_video, this, nc, _1, _2, _3, _4));
316 _pieces.push_back (shared_ptr<Piece> (new Piece (nc, bd)));
317 // cout << "\tblack @ " << video_pos << " -- " << (video_pos + diff) << "\n";
320 video_pos = (*i)->content->end();
322 Time const diff = (*i)->content->start() - audio_pos;
324 shared_ptr<NullContent> nc (new NullContent (_film, audio_pos, diff));
325 shared_ptr<SilenceDecoder> sd (new SilenceDecoder (_film, nc));
326 sd->Audio.connect (bind (&Player::process_audio, this, nc, _1, _2));
327 _pieces.push_back (shared_ptr<Piece> (new Piece (nc, sd)));
328 // cout << "\tsilence @ " << audio_pos << " -- " << (audio_pos + diff) << "\n";
330 audio_pos = (*i)->content->end();
336 Player::content_changed (weak_ptr<Content> w, int p)
338 shared_ptr<Content> c = w.lock ();
343 if (p == ContentProperty::START || p == VideoContentProperty::VIDEO_LENGTH) {
344 _have_valid_pieces = false;
349 Player::playlist_changed ()
351 _have_valid_pieces = false;