From 4f12976d30331ee91fbcaa878fd2b4180c8ae0bc Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Fri, 20 Mar 2020 15:13:19 +0000 Subject: [PATCH] Switch PlayerRadioLive to std::thread --- playerradiolive.cc | 62 ++++++++++++++++++++++++++++------------------ playerradiolive.h | 40 +++++++++++------------------- playervideolive.cc | 6 +++++ playervideolive.h | 12 +-------- 4 files changed, 60 insertions(+), 60 deletions(-) diff --git a/playerradiolive.cc b/playerradiolive.cc index 8add617..086e069 100644 --- a/playerradiolive.cc +++ b/playerradiolive.cc @@ -1,5 +1,5 @@ /* - Copyright 2008 Chris Tallon + Copyright 2008-2020 Chris Tallon This file is part of VOMP. @@ -17,7 +17,11 @@ along with VOMP. If not, see . */ -#include "playerradiolive.h" +#include +#ifndef WIN32 +#include +#endif +#include #include "defines.h" #include "log.h" @@ -30,22 +34,17 @@ #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 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"); diff --git a/playerradiolive.h b/playerradiolive.h index 6ee6e02..6bf4fea 100644 --- a/playerradiolive.h +++ b/playerradiolive.h @@ -1,5 +1,5 @@ /* - Copyright 2008 Chris Tallon + Copyright 2008-2020 Chris Tallon This file is part of VOMP. @@ -20,23 +20,13 @@ #ifndef PLAYERRADIOLIVE_H #define PLAYERRADIOLIVE_H -#include -#include -#ifndef WIN32 -#include -#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 "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 int* getTeletxtSubtitlePages(); 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 instructions; const static UCHAR I_SETCHANNEL = 1; const static UCHAR I_STOP = 2; - + std::queue 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 - diff --git a/playervideolive.cc b/playervideolive.cc index ea17eac..06d0dfc 100644 --- a/playervideolive.cc +++ b/playervideolive.cc @@ -17,6 +17,12 @@ along with VOMP. If not, see . */ +#include +#ifndef WIN32 +#include +#endif +#include + #include "defines.h" #include "log.h" #include "audio.h" diff --git a/playervideolive.h b/playervideolive.h index b619433..4581098 100644 --- a/playervideolive.h +++ b/playervideolive.h @@ -20,13 +20,6 @@ #ifndef PLAYERVIDEOLIVE_H #define PLAYERVIDEOLIVE_H -#include -#include -#ifndef WIN32 -#include -#endif -#include - #include #include #include @@ -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 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; -- 2.39.5