]> git.vomp.tv Git - vompclient.git/commitdiff
Convert PlayerVideoLive to std::thread
authorChris Tallon <chris@vomp.tv>
Sat, 14 Mar 2020 18:20:10 +0000 (18:20 +0000)
committerChris Tallon <chris@vomp.tv>
Sat, 14 Mar 2020 18:20:10 +0000 (18:20 +0000)
.astylerc
playervideolive.cc
playervideolive.h

index f1a7f7c4f8c1d42f1d5450a58d251d0be51dd5ef..1b85e893c262b30c777e91331d08273bb036a247 100644 (file)
--- a/.astylerc
+++ b/.astylerc
@@ -15,3 +15,4 @@ keep-one-line-blocks
 attach-return-type
 attach-return-type-decl
 lineend=linux
+indent-preproc-block
index bd68b07fe1e1f18cfe217a3dfebfc8eee21f122f..d41f99234923b08835db2ceb6aaf6132340b9d6d 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright 2007 Chris Tallon
+    Copyright 2007-2020 Chris Tallon
 
     This file is part of VOMP.
 
@@ -53,7 +53,6 @@ PlayerVideoLive::PlayerVideoLive(MessageQueue* tmessageQueue, void* tmessageRece
   videoStartup = false;
   pendingAudioPlay = false;
 
-  stopNow = false;
   state = S_STOP;
 
   video->turnVideoOn();
@@ -110,7 +109,12 @@ int PlayerVideoLive::shutdown()
 {
   if (!initted) return 0;
   logger->log("PlayerVideoLive", Log::DEBUG, "Shutdown");
-  stop();
+  if (state != S_STOP)
+  {
+    logger->log("PlayerVideoLive", Log::DEBUG, "state is not stop, calling");
+    stop();
+  }
+
   initted = false;
 
   delete demuxer;
@@ -193,11 +197,20 @@ void PlayerVideoLive::tellSubtitlesOSDVisible(bool visible)
 
 void PlayerVideoLive::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 PlayerVideoLive::setChannel(ULONG index)
@@ -207,17 +220,23 @@ void PlayerVideoLive::setChannel(ULONG index)
   i.instruction = I_SETCHANNEL;
   i.channelIndex = index;
   instructions.push(i);  
-  threadSignalNoLock();
+  playerThreadCond.notify_one();
 }
 
 void PlayerVideoLive::stop()
 {
   logger->log("PlayerVideoLive", 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("PlayerVideoLive", Log::DEBUG, "stop succesfull");
 }
 
@@ -281,7 +300,7 @@ void PlayerVideoLive::call(void* caller)
     {
       logger->log("PlayerVideoLive", Log::DEBUG, "afeed video startup");
       videoStartup = true;
-      threadSignalNoLock();
+      playerThreadCond.notify_one();
     }
   }
 }
@@ -315,7 +334,7 @@ void PlayerVideoLive::streamReceive(ULONG flag, void* data, ULONG len)
     s.data = data;
     s.len = len;
     streamChunks.push(s);
-    threadSignalNoLock();
+    playerThreadCond.notify_one();
   }
   else
   {
@@ -404,7 +423,7 @@ void PlayerVideoLive::switchState(UCHAR newState)
           if (!video->independentAVStartUp())
           {
             videoStartup = true;
-            threadSignalNoLock();
+            playerThreadCond.notify_one();
           }
           return;
         }
@@ -462,7 +481,7 @@ void PlayerVideoLive::switchState(UCHAR newState)
           if (!video->independentAVStartUp())
           {
             videoStartup = true;
-            threadSignalNoLock();
+            playerThreadCond.notify_one();
           }
           return;
         }        
@@ -623,7 +642,7 @@ void PlayerVideoLive::switchState(UCHAR newState)
           if (!video->independentAVStartUp())
           {
             videoStartup = true;
-            threadSignalNoLock();
+            playerThreadCond.notify_one();
           }
           return;
         }
@@ -679,6 +698,8 @@ void PlayerVideoLive::optimizeInstructionQueue()
 
 void PlayerVideoLive::threadMethod()
 {
+  std::unique_lock<std::mutex> ul(playerThreadMutex, std::defer_lock);
+
   while(1)
   {
     //logger->log("PlayerVideoLive", Log::DEBUG, "VS: %d pA %d",videoStartup,pendingAudioPlay);
@@ -691,7 +712,7 @@ void PlayerVideoLive::threadMethod()
       checkError();
     }  
   
-    while(!instructions.empty())
+    while (!instructions.empty())
     {
       if (instructions.size() > 1) optimizeInstructionQueue();
 
@@ -830,19 +851,13 @@ void PlayerVideoLive::threadMethod()
       }
       else if (i.instruction == I_STOP)
       {
-         logger->log("PlayerVideoLive", Log::DEBUG, "Stopping");
+         logger->log("PlayerVideoLive", Log::DEBUG, "Stopping by instruction");
          switchState(S_STOP);
          checkError();
-
-         stopNow = true;
-         break;
+         return;
       }
     }
 
-    threadCheckExit();
-
-    if (stopNow) break;
-
     while(streamChunks.size())
     {
       //logger->log("PlayerVideoLive", Log::DEBUG, "chunk mark1 %d", streamChunks.size());
@@ -872,9 +887,12 @@ void PlayerVideoLive::threadMethod()
       }
     }
     //logger->log("PlayerVideoLive", Log::DEBUG, "wait for signal %d", streamChunks.size());
-    threadLock();
-    threadWaitForSignal(); // unlocks and waits for signal
-    threadUnlock();
+
+    playerThreadMutex.lock();
+    if (!instructions.empty()) { playerThreadMutex.unlock(); continue; }
+    playerThreadCond.wait(ul);
+    playerThreadMutex.unlock();
+
     //logger->log("PlayerVideoLive", Log::DEBUG, "wait for signal2 %d",streamChunks.size());
   }
 
index 5fe49c2fac6b295fd5bcd36a241db65e3d8e1343..a7077489fb5653990376ab4050ed4dfdf4586f88 100644 (file)
 #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 "vfeed.h"
@@ -54,7 +52,7 @@ class DemuxerTS;
 class OSDReceiver;
 class DVBSubtitles;
 
-class PlayerVideoLive : public PlayerLive, public Thread_TYPE, public Callback, public StreamReceiver
+class PlayerVideoLive : public PlayerLive, public Callback, public StreamReceiver
 {
   public:
     PlayerVideoLive(MessageQueue* messageQueue, void* messageReceiver, OSDReceiver* tosdReceiver, ChannelList* chanList);
@@ -125,6 +123,10 @@ class PlayerVideoLive : public PlayerLive, public Thread_TYPE, public Callback,
     
     bool initted;
 
+    std::thread playerThread;
+    std::mutex playerThreadMutex;
+    std::condition_variable playerThreadCond;
+
     UCHAR state;
     const static UCHAR S_STOP = 1;
     const static UCHAR S_VIDEOSTARTUP = 2;
@@ -135,7 +137,6 @@ class PlayerVideoLive : public PlayerLive, public Thread_TYPE, public Callback,
 
     bool videoStartup;
     bool pendingAudioPlay;
-    bool stopNow;
     bool h264;
     int preBufferCount;
     const static int preBufferAmount = 3;