]> git.sesse.net Git - nageru/blobdiff - shared/disk_space_estimator.h
Unify disk_space_estimator.cpp from Nageru and Futatabi.
[nageru] / shared / disk_space_estimator.h
similarity index 65%
rename from nageru/disk_space_estimator.h
rename to shared/disk_space_estimator.h
index ce81321638e2350adc9626d2d347fa8c3daa0b2e..163b7efd555dd81ac2df84a5fc8591f63b393673 100644 (file)
@@ -9,21 +9,26 @@
 //
 // The bitrate is measured over a simple 30-second sliding window.
 
-#include <stdint.h>
-#include <sys/types.h>
 #include <atomic>
 #include <deque>
 #include <functional>
+#include <stdint.h>
 #include <string>
+#include <sys/types.h>
 
 #include "shared/timebase.h"
 
-class DiskSpaceEstimator
-{
+class DiskSpaceEstimator {
 public:
        typedef std::function<void(off_t free_bytes, double estimated_seconds_left)> callback_t;
        DiskSpaceEstimator(callback_t callback);
 
+       // Report that a video frame with the given pts and size has just been
+       // written (possibly appended) to the given file.
+       //
+       // <pts> is taken to be in TIMEBASE units (see shared/timebase.h).
+       void report_write(const std::string &filename, off_t bytes, uint64_t pts);
+
        // Report that a video frame with the given pts has just been written
        // to the given file, so the estimator should stat the file and see
        // by how much it grew since last time. Called by the Mux object
@@ -31,13 +36,19 @@ public:
        //
        // If the filename changed since last time, the estimation is reset.
        // <pts> is taken to be in TIMEBASE units (see shared/timebase.h).
-       void report_write(const std::string &filename, uint64_t pts);
+       //
+       // You should probably not mix this and report_write() on the same
+       // object. Really, report_write() matches Futatabi's controlled writes
+       // to a custom format, and report_append() matches Nageru's use of Mux
+       // (where we don't see the bytes flowing past).
+       void report_append(const std::string &filename, uint64_t pts);
 
 private:
        static constexpr int64_t window_length = 30 * TIMEBASE;
 
+       void report_write_internal(const std::string &filename, off_t file_size, uint64_t pts);
+
        callback_t callback;
-       std::string last_filename;
 
        struct MeasurePoint {
                uint64_t pts;
@@ -46,6 +57,9 @@ private:
        std::deque<MeasurePoint> measure_points;
        uint64_t last_pts_reported = 0;
 
+       off_t total_size = 0;  // For report_write().
+       std::string last_filename;  // For report_append().
+
        // Metrics.
        std::atomic<int64_t> metric_disk_free_bytes{-1};
 };