]> git.sesse.net Git - nageru/blob - disk_space_estimator.h
Allow symlinked frame files. Useful for testing.
[nageru] / disk_space_estimator.h
1 #ifndef _DISK_SPACE_ESTIMATOR_H
2 #define _DISK_SPACE_ESTIMATOR_H
3
4 // A class responsible for measuring how much disk there is left when we
5 // store our video to disk, and how much recording time that equates to.
6 // It gets callbacks from the Mux writing the stream to disk (which also
7 // knows which filesystem the file is going to), makes its calculations,
8 // and calls back to the MainWindow, which shows it to the user.
9 //
10 // The bitrate is measured over a simple 30-second sliding window.
11
12 #include "timebase.h"
13
14 #include <deque>
15 #include <functional>
16 #include <stdint.h>
17 #include <string>
18 #include <sys/types.h>
19
20 class DiskSpaceEstimator {
21 public:
22         typedef std::function<void(off_t free_bytes, double estimated_seconds_left)> callback_t;
23         DiskSpaceEstimator(callback_t callback);
24
25         // Report that a video frame with the given pts and size has just been
26         // written (possibly appended) to the given file.
27         //
28         // <pts> is taken to be in TIMEBASE units (see timebase.h).
29         void report_write(const std::string &filename, size_t bytes, uint64_t pts);
30
31 private:
32         static constexpr int64_t window_length = 30 * TIMEBASE;
33
34         callback_t callback;
35
36         struct MeasurePoint {
37                 uint64_t pts;
38                 off_t size;
39         };
40         std::deque<MeasurePoint> measure_points;
41         uint64_t last_pts_reported = 0;
42         off_t total_size = 0;
43 };
44
45 extern DiskSpaceEstimator *global_disk_space_estimator;
46
47 #endif  // !defined(_DISK_SPACE_ESTIMATOR_H)