From c08ed3ce44bcdc967b42e4c4e591888294d7774b Mon Sep 17 00:00:00 2001
From: Klaus Schmidinger <vdr@tvdr.de>
Date: Tue, 31 Oct 2017 12:20:56 +0100
Subject: The MTD mapper now avoids immediately reusing unique PIDs when
 switching channels

---
 CONTRIBUTORS |  3 +++
 HISTORY      |  2 ++
 mtd.c        | 11 +++++++++--
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/CONTRIBUTORS b/CONTRIBUTORS
index 3c02c1c1..34d71abc 100644
--- a/CONTRIBUTORS
+++ b/CONTRIBUTORS
@@ -3514,3 +3514,6 @@ Frank Richter <kulpstur@t-online.de>
 Daniel Scheller <d.scheller@gmx.net>
  for reporting a problem with detecting whether a CAM replies to queries, which didn't
  work on some systems since the implementation of RI_HOST_CONTROL
+
+Onur Sent�rk <onur@sentek.org>
+ for making the MTD mapper avoid immediately reusing unique PIDs when switching channels
diff --git a/HISTORY b/HISTORY
index 472526a8..4dff21c2 100644
--- a/HISTORY
+++ b/HISTORY
@@ -9174,3 +9174,5 @@ Video Disk Recorder Revision History
   of low disk space.
 - Now making sure that AssertFreeDiskSpace() is called with the maximum timer
   priority in case there are several timers recording with different priorities.
+- The MTD mapper now avoids immediately reusing unique PIDs when switching channels,
+  to prevent possible problems with old data in buffers (thanks to Onur Sent�rk).
diff --git a/mtd.c b/mtd.c
index 345288ec..04bf09df 100644
--- a/mtd.c
+++ b/mtd.c
@@ -4,7 +4,7 @@
  * See the main source file 'vdr.c' for copyright information and
  * how to reach the author.
  *
- * $Id: mtd.c 1.11 2017/05/01 09:19:52 kls Exp $
+ * $Id: mtd.c 1.12 2017/10/31 12:16:39 kls Exp $
  */
 
 #include "mtd.h"
@@ -148,6 +148,7 @@ class cMtdMapper {
 private:
   int number;
   int masterCamSlotNumber;
+  int nextUniqPid;
   uint16_t uniqPids[MAX_REAL_PIDS]; // maps a real PID to a unique PID
   uint16_t realPids[MAX_UNIQ_PIDS]; // maps a unique PID to a real PID
   cVector<uint16_t> uniqSids;
@@ -165,6 +166,7 @@ cMtdMapper::cMtdMapper(int Number, int MasterCamSlotNumber)
 {
   number = Number;
   masterCamSlotNumber = MasterCamSlotNumber;
+  nextUniqPid = 0;
   Clear();
 }
 
@@ -179,11 +181,15 @@ uint16_t cMtdMapper::MakeUniqPid(uint16_t RealPid)
   DBGMTD("CAM %d/%d: mapped PID %d (%04X) to %d (%04X)", masterCamSlotNumber, number, RealPid, RealPid, uniqPids[RealPid], uniqPids[RealPid]);
   return uniqPids[RealPid];
 #else
-  for (int i = 0; i < MAX_UNIQ_PIDS; i++) {
+  for (int p = 0; p < MAX_UNIQ_PIDS; p++) {
+      int i = nextUniqPid + p;
+      if (i >= MAX_UNIQ_PIDS)
+         i -= MAX_UNIQ_PIDS;
       if (realPids[i] == MTD_INVALID_PID) { // 0x0000 is a valid PID (PAT)!
          realPids[i] = RealPid;
          uniqPids[RealPid] = (number << UNIQ_PID_SHIFT) | i;
          DBGMTD("CAM %d/%d: mapped PID %d (%04X) to %d (%04X)", masterCamSlotNumber, number, RealPid, RealPid, uniqPids[RealPid], uniqPids[RealPid]);
+         nextUniqPid = i + 1;
          return uniqPids[RealPid];
          }
       }
@@ -212,6 +218,7 @@ void cMtdMapper::Clear(void)
   DBGMTD("CAM %d/%d: MTD mapper cleared", masterCamSlotNumber, number);
   memset(uniqPids, 0, sizeof(uniqPids));
   memset(realPids, MTD_INVALID_PID, sizeof(realPids));
+  // do not reset nextUniqPid here!
   uniqSids.Clear();
 }
 
-- 
cgit v1.2.3