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