/*
- Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
*/
-#include <sstream>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <signal.h>
-#include <fcntl.h>
-#include <poll.h>
-#include <boost/thread.hpp>
-#include <boost/algorithm/string.hpp>
+#include <stdint.h>
#include "player.h"
-#include "film_state.h"
-#include "filter.h"
-#include "screen.h"
-#include "exceptions.h"
+#include "film.h"
+#include "ffmpeg_decoder.h"
+#include "ffmpeg_content.h"
+#include "imagemagick_decoder.h"
+#include "imagemagick_content.h"
+#include "sndfile_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 namespace std;
-using namespace boost;
+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;
-Player::Player (shared_ptr<const FilmState> fs, shared_ptr<const Screen> screen, Split split)
- : _stdout_reader_should_run (true)
- , _position (0)
- , _paused (false)
+struct Piece
{
- assert (fs->format);
+ Piece (shared_ptr<Content> c, shared_ptr<Decoder> d)
+ : content (c)
+ , decoder (d)
+ {}
- if (pipe (_mplayer_stdin) < 0) {
- throw PlayError ("could not create pipe");
- }
+ shared_ptr<Content> content;
+ shared_ptr<Decoder> decoder;
+};
- if (pipe (_mplayer_stdout) < 0) {
- throw PlayError ("could not create pipe");
- }
+Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
+ : _film (f)
+ , _playlist (p)
+ , _video (true)
+ , _audio (true)
+ , _subtitles (true)
+ , _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));
+}
- if (pipe (_mplayer_stderr) < 0) {
- throw PlayError ("could not create pipe");
- }
-
- int const p = fork ();
- if (p < 0) {
- throw PlayError ("could not fork for mplayer");
- } else if (p == 0) {
- close (_mplayer_stdin[1]);
- dup2 (_mplayer_stdin[0], STDIN_FILENO);
-
- close (_mplayer_stdout[0]);
- dup2 (_mplayer_stdout[1], STDOUT_FILENO);
-
- close (_mplayer_stderr[0]);
- dup2 (_mplayer_stderr[1], STDERR_FILENO);
+void
+Player::disable_video ()
+{
+ _video = false;
+}
+
+void
+Player::disable_audio ()
+{
+ _audio = false;
+}
+
+void
+Player::disable_subtitles ()
+{
+ _subtitles = false;
+}
- char* p[] = { strdup ("TERM=xterm"), strdup ("DISPLAY=:0"), 0 };
- environ = p;
+bool
+Player::pass ()
+{
+ if (!_have_valid_pieces) {
+ setup_pieces ();
+ _have_valid_pieces = true;
+ }
- stringstream s;
- s << "/usr/local/bin/mplayer";
+ /* Here we are just finding the active decoder with the earliest last emission time, then
+ calling pass on it.
+ */
- s << " -vo x11 -noaspect -noautosub -nosub -vo x11 -noborder -slave -quiet -input nodefault-bindings:conf=/dev/null";
- s << " -sws " << fs->scaler->mplayer_id ();
+ Time earliest_t = TIME_MAX;
+ shared_ptr<Piece> earliest;
- stringstream vf;
+ for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
+ cout << "check " << (*i)->content->file()
+ << " start=" << (*i)->content->start()
+ << ", next=" << (*i)->decoder->next()
+ << ", end=" << (*i)->content->end() << "\n";
- Position position = screen->position (fs->format);
- Size screen_size = screen->size (fs->format);
- Size const cropped_size = fs->cropped_size (fs->size);
- switch (split) {
- case SPLIT_NONE:
- vf << crop_string (Position (fs->left_crop, fs->top_crop), cropped_size);
- s << " -geometry " << position.x << ":" << position.y;
- break;
- case SPLIT_LEFT:
- {
- Size split_size = cropped_size;
- split_size.width /= 2;
- vf << crop_string (Position (fs->left_crop, fs->top_crop), split_size);
- screen_size.width /= 2;
- s << " -geometry " << position.x << ":" << position.y;
- break;
- }
- case SPLIT_RIGHT:
- {
- Size split_size = cropped_size;
- split_size.width /= 2;
- vf << crop_string (Position (fs->left_crop + split_size.width, fs->top_crop), split_size);
- screen_size.width /= 2;
- s << " -geometry " << (position.x + screen_size.width) << ":" << position.y;
- break;
- }
+ if ((*i)->decoder->done ()) {
+ continue;
}
- vf << ",scale=" << screen_size.width << ":" << screen_size.height;
-
- pair<string, string> filters = Filter::ffmpeg_strings (fs->filters);
-
- if (!filters.first.empty()) {
- vf << "," << filters.first;
+ if (!_audio && dynamic_pointer_cast<AudioDecoder> ((*i)->decoder) && !dynamic_pointer_cast<VideoDecoder> ((*i)->decoder)) {
+ continue;
}
- if (!filters.second.empty ()) {
- vf << ",pp=" << filters.second;
+ Time const t = (*i)->content->start() + (*i)->decoder->next();
+ if (t < earliest_t) {
+ cout << "\t candidate; " << t << " " << (t / TIME_HZ) << ".\n";
+ earliest_t = t;
+ earliest = *i;
}
-
- s << " -vf " << vf.str();
- s << " \"" << fs->content_path() << "\" ";
+ }
- string cmd (s.str ());
+ if (!earliest) {
+ flush ();
+ return true;
+ }
- vector<string> b = split_at_spaces_considering_quotes (cmd);
-
- char** cl = new char*[b.size() + 1];
- for (vector<string>::size_type i = 0; i < b.size(); ++i) {
- cl[i] = strdup (b[i].c_str ());
- }
- cl[b.size()] = 0;
-
- execv (cl[0], cl);
+ cout << "PASS:\n";
+ cout << "\tpass " << earliest->content->file() << " ";
+ if (dynamic_pointer_cast<FFmpegContent> (earliest->content)) {
+ cout << " FFmpeg.\n";
+ } else if (dynamic_pointer_cast<ImageMagickContent> (earliest->content)) {
+ cout << " ImageMagickContent.\n";
+ } else if (dynamic_pointer_cast<SndfileContent> (earliest->content)) {
+ cout << " SndfileContent.\n";
+ } else if (dynamic_pointer_cast<BlackDecoder> (earliest->decoder)) {
+ cout << " Black.\n";
+ } else if (dynamic_pointer_cast<SilenceDecoder> (earliest->decoder)) {
+ cout << " Silence.\n";
+ }
+
+ earliest->decoder->pass ();
+ _position = earliest->content->start() + earliest->decoder->next ();
+ cout << "\tpassed to " << _position << " " << (_position / TIME_HZ) << "\n";
- stringstream e;
- e << "exec of mplayer failed " << strerror (errno);
- throw PlayError (e.str ());
-
- } else {
- _mplayer_pid = p;
- command ("pause");
+ return false;
+}
- _stdout_reader = new boost::thread (boost::bind (&Player::stdout_reader, this));
+void
+Player::process_video (weak_ptr<Content> weak_content, shared_ptr<const Image> image, bool same, Time time)
+{
+ shared_ptr<Content> content = weak_content.lock ();
+ if (!content) {
+ return;
}
+
+ time += content->start ();
+
+ Video (image, same, time);
}
-Player::~Player ()
+void
+Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuffers> audio, Time time)
{
- _stdout_reader_should_run = false;
- _stdout_reader->join ();
- delete _stdout_reader;
+ shared_ptr<Content> content = weak_content.lock ();
+ if (!content) {
+ return;
+ }
- close (_mplayer_stdin[0]);
- close (_mplayer_stdout[1]);
- kill (_mplayer_pid, SIGTERM);
+ /* 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 ();
+
+ 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::command (string c)
+Player::flush ()
{
- char buf[64];
- snprintf (buf, sizeof (buf), "%s\n", c.c_str ());
- write (_mplayer_stdin[1], buf, strlen (buf));
+ 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 */
void
-Player::stdout_reader ()
-{
- while (_stdout_reader_should_run) {
- char buf[1024];
- int r = read (_mplayer_stdout[0], buf, sizeof (buf));
- if (r > 0) {
- stringstream s (buf);
- while (s.good ()) {
- string line;
- getline (s, line);
-
- vector<string> b;
- split (b, line, is_any_of ("="));
- if (b.size() < 2) {
- continue;
- }
+Player::seek (Time t)
+{
+ if (!_have_valid_pieces) {
+ setup_pieces ();
+ _have_valid_pieces = true;
+ }
- if (b[0] == "ANS_time_pos") {
- set_position (atof (b[1].c_str ()));
- } else if (b[0] == "ANS_pause") {
- set_paused (b[1] == "yes");
- }
- }
- }
+ if (_pieces.empty ()) {
+ return;
+ }
- usleep (5e5);
+// cout << "seek to " << t << " " << (t / TIME_HZ) << "\n";
- snprintf (buf, sizeof (buf), "pausing_keep_force get_property time_pos\npausing_keep_force get_property pause\n");
- write (_mplayer_stdin[1], buf, strlen (buf));
+ 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);
+// cout << "seek [" << (*i)->content->file() << "," << (*i)->content->start() << "," << (*i)->content->end() << "] to " << s << "\n";
+ (*i)->decoder->seek (s);
}
+
+ /* XXX: don't seek audio because we don't need to... */
}
+
void
-Player::set_position (float p)
+Player::seek_back ()
{
- /* XXX: could be an atomic */
- boost::mutex::scoped_lock lm (_state_mutex);
- _position = p;
+
}
void
-Player::set_paused (bool p)
+Player::seek_forward ()
{
- /* XXX: could be an atomic */
- boost::mutex::scoped_lock lm (_state_mutex);
- _paused = p;
+
}
-float
-Player::position () const
+void
+Player::add_black_piece (Time s, Time len)
{
- boost::mutex::scoped_lock lm (_state_mutex);
- return _position;
+ 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)));
+ cout << "\tblack @ " << s << " -- " << (s + len) << "\n";
}
-bool
-Player::paused () const
+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)));
+ cout << "\tsilence @ " << s << " -- " << (s + len) << "\n";
+}
+
+
+void
+Player::setup_pieces ()
+{
+ cout << "----- Player SETUP PIECES.\n";
+
+ list<shared_ptr<Piece> > old_pieces = _pieces;
+
+ _pieces.clear ();
+
+ Playlist::ContentList content = _playlist->content ();
+ sort (content.begin(), content.end(), ContentSorter ());
+
+ 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, _subtitles));
+
+ fd->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3));
+ fd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
+
+ decoder = fd;
+ cout << "\tFFmpeg @ " << fc->start() << " -- " << fc->end() << "\n";
+ }
+
+ shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
+ if (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));
+ }
+
+ decoder = id;
+ cout << "\tImageMagick @ " << ic->start() << " -- " << ic->end() << "\n";
+ }
+
+ 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;
+ cout << "\tSndfile @ " << sc->start() << " -- " << sc->end() << "\n";
+ }
+
+ _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder)));
+ }
+
+ /* 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();
+ }
+ }
+
+ 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);
+ }
+}
+
+void
+Player::content_changed (weak_ptr<Content> w, int p)
+{
+ shared_ptr<Content> c = w.lock ();
+ if (!c) {
+ return;
+ }
+
+ if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
+ _have_valid_pieces = false;
+ }
+}
+
+void
+Player::playlist_changed ()
{
- boost::mutex::scoped_lock lm (_state_mutex);
- return _paused;
+ _have_valid_pieces = false;
}