]> git.sesse.net Git - nageru/blob - disk_space_estimator.cpp
Allow symlinked frame files. Useful for testing.
[nageru] / disk_space_estimator.cpp
1 #include "disk_space_estimator.h"
2
3 #include "timebase.h"
4
5 #include <memory>
6 #include <stdio.h>
7 #include <sys/stat.h>
8 #include <sys/statfs.h>
9
10 DiskSpaceEstimator::DiskSpaceEstimator(DiskSpaceEstimator::callback_t callback)
11         : callback(callback)
12 {
13 }
14
15 void DiskSpaceEstimator::report_write(const std::string &filename, size_t bytes, uint64_t pts)
16 {
17         // Reject points that are out-of-order (happens with B-frames).
18         if (!measure_points.empty() && pts <= measure_points.back().pts) {
19                 return;
20         }
21
22         // Remove too old points.
23         while (measure_points.size() > 1 && measure_points.front().pts + window_length < pts) {
24                 measure_points.pop_front();
25         }
26
27         total_size += bytes;
28
29         struct statfs fst;
30         if (statfs(filename.c_str(), &fst) == -1) {
31                 perror(filename.c_str());
32                 return;
33         }
34
35         off_t free_bytes = off_t(fst.f_bavail) * fst.f_frsize;
36
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;
41
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;
46                 }
47         }
48
49         measure_points.push_back({ pts, total_size });
50 }
51
52 DiskSpaceEstimator *global_disk_space_estimator = nullptr;  // Created in MainWindow::MainWindow().