From: Steinar H. Gunderson Date: Fri, 5 Oct 2018 18:57:42 +0000 (+0200) Subject: Small refactoring in Player. X-Git-Tag: 1.8.0~76^2~77 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=8fef708c90979cc18b6d9e525490e94c8173143b;p=nageru Small refactoring in Player. --- diff --git a/player.cpp b/player.cpp index 1e57ded..bb2377a 100644 --- a/player.cpp +++ b/player.cpp @@ -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 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 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) { diff --git a/player.h b/player.h index a10a7cb..294ffba 100644 --- 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;