/*
- 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 "player.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"
-using namespace std;
-using namespace boost;
+using std::list;
+using std::cout;
+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)
+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)
{
- assert (fs->format);
+ _playlist->Changed.connect (bind (&Player::playlist_changed, this));
+ _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
+}
+
+void
+Player::disable_video ()
+{
+ _video = false;
+}
+
+void
+Player::disable_audio ()
+{
+ _audio = false;
+}
+
+void
+Player::disable_subtitles ()
+{
+ _subtitles = false;
+}
+
+bool
+Player::pass ()
+{
+ if (!_have_valid_decoders) {
+ setup_decoders ();
+ _have_valid_decoders = true;
+ }
+
+ bool done = true;
- if (pipe (_mplayer_stdin) < 0) {
- throw PlayError ("could not create pipe");
+ if (_video_decoder != _video_decoders.end ()) {
+ if ((*_video_decoder)->pass ()) {
+ _video_decoder++;
+ }
+
+ if (_video_decoder != _video_decoders.end ()) {
+ done = false;
+ }
}
- if (pipe (_mplayer_stdout) < 0) {
- throw PlayError ("could not create pipe");
+ if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
+ for (list<shared_ptr<SndfileDecoder> >::iterator i = _sndfile_decoders.begin(); i != _sndfile_decoders.end(); ++i) {
+ if (!(*i)->pass ()) {
+ done = false;
+ }
+ }
+
+ Audio (_audio_buffers, _audio_time.get());
+ _audio_buffers.reset ();
+ _audio_time = boost::none;
}
- if (pipe (_mplayer_stderr) < 0) {
- throw PlayError ("could not create pipe");
+ return done;
+}
+
+void
+Player::set_progress (shared_ptr<Job> job)
+{
+ /* Assume progress can be divined from how far through the video we are */
+
+ if (_video_decoder == _video_decoders.end() || !_playlist->video_length()) {
+ return;
}
- 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);
+ ContentVideoFrame p = 0;
+ list<shared_ptr<VideoDecoder> >::iterator i = _video_decoders.begin ();
+ while (i != _video_decoders.end() && i != _video_decoder) {
+ p += (*i)->video_length ();
+ }
- char* p[] = { strdup ("TERM=xterm"), strdup ("DISPLAY=:0"), 0 };
- environ = p;
+ job->set_progress (float ((*_video_decoder)->video_frame ()) / _playlist->video_length ());
+}
- stringstream s;
- s << "/usr/local/bin/mplayer";
+void
+Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s, double t)
+{
+ /* XXX: this time will need mangling to add on the offset of the start of the content */
+ Video (i, same, s, t);
+}
- s << " -vo x11 -noaspect -noautosub -nosub -vo x11 -noborder -slave -quiet -input nodefault-bindings:conf=/dev/null";
- s << " -sws " << fs->scaler->mplayer_id ();
+void
+Player::process_audio (weak_ptr<const AudioContent> c, shared_ptr<AudioBuffers> b, double t)
+{
+ /* XXX: this time will need mangling to add on the offset of the start of the content */
+ 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;
+ }
- stringstream vf;
-
- 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;
- }
+ 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));
}
+ }
- 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 (!filters.second.empty ()) {
- vf << ",pp=" << filters.second;
- }
-
- s << " -vf " << vf.str();
- s << " \"" << fs->content_path() << "\" ";
+ 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;
+ }
+}
- string cmd (s.str ());
+/** @return true on error */
+bool
+Player::seek (double t)
+{
+ if (!_have_valid_decoders) {
+ setup_decoders ();
+ _have_valid_decoders = 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 ());
+ /* Find the decoder that contains this position */
+ _video_decoder = _video_decoders.begin ();
+ while (_video_decoder != _video_decoders.end ()) {
+ double const this_length = double ((*_video_decoder)->video_length()) / _film->video_frame_rate ();
+ if (t < this_length) {
+ break;
}
- cl[b.size()] = 0;
-
- execv (cl[0], cl);
-
- stringstream e;
- e << "exec of mplayer failed " << strerror (errno);
- throw PlayError (e.str ());
-
+ t -= this_length;
+ ++_video_decoder;
+ }
+
+ if (_video_decoder != _video_decoders.end()) {
+ (*_video_decoder)->seek (t);
} else {
- _mplayer_pid = p;
- command ("pause");
-
- _stdout_reader = new boost::thread (boost::bind (&Player::stdout_reader, this));
+ return true;
}
+
+ /* XXX: don't seek audio because we don't need to... */
+
+ return false;
}
-Player::~Player ()
+
+void
+Player::seek_back ()
{
- _stdout_reader_should_run = false;
- _stdout_reader->join ();
- delete _stdout_reader;
-
- close (_mplayer_stdin[0]);
- close (_mplayer_stdout[1]);
- kill (_mplayer_pid, SIGTERM);
+ /* XXX */
}
void
-Player::command (string c)
+Player::seek_forward ()
{
- char buf[64];
- snprintf (buf, sizeof (buf), "%s\n", c.c_str ());
- write (_mplayer_stdin[1], buf, strlen (buf));
+ /* XXX */
}
+
void
-Player::stdout_reader ()
+Player::setup_decoders ()
{
- 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;
- }
+ _video_decoders.clear ();
+ _video_decoder = _video_decoders.end ();
+ _sndfile_decoders.clear ();
+
+ if (_video) {
+ list<shared_ptr<const VideoContent> > vc = _playlist->video ();
+ for (list<shared_ptr<const VideoContent> >::iterator i = vc.begin(); i != vc.end(); ++i) {
+
+ shared_ptr<VideoDecoder> d;
+
+ /* 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
+ )
+ );
- 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 (_playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
+ fd->Audio.connect (bind (&Player::process_audio, this, fc, _1, _2));
}
+
+ d = fd;
+ }
+
+ shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
+ if (ic) {
+ d.reset (new ImageMagickDecoder (_film, ic));
}
+
+ d->connect_video (shared_from_this ());
+ _video_decoders.push_back (d);
}
- usleep (5e5);
+ _video_decoder = _video_decoders.begin ();
+ }
- 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));
+ if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
+ list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
+ for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
+ shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
+ _sndfile_decoders.push_back (d);
+ d->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
+ }
}
}
-void
-Player::set_position (float p)
+double
+Player::last_video_time () const
{
- /* XXX: could be an atomic */
- boost::mutex::scoped_lock lm (_state_mutex);
- _position = p;
+ double t = 0;
+ for (list<shared_ptr<VideoDecoder> >::const_iterator i = _video_decoders.begin(); i != _video_decoder; ++i) {
+ t += (*i)->video_length() / (*i)->video_frame_rate ();
+ }
+
+ return t + (*_video_decoder)->last_content_time ();
}
void
-Player::set_paused (bool p)
+Player::content_changed (weak_ptr<Content> w, int p)
{
- /* XXX: could be an atomic */
- boost::mutex::scoped_lock lm (_state_mutex);
- _paused = p;
-}
+ shared_ptr<Content> c = w.lock ();
+ if (!c) {
+ return;
+ }
-float
-Player::position () const
-{
- boost::mutex::scoped_lock lm (_state_mutex);
- return _position;
+ 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;
+ }
+ }
}
-bool
-Player::paused () const
+void
+Player::playlist_changed ()
{
- boost::mutex::scoped_lock lm (_state_mutex);
- return _paused;
+ _have_valid_decoders = false;
}