3 #include <condition_variable>
10 #include "jpeg_frame_view.h"
14 using namespace std::chrono;
16 extern mutex frame_mu;
17 extern vector<int64_t> frames[MAX_STREAMS];
19 void Player::thread_func()
22 // Wait until we're supposed to play something.
24 unique_lock<mutex> lock(queue_state_mu);
25 new_clip_changed.wait(lock, [this]{
26 return new_clip_ready && current_clip.pts_in != -1;
28 new_clip_ready = false;
34 lock_guard<mutex> lock(mu);
36 stream_idx = current_stream_idx;
38 steady_clock::time_point origin = steady_clock::now();
39 int64_t pts_origin = clip.pts_in;
41 int64_t next_pts = pts_origin;
45 // Find the next frame.
47 lock_guard<mutex> lock(frame_mu);
48 auto it = upper_bound(frames[stream_idx].begin(),
49 frames[stream_idx].end(),
51 if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
57 // FIXME: assumes a given timebase.
59 steady_clock::time_point next_frame_start =
60 origin + microseconds((next_pts - pts_origin) * int(1000000 / speed) / 12800);
62 // Sleep until the next frame start, or until there's a new clip we're supposed to play.
64 unique_lock<mutex> lock(queue_state_mu);
65 aborted = new_clip_changed.wait_until(lock, next_frame_start, [this]{
66 return new_clip_ready;
71 destination->setFrame(stream_idx, next_pts);
75 if (done_callback != nullptr && !aborted) {
81 Player::Player(JPEGFrameView *destination)
82 : destination(destination)
84 thread(&Player::thread_func, this).detach();
87 void Player::play_clip(const Clip &clip, unsigned stream_idx)
90 lock_guard<mutex> lock(mu);
92 current_stream_idx = stream_idx;
96 lock_guard<mutex> lock(queue_state_mu);
97 new_clip_ready = true;
98 new_clip_changed.notify_all();