1 #include "disk_space_estimator.h"
5 #include <sys/statfs.h>
10 DiskSpaceEstimator::DiskSpaceEstimator(DiskSpaceEstimator::callback_t callback)
15 void DiskSpaceEstimator::report_write(const std::string &filename, uint64_t pts)
17 if (filename != last_filename) {
18 last_filename = filename;
19 measure_points.clear();
22 // Reject points that are out-of-order (happens with B-frames).
23 if (!measure_points.empty() && pts < measure_points.back().pts) {
27 // Remove too old points.
28 while (measure_points.size() > 1 && measure_points.front().pts + window_length < pts) {
29 measure_points.pop_front();
33 if (stat(filename.c_str(), &st) == -1) {
34 perror(filename.c_str());
39 if (statfs(filename.c_str(), &fst) == -1) {
40 perror(filename.c_str());
44 if (!measure_points.empty()) {
45 off_t free_bytes = off_t(fst.f_bavail) * fst.f_frsize;
46 double bytes_per_second = double(st.st_size - measure_points.front().size) /
47 (pts - measure_points.front().pts) * TIMEBASE;
48 double seconds_left = free_bytes / bytes_per_second;
49 callback(free_bytes, seconds_left);
52 measure_points.push_back({ pts, st.st_size });
55 DiskSpaceEstimator *global_disk_space_estimator = nullptr; // Created in MainWindow::MainWindow().