]> git.sesse.net Git - nageru/blob - futatabi/frame_on_disk.h
When sending original frames, do the reading in the queueing thread.
[nageru] / futatabi / frame_on_disk.h
1 #ifndef _FRAME_ON_DISK_H
2 #define _FRAME_ON_DISK_H 1
3
4 #include "defs.h"
5
6 #include <algorithm>
7 #include <mutex>
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
11
12 extern std::mutex frame_mu;
13 struct FrameOnDisk {
14         int64_t pts = -1;  // -1 means empty.
15         off_t offset;
16         unsigned filename_idx;
17         uint32_t size;  // Not using size_t saves a few bytes; we can have so many frames.
18 };
19 extern std::vector<FrameOnDisk> frames[MAX_STREAMS];  // Under frame_mu.
20 extern std::vector<std::string> frame_filenames;  // Under frame_mu.
21
22 static bool inline operator==(const FrameOnDisk &a, const FrameOnDisk &b)
23 {
24         return a.pts == b.pts &&
25                 a.offset == b.offset &&
26                 a.filename_idx == b.filename_idx &&
27                 a.size == b.size;
28 }
29
30 // A helper class to read frames from disk. It caches the file descriptor
31 // so that the kernel has a better chance of doing readahead when it sees
32 // the sequential reads. (For this reason, each display has a private
33 // FrameReader. Thus, we can easily keep multiple open file descriptors around
34 // for a single .frames file.)
35 //
36 // Thread-compatible, but not thread-safe.
37 class FrameReader {
38 public:
39         FrameReader();
40         ~FrameReader();
41         std::string read_frame(FrameOnDisk frame);
42
43 private:
44         int fd = -1;
45         int last_filename_idx = -1;
46 };
47
48 // Utility functions for dealing with binary search.
49 inline std::vector<FrameOnDisk>::iterator
50 find_last_frame_before(std::vector<FrameOnDisk> &frames, int64_t pts_origin)
51 {
52         return std::lower_bound(frames.begin(), frames.end(), pts_origin,
53                                 [](const FrameOnDisk &frame, int64_t pts) { return frame.pts < pts; });
54 }
55
56 inline std::vector<FrameOnDisk>::iterator
57 find_first_frame_at_or_after(std::vector<FrameOnDisk> &frames, int64_t pts_origin)
58 {
59         return std::upper_bound(frames.begin(), frames.end(), pts_origin - 1,
60                                 [](int64_t pts, const FrameOnDisk &frame) { return pts < frame.pts; });
61 }
62
63 #endif  // !defined(_FRAME_ON_DISK_H)