]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
time update - make it work again using TDT/TOT tables, fixes #2776
authorJaroslav Kysela <perex@perex.cz>
Thu, 30 Apr 2015 09:36:46 +0000 (11:36 +0200)
committerJaroslav Kysela <perex@perex.cz>
Thu, 30 Apr 2015 09:37:26 +0000 (11:37 +0200)
configure
src/input/mpegts.h
src/input/mpegts/dvb.h
src/input/mpegts/dvb_psi.c
src/main.c
src/tvhlog.h
src/tvhtime.c
src/tvhtime.h

index cf3619c6ccac1efa07d5363e1078d23c4cd21e7d..40b51e8ff11c21eea948273215f84be0b70f1ae1 100755 (executable)
--- a/configure
+++ b/configure
@@ -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>
index 1ea32a6e44bc5e8c13be661a1326458fdb6e4f2a..9c5e1580815b42d94e980dcfc86099378c62bb3c 100644 (file)
@@ -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;
@@ -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);
 
index 69633a2fb925a501f55470979f053db4890839c5..89fe4c114538bbe8e69240e2daca5d2fb4874901 100644 (file)
@@ -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 */
@@ -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
index ac888ef3e37e85d928fcfcd66c264acc0d27de0a..343ecd3ce9a561b7eb3f276753119c58c54c6982 100644 (file)
@@ -27,6 +27,7 @@
 #include "bouquet.h"
 #include "fastscan.h"
 #include "descrambler/dvbcam.h"
+#include "tvhtime.h"
 
 #include <assert.h>
 #include <stdio.h>
@@ -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
  */
@@ -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;
index 0daab067ba4b449964ecf9fec75e92c1492ae7d8..ed06ff307fcc8dd8885cd9a30657a48ee041feb3 100644 (file)
@@ -71,6 +71,7 @@
 #endif
 #include "profile.h"
 #include "bouquet.h"
+#include "tvhtime.h"
 
 #ifdef PLATFORM_LINUX
 #include <sys/prctl.h>
@@ -966,6 +967,8 @@ main(int argc, char **argv)
   libav_init();
 #endif
 
+  tvhtime_init();
+
   profile_init();
 
   imagecache_init();
index de87e99592df8b35dda3a314dafa6eee39b0157d..c6b1acf7c57666ec4009d66e2eed2ecef93f5128 100644 (file)
@@ -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__ */
index 9bee7609fd3630eaa1b3aaf78772338d663e5e6d..ae4688dfca88ff1b64952b68a16c67a3a3ccfa6e 100644 (file)
@@ -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);
     }
   }
 
@@ -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;
@@ -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 )
@@ -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 )
index 0d07a6fc83b2cf9f563dd7bed639e39e710bbe4e..fec8701693c5bf323a7dc09c18b29793bfd6b632 100644 (file)
@@ -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 );