]> git.sesse.net Git - nageru/blob - disk_space_estimator.h
Support audio-only FFmpeg inputs. Somewhat wonky, though.
[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 <atomic>
15 #include <deque>
16 #include <functional>
17 #include <string>
18
19 #include "timebase.h"
20
21 class DiskSpaceEstimator
22 {
23 public:
24         typedef std::function<void(off_t free_bytes, double estimated_seconds_left)> callback_t;
25         DiskSpaceEstimator(callback_t callback);
26
27         // Report that a video frame with the given pts has just been written
28         // to the given file, so the estimator should stat the file and see
29         // by how much it grew since last time. Called by the Mux object
30         // responsible for writing to the stream on disk.
31         //
32         // If the filename changed since last time, the estimation is reset.
33         // <pts> is taken to be in TIMEBASE units (see timebase.h).
34         void report_write(const std::string &filename, uint64_t pts);
35
36 private:
37         static constexpr int64_t window_length = 30 * TIMEBASE;
38
39         callback_t callback;
40         std::string last_filename;
41
42         struct MeasurePoint {
43                 uint64_t pts;
44                 off_t size;
45         };
46         std::deque<MeasurePoint> measure_points;
47         uint64_t last_pts_reported = 0;
48
49         // Metrics.
50         std::atomic<int64_t> metric_disk_free_bytes{-1};
51 };
52
53 extern DiskSpaceEstimator *global_disk_space_estimator;
54
55 #endif  // !defined(_DISK_SPACE_ESTIMATOR_H)