3 #include <condition_variable>
10 #include <movit/util.h>
12 #include "clip_list.h"
15 #include "ffmpeg_raii.h"
17 #include "jpeg_frame_view.h"
21 #include "video_stream.h"
24 using namespace std::chrono;
26 extern mutex frame_mu;
27 extern vector<int64_t> frames[MAX_STREAMS];
28 extern HTTPD *global_httpd;
30 void Player::thread_func(bool also_output_to_stream)
32 pthread_setname_np(pthread_self(), "Player");
34 QSurface *surface = create_surface();
35 QOpenGLContext *context = create_context(surface);
36 if (!make_current(context, surface)) {
43 // Create the VideoStream object, now that we have an OpenGL context.
44 if (also_output_to_stream) {
45 video_stream.reset(new VideoStream);
46 video_stream->start();
51 constexpr double output_framerate = 60000.0 / 1001.0; // FIXME: make configurable
55 // Wait until we're supposed to play something.
57 unique_lock<mutex> lock(queue_state_mu);
58 new_clip_changed.wait(lock, [this]{
59 return new_clip_ready && current_clip.pts_in != -1;
61 new_clip_ready = false;
68 lock_guard<mutex> lock(mu);
70 stream_idx = current_stream_idx;
73 steady_clock::time_point origin = steady_clock::now();
74 int64_t in_pts_origin = clip.pts_in;
75 int64_t out_pts_origin = pts;
77 // Start playing exactly at a frame.
79 lock_guard<mutex> lock(frame_mu);
81 // Find the first frame such that frame.pts <= in_pts.
82 auto it = lower_bound(frames[stream_idx].begin(),
83 frames[stream_idx].end(),
85 if (it != frames[stream_idx].end()) {
90 // TODO: Lock to a rational multiple of the frame rate if possible.
93 for (int frameno = 0; ; ++frameno) { // Ends when the clip ends.
94 double out_pts = out_pts_origin + TIMEBASE * frameno / output_framerate;
95 steady_clock::time_point next_frame_start =
96 origin + microseconds(lrint((out_pts - out_pts_origin) * 1e6 / TIMEBASE));
97 int64_t in_pts = lrint(in_pts_origin + TIMEBASE * frameno * speed / output_framerate);
100 steady_clock::duration time_behind = steady_clock::now() - next_frame_start;
101 if (time_behind >= milliseconds(200)) {
102 fprintf(stderr, "WARNING: %ld ms behind, dropping a frame (no matter the type).\n",
103 lrint(1e3 * duration<double>(time_behind).count()));
107 int64_t in_pts_lower, in_pts_upper;
108 bool ok = find_surrounding_frames(in_pts, stream_idx, &in_pts_lower, &in_pts_upper);
109 if (!ok || in_pts_upper >= clip.pts_out) {
113 // Sleep until the next frame start, or until there's a new clip we're supposed to play.
115 unique_lock<mutex> lock(queue_state_mu);
116 new_clip_changed.wait_until(lock, next_frame_start, [this]{
117 return new_clip_ready || override_stream_idx != -1;
119 if (new_clip_ready) break;
120 if (override_stream_idx != -1) {
121 stream_idx = override_stream_idx;
122 override_stream_idx = -1;
127 if (progress_callback != nullptr) {
128 // NOTE: None of this will take into account any snapping done below.
129 double played_this_clip = double(in_pts - clip.pts_in) / TIMEBASE / speed;
130 double total_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / speed;
131 progress_callback(played_this_clip, total_length);
134 if (in_pts_lower == in_pts_upper) {
135 destination->setFrame(stream_idx, in_pts_lower, /*interpolated=*/false);
136 if (video_stream != nullptr) {
137 video_stream->schedule_original_frame(pts, stream_idx, in_pts_lower);
142 // Snap to input frame: If we can do so with less than 1% jitter
143 // (ie., move less than 1% of an _output_ frame), do so.
144 bool snapped = false;
145 for (int64_t snap_pts : { in_pts_lower, in_pts_upper }) {
146 double snap_pts_as_frameno = (snap_pts - in_pts_origin) * output_framerate / TIMEBASE / speed;
147 if (fabs(snap_pts_as_frameno - frameno) < 0.01) {
148 destination->setFrame(stream_idx, snap_pts, /*interpolated=*/false);
149 if (video_stream != nullptr) {
150 video_stream->schedule_original_frame(pts, stream_idx, snap_pts);
152 in_pts_origin += snap_pts - in_pts;
161 if (time_behind >= milliseconds(100)) {
162 fprintf(stderr, "WARNING: %ld ms behind, dropping an interpolated frame.\n",
163 lrint(1e3 * duration<double>(time_behind).count()));
167 double alpha = double(in_pts - in_pts_lower) / (in_pts_upper - in_pts_lower);
169 if (video_stream == nullptr) {
170 // Previews don't do any interpolation.
171 destination->setFrame(stream_idx, in_pts_lower, /*interpolated=*/false);
173 // Calculate the interpolated frame. When it's done, the destination
174 // will be unblocked.
175 destination->setFrame(stream_idx, pts, /*interpolated=*/true);
176 video_stream->schedule_interpolated_frame(pts, stream_idx, in_pts_lower, in_pts_upper, alpha);
180 if (next_clip_callback != nullptr) {
181 Clip next_clip = next_clip_callback();
182 if (next_clip.pts_in != -1) {
184 stream_idx = next_clip.stream_idx; // Override is used for previews only, and next_clip is used for live ony.
185 if (done_callback != nullptr) {
193 unique_lock<mutex> lock(queue_state_mu);
196 if (done_callback != nullptr) {
202 // Find the frame immediately before and after this point.
203 bool Player::find_surrounding_frames(int64_t pts, int stream_idx, int64_t *pts_lower, int64_t *pts_upper)
205 lock_guard<mutex> lock(frame_mu);
207 // Find the first frame such that frame.pts >= pts.
208 auto it = lower_bound(frames[stream_idx].begin(),
209 frames[stream_idx].end(),
211 if (it == frames[stream_idx].end()) {
216 // Find the last frame such that in_pts <= frame.pts (if any).
217 if (it == frames[stream_idx].begin()) {
220 *pts_lower = *(it - 1);
222 assert(pts >= *pts_lower);
223 assert(pts <= *pts_upper);
227 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
228 : destination(destination)
230 thread(&Player::thread_func, this, also_output_to_stream).detach();
233 void Player::play_clip(const Clip &clip, unsigned stream_idx)
236 lock_guard<mutex> lock(mu);
238 current_stream_idx = stream_idx;
242 lock_guard<mutex> lock(queue_state_mu);
243 new_clip_ready = true;
244 override_stream_idx = -1;
245 new_clip_changed.notify_all();
249 void Player::override_angle(unsigned stream_idx)
251 // Corner case: If a new clip is waiting to be played, change its stream and then we're done.
253 unique_lock<mutex> lock(queue_state_mu);
254 if (new_clip_ready) {
255 lock_guard<mutex> lock2(mu);
256 current_stream_idx = stream_idx;
261 // If we are playing a clip, set override_stream_idx, and the player thread will
262 // pick it up and change its internal index.
264 unique_lock<mutex> lock(queue_state_mu);
266 override_stream_idx = stream_idx;
267 new_clip_changed.notify_all();
271 // OK, so we're standing still, presumably at the end of a clip.
272 // Look at the current pts_out (if it exists), and show the closest
276 lock_guard<mutex> lock(mu);
277 if (current_clip.pts_out < 0) {
280 pts_out = current_clip.pts_out;
283 lock_guard<mutex> lock(frame_mu);
284 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
285 if (it == frames[stream_idx].end()) {
288 destination->setFrame(stream_idx, *it, /*interpolated=*/false);