From b67b8fac58e26f7d81cfd3769d46b980ef79e347 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Tue, 25 Feb 2020 15:09:46 +0000 Subject: [PATCH] Convert all remaining mutexes to std::mutex --- dvbsubtitles.cc | 93 ++++++++++--------------------------- dvbsubtitles.h | 11 ++--- playerradio.cc | 120 +++++++++++++++--------------------------------- playerradio.h | 34 ++++++-------- stream.cc | 75 ++++++++---------------------- stream.h | 33 +++---------- tcp.cc | 24 ++-------- tcp.h | 8 +--- videowin.cc | 82 ++++++++++++++++----------------- videowin.h | 3 +- windowsosd.cc | 9 +--- windowsosd.h | 5 +- 12 files changed, 159 insertions(+), 338 deletions(-) diff --git a/dvbsubtitles.cc b/dvbsubtitles.cc index ff8e4cb..f90a8d6 100644 --- a/dvbsubtitles.cc +++ b/dvbsubtitles.cc @@ -550,18 +550,11 @@ DVBSubtitles::DVBSubtitles(OSDReceiver* tosd) SubtitleTimeout(0), timeout_clear(false) { -#ifndef WIN32 - pthread_mutex_init(&input_mutex, NULL); - pthread_mutex_init(&output_mutex, NULL); -#else - input_mutex=CreateMutex(NULL,FALSE,NULL); - output_mutex=CreateMutex(NULL,FALSE,NULL); -#endif } void DVBSubtitles::put(const PESPacket& packet) { - lockInput(); + input_mutex.lock(); if (running) { if (packet.getPTS() != PESPacket::PTS_INVALID) @@ -570,7 +563,7 @@ void DVBSubtitles::put(const PESPacket& packet) nudge(); } } - unlockInput(); + input_mutex.unlock(); } bool DVBSubtitles::decodePacket(const PESPacket& packet) @@ -814,59 +807,23 @@ void DVBSubtitles::finishPage(const DVBSubtitlePage& page) Log::getInstance()->log("SUBTITLES", Log::DEBUG, "SubtitleTimeout %d", page.timeout); } -void DVBSubtitles::lockInput() -{ -#ifndef WIN32 - pthread_mutex_lock(&input_mutex); -#else - WaitForSingleObject(input_mutex, INFINITE); -#endif -} - -void DVBSubtitles::unlockInput() -{ -#ifndef WIN32 - pthread_mutex_unlock(&input_mutex); -#else - ReleaseMutex(input_mutex); -#endif -} - -void DVBSubtitles::lockOutput() -{ -#ifndef WIN32 - pthread_mutex_lock(&output_mutex); -#else - WaitForSingleObject(output_mutex, INFINITE); -#endif -} - -void DVBSubtitles::unlockOutput() -{ -#ifndef WIN32 - pthread_mutex_unlock(&output_mutex); -#else - ReleaseMutex(output_mutex); -#endif -} - int DVBSubtitles::start() { - lockInput(); + input_mutex.lock(); dds=DVBSubtitleDisplayDefinition(); running = true; - unlockInput(); + input_mutex.unlock(); return threadStart(); } void DVBSubtitles::stop() { threadStop(); - lockInput(); + input_mutex.lock(); running = false; worklist.clear(); - unlockInput(); - lockOutput(); + input_mutex.unlock(); + output_mutex.lock(); PageMap::const_iterator pageEntry = pages.find(pageOnDisplay); if (pageEntry != pages.end()) { @@ -883,20 +840,20 @@ void DVBSubtitles::stop() } pages.clear(); pageOnDisplay = 65536; - unlockOutput(); + output_mutex.unlock(); threadNudged = false; } void DVBSubtitles::show() { - lockOutput(); + output_mutex.lock(); showing = true; - unlockOutput(); + output_mutex.unlock(); } void DVBSubtitles::hide() { - lockOutput(); + output_mutex.lock(); showing = false; PageMap::const_iterator pageEntry = pages.find(pageOnDisplay); if (pageEntry != pages.end()) @@ -914,7 +871,7 @@ void DVBSubtitles::hide() } pages.clear(); pageOnDisplay = 65536; - unlockOutput(); + output_mutex.unlock(); } void DVBSubtitles::nudge() @@ -951,14 +908,14 @@ void DVBSubtitles::threadMethod() { if (SubtitleTimeout.TimedOut()) // do we have a subtitle timeout { - lockOutput(); + output_mutex.lock(); if (showing && !osdMenuShowing) { if (!timeout_clear) { osd->clearOSD(); // if we have the timeout, lets clear the OSD timeout_clear=true; } } - unlockOutput(); + output_mutex.unlock(); } else if (showing) // if not lets check when will we have it { @@ -1007,10 +964,10 @@ void DVBSubtitles::threadMethod() threadCheckExit(); finished = true; pktPTS = PESPacket::PTS_INVALID; - lockInput(); + input_mutex.lock(); if (!worklist.empty()) pktPTS = worklist.front().getPTS(); - unlockInput(); + input_mutex.unlock(); if (pktPTS != PESPacket::PTS_INVALID) { // An entry exists in the work list nowPTS = Video::getInstance()->getCurrentTimestamp(); @@ -1018,24 +975,24 @@ void DVBSubtitles::threadMethod() PTSDifference(pktPTS, nowPTS) < 4000) { // It is due for processing or discarding. finished = false; - lockInput(); + input_mutex.lock(); if (!worklist.empty()) { PESPacket packet = worklist.front(); worklist.pop_front(); - unlockInput(); - lockOutput(); + input_mutex.unlock(); + output_mutex.lock(); if (showing) decodePacket(packet); - unlockOutput(); + output_mutex.unlock(); } - else unlockInput(); + else input_mutex.unlock(); } else if (PTSDifference(pktPTS, nowPTS) >= 60*90000) { // Seems like a bad or very old entry. Get rid of it. finished = false; - lockInput(); + input_mutex.lock(); if (!worklist.empty()) worklist.pop_front(); - unlockInput(); + input_mutex.unlock(); } } } @@ -1066,8 +1023,8 @@ void DVBSubtitles::threadMethod() void DVBSubtitles::setOSDMenuVisibility(bool visible) { - lockOutput(); + output_mutex.lock(); osdMenuShowing = visible; - unlockOutput(); + output_mutex.unlock(); } diff --git a/dvbsubtitles.h b/dvbsubtitles.h index c76abef..f3029f2 100644 --- a/dvbsubtitles.h +++ b/dvbsubtitles.h @@ -34,6 +34,7 @@ typedef unsigned long long uint64_t; #include #include #include +#include class cTimeMs { private: @@ -155,13 +156,9 @@ class DVBSubtitles : public Thread_TYPE void lockOutput(); void unlockOutput(); void threadMethod(); -#ifndef WIN32 - pthread_mutex_t input_mutex; - pthread_mutex_t output_mutex; -#else - HANDLE input_mutex; - HANDLE output_mutex; -#endif + + std::mutex input_mutex; + std::mutex output_mutex; }; #endif diff --git a/playerradio.cc b/playerradio.cc index 93ac63d..b291a28 100644 --- a/playerradio.cc +++ b/playerradio.cc @@ -17,8 +17,6 @@ along with VOMP. If not, see . */ -#include "playerradio.h" - #include "log.h" #include "audio.h" #include "video.h" @@ -29,30 +27,16 @@ #include "message.h" #include "messagequeue.h" +#include "playerradio.h" + // ----------------------------------- Called from outside, one offs or info funcs PlayerRadio::PlayerRadio(MessageQueue* tmessageQueue, void* tmessageReceiver) -: afeed(this) +: messageQueue(tmessageQueue), messageReceiver(tmessageReceiver), afeed(this) { - messageQueue = tmessageQueue; - messageReceiver = tmessageReceiver; audio = Audio::getInstance(); logger = Log::getInstance(); vdr = VDR::getInstance(); - initted = false; - lengthBytes = 0; - lengthFrames = 0; - currentFrameNumber = 0; - state = S_STOP; - - startPTS = 0; - lengthSeconds = 0; - - threadBuffer = NULL; - - blockSize = 10000; - startupBlockSize = 20000; - Video::getInstance()->turnVideoOff(); } @@ -61,26 +45,21 @@ PlayerRadio::~PlayerRadio() if (initted) shutdown(); } -int PlayerRadio::init(ULLONG tlengthBytes, ULONG tlengthFrames, bool isPesRecording) +bool PlayerRadio::init(ULLONG tlengthBytes, ULONG tlengthFrames, bool isPesRecording) { - if (initted) return 0; -#ifndef WIN32 - pthread_mutex_init(&mutex, NULL); -#else - mutex=CreateMutex(NULL,FALSE,NULL); -#endif + if (initted) return false; if (isPesRecording) demuxer = new DemuxerVDR(); else demuxer = new DemuxerTS(); - if (!demuxer) return 0; + if (!demuxer) return false; if (!demuxer->init(this, audio, NULL, NULL, 0, 40000, 0)) { logger->log("PlayerRadio", Log::ERR, "Demuxer failed to init"); shutdown(); - return 0; + return false; } afeed.init(); @@ -100,7 +79,7 @@ int PlayerRadio::init(ULLONG tlengthBytes, ULONG tlengthFrames, bool isPesRecord logger->log("PlayerRadio", Log::ERR, "Failed to get start block"); shutdown(); if (!vdr->isConnected()) doConnectionLost(); - return 0; + return false; } success = demuxer->findPTS(buffer, thisRead, &startPTS); @@ -109,7 +88,7 @@ int PlayerRadio::init(ULLONG tlengthBytes, ULONG tlengthFrames, bool isPesRecord logger->log("PlayerRadio", Log::ERR, "Failed to get start PTS"); free(buffer); shutdown(); - return 0; + return false; } free(buffer); @@ -118,11 +97,11 @@ int PlayerRadio::init(ULLONG tlengthBytes, ULONG tlengthFrames, bool isPesRecord { logger->log("PlayerRadio", Log::ERR, "Failed to setLengthSeconds"); shutdown(); - return 0; + return false; } initted = true; - return 1; + return true; } bool PlayerRadio::setLengthSeconds() @@ -148,11 +127,11 @@ bool PlayerRadio::setLengthSeconds() if (startPTS < endPTS) { - lengthSeconds = (endPTS - startPTS) / 90000; + lengthSeconds = static_cast((endPTS - startPTS) / 90000); } else { - lengthSeconds = (startPTS - endPTS) / 90000; + lengthSeconds = static_cast((startPTS - endPTS) / 90000); } return true; @@ -167,10 +146,6 @@ int PlayerRadio::shutdown() delete demuxer; demuxer = NULL; -#ifdef WIN32 - CloseHandle(mutex); -#endif - return 1; } @@ -192,7 +167,7 @@ ULONG PlayerRadio::getCurrentSeconds() long long currentPTS = demuxer->getAudioPTS(); currentPTS -= startPTS; if (currentPTS < 0) currentPTS += 8589934592ULL; - ULONG ret = currentPTS / 90000; + ULONG ret = static_cast(currentPTS / 90000); return ret; } @@ -202,38 +177,38 @@ void PlayerRadio::play() { if (!initted) return; if (state == S_PLAY) return; - lock(); + stateLock.lock(); switchState(S_PLAY); - unLock(); + stateLock.unlock(); } void PlayerRadio::playpause() { if (!initted) return; - lock(); + stateLock.lock(); if (state==S_PLAY) { switchState(S_PAUSE_P); } else { switchState(S_PLAY); } - unLock(); + stateLock.unlock(); } void PlayerRadio::stop() { if (!initted) return; if (state == S_STOP) return; - lock(); + stateLock.lock(); logger->log("PlayerRadio", Log::DEBUG, "Stop called lock"); switchState(S_STOP); - unLock(); + stateLock.unlock(); } void PlayerRadio::pause() { if (!initted) return; - lock(); + stateLock.lock(); if (state == S_PAUSE_P) { @@ -244,53 +219,53 @@ void PlayerRadio::pause() switchState(S_PAUSE_P); } - unLock(); + stateLock.unlock(); } void PlayerRadio::jumpToPercent(double percent) { - lock(); + stateLock.lock(); logger->log("PlayerRadio", Log::DEBUG, "JUMP TO %i%%", percent); - ULONG newFrame = (ULONG)(percent * lengthFrames / 100); + ULONG newFrame = static_cast(percent * lengthFrames / 100); switchState(S_JUMP, newFrame); - unLock(); + stateLock.unlock(); } void PlayerRadio::skipForward(UINT seconds) { - lock(); + stateLock.lock(); logger->log("PlayerRadio", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds); ULONG currentSeconds = getCurrentSeconds(); ULONG currentFrame = demuxer->getPacketNum(); - if (currentSeconds == 0) { unLock(); return; } // div by zero - if (currentFrame == 0) { unLock(); return; } // Current pos from demuxer is not valid + if (currentSeconds == 0) { stateLock.unlock(); return; } // div by zero + if (currentFrame == 0) { stateLock.unlock(); return; } // Current pos from demuxer is not valid ULONG newFrame = currentFrame + (currentFrame * seconds / currentSeconds); - if (newFrame > lengthFrames) { switchState(S_PLAY); unLock(); } + if (newFrame > lengthFrames) { switchState(S_PLAY); stateLock.unlock(); } else switchState(S_JUMP, newFrame); - unLock(); + stateLock.unlock(); } void PlayerRadio::skipBackward(UINT seconds) { - lock(); + stateLock.lock(); logger->log("PlayerRadio", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds); ULONG currentSeconds = getCurrentSeconds(); ULONG currentFrame = demuxer->getPacketNum(); - if (currentSeconds == 0) { unLock(); return; } // div by zero - if (currentFrame == 0) { unLock(); return; } // Current pos from demuxer is not valid + if (currentSeconds == 0) { stateLock.unlock(); return; } // div by zero + if (currentFrame == 0) { stateLock.unlock(); return; } // Current pos from demuxer is not valid ULONG newFrame; - if ((UINT)seconds > currentSeconds) + if (seconds > currentSeconds) newFrame = 0; else newFrame = currentFrame - (currentFrame * seconds / currentSeconds); switchState(S_JUMP, newFrame); - unLock(); + stateLock.unlock(); } // ----------------------------------- Implementations called events @@ -414,27 +389,6 @@ void PlayerRadio::switchState(UCHAR toState, ULONG jumpToFrame) // ----------------------------------- Internal functions -void PlayerRadio::lock() -{ -#ifndef WIN32 - pthread_mutex_lock(&mutex); - logger->log("PlayerRadio", Log::DEBUG, "LOCKED"); - -#else - WaitForSingleObject(mutex, INFINITE); -#endif -} - -void PlayerRadio::unLock() -{ -#ifndef WIN32 - logger->log("PlayerRadio", Log::DEBUG, "UNLOCKING"); - pthread_mutex_unlock(&mutex); -#else - ReleaseMutex(mutex); -#endif -} - void PlayerRadio::restartAtFrame(ULONG newFrame) { afeed.stop(); @@ -515,14 +469,14 @@ void PlayerRadio::threadFeedPlay() if (startup) { if (startupBlockSize > lengthBytes) - askFor = lengthBytes; // is a very small recording! + askFor = static_cast(lengthBytes); // is a very small recording! else askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams } else { if ((feedPosition + blockSize) > lengthBytes) // last block of recording - askFor = lengthBytes - feedPosition; + askFor = static_cast(lengthBytes - feedPosition); else // normal askFor = blockSize; } diff --git a/playerradio.h b/playerradio.h index 07f9f41..57e6625 100644 --- a/playerradio.h +++ b/playerradio.h @@ -28,6 +28,8 @@ #endif #include +#include + #include "threadsystem.h" #include "callback.h" @@ -54,7 +56,7 @@ class PlayerRadio : public Thread_TYPE, public Callback PlayerRadio(MessageQueue* messageQueue, void* messageReceiver); virtual ~PlayerRadio(); - int init(ULLONG lengthBytes, ULONG lengthFrames, bool IsPesRecording); + bool init(ULLONG lengthBytes, ULONG lengthFrames, bool IsPesRecording); int shutdown(); void setCurrentFrameNumber(ULONG num); @@ -106,27 +108,21 @@ class PlayerRadio : public Thread_TYPE, public Callback VDR* vdr; AFeed afeed; - bool initted; + bool initted{}; bool startup; - ULLONG startPTS; - ULONG lengthSeconds; + ULLONG startPTS{}; + ULONG lengthSeconds{}; -#ifndef WIN32 - pthread_mutex_t mutex; -#else - HANDLE mutex; -#endif - void lock(); - void unLock(); - - ULLONG lengthBytes; - ULONG lengthFrames; - ULONG currentFrameNumber; - UINT blockSize; - UINT startupBlockSize; - UCHAR* threadBuffer; - UCHAR state; + std::mutex stateLock; + + ULLONG lengthBytes{}; + ULONG lengthFrames{}; + ULONG currentFrameNumber{}; + static const UINT blockSize{10000}; + static const UINT startupBlockSize{20000}; + UCHAR* threadBuffer{}; + UCHAR state{S_STOP}; }; #endif diff --git a/stream.cc b/stream.cc index 206fd26..64049c5 100644 --- a/stream.cc +++ b/stream.cc @@ -18,15 +18,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "stream.h" -#include "log.h" +#include +#include -Stream::Stream() -{ - initted = 0; - draintarget = NULL; - cur_packet_pos = 0; -} +#include "log.h" +#include "stream.h" Stream::~Stream() { @@ -35,39 +31,27 @@ Stream::~Stream() void Stream::shutdown() { - if (initted) - { - free(outbuf); -#ifdef WIN32 - CloseHandle(mutex); -#endif - - } + if (initted) free(outbuf); initted = 0; } int Stream::init(DrainTarget* tdt, int bufsize) { - outbuf = (UCHAR*) malloc(bufsize); + outbuf = (UCHAR*)malloc(bufsize); if (!outbuf) return 0; draintarget = tdt; bufferSize = bufsize; initted = 1; -#ifndef WIN32 - pthread_mutex_init(&mutex, NULL); -#else - mutex=CreateMutex(NULL,FALSE,NULL); -#endif return 1; } void Stream::flush() { - lock(); - + mutex.lock(); mediapackets.clear(); cur_packet_pos = 0; - unLock(); + mutex.unlock(); + if (draintarget) draintarget->ResetTimeOffsets(); } @@ -113,7 +97,7 @@ int Stream::put(const UCHAR* inbuf, int len, UCHAR type,unsigned int index) } } - lock(); + mutex.lock(); int front, back; if (mediapackets.empty()) { @@ -125,7 +109,7 @@ int Stream::put(const UCHAR* inbuf, int len, UCHAR type,unsigned int index) back = mediapackets.back().pos_buffer + mediapackets.back().length; if (back == bufferSize) back = 0; } - unLock(); + mutex.unlock(); if (back <= front) { @@ -148,9 +132,9 @@ int Stream::put(const UCHAR* inbuf, int len, UCHAR type,unsigned int index) { memcpy(outbuf + back, inbuf, len); newPacket.pos_buffer = back; - lock(); + mutex.lock(); mediapackets.push_back(newPacket); - unLock(); + mutex.unlock(); } else { // Log::getInstance()->log("Stream", Log::DEBUG, "We are full %d!",bufferSize); } @@ -161,17 +145,17 @@ int Stream::put(const UCHAR* inbuf, int len, UCHAR type,unsigned int index) bool Stream::drain(bool * dataavail) { bool ret = false; - if (dataavail) *dataavail=false; + if (dataavail) *dataavail = false; if (draintarget->DrainTargetBufferFull()) return false; //We are full, no need to do something else - lock(); + mutex.lock(); UINT listlength = mediapackets.size(); if (listlength != 0) { draintarget->PrepareMediaSample(mediapackets, cur_packet_pos); - unLock(); - if (dataavail && draintarget->DrainTargetReady()) *dataavail=true; + mutex.unlock(); + if (dataavail && draintarget->DrainTargetReady()) *dataavail = true; UINT consumed = draintarget->DeliverMediaSample(outbuf, &cur_packet_pos); - lock(); + mutex.lock(); if (consumed != 0) ret = true; if (consumed > listlength) consumed = listlength; while (consumed--) @@ -179,27 +163,6 @@ bool Stream::drain(bool * dataavail) mediapackets.pop_front(); } } - unLock(); + mutex.unlock(); return ret; } - -void Stream::lock() -{ -#ifndef WIN32 - pthread_mutex_lock(&mutex); - //logger->log("Player", Log::DEBUG, "LOCKED"); - -#else - WaitForSingleObject(mutex, INFINITE ); -#endif -} - -void Stream::unLock() -{ -#ifndef WIN32 - //logger->log("Player", Log::DEBUG, "UNLOCKING"); - pthread_mutex_unlock(&mutex); -#else - ReleaseMutex(mutex); -#endif -} diff --git a/stream.h b/stream.h index dc97dfc..301138f 100644 --- a/stream.h +++ b/stream.h @@ -21,46 +21,27 @@ #ifndef STREAM_H #define STREAM_H -#ifndef WIN32 -#include -#endif +#include -#include -#ifndef WIN32 -#include -#else -#include -#include -#endif -#include #include "defines.h" - #include "draintarget.h" class Stream { public: - Stream(); ~Stream(); int init(DrainTarget* tdt, int bufsize); void shutdown(); void flush(); - int put(const UCHAR* inbuf, int len, UCHAR type,unsigned int index); - bool drain(bool * dataavail=NULL); + int put(const UCHAR* inbuf, int len, UCHAR type, UINT index); + bool drain(bool* dataavail = NULL); private: MediaPacketList mediapackets; - UINT cur_packet_pos; -#ifndef WIN32 - pthread_mutex_t mutex; -#else - HANDLE mutex; -#endif - void lock(); - void unLock(); - - DrainTarget* draintarget; - int initted; + UINT cur_packet_pos{}; + std::mutex mutex; + DrainTarget* draintarget{}; + int initted{}; UCHAR* outbuf; int bufferSize; }; diff --git a/tcp.cc b/tcp.cc index de01ee1..be2133a 100644 --- a/tcp.cc +++ b/tcp.cc @@ -32,25 +32,11 @@ #include "tcp.h" -#ifndef WIN32 -#define MUTEX_LOCK(mutex) pthread_mutex_lock(mutex) -#define MUTEX_UNLOCK(mutex) pthread_mutex_unlock(mutex) -#else -#define MUTEX_LOCK(mutex) WaitForSingleObject(*(mutex), INFINITE ) -#define MUTEX_UNLOCK(mutex) ReleaseMutex(*(mutex)) -#endif - TCP::TCP() { sock = 0; connected = 0; timeoutEnabled = 1; - -#ifndef WIN32 - pthread_mutex_init(&mutex, NULL); -#else - mutex=CreateMutex(NULL,FALSE,NULL); -#endif } TCP::~TCP() @@ -60,10 +46,6 @@ TCP::~TCP() CLOSESOCKET(sock); Log::getInstance()->log("TCP", Log::DEBUG, "Have closed"); } - -#ifdef WIN32 - CloseHandle(mutex); -#endif } void TCP::disableTimeout() @@ -295,7 +277,7 @@ int TCP::sendData(void* bufR, size_t count) unsigned char* buf = static_cast(bufR); - MUTEX_LOCK(&mutex); + mutex.lock(); while (bytes_sent < count) { @@ -311,14 +293,14 @@ int TCP::sendData(void* bufR, size_t count) #endif if (this_write <= 0) { - MUTEX_UNLOCK(&mutex); + mutex.unlock(); return(this_write); } bytes_sent += this_write; buf += this_write; } - MUTEX_UNLOCK(&mutex); + mutex.unlock(); return(count); } diff --git a/tcp.h b/tcp.h index 524cfea..f053752 100644 --- a/tcp.h +++ b/tcp.h @@ -28,6 +28,7 @@ #include #include #include +#include #ifndef WIN32 #include @@ -70,12 +71,7 @@ class TCP int connected; int timeoutEnabled; int dataLength; - -#ifndef WIN32 - pthread_mutex_t mutex; -#else - HANDLE mutex; -#endif + std::mutex mutex; static UCHAR dcc(UCHAR c); }; diff --git a/videowin.cc b/videowin.cc index dcbf5dc..792e2a3 100644 --- a/videowin.cc +++ b/videowin.cc @@ -57,7 +57,6 @@ VideoWin::VideoWin() lastaudiomode=MPTYPE_MPEG_AUDIO; //lastaudiomode=MPTYPE_AC3; dsvmrsurfnotify=NULL; - filtermutex=CreateMutex(NULL,FALSE,NULL); offsetnotset=true; offsetvideonotset=true; offsetaudionotset=true; @@ -104,7 +103,6 @@ VideoWin::VideoWin() VideoWin::~VideoWin() { CleanupDS(); - CloseHandle(filtermutex); unsigned int i; for (i=0;ilog("VideoWin", Log::WARN ,"Failed creating VMR9 renderer!"); - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); } /*VMR 9 stuff**/ if (hres=dsgraphbuilder->AddFilter(dsrenderer,L"VMR9")!=S_OK) { - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); Log::getInstance()->log("VideoWin", Log::WARN ,"Failed adding VMR9 renderer!"); return 0; @@ -1027,7 +1025,7 @@ int VideoWin::dsInitVideoFilter() IVMRFilterConfig9* vmrfilconfig; if (dsrenderer->QueryInterface(IID_IVMRFilterConfig9,(void**)&vmrfilconfig)!=S_OK) { - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting VMR9 Filterconfig interface!"); return 0; @@ -1038,7 +1036,7 @@ int VideoWin::dsInitVideoFilter() if (dsrenderer->QueryInterface(IID_IVMRSurfaceAllocatorNotify9, (void**)& dsvmrsurfnotify) != S_OK) { - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting VMR9 Surface Allocator interface!"); return 0; @@ -1050,7 +1048,7 @@ int VideoWin::dsInitVideoFilter() IVMRDeinterlaceControl9* deintctrl; if (dsrenderer->QueryInterface(IID_IVMRDeinterlaceControl9,(void**)&deintctrl)!=S_OK) { - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting VMR9 Deinterlace control!"); return 0; @@ -1081,13 +1079,13 @@ int VideoWin::dsInitVideoFilter() CLSCTX_INPROC_SERVER,IID_IBaseFilter,(void**) &dsrenderer)!=S_OK) { Log::getInstance()->log("VideoWin", Log::WARN ,"Failed creating EVR renderer!"); - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); } /*EVR stuff**/ if (hres=dsgraphbuilder->AddFilter(dsrenderer,L"EVR")!=S_OK) { - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); Log::getInstance()->log("VideoWin", Log::WARN ,"Failed adding EVR renderer!"); return 0; @@ -1097,7 +1095,7 @@ int VideoWin::dsInitVideoFilter() IMFGetService *evr_services; if (dsrenderer->QueryInterface(IID_IMFGetService,(void**)&evr_services)!=S_OK) { - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting EVR IMFGetServices interface!"); return 0; @@ -1106,7 +1104,7 @@ int VideoWin::dsInitVideoFilter() IMFVideoDisplayControl* mfvideodisplaycontrol; if (evr_services->GetService(MR_VIDEO_RENDER_SERVICE,IID_IMFVideoDisplayControl,(void**)&mfvideodisplaycontrol)!=S_OK) { - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting EVR IMFVideoDisplayControl interface!"); return 0; @@ -1126,7 +1124,7 @@ int VideoWin::dsInitVideoFilter() IMFVideoRenderer *mfvideorenderer; if (dsrenderer->QueryInterface(IID_IMFVideoRenderer,(void**)&mfvideorenderer)!=S_OK) { - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting EVR IMFVideoRenderer interface!"); return 0; @@ -1140,7 +1138,7 @@ int VideoWin::dsInitVideoFilter() /* IVMRDeinterlaceControl9* deintctrl; if (dsrenderer->QueryInterface(IID_IVMRDeinterlaceControl9,(void**)&deintctrl)!=S_OK) { - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting VMR9 Deinterlace control!"); return 0; @@ -1171,7 +1169,7 @@ int VideoWin::dsInitVideoFilter() if (dsgraphbuilder->QueryInterface(IID_IFilterGraph2,(void**)&fg2)!= S_OK) { Log::getInstance()->log("VideoWin", Log::WARN , "Failed querying for FilterGraph2 Interface!"); - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); return 0; } @@ -1182,7 +1180,7 @@ int VideoWin::dsInitVideoFilter() { Log::getInstance()->log("VideoWin", Log::WARN , "Failed rendering Video!"); fg2->Release(); - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); return 0; } @@ -1202,7 +1200,7 @@ int VideoWin::dsInitVideoFilter() if (hres=dsgraphbuilder->AddFilter(videofilter,NULL) != S_OK) { Log::getInstance()->log("VideoWin", Log::WARN , "Failed adding Video Filter!"); - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); return 0; } @@ -1264,7 +1262,7 @@ int VideoWin::dsInitVideoFilter() { Log::getInstance()->log("VideoWin", Log::WARN , "Video Filter has no suitable input!"); videofilter->Release(); - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); return 0; } @@ -1293,7 +1291,7 @@ int VideoWin::dsInitVideoFilter() { Log::getInstance()->log("VideoWin", Log::WARN , "Video Filter has no suitable output!"); videofilter->Release(); - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); return 0; } @@ -1327,11 +1325,11 @@ int VideoWin::dsplay() //Build filter graph HRESULT hres; //So this is the real code, this prevents the feeder from calling noexisting objects! - WaitForSingleObject(filtermutex,INFINITE); + filtermutex.lock(); if (hres=CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER, IID_IGraphBuilder,(void**)&dsgraphbuilder) != S_OK) { - ReleaseMutex(filtermutex); + filtermutex.unlock(); return 0; } #ifdef DS_DEBUG @@ -1350,7 +1348,7 @@ int VideoWin::dsplay() if (hres=dsgraphbuilder->AddFilter(sourcefilter,L"Vomp Win Source Filter") != S_OK) { Log::getInstance()->log("VideoWin", Log::WARN , "Failed adding Vomp Source Filter!"); - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); return 0; } @@ -1358,14 +1356,14 @@ int VideoWin::dsplay() /*if (hres=dsgraphbuilder->Render((IPin*)sourcefilter->GetAudioPin()/*audio*)!=S_OK) { Log::getInstance()->log("VideoWin", Log::WARN , "Failed rendering audio!"); - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); return 0; }*/ if (((AudioWin*)Audio::getInstance())->dsInitAudioFilter(dsgraphbuilder)==0) { Log::getInstance()->log("VideoWin", Log::WARN , "Failed rendering audio!"); - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); return 0; } @@ -1393,7 +1391,7 @@ int VideoWin::dsplay() hresdeb=dsmediacontrol->Run(); iframemode=false;//exit iframe mode - ReleaseMutex(filtermutex); + filtermutex.unlock(); return 1; } @@ -1402,13 +1400,13 @@ int VideoWin::EnterIframePlayback() if (!initted) return 0; CleanupDS(); //So this is the real code, this prevents the feeder from calling noexisting objects! - WaitForSingleObject(filtermutex,INFINITE); + filtermutex.lock(); iframemode=true;//enter iframe mode //Build filter graph HRESULT hres; if (hres=CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER, IID_IGraphBuilder,(void**)&dsgraphbuilder)!=S_OK) { - ReleaseMutex(filtermutex); + filtermutex.unlock(); return 0; } #ifdef DS_DEBUG @@ -1420,7 +1418,7 @@ int VideoWin::EnterIframePlayback() // to DirectShow if (hres=dsgraphbuilder->AddFilter(sourcefilter,L"Vomp Win Source Filter")!=S_OK) { Log::getInstance()->log("VideoWin", Log::WARN , "Failed adding Vomp Source Filter!"); - ReleaseMutex(filtermutex); + filtermutex.unlock(); CleanupDS(); return 0; } @@ -1443,7 +1441,7 @@ int VideoWin::EnterIframePlayback() dsmediacontrol->Run(); - ReleaseMutex(filtermutex); + filtermutex.unlock(); return 1; } @@ -1486,9 +1484,9 @@ int VideoWin::dsreset() int VideoWin::dspause() { if (!initted) return 0; - WaitForSingleObject(filtermutex,INFINITE); + filtermutex.lock(); if (dsmediacontrol) dsmediacontrol->Pause(); - ReleaseMutex(filtermutex); + filtermutex.unlock(); return 1; } @@ -1508,9 +1506,9 @@ int VideoWin::unPause() // FIXME get rid - same as play!! int VideoWin::dsunPause() // FIXME get rid - same as play!! {//No on windows this is not the same, I don't get rid of! if (!initted) return 0; - WaitForSingleObject(filtermutex,INFINITE); + filtermutex.lock(); if (dsmediacontrol) dsmediacontrol->Run(); - ReleaseMutex(filtermutex); + filtermutex.unlock(); return 1; } @@ -1581,7 +1579,7 @@ ULLONG VideoWin::frameNumberToTimecode(ULONG framenumber) */ void VideoWin::CleanupDS() { - WaitForSingleObject(filtermutex,INFINITE); + filtermutex.lock(); dsinited=false; if (dsmediacontrol)dsmediacontrol->Stop(); if (cur_audio_media_sample) { @@ -1635,7 +1633,7 @@ void VideoWin::CleanupDS() sourcefilter=NULL; //The Graph Builder destroys our SourceFilter } - ReleaseMutex(filtermutex); + filtermutex.unlock(); } @@ -1796,9 +1794,9 @@ UINT VideoWin::DeliverMediaPacket(MediaPacket packet, int VideoWin::getCurrentAudioMediaSample(IMediaSample** ms) { - //WaitForSingleObject(filtermutex,INFINITE); + //filtermutex.lock(); if (!sourcefilter){ - // ReleaseMutex(filtermutex); + // filtermutex.unlock(); return 0; } if (cur_audio_media_sample) { @@ -1806,7 +1804,7 @@ int VideoWin::getCurrentAudioMediaSample(IMediaSample** ms) return 1; } if (!sourcefilter->getCurrentAudioMediaSample(ms)) { - // ReleaseMutex(filtermutex); + // filtermutex.unlock(); } if (*ms) (*ms)->SetActualDataLength(0); cur_audio_media_sample=*ms; @@ -1816,9 +1814,9 @@ int VideoWin::getCurrentAudioMediaSample(IMediaSample** ms) int VideoWin::getCurrentVideoMediaSample(IMediaSample** ms) { - //WaitForSingleObject(filtermutex,INFINITE); + //filtermutex.lock(); if (!sourcefilter){ - // ReleaseMutex(filtermutex); + // filtermutex.unlock(); return 0; } if (cur_video_media_sample) { @@ -1826,7 +1824,7 @@ int VideoWin::getCurrentVideoMediaSample(IMediaSample** ms) return 1; } if (!sourcefilter->getCurrentVideoMediaSample(ms)) { - // ReleaseMutex(filtermutex); + // filtermutex.unlock(); } if (*ms) (*ms)->SetActualDataLength(0); @@ -1840,7 +1838,7 @@ int VideoWin::DeliverAudioMediaSample(){ sourcefilter->DeliverAudioMediaSample(cur_audio_media_sample); cur_audio_media_sample=NULL; } - //ReleaseMutex(filtermutex); + //filtermutex.unlock(); return 1; } @@ -1849,7 +1847,7 @@ int VideoWin::DeliverVideoMediaSample(){ sourcefilter->DeliverVideoMediaSample(cur_video_media_sample); cur_video_media_sample=NULL; } - //ReleaseMutex(filtermutex); + //filtermutex.unlock(); return 1; } diff --git a/videowin.h b/videowin.h index f5a98c5..fa84153 100644 --- a/videowin.h +++ b/videowin.h @@ -28,6 +28,7 @@ #include #include #include +#include #include "defines.h" #include "video.h" @@ -197,7 +198,7 @@ private: DsSourceFilter* sourcefilter; DsAllocator* allocatorvmr; - HANDLE filtermutex; + std::mutex filtermutex; void CleanupDS(); bool offsetnotset; bool offsetvideonotset; diff --git a/windowsosd.cc b/windowsosd.cc index 780b185..36a8340 100644 --- a/windowsosd.cc +++ b/windowsosd.cc @@ -18,7 +18,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - #include "windowsosd.h" #include "dsallocator.h" #include "video.h" @@ -26,14 +25,12 @@ #include "log.h" #include "colour.h" - typedef HRESULT(__stdcall *FCT_DXVA2CreateDirect3DDeviceManager9)(UINT* pResetToken, IDirect3DDeviceManager9** ppDeviceManager); typedef HRESULT(__stdcall *FCT_MFCreateVideoSampleFromSurface)(IUnknown* pUnkSurface, IMFSample** ppSample); FCT_DXVA2CreateDirect3DDeviceManager9 ptrDXVA2CreateDirect3DDeviceManager9 = NULL; FCT_MFCreateVideoSampleFromSurface ptrMFCreateVideoSampleFromSurface = NULL; - WindowsOsd::WindowsOsd() { d3d = NULL; @@ -53,7 +50,6 @@ WindowsOsd::WindowsOsd() lastrendertime = timeGetTime(); event = CreateEvent(NULL,/*FALSE*/TRUE, FALSE, NULL); - d3dmutex = CreateMutex(NULL, FALSE, NULL); /*EVR stuff*/ dxvadevicehandle = NULL; @@ -86,7 +82,6 @@ WindowsOsd::WindowsOsd() WindowsOsd::~WindowsOsd() { CloseHandle(event); - CloseHandle(d3dmutex); } @@ -355,13 +350,13 @@ void WindowsOsd::RenderDS(LPDIRECT3DSURFACE9 present){ void WindowsOsd::BeginPainting() {//We synchronize calls to d3d between different threads - WaitForSingleObject(d3dmutex, INFINITE); + d3dmutex.lock(); LockDevice(); } void WindowsOsd::EndPainting() { UnlockDevice(); - ReleaseMutex(d3dmutex); + d3dmutex.unlock(); } DWORD WindowsOsd::getFilterCaps() diff --git a/windowsosd.h b/windowsosd.h index fa1c723..4bb1950 100644 --- a/windowsosd.h +++ b/windowsosd.h @@ -21,6 +21,7 @@ #ifndef WINDOWSOSD_H #define WINDOWSOSD_H +#include #include #include #include @@ -70,7 +71,7 @@ public: void BeginPainting(); void EndPainting(); - virtual int isInitialized()=0; + virtual bool isInitialized()=0; void threadMethod(); @@ -126,7 +127,7 @@ protected: DsAllocator* dsallocator; bool external_driving; - HANDLE d3dmutex; + std::mutex d3dmutex; HANDLE event; DWORD lastrendertime; DWORD lastosdrendertime; -- 2.39.5