]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
tsdebug: move most of tsdebug code from mpegts_input.c to mpegts_tsdebug.c
authorJaroslav Kysela <perex@perex.cz>
Tue, 30 May 2017 12:12:57 +0000 (14:12 +0200)
committerJaroslav Kysela <perex@perex.cz>
Tue, 30 May 2017 12:12:57 +0000 (14:12 +0200)
Makefile
src/input/mpegts.h
src/input/mpegts/mpegts_input.c
src/input/mpegts/mpegts_tsdebug.c [new file with mode: 0644]

index 97f45513a360991d525e2e3069e50fe6dd99c90a..56aa69eaebafbe94fe886e9abe3c4ee4a0260437 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -524,6 +524,7 @@ I18N-C += $(SRCS-DVBCAM)
 
 # TSDEBUGCW
 SRCS-TSDEBUG = \
+       src/input/mpegts/mpegts_tsdebug.c \
        src/descrambler/tsdebugcw.c
 SRCS-${CONFIG_TSDEBUG} += $(SRCS-TSDEBUG)
 I18N-C += $(SRCS-TSDEBUG)
index 9f5970c1bcded31e1454cf85c41d2d06de0cb244..f30daae3c8d38609c20c576ee82f7e80d31799ea 100644 (file)
@@ -1006,27 +1006,36 @@ int mpegts_input_close_pid
 void mpegts_input_close_pids
   ( mpegts_input_t *mi, mpegts_mux_t *mm, void *owner, int all );
 
+#if ENABLE_TSDEBUG
+
+void tsdebug_started_mux(mpegts_input_t *mi, mpegts_mux_t *mm);
+void tsdebug_stopped_mux(mpegts_input_t *mi, mpegts_mux_t *mm);
+void tsdebug_check_tspkt(mpegts_mux_t *mm, uint8_t *pkt, int len);
+
 static inline void
 tsdebug_write(mpegts_mux_t *mm, uint8_t *buf, size_t len)
 {
-#if ENABLE_TSDEBUG
   if (mm && mm->mm_tsdebug_fd2 >= 0)
     if (write(mm->mm_tsdebug_fd2, buf, len) != len)
       tvherror(LS_TSDEBUG, "unable to write input data (%i)", errno);
-#endif
 }
 
 static inline ssize_t
 sbuf_tsdebug_read(mpegts_mux_t *mm, sbuf_t *sb, int fd)
 {
-#if ENABLE_TSDEBUG
   ssize_t r = sbuf_read(sb, fd);
   tsdebug_write(mm, sb->sb_data + sb->sb_ptr - r, r);
   return r;
+}
+
 #else
-  return sbuf_read(sb, fd);
+
+static inline void tsdebug_started_mux(mpegts_input_t *mi, mpegts_mux_t *mm) { return; }
+static inline void tsdebug_stopped_mux(mpegts_input_t *mi, mpegts_mux_t *mm) { return; }
+static inline void tsdebug_write(mpegts_mux_t *mm, uint8_t *buf, size_t len) { return; }
+static inline ssize_t sbuf_tsdebug_read(mpegts_mux_t *mm, sbuf_t *sb, int fd) { return sbuf_read(sb, fd); }
+
 #endif
-}
 
 void mpegts_table_dispatch
   (const uint8_t *sec, size_t r, void *mt);
index fff35f4f2d9b1f338eaf2e688c6ef7d53ece9b7d..daefc67861427b0592481c9a6ef95647ead66a21 100644 (file)
 
 #include "input.h"
 #include "tsdemux.h"
-#include "packet.h"
 #include "streaming.h"
-#include "subscriptions.h"
 #include "access.h"
-#include "atomic.h"
 #include "notify.h"
-#include "idnode.h"
 #include "dbus.h"
 #include "memoryinfo.h"
 
-#include <pthread.h>
-#include <assert.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-
 memoryinfo_t mpegts_input_queue_memoryinfo = { .my_name = "MPEG-TS input queue" };
 memoryinfo_t mpegts_input_table_memoryinfo = { .my_name = "MPEG-TS table queue" };
 
@@ -871,33 +862,9 @@ static void
 mpegts_input_started_mux
   ( mpegts_input_t *mi, mpegts_mux_instance_t *mmi )
 {
-#if ENABLE_TSDEBUG
-  extern char *tvheadend_tsdebug;
-  static const char *tmpdir = "/tmp/tvheadend.tsdebug/";
-  char buf[128];
-  char path[PATH_MAX];
-  struct stat st;
-  if (!tvheadend_tsdebug && !stat(tmpdir, &st) && (st.st_mode & S_IFDIR) != 0)
-    tvheadend_tsdebug = (char *)tmpdir;
-  if (tvheadend_tsdebug && !strcmp(tvheadend_tsdebug, tmpdir) && stat(tmpdir, &st))
-    tvheadend_tsdebug = NULL;
-  if (tvheadend_tsdebug) {
-    mpegts_mux_nice_name(mmi->mmi_mux, buf, sizeof(buf));
-    snprintf(path, sizeof(path), "%s/%s-%li-%p-mux.ts", tvheadend_tsdebug,
-             buf, (long)mono2sec(mclk()), mi);
-    mmi->mmi_mux->mm_tsdebug_fd = tvh_open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
-    if (mmi->mmi_mux->mm_tsdebug_fd < 0)
-      tvherror(LS_TSDEBUG, "unable to create file '%s' (%i)", path, errno);
-    snprintf(path, sizeof(path), "%s/%s-%li-%p-input.ts", tvheadend_tsdebug,
-             buf, (long)mono2sec(mclk()), mi);
-    mmi->mmi_mux->mm_tsdebug_fd2 = tvh_open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
-    if (mmi->mmi_mux->mm_tsdebug_fd2 < 0)
-      tvherror(LS_TSDEBUG, "unable to create file '%s' (%i)", path, errno);
-  } else {
-    mmi->mmi_mux->mm_tsdebug_fd = -1;
-    mmi->mmi_mux->mm_tsdebug_fd2 = -1;
-  }
-#endif
+  mpegts_mux_t *mm = mmi->mmi_mux;
+
+  tsdebug_started_mux(mi, mm);
 
   /* Deliver first TS packets as fast as possible */
   mi->mi_last_dispatch = 0;
@@ -907,7 +874,7 @@ mpegts_input_started_mux
     mtimer_arm_rel(&mi->mi_status_timer, mpegts_input_status_timer, mi, sec2mono(1));
 
   /* Update */
-  mmi->mmi_mux->mm_active = mmi;
+  mm->mm_active = mmi;
 
   /* Accept packets */
   LIST_INSERT_HEAD(&mi->mi_mux_active, mmi, mmi_active_link);
@@ -953,20 +920,7 @@ mpegts_input_stopped_mux
   notify_reload("input_status");
   mpegts_input_dbus_notify(mi, 0);
 
-#if ENABLE_TSDEBUG
-  tsdebug_packet_t *tp;
-  if (mm->mm_tsdebug_fd >= 0)
-    close(mm->mm_tsdebug_fd);
-  if (mm->mm_tsdebug_fd2 >= 0)
-    close(mm->mm_tsdebug_fd2);
-  mm->mm_tsdebug_fd = -1;
-  mm->mm_tsdebug_fd2 = -1;
-  mm->mm_tsdebug_pos = 0;
-  while ((tp = TAILQ_FIRST(&mm->mm_tsdebug_packets)) != NULL) {
-    TAILQ_REMOVE(&mm->mm_tsdebug_packets, tp, link);
-    free(tp);
-  }
-#endif
+  tsdebug_stopped_mux(mi, mm);
 }
 
 static int
@@ -1274,41 +1228,6 @@ mpegts_input_table_waiting ( mpegts_input_t *mi, mpegts_mux_t *mm )
   pthread_mutex_unlock(&mm->mm_tables_lock);
 }
 
-#if ENABLE_TSDEBUG
-static void
-tsdebug_check_tspkt( mpegts_mux_t *mm, uint8_t *pkt, int len )
-{
-  void tsdebugcw_new_keys(service_t *t, int type, uint16_t pid, uint8_t *odd, uint8_t *even);
-  uint32_t pos, type, keylen, sid, crc;
-  uint16_t pid;
-  mpegts_service_t *t;
-
-  for ( ; len > 0; pkt += 188, len -= 188) {
-    if (memcmp(pkt + 4, "TVHeadendDescramblerKeys", 24))
-      continue;
-    pos = 4 + 24;
-    type = pkt[pos + 0];
-    keylen = pkt[pos + 1];
-    sid = (pkt[pos + 2] << 8) | pkt[pos + 3];
-    pid = (pkt[pos + 4] << 8) | pkt[pos + 5];
-    pos += 6 + 2 * keylen;
-    if (pos > 184)
-      return;
-    crc = (pkt[pos + 0] << 24) | (pkt[pos + 1] << 16) |
-          (pkt[pos + 2] << 8) | pkt[pos + 3];
-    if (crc != tvh_crc32(pkt, pos, 0x859aa5ba))
-      return;
-    LIST_FOREACH(t, &mm->mm_services, s_dvb_mux_link)
-      if (t->s_dvb_service_id == sid) break;
-    if (!t)
-      return;
-    pos = 4 + 24 + 4;
-    tvhdebug(LS_DESCRAMBLER, "Keys from MPEG-TS source (PID 0x1FFF)!");
-    tsdebugcw_new_keys((service_t *)t, type, pid, pkt + pos, pkt + pos + keylen);
-  }
-}
-#endif
-
 static int
 mpegts_input_process
   ( mpegts_input_t *mi, mpegts_packet_t *mpkt )
diff --git a/src/input/mpegts/mpegts_tsdebug.c b/src/input/mpegts/mpegts_tsdebug.c
new file mode 100644 (file)
index 0000000..24ed6cd
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ *  Tvheadend - MPEGTS debug output
+ *  Copyright (C) 2015,2016,2017 Jaroslav Kysela
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "input.h"
+
+#include <fcntl.h>
+#include <sys/stat.h>
+
+void
+tsdebug_started_mux
+  ( mpegts_input_t *mi, mpegts_mux_t *mm )
+{
+  extern char *tvheadend_tsdebug;
+  static const char *tmpdir = "/tmp/tvheadend.tsdebug/";
+  char buf[128];
+  char path[PATH_MAX];
+  struct stat st;
+  if (!tvheadend_tsdebug && !stat(tmpdir, &st) && (st.st_mode & S_IFDIR) != 0)
+    tvheadend_tsdebug = (char *)tmpdir;
+  if (tvheadend_tsdebug && !strcmp(tvheadend_tsdebug, tmpdir) && stat(tmpdir, &st))
+    tvheadend_tsdebug = NULL;
+  if (tvheadend_tsdebug) {
+    mpegts_mux_nice_name(mm, buf, sizeof(buf));
+    snprintf(path, sizeof(path), "%s/%s-%li-%p-mux.ts", tvheadend_tsdebug,
+             buf, (long)mono2sec(mclk()), mi);
+    mm->mm_tsdebug_fd = tvh_open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
+    if (mm->mm_tsdebug_fd < 0)
+      tvherror(LS_TSDEBUG, "unable to create file '%s' (%i)", path, errno);
+    snprintf(path, sizeof(path), "%s/%s-%li-%p-input.ts", tvheadend_tsdebug,
+             buf, (long)mono2sec(mclk()), mi);
+    mm->mm_tsdebug_fd2 = tvh_open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
+    if (mm->mm_tsdebug_fd2 < 0)
+      tvherror(LS_TSDEBUG, "unable to create file '%s' (%i)", path, errno);
+  } else {
+    mm->mm_tsdebug_fd = -1;
+    mm->mm_tsdebug_fd2 = -1;
+  }
+}
+
+void
+tsdebug_stopped_mux
+  ( mpegts_input_t *mi, mpegts_mux_t *mm )
+{
+  tsdebug_packet_t *tp;
+  if (mm->mm_tsdebug_fd >= 0)
+    close(mm->mm_tsdebug_fd);
+  if (mm->mm_tsdebug_fd2 >= 0)
+    close(mm->mm_tsdebug_fd2);
+  mm->mm_tsdebug_fd = -1;
+  mm->mm_tsdebug_fd2 = -1;
+  mm->mm_tsdebug_pos = 0;
+  while ((tp = TAILQ_FIRST(&mm->mm_tsdebug_packets)) != NULL) {
+    TAILQ_REMOVE(&mm->mm_tsdebug_packets, tp, link);
+    free(tp);
+  }
+}
+
+void
+tsdebug_check_tspkt( mpegts_mux_t *mm, uint8_t *pkt, int len )
+{
+  void tsdebugcw_new_keys(service_t *t, int type, uint16_t pid, uint8_t *odd, uint8_t *even);
+  uint32_t pos, type, keylen, sid, crc;
+  uint16_t pid;
+  mpegts_service_t *t;
+
+  for ( ; len > 0; pkt += 188, len -= 188) {
+    if (memcmp(pkt + 4, "TVHeadendDescramblerKeys", 24))
+      continue;
+    pos = 4 + 24;
+    type = pkt[pos + 0];
+    keylen = pkt[pos + 1];
+    sid = (pkt[pos + 2] << 8) | pkt[pos + 3];
+    pid = (pkt[pos + 4] << 8) | pkt[pos + 5];
+    pos += 6 + 2 * keylen;
+    if (pos > 184)
+      return;
+    crc = (pkt[pos + 0] << 24) | (pkt[pos + 1] << 16) |
+          (pkt[pos + 2] << 8) | pkt[pos + 3];
+    if (crc != tvh_crc32(pkt, pos, 0x859aa5ba))
+      return;
+    LIST_FOREACH(t, &mm->mm_services, s_dvb_mux_link)
+      if (t->s_dvb_service_id == sid) break;
+    if (!t)
+      return;
+    pos = 4 + 24 + 4;
+    tvhdebug(LS_DESCRAMBLER, "Keys from MPEG-TS source (PID 0x1FFF)!");
+    tsdebugcw_new_keys((service_t *)t, type, pid, pkt + pos, pkt + pos + keylen);
+  }
+}