]> git.vomp.tv Git - vompclient.git/commitdiff
Switch PlayerRadioLive to std::thread
authorChris Tallon <chris@vomp.tv>
Fri, 20 Mar 2020 15:13:19 +0000 (15:13 +0000)
committerChris Tallon <chris@vomp.tv>
Fri, 20 Mar 2020 15:13:19 +0000 (15:13 +0000)
playerradiolive.cc
playerradiolive.h
playervideolive.cc
playervideolive.h

index 8add617bce2c881449a75ad3abf93552e6013ea9..086e069749001f25cfe8c3f8fb826c643f693afd 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright 2008 Chris Tallon
+    Copyright 2008-2020 Chris Tallon
 
     This file is part of VOMP.
 
     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
 */
 
-#include "playerradiolive.h"
+#include <stdlib.h>
+#ifndef WIN32
+#include <sys/time.h>
+#endif
+#include <time.h>
 
 #include "defines.h"
 #include "log.h"
 #include "channel.h"
 #include "video.h"
 
+#include "playerradiolive.h"
+
 // ----------------------------------- Called from outside, one offs or info funcs
 
 PlayerRadioLive::PlayerRadioLive(MessageQueue* tmessageQueue, void* tmessageReceiver, ChannelList* tchanList)
-: afeed(this)
+: messageQueue(tmessageQueue), messageReceiver(tmessageReceiver), afeed(this), chanList(tchanList)
 {
-  messageQueue = tmessageQueue;
-  messageReceiver = tmessageReceiver;
-  chanList = tchanList;
-  
   audio = Audio::getInstance();
   logger = Log::getInstance();
   vdr = VDR::getInstance();
-  initted = false;
 
-  stopNow = false;
-  state = S_STOP;
   Video::getInstance()->turnVideoOff();
 }
 
@@ -77,7 +76,11 @@ int PlayerRadioLive::init()
 int PlayerRadioLive::shutdown()
 {
   if (!initted) return 0;
-  stop();
+  if (state != S_STOP) // FIXME check when this is called and how. This is not thread-sync bullet proof as-is.
+  {
+    logger->log("PlayerRadioLive", Log::DEBUG, "state is not stop, calling");
+    stop();
+  }
   initted = false;
   delete demuxer;
   return 1;
@@ -122,11 +125,20 @@ void PlayerRadioLive::setSubtitleChannel(int newChannel)
 
 void PlayerRadioLive::go(ULONG index)
 {
+  playerThreadMutex.lock();
+
   struct PLInstruction i;
   i.instruction = I_SETCHANNEL;
   i.channelIndex = index;
   instructions.push(i);
-  threadStart();
+
+  playerThread = std::thread([this]
+  {
+    playerThreadMutex.lock();
+    playerThreadMutex.unlock();
+    threadMethod();
+  });
+  playerThreadMutex.unlock();
 }
 
 void PlayerRadioLive::setChannel(ULONG index)
@@ -137,17 +149,20 @@ void PlayerRadioLive::setChannel(ULONG index)
   i.channelIndex = index;
   instructions.push(i);  
   logger->log("PlayerRadioLive", Log::DEBUG, "posted setChannel instruction, now %i in queue", instructions.size());
-  threadSignalNoLock();
+  playerThreadCond.notify_one();
 }
 
 void PlayerRadioLive::stop()
 {
   logger->log("PlayerRadioLive", Log::DEBUG, "stop");
+  playerThreadMutex.lock();
   struct PLInstruction i;
   i.instruction = I_STOP;
   instructions.push(i);
-  threadSignal();
-  threadStop();
+  playerThreadCond.notify_one();
+  playerThreadMutex.unlock();
+  playerThread.join();
+  logger->log("PlayerRadioLive", Log::DEBUG, "stop successful");
 }
 
 // ----------------------------------- Callback
@@ -183,7 +198,7 @@ void PlayerRadioLive::streamReceive(ULONG flag, void* data, ULONG len)
     s.data = data;
     s.len = len;
     streamChunks.push(s);
-    threadSignalNoLock();
+    playerThreadCond.notify_one();
   }
   else
   {
@@ -377,6 +392,8 @@ void PlayerRadioLive::optimizeInstructionQueue()
 
 void PlayerRadioLive::threadMethod()
 {
+  std::unique_lock<std::mutex> ul(playerThreadMutex, std::defer_lock);
+
   while(1)
   {
     while(!instructions.empty())
@@ -459,14 +476,10 @@ void PlayerRadioLive::threadMethod()
         logger->log("PlayerRadioLive", Log::DEBUG, "Stopping");
         switchState(S_STOP);
         checkError();
-
-        stopNow = true;
-        break;
+        return;
       }
     }
 
-    if (stopNow) break;
-
     while(streamChunks.size())
     {
       chunkToDemuxer();
@@ -494,9 +507,10 @@ void PlayerRadioLive::threadMethod()
       }
     }
     
-    threadLock();
-    threadWaitForSignal(); // unlocks and waits for signal
-    threadUnlock();
+    ul.lock();
+    if (!instructions.empty()) { ul.unlock(); continue; }
+    playerThreadCond.wait(ul);
+    ul.unlock();
   }
 
   logger->log("PlayerRadioLive", Log::DEBUG, "End of thread");
index 6ee6e02134f68e0b514a15318f700f4688187547..6bf4feaced7ee70b03dbf9cde4474afef4e0d5f0 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright 2008 Chris Tallon
+    Copyright 2008-2020 Chris Tallon
 
     This file is part of VOMP.
 
 #ifndef PLAYERRADIOLIVE_H
 #define PLAYERRADIOLIVE_H
 
-#include <stdio.h>
-#include <stdlib.h>
-#ifndef WIN32
-#include <sys/time.h>
-#endif
-#include <time.h>
+#include <mutex>
+#include <thread>
+#include <condition_variable>
 
 #include <queue>
 
 #include "playerlive.h"
-
-#ifdef WIN32
-#include "threadwin.h"
-#else
-#include "threadp.h"
-#endif
-
 #include "callback.h"
 #include "defines.h"
 #include "afeed.h"
@@ -47,7 +37,7 @@ class Audio;
 class Log;
 class DemuxerTS;
 
-class PlayerRadioLive : public PlayerLive, public Thread_TYPE, public Callback, public StreamReceiver
+class PlayerRadioLive : public PlayerLive, public Callback, public StreamReceiver
 {
   public:
     PlayerRadioLive(MessageQueue* messageQueue, void* messageReceiver, ChannelList* chanList);
@@ -65,7 +55,7 @@ class PlayerRadioLive : public PlayerLive, public Thread_TYPE, public Callback,
     virtual bool* getDemuxerMpegAudioChannels();
     virtual bool* getDemuxerAc3AudioChannels();
     virtual int getCurrentAudioChannel();
-    virtual int *getTeletxtSubtitlePages();
+    virtual intgetTeletxtSubtitlePages();
     virtual int getCurrentSubtitleChannel();
 
     void call(void*); // for callback interface
@@ -82,9 +72,6 @@ class PlayerRadioLive : public PlayerLive, public Thread_TYPE, public Callback,
     const static UCHAR ASPECT43 = 4;
     const static UCHAR ASPECT169 = 5;
     const static UCHAR PREBUFFERING = 6;
-    
-  protected:
-    void threadMethod();
 
   private:
     MessageQueue* messageQueue;
@@ -99,26 +86,29 @@ class PlayerRadioLive : public PlayerLive, public Thread_TYPE, public Callback,
     std::queue<PLInstruction> instructions;
     const static UCHAR I_SETCHANNEL = 1;
     const static UCHAR I_STOP = 2;
-    
+
     std::queue<StreamChunk> streamChunks;
-    
-    bool initted;
 
-    UCHAR state;
+    bool initted{};
+
+    UCHAR state{S_STOP};
     const static UCHAR S_STOP = 1;
     const static UCHAR S_PREBUFFERING = 2;
     const static UCHAR S_PLAY = 3;
     void switchState(UCHAR newState);
     bool checkError();
 
-    bool stopNow;
     int preBufferCount;
     const static int preBufferAmount = 3;
 
+    std::thread playerThread;
+    std::mutex playerThreadMutex;
+    std::condition_variable playerThreadCond;
+    void threadMethod();
+
     void clearStreamChunks();
     void chunkToDemuxer();
     void optimizeInstructionQueue();
 };
 
 #endif
-
index ea17eaca0bff1624efac24c390962a4cf933e816..06d0dfc181225f98f2c707ef1f35f23a30d905cc 100644 (file)
     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
 */
 
+#include <stdlib.h>
+#ifndef WIN32
+#include <sys/time.h>
+#endif
+#include <time.h>
+
 #include "defines.h"
 #include "log.h"
 #include "audio.h"
index b61943388ff734988fd07ab8eddf1ef1189373a0..4581098469f08790b100b678d868c1e1c7b75130 100644 (file)
 #ifndef PLAYERVIDEOLIVE_H
 #define PLAYERVIDEOLIVE_H
 
-#include <stdio.h>
-#include <stdlib.h>
-#ifndef WIN32
-#include <sys/time.h>
-#endif
-#include <time.h>
-
 #include <mutex>
 #include <thread>
 #include <condition_variable>
@@ -94,9 +87,6 @@ class PlayerVideoLive : public PlayerLive, public Callback, public StreamReceive
     const static UCHAR ASPECT169 = 5;
     const static UCHAR PREBUFFERING = 6;
 
-  protected:
-    void threadMethod();
-
   private:
     VFeed vfeed;
     AFeed afeed;
@@ -119,10 +109,10 @@ class PlayerVideoLive : public PlayerLive, public Callback, public StreamReceive
 
     std::queue<StreamChunk> streamChunks;
 
-
     std::thread playerThread;
     std::mutex playerThreadMutex;
     std::condition_variable playerThreadCond;
+    void threadMethod();
 
     const static UCHAR S_STOP = 1;
     const static UCHAR S_VIDEOSTARTUP = 2;