/*
- 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 "film_state.h"
-#include "filter.h"
-#include "screen.h"
-#include "exceptions.h"
-
-using namespace std;
-using namespace boost;
-
-Player::Player (shared_ptr<const FilmState> fs, shared_ptr<const Screen> screen, Split split)
- : _stdout_reader_should_run (true)
- , _position (0)
- , _paused (false)
+#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 std::list;
+using std::cout;
+using std::vector;
+using boost::shared_ptr;
+using boost::weak_ptr;
+using boost::dynamic_pointer_cast;
+
+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);
-
- if (pipe (_mplayer_stdin) < 0) {
- throw PlayError ("could not create pipe");
- }
+ _playlist->Changed.connect (bind (&Player::playlist_changed, this));
+ _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
+}
- if (pipe (_mplayer_stdout) < 0) {
- throw PlayError ("could not create pipe");
- }
+void
+Player::disable_video ()
+{
+ _video = false;
+}
+
+void
+Player::disable_audio ()
+{
+ _audio = false;
+}
+
+void
+Player::disable_subtitles ()
+{
+ _subtitles = false;
+}
- if (pipe (_mplayer_stderr) < 0) {
- throw PlayError ("could not create pipe");
+bool
+Player::pass ()
+{
+ if (!_have_valid_decoders) {
+ setup_decoders ();
+ _have_valid_decoders = true;
}
- 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);
+ bool done = true;
+
+ if (_video && _video_decoder < _video_decoders.size ()) {
- char* p[] = { strdup ("TERM=xterm"), strdup ("DISPLAY=:0"), 0 };
- environ = p;
+ /* Run video decoder; this may also produce audio */
- stringstream s;
- s << "/usr/local/bin/mplayer";
+ if (_video_decoders[_video_decoder]->pass ()) {
+ _video_decoder++;
+ }
+
+ if (_video_decoder < _video_decoders.size ()) {
+ done = false;
+ }
+
+ }
- s << " -vo x11 -noaspect -noautosub -nosub -vo x11 -noborder -slave -quiet -input nodefault-bindings:conf=/dev/null";
- s << " -sws " << fs->scaler->mplayer_id ();
+ if (!_video && _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG && _sequential_audio_decoder < _audio_decoders.size ()) {
- stringstream vf;
+ /* We're not producing video, so we may need to run FFmpeg content to get the audio */
- 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 (_audio_decoders[_sequential_audio_decoder]->pass ()) {
+ _sequential_audio_decoder++;
}
+
+ if (_sequential_audio_decoder < _audio_decoders.size ()) {
+ done = false;
}
+
+ }
- vf << ",scale=" << screen_size.width << ":" << screen_size.height;
+ if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
- pair<string, string> filters = Filter::ffmpeg_strings (fs->filters);
+ /* We're getting audio from SndfileContent */
- if (!filters.first.empty()) {
- vf << "," << filters.first;
+ for (vector<shared_ptr<AudioDecoder> >::iterator i = _audio_decoders.begin(); i != _audio_decoders.end(); ++i) {
+ if (!(*i)->pass ()) {
+ done = false;
+ }
}
-
- if (!filters.second.empty ()) {
- vf << ",pp=" << filters.second;
+
+ Audio (_audio_buffers, _audio_time.get());
+ _audio_buffers.reset ();
+ _audio_time = boost::none;
+ }
+
+ return done;
+}
+
+void
+Player::process_video (shared_ptr<const Image> i, bool same, shared_ptr<Subtitle> s, double t)
+{
+ Video (i, same, s, _video_start[_video_decoder] + t);
+}
+
+void
+Player::process_audio (weak_ptr<const AudioContent> c, shared_ptr<const AudioBuffers> b, double t)
+{
+ 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];
}
-
- s << " -vf " << vf.str();
- s << " \"" << fs->content_path() << "\" ";
+ }
- string cmd (s.str ());
+ 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));
+ }
+ }
- 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 ());
+ 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;
+ }
+}
+
+/** @return true on error */
+bool
+Player::seek (double t)
+{
+ if (!_have_valid_decoders) {
+ setup_decoders ();
+ _have_valid_decoders = 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;
}
- cl[b.size()] = 0;
-
- execv (cl[0], cl);
+ }
- stringstream e;
- e << "exec of mplayer failed " << strerror (errno);
- throw PlayError (e.str ());
-
+ if (_video_decoder < _video_decoders.size()) {
+ _video_decoders[_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;
+ vector<shared_ptr<VideoDecoder> > old_video_decoders = _video_decoders;
+
+ _video_decoders.clear ();
+ _video_decoder = 0;
+ _audio_decoders.clear ();
+ _sequential_audio_decoder = 0;
+
+ _video_start.clear();
+ _audio_start.clear();
+
+ double video_so_far = 0;
+ double audio_so_far = 0;
+
+ for (int l = 0; l < _playlist->loop(); ++l) {
+ 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? */
+
+ 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
+ )
+ );
+
+ video_content = fc;
+ audio_content = fc;
+ video_decoder = fd;
+ audio_decoder = fd;
+
+ video_decoder->connect_video (shared_from_this ());
+ }
+
+ shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
+ if (ic) {
+ video_content = ic;
+
+ /* See if we can re-use an old ImageMagickDecoder */
+ for (vector<shared_ptr<VideoDecoder> >::const_iterator i = old_video_decoders.begin(); i != old_video_decoders.end(); ++i) {
+ shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> (*i);
+ if (imd && imd->content() == ic) {
+ video_decoder = *i;
+ }
}
- 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 (!video_decoder) {
+ video_decoder.reset (new ImageMagickDecoder (_film, ic));
+ 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();
+ }
+ }
+
+ _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);
+ }
}
-
- usleep (5e5);
-
- 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));
}
}
-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;
+ if (_video_decoder >= _video_decoders.size ()) {
+ return 0;
+ }
+
+ return _video_start[_video_decoder] + _video_decoders[_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) {
+ _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;
}