1 #include "disk_space_estimator.h"
8 #include <sys/statfs.h>
10 DiskSpaceEstimator::DiskSpaceEstimator(DiskSpaceEstimator::callback_t callback)
15 void DiskSpaceEstimator::report_write(const std::string &filename, size_t bytes, uint64_t pts)
17 // Reject points that are out-of-order (happens with B-frames).
18 if (!measure_points.empty() && pts <= measure_points.back().pts) {
22 // Remove too old points.
23 while (measure_points.size() > 1 && measure_points.front().pts + window_length < pts) {
24 measure_points.pop_front();
30 if (statfs(filename.c_str(), &fst) == -1) {
31 perror(filename.c_str());
35 off_t free_bytes = off_t(fst.f_bavail) * fst.f_frsize;
37 if (!measure_points.empty()) {
38 double bytes_per_second = double(total_size - measure_points.front().size) /
39 (pts - measure_points.front().pts) * TIMEBASE;
40 double seconds_left = free_bytes / bytes_per_second;
42 // Only report every second, since updating the UI can be expensive.
43 if (last_pts_reported == 0 || pts - last_pts_reported >= TIMEBASE) {
44 callback(free_bytes, seconds_left);
45 last_pts_reported = pts;
49 measure_points.push_back({ pts, total_size });
52 DiskSpaceEstimator *global_disk_space_estimator = nullptr; // Created in MainWindow::MainWindow().