]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
mpegts mux: the mm_pids flush must be protected by mi_output_lock
authorJaroslav Kysela <perex@perex.cz>
Tue, 21 Oct 2014 14:07:59 +0000 (16:07 +0200)
committerJaroslav Kysela <perex@perex.cz>
Tue, 21 Oct 2014 14:07:59 +0000 (16:07 +0200)
src/input/mpegts/mpegts_mux.c

index 3712394741e9fcaf943d0b163a5293a683c6e138..c8411005a4b4d9534bf1c64a65951b39ae4b1320 100644 (file)
@@ -713,19 +713,26 @@ mpegts_mux_stop ( mpegts_mux_t *mm, int force )
     mpegts_input_flush_mux(mi, mm);
 
   /* Ensure PIDs are cleared */
-  mm->mm_last_pid = -1;
-  mm->mm_last_mp = NULL;
-  while ((mp = RB_FIRST(&mm->mm_pids))) {
-    while ((mps = RB_FIRST(&mp->mp_subs))) {
-      RB_REMOVE(&mp->mp_subs, mps, mps_link);
-      free(mps);
-    }
-    RB_REMOVE(&mm->mm_pids, mp, mp_link);
-    if (mp->mp_fd != -1) {
-      tvhdebug("mpegts", "%s - close PID %04X (%d)", buf, mp->mp_pid, mp->mp_pid);
-      close(mp->mp_fd);
+  if (mi) {
+    pthread_mutex_lock(&mi->mi_output_lock);
+    mm->mm_last_pid = -1;
+    mm->mm_last_mp = NULL;
+    while ((mp = RB_FIRST(&mm->mm_pids))) {
+      assert(mi);
+      while ((mps = RB_FIRST(&mp->mp_subs))) {
+        RB_REMOVE(&mp->mp_subs, mps, mps_link);
+        free(mps);
+      }
+      RB_REMOVE(&mm->mm_pids, mp, mp_link);
+      if (mp->mp_fd != -1) {
+        tvhdebug("mpegts", "%s - close PID %04X (%d)", buf, mp->mp_pid, mp->mp_pid);
+        close(mp->mp_fd);
+      }
+      free(mp);
     }
-    free(mp);
+    pthread_mutex_unlock(&mi->mi_output_lock);
+  } else {
+    assert(RB_FIRST(&mm->mm_pids) == NULL);
   }
 
   /* Scanning */