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
54 bool got_next_clip = false;
55 double next_clip_fade_time = -1.0;
58 // Wait until we're supposed to play something.
60 unique_lock<mutex> lock(queue_state_mu);
61 new_clip_changed.wait(lock, [this]{
62 return new_clip_ready && current_clip.pts_in != -1;
64 new_clip_ready = false;
71 lock_guard<mutex> lock(mu);
73 stream_idx = current_stream_idx;
76 steady_clock::time_point origin = steady_clock::now();
77 int64_t in_pts_origin = clip.pts_in;
78 int64_t out_pts_origin = pts;
80 // Start playing exactly at a frame.
82 lock_guard<mutex> lock(frame_mu);
84 // Find the first frame such that frame.pts <= in_pts.
85 auto it = lower_bound(frames[stream_idx].begin(),
86 frames[stream_idx].end(),
88 if (it != frames[stream_idx].end()) {
93 // TODO: Lock to a rational multiple of the frame rate if possible.
96 for (int frameno = 0; ; ++frameno) { // Ends when the clip ends.
97 double out_pts = out_pts_origin + TIMEBASE * frameno / output_framerate;
98 steady_clock::time_point next_frame_start =
99 origin + microseconds(lrint((out_pts - out_pts_origin) * 1e6 / TIMEBASE));
100 int64_t in_pts = lrint(in_pts_origin + TIMEBASE * frameno * speed / output_framerate);
101 pts = lrint(out_pts);
103 steady_clock::duration time_behind = steady_clock::now() - next_frame_start;
104 if (time_behind >= milliseconds(200)) {
105 fprintf(stderr, "WARNING: %ld ms behind, dropping a frame (no matter the type).\n",
106 lrint(1e3 * duration<double>(time_behind).count()));
110 double time_left_this_clip = double(clip.pts_out - in_pts) / TIMEBASE / speed;
111 if (!got_next_clip && next_clip_callback != nullptr && time_left_this_clip <= clip.fade_time_seconds) {
112 // Find the next clip so that we can begin a fade.
113 next_clip = next_clip_callback();
114 if (next_clip.pts_in != -1) {
115 got_next_clip = true;
117 double duration_next_clip = (next_clip.pts_out - next_clip.pts_in) / TIMEBASE / speed;
118 next_clip_fade_time = std::min(time_left_this_clip, duration_next_clip);
119 fprintf(stderr, "decided on %.3f seconds fade time [%f %f]\n", next_clip_fade_time, time_left_this_clip, duration_next_clip);
123 int primary_stream_idx = stream_idx;
124 int secondary_stream_idx = -1;
125 float fade_alpha = 0.0f;
127 secondary_stream_idx = next_clip.stream_idx;
128 fade_alpha = 1.0f - time_left_this_clip / next_clip_fade_time;
130 // If more than half-way through the fade, interpolate the next clip
131 // instead of the current one, since it's more visible.
132 if (fade_alpha >= 0.5f) {
133 swap(primary_stream_idx, secondary_stream_idx);
134 fade_alpha = 1.0f - fade_alpha;
138 int64_t secondary_pts = -1;
140 int64_t in_pts_lower, in_pts_upper;
141 bool ok = find_surrounding_frames(in_pts, secondary_stream_idx, &in_pts_lower, &in_pts_upper);
143 secondary_pts = in_pts_lower;
145 secondary_stream_idx = -1;
149 if (progress_callback != nullptr) {
150 // NOTE: None of this will take into account any snapping done below.
151 double played_this_clip = double(in_pts - clip.pts_in) / TIMEBASE / speed;
152 double total_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / speed;
153 progress_callback(played_this_clip, total_length);
156 int64_t in_pts_lower, in_pts_upper;
157 bool ok = find_surrounding_frames(in_pts, primary_stream_idx, &in_pts_lower, &in_pts_upper);
158 if (!ok || in_pts_upper >= clip.pts_out) {
162 // Sleep until the next frame start, or until there's a new clip we're supposed to play.
164 unique_lock<mutex> lock(queue_state_mu);
165 new_clip_changed.wait_until(lock, next_frame_start, [this]{
166 return new_clip_ready || override_stream_idx != -1;
168 if (new_clip_ready) break;
169 if (override_stream_idx != -1) {
170 stream_idx = override_stream_idx;
171 override_stream_idx = -1;
176 if (in_pts_lower == in_pts_upper) {
177 destination->setFrame(primary_stream_idx, in_pts_lower, /*interpolated=*/false, secondary_stream_idx, secondary_pts, fade_alpha);
178 if (video_stream != nullptr) {
179 if (secondary_stream_idx == -1) {
180 video_stream->schedule_original_frame(pts, primary_stream_idx, in_pts_lower);
182 video_stream->schedule_faded_frame(pts, primary_stream_idx, in_pts_lower, secondary_stream_idx, secondary_pts, fade_alpha);
188 // Snap to input frame: If we can do so with less than 1% jitter
189 // (ie., move less than 1% of an _output_ frame), do so.
190 bool snapped = false;
191 for (int64_t snap_pts : { in_pts_lower, in_pts_upper }) {
192 double snap_pts_as_frameno = (snap_pts - in_pts_origin) * output_framerate / TIMEBASE / speed;
193 if (fabs(snap_pts_as_frameno - frameno) < 0.01) {
194 destination->setFrame(primary_stream_idx, snap_pts, /*interpolated=*/false, secondary_stream_idx, secondary_pts, fade_alpha);
195 if (video_stream != nullptr) {
196 if (secondary_stream_idx == -1) {
197 video_stream->schedule_original_frame(pts, primary_stream_idx, snap_pts);
199 video_stream->schedule_faded_frame(pts, primary_stream_idx, snap_pts, secondary_stream_idx, secondary_pts, fade_alpha);
202 in_pts_origin += snap_pts - in_pts;
211 if (time_behind >= milliseconds(100)) {
212 fprintf(stderr, "WARNING: %ld ms behind, dropping an interpolated frame.\n",
213 lrint(1e3 * duration<double>(time_behind).count()));
217 double alpha = double(in_pts - in_pts_lower) / (in_pts_upper - in_pts_lower);
219 if (video_stream == nullptr) {
220 // Previews don't do any interpolation.
221 assert(secondary_stream_idx == -1);
222 destination->setFrame(primary_stream_idx, in_pts_lower, /*interpolated=*/false, fade_alpha);
224 // Calculate the interpolated frame. When it's done, the destination
225 // will be unblocked.
226 destination->setFrame(primary_stream_idx, pts, /*interpolated=*/true, secondary_stream_idx, secondary_pts, fade_alpha);
227 video_stream->schedule_interpolated_frame(pts, primary_stream_idx, in_pts_lower, in_pts_upper, alpha, secondary_stream_idx, secondary_pts, fade_alpha);
233 // Last-ditch effort to get the next clip (if e.g. the fade time was zero seconds).
234 if (!got_next_clip && next_clip_callback != nullptr) {
235 next_clip = next_clip_callback();
236 if (next_clip.pts_in != -1) {
237 got_next_clip = true;
241 // Switch to next clip if we got it.
244 stream_idx = next_clip.stream_idx; // Override is used for previews only, and next_clip is used for live ony.
245 if (done_callback != nullptr) {
248 got_next_clip = false;
253 unique_lock<mutex> lock(queue_state_mu);
256 if (done_callback != nullptr) {
262 // Find the frame immediately before and after this point.
263 bool Player::find_surrounding_frames(int64_t pts, int stream_idx, int64_t *pts_lower, int64_t *pts_upper)
265 lock_guard<mutex> lock(frame_mu);
267 // Find the first frame such that frame.pts >= pts.
268 auto it = lower_bound(frames[stream_idx].begin(),
269 frames[stream_idx].end(),
271 if (it == frames[stream_idx].end()) {
276 // Find the last frame such that in_pts <= frame.pts (if any).
277 if (it == frames[stream_idx].begin()) {
280 *pts_lower = *(it - 1);
282 assert(pts >= *pts_lower);
283 assert(pts <= *pts_upper);
287 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
288 : destination(destination)
290 thread(&Player::thread_func, this, also_output_to_stream).detach();
293 void Player::play_clip(const Clip &clip, unsigned stream_idx)
296 lock_guard<mutex> lock(mu);
298 current_stream_idx = stream_idx;
302 lock_guard<mutex> lock(queue_state_mu);
303 new_clip_ready = true;
304 override_stream_idx = -1;
305 new_clip_changed.notify_all();
309 void Player::override_angle(unsigned stream_idx)
311 // Corner case: If a new clip is waiting to be played, change its stream and then we're done.
313 unique_lock<mutex> lock(queue_state_mu);
314 if (new_clip_ready) {
315 lock_guard<mutex> lock2(mu);
316 current_stream_idx = stream_idx;
321 // If we are playing a clip, set override_stream_idx, and the player thread will
322 // pick it up and change its internal index.
324 unique_lock<mutex> lock(queue_state_mu);
326 override_stream_idx = stream_idx;
327 new_clip_changed.notify_all();
331 // OK, so we're standing still, presumably at the end of a clip.
332 // Look at the current pts_out (if it exists), and show the closest
336 lock_guard<mutex> lock(mu);
337 if (current_clip.pts_out < 0) {
340 pts_out = current_clip.pts_out;
343 lock_guard<mutex> lock(frame_mu);
344 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
345 if (it == frames[stream_idx].end()) {
348 destination->setFrame(stream_idx, *it, /*interpolated=*/false);