Seemingly basically working butler for video.
authorCarl Hetherington <cth@carlh.net>
Wed, 8 Mar 2017 00:11:50 +0000 (00:11 +0000)
committerCarl Hetherington <cth@carlh.net>
Wed, 19 Apr 2017 22:04:32 +0000 (23:04 +0100)
src/lib/butler.cc
src/lib/butler.h
src/lib/player.cc
src/lib/video_ring_buffers.cc
src/lib/video_ring_buffers.h
src/wx/film_viewer.cc

index e40ae0b24f958767f85f318543112957fe4818d3..1dbad61521b65d6927c494e35ae8b6505231d210 100644 (file)
@@ -40,7 +40,8 @@ Butler::Butler (weak_ptr<const Film> film, shared_ptr<Player> player)
        , _pending_seek_accurate (false)
        , _finished (false)
 {
-       _player->Video.connect (bind (&VideoRingBuffers::put, &_video, _1, _2));
+       _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
+       _player_changed_connection = _player->Changed.connect (bind (&Butler::player_changed, this));
        _thread = new boost::thread (bind (&Butler::thread, this));
 }
 
@@ -61,21 +62,30 @@ Butler::thread ()
        while (true) {
                boost::mutex::scoped_lock lm (_mutex);
 
-               while (_video.size() > VIDEO_READAHEAD && !_pending_seek_position) {
+               /* Wait until we have something to do */
+               while (_video.size() >= VIDEO_READAHEAD && !_pending_seek_position) {
                        _summon.wait (lm);
                }
 
+               /* Do any seek that has been requested */
                if (_pending_seek_position) {
                        _player->seek (*_pending_seek_position, _pending_seek_accurate);
                        _pending_seek_position = optional<DCPTime> ();
                }
 
-               while (_video.size() < VIDEO_READAHEAD) {
-                       _arrived.notify_all ();
+               /* Fill _video.  Don't try to carry on if a pending seek appears
+                  while lm is unlocked, as in that state nothing will be added to
+                  _video.
+               */
+               while (_video.size() < VIDEO_READAHEAD && !_pending_seek_position) {
+                       lm.unlock ();
                        if (_player->pass ()) {
                                _finished = true;
+                               _arrived.notify_all ();
                                break;
                        }
+                       lm.lock ();
+                       _arrived.notify_all ();
                }
        }
 }
@@ -84,7 +94,9 @@ pair<shared_ptr<PlayerVideo>, DCPTime>
 Butler::get_video ()
 {
        boost::mutex::scoped_lock lm (_mutex);
-       while (_video.size() == 0 && !_finished) {
+
+       /* Wait for data if we have none */
+       while (_video.empty() && !_finished) {
                _arrived.wait (lm);
        }
 
@@ -92,20 +104,47 @@ Butler::get_video ()
                return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
        }
 
-       return _video.get ();
+       pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
+       _summon.notify_all ();
+       return r;
 }
 
 void
 Butler::seek (DCPTime position, bool accurate)
 {
+       boost::mutex::scoped_lock lm (_mutex);
        _video.clear ();
+       _finished = false;
+       _pending_seek_position = position;
+       _pending_seek_accurate = accurate;
+       _summon.notify_all ();
+}
+
+void
+Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
+{
+       {
+               boost::mutex::scoped_lock lm (_mutex);
+               if (_pending_seek_position) {
+                       /* Don't store any video while a seek is pending */
+                       return;
+               }
+       }
+
+       _video.put (video, time);
+}
+
+void
+Butler::player_changed ()
+{
+       optional<DCPTime> t;
 
        {
                boost::mutex::scoped_lock lm (_mutex);
-               _finished = false;
-               _pending_seek_position = position;
-               _pending_seek_accurate = accurate;
+               t = _video.earliest ();
        }
 
-       _summon.notify_all ();
+       if (t) {
+               seek (*t, true);
+       }
 }
index d801d6cf9b7f1d12ff1eaa2d9cac1b21d04ca62d..e02351b0fd45fbfa608b20f263b3c94b57dce6f1 100644 (file)
@@ -41,6 +41,7 @@ public:
 private:
        void thread ();
        void video (boost::shared_ptr<PlayerVideo> video, DCPTime time);
+       void player_changed ();
 
        boost::weak_ptr<const Film> _film;
        boost::shared_ptr<Player> _player;
@@ -57,4 +58,5 @@ private:
        bool _finished;
 
        boost::signals2::scoped_connection _player_video_connection;
+       boost::signals2::scoped_connection _player_changed_connection;
 };
index 844706e4beaa40928ca7fff407d58cd6e9a7d62e..a6df63ac0377ab6e42ecaeb476bb990d17d33c1a 100644 (file)
@@ -226,10 +226,16 @@ Player::playlist_content_changed (weak_ptr<Content> w, int property, bool freque
 void
 Player::set_video_container_size (dcp::Size s)
 {
+       if (s == _video_container_size) {
+               return;
+       }
+
        _video_container_size = s;
 
        _black_image.reset (new Image (AV_PIX_FMT_RGB24, _video_container_size, true));
        _black_image->make_black ();
+
+       Changed (false);
 }
 
 void
index 254285456ebc4253d125111df1d5e9e45bdd4da9..e81605732dd323ea240e501f57af299609d12987 100644 (file)
@@ -60,6 +60,13 @@ VideoRingBuffers::size () const
        return _data.size ();
 }
 
+bool
+VideoRingBuffers::empty () const
+{
+       boost::mutex::scoped_lock lm (_mutex);
+       return _data.empty ();
+}
+
 void
 VideoRingBuffers::clear ()
 {
@@ -68,12 +75,12 @@ VideoRingBuffers::clear ()
 }
 
 optional<DCPTime>
-VideoRingBuffers::latest () const
+VideoRingBuffers::earliest () const
 {
        boost::mutex::scoped_lock lm (_mutex);
        if (_data.empty ()) {
                return optional<DCPTime> ();
        }
 
-       return _data.back().second;
+       return _data.front().second;
 }
index f728247fe8aa5213ec4fd72a1b2729922cbff0ce..cf61b90cc905c405d1a9c11b0dcac26f020479f9 100644 (file)
@@ -36,8 +36,8 @@ public:
 
        void clear ();
        Frame size () const;
-
-       boost::optional<DCPTime> latest () const;
+       bool empty () const;
+       boost::optional<DCPTime> earliest () const;
 
 private:
        mutable boost::mutex _mutex;
index 8fff213454b2193c4fa8d218cf2e798cfe4a7980..5e15b4b07f8724eb89d94906c4b24ca53991c494 100644 (file)
@@ -212,6 +212,8 @@ FilmViewer::refresh_panel ()
 void
 FilmViewer::get ()
 {
+       cout << "get!\n";
+
        pair<shared_ptr<PlayerVideo>, DCPTime> video;
        do {
                video = _butler->get_video ();