Use generic FIFO queue for recording

Replace the specific recording queue by the new generic FIFO queue
implementation.
This commit is contained in:
Romain Vimont 2019-08-01 23:15:47 +02:00
parent 53b6ee2cf4
commit 5e4ccfd832
2 changed files with 19 additions and 63 deletions

View file

@ -37,7 +37,6 @@ record_packet_new(const AVPacket *packet) {
SDL_free(rec); SDL_free(rec);
return NULL; return NULL;
} }
rec->next = NULL;
return rec; return rec;
} }
@ -47,60 +46,13 @@ record_packet_delete(struct record_packet *rec) {
SDL_free(rec); SDL_free(rec);
} }
static void
recorder_queue_init(struct recorder_queue *queue) {
queue->first = NULL;
// queue->last is undefined if queue->first == NULL
}
static inline bool
recorder_queue_is_empty(struct recorder_queue *queue) {
return !queue->first;
}
static bool
recorder_queue_push(struct recorder_queue *queue, const AVPacket *packet) {
struct record_packet *rec = record_packet_new(packet);
if (!rec) {
LOGC("Could not allocate record packet");
return false;
}
rec->next = NULL;
if (recorder_queue_is_empty(queue)) {
queue->first = queue->last = rec;
} else {
// chain rec after the (current) last packet
queue->last->next = rec;
// the last packet is now rec
queue->last = rec;
}
return true;
}
static inline struct record_packet *
recorder_queue_take(struct recorder_queue *queue) {
SDL_assert(!recorder_queue_is_empty(queue));
struct record_packet *rec = queue->first;
SDL_assert(rec);
queue->first = rec->next;
// no need to update queue->last if the queue is left empty:
// queue->last is undefined if queue->first == NULL
return rec;
}
static void static void
recorder_queue_clear(struct recorder_queue *queue) { recorder_queue_clear(struct recorder_queue *queue) {
struct record_packet *rec = queue->first; while (!queue_is_empty(queue)) {
while (rec) { struct record_packet *rec;
struct record_packet *current = rec; queue_take(queue, next, &rec);
rec = rec->next; record_packet_delete(rec);
record_packet_delete(current);
} }
queue->first = NULL;
} }
bool bool
@ -129,7 +81,7 @@ recorder_init(struct recorder *recorder,
return false; return false;
} }
recorder_queue_init(&recorder->queue); queue_init(&recorder->queue);
recorder->stopped = false; recorder->stopped = false;
recorder->failed = false; recorder->failed = false;
recorder->format = format; recorder->format = format;
@ -296,20 +248,20 @@ run_recorder(void *data) {
for (;;) { for (;;) {
mutex_lock(recorder->mutex); mutex_lock(recorder->mutex);
while (!recorder->stopped && while (!recorder->stopped && queue_is_empty(&recorder->queue)) {
recorder_queue_is_empty(&recorder->queue)) {
cond_wait(recorder->queue_cond, recorder->mutex); cond_wait(recorder->queue_cond, recorder->mutex);
} }
// if stopped is set, continue to process the remaining events (to // if stopped is set, continue to process the remaining events (to
// finish the recording) before actually stopping // finish the recording) before actually stopping
if (recorder->stopped && recorder_queue_is_empty(&recorder->queue)) { if (recorder->stopped && queue_is_empty(&recorder->queue)) {
mutex_unlock(recorder->mutex); mutex_unlock(recorder->mutex);
break; break;
} }
struct record_packet *rec = recorder_queue_take(&recorder->queue); struct record_packet *rec;
queue_take(&recorder->queue, next, &rec);
mutex_unlock(recorder->mutex); mutex_unlock(recorder->mutex);
@ -369,9 +321,15 @@ recorder_push(struct recorder *recorder, const AVPacket *packet) {
return false; return false;
} }
bool ok = recorder_queue_push(&recorder->queue, packet); struct record_packet *rec = record_packet_new(packet);
if (!rec) {
LOGC("Could not allocate record packet");
return false;
}
queue_push(&recorder->queue, next, rec);
cond_signal(recorder->queue_cond); cond_signal(recorder->queue_cond);
mutex_unlock(recorder->mutex); mutex_unlock(recorder->mutex);
return ok; return true;
} }

View file

@ -7,6 +7,7 @@
#include <SDL2/SDL_thread.h> #include <SDL2/SDL_thread.h>
#include "common.h" #include "common.h"
#include "queue.h"
enum recorder_format { enum recorder_format {
RECORDER_FORMAT_MP4 = 1, RECORDER_FORMAT_MP4 = 1,
@ -18,10 +19,7 @@ struct record_packet {
struct record_packet *next; struct record_packet *next;
}; };
struct recorder_queue { struct recorder_queue QUEUE(struct record_packet);
struct record_packet *first;
struct record_packet *last; // undefined if first is NULL
};
struct recorder { struct recorder {
char *filename; char *filename;