]> git.sesse.net Git - nageru/blob - futatabi/player.cpp
6f329ef506e7ae3df18865d685ef92682b538ae7
[nageru] / futatabi / player.cpp
1 #include "player.h"
2
3 #include "clip_list.h"
4 #include "defs.h"
5 #include "flags.h"
6 #include "frame_on_disk.h"
7 #include "jpeg_frame_view.h"
8 #include "shared/context.h"
9 #include "shared/ffmpeg_raii.h"
10 #include "shared/httpd.h"
11 #include "shared/metrics.h"
12 #include "shared/mux.h"
13 #include "shared/timebase.h"
14 #include "video_stream.h"
15
16 #include <algorithm>
17 #include <chrono>
18 #include <condition_variable>
19 #include <movit/util.h>
20 #include <mutex>
21 #include <stdio.h>
22 #include <thread>
23 #include <vector>
24
25 using namespace std;
26 using namespace std::chrono;
27
28 extern HTTPD *global_httpd;
29
30 void Player::thread_func(AVFormatContext *file_avctx)
31 {
32         pthread_setname_np(pthread_self(), "Player");
33
34         QSurface *surface = create_surface();
35         QOpenGLContext *context = create_context(surface);
36         if (!make_current(context, surface)) {
37                 printf("oops\n");
38                 exit(1);
39         }
40
41         check_error();
42
43         // Create the VideoStream object, now that we have an OpenGL context.
44         if (stream_output != NO_STREAM_OUTPUT) {
45                 video_stream.reset(new VideoStream(file_avctx));
46                 video_stream->start();
47         }
48
49         check_error();
50
51         while (!should_quit) {
52                 play_playlist_once();
53         }
54 }
55
56 namespace {
57
58 double calc_progress(const Clip &clip, int64_t pts)
59 {
60         return double(pts - clip.pts_in) / (clip.pts_out - clip.pts_in);
61 }
62
63 void do_splice(const vector<ClipWithID> &new_list, size_t playing_index1, ssize_t playing_index2, vector<ClipWithID> *old_list)
64 {
65         assert(playing_index2 == -1 || size_t(playing_index2) == playing_index1 + 1);
66
67         // First see if we can do the simple thing; find an element in the new
68         // list that we are already playing, which will serve as our splice point.
69         int splice_start_new_list = -1;
70         for (size_t clip_idx = 0; clip_idx < new_list.size(); ++clip_idx) {
71                 if (new_list[clip_idx].id == (*old_list)[playing_index1].id) {
72                         splice_start_new_list = clip_idx + 1;
73                 } else if (playing_index2 != -1 && new_list[clip_idx].id == (*old_list)[playing_index2].id) {
74                         splice_start_new_list = clip_idx + 1;
75                 }
76         }
77         if (splice_start_new_list == -1) {
78                 // OK, so the playing items are no longer in the new list. Most likely,
79                 // that means we deleted some range that included them. But the ones
80                 // before should stay put -- and we don't want to play them. So find
81                 // the ones that we've already played, and ignore them. Hopefully,
82                 // they're contiguous; the last one that's not seen will be our cut point.
83                 //
84                 // Keeping track of the playlist range explicitly in the UI would remove
85                 // the need for these heuristics, but it would probably also mean we'd
86                 // have to lock the playing clip, which sounds annoying.
87                 unordered_map<uint64_t, size_t> played_ids;
88                 for (size_t clip_idx = 0; clip_idx < playing_index1; ++old_list) {
89                         played_ids.emplace((*old_list)[clip_idx].id, clip_idx);
90                 }
91                 for (size_t clip_idx = 0; clip_idx < new_list.size(); ++clip_idx) {
92                         if (played_ids.count(new_list[clip_idx].id)) {
93                                 splice_start_new_list = clip_idx + 1;
94                         }
95                 }
96
97                 if (splice_start_new_list == -1) {
98                         // OK, we didn't find any matches; the lists are totally distinct.
99                         // So probably the entire thing was deleted; leave it alone.
100                         return;
101                 }
102         }
103
104         size_t splice_start_old_list = ((playing_index2 == -1) ? playing_index1 : playing_index2) + 1;
105         old_list->erase(old_list->begin() + splice_start_old_list, old_list->end());
106         old_list->insert(old_list->end(), new_list.begin() + splice_start_new_list, new_list.end());
107 }
108
109 }  // namespace
110
111 void Player::play_playlist_once()
112 {
113         vector<ClipWithID> clip_list;
114         bool clip_ready;
115         steady_clock::time_point before_sleep = steady_clock::now();
116
117         // Wait until we're supposed to play something.
118         {
119                 unique_lock<mutex> lock(queue_state_mu);
120                 playing = false;
121                 clip_ready = new_clip_changed.wait_for(lock, milliseconds(100), [this] {
122                         return should_quit || new_clip_ready;
123                 });
124                 if (should_quit) {
125                         return;
126                 }
127                 if (clip_ready) {
128                         new_clip_ready = false;
129                         playing = true;
130                         clip_list = move(queued_clip_list);
131                         queued_clip_list.clear();
132                         assert(!clip_list.empty());
133                         assert(!splice_ready);  // This corner case should have been handled in splice_play().
134                 }
135         }
136
137         steady_clock::duration time_slept = steady_clock::now() - before_sleep;
138         pts += duration_cast<duration<size_t, TimebaseRatio>>(time_slept).count();
139
140         if (!clip_ready) {
141                 if (video_stream != nullptr) {
142                         ++metric_refresh_frame;
143                         video_stream->schedule_refresh_frame(steady_clock::now(), pts, /*display_func=*/nullptr, QueueSpotHolder());
144                 }
145                 return;
146         }
147
148         steady_clock::time_point origin = steady_clock::now();  // TODO: Add a 100 ms buffer for ramp-up?
149         int64_t in_pts_origin = clip_list[0].clip.pts_in;
150         for (size_t clip_idx = 0; clip_idx < clip_list.size(); ++clip_idx) {
151                 const Clip *clip = &clip_list[clip_idx].clip;
152                 const Clip *next_clip = (clip_idx + 1 < clip_list.size()) ? &clip_list[clip_idx + 1].clip : nullptr;
153                 int64_t out_pts_origin = pts;
154
155                 double next_clip_fade_time = -1.0;
156                 if (next_clip != nullptr) {
157                         double duration_this_clip = double(clip->pts_out - in_pts_origin) / TIMEBASE / clip->speed;
158                         double duration_next_clip = double(next_clip->pts_out - next_clip->pts_in) / TIMEBASE / clip->speed;
159                         next_clip_fade_time = min(min(duration_this_clip, duration_next_clip), clip->fade_time_seconds);
160                 }
161
162                 int stream_idx = clip->stream_idx;
163
164                 // Start playing exactly at a frame.
165                 // TODO: Snap secondary (fade-to) clips in the same fashion
166                 // so that we don't get jank here).
167                 {
168                         lock_guard<mutex> lock(frame_mu);
169
170                         // Find the first frame such that frame.pts <= in_pts.
171                         auto it = find_last_frame_before(frames[stream_idx], in_pts_origin);
172                         if (it != frames[stream_idx].end()) {
173                                 in_pts_origin = it->pts;
174                         }
175                 }
176
177                 steady_clock::time_point next_frame_start;
178                 for (int frameno = 0; !should_quit; ++frameno) {  // Ends when the clip ends.
179                         double out_pts = out_pts_origin + TIMEBASE * frameno / global_flags.output_framerate;
180                         next_frame_start =
181                                 origin + microseconds(lrint((out_pts - out_pts_origin) * 1e6 / TIMEBASE));
182                         int64_t in_pts = lrint(in_pts_origin + TIMEBASE * frameno * clip->speed / global_flags.output_framerate);
183                         pts = lrint(out_pts);
184
185                         if (in_pts >= clip->pts_out) {
186                                 break;
187                         }
188
189                         {
190                                 lock_guard<mutex> lock(queue_state_mu);
191                                 if (splice_ready) {
192                                         fprintf(stderr, "splicing\n");
193                                         if (next_clip == nullptr) {
194                                                 do_splice(to_splice_clip_list, clip_idx, -1, &clip_list);
195                                         } else {
196                                                 do_splice(to_splice_clip_list, clip_idx, clip_idx + 1, &clip_list);
197                                         }
198                                         to_splice_clip_list.clear();
199                                         splice_ready = false;
200
201                                         // Refresh the clip pointer, since the clip list may have been reallocated.
202                                         clip = &clip_list[clip_idx].clip;
203
204                                         // Recompute next_clip and any needed fade times, since the next clip may have changed
205                                         // (or we may have gone from no new clip to having one, or the other way).
206                                         next_clip = (clip_idx + 1 < clip_list.size()) ? &clip_list[clip_idx + 1].clip : nullptr;
207                                         if (next_clip != nullptr) {
208                                                 double duration_this_clip = double(clip->pts_out - in_pts) / TIMEBASE / clip->speed;
209                                                 double duration_next_clip = double(next_clip->pts_out - next_clip->pts_in) / TIMEBASE / clip->speed;
210                                                 next_clip_fade_time = min(min(duration_this_clip, duration_next_clip), clip->fade_time_seconds);
211                                         }
212                                 }
213                         }
214
215                         steady_clock::duration time_behind = steady_clock::now() - next_frame_start;
216                         if (stream_output != FILE_STREAM_OUTPUT && time_behind >= milliseconds(200)) {
217                                 fprintf(stderr, "WARNING: %ld ms behind, dropping a frame (no matter the type).\n",
218                                         lrint(1e3 * duration<double>(time_behind).count()));
219                                 ++metric_dropped_unconditional_frame;
220                                 continue;
221                         }
222
223                         // pts not affected by the swapping below.
224                         int64_t in_pts_for_progress = in_pts, in_pts_secondary_for_progress = -1;
225
226                         int primary_stream_idx = stream_idx;
227                         FrameOnDisk secondary_frame;
228                         int secondary_stream_idx = -1;
229                         float fade_alpha = 0.0f;
230                         double time_left_this_clip = double(clip->pts_out - in_pts) / TIMEBASE / clip->speed;
231                         if (next_clip != nullptr && time_left_this_clip <= next_clip_fade_time) {
232                                 // We're in a fade to the next clip->
233                                 secondary_stream_idx = next_clip->stream_idx;
234                                 int64_t in_pts_secondary = lrint(next_clip->pts_in + (next_clip_fade_time - time_left_this_clip) * TIMEBASE * clip->speed);
235                                 in_pts_secondary_for_progress = in_pts_secondary;
236                                 fade_alpha = 1.0f - time_left_this_clip / next_clip_fade_time;
237
238                                 // If more than half-way through the fade, interpolate the next clip
239                                 // instead of the current one, since it's more visible.
240                                 if (fade_alpha >= 0.5f) {
241                                         swap(primary_stream_idx, secondary_stream_idx);
242                                         swap(in_pts, in_pts_secondary);
243                                         fade_alpha = 1.0f - fade_alpha;
244                                 }
245
246                                 FrameOnDisk frame_lower, frame_upper;
247                                 bool ok = find_surrounding_frames(in_pts_secondary, secondary_stream_idx, &frame_lower, &frame_upper);
248                                 if (ok) {
249                                         secondary_frame = frame_lower;
250                                 }
251                         }
252
253                         if (progress_callback != nullptr) {
254                                 // NOTE: None of this will take into account any snapping done below.
255                                 double clip_progress = calc_progress(*clip, in_pts_for_progress);
256                                 map<uint64_t, double> progress{ { clip_list[clip_idx].id, clip_progress } };
257                                 double time_remaining;
258                                 if (next_clip != nullptr && time_left_this_clip <= next_clip_fade_time) {
259                                         double next_clip_progress = calc_progress(*next_clip, in_pts_secondary_for_progress);
260                                         progress[clip_list[clip_idx + 1].id] = next_clip_progress;
261                                         time_remaining = compute_time_left(clip_list, clip_idx + 1, next_clip_progress);
262                                 } else {
263                                         time_remaining = compute_time_left(clip_list, clip_idx, clip_progress);
264                                 }
265                                 progress_callback(progress, time_remaining);
266                         }
267
268                         FrameOnDisk frame_lower, frame_upper;
269                         bool ok = find_surrounding_frames(in_pts, primary_stream_idx, &frame_lower, &frame_upper);
270                         if (!ok) {
271                                 break;
272                         }
273
274                         // Wait until we should, or (given buffering) can, output the frame.
275                         {
276                                 unique_lock<mutex> lock(queue_state_mu);
277                                 if (video_stream == nullptr) {
278                                         // No queue, just wait until the right time and then show the frame.
279                                         new_clip_changed.wait_until(lock, next_frame_start, [this] {
280                                                 return should_quit || new_clip_ready || override_stream_idx != -1;
281                                         });
282                                         if (should_quit) {
283                                                 return;
284                                         }
285                                 } else {
286                                         // If the queue is full (which is really the state we'd like to be in),
287                                         // wait until there's room for one more frame (ie., one was output from
288                                         // VideoStream), or until or until there's a new clip we're supposed to play.
289                                         //
290                                         // In this case, we don't sleep until next_frame_start; the displaying is
291                                         // done by the queue.
292                                         new_clip_changed.wait(lock, [this] {
293                                                 if (num_queued_frames < max_queued_frames) {
294                                                         return true;
295                                                 }
296                                                 return should_quit || new_clip_ready || override_stream_idx != -1;
297                                         });
298                                 }
299                                 if (should_quit) {
300                                         return;
301                                 }
302                                 if (new_clip_ready) {
303                                         if (video_stream != nullptr) {
304                                                 lock.unlock();  // Urg.
305                                                 video_stream->clear_queue();
306                                                 lock.lock();
307                                         }
308                                         return;
309                                 }
310                                 // Honor if we got an override request for the camera.
311                                 if (override_stream_idx != -1) {
312                                         stream_idx = override_stream_idx;
313                                         override_stream_idx = -1;
314                                         continue;
315                                 }
316                         }
317
318                         // If there's nothing to interpolate between, or if interpolation is turned off,
319                         // or we're a preview, then just display the frame.
320                         if (frame_lower.pts == frame_upper.pts || global_flags.interpolation_quality == 0 || video_stream == nullptr) {
321                                 display_single_frame(primary_stream_idx, frame_lower, secondary_stream_idx,
322                                                      secondary_frame, fade_alpha, next_frame_start, /*snapped=*/false);
323                                 continue;
324                         }
325
326                         // Snap to input frame: If we can do so with less than 1% jitter
327                         // (ie., move less than 1% of an _output_ frame), do so.
328                         // TODO: Snap secondary (fade-to) clips in the same fashion.
329                         double pts_snap_tolerance = 0.01 * double(TIMEBASE) / global_flags.output_framerate;
330                         bool snapped = false;
331                         for (FrameOnDisk snap_frame : { frame_lower, frame_upper }) {
332                                 if (fabs(snap_frame.pts - in_pts) < pts_snap_tolerance) {
333                                         display_single_frame(primary_stream_idx, snap_frame, secondary_stream_idx,
334                                                              secondary_frame, fade_alpha, next_frame_start, /*snapped=*/true);
335                                         in_pts_origin += snap_frame.pts - in_pts;
336                                         snapped = true;
337                                         break;
338                                 }
339                         }
340                         if (snapped) {
341                                 continue;
342                         }
343
344                         // The snapping above makes us lock to the input framerate, even in the presence
345                         // of pts drift, for most typical cases where it's needed, like converting 60 â†’ 2x60
346                         // or 60 â†’ 2x59.94. However, there are some corner cases like 25 â†’ 2x59.94, where we'd
347                         // get a snap very rarely (in the given case, once every 24 output frames), and by
348                         // that time, we'd have drifted out. We could have solved this by changing the overall
349                         // speed ever so slightly, but it requires that we know the actual frame rate (which
350                         // is difficult in the presence of jitter and missed frames), or at least do some kind
351                         // of matching/clustering. Instead, we take the opportunity to lock to in-between rational
352                         // points if we can. E.g., if we are converting 60 â†’ 2x60, we would not only snap to
353                         // an original frame every other frame; we would also snap to exactly alpha=0.5 every
354                         // in-between frame. Of course, we will still need to interpolate, but we get a lot
355                         // closer when we actually get close to an original frame. In other words: Snap more
356                         // often, but snap less each time. Unless the input and output frame rates are completely
357                         // decorrelated with no common factor, of course (e.g. 12.345 â†’ 34.567, which we should
358                         // really never see in practice).
359                         for (double fraction : { 1.0 / 2.0, 1.0 / 3.0, 2.0 / 3.0, 1.0 / 4.0, 3.0 / 4.0,
360                                                  1.0 / 5.0, 2.0 / 5.0, 3.0 / 5.0, 4.0 / 5.0 }) {
361                                 double subsnap_pts = frame_lower.pts + fraction * (frame_upper.pts - frame_lower.pts);
362                                 if (fabs(subsnap_pts - in_pts) < pts_snap_tolerance) {
363                                         in_pts_origin += lrint(subsnap_pts) - in_pts;
364                                         in_pts = lrint(subsnap_pts);
365                                         break;
366                                 }
367                         }
368
369                         if (stream_output != FILE_STREAM_OUTPUT && time_behind >= milliseconds(100)) {
370                                 fprintf(stderr, "WARNING: %ld ms behind, dropping an interpolated frame.\n",
371                                         lrint(1e3 * duration<double>(time_behind).count()));
372                                 ++metric_dropped_interpolated_frame;
373                                 continue;
374                         }
375
376                         double alpha = double(in_pts - frame_lower.pts) / (frame_upper.pts - frame_lower.pts);
377                         auto display_func = [this](shared_ptr<Frame> frame) {
378                                 if (destination != nullptr) {
379                                         destination->setFrame(frame);
380                                 }
381                         };
382                         if (secondary_stream_idx == -1) {
383                                 ++metric_interpolated_frame;
384                         } else {
385                                 ++metric_interpolated_faded_frame;
386                         }
387                         video_stream->schedule_interpolated_frame(
388                                 next_frame_start, pts, display_func, QueueSpotHolder(this),
389                                 frame_lower, frame_upper, alpha,
390                                 secondary_frame, fade_alpha);
391                         last_pts_played = in_pts;  // Not really needed; only previews use last_pts_played.
392                 }
393
394                 // The clip ended.
395                 if (should_quit) {
396                         return;
397                 }
398
399                 // Start the next clip from the point where the fade went out.
400                 if (next_clip != nullptr) {
401                         origin = next_frame_start;
402                         in_pts_origin = next_clip->pts_in + lrint(next_clip_fade_time * TIMEBASE * clip->speed);
403                 }
404         }
405
406         if (done_callback != nullptr) {
407                 done_callback();
408         }
409 }
410
411 void Player::display_single_frame(int primary_stream_idx, const FrameOnDisk &primary_frame, int secondary_stream_idx, const FrameOnDisk &secondary_frame, double fade_alpha, steady_clock::time_point frame_start, bool snapped)
412 {
413         auto display_func = [this, primary_stream_idx, primary_frame, secondary_frame, fade_alpha] {
414                 if (destination != nullptr) {
415                         destination->setFrame(primary_stream_idx, primary_frame, secondary_frame, fade_alpha);
416                 }
417         };
418         if (video_stream == nullptr) {
419                 display_func();
420         } else {
421                 if (secondary_stream_idx == -1) {
422                         // NOTE: We could be increasing unused metrics for previews, but that's harmless.
423                         if (snapped) {
424                                 ++metric_original_snapped_frame;
425                         } else {
426                                 ++metric_original_frame;
427                         }
428                         video_stream->schedule_original_frame(
429                                 frame_start, pts, display_func, QueueSpotHolder(this),
430                                 primary_frame);
431                 } else {
432                         assert(secondary_frame.pts != -1);
433                         // NOTE: We could be increasing unused metrics for previews, but that's harmless.
434                         if (snapped) {
435                                 ++metric_faded_snapped_frame;
436                         } else {
437                                 ++metric_faded_frame;
438                         }
439                         video_stream->schedule_faded_frame(frame_start, pts, display_func,
440                                                            QueueSpotHolder(this), primary_frame,
441                                                            secondary_frame, fade_alpha);
442                 }
443         }
444         last_pts_played = primary_frame.pts;
445 }
446
447 // Find the frame immediately before and after this point.
448 bool Player::find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *frame_lower, FrameOnDisk *frame_upper)
449 {
450         lock_guard<mutex> lock(frame_mu);
451
452         // Find the first frame such that frame.pts >= pts.
453         auto it = find_last_frame_before(frames[stream_idx], pts);
454         if (it == frames[stream_idx].end()) {
455                 return false;
456         }
457         *frame_upper = *it;
458
459         // Find the last frame such that in_pts <= frame.pts (if any).
460         if (it == frames[stream_idx].begin()) {
461                 *frame_lower = *it;
462         } else {
463                 *frame_lower = *(it - 1);
464         }
465         assert(pts >= frame_lower->pts);
466         assert(pts <= frame_upper->pts);
467         return true;
468 }
469
470 Player::Player(JPEGFrameView *destination, Player::StreamOutput stream_output, AVFormatContext *file_avctx)
471         : destination(destination), stream_output(stream_output)
472 {
473         player_thread = thread(&Player::thread_func, this, file_avctx);
474
475         if (stream_output == HTTPD_STREAM_OUTPUT) {
476                 global_metrics.add("http_output_frames", { { "type", "original" }, { "reason", "edge_frame_or_no_interpolation" } }, &metric_original_frame);
477                 global_metrics.add("http_output_frames", { { "type", "faded" }, { "reason", "edge_frame_or_no_interpolation" } }, &metric_faded_frame);
478                 global_metrics.add("http_output_frames", { { "type", "original" }, { "reason", "snapped" } }, &metric_original_snapped_frame);
479                 global_metrics.add("http_output_frames", { { "type", "faded" }, { "reason", "snapped" } }, &metric_faded_snapped_frame);
480                 global_metrics.add("http_output_frames", { { "type", "interpolated" } }, &metric_interpolated_frame);
481                 global_metrics.add("http_output_frames", { { "type", "interpolated_faded" } }, &metric_interpolated_faded_frame);
482                 global_metrics.add("http_output_frames", { { "type", "refresh" } }, &metric_refresh_frame);
483                 global_metrics.add("http_dropped_frames", { { "type", "interpolated" } }, &metric_dropped_interpolated_frame);
484                 global_metrics.add("http_dropped_frames", { { "type", "unconditional" } }, &metric_dropped_unconditional_frame);
485         }
486 }
487
488 Player::~Player()
489 {
490         should_quit = true;
491         if (video_stream != nullptr) {
492                 video_stream->stop();
493         }
494         new_clip_changed.notify_all();
495         player_thread.join();
496 }
497
498 void Player::play(const vector<ClipWithID> &clips)
499 {
500         lock_guard<mutex> lock(queue_state_mu);
501         new_clip_ready = true;
502         queued_clip_list = clips;
503         splice_ready = false;
504         override_stream_idx = -1;
505         new_clip_changed.notify_all();
506 }
507
508 void Player::splice_play(const vector<ClipWithID> &clips)
509 {
510         lock_guard<mutex> lock(queue_state_mu);
511         if (new_clip_ready) {
512                 queued_clip_list = clips;
513                 assert(!splice_ready);
514                 return;
515         }
516
517         splice_ready = true;
518         to_splice_clip_list = clips;  // Overwrite any queued but not executed splice.
519 }
520
521 void Player::override_angle(unsigned stream_idx)
522 {
523         int64_t last_pts;
524
525         // Corner case: If a new clip is waiting to be played, change its stream and then we're done.
526         {
527                 lock_guard<mutex> lock(queue_state_mu);
528                 if (new_clip_ready) {
529                         assert(queued_clip_list.size() == 1);
530                         queued_clip_list[0].clip.stream_idx = stream_idx;
531                         return;
532                 }
533
534                 // If we are playing a clip, set override_stream_idx, and the player thread will
535                 // pick it up and change its internal index.
536                 if (playing) {
537                         override_stream_idx = stream_idx;
538                         new_clip_changed.notify_all();
539                         return;
540                 }
541
542                 // OK, so we're standing still, presumably at the end of a clip.
543                 // Look at the last frame played (if it exists), and show the closest
544                 // thing we've got.
545                 if (last_pts_played < 0) {
546                         return;
547                 }
548                 last_pts = last_pts_played;
549         }
550
551         lock_guard<mutex> lock(frame_mu);
552         auto it = find_first_frame_at_or_after(frames[stream_idx], last_pts);
553         if (it == frames[stream_idx].end()) {
554                 return;
555         }
556         destination->setFrame(stream_idx, *it);
557 }
558
559 void Player::take_queue_spot()
560 {
561         lock_guard<mutex> lock(queue_state_mu);
562         ++num_queued_frames;
563 }
564
565 void Player::release_queue_spot()
566 {
567         lock_guard<mutex> lock(queue_state_mu);
568         assert(num_queued_frames > 0);
569         --num_queued_frames;
570         new_clip_changed.notify_all();
571 }
572
573 double compute_time_left(const vector<ClipWithID> &clips, size_t currently_playing_idx, double progress_currently_playing) 
574 {
575         // Look at the last clip and then start counting from there.
576         double remaining = 0.0;
577         double last_fade_time_seconds = 0.0;
578         for (size_t row = currently_playing_idx; row < clips.size(); ++row) {
579                 const Clip &clip = clips[row].clip;
580                 double clip_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / clip.speed;
581                 if (row == currently_playing_idx) {
582                         // A clip we're playing: Subtract the part we've already played.
583                         remaining = clip_length * (1.0 - progress_currently_playing);
584                 } else {
585                         // A clip we haven't played yet: Subtract the part that's overlapping
586                         // with a previous clip (due to fade).
587                         remaining += max(clip_length - last_fade_time_seconds, 0.0);
588                 }
589                 last_fade_time_seconds = min(clip_length, clip.fade_time_seconds);
590         }
591         return remaining;
592 }