Fix Player::overlaps for the new world order.
authorCarl Hetherington <cth@carlh.net>
Thu, 14 Apr 2016 01:54:33 +0000 (02:54 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 18 May 2016 10:50:29 +0000 (11:50 +0100)
src/lib/player.cc
src/lib/player.h
test/player_test.cc

index d37910ad8fdcd3dd816cb165e9ccef72a7c91f84..617085a7b88abbdf2b1f12754e5058b954a89939 100644 (file)
@@ -77,6 +77,24 @@ using boost::dynamic_pointer_cast;
 using boost::optional;
 using boost::scoped_ptr;
 
+static bool
+has_video (Content* c)
+{
+       return c->video;
+}
+
+static bool
+has_audio (Content* c)
+{
+       return c->audio;
+}
+
+static bool
+has_subtitle (Content* c)
+{
+       return c->subtitle;
+}
+
 Player::Player (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
        : _film (film)
        , _playlist (playlist)
@@ -384,9 +402,10 @@ Player::get_video (DCPTime time, bool accurate)
 
        /* Find pieces containing video which is happening now */
 
-       list<shared_ptr<Piece> > ov = overlaps<VideoContent> (
+       list<shared_ptr<Piece> > ov = overlaps (
                time,
-               time + DCPTime::from_frames (1, _film->video_frame_rate ())
+               time + DCPTime::from_frames (1, _film->video_frame_rate ()),
+               &has_video
                );
 
        list<shared_ptr<PlayerVideo> > pvf;
@@ -474,7 +493,7 @@ Player::get_audio (DCPTime time, DCPTime length, bool accurate)
        shared_ptr<AudioBuffers> audio (new AudioBuffers (_film->audio_channels(), length_frames));
        audio->make_silent ();
 
-       list<shared_ptr<Piece> > ov = overlaps<AudioContent> (time, time + length);
+       list<shared_ptr<Piece> > ov = overlaps (time, time + length, has_audio);
        if (ov.empty ()) {
                return audio;
        }
@@ -623,7 +642,7 @@ Player::content_subtitle_to_dcp (shared_ptr<const Piece> piece, ContentTime t) c
 PlayerSubtitles
 Player::get_subtitles (DCPTime time, DCPTime length, bool starting, bool burnt, bool accurate)
 {
-       list<shared_ptr<Piece> > subs = overlaps<SubtitleContent> (time, time + length);
+       list<shared_ptr<Piece> > subs = overlaps (time, time + length, has_subtitle);
 
        PlayerSubtitles ps (time, length);
 
@@ -807,3 +826,20 @@ Player::get_reel_assets ()
 
        return a;
 }
+
+list<shared_ptr<Piece> >
+Player::overlaps (DCPTime from, DCPTime to, boost::function<bool (Content *)> valid)
+{
+       if (!_have_valid_pieces) {
+               setup_pieces ();
+       }
+
+       list<shared_ptr<Piece> > overlaps;
+       BOOST_FOREACH (shared_ptr<Piece> i, _pieces) {
+               if (valid (i->content.get ()) && i->content->position() < to && i->content->end() > from) {
+                       overlaps.push_back (i);
+               }
+       }
+
+       return overlaps;
+}
index bfb21abba515c3e0fed011b006e7adc8fa0a14cd..696c0c8576398b0ebd3ce639298de6b24b638b01 100644 (file)
@@ -90,29 +90,7 @@ private:
        ContentTime dcp_to_content_subtitle (boost::shared_ptr<const Piece> piece, DCPTime t) const;
        DCPTime content_subtitle_to_dcp (boost::shared_ptr<const Piece> piece, ContentTime t) const;
        boost::shared_ptr<PlayerVideo> black_player_video_frame (DCPTime) const;
-
-       /** @return Pieces of content type C that overlap a specified time range in the DCP */
-       template<class C>
-       std::list<boost::shared_ptr<Piece> >
-       overlaps (DCPTime from, DCPTime to)
-       {
-               if (!_have_valid_pieces) {
-                       setup_pieces ();
-               }
-
-               std::list<boost::shared_ptr<Piece> > overlaps;
-               for (typename std::list<boost::shared_ptr<Piece> >::const_iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
-                       if (!boost::dynamic_pointer_cast<C> ((*i)->content)) {
-                               continue;
-                       }
-
-                       if ((*i)->content->position() < to && (*i)->content->end() > from) {
-                               overlaps.push_back (*i);
-                       }
-               }
-
-               return overlaps;
-       }
+       std::list<boost::shared_ptr<Piece> > overlaps (DCPTime from, DCPTime to, boost::function<bool (Content *)> valid);
 
        boost::shared_ptr<const Film> _film;
        boost::shared_ptr<const Playlist> _playlist;
index 12e928a3528a9267840b035e4cd7e0eb10bf0e51..842c4b3a9f6f54aa1200a7389b1f9597776c4be4 100644 (file)
@@ -35,6 +35,12 @@ using std::cout;
 using std::list;
 using boost::shared_ptr;
 
+static bool
+valid (Content const *)
+{
+       return true;
+}
+
 /** Player::overlaps */
 BOOST_AUTO_TEST_CASE (player_overlaps_test)
 {
@@ -59,23 +65,23 @@ BOOST_AUTO_TEST_CASE (player_overlaps_test)
 
        shared_ptr<Player> player (new Player (film, film->playlist ()));
 
-       list<shared_ptr<Piece> > o = player->overlaps<FFmpegContent> (DCPTime::from_seconds (0), DCPTime::from_seconds (5));
+       list<shared_ptr<Piece> > o = player->overlaps (DCPTime::from_seconds (0), DCPTime::from_seconds (5), &valid);
        BOOST_CHECK_EQUAL (o.size(), 1U);
        BOOST_CHECK_EQUAL (o.front()->content, A);
 
-       o = player->overlaps<FFmpegContent> (DCPTime::from_seconds (5), DCPTime::from_seconds (8));
+       o = player->overlaps (DCPTime::from_seconds (5), DCPTime::from_seconds (8), &valid);
        BOOST_CHECK_EQUAL (o.size(), 0U);
 
-       o = player->overlaps<FFmpegContent> (DCPTime::from_seconds (8), DCPTime::from_seconds (12));
+       o = player->overlaps (DCPTime::from_seconds (8), DCPTime::from_seconds (12), &valid);
        BOOST_CHECK_EQUAL (o.size(), 1U);
        BOOST_CHECK_EQUAL (o.front()->content, B);
 
-       o = player->overlaps<FFmpegContent> (DCPTime::from_seconds (2), DCPTime::from_seconds (12));
+       o = player->overlaps (DCPTime::from_seconds (2), DCPTime::from_seconds (12), &valid);
        BOOST_CHECK_EQUAL (o.size(), 2U);
        BOOST_CHECK_EQUAL (o.front()->content, A);
        BOOST_CHECK_EQUAL (o.back()->content, B);
 
-       o = player->overlaps<FFmpegContent> (DCPTime::from_seconds (8), DCPTime::from_seconds (11));
+       o = player->overlaps (DCPTime::from_seconds (8), DCPTime::from_seconds (11), &valid);
        BOOST_CHECK_EQUAL (o.size(), 1U);
        BOOST_CHECK_EQUAL (o.front()->content, B);
 }