From 10675d7568e66b0bcf9bfccfd3ae337bcf8860cb Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 22 Sep 2018 21:04:50 +0200 Subject: [PATCH] Show time remaining when playing clips live. --- mainwindow.cpp | 27 +++++++++++++++++++++++++++ mainwindow.h | 1 + player.cpp | 7 +++++++ player.h | 6 ++++++ ui_mainwindow.ui | 4 ++-- 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index f0a0341..c168611 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -3,6 +3,7 @@ #include "clip_list.h" #include "player.h" #include "post_to_main_thread.h" +#include "timebase.h" #include "ui_mainwindow.h" #include @@ -107,6 +108,11 @@ MainWindow::MainWindow() live_player_clip_done(); }); }); + live_player->set_progress_callback([this](double played_this_clip, double total_length) { + post_to_main_thread([this, played_this_clip, total_length] { + live_player_clip_progress(played_this_clip, total_length); + }); + }); } void MainWindow::cue_in_clicked() @@ -263,7 +269,28 @@ void MainWindow::live_player_clip_done() playlist_clips->set_currently_playing(row); } else { playlist_clips->set_currently_playing(-1); + ui->live_label->setText("Current output (paused)"); + } +} + +void MainWindow::live_player_clip_progress(double played_this_clip, double total_length) +{ + double remaining = total_length - played_this_clip; + for (int row = playlist_clips->get_currently_playing() + 1; row < int(playlist_clips->size()); ++row) { + const Clip clip = *playlist_clips->clip(row); + remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5; // FIXME: stop hardcoding speed. } + int remaining_ms = lrint(remaining * 1e3); + + int ms = remaining_ms % 1000; + remaining_ms /= 1000; + int s = remaining_ms % 60; + remaining_ms /= 60; + int m = remaining_ms; + + char buf[256]; + snprintf(buf, sizeof(buf), "Current output (%d:%02d.%03d left)", m, s, ms); + ui->live_label->setText(buf); } void MainWindow::resizeEvent(QResizeEvent *event) diff --git a/mainwindow.h b/mainwindow.h index 4801667..24eea60 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -47,6 +47,7 @@ private: void preview_angle_clicked(unsigned stream_idx); void play_clicked(); void live_player_clip_done(); + void live_player_clip_progress(double played_this_clip, double total_length); void playlist_duplicate(); void playlist_remove(); void playlist_move(int delta); diff --git a/player.cpp b/player.cpp index 5ff448c..1eea8d7 100644 --- a/player.cpp +++ b/player.cpp @@ -136,6 +136,13 @@ void Player::thread_func(bool also_output_to_stream) } } + if (progress_callback != nullptr) { + // NOTE: None of this will take into account any snapping done below. + double played_this_clip = double(in_pts - clip.pts_in) / TIMEBASE / speed; + double total_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / speed; + progress_callback(played_this_clip, total_length); + } + if (in_pts_lower == in_pts_upper) { destination->setFrame(stream_idx, in_pts_lower, /*interpolated=*/false); if (video_stream != nullptr) { diff --git a/player.h b/player.h index 10e0b6a..a10a7cb 100644 --- a/player.h +++ b/player.h @@ -28,6 +28,11 @@ public: using done_callback_func = std::function; void set_done_callback(done_callback_func cb) { done_callback = cb; } + // Not thread-safe to set concurrently with playing. + // Will be called back from the player thread. + using progress_callback_func = std::function; + void set_progress_callback(progress_callback_func cb) { progress_callback = cb; } + private: void thread_func(bool also_output_to_stream); void open_output_stream(); @@ -36,6 +41,7 @@ private: JPEGFrameView *destination; done_callback_func done_callback; + progress_callback_func progress_callback; std::mutex mu; Clip current_clip; // Under mu. Can have pts_in = -1 for no clip. diff --git a/ui_mainwindow.ui b/ui_mainwindow.ui index 183fcea..01b05b3 100644 --- a/ui_mainwindow.ui +++ b/ui_mainwindow.ui @@ -146,9 +146,9 @@ - + - Current output + Current output (paused) Qt::AlignCenter -- 2.39.2