From a780987e028e97d416594b2a9f70008af5b04da7 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sat, 14 Mar 2020 18:20:10 +0000 Subject: [PATCH] Convert PlayerVideoLive to std::thread --- .astylerc | 1 + playervideolive.cc | 66 +++++++++++++++++++++++++++++----------------- playervideolive.h | 17 ++++++------ 3 files changed, 52 insertions(+), 32 deletions(-) diff --git a/.astylerc b/.astylerc index f1a7f7c..1b85e89 100644 --- a/.astylerc +++ b/.astylerc @@ -15,3 +15,4 @@ keep-one-line-blocks attach-return-type attach-return-type-decl lineend=linux +indent-preproc-block diff --git a/playervideolive.cc b/playervideolive.cc index bd68b07..d41f992 100644 --- a/playervideolive.cc +++ b/playervideolive.cc @@ -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 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()); } diff --git a/playervideolive.h b/playervideolive.h index 5fe49c2..a707748 100644 --- a/playervideolive.h +++ b/playervideolive.h @@ -27,16 +27,14 @@ #endif #include +#include +#include +#include + #include #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; -- 2.39.5