*/
+#include <libcxml/cxml.h>
#include <boost/shared_ptr.hpp>
+#include <boost/lexical_cast.hpp>
#include "playlist.h"
#include "sndfile_content.h"
#include "sndfile_decoder.h"
-#include "ffmpeg_content.h"
+#include "video_content.h"
#include "ffmpeg_decoder.h"
-#include "imagemagick_content.h"
-#include "imagemagick_decoder.h"
+#include "ffmpeg_content.h"
+#include "still_image_decoder.h"
+#include "still_image_content.h"
+#include "content_factory.h"
+#include "job.h"
+#include "config.h"
+#include "util.h"
+
+#include "i18n.h"
using std::list;
using std::cout;
using std::vector;
+using std::min;
+using std::max;
+using std::string;
+using std::stringstream;
+using std::pair;
+using boost::optional;
using boost::shared_ptr;
+using boost::weak_ptr;
using boost::dynamic_pointer_cast;
+using boost::lexical_cast;
Playlist::Playlist ()
- : _video_from (VIDEO_NONE)
- , _audio_from (AUDIO_NONE)
+ : _sequence_video (true)
+ , _sequencing_video (false)
{
}
-void
-Playlist::setup (ContentList content)
+Playlist::~Playlist ()
{
- _video_from = VIDEO_NONE;
- _audio_from = AUDIO_NONE;
-
- _ffmpeg.reset ();
- _imagemagick.clear ();
- _sndfile.clear ();
-
- for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
- shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
- if (fc) {
- assert (!_ffmpeg);
- _ffmpeg = fc;
- _video_from = VIDEO_FFMPEG;
- if (_audio_from == AUDIO_NONE) {
- _audio_from = AUDIO_FFMPEG;
- }
- }
-
- shared_ptr<ImageMagickContent> ic = dynamic_pointer_cast<ImageMagickContent> (*i);
- if (ic) {
- _imagemagick.push_back (ic);
- if (_video_from == VIDEO_NONE) {
- _video_from = VIDEO_IMAGEMAGICK;
- }
- }
+ _content.clear ();
+ reconnect ();
+}
- shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
- if (sc) {
- _sndfile.push_back (sc);
- _audio_from = AUDIO_SNDFILE;
- }
+void
+Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
+{
+ if (property == ContentProperty::LENGTH) {
+ maybe_sequence_video ();
}
+
+ ContentChanged (content, property, frequent);
}
-ContentAudioFrame
-Playlist::audio_length () const
+void
+Playlist::maybe_sequence_video ()
{
- switch (_audio_from) {
- case AUDIO_NONE:
- return 0;
- case AUDIO_FFMPEG:
- return _ffmpeg->audio_length ();
- case AUDIO_SNDFILE:
- {
- ContentAudioFrame l = 0;
- for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
- l += (*i)->audio_length ();
- }
- return l;
+ if (!_sequence_video || _sequencing_video) {
+ return;
}
+
+ _sequencing_video = true;
+
+ ContentList cl = _content;
+ Time next = 0;
+ for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+ if (!dynamic_pointer_cast<VideoContent> (*i)) {
+ continue;
+ }
+
+ (*i)->set_position (next);
+ next = (*i)->end() + 1;
}
- return 0;
+ /* This won't change order, so it does not need a sort */
+
+ _sequencing_video = false;
}
-int
-Playlist::audio_channels () const
+string
+Playlist::video_identifier () const
{
- switch (_audio_from) {
- case AUDIO_NONE:
- return 0;
- case AUDIO_FFMPEG:
- return _ffmpeg->audio_channels ();
- case AUDIO_SNDFILE:
- {
- int c = 0;
- for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
- c += (*i)->audio_channels ();
+ string t;
+
+ for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+ shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
+ if (vc) {
+ t += vc->identifier ();
}
- return c;
- }
}
- return 0;
+ return md5_digest (t.c_str(), t.length());
}
-int
-Playlist::audio_frame_rate () const
+/** @param node <Playlist> node */
+void
+Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
{
- switch (_audio_from) {
- case AUDIO_NONE:
- return 0;
- case AUDIO_FFMPEG:
- return _ffmpeg->audio_frame_rate ();
- case AUDIO_SNDFILE:
- return _sndfile.front()->audio_frame_rate ();
+ list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
+ for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
+ _content.push_back (content_factory (film, *i));
}
- return 0;
+ sort (_content.begin(), _content.end(), ContentSorter ());
+
+ reconnect ();
}
-int64_t
-Playlist::audio_channel_layout () const
+/** @param node <Playlist> node */
+void
+Playlist::as_xml (xmlpp::Node* node)
{
- switch (_audio_from) {
- case AUDIO_NONE:
- return 0;
- case AUDIO_FFMPEG:
- return _ffmpeg->audio_channel_layout ();
- case AUDIO_SNDFILE:
- /* XXX */
- return 0;
+ for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+ (*i)->as_xml (node->add_child ("Content"));
}
-
- return 0;
}
-float
-Playlist::video_frame_rate () const
+void
+Playlist::add (shared_ptr<Content> c)
{
- switch (_video_from) {
- case VIDEO_NONE:
- return 0;
- case VIDEO_FFMPEG:
- return _ffmpeg->video_frame_rate ();
- case VIDEO_IMAGEMAGICK:
- return 24;
- }
-
- return 0;
+ _content.push_back (c);
+ sort (_content.begin(), _content.end(), ContentSorter ());
+ reconnect ();
+ Changed ();
}
-libdcp::Size
-Playlist::video_size () const
+void
+Playlist::remove (shared_ptr<Content> c)
{
- switch (_video_from) {
- case VIDEO_NONE:
- return libdcp::Size ();
- case VIDEO_FFMPEG:
- return _ffmpeg->video_size ();
- case VIDEO_IMAGEMAGICK:
- /* XXX */
- return _imagemagick.front()->video_size ();
+ ContentList::iterator i = _content.begin ();
+ while (i != _content.end() && *i != c) {
+ ++i;
+ }
+
+ if (i != _content.end ()) {
+ _content.erase (i);
+ Changed ();
}
- return libdcp::Size ();
+ /* This won't change order, so it does not need a sort */
}
-ContentVideoFrame
-Playlist::video_length () const
+void
+Playlist::remove (ContentList c)
{
- switch (_video_from) {
- case VIDEO_NONE:
- return 0;
- case VIDEO_FFMPEG:
- return _ffmpeg->video_length ();
- case VIDEO_IMAGEMAGICK:
- {
- ContentVideoFrame l = 0;
- for (list<shared_ptr<const ImageMagickContent> >::const_iterator i = _imagemagick.begin(); i != _imagemagick.end(); ++i) {
- l += (*i)->video_length ();
+ for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
+ ContentList::iterator j = _content.begin ();
+ while (j != _content.end() && *j != *i) {
+ ++j;
+ }
+
+ if (j != _content.end ()) {
+ _content.erase (j);
}
- return l;
- }
}
- return 0;
+ /* This won't change order, so it does not need a sort */
+
+ Changed ();
}
bool
-Playlist::has_audio () const
+Playlist::has_subtitles () const
{
- return _audio_from != AUDIO_NONE;
+ for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+ shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
+ if (fc && !fc->subtitle_streams().empty()) {
+ return true;
+ }
+ }
+
+ return false;
}
-Player::Player (boost::shared_ptr<const Film> f, boost::shared_ptr<const Playlist> p)
- : _film (f)
- , _playlist (p)
- , _video (true)
- , _audio (true)
- , _subtitles (true)
- , _have_setup_decoders (false)
- , _ffmpeg_decoder_done (false)
- , _video_sync (true)
+class FrameRateCandidate
{
+public:
+ FrameRateCandidate (float source_, int dcp_)
+ : source (source_)
+ , dcp (dcp_)
+ {}
-}
-
-void
-Player::disable_video ()
-{
- _video = false;
-}
+ float source;
+ int dcp;
+};
-void
-Player::disable_audio ()
+int
+Playlist::best_dcp_frame_rate () const
{
- _audio = false;
-}
+ list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
-void
-Player::disable_subtitles ()
-{
- _subtitles = false;
-}
+ /* Work out what rates we could manage, including those achieved by using skip / repeat. */
+ list<FrameRateCandidate> candidates;
-bool
-Player::pass ()
-{
- if (!_have_setup_decoders) {
- setup_decoders ();
- _have_setup_decoders = true;
+ /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
+ for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
+ candidates.push_back (FrameRateCandidate (*i, *i));
}
-
- bool done = true;
-
- if (_playlist->video_from() == Playlist::VIDEO_FFMPEG || _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
- if (!_ffmpeg_decoder_done) {
- if (_ffmpeg_decoder->pass ()) {
- _ffmpeg_decoder_done = true;
- } else {
- done = false;
- }
- }
+
+ /* Then the skip/repeat ones */
+ for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
+ candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
+ candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
}
- if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
- if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
- if ((*_imagemagick_decoder)->pass ()) {
- _imagemagick_decoder++;
+ /* Pick the best one */
+ float error = std::numeric_limits<float>::max ();
+ optional<FrameRateCandidate> best;
+ list<FrameRateCandidate>::iterator i = candidates.begin();
+ while (i != candidates.end()) {
+
+ float this_error = 0;
+ for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
+ shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
+ if (!vc) {
+ continue;
}
- if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
- done = false;
- }
+ /* Use the largest difference between DCP and source as the "error" */
+ this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
+ }
+
+ if (this_error < error) {
+ error = this_error;
+ best = *i;
}
+
+ ++i;
}
- /* XXX: sndfile */
+ if (!best) {
+ return 24;
+ }
+
+ return best->dcp;
+}
+
+Time
+Playlist::length () const
+{
+ Time len = 0;
+ for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+ len = max (len, (*i)->end() + 1);
+ }
- return done;
+ return len;
}
void
-Player::set_progress (shared_ptr<Job> job)
+Playlist::reconnect ()
{
- /* XXX */
+ for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
+ i->disconnect ();
+ }
+
+ _content_connections.clear ();
+
+ for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+ _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
+ }
}
-void
-Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
+Time
+Playlist::video_end () const
{
- Video (i, same, s);
+ Time end = 0;
+ for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+ if (dynamic_pointer_cast<const VideoContent> (*i)) {
+ end = max (end, (*i)->end ());
+ }
+ }
+
+ return end;
}
void
-Player::process_audio (shared_ptr<AudioBuffers> b)
+Playlist::set_sequence_video (bool s)
{
- Audio (b);
+ _sequence_video = s;
}
-/** @return true on error */
bool
-Player::seek (double t)
+ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
{
- if (!_have_setup_decoders) {
- setup_decoders ();
- _have_setup_decoders = true;
- }
-
- bool r = false;
-
- switch (_playlist->video_from()) {
- case Playlist::VIDEO_NONE:
- break;
- case Playlist::VIDEO_FFMPEG:
- if (!_ffmpeg_decoder || _ffmpeg_decoder->seek (t)) {
- r = true;
- }
- break;
- case Playlist::VIDEO_IMAGEMAGICK:
- /* Find the decoder that contains this position */
- _imagemagick_decoder = _imagemagick_decoders.begin ();
- while (_imagemagick_decoder != _imagemagick_decoders.end ()) {
- double const this_length = (*_imagemagick_decoder)->video_length() / _film->video_frame_rate ();
- if (t < this_length) {
- break;
- }
- t -= this_length;
- ++_imagemagick_decoder;
- }
-
- if (_imagemagick_decoder != _imagemagick_decoders.end()) {
- (*_imagemagick_decoder)->seek (t);
- } else {
- r = true;
- }
- break;
- }
-
- /* XXX: don't seek audio because we don't need to... */
+ return a->position() < b->position();
+}
- return r;
+/** @return content in an undefined order */
+ContentList
+Playlist::content () const
+{
+ return _content;
}
-bool
-Player::seek_to_last ()
+void
+Playlist::repeat (ContentList c, int n)
{
- if (!_have_setup_decoders) {
- setup_decoders ();
- _have_setup_decoders = true;
+ pair<Time, Time> range (TIME_MAX, 0);
+ for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
+ range.first = min (range.first, (*i)->position ());
+ range.second = max (range.second, (*i)->position ());
+ range.first = min (range.first, (*i)->end ());
+ range.second = max (range.second, (*i)->end ());
}
- bool r = false;
-
- switch (_playlist->video_from ()) {
- case Playlist::VIDEO_NONE:
- break;
- case Playlist::VIDEO_FFMPEG:
- if (!_ffmpeg_decoder || _ffmpeg_decoder->seek_to_last ()) {
- r = true;
+ Time pos = range.second;
+ for (int i = 0; i < n; ++i) {
+ for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
+ shared_ptr<Content> copy = (*i)->clone ();
+ copy->set_position (pos + copy->position() - range.first);
+ _content.push_back (copy);
}
- break;
- case Playlist::VIDEO_IMAGEMAGICK:
- if ((*_imagemagick_decoder)->seek_to_last ()) {
- r = true;
- }
- break;
+ pos += range.second - range.first;
}
- /* XXX: don't seek audio because we don't need to... */
-
- return r;
+ sort (_content.begin(), _content.end(), ContentSorter ());
+
+ reconnect ();
+ Changed ();
}
void
-Player::setup_decoders ()
+Playlist::move_earlier (shared_ptr<Content> c)
{
- if ((_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) || (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG)) {
- _ffmpeg_decoder.reset (
- new FFmpegDecoder (
- _film,
- _playlist->ffmpeg(),
- _video && _playlist->video_from() == Playlist::VIDEO_FFMPEG,
- _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
- _subtitles && _film->with_subtitles(),
- _video_sync
- )
- );
- }
+ sort (_content.begin(), _content.end(), ContentSorter ());
- if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
- _ffmpeg_decoder->connect_video (shared_from_this ());
+ ContentList::iterator previous = _content.end ();
+ ContentList::iterator i = _content.begin();
+ while (i != _content.end() && *i != c) {
+ previous = i;
+ ++i;
}
- if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
- _ffmpeg_decoder->connect_audio (shared_from_this ());
+ assert (i != _content.end ());
+ if (previous == _content.end ()) {
+ return;
}
+
+ Time const p = (*previous)->position ();
+ (*previous)->set_position (p + c->length_after_trim ());
+ c->set_position (p);
+ sort (_content.begin(), _content.end(), ContentSorter ());
+
+ Changed ();
+}
- if (_video && _playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
- list<shared_ptr<const ImageMagickContent> > ic = _playlist->imagemagick ();
- for (list<shared_ptr<const ImageMagickContent> >::iterator i = ic.begin(); i != ic.end(); ++i) {
- shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
- _imagemagick_decoders.push_back (d);
- d->connect_video (shared_from_this ());
- }
-
- _imagemagick_decoder = _imagemagick_decoders.begin ();
+void
+Playlist::move_later (shared_ptr<Content> c)
+{
+ sort (_content.begin(), _content.end(), ContentSorter ());
+
+ ContentList::iterator i = _content.begin();
+ while (i != _content.end() && *i != c) {
+ ++i;
}
- 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->connect_audio (shared_from_this ());
- }
+ assert (i != _content.end ());
+
+ ContentList::iterator next = i;
+ ++next;
+
+ if (next == _content.end ()) {
+ return;
}
-}
-void
-Player::disable_video_sync ()
-{
- _video_sync = false;
+ Time const p = (*next)->position ();
+ (*next)->set_position (c->position ());
+ c->set_position (p + c->length_after_trim ());
+ sort (_content.begin(), _content.end(), ContentSorter ());
+
+ Changed ();
}
-double
-Player::last_video_time () const
+bool
+Playlist::content_paths_valid () const
{
- switch (_playlist->video_from ()) {
- case Playlist::VIDEO_NONE:
- return 0;
- case Playlist::VIDEO_FFMPEG:
- return _ffmpeg_decoder->last_source_time ();
- case Playlist::VIDEO_IMAGEMAGICK:
- return (*_imagemagick_decoder)->last_source_time ();
+ for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+ if (!(*i)->path_valid ()) {
+ return false;
+ }
}
- return 0;
+ return true;
}