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;
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(steady_clock::now(), pts, /*display_func=*/nullptr);
84 lock_guard<mutex> lock(mu);
86 stream_idx = current_stream_idx;
88 steady_clock::time_point origin = steady_clock::now(); // TODO: Add a 100 ms buffer for ramp-up?
89 int64_t in_pts_origin = clip.pts_in;
91 int64_t out_pts_origin = pts;
93 // Start playing exactly at a frame.
94 // TODO: Snap secondary (fade-to) clips in the same fashion
95 // so that we don't get jank here).
97 lock_guard<mutex> lock(frame_mu);
99 // Find the first frame such that frame.pts <= in_pts.
100 auto it = lower_bound(frames[stream_idx].begin(),
101 frames[stream_idx].end(),
103 if (it != frames[stream_idx].end()) {
108 // TODO: Lock to a rational multiple of the frame rate if possible.
111 int64_t in_pts_start_next_clip = -1;
112 for (int frameno = 0; ; ++frameno) { // Ends when the clip ends.
113 double out_pts = out_pts_origin + TIMEBASE * frameno / output_framerate;
114 steady_clock::time_point next_frame_start =
115 origin + microseconds(lrint((out_pts - out_pts_origin) * 1e6 / TIMEBASE));
116 int64_t in_pts = lrint(in_pts_origin + TIMEBASE * frameno * speed / output_framerate);
117 pts = lrint(out_pts);
119 if (in_pts >= clip.pts_out) {
123 steady_clock::duration time_behind = steady_clock::now() - next_frame_start;
124 if (time_behind >= milliseconds(200)) {
125 fprintf(stderr, "WARNING: %ld ms behind, dropping a frame (no matter the type).\n",
126 lrint(1e3 * duration<double>(time_behind).count()));
130 double time_left_this_clip = double(clip.pts_out - in_pts) / TIMEBASE / speed;
131 if (!got_next_clip && next_clip_callback != nullptr && time_left_this_clip <= clip.fade_time_seconds) {
132 // Find the next clip so that we can begin a fade.
133 next_clip = next_clip_callback();
134 if (next_clip.pts_in != -1) {
135 got_next_clip = true;
137 double duration_next_clip = (next_clip.pts_out - next_clip.pts_in) / TIMEBASE / speed;
138 next_clip_fade_time = std::min(time_left_this_clip, duration_next_clip);
139 in_pts_start_next_clip = next_clip.pts_in + lrint(next_clip_fade_time * TIMEBASE * speed);
143 int primary_stream_idx = stream_idx;
144 int secondary_stream_idx = -1;
145 int64_t secondary_pts = -1;
146 int64_t in_pts_secondary = -1;
147 float fade_alpha = 0.0f;
149 secondary_stream_idx = next_clip.stream_idx;
150 in_pts_secondary = lrint(next_clip.pts_in + (next_clip_fade_time - time_left_this_clip) * TIMEBASE * speed);
151 fade_alpha = 1.0f - time_left_this_clip / next_clip_fade_time;
153 // If more than half-way through the fade, interpolate the next clip
154 // instead of the current one, since it's more visible.
155 if (fade_alpha >= 0.5f) {
156 swap(primary_stream_idx, secondary_stream_idx);
157 swap(in_pts, in_pts_secondary);
158 fade_alpha = 1.0f - fade_alpha;
161 int64_t in_pts_lower, in_pts_upper;
162 bool ok = find_surrounding_frames(in_pts_secondary, secondary_stream_idx, &in_pts_lower, &in_pts_upper);
164 secondary_pts = in_pts_lower;
166 secondary_stream_idx = -1;
170 if (progress_callback != nullptr) {
171 // NOTE: None of this will take into account any snapping done below.
172 double played_this_clip = double(in_pts - clip.pts_in) / TIMEBASE / speed;
173 double total_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / speed;
174 progress_callback(played_this_clip, total_length);
177 int64_t in_pts_lower, in_pts_upper;
178 bool ok = find_surrounding_frames(in_pts, primary_stream_idx, &in_pts_lower, &in_pts_upper);
183 // If the queue is full (which is really the state we'd like to be in),
184 // wait until there's room for one more frame (ie., one was output from
185 // VideoStream), or until or until there's a new clip we're supposed to play.
187 unique_lock<mutex> lock(queue_state_mu);
188 new_clip_changed.wait(lock, [this]{
189 if (video_stream != nullptr && num_queued_frames < max_queued_frames) {
192 return new_clip_ready || override_stream_idx != -1;
194 if (new_clip_ready) {
195 if (video_stream != nullptr) {
196 video_stream->clear_queue();
200 if (override_stream_idx != -1) {
201 stream_idx = override_stream_idx;
202 override_stream_idx = -1;
207 if (in_pts_lower == in_pts_upper) {
208 auto display_func = [this, primary_stream_idx, in_pts_lower, secondary_stream_idx, secondary_pts, fade_alpha]{
209 destination->setFrame(primary_stream_idx, in_pts_lower, /*interpolated=*/false, secondary_stream_idx, secondary_pts, fade_alpha);
210 unique_lock<mutex> lock(queue_state_mu);
211 assert(num_queued_frames > 0);
214 if (video_stream == nullptr) {
217 if (secondary_stream_idx == -1) {
218 video_stream->schedule_original_frame(next_frame_start, pts, display_func, primary_stream_idx, in_pts_lower);
219 unique_lock<mutex> lock(queue_state_mu);
222 bool ok = video_stream->schedule_faded_frame(next_frame_start, pts, display_func, primary_stream_idx, in_pts_lower, secondary_stream_idx, secondary_pts, fade_alpha);
223 unique_lock<mutex> lock(queue_state_mu);
224 if (ok) ++num_queued_frames;
230 // Snap to input frame: If we can do so with less than 1% jitter
231 // (ie., move less than 1% of an _output_ frame), do so.
232 // TODO: Snap secondary (fade-to) clips in the same fashion.
233 bool snapped = false;
234 for (int64_t snap_pts : { in_pts_lower, in_pts_upper }) {
235 double snap_pts_as_frameno = (snap_pts - in_pts_origin) * output_framerate / TIMEBASE / speed;
236 if (fabs(snap_pts_as_frameno - frameno) < 0.01) {
237 auto display_func = [this, primary_stream_idx, snap_pts, secondary_stream_idx, secondary_pts, fade_alpha]{
238 destination->setFrame(primary_stream_idx, snap_pts, /*interpolated=*/false, secondary_stream_idx, secondary_pts, fade_alpha);
239 unique_lock<mutex> lock(queue_state_mu);
240 assert(num_queued_frames > 0);
243 if (video_stream == nullptr) {
246 if (secondary_stream_idx == -1) {
247 video_stream->schedule_original_frame(next_frame_start, pts, display_func, primary_stream_idx, snap_pts);
248 unique_lock<mutex> lock(queue_state_mu);
251 bool ok = video_stream->schedule_faded_frame(next_frame_start, pts, display_func, primary_stream_idx, snap_pts, secondary_stream_idx, secondary_pts, fade_alpha);
252 unique_lock<mutex> lock(queue_state_mu);
253 if (ok) ++num_queued_frames;
256 in_pts_origin += snap_pts - in_pts;
265 if (time_behind >= milliseconds(100)) {
266 fprintf(stderr, "WARNING: %ld ms behind, dropping an interpolated frame.\n",
267 lrint(1e3 * duration<double>(time_behind).count()));
271 double alpha = double(in_pts - in_pts_lower) / (in_pts_upper - in_pts_lower);
273 if (video_stream == nullptr) {
274 // Previews don't do any interpolation.
275 assert(secondary_stream_idx == -1);
276 destination->setFrame(primary_stream_idx, in_pts_lower, /*interpolated=*/false);
278 auto display_func = [this, primary_stream_idx, pts, secondary_stream_idx, secondary_pts, fade_alpha]{
279 destination->setFrame(primary_stream_idx, pts, /*interpolated=*/true, secondary_stream_idx, secondary_pts, fade_alpha);
280 assert(num_queued_frames > 0);
283 bool ok = video_stream->schedule_interpolated_frame(next_frame_start, pts, display_func, primary_stream_idx, in_pts_lower, in_pts_upper, alpha, secondary_stream_idx, secondary_pts, fade_alpha);
284 unique_lock<mutex> lock(queue_state_mu);
285 if (ok) ++num_queued_frames;
291 // Last-ditch effort to get the next clip (if e.g. the fade time was zero seconds).
292 if (!got_next_clip && next_clip_callback != nullptr) {
293 next_clip = next_clip_callback();
294 if (next_clip.pts_in != -1) {
295 got_next_clip = true;
296 in_pts_start_next_clip = next_clip.pts_in;
300 // Switch to next clip if we got it.
303 stream_idx = next_clip.stream_idx; // Override is used for previews only, and next_clip is used for live ony.
304 if (done_callback != nullptr) {
307 got_next_clip = false;
309 // Start the next clip from the point where the fade went out.
310 origin = steady_clock::now();
311 in_pts_origin = in_pts_start_next_clip;
316 unique_lock<mutex> lock(queue_state_mu);
319 if (done_callback != nullptr) {
325 // Find the frame immediately before and after this point.
326 bool Player::find_surrounding_frames(int64_t pts, int stream_idx, int64_t *pts_lower, int64_t *pts_upper)
328 lock_guard<mutex> lock(frame_mu);
330 // Find the first frame such that frame.pts >= pts.
331 auto it = lower_bound(frames[stream_idx].begin(),
332 frames[stream_idx].end(),
334 if (it == frames[stream_idx].end()) {
339 // Find the last frame such that in_pts <= frame.pts (if any).
340 if (it == frames[stream_idx].begin()) {
343 *pts_lower = *(it - 1);
345 assert(pts >= *pts_lower);
346 assert(pts <= *pts_upper);
350 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
351 : destination(destination)
353 thread(&Player::thread_func, this, also_output_to_stream).detach();
356 void Player::play_clip(const Clip &clip, unsigned stream_idx)
359 lock_guard<mutex> lock(mu);
361 current_stream_idx = stream_idx;
365 lock_guard<mutex> lock(queue_state_mu);
366 new_clip_ready = true;
367 override_stream_idx = -1;
368 new_clip_changed.notify_all();
372 void Player::override_angle(unsigned stream_idx)
374 // Corner case: If a new clip is waiting to be played, change its stream and then we're done.
376 unique_lock<mutex> lock(queue_state_mu);
377 if (new_clip_ready) {
378 lock_guard<mutex> lock2(mu);
379 current_stream_idx = stream_idx;
384 // If we are playing a clip, set override_stream_idx, and the player thread will
385 // pick it up and change its internal index.
387 unique_lock<mutex> lock(queue_state_mu);
389 override_stream_idx = stream_idx;
390 new_clip_changed.notify_all();
394 // OK, so we're standing still, presumably at the end of a clip.
395 // Look at the current pts_out (if it exists), and show the closest
399 lock_guard<mutex> lock(mu);
400 if (current_clip.pts_out < 0) {
403 pts_out = current_clip.pts_out;
406 lock_guard<mutex> lock(frame_mu);
407 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
408 if (it == frames[stream_idx].end()) {
411 destination->setFrame(stream_idx, *it, /*interpolated=*/false);