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, 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();
28 if (stat(filename.c_str(), &st) == -1) {
29 perror(filename.c_str());
33 total_size += st.st_size;
36 if (statfs(filename.c_str(), &fst) == -1) {
37 perror(filename.c_str());
41 off_t free_bytes = off_t(fst.f_bavail) * fst.f_frsize;
43 if (!measure_points.empty()) {
44 double bytes_per_second = double(total_size - measure_points.front().size) /
45 (pts - measure_points.front().pts) * TIMEBASE;
46 double seconds_left = free_bytes / bytes_per_second;
48 // Only report every second, since updating the UI can be expensive.
49 if (last_pts_reported == 0 || pts - last_pts_reported >= TIMEBASE) {
50 callback(free_bytes, seconds_left);
51 last_pts_reported = pts;
55 measure_points.push_back({ pts, total_size });
58 DiskSpaceEstimator *global_disk_space_estimator = nullptr; // Created in MainWindow::MainWindow().