Skip to content

Commit

Permalink
time update - make it work again using TDT/TOT tables, fixes #2776
Browse files Browse the repository at this point in the history
  • Loading branch information
perexg committed Apr 30, 2015
1 parent 86b3650 commit 0294882
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 19 deletions.
11 changes: 11 additions & 0 deletions configure
Expand Up @@ -165,6 +165,17 @@ int test(void)
}
'

check_cc_snippet stime '
#include <time.h>
#define TEST test
int test(void)
{
time_t t = 1;
stime(&t);
return 0;
}
'

check_cc_snippet recvmmsg '
#define _GNU_SOURCE
#include <stdlib.h>
Expand Down
3 changes: 3 additions & 0 deletions src/input/mpegts.h
Expand Up @@ -164,6 +164,7 @@ typedef struct mpegts_pid_sub
#define MPS_WEIGHT_RAW 400
#define MPS_WEIGHT_NIT2 300
#define MPS_WEIGHT_SDT2 300
#define MPS_WEIGHT_TDT 101
#define MPS_WEIGHT_PMT_SCAN 100
int mps_weight;
void *mps_owner;
Expand Down Expand Up @@ -964,6 +965,8 @@ int dvb_sdt_callback
(struct mpegts_table *mt, const uint8_t *ptr, int len, int tableid);
int dvb_tdt_callback
(struct mpegts_table *mt, const uint8_t *ptr, int len, int tableid);
int dvb_tot_callback
(struct mpegts_table *mt, const uint8_t *ptr, int len, int tableid);
int atsc_vct_callback
(struct mpegts_table *mt, const uint8_t *ptr, int len, int tableid);

Expand Down
15 changes: 15 additions & 0 deletions src/input/mpegts/dvb.h
Expand Up @@ -34,10 +34,19 @@ struct mpegts_mux;

#define DVB_PAT_PID 0x00
#define DVB_CAT_PID 0x01
#define DVB_TSDT_PID 0x02
#define DVB_NIT_PID 0x10
#define DVB_SDT_PID 0x11
#define DVB_BAT_PID 0x11
#define DVB_EIT_PID 0x12
#define DVB_RST_PID 0x13
#define DVB_TDT_PID 0x14
#define DVB_SNC_PID 0x15
#define DVB_RNT_PID 0x16
#define DVB_INB_PID 0x1C
#define DVB_MSR_PID 0x1D
#define DVB_DIT_PID 0x1E
#define DVB_SIT_PID 0x1F
#define DVB_VCT_PID 0x1FFB

/* Tables */
Expand All @@ -60,6 +69,12 @@ struct mpegts_mux;
#define DVB_BAT_BASE 0x48
#define DVB_BAT_MASK 0xF8

#define DVB_TDT_BASE 0x70
#define DVB_TDT_MASK 0xFF

#define DVB_TOT_BASE 0x73
#define DVB_TOT_MASK 0xFF

#define DVB_FASTSCAN_NIT_BASE 0xBC
#define DVB_FASTSCAN_SDT_BASE 0xBD
#define DVB_FASTSCAN_MASK 0xFF
Expand Down
53 changes: 53 additions & 0 deletions src/input/mpegts/dvb_psi.c
Expand Up @@ -27,6 +27,7 @@
#include "bouquet.h"
#include "fastscan.h"
#include "descrambler/dvbcam.h"
#include "tvhtime.h"

#include <assert.h>
#include <stdio.h>
Expand Down Expand Up @@ -2370,6 +2371,50 @@ psi_parse_pmt
return ret;
}

/**
* TDT parser, from ISO 13818-1 and ETSI EN 300 468
*/

static void dvb_time_update(const uint8_t *ptr, const char *srcname)
{
static time_t dvb_last_update = 0;
time_t t;
if (dvb_last_update + 1800 < dispatch_clock) {
t = dvb_convert_date(ptr, 0);
tvhtime_update(t, srcname);
dvb_last_update = dispatch_clock;
}
}

int
dvb_tdt_callback
(mpegts_table_t *mt, const uint8_t *ptr, int len, int tableid)
{
if (len == 5) {
dvb_time_update(ptr, "TDT");
mt->mt_incomplete = 0;
mt->mt_complete = 1;
mt->mt_finished = 1;
}
return 0;
}

/**
* TOT parser, from ISO 13818-1 and ETSI EN 300 468
*/
int
dvb_tot_callback
(mpegts_table_t *mt, const uint8_t *ptr, int len, int tableid)
{
if (len >= 5) {
dvb_time_update(ptr, "TOT");
mt->mt_incomplete = 0;
mt->mt_complete = 1;
mt->mt_finished = 1;
}
return 0;
}

/*
* Install default table sets
*/
Expand Down Expand Up @@ -2410,6 +2455,14 @@ psi_tables_dvb ( mpegts_mux_t *mm )
DVB_SDT_PID, MPS_WEIGHT_SDT);
mpegts_table_add(mm, DVB_BAT_BASE, DVB_BAT_MASK, dvb_bat_callback,
NULL, "bat", MT_CRC, DVB_BAT_PID, MPS_WEIGHT_BAT);
if (tvhtime_update_enabled) {
mpegts_table_add(mm, DVB_TDT_BASE, DVB_TDT_MASK, dvb_tdt_callback,
NULL, "tdt", MT_ONESHOT | MT_QUICKREQ | MT_RECORD,
DVB_TDT_PID, MPS_WEIGHT_TDT);
mpegts_table_add(mm, DVB_TOT_BASE, DVB_TOT_MASK, dvb_tot_callback,
NULL, "tot", MT_ONESHOT | MT_QUICKREQ | MT_CRC | MT_RECORD,
DVB_TDT_PID, MPS_WEIGHT_TDT);
}
#if ENABLE_MPEGTS_DVB
if (idnode_is_instance(&mm->mm_id, &dvb_mux_dvbs_class)) {
dvb_mux_conf_t *mc = &((dvb_mux_t *)mm)->lm_tuning;
Expand Down
3 changes: 3 additions & 0 deletions src/main.c
Expand Up @@ -71,6 +71,7 @@
#endif
#include "profile.h"
#include "bouquet.h"
#include "tvhtime.h"

#ifdef PLATFORM_LINUX
#include <sys/prctl.h>
Expand Down Expand Up @@ -966,6 +967,8 @@ main(int argc, char **argv)
libav_init();
#endif

tvhtime_init();

profile_init();

imagecache_init();
Expand Down
9 changes: 5 additions & 4 deletions src/tvhlog.h
Expand Up @@ -111,9 +111,10 @@ static inline int tvhlog_limit ( tvhlog_limit_t *limit, uint32_t delay )
tvhtrace(subsys, "%s() leave", #fcn); \
} while (0)

#define tvhdebug(...) tvhlog(LOG_DEBUG, ##__VA_ARGS__)
#define tvhinfo(...) tvhlog(LOG_INFO, ##__VA_ARGS__)
#define tvhwarn(...) tvhlog(LOG_WARNING, ##__VA_ARGS__)
#define tvherror(...) tvhlog(LOG_ERR, ##__VA_ARGS__)
#define tvhdebug(...) tvhlog(LOG_DEBUG, ##__VA_ARGS__)
#define tvhinfo(...) tvhlog(LOG_INFO, ##__VA_ARGS__)
#define tvhwarn(...) tvhlog(LOG_WARNING, ##__VA_ARGS__)
#define tvhnotice(...) tvhlog(LOG_NOTICE, ##__VA_ARGS__)
#define tvherror(...) tvhlog(LOG_ERR, ##__VA_ARGS__)

#endif /* __TVH_LOGGING_H__ */
36 changes: 22 additions & 14 deletions src/tvhtime.c
Expand Up @@ -82,34 +82,40 @@ ntp_shm_init ( void )
* Update time
*/
void
tvhtime_update ( struct tm *tm )
tvhtime_update ( time_t utc, const char *srcname )
{
#if !ENABLE_ANDROID
time_t now;
struct tm tm;
struct timeval tv;
ntp_shm_t *ntp_shm;
int64_t t1, t2;
int64_t t1, t2, diff;

tvhtrace("time", "current time is %04d/%02d/%02d %02d:%02d:%02d",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
localtime_r(&utc, &tm);

tvhtrace("time", "%s - current time is %04d/%02d/%02d %02d:%02d:%02d",
srcname,
tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);

/* Current and reported time */
now = mktime(tm);
gettimeofday(&tv, NULL);

/* Delta */
t1 = now * 1000000;
t1 = utc * 1000000;
t2 = tv.tv_sec * 1000000 + tv.tv_usec;

/* Update local clock */
if (tvhtime_update_enabled) {
if (llabs(t2 - t1) > tvhtime_tolerance) {
tvhlog(LOG_DEBUG, "time", "updated system clock");
#ifdef stime
stime(&now);
if ((diff = llabs(t2 - t1)) > tvhtime_tolerance) {
#if ENABLE_STIME
if (stime(&utc) < 0)
tvherror("time", "%s - unable to update system time: %s", srcname, strerror(errno));
else
tvhdebug("time", "%s - updated system clock", srcname);
#else
tvhlog(LOG_NOTICE, "time", "stime(2) not implemented");
tvhnotice("time", "%s - stime(2) not implemented", srcname);
#endif
} else {
tvhwarn("time", "%s - time not updated (diff %"PRId64")", srcname, diff);
}
}

Expand All @@ -121,7 +127,7 @@ tvhtime_update ( struct tm *tm )
tvhtrace("time", "ntp delta = %"PRId64" us\n", t2 - t1);
ntp_shm->valid = 0;
ntp_shm->count++;
ntp_shm->clockTimeStampSec = now;
ntp_shm->clockTimeStampSec = utc;
ntp_shm->clockTimeStampUSec = 0;
ntp_shm->receiveTimeStampSec = tv.tv_sec;
ntp_shm->receiveTimeStampUSec = (int)tv.tv_usec;
Expand All @@ -141,6 +147,7 @@ void tvhtime_init ( void )
tvhtime_ntp_enabled = 0;
if (htsmsg_get_u32(m, "tolerance", &tvhtime_tolerance))
tvhtime_tolerance = 5000;
htsmsg_destroy(m);
}

static void tvhtime_save ( void )
Expand All @@ -150,6 +157,7 @@ static void tvhtime_save ( void )
htsmsg_add_u32(m, "ntp_enabled", tvhtime_ntp_enabled);
htsmsg_add_u32(m, "tolerance", tvhtime_tolerance);
hts_settings_save(m, "tvhtime/config");
htsmsg_destroy(m);
}

void tvhtime_set_update_enabled ( uint32_t on )
Expand Down
2 changes: 1 addition & 1 deletion src/tvhtime.h
Expand Up @@ -25,7 +25,7 @@ extern uint32_t tvhtime_ntp_enabled;
extern uint32_t tvhtime_tolerance;

void tvhtime_init ( void );
void tvhtime_update ( struct tm *now );
void tvhtime_update ( time_t utc, const char *srcname );

void tvhtime_set_update_enabled ( uint32_t on );
void tvhtime_set_ntp_enabled ( uint32_t on );
Expand Down

0 comments on commit 0294882

Please sign in to comment.