]> git.sesse.net Git - nageru/commitdiff
Move estimation of time left into Player.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 28 Dec 2018 11:33:53 +0000 (12:33 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 28 Dec 2018 11:33:53 +0000 (12:33 +0100)
futatabi/export.cpp
futatabi/mainwindow.cpp
futatabi/mainwindow.h
futatabi/player.cpp
futatabi/player.h

index 2b9f5317a0dcfb5612edde9cbcd70889ec9f7a9c..1aea044c224162398debf7fd6ae67aa4480871ff 100644 (file)
@@ -223,11 +223,11 @@ void export_interpolated_clip(const string &filename, const vector<Clip> &clips)
        progress.setMaximum(100000);
        progress.setValue(0);
 
-       double total_length = compute_time_left(clips, { { 0, 0.0 } });
        vector<Player::ClipWithRow> clips_with_row;
        for (const Clip &clip : clips) {
                clips_with_row.emplace_back(Player::ClipWithRow{ clip, 0 });
        }
+       double total_length = compute_total_time(clips_with_row);
 
        promise<void> done_promise;
        future<void> done = done_promise.get_future();
@@ -240,8 +240,8 @@ void export_interpolated_clip(const string &filename, const vector<Clip> &clips)
                        done_promise.set_value();
                }
        });
-       player.set_progress_callback([&current_value, &clips, total_length](const std::map<size_t, double> &player_progress) {
-               current_value = 1.0 - compute_time_left(clips, player_progress) / total_length;
+       player.set_progress_callback([&current_value, &clips, total_length](const std::map<size_t, double> &player_progress, double time_remaining) {
+               current_value = 1.0 - time_remaining / total_length;
        });
        player.play(clips_with_row);
        while (done.wait_for(std::chrono::milliseconds(100)) != future_status::ready && !progress.wasCanceled()) {
index 5cbfb133927f18852b8c4367cd485e2e9a055c96..bbaa5a417ab8c9c636d76f324f3b396e7ef236b4 100644 (file)
@@ -189,9 +189,9 @@ MainWindow::MainWindow()
                        live_player_clip_done();
                });
        });
-       live_player->set_progress_callback([this](const map<size_t, double> &progress) {
-               post_to_main_thread([this, progress] {
-                       live_player_clip_progress(progress);
+       live_player->set_progress_callback([this](const map<size_t, double> &progress, double time_remaining) {
+               post_to_main_thread([this, progress, time_remaining] {
+                       live_player_clip_progress(progress, time_remaining);
                });
        });
        set_output_status("paused");
@@ -558,16 +558,10 @@ static string format_duration(double t)
        return buf;
 }
 
-void MainWindow::live_player_clip_progress(const map<size_t, double> &progress)
+void MainWindow::live_player_clip_progress(const map<size_t, double> &progress, double time_remaining)
 {
        playlist_clips->set_progress(progress);
-
-       vector<Clip> clips;
-       for (size_t row = 0; row < playlist_clips->size(); ++row) {
-               clips.push_back(*playlist_clips->clip(row));
-       }
-       double remaining = compute_time_left(clips, progress);
-       set_output_status(format_duration(remaining) + " left");
+       set_output_status(format_duration(time_remaining) + " left");
 }
 
 void MainWindow::resizeEvent(QResizeEvent *event)
@@ -849,11 +843,11 @@ void MainWindow::playlist_selection_changed()
        if (!any_selected) {
                set_output_status("paused");
        } else {
-               vector<Clip> clips;
-               for (size_t row = 0; row < playlist_clips->size(); ++row) {
-                       clips.push_back(*playlist_clips->clip(row));
+               vector<Player::ClipWithRow> clips;
+               for (size_t row = selected->selectedRows().front().row(); row < playlist_clips->size(); ++row) {
+                       clips.emplace_back(Player::ClipWithRow{ *playlist_clips->clip(row), row });
                }
-               double remaining = compute_time_left(clips, { { selected->selectedRows().front().row(), 0.0 } });
+               double remaining = compute_total_time(clips);
                set_output_status(format_duration(remaining) + " ready");
        }
 }
index 33a510e1be961f9198c73e8165fd8008eb78a8c8..67e46dd23e22ad9873790725d2ad3a1877b7da6f 100644 (file)
@@ -104,7 +104,7 @@ private:
        void stop_clicked();
        void live_player_clip_done();
        std::pair<Clip, size_t> live_player_get_next_clip();
-       void live_player_clip_progress(const std::map<size_t, double> &progress);
+       void live_player_clip_progress(const std::map<size_t, double> &progress, double time_remaining);
        void set_output_status(const std::string &status);
        void playlist_duplicate();
        void playlist_remove();
index a82b16e72e6a527618b7e46f34e518ff9427d962..3545eb13c83c4008cf797324421ef7b1068d3ca5 100644 (file)
@@ -179,11 +179,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_list[clip_idx].row, 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_list[clip_idx + 1].row] = 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;
@@ -480,20 +486,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).
index a581a54f79dd41ae076403bb2a8b3b2831b0d029..c8ed083fd544899124f749926c9598a0b0f9d235 100644 (file)
@@ -20,8 +20,6 @@ class VideoStream;
 class QSurface;
 class QSurfaceFormat;
 
-double compute_time_left(const std::vector<Clip> &clips, const std::map<size_t, double> &progress);
-
 class Player : public QueueInterface {
 public:
        enum StreamOutput {
@@ -34,7 +32,7 @@ public:
 
        struct ClipWithRow {
                Clip clip;
-               unsigned row;  // Used for progress callback only.
+               size_t row;  // Used for progress callback only.
        };
        void play(const Clip &clip)
        {
@@ -51,7 +49,7 @@ public:
        // Not thread-safe to set concurrently with playing.
        // Will be called back from the player thread.
        // The keys in the given map are row members in the vector given to play().
-       using progress_callback_func = std::function<void(const std::map<size_t, double> &progress)>;
+       using progress_callback_func = std::function<void(const std::map<size_t, double> &progress, double time_remaining)>;
        void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
 
        // QueueInterface.
@@ -107,4 +105,11 @@ private:
        const StreamOutput stream_output;
 };
 
+double compute_time_left(const std::vector<Player::ClipWithRow> &clips, size_t currently_playing_idx, double progress_currently_playing);
+
+static inline double compute_total_time(const std::vector<Player::ClipWithRow> &clips)
+{
+       return compute_time_left(clips, 0, 0.0);
+}
+
 #endif  // !defined(_PLAYER_H)