From b6b692a4b52a6897ec4c1aa311502a8b6ecf5e0a Mon Sep 17 00:00:00 2001
From: leslie <unknown>
Date: Sun, 17 Feb 2008 14:13:23 +0100
Subject: [PATCH] fixed deCSA when first recording FTA then view encrypted

---
 cam.c | 32 +++++++++++++++++++++++++-------
 cam.h |  2 ++
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/cam.c b/cam.c
index d0c426f..1a20ea0 100644
--- a/cam.c
+++ b/cam.c
@@ -2419,17 +2419,19 @@ public:
   cDeCsaTSBuffer(int File, int Size, int CardIndex, cDeCSA *DeCsa, bool ScActive);
   ~cDeCsaTSBuffer();
   uchar *Get(void);
+  void SetActive(bool ScActive);
   };
 
 cDeCsaTSBuffer::cDeCsaTSBuffer(int File, int Size, int CardIndex, cDeCSA *DeCsa, bool ScActive)
 {
   SetDescription("TS buffer on device %d", CardIndex);
-  f=File; cardIndex=CardIndex; decsa=DeCsa; scActive=ScActive;
+  f=File; cardIndex=CardIndex; decsa=DeCsa;
   delivered=false;
   lastP=0; lastCount=0;
   ringBuffer=new cRingBufferLinear(Size,TS_SIZE,true,"FFdecsa-TS");
   ringBuffer->SetTimeouts(100,100);
   if(decsa) decsa->SetActive(true);
+  SetActive(ScActive);
   Start();
 }
 
@@ -2440,6 +2442,11 @@ cDeCsaTSBuffer::~cDeCsaTSBuffer()
   delete ringBuffer;
 }
 
+void cDeCsaTSBuffer::SetActive(bool ScActive)
+{
+  scActive=ScActive;
+}
+
 void cDeCsaTSBuffer::Action(void)
 {
   if(ringBuffer) {
@@ -2677,6 +2684,9 @@ bool cScDvbDevice::Ready(void)
 bool cScDvbDevice::SetPid(cPidHandle *Handle, int Type, bool On)
 {
   if(cam) cam->SetPid(Type,Handle->pid,On);
+  tsMutex.Lock();
+  if(tsBuffer) tsBuffer->SetActive(ScActive());
+  tsMutex.Unlock();
   return cDvbDevice::SetPid(Handle,Type,On);
 }
 
@@ -2765,24 +2775,32 @@ void cScDvbDevice::CiStartDecrypting(void)
 }
 #endif //APIVERSNUM < 10500
 
+bool cScDvbDevice::ScActive(void)
+{
+#if APIVERSNUM >= 10500
+  return dynamic_cast<cScCamSlot *>(CamSlot())!=0;
+#else
+  return cam && softcsa;
+#endif
+}
+
 bool cScDvbDevice::OpenDvr(void)
 {
   CloseDvr();
   fd_dvr=DvbOpen(DEV_DVB_DVR,CardIndex(),O_RDONLY|O_NONBLOCK,true);
   if(fd_dvr>=0) {
-#if APIVERSNUM >= 10500
-    bool active=dynamic_cast<cScCamSlot *>(CamSlot())!=0;
-#else
-    bool active=cam && softcsa;
-#endif
-    tsBuffer=new cDeCsaTSBuffer(fd_dvr,MEGABYTE(2),CardIndex()+1,decsa,active);
+    tsMutex.Lock();
+    tsBuffer=new cDeCsaTSBuffer(fd_dvr,MEGABYTE(2),CardIndex()+1,decsa,ScActive());
+    tsMutex.Unlock();
     }
   return fd_dvr>=0;
 }
 
 void cScDvbDevice::CloseDvr(void)
 {
+  tsMutex.Lock();
   delete tsBuffer; tsBuffer=0;
+  tsMutex.Unlock();
   if(fd_dvr>=0) { close(fd_dvr); fd_dvr=-1; }
 }
 
diff --git a/cam.h b/cam.h
index 852b961..94031d6 100644
--- a/cam.h
+++ b/cam.h
@@ -151,6 +151,7 @@ class cScDvbDevice : public cDvbDevice {
 private:
   cDeCSA *decsa;
   cDeCsaTSBuffer *tsBuffer;
+  cMutex tsMutex;
 #if APIVERSNUM >= 10500
   cScCiAdapter *ciadapter;
   cCiAdapter *hwciadapter;
@@ -171,6 +172,7 @@ private:
   void LateInit(void);
   void EarlyShutdown(void);
   int FindLRUPrg(int source, int transponder, int prg);
+  bool ScActive(void);
 #endif //SASC
 protected:
 #ifndef SASC
-- 
2.39.5