]> git.sesse.net Git - nageru/blob - jpeg_frame_view.cpp
Write some drop counters to stderr.
[nageru] / jpeg_frame_view.cpp
1 #include "jpeg_frame_view.h"
2
3 #include <stdint.h>
4
5 #include <atomic>
6 #include <condition_variable>
7 #include <deque>
8 #include <mutex>
9 #include <thread>
10 #include <utility>
11 #include <QGraphicsPixmapItem>
12 #include <QPixmap>
13
14 #include "defs.h"
15 #include "post_to_main_thread.h"
16
17 using namespace std;
18
19 string filename_for_frame(unsigned stream_idx, int64_t pts);
20
21 struct JPEGID {
22         unsigned stream_idx;
23         int64_t pts;
24 };
25 bool operator< (const JPEGID &a, const JPEGID &b) {
26         return make_pair(a.stream_idx, a.pts) < make_pair(b.stream_idx, b.pts);
27 }
28
29 struct LRUPixmap {
30         shared_ptr<QPixmap> pixmap;
31         size_t last_used;
32 };
33
34 mutex cache_mu;
35 map<JPEGID, LRUPixmap> cache;  // Under cache_mu.
36 condition_variable any_pending_decodes;
37 deque<pair<JPEGID, JPEGFrameView *>> pending_decodes;  // Under cache_mu.
38 atomic<size_t> event_counter{0};
39
40 void prune_cache()
41 {
42         // Assumes cache_mu is held.
43         vector<size_t> lru_timestamps;
44         for (const auto &key_and_value : cache) {
45                 lru_timestamps.push_back(key_and_value.second.last_used);
46         }
47
48         size_t cutoff_point = CACHE_SIZE / 10;  // Prune away the 10% oldest ones.
49         nth_element(lru_timestamps.begin(), lru_timestamps.begin() + cutoff_point, lru_timestamps.end());
50         size_t must_be_used_after = lru_timestamps[cutoff_point];
51         for (auto it = cache.begin(); it != cache.end(); ) {
52                 if (it->second.last_used < must_be_used_after) {
53                         it = cache.erase(it);
54                 } else {
55                         ++it;
56                 }
57         }
58 }
59
60 void jpeg_decoder_thread()
61 {
62         size_t num_decoded = 0, num_dropped = 0;
63
64         pthread_setname_np(pthread_self(), "JPEGDecoder");
65         for ( ;; ) {
66                 JPEGID id;
67                 JPEGFrameView *dest;
68                 shared_ptr<QPixmap> pixmap;
69                 {
70                         unique_lock<mutex> lock(cache_mu);
71                         any_pending_decodes.wait(lock, [] {
72                                 return !pending_decodes.empty();
73                         });
74                         id = pending_decodes.front().first;
75                         dest = pending_decodes.front().second;
76                         pending_decodes.pop_front();
77
78                         auto it = cache.find(id);
79                         if (it != cache.end()) {
80                                 pixmap = it->second.pixmap;
81                                 it->second.last_used = event_counter++;
82                         }
83                 }
84
85                 if (pixmap == nullptr) {
86                         // Not found in the cache, so we need to do a decode or drop the request.
87                         // Prune the queue if there are too many pending for this destination.
88                         // TODO: Could we get starvation here?
89                         size_t num_pending = 0;
90                         for (const pair<JPEGID, JPEGFrameView *> &decode : pending_decodes) {
91                                 if (decode.second == dest) {
92                                         ++num_pending;
93                                 }
94                         }
95                         if (num_pending > 3) {
96                                 ++num_dropped;
97                                 continue;
98                         }
99
100                         pixmap.reset(
101                                 new QPixmap(QString::fromStdString(filename_for_frame(id.stream_idx, id.pts))));
102
103                         unique_lock<mutex> lock(cache_mu);
104                         cache[id] = LRUPixmap{ pixmap, event_counter++ };
105
106                         if (cache.size() > CACHE_SIZE) {
107                                 prune_cache();
108                         }
109                         ++num_decoded;
110                         if (num_decoded % 1000 == 0) {
111                                 fprintf(stderr, "Decoded %zu images, dropped %zu (%.2f%% dropped)\n",
112                                         num_decoded, num_dropped, (100.0 * num_dropped) / (num_decoded + num_dropped));
113                         }
114                 }
115
116                 dest->setPixmap(pixmap);
117         }
118 }
119
120 JPEGFrameView::JPEGFrameView(QWidget *parent)
121         : QGraphicsView(parent) {
122         scene.addItem(&item);
123         setScene(&scene);
124         setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
125         setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
126
127         static once_flag once;
128         call_once(once, [] {
129                 std::thread(&jpeg_decoder_thread).detach();
130         });
131 }
132
133 void JPEGFrameView::update_frame()
134 {
135         unique_lock<mutex> lock(cache_mu);
136         pending_decodes.emplace_back(JPEGID{ stream_idx, pts }, this);
137         any_pending_decodes.notify_all();
138 }
139
140 void JPEGFrameView::resizeEvent(QResizeEvent *event)
141 {
142         fitInView(&item, Qt::KeepAspectRatio);
143 }
144
145 void JPEGFrameView::setPixmap(std::shared_ptr<QPixmap> pixmap)
146 {
147         post_to_main_thread([this, pixmap] {
148                 item.setPixmap(*pixmap);
149                 fitInView(&item, Qt::KeepAspectRatio);
150         });
151 }