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;
35 lock_guard<mutex> lock(mu);
37 stream_idx = current_stream_idx;
39 steady_clock::time_point origin = steady_clock::now();
40 int64_t pts_origin = clip.pts_in;
42 int64_t next_pts = pts_origin - 1; // Make sure we play the frame at clip.pts_in if it exists.
46 // Find the next frame.
48 lock_guard<mutex> lock(frame_mu);
49 auto it = upper_bound(frames[stream_idx].begin(),
50 frames[stream_idx].end(),
52 if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
58 // FIXME: assumes a given timebase.
60 steady_clock::time_point next_frame_start =
61 origin + microseconds((next_pts - pts_origin) * int(1000000 / speed) / 12800);
63 // Sleep until the next frame start, or until there's a new clip we're supposed to play.
65 unique_lock<mutex> lock(queue_state_mu);
66 new_clip_changed.wait_until(lock, next_frame_start, [this]{
67 return new_clip_ready || override_stream_idx != -1;
69 if (new_clip_ready) break;
70 if (override_stream_idx != -1) {
71 stream_idx = override_stream_idx;
72 override_stream_idx = -1;
77 destination->setFrame(stream_idx, next_pts);
82 unique_lock<mutex> lock(queue_state_mu);
85 if (done_callback != nullptr && !aborted) {
91 Player::Player(JPEGFrameView *destination)
92 : destination(destination)
94 thread(&Player::thread_func, this).detach();
97 void Player::play_clip(const Clip &clip, unsigned stream_idx)
100 lock_guard<mutex> lock(mu);
102 current_stream_idx = stream_idx;
106 lock_guard<mutex> lock(queue_state_mu);
107 new_clip_ready = true;
108 override_stream_idx = -1;
109 new_clip_changed.notify_all();
113 void Player::override_angle(unsigned stream_idx)
115 // Corner case: If a new clip is waiting to be played, change its stream and then we're done.
117 unique_lock<mutex> lock(queue_state_mu);
118 if (new_clip_ready) {
119 lock_guard<mutex> lock2(mu);
120 current_stream_idx = stream_idx;
125 // If we are playing a clip, set override_stream_idx, and the player thread will
126 // pick it up and change its internal index.
128 unique_lock<mutex> lock(queue_state_mu);
130 override_stream_idx = stream_idx;
131 new_clip_changed.notify_all();
135 // OK, so we're standing still, presumably at the end of a clip.
136 // Look at the current pts_out (if it exists), and show the closest
140 lock_guard<mutex> lock(mu);
141 if (current_clip.pts_out < 0) {
144 pts_out = current_clip.pts_out;
147 lock_guard<mutex> lock(frame_mu);
148 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
149 if (it == frames[stream_idx].end()) {
152 destination->setFrame(stream_idx, *it);