Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
timeshift: fix file refcounting, create timeshift_seek_t to maintain …
…seek state correctly
  • Loading branch information
perexg committed Jan 4, 2016
1 parent 545dc38 commit 80a50ce
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 133 deletions.
2 changes: 2 additions & 0 deletions src/timeshift.c
Expand Up @@ -429,6 +429,8 @@ streaming_target_t *timeshift_create
ts->buf_time = 0;
ts->start_pts = 0;
ts->ref_time = 0;
ts->seek.file = NULL;
ts->seek.frame = NULL;
for (i = 0; i < TIMESHIFT_BACKLOG_MAX; i++)
TAILQ_INIT(&ts->backlog[i]);
pthread_mutex_init(&ts->state_mutex, NULL);
Expand Down
41 changes: 40 additions & 1 deletion src/timeshift/private.h
Expand Up @@ -19,7 +19,7 @@
#ifndef __TVH_TIMESHIFT_PRIVATE_H__
#define __TVH_TIMESHIFT_PRIVATE_H__

#define TIMESHIFT_PLAY_BUF 1000000 //< us to buffer in TX
#define TIMESHIFT_PLAY_BUF 1000000 //< us to buffer in TX
#define TIMESHIFT_FILE_PERIOD 60 //< number of secs in each buffer file
#define TIMESHIFT_BACKLOG_MAX 16 //< maximum elementary streams

Expand Down Expand Up @@ -79,6 +79,14 @@ typedef struct timeshift_file

typedef TAILQ_HEAD(timeshift_file_list,timeshift_file) timeshift_file_list_t;

/**
*
*/
typedef struct timeshift_seek {
timeshift_file_t *file;
timeshift_index_iframe_t *frame;
} timeshift_seek_t;

/**
*
*/
Expand Down Expand Up @@ -110,6 +118,8 @@ typedef struct timeshift {
pthread_mutex_t state_mutex; ///< Protect state changes
uint8_t exit; ///< Exit from the main input thread
uint8_t full; ///< Buffer is full

timeshift_seek_t seek; ///< Seek into buffered data

streaming_queue_t wr_queue; ///< Writer queue
pthread_t wr_thread; ///< Writer thread
Expand Down Expand Up @@ -165,6 +175,35 @@ void timeshift_filemgr_init ( void );
void timeshift_filemgr_term ( void );
int timeshift_filemgr_makedirs ( int ts_index, char *buf, size_t len );

static inline void timeshift_file_get0 ( timeshift_file_t *tsf )
{
if (tsf)
tsf->refcount++;
}

#define timeshift_file_get(tsf) ({ \
timeshift_file_get0(tsf); \
/* tvhtrace("timeshift", "file get %p refcount %d fcn %s:%d", \
tsf, tsf ? tsf->refcount : -1, __FUNCTION__, __LINE__); */ \
tsf; \
})

static inline void timeshift_file_put0 ( timeshift_file_t *tsf )
{
if (tsf) {
if (!tsf->refcount)
abort();

This comment has been minimized.

Copy link
@Jalle19

Jalle19 Jan 4, 2016

Contributor

Missing return?

This comment has been minimized.

Copy link
@perexg

perexg Jan 5, 2016

Author Contributor

No, but changed a bit in 3868710 .

tsf->refcount--;
}
}

#define timeshift_file_put(tsf) ({ \
/* tvhtrace("timeshift", "file put %p refcount %d fcn %s:%d", \
tsf, tsf ? (tsf->refcount - 1) : -1, __FUNCTION__, __LINE__); */ \
timeshift_file_put0(tsf); \
tsf; \
})

timeshift_file_t *timeshift_filemgr_get
( timeshift_t *ts, int64_t start_time );
timeshift_file_t *timeshift_filemgr_oldest
Expand Down
34 changes: 12 additions & 22 deletions src/timeshift/timeshift_filemgr.c
Expand Up @@ -129,8 +129,8 @@ timeshift_filemgr_dump0 ( timeshift_t *ts )
return;
}
TAILQ_FOREACH(tsf, &ts->files, link) {
tvhtrace("timeshift", "ts %d file dump tsf %p time %"PRId64" last %"PRId64,
ts->id, tsf, tsf->time, tsf->last);
tvhtrace("timeshift", "ts %d (full=%d) file dump tsf %p time %4"PRId64" last %10"PRId64" bad %d refcnt %d",
ts->id, ts->full, tsf, tsf->time, tsf->last, tsf->bad, tsf->refcount);
}
}

Expand Down Expand Up @@ -358,9 +358,7 @@ timeshift_file_t *timeshift_filemgr_get ( timeshift_t *ts, int64_t start_time )
tsf_tl = tsf_tmp;
}

if (tsf_tl)
tsf_tl->refcount++;
return tsf_tl;
return timeshift_file_get(tsf_tl);
}

timeshift_file_t *timeshift_filemgr_next
Expand All @@ -369,22 +367,18 @@ timeshift_file_t *timeshift_filemgr_next
timeshift_file_t *nxt = TAILQ_NEXT(tsf, link);
if (!nxt && end) *end = 1;
if (!nxt && keep) return tsf;
tsf->refcount--;
if (nxt)
nxt->refcount++;
return nxt;
timeshift_file_put(tsf);
return timeshift_file_get(nxt);
}

timeshift_file_t *timeshift_filemgr_prev
( timeshift_file_t *tsf, int *end, int keep )
{
timeshift_file_t *nxt = TAILQ_PREV(tsf, timeshift_file_list, link);
if (!nxt && end) *end = 1;
if (!nxt && keep) return tsf;
tsf->refcount--;
if (nxt)
nxt->refcount++;
return nxt;
timeshift_file_t *prev = TAILQ_PREV(tsf, timeshift_file_list, link);
if (!prev && end) *end = 1;
if (!prev && keep) return tsf;
timeshift_file_put(tsf);
return timeshift_file_get(prev);
}

/*
Expand All @@ -393,9 +387,7 @@ timeshift_file_t *timeshift_filemgr_prev
timeshift_file_t *timeshift_filemgr_oldest ( timeshift_t *ts )
{
timeshift_file_t *tsf = TAILQ_FIRST(&ts->files);
if (tsf)
tsf->refcount++;
return tsf;
return timeshift_file_get(tsf);
}

/*
Expand All @@ -404,9 +396,7 @@ timeshift_file_t *timeshift_filemgr_oldest ( timeshift_t *ts )
timeshift_file_t *timeshift_filemgr_newest ( timeshift_t *ts )
{
timeshift_file_t *tsf = TAILQ_LAST(&ts->files, timeshift_file_list);
if (tsf)
tsf->refcount++;
return tsf;
return timeshift_file_get(tsf);
}

/* **************************************************************************
Expand Down

0 comments on commit 80a50ce

Please sign in to comment.