]> git.sesse.net Git - nageru/blob - disk_space_estimator.h
Add a disk space estimator. Code largely borrowed from Nageru.
[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 <stdint.h>
13 #include <sys/types.h>
14 #include <deque>
15 #include <functional>
16 #include <string>
17
18 #include "timebase.h"
19
20 class DiskSpaceEstimator
21 {
22 public:
23         typedef std::function<void(off_t free_bytes, double estimated_seconds_left)> callback_t;
24         DiskSpaceEstimator(callback_t callback);
25
26         // Report that a video frame with the given pts has just been written
27         // to the given file, so the estimator should stat the file and see
28         // by big it is. (The file is assumed to hold only that single frame,
29         // unlike in Nageru, where it is a growing file.)
30         //
31         // If the filename changed since last time, the estimation is reset.
32         // <pts> is taken to be in TIMEBASE units (see timebase.h).
33         void report_write(const std::string &filename, uint64_t pts);
34
35 private:
36         static constexpr int64_t window_length = 30 * TIMEBASE;
37
38         callback_t callback;
39
40         struct MeasurePoint {
41                 uint64_t pts;
42                 off_t size;
43         };
44         std::deque<MeasurePoint> measure_points;
45         uint64_t last_pts_reported = 0;
46         off_t total_size = 0;
47 };
48
49 extern DiskSpaceEstimator *global_disk_space_estimator;
50
51 #endif  // !defined(_DISK_SPACE_ESTIMATOR_H)