]> git.sesse.net Git - nageru/commitdiff
Small refactoring in Player.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 5 Oct 2018 18:57:42 +0000 (20:57 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 5 Oct 2018 18:57:42 +0000 (20:57 +0200)
player.cpp
player.h

index 1e57ded102df14eff8fbd69f7a544fed77e9a5bf..bb2377a643c729cd4c5c8703140f2a4054e30a0b 100644 (file)
@@ -105,29 +105,10 @@ void Player::thread_func(bool also_output_to_stream)
                        }
 
                        int64_t in_pts_lower, in_pts_upper;
-
-                       // Find the frame immediately before and after this point.
-                       {
-                               lock_guard<mutex> lock(frame_mu);
-
-                               // Find the first frame such that in_pts >= frame.pts.
-                               auto it = lower_bound(frames[stream_idx].begin(),
-                                       frames[stream_idx].end(),
-                                       in_pts);
-                               if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
-                                       break;
-                               }
-                               in_pts_upper = *it;
-
-                               // Find the last frame such that in_pts <= frame.pts (if any).
-                               if (it == frames[stream_idx].begin()) {
-                                       in_pts_lower = *it;
-                               } else {
-                                       in_pts_lower = *(it - 1);
-                               }
+                       bool ok = find_surrounding_frames(in_pts, stream_idx, &in_pts_lower, &in_pts_upper);
+                       if (!ok || in_pts_upper >= clip.pts_out) {
+                               break;
                        }
-                       assert(in_pts >= in_pts_lower);
-                       assert(in_pts <= in_pts_upper);
 
                        // Sleep until the next frame start, or until there's a new clip we're supposed to play.
                        {
@@ -207,6 +188,31 @@ void Player::thread_func(bool also_output_to_stream)
        }
 }
 
+// Find the frame immediately before and after this point.
+bool Player::find_surrounding_frames(int64_t pts, int stream_idx, int64_t *pts_lower, int64_t *pts_upper)
+{
+       lock_guard<mutex> lock(frame_mu);
+
+       // Find the first frame such that frame.pts >= pts.
+       auto it = lower_bound(frames[stream_idx].begin(),
+               frames[stream_idx].end(),
+               pts);
+       if (it == frames[stream_idx].end()) {
+               return false;
+       }
+       *pts_upper = *it;
+
+       // Find the last frame such that in_pts <= frame.pts (if any).
+       if (it == frames[stream_idx].begin()) {
+               *pts_lower = *it;
+       } else {
+               *pts_lower = *(it - 1);
+       }
+       assert(pts >= *pts_lower);
+       assert(pts <= *pts_upper);
+       return true;
+}
+
 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
        : destination(destination)
 {
index a10a7cb9671f09924b5eda46266d6a492baccb25..294ffbaa3b9d765baade1f20966c32cf42d4800d 100644 (file)
--- a/player.h
+++ b/player.h
@@ -39,6 +39,10 @@ private:
        static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
        int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
 
+       // Find the frame immediately before and after this point.
+       // Returns false if pts is after the last frame.
+       bool find_surrounding_frames(int64_t pts, int stream_idx, int64_t *pts_lower, int64_t *pts_upper);
+
        JPEGFrameView *destination;
        done_callback_func done_callback;
        progress_callback_func progress_callback;