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;
59 steady_clock::time_point before_sleep = steady_clock::now();
61 // Wait until we're supposed to play something.
63 unique_lock<mutex> lock(queue_state_mu);
64 clip_ready = new_clip_changed.wait_for(lock, milliseconds(100), [this]{
65 return new_clip_ready && current_clip.pts_in != -1;
67 new_clip_ready = false;
71 steady_clock::duration time_slept = steady_clock::now() - before_sleep;
72 pts += duration_cast<duration<size_t, TimebaseRatio>>(time_slept).count();
75 if (video_stream != nullptr) {
76 video_stream->schedule_refresh_frame(pts);
84 lock_guard<mutex> lock(mu);
86 stream_idx = current_stream_idx;
89 steady_clock::time_point origin = steady_clock::now();
90 int64_t in_pts_origin = clip.pts_in;
91 int64_t out_pts_origin = pts;
93 // Start playing exactly at a frame.
95 lock_guard<mutex> lock(frame_mu);
97 // Find the first frame such that frame.pts <= in_pts.
98 auto it = lower_bound(frames[stream_idx].begin(),
99 frames[stream_idx].end(),
101 if (it != frames[stream_idx].end()) {
106 // TODO: Lock to a rational multiple of the frame rate if possible.
109 for (int frameno = 0; ; ++frameno) { // Ends when the clip ends.
110 double out_pts = out_pts_origin + TIMEBASE * frameno / output_framerate;
111 steady_clock::time_point next_frame_start =
112 origin + microseconds(lrint((out_pts - out_pts_origin) * 1e6 / TIMEBASE));
113 int64_t in_pts = lrint(in_pts_origin + TIMEBASE * frameno * speed / output_framerate);
114 pts = lrint(out_pts);
116 steady_clock::duration time_behind = steady_clock::now() - next_frame_start;
117 if (time_behind >= milliseconds(200)) {
118 fprintf(stderr, "WARNING: %ld ms behind, dropping a frame (no matter the type).\n",
119 lrint(1e3 * duration<double>(time_behind).count()));
123 double time_left_this_clip = double(clip.pts_out - in_pts) / TIMEBASE / speed;
124 if (!got_next_clip && next_clip_callback != nullptr && time_left_this_clip <= clip.fade_time_seconds) {
125 // Find the next clip so that we can begin a fade.
126 next_clip = next_clip_callback();
127 if (next_clip.pts_in != -1) {
128 got_next_clip = true;
130 double duration_next_clip = (next_clip.pts_out - next_clip.pts_in) / TIMEBASE / speed;
131 next_clip_fade_time = std::min(time_left_this_clip, duration_next_clip);
135 int primary_stream_idx = stream_idx;
136 int secondary_stream_idx = -1;
137 float fade_alpha = 0.0f;
139 secondary_stream_idx = next_clip.stream_idx;
140 fade_alpha = 1.0f - time_left_this_clip / next_clip_fade_time;
142 // If more than half-way through the fade, interpolate the next clip
143 // instead of the current one, since it's more visible.
144 if (fade_alpha >= 0.5f) {
145 swap(primary_stream_idx, secondary_stream_idx);
146 fade_alpha = 1.0f - fade_alpha;
150 int64_t secondary_pts = -1;
152 int64_t in_pts_lower, in_pts_upper;
153 bool ok = find_surrounding_frames(in_pts, secondary_stream_idx, &in_pts_lower, &in_pts_upper);
155 secondary_pts = in_pts_lower;
157 secondary_stream_idx = -1;
161 if (progress_callback != nullptr) {
162 // NOTE: None of this will take into account any snapping done below.
163 double played_this_clip = double(in_pts - clip.pts_in) / TIMEBASE / speed;
164 double total_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / speed;
165 progress_callback(played_this_clip, total_length);
168 int64_t in_pts_lower, in_pts_upper;
169 bool ok = find_surrounding_frames(in_pts, primary_stream_idx, &in_pts_lower, &in_pts_upper);
170 if (!ok || in_pts_upper >= clip.pts_out) {
174 // Sleep until the next frame start, or until there's a new clip we're supposed to play.
176 unique_lock<mutex> lock(queue_state_mu);
177 new_clip_changed.wait_until(lock, next_frame_start, [this]{
178 return new_clip_ready || override_stream_idx != -1;
180 if (new_clip_ready) break;
181 if (override_stream_idx != -1) {
182 stream_idx = override_stream_idx;
183 override_stream_idx = -1;
188 if (in_pts_lower == in_pts_upper) {
189 destination->setFrame(primary_stream_idx, in_pts_lower, /*interpolated=*/false, secondary_stream_idx, secondary_pts, fade_alpha);
190 if (video_stream != nullptr) {
191 if (secondary_stream_idx == -1) {
192 video_stream->schedule_original_frame(pts, primary_stream_idx, in_pts_lower);
194 video_stream->schedule_faded_frame(pts, primary_stream_idx, in_pts_lower, secondary_stream_idx, secondary_pts, fade_alpha);
200 // Snap to input frame: If we can do so with less than 1% jitter
201 // (ie., move less than 1% of an _output_ frame), do so.
202 bool snapped = false;
203 for (int64_t snap_pts : { in_pts_lower, in_pts_upper }) {
204 double snap_pts_as_frameno = (snap_pts - in_pts_origin) * output_framerate / TIMEBASE / speed;
205 if (fabs(snap_pts_as_frameno - frameno) < 0.01) {
206 destination->setFrame(primary_stream_idx, snap_pts, /*interpolated=*/false, secondary_stream_idx, secondary_pts, fade_alpha);
207 if (video_stream != nullptr) {
208 if (secondary_stream_idx == -1) {
209 video_stream->schedule_original_frame(pts, primary_stream_idx, snap_pts);
211 video_stream->schedule_faded_frame(pts, primary_stream_idx, snap_pts, secondary_stream_idx, secondary_pts, fade_alpha);
214 in_pts_origin += snap_pts - in_pts;
223 if (time_behind >= milliseconds(100)) {
224 fprintf(stderr, "WARNING: %ld ms behind, dropping an interpolated frame.\n",
225 lrint(1e3 * duration<double>(time_behind).count()));
229 double alpha = double(in_pts - in_pts_lower) / (in_pts_upper - in_pts_lower);
231 if (video_stream == nullptr) {
232 // Previews don't do any interpolation.
233 assert(secondary_stream_idx == -1);
234 destination->setFrame(primary_stream_idx, in_pts_lower, /*interpolated=*/false, fade_alpha);
236 // Calculate the interpolated frame. When it's done, the destination
237 // will be unblocked.
238 destination->setFrame(primary_stream_idx, pts, /*interpolated=*/true, secondary_stream_idx, secondary_pts, fade_alpha);
239 video_stream->schedule_interpolated_frame(pts, primary_stream_idx, in_pts_lower, in_pts_upper, alpha, secondary_stream_idx, secondary_pts, fade_alpha);
245 // Last-ditch effort to get the next clip (if e.g. the fade time was zero seconds).
246 if (!got_next_clip && next_clip_callback != nullptr) {
247 next_clip = next_clip_callback();
248 if (next_clip.pts_in != -1) {
249 got_next_clip = true;
253 // Switch to next clip if we got it.
256 stream_idx = next_clip.stream_idx; // Override is used for previews only, and next_clip is used for live ony.
257 if (done_callback != nullptr) {
260 got_next_clip = false;
265 unique_lock<mutex> lock(queue_state_mu);
268 if (done_callback != nullptr) {
274 // Find the frame immediately before and after this point.
275 bool Player::find_surrounding_frames(int64_t pts, int stream_idx, int64_t *pts_lower, int64_t *pts_upper)
277 lock_guard<mutex> lock(frame_mu);
279 // Find the first frame such that frame.pts >= pts.
280 auto it = lower_bound(frames[stream_idx].begin(),
281 frames[stream_idx].end(),
283 if (it == frames[stream_idx].end()) {
288 // Find the last frame such that in_pts <= frame.pts (if any).
289 if (it == frames[stream_idx].begin()) {
292 *pts_lower = *(it - 1);
294 assert(pts >= *pts_lower);
295 assert(pts <= *pts_upper);
299 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
300 : destination(destination)
302 thread(&Player::thread_func, this, also_output_to_stream).detach();
305 void Player::play_clip(const Clip &clip, unsigned stream_idx)
308 lock_guard<mutex> lock(mu);
310 current_stream_idx = stream_idx;
314 lock_guard<mutex> lock(queue_state_mu);
315 new_clip_ready = true;
316 override_stream_idx = -1;
317 new_clip_changed.notify_all();
321 void Player::override_angle(unsigned stream_idx)
323 // Corner case: If a new clip is waiting to be played, change its stream and then we're done.
325 unique_lock<mutex> lock(queue_state_mu);
326 if (new_clip_ready) {
327 lock_guard<mutex> lock2(mu);
328 current_stream_idx = stream_idx;
333 // If we are playing a clip, set override_stream_idx, and the player thread will
334 // pick it up and change its internal index.
336 unique_lock<mutex> lock(queue_state_mu);
338 override_stream_idx = stream_idx;
339 new_clip_changed.notify_all();
343 // OK, so we're standing still, presumably at the end of a clip.
344 // Look at the current pts_out (if it exists), and show the closest
348 lock_guard<mutex> lock(mu);
349 if (current_clip.pts_out < 0) {
352 pts_out = current_clip.pts_out;
355 lock_guard<mutex> lock(frame_mu);
356 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
357 if (it == frames[stream_idx].end()) {
360 destination->setFrame(stream_idx, *it, /*interpolated=*/false);