From 3ac0218fb24abdf90ca53f2d38026ce504d3e751 Mon Sep 17 00:00:00 2001 From: Petr N Date: Sat, 28 Dec 2019 18:30:39 +0100 Subject: [PATCH] First try to sync the stream --- dvb.c | 61 ++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/dvb.c b/dvb.c index f144c90..fa163f7 100644 --- a/dvb.c +++ b/dvb.c @@ -159,39 +159,66 @@ void dvb_Reset( void ) *****************************************************************************/ static void DVRRead(struct ev_loop *loop, struct ev_io *w, int revents) { - int i, i_len; + int i, j, i_len, ok; block_t *p_ts = p_freelist, **pp_current = &p_ts; - struct iovec p_iov[MAX_READ_ONCE]; + static unsigned char buffer[(MAX_READ_ONCE+2)*TS_SIZE]; + static int i_remaining = 0; + static int offset = 0; + int new_offset = 0; - for ( i = 0; i < MAX_READ_ONCE; i++ ) - { - if ( (*pp_current) == NULL ) *pp_current = block_New(); - p_iov[i].iov_base = (*pp_current)->p_ts; - p_iov[i].iov_len = TS_SIZE; - pp_current = &(*pp_current)->p_next; - } - - if ( (i_len = readv(i_dvr, p_iov, MAX_READ_ONCE)) < 0 ) + if ( (i_len = read(i_dvr, buffer+i_remaining, MAX_READ_ONCE*TS_SIZE)) < 0 ) { msg_Err( NULL, "couldn't read from DVR device (%s)", strerror(errno) ); i_len = 0; } - i_len /= TS_SIZE; + i_len += i_remaining; + if (i_len < TS_SIZE) { + i_remaining = i_len; + return; + } - if ( i_len ) - ev_timer_again(loop, &mute_watcher); + ev_timer_again(loop, &mute_watcher); - pp_current = &p_ts; - while ( i_len && *pp_current ) + for (i = 0; i < TS_SIZE; i++) { + ok = 1; + for (j = i; j < i_len; j += TS_SIZE) { + if (buffer[j] != 0x47) { + ok = 0; + break; + } + } + if (ok) { + new_offset = i; + break; + } + } + + if (!ok) { + msg_Err( NULL, "cannot sync data from DVR device"); + i_remaining = 0; /* erase bufferred data */ + return; + } + + if (offset != new_offset) { + msg_Err( NULL, "New offset is %d", new_offset); + offset = new_offset; + } + + + for ( i = offset; i < i_len; i += TS_SIZE ) { + if ( (*pp_current) == NULL ) *pp_current = block_New(); + memcpy((*pp_current)->p_ts, buffer+i, TS_SIZE); pp_current = &(*pp_current)->p_next; - i_len--; } p_freelist = *pp_current; *pp_current = NULL; + i_remaining = i_len - i; + memmove(buffer, buffer+i, i_remaining); + demux_Run( p_ts ); } -- 2.11.4.GIT