Don't busy-wait when there's nothing to play.
authorCarl Hetherington <cth@carlh.net>
Sat, 23 Nov 2019 09:42:16 +0000 (10:42 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 8 Jan 2020 20:56:47 +0000 (21:56 +0100)
src/wx/gl_video_view.cc
src/wx/simple_video_view.cc
src/wx/video_view.cc
src/wx/video_view.h

index ddae9bb3c4861b0406550cd9ad542ec1c0881b3d..a461939a7f537ad4a7a3dafb2094bf45fc93af65 100644 (file)
@@ -334,13 +334,17 @@ try
                }
                draw (inter_position, inter_size);
 
-               while (time_until_next_frame() < 5) {
+               while (true) {
+                       optional<int> n = time_until_next_frame();
+                       if (!n || *n > 5) {
+                               break;
+                       }
                        get_next_frame (true);
                        add_dropped ();
                }
 
                boost::this_thread::interruption_point ();
-               dcpomatic_sleep_milliseconds (time_until_next_frame());
+               dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
        }
 
        /* XXX: leaks _context, but that seems preferable to deleting it here
index 6eabbc0b033018f11737e5648443ef4bbb6f77e4..e349b865cf03185531318a20eae95a7bda40bf29 100644 (file)
@@ -159,7 +159,7 @@ SimpleVideoView::timer ()
        }
 
        LOG_DEBUG_PLAYER("%1 -> %2; delay %3", next.seconds(), _viewer->time().seconds(), max((next.seconds() - _viewer->time().seconds()) * 1000, 1.0));
-       _timer.Start (time_until_next_frame(), wxTIMER_ONE_SHOT);
+       _timer.Start (time_until_next_frame().get_value_or(0), wxTIMER_ONE_SHOT);
 
        if (_viewer->butler()) {
                _viewer->butler()->rethrow ();
index 7f93f765e72fbc84fcf4dea98fe85642c35ba89c..4edc2cd23a28cf0f3b943fe3a9d6cb409fecd973 100644 (file)
 #include "wx_util.h"
 #include "film_viewer.h"
 #include "lib/butler.h"
+#include <boost/optional.hpp>
 
 using boost::shared_ptr;
+using boost::optional;
 
 VideoView::VideoView (FilmViewer* viewer)
        : _viewer (viewer)
@@ -89,13 +91,13 @@ VideoView::one_video_frame () const
        return dcpomatic::DCPTime::from_frames (1, video_frame_rate());
 }
 
-/** @return Time in ms until the next frame is due */
-int
+/** @return Time in ms until the next frame is due, or empty if nothing is due */
+optional<int>
 VideoView::time_until_next_frame () const
 {
        if (length() == dcpomatic::DCPTime()) {
                /* There's no content, so this doesn't matter */
-               return 0;
+               return optional<int>();
        }
 
        dcpomatic::DCPTime const next = position() + one_video_frame();
index ad492bd435fec51707c7786239c2a96ddd9c3580..f9e0670435ed13edaa2e919cc373e11d12a0a48d 100644 (file)
@@ -107,7 +107,7 @@ public:
 
 protected:
        bool get_next_frame (bool non_blocking);
-       int time_until_next_frame () const;
+       boost::optional<int> time_until_next_frame () const;
        dcpomatic::DCPTime one_video_frame () const;
 
        int video_frame_rate () const {