-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
buffer_lock.c
197 lines (161 loc) · 5.23 KB
/
buffer_lock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "device/buffer_lock.h"
#include "device/buffer_list.h"
#include "device/buffer.h"
#include "util/opts/log.h"
bool buffer_lock_is_used(buffer_lock_t *buf_lock)
{
int refs = 0;
pthread_mutex_lock(&buf_lock->lock);
refs = buf_lock->refs;
pthread_mutex_unlock(&buf_lock->lock);
return refs;
}
void buffer_lock_use(buffer_lock_t *buf_lock, int ref)
{
pthread_mutex_lock(&buf_lock->lock);
buf_lock->refs += ref;
pthread_mutex_unlock(&buf_lock->lock);
}
bool buffer_lock_needs_buffer(buffer_lock_t *buf_lock)
{
uint64_t now = get_monotonic_time_us(NULL, NULL);
bool needs_buffer = false;
pthread_mutex_lock(&buf_lock->lock);
if (buf_lock->timeout_us > 0 && now - buf_lock->buf_time_us > buf_lock->timeout_us) {
buffer_consumed(buf_lock->buf, buf_lock->name);
buf_lock->buf = NULL;
}
if (buf_lock->refs > 0) {
needs_buffer = true;
}
for (int i = 0; !needs_buffer && buf_lock->check_streaming[i] && i < BUFFER_LOCK_MAX_CALLBACKS; i++) {
needs_buffer = buf_lock->check_streaming[i](buf_lock);
}
pthread_mutex_unlock(&buf_lock->lock);
return needs_buffer;
}
static void buffer_lock_clear_buffers(buffer_lock_t *buf_lock, uint64_t now)
{
buffer_consumed(buf_lock->buf, buf_lock->name);
buf_lock->buf = NULL;
buf_lock->buf_time_us = now;
}
static void buffer_lock_set_buffer(buffer_lock_t *buf_lock, buffer_t *buf, uint64_t now)
{
buffer_consumed(buf_lock->buf, buf_lock->name);
buffer_use(buf);
buf_lock->buf = buf;
buf_lock->buf_time_us = now;
buf_lock->counter++;
LOG_DEBUG(buf_lock, "Captured buffer %s (refs=%d), frame=%d/%d, processing_ms=%.1f, frame_ms=%.1f",
dev_name(buf), buf ? buf->mmap_reflinks : 0,
buf_lock->counter, buf_lock->dropped,
(now - buf->captured_time_us) / 1000.0f,
(now - buf_lock->buf_time_us) / 1000.0f);
pthread_cond_broadcast(&buf_lock->cond_wait);
for (int i = 0; buf_lock->notify_buffer[i] && i < BUFFER_LOCK_MAX_CALLBACKS; i++) {
buf_lock->notify_buffer[i](buf_lock, buf);
}
}
void buffer_lock_capture(buffer_lock_t *buf_lock, buffer_t *buf)
{
uint64_t now = get_monotonic_time_us(NULL, NULL);
pthread_mutex_lock(&buf_lock->lock);
if (!buf) {
buffer_lock_clear_buffers(buf_lock, now);
} else if (buf->flags.is_keyframe) {
buffer_lock_set_buffer(buf_lock, buf, now);
} else if (now - buf_lock->buf_time_us >= buf_lock->frame_interval_ms * 1000) {
buffer_lock_set_buffer(buf_lock, buf, now);
} else {
buf_lock->dropped++;
LOG_DEBUG(buf_lock, "Dropped buffer %s (refs=%d), frame=%d/%d, frame_ms=%.1f",
dev_name(buf), buf ? buf->mmap_reflinks : 0,
buf_lock->counter, buf_lock->dropped,
(now - buf->captured_time_us) / 1000.0f);
}
pthread_mutex_unlock(&buf_lock->lock);
}
buffer_t *buffer_lock_get(buffer_lock_t *buf_lock, int timeout_ms, int *counter)
{
buffer_t *buf = NULL;
struct timespec timeout;
if(!timeout_ms)
timeout_ms = DEFAULT_BUFFER_LOCK_GET_TIMEOUT;
get_time_us(CLOCK_REALTIME, &timeout, NULL, timeout_ms * 1000LL);
pthread_mutex_lock(&buf_lock->lock);
if (*counter == buf_lock->counter || !buf_lock->buf) {
int ret = pthread_cond_timedwait(&buf_lock->cond_wait, &buf_lock->lock, &timeout);
if (ret == ETIMEDOUT) {
goto ret;
} else if (ret < 0) {
goto ret;
}
}
buf = buf_lock->buf;
buffer_use(buf);
*counter = buf_lock->counter;
ret:
pthread_mutex_unlock(&buf_lock->lock);
return buf;
}
int buffer_lock_write_loop(buffer_lock_t *buf_lock, int nframes, unsigned timeout_ms, buffer_write_fn fn, void *data)
{
int counter = 0;
int frames = 0;
uint64_t deadline_ms = get_monotonic_time_us(NULL, NULL) + DEFAULT_BUFFER_LOCK_GET_TIMEOUT * 1000LL;
uint64_t frame_stop_ms = get_monotonic_time_us(NULL, NULL) + timeout_ms * 1000LL;
buffer_lock_use(buf_lock, 1);
while (nframes == 0 || frames < nframes) {
if (timeout_ms && frame_stop_ms < get_monotonic_time_us(NULL, NULL)) {
break;
}
buffer_t *buf = buffer_lock_get(buf_lock, 0, &counter);
if (!buf) {
goto error;
}
int ret = fn(buf_lock, buf, frames, data);
buffer_consumed(buf, "write-loop");
if (ret > 0) {
frames++;
} else if (ret < 0) {
goto error;
} else if(!frames && deadline_ms < get_monotonic_time_us(NULL, NULL)) {
LOG_DEBUG(buf_lock, "Deadline getting frame elapsed.");
goto error;
}
}
buffer_lock_use(buf_lock, -1);
return frames;
error:
buffer_lock_use(buf_lock, -1);
return -frames;
}
bool buffer_lock_register_check_streaming(buffer_lock_t *buf_lock, buffer_lock_check_streaming check_streaming)
{
bool ret = false;
pthread_mutex_lock(&buf_lock->lock);
for (int i = 0; i < BUFFER_LOCK_MAX_CALLBACKS; i++) {
if (!buf_lock->check_streaming[i]) {
buf_lock->check_streaming[i] = check_streaming;
ret = true;
break;
}
}
pthread_mutex_unlock(&buf_lock->lock);
return ret;
}
bool buffer_lock_register_notify_buffer(buffer_lock_t *buf_lock, buffer_lock_notify_buffer notify_buffer)
{
bool ret = false;
pthread_mutex_lock(&buf_lock->lock);
for (int i = 0; i < BUFFER_LOCK_MAX_CALLBACKS; i++) {
if (!buf_lock->notify_buffer[i]) {
buf_lock->notify_buffer[i] = notify_buffer;
ret = true;
break;
}
}
pthread_mutex_unlock(&buf_lock->lock);
return ret;
}