]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
tvhdhomerun: add discovery thread (interval 15 seconds), fixes #3549
authorJaroslav Kysela <perex@perex.cz>
Wed, 3 Feb 2016 16:50:41 +0000 (17:50 +0100)
committerJaroslav Kysela <perex@perex.cz>
Wed, 3 Feb 2016 16:50:41 +0000 (17:50 +0100)
src/input/mpegts/tvhdhomerun/tvhdhomerun.c

index a3d6657613303a8703f4f215c91860fc897420a2..007b6936249cce7abc43a4f705c74bb775dca236 100644 (file)
@@ -32,8 +32,6 @@
            hdhomerun_discover_find_devices_custom_v2
 #endif
 
-static void tvhdhomerun_device_discovery( void );
-
 static void
 tvhdhomerun_device_class_save ( idnode_t *in )
 {
@@ -61,6 +59,10 @@ TAILQ_HEAD(tvhdhomerun_discovery_queue, tvhdhomerun_discovery);
 static int tvhdhomerun_discoveries_count;
 static struct tvhdhomerun_discovery_queue tvhdhomerun_discoveries;
 
+static pthread_t tvhdhomerun_discovery_tid;
+static pthread_mutex_t tvhdhomerun_discovery_lock;
+static pthread_cond_t tvhdhomerun_discovery_cond;
+
 static const char *
 tvhdhomerun_device_class_get_title( idnode_t *in, const char *lang )
 {
@@ -345,43 +347,74 @@ static void tvhdhomerun_device_create(struct hdhomerun_discover_device_t *dInfo)
   htsmsg_destroy(conf);
 }
 
-static void
-tvhdhomerun_device_discovery( void )
+static void *
+tvhdhomerun_device_discovery_thread( void *aux )
 {
   struct hdhomerun_discover_device_t result_list[MAX_HDHOMERUN_DEVICES];
-
-  if (!tvheadend_running)
-    return;
-
-  int numDevices = hdhomerun_discover_find_devices_custom(0,
-                                                          HDHOMERUN_DEVICE_TYPE_TUNER,
-                                                          HDHOMERUN_DEVICE_ID_WILDCARD,
-                                                          result_list,
-                                                          MAX_HDHOMERUN_DEVICES);
-
-  if (numDevices > 0)
-  {
-    while (numDevices > 0 ) {
-      numDevices--;
-      struct hdhomerun_discover_device_t* cDev = &result_list[numDevices];
-      if ( cDev->device_type == HDHOMERUN_DEVICE_TYPE_TUNER ) {
-        if ( !tvhdhomerun_device_find(cDev->device_id) ) {
-          tvhlog(LOG_INFO, "tvhdhomerun","Found HDHomerun device %08x with %d tuners", cDev->device_id, cDev->tuner_count);
-          tvhdhomerun_device_create(cDev);
+  struct timespec ts;
+  struct timeval  tp;
+  int numDevices, brk;
+
+  while (tvheadend_running) {
+
+    numDevices =
+      hdhomerun_discover_find_devices_custom(0,
+                                             HDHOMERUN_DEVICE_TYPE_TUNER,
+                                             HDHOMERUN_DEVICE_ID_WILDCARD,
+                                             result_list,
+                                             MAX_HDHOMERUN_DEVICES);
+
+    if (numDevices > 0) {
+      while (numDevices > 0 ) {
+        numDevices--;
+        struct hdhomerun_discover_device_t* cDev = &result_list[numDevices];
+        if ( cDev->device_type == HDHOMERUN_DEVICE_TYPE_TUNER ) {
+          pthread_mutex_lock(&global_lock);
+          if ( !tvhdhomerun_device_find(cDev->device_id) &&
+               tvheadend_running ) {
+            tvhlog(LOG_INFO, "tvhdhomerun","Found HDHomerun device %08x with %d tuners",
+                   cDev->device_id, cDev->tuner_count);
+            tvhdhomerun_device_create(cDev);
+          }
+          pthread_mutex_unlock(&global_lock);
         }
       }
     }
+
+    pthread_mutex_lock(&tvhdhomerun_discovery_lock);
+    brk = 0;
+    if (tvheadend_running) {
+      gettimeofday(&tp, NULL);
+      ts.tv_sec  = tp.tv_sec + 15;
+      ts.tv_nsec = tp.tv_usec * 1000;
+      brk = pthread_cond_timedwait(&tvhdhomerun_discovery_cond,
+                                   &tvhdhomerun_discovery_lock,
+                                   &ts);
+      brk = !ERRNO_AGAIN(brk) && brk != ETIMEDOUT;
+    }
+    pthread_mutex_unlock(&tvhdhomerun_discovery_lock);
+    if (brk)
+      break;
   }
+
+  return NULL;
 }
 
 void tvhdhomerun_init ( void )
 {
   hdhomerun_debug_obj = hdhomerun_debug_create();
+  const char *s = getenv("TVHEADEND_HDHOMERUN_DEBUG");
 
-  hdhomerun_debug_set_filename(hdhomerun_debug_obj, "/tmp/tvheadend_hdhomerun_errors.log");
-  hdhomerun_debug_enable(hdhomerun_debug_obj);
+  if (s != NULL && *s) {
+    hdhomerun_debug_set_filename(hdhomerun_debug_obj, s);
+    hdhomerun_debug_enable(hdhomerun_debug_obj);
+  }
   TAILQ_INIT(&tvhdhomerun_discoveries);
-  tvhdhomerun_device_discovery();
+  pthread_mutex_init(&tvhdhomerun_discovery_lock, NULL);
+  pthread_cond_init(&tvhdhomerun_discovery_cond, NULL);
+  tvhthread_create(&tvhdhomerun_discovery_tid, NULL,
+                   tvhdhomerun_device_discovery_thread,
+                   NULL, "hdhm-disc");
 }
 
 void tvhdhomerun_done ( void )
@@ -389,6 +422,11 @@ void tvhdhomerun_done ( void )
   tvh_hardware_t *th, *n;
   tvhdhomerun_discovery_t *d, *nd;
 
+  pthread_mutex_lock(&tvhdhomerun_discovery_lock);
+  pthread_cond_signal(&tvhdhomerun_discovery_cond);
+  pthread_mutex_unlock(&tvhdhomerun_discovery_lock);
+  pthread_join(tvhdhomerun_discovery_tid, NULL);
+
   pthread_mutex_lock(&global_lock);
   for (th = LIST_FIRST(&tvh_hardware); th != NULL; th = n) {
     n = LIST_NEXT(th, th_link);