]> git.sesse.net Git - nageru/blobdiff - futatabi/player.cpp
Call the done callback only when the entire playlist is done. Simplifies a fair amoun...
[nageru] / futatabi / player.cpp
index 4f110f9222fe621c7d55874a2e0bdfafdcd9feca..0d9e0926b3795285c8a80d5d04fab1b2a153ff23 100644 (file)
@@ -1,13 +1,13 @@
 #include "player.h"
 
 #include "clip_list.h"
-#include "shared/context.h"
 #include "defs.h"
-#include "shared/ffmpeg_raii.h"
 #include "flags.h"
 #include "frame_on_disk.h"
-#include "shared/httpd.h"
 #include "jpeg_frame_view.h"
+#include "shared/context.h"
+#include "shared/ffmpeg_raii.h"
+#include "shared/httpd.h"
 #include "shared/metrics.h"
 #include "shared/mux.h"
 #include "shared/timebase.h"
@@ -50,6 +50,9 @@ void Player::thread_func(AVFormatContext *file_avctx)
 
        while (!should_quit) {
                play_playlist_once();
+               if (done_callback != nullptr) {
+                       done_callback();
+               }
        }
 }
 
@@ -64,7 +67,7 @@ double calc_progress(const Clip &clip, int64_t pts)
 
 void Player::play_playlist_once()
 {
-       vector<Clip> clip_list;
+       vector<ClipWithRow> clip_list;
        bool clip_ready;
        steady_clock::time_point before_sleep = steady_clock::now();
 
@@ -99,10 +102,10 @@ void Player::play_playlist_once()
        }
 
        steady_clock::time_point origin = steady_clock::now();  // TODO: Add a 100 ms buffer for ramp-up?
-       int64_t in_pts_origin = clip_list[0].pts_in;
+       int64_t in_pts_origin = clip_list[0].clip.pts_in;
        for (size_t clip_idx = 0; clip_idx < clip_list.size(); ++clip_idx) {
-               const Clip &clip = clip_list[clip_idx];
-               const Clip *next_clip = (clip_idx + 1 < clip_list.size()) ? &clip_list[clip_idx + 1] : nullptr;
+               const Clip &clip = clip_list[clip_idx].clip;
+               const Clip *next_clip = (clip_idx + 1 < clip_list.size()) ? &clip_list[clip_idx + 1].clip : nullptr;
                int64_t out_pts_origin = pts;
 
                double next_clip_fade_time = -1.0;
@@ -142,12 +145,11 @@ void Player::play_playlist_once()
                        steady_clock::duration time_behind = steady_clock::now() - next_frame_start;
                        if (stream_output != FILE_STREAM_OUTPUT && time_behind >= milliseconds(200)) {
                                fprintf(stderr, "WARNING: %ld ms behind, dropping a frame (no matter the type).\n",
-                                       lrint(1e3 * duration<double>(time_behind).count()));
+                                       lrint(1e3 * duration<double>(time_behind).count()));
                                ++metric_dropped_unconditional_frame;
                                continue;
                        }
 
-
                        // pts not affected by the swapping below.
                        int64_t in_pts_for_progress = in_pts, in_pts_secondary_for_progress = -1;
 
@@ -180,11 +182,17 @@ void Player::play_playlist_once()
 
                        if (progress_callback != nullptr) {
                                // NOTE: None of this will take into account any snapping done below.
-                               map<size_t, double> progress{{ clip_idx, calc_progress(clip, in_pts_for_progress) }};
+                               double clip_progress = calc_progress(clip, in_pts_for_progress);
+                               map<size_t, double> progress{ { clip_list[clip_idx].row, clip_progress } };
+                               double time_remaining;
                                if (next_clip != nullptr && time_left_this_clip <= next_clip_fade_time) {
-                                       progress[clip_idx + 1] = calc_progress(*next_clip, in_pts_secondary_for_progress);
+                                       double next_clip_progress = calc_progress(*next_clip, in_pts_secondary_for_progress);
+                                       progress[clip_list[clip_idx + 1].row] = next_clip_progress;
+                                       time_remaining = compute_time_left(clip_list, clip_idx + 1, next_clip_progress);
+                               } else {
+                                       time_remaining = compute_time_left(clip_list, clip_idx, clip_progress);
                                }
-                               progress_callback(progress);
+                               progress_callback(progress, time_remaining);
                        }
 
                        FrameOnDisk frame_lower, frame_upper;
@@ -198,7 +206,7 @@ void Player::play_playlist_once()
                                unique_lock<mutex> lock(queue_state_mu);
                                if (video_stream == nullptr) {
                                        // No queue, just wait until the right time and then show the frame.
-                                       new_clip_changed.wait_until(lock, next_frame_start, [this]{
+                                       new_clip_changed.wait_until(lock, next_frame_start, [this] {
                                                return should_quit || new_clip_ready || override_stream_idx != -1;
                                        });
                                        if (should_quit) {
@@ -211,7 +219,7 @@ void Player::play_playlist_once()
                                        //
                                        // In this case, we don't sleep until next_frame_start; the displaying is
                                        // done by the queue.
-                                       new_clip_changed.wait(lock, [this]{
+                                       new_clip_changed.wait(lock, [this] {
                                                if (num_queued_frames < max_queued_frames) {
                                                        return true;
                                                }
@@ -253,7 +261,7 @@ void Player::play_playlist_once()
                        for (FrameOnDisk snap_frame : { frame_lower, frame_upper }) {
                                if (fabs(snap_frame.pts - in_pts) < pts_snap_tolerance) {
                                        display_single_frame(primary_stream_idx, snap_frame, secondary_stream_idx,
-                                                            secondary_frame, fade_alpha, next_frame_start, /*snapped=*/true);
+                                                            secondary_frame, fade_alpha, next_frame_start, /*snapped=*/true);
                                        in_pts_origin += snap_frame.pts - in_pts;
                                        snapped = true;
                                        break;
@@ -279,7 +287,7 @@ void Player::play_playlist_once()
                        // decorrelated with no common factor, of course (e.g. 12.345 → 34.567, which we should
                        // really never see in practice).
                        for (double fraction : { 1.0 / 2.0, 1.0 / 3.0, 2.0 / 3.0, 1.0 / 4.0, 3.0 / 4.0,
-                                                1.0 / 5.0, 2.0 / 5.0, 3.0 / 5.0, 4.0 / 5.0 }) {
+                                                1.0 / 5.0, 2.0 / 5.0, 3.0 / 5.0, 4.0 / 5.0 }) {
                                double subsnap_pts = frame_lower.pts + fraction * (frame_upper.pts - frame_lower.pts);
                                if (fabs(subsnap_pts - in_pts) < pts_snap_tolerance) {
                                        in_pts_origin += lrint(subsnap_pts) - in_pts;
@@ -290,7 +298,7 @@ void Player::play_playlist_once()
 
                        if (stream_output != FILE_STREAM_OUTPUT && time_behind >= milliseconds(100)) {
                                fprintf(stderr, "WARNING: %ld ms behind, dropping an interpolated frame.\n",
-                                       lrint(1e3 * duration<double>(time_behind).count()));
+                                       lrint(1e3 * duration<double>(time_behind).count()));
                                ++metric_dropped_interpolated_frame;
                                continue;
                        }
@@ -317,9 +325,6 @@ void Player::play_playlist_once()
                if (should_quit) {
                        return;
                }
-               if (done_callback != nullptr) {
-                       done_callback();
-               }
 
                // Start the next clip from the point where the fade went out.
                if (next_clip != nullptr) {
@@ -327,15 +332,11 @@ void Player::play_playlist_once()
                        in_pts_origin = next_clip->pts_in + lrint(next_clip_fade_time * TIMEBASE * clip.speed);
                }
        }
-
-       if (done_callback != nullptr) {
-               done_callback();
-       }
 }
 
 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)
 {
-       auto display_func = [this, primary_stream_idx, primary_frame, secondary_frame, fade_alpha]{
+       auto display_func = [this, primary_stream_idx, primary_frame, secondary_frame, fade_alpha] {
                if (destination != nullptr) {
                        destination->setFrame(primary_stream_idx, primary_frame, secondary_frame, fade_alpha);
                }
@@ -362,8 +363,8 @@ void Player::display_single_frame(int primary_stream_idx, const FrameOnDisk &pri
                                ++metric_faded_frame;
                        }
                        video_stream->schedule_faded_frame(frame_start, pts, display_func,
-                               QueueSpotHolder(this), primary_frame,
-                               secondary_frame, fade_alpha);
+                                                          QueueSpotHolder(this), primary_frame,
+                                                          secondary_frame, fade_alpha);
                }
        }
        last_pts_played = primary_frame.pts;
@@ -398,15 +399,15 @@ Player::Player(JPEGFrameView *destination, Player::StreamOutput stream_output, A
        player_thread = thread(&Player::thread_func, this, file_avctx);
 
        if (stream_output == HTTPD_STREAM_OUTPUT) {
-               global_metrics.add("http_output_frames", {{ "type", "original" }, { "reason", "edge_frame_or_no_interpolation" }}, &metric_original_frame);
-               global_metrics.add("http_output_frames", {{ "type", "faded" }, { "reason", "edge_frame_or_no_interpolation" }}, &metric_faded_frame);
-               global_metrics.add("http_output_frames", {{ "type", "original" }, { "reason", "snapped" }}, &metric_original_snapped_frame);
-               global_metrics.add("http_output_frames", {{ "type", "faded" }, { "reason", "snapped" }}, &metric_faded_snapped_frame);
-               global_metrics.add("http_output_frames", {{ "type", "interpolated" }}, &metric_interpolated_frame);
-               global_metrics.add("http_output_frames", {{ "type", "interpolated_faded" }}, &metric_interpolated_faded_frame);
-               global_metrics.add("http_output_frames", {{ "type", "refresh" }}, &metric_refresh_frame);
-               global_metrics.add("http_dropped_frames", {{ "type", "interpolated" }}, &metric_dropped_interpolated_frame);
-               global_metrics.add("http_dropped_frames", {{ "type", "unconditional" }}, &metric_dropped_unconditional_frame);
+               global_metrics.add("http_output_frames", { { "type", "original" }, { "reason", "edge_frame_or_no_interpolation" } }, &metric_original_frame);
+               global_metrics.add("http_output_frames", { { "type", "faded" }, { "reason", "edge_frame_or_no_interpolation" } }, &metric_faded_frame);
+               global_metrics.add("http_output_frames", { { "type", "original" }, { "reason", "snapped" } }, &metric_original_snapped_frame);
+               global_metrics.add("http_output_frames", { { "type", "faded" }, { "reason", "snapped" } }, &metric_faded_snapped_frame);
+               global_metrics.add("http_output_frames", { { "type", "interpolated" } }, &metric_interpolated_frame);
+               global_metrics.add("http_output_frames", { { "type", "interpolated_faded" } }, &metric_interpolated_faded_frame);
+               global_metrics.add("http_output_frames", { { "type", "refresh" } }, &metric_refresh_frame);
+               global_metrics.add("http_dropped_frames", { { "type", "interpolated" } }, &metric_dropped_interpolated_frame);
+               global_metrics.add("http_dropped_frames", { { "type", "unconditional" } }, &metric_dropped_unconditional_frame);
        }
 }
 
@@ -420,7 +421,7 @@ Player::~Player()
        player_thread.join();
 }
 
-void Player::play(const vector<Clip> &clips)
+void Player::play(const vector<Player::ClipWithRow> &clips)
 {
        lock_guard<mutex> lock(queue_state_mu);
        new_clip_ready = true;
@@ -438,7 +439,7 @@ void Player::override_angle(unsigned stream_idx)
                lock_guard<mutex> lock(queue_state_mu);
                if (new_clip_ready) {
                        assert(queued_clip_list.size() == 1);
-                       queued_clip_list[0].stream_idx = stream_idx;
+                       queued_clip_list[0].clip.stream_idx = stream_idx;
                        return;
                }
 
@@ -481,20 +482,17 @@ void Player::release_queue_spot()
        new_clip_changed.notify_all();
 }
 
-double compute_time_left(const vector<Clip> &clips, const map<size_t, double> &progress)
+double compute_time_left(const vector<Player::ClipWithRow> &clips, size_t currently_playing_idx, double progress_currently_playing) 
 {
        // Look at the last clip and then start counting from there.
-       assert(!progress.empty());
-       auto last_it = progress.end();
-       --last_it;
        double remaining = 0.0;
        double last_fade_time_seconds = 0.0;
-       for (size_t row = last_it->first; row < clips.size(); ++row) {
-               const Clip &clip = clips[row];
+       for (size_t row = currently_playing_idx; row < clips.size(); ++row) {
+               const Clip &clip = clips[row].clip;
                double clip_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / clip.speed;
-               if (row == last_it->first) {
+               if (row == currently_playing_idx) {
                        // A clip we're playing: Subtract the part we've already played.
-                       remaining = clip_length * (1.0 - last_it->second);
+                       remaining = clip_length * (1.0 - progress_currently_playing);
                } else {
                        // A clip we haven't played yet: Subtract the part that's overlapping
                        // with a previous clip (due to fade).