Skip to content

Commit 5f7a674

Browse files
committed
ffserver.c/lmhttp.c/publisher.c/publisher.h: Adopt new AVFifo API in favor of deprecated AVFifoBuffer API.
Signed-off-by: Stephan Holljes <[email protected]>
1 parent 5e1c7b9 commit 5f7a674

File tree

4 files changed

+48
-48
lines changed

4 files changed

+48
-48
lines changed

ffserver.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,14 @@ void write_segment(struct Client *c)
367367
int pkt_count = 0;
368368
AVRational tb = {1, AV_TIME_BASE};
369369
pthread_mutex_lock(&c->buffer_lock);
370-
if (av_fifo_size(c->buffer) > 0) {
370+
if (av_fifo_can_read(c->buffer)) {
371371
AVFormatContext *fmt_ctx;
372372
AVIOContext *avio_ctx;
373373
AVPacket pkt;
374374
struct SegmentReadInfo info;
375375
unsigned char *avio_buffer;
376376

377-
av_fifo_generic_peek(c->buffer, &seg, sizeof(seg), NULL);
377+
av_fifo_peek(c->buffer, &seg, 1, 0);
378378
pthread_mutex_unlock(&c->buffer_lock);
379379
c->current_segment_id = seg->id;
380380
info.buf = seg->buf;
@@ -453,7 +453,7 @@ void write_segment(struct Client *c)
453453
avformat_close_input(&fmt_ctx);
454454
avio_context_free(&avio_ctx);
455455
pthread_mutex_lock(&c->buffer_lock);
456-
av_fifo_drain(c->buffer, sizeof(seg));
456+
av_fifo_drain2(c->buffer, 1);
457457
pthread_mutex_unlock(&c->buffer_lock);
458458
segment_unref(seg);
459459
client_set_state(c, WRITABLE);

lmhttpd.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040

4141
struct MHDServer {
4242
struct MHD_Daemon *daemon;
43-
AVFifoBuffer *clients;
43+
AVFifo *clients;
4444
};
4545

4646
struct ConnectionInfo {
47-
AVFifoBuffer *buffer;
47+
AVFifo *buffer;
4848
pthread_mutex_t buffer_lock;
4949
struct MHD_Connection *connection;
5050
int close_connection;
@@ -59,15 +59,15 @@ ssize_t helper_callback(void *cls, uint64_t pos, char *buf, size_t max)
5959
{
6060
struct ConnectionInfo *cinfo = (struct ConnectionInfo*) cls;
6161
pthread_mutex_lock(&cinfo->buffer_lock);
62-
int buf_size = av_fifo_size(cinfo->buffer);
62+
int buf_size = av_fifo_can_read(cinfo->buffer);
6363
if (buf_size > 0) {
6464
max = max > buf_size ? buf_size : max;
65-
av_fifo_generic_read(cinfo->buffer, buf, max, NULL);
65+
av_fifo_read(cinfo->buffer, buf, max);
6666
} else {
6767
max = 0;
6868
}
6969
if (max == 0 && cinfo->close_connection) {
70-
av_fifo_free(cinfo->buffer);
70+
av_fifo_freep2(&cinfo->buffer);
7171
pthread_mutex_unlock(&cinfo->buffer_lock);
7272
av_free(cinfo);
7373
return MHD_CONTENT_READER_END_OF_STREAM;
@@ -123,8 +123,8 @@ static enum MHD_Result answer_to_connection (void *cls, struct MHD_Connection *c
123123
// no locking needed, running on the same thread
124124

125125
if (!strcmp("GET", method)) {
126-
if (av_fifo_space(server->clients) > sizeof(struct HTTPClient*)) {
127-
cinfo->buffer = av_fifo_alloc(INITIAL_BUFSIZE);
126+
if (av_fifo_can_write(server->clients) > sizeof(struct HTTPClient*)) {
127+
cinfo->buffer = av_fifo_alloc2(INITIAL_BUFSIZE, 1, 0);
128128
cinfo->connection = connection;
129129
cinfo->close_connection = 0;
130130

@@ -134,7 +134,7 @@ static enum MHD_Result answer_to_connection (void *cls, struct MHD_Connection *c
134134
client->resource = av_strdup(url);
135135
client->method = av_strdup(method);
136136
client->httpd_data = cinfo;
137-
av_fifo_generic_write(server->clients, &client, sizeof(struct HTTPClient*), NULL);
137+
av_fifo_write(server->clients, &client, 1);
138138
}
139139
response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN,
140140
1024,
@@ -166,7 +166,7 @@ int lmhttpd_init(void **server, struct HTTPDConfig config) {
166166
av_log(NULL, AV_LOG_ERROR, "Could not allocate MHDServer struct\n");
167167
return -1;
168168
}
169-
server_p->clients = av_fifo_alloc_array(sizeof(struct HTTPClient), MAX_CLIENTS);
169+
server_p->clients = av_fifo_alloc2(MAX_CLIENTS, sizeof(struct HTTPClient), 0);
170170
server_p->daemon = MHD_start_daemon (0, config.port, NULL, NULL,
171171
&answer_to_connection, server_p, MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 10, MHD_OPTION_END);
172172
if (!server_p->daemon || !server_p->clients) {
@@ -208,8 +208,8 @@ int lmhttpd_accept(void *server, struct HTTPClient **client, const char **valid_
208208
// run read and write operations
209209
if (MHD_run_from_select(s->daemon, &rs, &ws, &es) != MHD_YES)
210210
return HTTPD_OTHER_ERROR;
211-
if(av_fifo_size(s->clients)) {
212-
av_fifo_generic_read(s->clients, client, sizeof(struct HTTPClient*), NULL);
211+
if(av_fifo_can_read(s->clients)) {
212+
av_fifo_read(s->clients, client, 1);
213213

214214
return 0;
215215
}
@@ -225,8 +225,8 @@ int lmhttpd_write(void *server, struct HTTPClient *client, const unsigned char *
225225
struct ConnectionInfo* cinfo = (struct ConnectionInfo*) client->httpd_data;
226226
int ret;
227227
pthread_mutex_lock(&cinfo->buffer_lock);
228-
if (!cinfo->close_connection && av_fifo_space(cinfo->buffer) >= size) {
229-
ret = av_fifo_generic_write(cinfo->buffer, (void*) buf, size, NULL);
228+
if (!cinfo->close_connection && av_fifo_can_write(cinfo->buffer) >= size) {
229+
ret = av_fifo_write(cinfo->buffer, (void*) buf, size);
230230

231231
} else {
232232
ret = -1;
@@ -296,7 +296,7 @@ void lmhttpd_shutdown(void *server)
296296
}
297297

298298
MHD_stop_daemon(mhd_server->daemon);
299-
av_fifo_free(mhd_server->clients);
299+
av_fifo_freep2(&mhd_server->clients);
300300
av_free(mhd_server);
301301
}
302302

publisher.c

+28-28
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ void client_disconnect(struct Client *c, int write_trailer)
6262
c->ofmt_ctx = NULL;
6363
c->ffinfo = NULL;
6464
pthread_mutex_lock(&c->buffer_lock);
65-
while(av_fifo_size(c->buffer)) {
66-
av_fifo_generic_read(c->buffer, &seg, sizeof(struct Segment*), NULL);
65+
while(av_fifo_can_read(c->buffer)) {
66+
av_fifo_read(c->buffer, &seg, 1);
6767
segment_unref(seg);
6868
}
6969
pthread_mutex_unlock(&c->buffer_lock);
@@ -81,14 +81,14 @@ void client_set_state(struct Client *c, enum State state)
8181
void client_push_segment(struct Client *c, struct Segment *seg)
8282
{
8383
pthread_mutex_lock(&c->buffer_lock);
84-
if (av_fifo_space(c->buffer) == 0) {
84+
if (av_fifo_can_write(c->buffer) == 0) {
8585
av_log(NULL, AV_LOG_WARNING, "Client buffer full, dropping Segment.\n");
8686
client_set_state(c, BUFFER_FULL);
8787
pthread_mutex_unlock(&c->buffer_lock);
8888
return;
8989
}
9090
segment_ref(seg);
91-
av_fifo_generic_write(c->buffer, &seg, sizeof(struct Segment*), NULL);
91+
av_fifo_write(c->buffer, &seg, 1);
9292
pthread_mutex_unlock(&c->buffer_lock);
9393
client_set_state(c, WRITABLE);
9494
}
@@ -105,24 +105,24 @@ void publisher_init(struct PublisherContext **pub, char *stream_name)
105105
pc->stream_name = stream_name;
106106
pc->current_segment_id = -1;
107107
pc->shutdown = 0;
108-
pc->buffer = av_fifo_alloc_array(sizeof(struct Segment), MAX_SEGMENTS);
108+
pc->buffer = av_fifo_alloc2(MAX_SEGMENTS, sizeof(struct Segment*), 0);
109109
if (!pc->buffer) {
110110
av_log(NULL, AV_LOG_ERROR, "Could not allocate publisher buffer.\n");
111111
av_free(pc);
112112
return;
113113
}
114-
pc->fs_buffer = av_fifo_alloc_array(sizeof(struct Segment), MAX_SEGMENTS);
114+
pc->fs_buffer = av_fifo_alloc2(MAX_SEGMENTS, sizeof(struct Segment*), 0);
115115
if (!pc->fs_buffer) {
116116
av_log(NULL, AV_LOG_ERROR, "Could not allocate publisher fast-start buffer.\n");
117-
av_fifo_free(pc->buffer);
117+
av_fifo_freep2(&pc->buffer);
118118
av_free(pc);
119119
return;
120120
}
121121
pthread_mutex_init(&pc->buffer_lock, NULL);
122122
pthread_mutex_init(&pc->fs_buffer_lock, NULL);
123123
for (i = 0; i < MAX_CLIENTS; i++) {
124124
struct Client *c = &pc->clients[i];
125-
c->buffer = av_fifo_alloc_array(sizeof(struct Segment), MAX_SEGMENTS);
125+
c->buffer = av_fifo_alloc2(MAX_SEGMENTS, sizeof(struct Segment*), 0);
126126
if (!c->buffer) {
127127
av_log(NULL, AV_LOG_ERROR, "Could not allocate client buffer.\n");
128128
publisher_free(pc);
@@ -144,13 +144,13 @@ void publisher_push_segment(struct PublisherContext *pub, struct Segment *seg)
144144
struct Segment *drop;
145145
pthread_mutex_lock(&pub->buffer_lock);
146146
pthread_mutex_lock(&pub->fs_buffer_lock);
147-
av_fifo_generic_write(pub->buffer, &seg, sizeof(struct Segment*), NULL);
147+
av_fifo_write(pub->buffer, &seg, 1);
148148
segment_ref(seg);
149-
if (av_fifo_size(pub->fs_buffer) >= BUFFER_SEGMENTS * sizeof(struct Segment*)) {
150-
av_fifo_generic_read(pub->fs_buffer, &drop, sizeof(struct Segment*), NULL);
149+
if (av_fifo_can_read(pub->fs_buffer) >= BUFFER_SEGMENTS) {
150+
av_fifo_read(pub->fs_buffer, &drop, 1);
151151
segment_unref(drop);
152152
}
153-
av_fifo_generic_write(pub->fs_buffer, &seg, sizeof(struct Segment*), NULL);
153+
av_fifo_write(pub->fs_buffer, &seg, 1);
154154
pthread_mutex_unlock(&pub->buffer_lock);
155155
pthread_mutex_unlock(&pub->fs_buffer_lock);
156156
segment_ref(seg);
@@ -192,9 +192,9 @@ void client_push_prebuffer(struct PublisherContext *pub, struct Client *c)
192192
int size;
193193
struct Segment *seg;
194194
pthread_mutex_lock(&pub->fs_buffer_lock);
195-
size = av_fifo_size(pub->fs_buffer);
196-
for (off = 0; off < size; off += sizeof(struct Segment*)) {
197-
av_fifo_generic_peek_at(pub->fs_buffer, &seg, off, sizeof(struct Segment*), NULL);
195+
size = av_fifo_can_read(pub->fs_buffer);
196+
for (off = 0; off < size; off++) {
197+
av_fifo_peek(pub->fs_buffer, &seg, 1, off);
198198
client_push_segment(c, seg);
199199
}
200200
pthread_mutex_unlock(&pub->fs_buffer_lock);
@@ -222,21 +222,21 @@ void publisher_free(struct PublisherContext *pub)
222222
int i;
223223
struct Segment *seg;
224224
pthread_mutex_lock(&pub->buffer_lock);
225-
while(av_fifo_size(pub->buffer)) {
226-
av_fifo_generic_read(pub->buffer, &seg, sizeof(struct Segment*), NULL);
225+
while(av_fifo_can_read(pub->buffer)) {
226+
av_fifo_read(pub->buffer, &seg, 1);
227227
segment_unref(seg);
228228
}
229-
av_fifo_freep(&pub->buffer);
229+
av_fifo_freep2(&pub->buffer);
230230
pthread_mutex_unlock(&pub->buffer_lock);
231-
231+
232232
pthread_mutex_lock(&pub->fs_buffer_lock);
233-
while(av_fifo_size(pub->fs_buffer)) {
234-
av_fifo_generic_read(pub->fs_buffer, &seg, sizeof(struct Segment*), NULL);
233+
while(av_fifo_can_read(pub->fs_buffer)) {
234+
av_fifo_read(pub->fs_buffer, &seg, 1);
235235
segment_unref(seg);
236236
}
237-
av_fifo_freep(&pub->fs_buffer);
237+
av_fifo_freep2(&pub->fs_buffer);
238238
for (i = 0; i < MAX_CLIENTS; i++) {
239-
av_fifo_freep(&pub->clients[i].buffer);
239+
av_fifo_freep2(&pub->clients[i].buffer);
240240
}
241241
pthread_mutex_unlock(&pub->fs_buffer_lock);
242242
av_free(pub);
@@ -256,18 +256,18 @@ void publish(struct PublisherContext *pub)
256256
struct Segment *seg;
257257
char filename[128] = {0};
258258
pthread_mutex_lock(&pub->buffer_lock);
259-
av_log(NULL, AV_LOG_DEBUG, "pub->buffer size: %d\n", av_fifo_size(pub->buffer));
260-
if (av_fifo_size(pub->buffer) == 0) {
259+
av_log(NULL, AV_LOG_DEBUG, "pub->buffer size: %ld\n", av_fifo_can_read(pub->buffer));
260+
if (av_fifo_can_read(pub->buffer) == 0) {
261261
pthread_mutex_unlock(&pub->buffer_lock);
262262
return;
263263
}
264-
av_fifo_generic_read(pub->buffer, &seg, sizeof(struct Segment*), NULL);
264+
av_fifo_read(pub->buffer, &seg, 1);
265265
pthread_mutex_unlock(&pub->buffer_lock);
266266
if (seg) {
267267
pub->current_segment_id = seg->id;
268268
snprintf(filename, 127, "segment-%04d.mkv", seg->id);
269269
// segment_save(seg, filename);
270-
270+
271271
for (i = 0; i < MAX_CLIENTS; i++) {
272272
switch(pub->clients[i].state) {
273273
case BUFFER_FULL:
@@ -304,7 +304,7 @@ void publisher_gen_status_json(struct PublisherContext *pub, char *status)
304304
}
305305
states[c->state]++;
306306
}
307-
307+
308308

309309
snprintf(status, 4095,
310310
"{\n\t\"free\": %d,\n"

publisher.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ enum State {
4444

4545
struct Client {
4646
AVFormatContext *ofmt_ctx; // writable AVFormatContext, basically our tcp connection to the client
47-
AVFifoBuffer *buffer; // Client buffer of Segment references
47+
AVFifo *buffer; // Client buffer of Segment references
4848
char *method;
4949
char *resource;
5050
struct FFServerInfo *ffinfo;
@@ -57,8 +57,8 @@ struct Client {
5757

5858
struct PublisherContext {
5959
struct Client clients[MAX_CLIENTS]; // currently compile-time configuration, easly made dynamic with malloc?
60-
AVFifoBuffer *buffer; // publisher buffer for new Segments
61-
AVFifoBuffer *fs_buffer; // fast start buffer
60+
AVFifo *buffer; // publisher buffer for new Segments
61+
AVFifo *fs_buffer; // fast start buffer
6262
pthread_mutex_t buffer_lock;
6363
pthread_mutex_t fs_buffer_lock;
6464
int nb_threads;

0 commit comments

Comments
 (0)