6 #include "ffmpeg_raii.h"
8 #include "jpeg_frame_view.h"
11 #include "video_stream.h"
15 #include <condition_variable>
16 #include <movit/util.h>
23 using namespace std::chrono;
25 extern mutex frame_mu;
26 extern vector<int64_t> frames[MAX_STREAMS];
27 extern HTTPD *global_httpd;
29 void Player::thread_func(bool also_output_to_stream)
31 pthread_setname_np(pthread_self(), "Player");
33 QSurface *surface = create_surface();
34 QOpenGLContext *context = create_context(surface);
35 if (!make_current(context, surface)) {
42 // Create the VideoStream object, now that we have an OpenGL context.
43 if (also_output_to_stream) {
44 video_stream.reset(new VideoStream);
45 video_stream->start();
50 constexpr double output_framerate = 60000.0 / 1001.0; // FIXME: make configurable
53 bool got_next_clip = false;
54 double next_clip_fade_time = -1.0;
58 steady_clock::time_point before_sleep = steady_clock::now();
60 // Wait until we're supposed to play something.
62 unique_lock<mutex> lock(queue_state_mu);
63 clip_ready = new_clip_changed.wait_for(lock, milliseconds(100), [this] {
64 return new_clip_ready && current_clip.pts_in != -1;
66 new_clip_ready = false;
70 steady_clock::duration time_slept = steady_clock::now() - before_sleep;
71 pts += duration_cast<duration<size_t, TimebaseRatio>>(time_slept).count();
74 if (video_stream != nullptr) {
75 video_stream->schedule_refresh_frame(pts);
83 lock_guard<mutex> lock(mu);
85 stream_idx = current_stream_idx;
87 steady_clock::time_point origin = steady_clock::now();
88 int64_t in_pts_origin = clip.pts_in;
90 int64_t out_pts_origin = pts;
92 // Start playing exactly at a frame.
93 // TODO: Snap secondary (fade-to) clips in the same fashion
94 // so that we don't get jank here).
96 lock_guard<mutex> lock(frame_mu);
98 // Find the first frame such that frame.pts <= in_pts.
99 auto it = lower_bound(frames[stream_idx].begin(),
100 frames[stream_idx].end(),
102 if (it != frames[stream_idx].end()) {
107 // TODO: Lock to a rational multiple of the frame rate if possible.
110 int64_t in_pts_start_next_clip = -1;
111 for (int frameno = 0; ; ++frameno) { // Ends when the clip ends.
112 double out_pts = out_pts_origin + TIMEBASE * frameno / output_framerate;
113 steady_clock::time_point next_frame_start =
114 origin + microseconds(lrint((out_pts - out_pts_origin) * 1e6 / TIMEBASE));
115 int64_t in_pts = lrint(in_pts_origin + TIMEBASE * frameno * speed / output_framerate);
116 pts = lrint(out_pts);
118 if (in_pts >= clip.pts_out) {
122 steady_clock::duration time_behind = steady_clock::now() - next_frame_start;
123 if (time_behind >= milliseconds(200)) {
124 fprintf(stderr, "WARNING: %ld ms behind, dropping a frame (no matter the type).\n",
125 lrint(1e3 * duration<double>(time_behind).count()));
129 double time_left_this_clip = double(clip.pts_out - in_pts) / TIMEBASE / speed;
130 if (!got_next_clip && next_clip_callback != nullptr && time_left_this_clip <= clip.fade_time_seconds) {
131 // Find the next clip so that we can begin a fade.
132 next_clip = next_clip_callback();
133 if (next_clip.pts_in != -1) {
134 got_next_clip = true;
136 double duration_next_clip = (next_clip.pts_out - next_clip.pts_in) / TIMEBASE / speed;
137 next_clip_fade_time = std::min(time_left_this_clip, duration_next_clip);
138 in_pts_start_next_clip = next_clip.pts_in + lrint(next_clip_fade_time * TIMEBASE * speed);
142 int primary_stream_idx = stream_idx;
143 int secondary_stream_idx = -1;
144 int64_t secondary_pts = -1;
145 int64_t in_pts_secondary = -1;
146 float fade_alpha = 0.0f;
148 secondary_stream_idx = next_clip.stream_idx;
149 in_pts_secondary = lrint(next_clip.pts_in + (next_clip_fade_time - time_left_this_clip) * TIMEBASE * speed);
150 fade_alpha = 1.0f - time_left_this_clip / next_clip_fade_time;
152 // If more than half-way through the fade, interpolate the next clip
153 // instead of the current one, since it's more visible.
154 if (fade_alpha >= 0.5f) {
155 swap(primary_stream_idx, secondary_stream_idx);
156 swap(in_pts, in_pts_secondary);
157 fade_alpha = 1.0f - fade_alpha;
160 int64_t in_pts_lower, in_pts_upper;
161 bool ok = find_surrounding_frames(in_pts_secondary, secondary_stream_idx, &in_pts_lower, &in_pts_upper);
163 secondary_pts = in_pts_lower;
165 secondary_stream_idx = -1;
169 if (progress_callback != nullptr) {
170 // NOTE: None of this will take into account any snapping done below.
171 double played_this_clip = double(in_pts - clip.pts_in) / TIMEBASE / speed;
172 double total_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / speed;
173 progress_callback(played_this_clip, total_length);
176 int64_t in_pts_lower, in_pts_upper;
177 bool ok = find_surrounding_frames(in_pts, primary_stream_idx, &in_pts_lower, &in_pts_upper);
182 // Sleep until the next frame start, or until there's a new clip we're supposed to play.
184 unique_lock<mutex> lock(queue_state_mu);
185 new_clip_changed.wait_until(lock, next_frame_start, [this]{
186 return new_clip_ready || override_stream_idx != -1;
188 if (new_clip_ready) break;
189 if (override_stream_idx != -1) {
190 stream_idx = override_stream_idx;
191 override_stream_idx = -1;
196 if (in_pts_lower == in_pts_upper) {
197 destination->setFrame(primary_stream_idx, in_pts_lower, /*interpolated=*/false, secondary_stream_idx, secondary_pts, fade_alpha);
198 if (video_stream != nullptr) {
199 if (secondary_stream_idx == -1) {
200 video_stream->schedule_original_frame(pts, primary_stream_idx, in_pts_lower);
202 video_stream->schedule_faded_frame(pts, primary_stream_idx, in_pts_lower, secondary_stream_idx, secondary_pts, fade_alpha);
208 // Snap to input frame: If we can do so with less than 1% jitter
209 // (ie., move less than 1% of an _output_ frame), do so.
210 // TODO: Snap secondary (fade-to) clips in the same fashion.
211 bool snapped = false;
212 for (int64_t snap_pts : { in_pts_lower, in_pts_upper }) {
213 double snap_pts_as_frameno = (snap_pts - in_pts_origin) * output_framerate / TIMEBASE / speed;
214 if (fabs(snap_pts_as_frameno - frameno) < 0.01) {
215 destination->setFrame(primary_stream_idx, snap_pts, /*interpolated=*/false, secondary_stream_idx, secondary_pts, fade_alpha);
216 if (video_stream != nullptr) {
217 if (secondary_stream_idx == -1) {
218 video_stream->schedule_original_frame(pts, primary_stream_idx, snap_pts);
220 video_stream->schedule_faded_frame(pts, primary_stream_idx, snap_pts, secondary_stream_idx, secondary_pts, fade_alpha);
223 in_pts_origin += snap_pts - in_pts;
232 if (time_behind >= milliseconds(100)) {
233 fprintf(stderr, "WARNING: %ld ms behind, dropping an interpolated frame.\n",
234 lrint(1e3 * duration<double>(time_behind).count()));
238 double alpha = double(in_pts - in_pts_lower) / (in_pts_upper - in_pts_lower);
240 if (video_stream == nullptr) {
241 // Previews don't do any interpolation.
242 assert(secondary_stream_idx == -1);
243 destination->setFrame(primary_stream_idx, in_pts_lower, /*interpolated=*/false);
245 // Calculate the interpolated frame. When it's done, the destination
246 // will be unblocked.
247 destination->setFrame(primary_stream_idx, pts, /*interpolated=*/true, secondary_stream_idx, secondary_pts, fade_alpha);
248 video_stream->schedule_interpolated_frame(pts, primary_stream_idx, in_pts_lower, in_pts_upper, alpha, secondary_stream_idx, secondary_pts, fade_alpha);
254 // Last-ditch effort to get the next clip (if e.g. the fade time was zero seconds).
255 if (!got_next_clip && next_clip_callback != nullptr) {
256 next_clip = next_clip_callback();
257 if (next_clip.pts_in != -1) {
258 got_next_clip = true;
259 in_pts_start_next_clip = next_clip.pts_in;
263 // Switch to next clip if we got it.
266 stream_idx = next_clip.stream_idx; // Override is used for previews only, and next_clip is used for live ony.
267 if (done_callback != nullptr) {
270 got_next_clip = false;
272 // Start the next clip from the point where the fade went out.
273 origin = steady_clock::now();
274 in_pts_origin = in_pts_start_next_clip;
279 unique_lock<mutex> lock(queue_state_mu);
282 if (done_callback != nullptr) {
288 // Find the frame immediately before and after this point.
289 bool Player::find_surrounding_frames(int64_t pts, int stream_idx, int64_t *pts_lower, int64_t *pts_upper)
291 lock_guard<mutex> lock(frame_mu);
293 // Find the first frame such that frame.pts >= pts.
294 auto it = lower_bound(frames[stream_idx].begin(),
295 frames[stream_idx].end(),
297 if (it == frames[stream_idx].end()) {
302 // Find the last frame such that in_pts <= frame.pts (if any).
303 if (it == frames[stream_idx].begin()) {
306 *pts_lower = *(it - 1);
308 assert(pts >= *pts_lower);
309 assert(pts <= *pts_upper);
313 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
314 : destination(destination)
316 thread(&Player::thread_func, this, also_output_to_stream).detach();
319 void Player::play_clip(const Clip &clip, unsigned stream_idx)
322 lock_guard<mutex> lock(mu);
324 current_stream_idx = stream_idx;
328 lock_guard<mutex> lock(queue_state_mu);
329 new_clip_ready = true;
330 override_stream_idx = -1;
331 new_clip_changed.notify_all();
335 void Player::override_angle(unsigned stream_idx)
337 // Corner case: If a new clip is waiting to be played, change its stream and then we're done.
339 unique_lock<mutex> lock(queue_state_mu);
340 if (new_clip_ready) {
341 lock_guard<mutex> lock2(mu);
342 current_stream_idx = stream_idx;
347 // If we are playing a clip, set override_stream_idx, and the player thread will
348 // pick it up and change its internal index.
350 unique_lock<mutex> lock(queue_state_mu);
352 override_stream_idx = stream_idx;
353 new_clip_changed.notify_all();
357 // OK, so we're standing still, presumably at the end of a clip.
358 // Look at the current pts_out (if it exists), and show the closest
362 lock_guard<mutex> lock(mu);
363 if (current_clip.pts_out < 0) {
366 pts_out = current_clip.pts_out;
369 lock_guard<mutex> lock(frame_mu);
370 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
371 if (it == frames[stream_idx].end()) {
374 destination->setFrame(stream_idx, *it, /*interpolated=*/false);