*/
+#include <stdint.h>
#include "player.h"
#include "film.h"
#include "ffmpeg_decoder.h"
#include "sndfile_content.h"
#include "playlist.h"
#include "job.h"
+#include "image.h"
+#include "null_content.h"
+#include "black_decoder.h"
+#include "silence_decoder.h"
using std::list;
using std::cout;
+using std::min;
+using std::max;
using std::vector;
using boost::shared_ptr;
using boost::weak_ptr;
using boost::dynamic_pointer_cast;
+#define DEBUG_PLAYER 1
+
+struct Piece
+{
+ Piece (shared_ptr<Content> c, shared_ptr<Decoder> d)
+ : content (c)
+ , decoder (d)
+ {}
+
+ shared_ptr<Content> content;
+ shared_ptr<Decoder> decoder;
+};
+
+
+#ifdef DEBUG_PLAYER
+std::ostream& operator<<(std::ostream& s, Piece const & p)
+{
+ if (dynamic_pointer_cast<NullContent> (p.content)) {
+ if (dynamic_pointer_cast<SilenceDecoder> (p.decoder)) {
+ s << "\tsilence ";
+ } else {
+ s << "\tblack ";
+ }
+ } else if (dynamic_pointer_cast<FFmpegContent> (p.content)) {
+ s << "\tffmpeg ";
+ } else if (dynamic_pointer_cast<ImageMagickContent> (p.content)) {
+ s << "\timagemagick";
+ } else if (dynamic_pointer_cast<SndfileContent> (p.content)) {
+ s << "\tsndfile ";
+ }
+
+ s << " at " << p.content->start() << " until " << p.content->end();
+
+ return s;
+}
+#endif
+
Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
: _film (f)
, _playlist (p)
, _video (true)
, _audio (true)
- , _subtitles (true)
- , _have_valid_decoders (false)
+ , _have_valid_pieces (false)
+ , _position (0)
+ , _audio_buffers (f->dcp_audio_channels(), 0)
+ , _next_audio (0)
{
_playlist->Changed.connect (bind (&Player::playlist_changed, this));
_playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
_audio = false;
}
-void
-Player::disable_subtitles ()
-{
- _subtitles = false;
-}
-
bool
Player::pass ()
{
- if (!_have_valid_decoders) {
- setup_decoders ();
- _have_valid_decoders = true;
+ if (!_have_valid_pieces) {
+ setup_pieces ();
+ _have_valid_pieces = true;
}
-
- bool done = true;
-
- if (_video && _video_decoder < _video_decoders.size ()) {
- /* Run video decoder; this may also produce audio */
-
- if (_video_decoders[_video_decoder]->pass ()) {
- _video_decoder++;
- }
-
- if (_video_decoder < _video_decoders.size ()) {
- done = false;
- }
-
- }
+ /* Here we are just finding the active decoder with the earliest last emission time, then
+ calling pass on it.
+ */
- if (!_video && _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG && _sequential_audio_decoder < _audio_decoders.size ()) {
+ Time earliest_t = TIME_MAX;
+ shared_ptr<Piece> earliest;
- /* We're not producing video, so we may need to run FFmpeg content to get the audio */
-
- if (_audio_decoders[_sequential_audio_decoder]->pass ()) {
- _sequential_audio_decoder++;
- }
-
- if (_sequential_audio_decoder < _audio_decoders.size ()) {
- done = false;
+ for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
+ if ((*i)->decoder->done ()) {
+ continue;
}
-
- }
- if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
-
- /* We're getting audio from SndfileContent */
+ if (!_audio && dynamic_pointer_cast<AudioDecoder> ((*i)->decoder) && !dynamic_pointer_cast<VideoDecoder> ((*i)->decoder)) {
+ continue;
+ }
- for (vector<shared_ptr<AudioDecoder> >::iterator i = _audio_decoders.begin(); i != _audio_decoders.end(); ++i) {
- if (!(*i)->pass ()) {
- done = false;
- }
+ Time const t = (*i)->content->start() + (*i)->decoder->position();
+ if (t < earliest_t) {
+ earliest_t = t;
+ earliest = *i;
}
+ }
- Audio (_audio_buffers, _audio_time.get());
- _audio_buffers.reset ();
- _audio_time = boost::none;
+ if (!earliest) {
+ flush ();
+ return true;
}
- return done;
+ earliest->decoder->pass ();
+ _position = earliest->content->start() + earliest->decoder->position ();
+
+ return false;
}
void
-Player::set_progress (shared_ptr<Job> job)
+Player::process_video (weak_ptr<Content> weak_content, shared_ptr<const Image> image, bool same, Time time)
{
- /* Assume progress can be divined from how far through the video we are */
-
- if (_video_decoder >= _video_decoders.size() || !_playlist->video_length()) {
+ shared_ptr<Content> content = weak_content.lock ();
+ if (!content) {
return;
}
-
- job->set_progress ((_video_start[_video_decoder] + _video_decoders[_video_decoder]->video_frame()) / _playlist->video_length ());
+
+ time += content->start ();
+
+ Video (image, same, time);
}
void
-Player::process_video (shared_ptr<const Image> i, bool same, shared_ptr<Subtitle> s, double t)
+Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuffers> audio, Time time)
{
- Video (i, same, s, _video_start[_video_decoder] + t);
+ shared_ptr<Content> content = weak_content.lock ();
+ if (!content) {
+ return;
+ }
+
+ /* The time of this audio may indicate that some of our buffered audio is not going to
+ be added to any more, so it can be emitted.
+ */
+
+ time += content->start ();
+
+ cout << "Player gets " << audio->frames() << " @ " << time << " cf " << _next_audio << "\n";
+
+ if (time > _next_audio) {
+ /* We can emit some audio from our buffers */
+ OutputAudioFrame const N = _film->time_to_audio_frames (time - _next_audio);
+ assert (N <= _audio_buffers.frames());
+ shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
+ emit->copy_from (&_audio_buffers, N, 0, 0);
+ Audio (emit, _next_audio);
+ _next_audio += _film->audio_frames_to_time (N);
+
+ /* And remove it from our buffers */
+ if (_audio_buffers.frames() > N) {
+ _audio_buffers.move (N, 0, _audio_buffers.frames() - N);
+ }
+ _audio_buffers.set_frames (_audio_buffers.frames() - N);
+ }
+
+ /* Now accumulate the new audio into our buffers */
+ _audio_buffers.ensure_size (_audio_buffers.frames() + audio->frames());
+ _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ());
+ _audio_buffers.set_frames (_audio_buffers.frames() + audio->frames());
}
void
-Player::process_audio (weak_ptr<const AudioContent> c, shared_ptr<const AudioBuffers> b, double t)
+Player::flush ()
{
- AudioMapping mapping = _film->audio_mapping ();
- if (!_audio_buffers) {
- _audio_buffers.reset (new AudioBuffers (mapping.dcp_channels(), b->frames ()));
- _audio_buffers->make_silent ();
- _audio_time = t;
- if (_playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
- _audio_time = _audio_time.get() + _audio_start[_sequential_audio_decoder];
- }
- }
-
- for (int i = 0; i < b->channels(); ++i) {
- list<libdcp::Channel> dcp = mapping.content_to_dcp (AudioMapping::Channel (c, i));
- for (list<libdcp::Channel>::iterator j = dcp.begin(); j != dcp.end(); ++j) {
- _audio_buffers->accumulate (b, i, static_cast<int> (*j));
- }
- }
-
- if (_playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
- /* We can just emit this audio now as it will all be here */
- Audio (_audio_buffers, t);
- _audio_buffers.reset ();
- _audio_time = boost::none;
+ if (_audio_buffers.frames() > 0) {
+ shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), _audio_buffers.frames()));
+ emit->copy_from (&_audio_buffers, _audio_buffers.frames(), 0, 0);
+ Audio (emit, _next_audio);
+ _next_audio += _film->audio_frames_to_time (_audio_buffers.frames ());
+ _audio_buffers.set_frames (0);
}
}
/** @return true on error */
-bool
-Player::seek (double t)
+void
+Player::seek (Time t)
{
- if (!_have_valid_decoders) {
- setup_decoders ();
- _have_valid_decoders = true;
+ if (!_have_valid_pieces) {
+ setup_pieces ();
+ _have_valid_pieces = true;
}
- if (_video_decoders.empty ()) {
- return true;
- }
-
- /* Find the decoder that contains this position */
- _video_decoder = 0;
- while (1) {
- ++_video_decoder;
- if (_video_decoder >= _video_decoders.size () || t < _video_start[_video_decoder]) {
- --_video_decoder;
- t -= _video_start[_video_decoder];
- break;
- }
+ if (_pieces.empty ()) {
+ return;
}
- if (_video_decoder < _video_decoders.size()) {
- _video_decoders[_video_decoder]->seek (t);
- } else {
- return true;
+ for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
+ Time s = t - (*i)->content->start ();
+ s = max (static_cast<Time> (0), s);
+ s = min ((*i)->content->length(), s);
+ (*i)->decoder->seek (s);
}
/* XXX: don't seek audio because we don't need to... */
-
- return false;
}
void
Player::seek_back ()
{
- /* XXX */
+
}
void
Player::seek_forward ()
{
- /* XXX */
+
+}
+
+void
+Player::add_black_piece (Time s, Time len)
+{
+ shared_ptr<NullContent> nc (new NullContent (_film, s, len));
+ nc->set_ratio (_film->container ());
+ shared_ptr<BlackDecoder> bd (new BlackDecoder (_film, nc));
+ bd->Video.connect (bind (&Player::process_video, this, nc, _1, _2, _3));
+ _pieces.push_back (shared_ptr<Piece> (new Piece (nc, bd)));
+}
+
+void
+Player::add_silent_piece (Time s, Time len)
+{
+ shared_ptr<NullContent> nc (new NullContent (_film, s, len));
+ shared_ptr<SilenceDecoder> sd (new SilenceDecoder (_film, nc));
+ sd->Audio.connect (bind (&Player::process_audio, this, nc, _1, _2));
+ _pieces.push_back (shared_ptr<Piece> (new Piece (nc, sd)));
}
void
-Player::setup_decoders ()
+Player::setup_pieces ()
{
- _video_decoders.clear ();
- _video_decoder = 0;
- _audio_decoders.clear ();
- _sequential_audio_decoder = 0;
+ list<shared_ptr<Piece> > old_pieces = _pieces;
- _video_start.clear();
- _audio_start.clear();
+ _pieces.clear ();
- double video_so_far = 0;
- double audio_so_far = 0;
+ Playlist::ContentList content = _playlist->content ();
+ sort (content.begin(), content.end(), ContentSorter ());
- list<shared_ptr<const VideoContent> > vc = _playlist->video ();
- for (list<shared_ptr<const VideoContent> >::iterator i = vc.begin(); i != vc.end(); ++i) {
-
- shared_ptr<const VideoContent> video_content;
- shared_ptr<const AudioContent> audio_content;
- shared_ptr<VideoDecoder> video_decoder;
- shared_ptr<AudioDecoder> audio_decoder;
-
- /* XXX: into content? */
+ for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
+
+ shared_ptr<Decoder> decoder;
+ /* XXX: into content? */
+
shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
if (fc) {
- shared_ptr<FFmpegDecoder> fd (
- new FFmpegDecoder (
- _film, fc, _video,
- _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
- _subtitles
- )
- );
+ shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio));
- video_content = fc;
- audio_content = fc;
- video_decoder = fd;
- audio_decoder = fd;
+ fd->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3));
+ fd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
+ if (_video_container_size) {
+ fd->set_video_container_size (_video_container_size.get ());
+ }
+
+ decoder = fd;
}
shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
if (ic) {
- video_content = ic;
- video_decoder.reset (new ImageMagickDecoder (_film, ic));
+ shared_ptr<ImageMagickDecoder> id;
+
+ /* See if we can re-use an old ImageMagickDecoder */
+ for (list<shared_ptr<Piece> >::const_iterator j = old_pieces.begin(); j != old_pieces.end(); ++j) {
+ shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> ((*j)->decoder);
+ if (imd && imd->content() == ic) {
+ id = imd;
+ }
+ }
+
+ if (!id) {
+ id.reset (new ImageMagickDecoder (_film, ic));
+ id->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3));
+ if (_video_container_size) {
+ id->set_video_container_size (_video_container_size.get ());
+ }
+ }
+
+ decoder = id;
}
-
- video_decoder->connect_video (shared_from_this ());
- _video_decoders.push_back (video_decoder);
- _video_start.push_back (video_so_far);
- video_so_far += video_content->video_length() / video_content->video_frame_rate();
-
- if (audio_decoder && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
- audio_decoder->Audio.connect (bind (&Player::process_audio, this, audio_content, _1, _2));
- _audio_decoders.push_back (audio_decoder);
- _audio_start.push_back (audio_so_far);
- audio_so_far += double(audio_content->audio_length()) / audio_content->audio_frame_rate();
+
+ shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
+ if (sc) {
+ shared_ptr<AudioDecoder> sd (new SndfileDecoder (_film, sc));
+ sd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
+
+ decoder = sd;
}
+
+ _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder)));
}
-
- _video_decoder = 0;
- _sequential_audio_decoder = 0;
- if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
-
- list<shared_ptr<const AudioContent> > ac = _playlist->audio ();
- for (list<shared_ptr<const AudioContent> >::iterator i = ac.begin(); i != ac.end(); ++i) {
-
- shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
- assert (sc);
-
- shared_ptr<AudioDecoder> d (new SndfileDecoder (_film, sc));
- d->Audio.connect (bind (&Player::process_audio, this, sc, _1, _2));
- _audio_decoders.push_back (d);
- _audio_start.push_back (audio_so_far);
+ /* Fill in visual gaps with black and audio gaps with silence */
+
+ Time video_pos = 0;
+ Time audio_pos = 0;
+ list<shared_ptr<Piece> > pieces_copy = _pieces;
+ for (list<shared_ptr<Piece> >::iterator i = pieces_copy.begin(); i != pieces_copy.end(); ++i) {
+ if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
+ Time const diff = (*i)->content->start() - video_pos;
+ if (diff > 0) {
+ add_black_piece (video_pos, diff);
+ }
+ video_pos = (*i)->content->end();
+ }
+
+ shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> ((*i)->content);
+ if (ac && ac->audio_channels()) {
+ Time const diff = (*i)->content->start() - audio_pos;
+ if (diff > 0) {
+ add_silent_piece (video_pos, diff);
+ }
+ audio_pos = (*i)->content->end();
}
}
-}
-double
-Player::last_video_time () const
-{
- return _video_start[_video_decoder] + _video_decoders[_video_decoder]->last_content_time ();
+ if (video_pos < audio_pos) {
+ add_black_piece (video_pos, audio_pos - video_pos);
+ } else if (audio_pos < video_pos) {
+ add_silent_piece (audio_pos, video_pos - audio_pos);
+ }
+
+#ifdef DEBUG_PLAYER
+ cout << "=== Player setup:\n";
+ for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
+ cout << *(i->get()) << "\n";
+ }
+#endif
}
void
return;
}
- if (p == VideoContentProperty::VIDEO_LENGTH) {
- if (dynamic_pointer_cast<FFmpegContent> (c)) {
- /* FFmpeg content length changes are serious; we need new decoders */
- _have_valid_decoders = false;
- }
+ if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
+ _have_valid_pieces = false;
}
}
void
Player::playlist_changed ()
{
- _have_valid_decoders = false;
+ _have_valid_pieces = false;
+}
+
+void
+Player::set_video_container_size (libdcp::Size s)
+{
+ _video_container_size = s;
+ for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
+ shared_ptr<VideoDecoder> vd = dynamic_pointer_cast<VideoDecoder> ((*i)->decoder);
+ if (vd) {
+ vd->set_video_container_size (s);
+ }
+ }
}