deps-libff: Add reference counting to clock

Add referencing counting to determine when to release a clock Due to no fixed ownership of clocks (packets, frames, decoders and  refresh thread all may own a clock at some point).
This commit is contained in:
John Bradley 2015-03-12 15:24:15 -05:00
parent 164cbeeede
commit 5b3190593c
2 changed files with 64 additions and 0 deletions

View file

@ -15,8 +15,60 @@
*/
#include "ff-clock.h"
#include "ff-threading.h"
#include <libavutil/avutil.h>
double ff_get_sync_clock(struct ff_clock *clock)
{
return clock->sync_clock(clock->opaque);
}
struct ff_clock *ff_clock_init(struct ff_clock *clock)
{
clock = av_mallocz(sizeof(struct ff_clock));
if (clock == NULL)
return NULL;
if (pthread_mutex_init(&clock->mutex, NULL) != 0)
goto fail;
if (pthread_cond_init(&clock->cond, NULL) != 0)
goto fail1;
return clock;
fail1:
pthread_mutex_destroy(&clock->mutex);
fail:
av_free(clock);
return NULL;
}
struct ff_clock *ff_clock_retain(struct ff_clock *clock)
{
ff_atomic_inc_long(&clock->retain);
return clock;
}
struct ff_clock *ff_clock_move(struct ff_clock **clock)
{
struct ff_clock *retained_clock = ff_clock_retain(*clock);
ff_clock_release(clock);
return retained_clock;
}
void ff_clock_release(struct ff_clock **clock)
{
if (ff_atomic_dec_long(&(*clock)->retain) == 0) {
pthread_cond_destroy(&(*clock)->cond);
pthread_mutex_destroy(&(*clock)->mutex);
av_free(*clock);
}
*clock = NULL;
}

View file

@ -19,6 +19,9 @@
#define AV_SYNC_THRESHOLD 0.01
#define AV_NOSYNC_THRESHOLD 10.0
#include <stdbool.h>
#include <pthread.h>
enum ff_av_sync_type {
AV_SYNC_AUDIO_MASTER,
AV_SYNC_VIDEO_MASTER,
@ -30,9 +33,18 @@ typedef double (*ff_sync_clock)(void *opaque);
struct ff_clock {
ff_sync_clock sync_clock;
enum ff_av_sync_type sync_type;
pthread_mutex_t mutex;
pthread_cond_t cond;
volatile long retain;
void *opaque;
};
typedef struct ff_clock ff_clock_t;
struct ff_clock * ff_clock_init();
double ff_get_sync_clock(struct ff_clock *clock);
struct ff_clock *ff_clock_retain(struct ff_clock *clock);
struct ff_clock *ff_clock_move(struct ff_clock **clock);
void ff_clock_release(struct ff_clock **clock);