A little thread safety.
[dcpomatic.git] / src / wx / video_view.cc
index eb85079c3795285adf4f5548ff94f86ea4f5e91d..e1a8b7306681b12972b63abb5c5780f92ef0cf2b 100644 (file)
 */
 
 #include "video_view.h"
+#include "wx_util.h"
+#include "film_viewer.h"
+#include "lib/butler.h"
 
 void
 VideoView::clear ()
 {
+       boost::mutex::scoped_lock lm (_mutex);
        _player_video.first.reset ();
        _player_video.second = dcpomatic::DCPTime ();
 }
+
+/** @param non_blocking true to return false quickly if no video is available quickly.
+ *  @return false if we gave up because it would take too long, otherwise true.
+ */
+bool
+VideoView::get_next_frame (bool non_blocking)
+{
+       DCPOMATIC_ASSERT (_viewer->butler());
+       _viewer->_gets++;
+
+       boost::mutex::scoped_lock lm (_mutex);
+
+       do {
+               Butler::Error e;
+               _player_video = _viewer->butler()->get_video (!non_blocking, &e);
+               if (!_player_video.first && e == Butler::AGAIN) {
+                       return false;
+               }
+       } while (
+               _player_video.first &&
+               _viewer->film()->three_d() &&
+               _viewer->_eyes != _player_video.first->eyes() &&
+               _player_video.first->eyes() != EYES_BOTH
+               );
+
+       try {
+               _viewer->butler()->rethrow ();
+       } catch (DecodeError& e) {
+               error_dialog (get(), e.what());
+       }
+
+       return true;
+}
+
+dcpomatic::DCPTime
+VideoView::one_video_frame () const
+{
+       return dcpomatic::DCPTime::from_frames (1, film()->video_frame_rate());
+}
+
+/* XXX_b: comment */
+int
+VideoView::time_until_next_frame () const
+{
+       dcpomatic::DCPTime const next = position() + one_video_frame();
+       dcpomatic::DCPTime const time = _viewer->audio_time().get_value_or(position());
+       std::cout << to_string(next) << " " << to_string(time) << " " << ((next.seconds() - time.seconds()) * 1000) << "\n";
+       if (next < time) {
+               return 0;
+       }
+       return (next.seconds() - time.seconds()) * 1000;
+}
+