Merge master; fix crash on new film.
[dcpomatic.git] / src / lib / player.cc
index db69c66d1377d1ceadd21fdf96776b10dabc9930..09f1f55a32cdc3a909448d37642278d59b5961ea 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    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)
-{
-       assert (fs->format);
-       
-       if (pipe (_mplayer_stdin) < 0) {
-               throw PlayError ("could not create pipe");
-       }
+#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"
 
-       if (pipe (_mplayer_stdout) < 0) {
-               throw PlayError ("could not create pipe");
-       }
+using std::list;
+using std::cout;
+using std::vector;
+using boost::shared_ptr;
+using boost::weak_ptr;
+using boost::dynamic_pointer_cast;
 
-       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);
+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)
+{
+       _playlist->Changed.connect (bind (&Player::playlist_changed, this));
+       _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
+}
 
-               char* p[] = { strdup ("TERM=xterm"), strdup ("DISPLAY=:0"), 0 };
-               environ = p;
+void
+Player::disable_video ()
+{
+       _video = false;
+}
 
-               stringstream s;
-               s << "/usr/local/bin/mplayer";
+void
+Player::disable_audio ()
+{
+       _audio = false;
+}
 
-               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::disable_subtitles ()
+{
+       _subtitles = false;
+}
 
-               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;
-               }
-               }
+bool
+Player::pass ()
+{
+       if (!_have_valid_decoders) {
+               setup_decoders ();
+               _have_valid_decoders = true;
+       }
+       
+       bool done = true;
+       
+       if (_video && _video_decoder < _video_decoders.size ()) {
 
-               vf << ",scale=" << screen_size.width << ":" << screen_size.height;
-               
-               pair<string, string> filters = Filter::ffmpeg_strings (fs->filters);
+               /* Run video decoder; this may also produce audio */
                
-               if (!filters.first.empty()) {
-                       vf << "," << filters.first;
+               if (_video_decoders[_video_decoder]->pass ()) {
+                       _video_decoder++;
                }
                
-               if (!filters.second.empty ()) {
-                       vf << ",pp=" << filters.second;
+               if (_video_decoder < _video_decoders.size ()) {
+                       done = false;
                }
                
-               s << " -vf " << vf.str();
-               s << " \"" << fs->content_path() << "\" ";
+       }
 
-               string cmd (s.str ());
+       if (!_video && _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG && _sequential_audio_decoder < _audio_decoders.size ()) {
 
-               vector<string> b = split_at_spaces_considering_quotes (cmd);
+               /* 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++;
+               }
                
-               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 (_sequential_audio_decoder < _audio_decoders.size ()) {
+                       done = false;
                }
-               cl[b.size()] = 0;
                
-               execv (cl[0], cl);
+       }
 
-               stringstream e;
-               e << "exec of mplayer failed " << strerror (errno);
-               throw PlayError (e.str ());
+       if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
                
-       } else {
-               _mplayer_pid = p;
-               command ("pause");
+               /* We're getting audio from SndfileContent */
+               
+               for (vector<shared_ptr<AudioDecoder> >::iterator i = _audio_decoders.begin(); i != _audio_decoders.end(); ++i) {
+                       if (!(*i)->pass ()) {
+                               done = false;
+                       }
+               }
 
-               _stdout_reader = new boost::thread (boost::bind (&Player::stdout_reader, this));
+               Audio (_audio_buffers, _audio_time.get());
+               _audio_buffers.reset ();
+               _audio_time = boost::none;
        }
+
+       return done;
 }
 
-Player::~Player ()
+void
+Player::set_progress (shared_ptr<Job> job)
 {
-       _stdout_reader_should_run = false;
-       _stdout_reader->join ();
-       delete _stdout_reader;
-       
-       close (_mplayer_stdin[0]);
-       close (_mplayer_stdout[1]);
-       kill (_mplayer_pid, SIGTERM);
+       /* Assume progress can be divined from how far through the video we are */
+
+       if (_video_decoder >= _video_decoders.size() || !_playlist->video_length()) {
+               return;
+       }
+
+       job->set_progress ((_video_start[_video_decoder] + _video_decoders[_video_decoder]->video_frame()) / _playlist->video_length ());
 }
 
 void
-Player::command (string c)
+Player::process_video (shared_ptr<const Image> i, bool same, shared_ptr<Subtitle> s, double t)
 {
-       char buf[64];
-       snprintf (buf, sizeof (buf), "%s\n", c.c_str ());
-       write (_mplayer_stdin[1], buf, strlen (buf));
+       Video (i, same, s, _video_start[_video_decoder] + t);
 }
 
 void
-Player::stdout_reader ()
+Player::process_audio (weak_ptr<const AudioContent> c, shared_ptr<const AudioBuffers> b, double t)
 {
-       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;
-                               }
-
-                               if (b[0] == "ANS_time_pos") {
-                                       set_position (atof (b[1].c_str ()));
-                               } else if (b[0] == "ANS_pause") {
-                                       set_paused (b[1] == "yes");
-                               }
-                       }
+       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;
+       }
+}
 
-               usleep (5e5);
+/** @return true on error */
+bool
+Player::seek (double t)
+{
+       if (!_have_valid_decoders) {
+               setup_decoders ();
+               _have_valid_decoders = true;
+       }
 
-               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 (_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 (_video_decoder < _video_decoders.size()) {
+               _video_decoders[_video_decoder]->seek (t);
+       } else {
+               return true;
+       }
+
+       /* XXX: don't seek audio because we don't need to... */
+
+       return false;
+}
+
+
+void
+Player::seek_back ()
+{
+       /* XXX */
 }
 
 void
-Player::set_position (float p)
+Player::seek_forward ()
 {
-       /* XXX: could be an atomic */
-       boost::mutex::scoped_lock lm (_state_mutex);
-       _position = p;
+       /* XXX */
 }
 
+
 void
-Player::set_paused (bool p)
+Player::setup_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;
+       
+       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;
+               }
+               
+               shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
+               if (ic) {
+                       video_content = ic;
+                       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);
+               }
+       }
+}
+
+double
+Player::last_video_time () const
 {
-       /* XXX: could be an atomic */
-       boost::mutex::scoped_lock lm (_state_mutex);
-       _paused = p;
+       return _video_start[_video_decoder] + _video_decoders[_video_decoder]->last_content_time ();
 }
 
-float
-Player::position () const
+void
+Player::content_changed (weak_ptr<Content> w, int p)
 {
-       boost::mutex::scoped_lock lm (_state_mutex);
-       return _position;
+       shared_ptr<Content> c = w.lock ();
+       if (!c) {
+               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;
+               }
+       }
 }
 
-bool
-Player::paused () const
+void
+Player::playlist_changed ()
 {
-       boost::mutex::scoped_lock lm (_state_mutex);
-       return _paused;
+       _have_valid_decoders = false;
 }