1 From 482d898333facf53bd3208cf5e44a0cf3e1f4f3b Mon Sep 17 00:00:00 2001
2 From: phunkyfish <phunkyfish@gmail.com>
3 Date: Thu, 8 Oct 2020 14:59:55 +0100
4 Subject: [PATCH] Use std::thread, std::mutex, condition_variable instead of
7 Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
9 src/lib/tsreader/DeMultiplexer.cpp | 2 +-
10 src/lib/tsreader/DeMultiplexer.h | 4 +-
11 src/lib/tsreader/FileReader.cpp | 2 +-
12 src/lib/tsreader/MemoryBuffer.cpp | 15 +++--
13 src/lib/tsreader/MemoryBuffer.h | 7 +-
14 src/lib/tsreader/MemoryReader.h | 1 +
15 src/lib/tsreader/MemorySink.cpp | 2 +-
16 src/lib/tsreader/MemorySink.h | 4 +-
17 src/lib/tsreader/MepoRTSPClient.cpp | 28 ++++----
18 src/lib/tsreader/MepoRTSPClient.h | 10 ++-
19 src/lib/tsreader/MultiFileReader.cpp | 9 ++-
20 src/os-dependent.h | 95 ++++++++++++++++++++++++++++
21 src/pvrclient-mediaportal.cpp | 29 ++++-----
22 src/pvrclient-mediaportal.h | 21 +++---
23 14 files changed, 168 insertions(+), 61 deletions(-)
25 diff --git a/src/lib/tsreader/DeMultiplexer.cpp b/src/lib/tsreader/DeMultiplexer.cpp
26 index 436e452..3d0d9a2 100644
27 --- a/src/lib/tsreader/DeMultiplexer.cpp
28 +++ b/src/lib/tsreader/DeMultiplexer.cpp
29 @@ -104,7 +104,7 @@ namespace MPTV
30 if (m_filter.IsSeeking())
31 return 0; // Ambass : to check
33 - P8PLATFORM::CLockObject lock(m_sectionRead);
34 + std::lock_guard<std::mutex> lock(m_sectionRead);
38 diff --git a/src/lib/tsreader/DeMultiplexer.h b/src/lib/tsreader/DeMultiplexer.h
39 index c7cd577..72ed87d 100644
40 --- a/src/lib/tsreader/DeMultiplexer.h
41 +++ b/src/lib/tsreader/DeMultiplexer.h
43 #include "PacketSync.h"
45 #include "PatParser.h"
46 -#include "p8-platform/threads/mutex.h"
51 @@ -60,7 +60,7 @@ namespace MPTV
53 unsigned long long m_LastDataFromRtsp;
55 - P8PLATFORM::CMutex m_sectionRead;
56 + std::mutex m_sectionRead;
58 CPatParser m_patParser;
60 diff --git a/src/lib/tsreader/FileReader.cpp b/src/lib/tsreader/FileReader.cpp
61 index 73b23af..358b05f 100644
62 --- a/src/lib/tsreader/FileReader.cpp
63 +++ b/src/lib/tsreader/FileReader.cpp
65 #include "FileReader.h"
66 #include <kodi/General.h> //for kodi::Log
68 -#include "p8-platform/threads/threads.h"
69 +#include "os-dependent.h"
70 #include <algorithm> //std::min, std::max
73 diff --git a/src/lib/tsreader/MemoryBuffer.cpp b/src/lib/tsreader/MemoryBuffer.cpp
74 index 0e736f2..b5400da 100644
75 --- a/src/lib/tsreader/MemoryBuffer.cpp
76 +++ b/src/lib/tsreader/MemoryBuffer.cpp
81 -#include "p8-platform/threads/mutex.h"
82 +#include "os-dependent.h"
83 #include "MemoryBuffer.h"
84 #include <kodi/General.h> //for kodi::Log
86 @@ -56,7 +56,7 @@ bool CMemoryBuffer::IsRunning()
88 void CMemoryBuffer::Clear()
90 - P8PLATFORM::CLockObject BufferLock(m_BufferLock);
91 + std::lock_guard<std::mutex> BufferLock(m_BufferLock);
92 std::vector<BufferItem *>::iterator it = m_Array.begin();
94 for (auto& item : m_Array)
95 @@ -104,14 +104,17 @@ size_t CMemoryBuffer::ReadFromBuffer(unsigned char *pbData, size_t lDataLength)
101 + std::unique_lock<std::mutex> lock(m_BufferLock);
102 + m_condition.wait_for(lock, std::chrono::milliseconds(5000));
108 // kodi::Log(ADDON_LOG_DEBUG, "get..%d/%d", lDataLength, m_BytesInBuffer);
109 size_t bytesWritten = 0;
110 - P8PLATFORM::CLockObject BufferLock(m_BufferLock);
111 + std::lock_guard<std::mutex> BufferLock(m_BufferLock);
113 while (bytesWritten < lDataLength)
115 @@ -172,7 +175,7 @@ long CMemoryBuffer::PutBuffer(unsigned char *pbData, size_t lDataLength)
116 memcpy(item->data, pbData, lDataLength);
119 - P8PLATFORM::CLockObject BufferLock(m_BufferLock);
120 + std::lock_guard<std::mutex> BufferLock(m_BufferLock);
121 m_Array.push_back(item);
122 m_BytesInBuffer += lDataLength;
124 @@ -192,7 +195,7 @@ long CMemoryBuffer::PutBuffer(unsigned char *pbData, size_t lDataLength)
126 if (m_BytesInBuffer > 0)
128 - m_event.Broadcast();
129 + m_condition.notify_one();
133 diff --git a/src/lib/tsreader/MemoryBuffer.h b/src/lib/tsreader/MemoryBuffer.h
134 index 080553b..4f8708f 100644
135 --- a/src/lib/tsreader/MemoryBuffer.h
136 +++ b/src/lib/tsreader/MemoryBuffer.h
141 -#include "p8-platform/threads/mutex.h"
142 +#include <condition_variable>
147 @@ -55,9 +56,9 @@ class CMemoryBuffer
150 std::vector<BufferItem *> m_Array;
151 - P8PLATFORM::CMutex m_BufferLock;
152 + std::mutex m_BufferLock;
153 size_t m_BytesInBuffer;
154 - P8PLATFORM::CEvent m_event;
155 + std::condition_variable m_condition;
159 diff --git a/src/lib/tsreader/MemoryReader.h b/src/lib/tsreader/MemoryReader.h
160 index fef4f98..288984b 100644
161 --- a/src/lib/tsreader/MemoryReader.h
162 +++ b/src/lib/tsreader/MemoryReader.h
165 #include "FileReader.h"
166 #include "MemoryBuffer.h"
167 +#include "os-dependent.h"
171 diff --git a/src/lib/tsreader/MemorySink.cpp b/src/lib/tsreader/MemorySink.cpp
172 index dafef56..af8b74c 100644
173 --- a/src/lib/tsreader/MemorySink.cpp
174 +++ b/src/lib/tsreader/MemorySink.cpp
175 @@ -84,7 +84,7 @@ void CMemorySink::addData(unsigned char* data, size_t dataSize, struct timeval U
179 - P8PLATFORM::CLockObject BufferLock(m_BufferLock);
180 + std::lock_guard<std::mutex> BufferLock(m_BufferLock);
183 m_buffer.PutBuffer(data, dataSize);
184 diff --git a/src/lib/tsreader/MemorySink.h b/src/lib/tsreader/MemorySink.h
185 index cc0f3c8..22d91c6 100644
186 --- a/src/lib/tsreader/MemorySink.h
187 +++ b/src/lib/tsreader/MemorySink.h
191 #include "MemoryBuffer.h"
192 -#include "p8-platform/threads/mutex.h"
195 class CMemorySink: public MediaSink
197 @@ -57,7 +57,7 @@ class CMemorySink: public MediaSink
198 private: // redefined virtual functions:
199 virtual Boolean continuePlaying();
201 - P8PLATFORM::CMutex m_BufferLock;
202 + std::mutex m_BufferLock;
203 unsigned char* m_pSubmitBuffer;
204 int m_iSubmitBufferPos;
206 diff --git a/src/lib/tsreader/MepoRTSPClient.cpp b/src/lib/tsreader/MepoRTSPClient.cpp
207 index ccd6761..688ae84 100644
208 --- a/src/lib/tsreader/MepoRTSPClient.cpp
209 +++ b/src/lib/tsreader/MepoRTSPClient.cpp
210 @@ -54,7 +54,7 @@ CRTSPClient::CRTSPClient()
214 - m_bRunning = false;
218 CRTSPClient::~CRTSPClient()
219 @@ -496,7 +496,9 @@ void CRTSPClient::StartBufferThread()
221 if (!m_BufferThreadActive)
225 + m_thread = std::thread([&] { Process(); });
227 m_BufferThreadActive = true;
229 kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::StartBufferThread done");
230 @@ -505,11 +507,12 @@ void CRTSPClient::StartBufferThread()
231 void CRTSPClient::StopBufferThread()
233 kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::StopBufferThread");
234 - m_bRunning = false;
236 if (!m_BufferThreadActive)
240 + if (m_thread.joinable())
243 m_BufferThreadActive = false;
244 kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::StopBufferThread done");
245 @@ -539,25 +542,22 @@ void CRTSPClient::FillBuffer(unsigned long byteCount)
246 kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::Fillbuffer...%d/%d\n", byteCount, m_buffer->Size() );
249 -void *CRTSPClient::Process()
250 +void CRTSPClient::Process()
252 m_BufferThreadActive = true;
255 kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient:: thread started");
257 - while (m_env != NULL && !IsStopped())
258 + while (m_env != NULL && m_running)
260 m_env->taskScheduler().doEventLoop();
261 - if (m_bRunning == false)
262 + if (m_running == false)
266 kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient:: thread stopped");
268 m_BufferThreadActive = false;
273 void CRTSPClient::Continue()
274 @@ -582,8 +582,12 @@ bool CRTSPClient::Pause()
275 if (m_ourClient != NULL && m_session != NULL)
277 kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::Pause() stopthread");
278 - StopThread(10000); // Ambass : sometimes 100mS ( prev value ) is not enough and thread is not stopped.
279 - // now stopping takes around 5 secs ?!?! why ????
280 + // Ambass : sometimes 100mS ( prev value ) is not enough and thread is not stopped.
281 + // now stopping takes around 5 secs ?!?! why ????
283 + if (m_thread.joinable())
286 kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::Pause() thread stopped");
287 RTSPClient* rtspClient=(RTSPClient*)m_ourClient;
288 rtspClient->pauseMediaSession(*m_session);
289 diff --git a/src/lib/tsreader/MepoRTSPClient.h b/src/lib/tsreader/MepoRTSPClient.h
290 index bd6e578..9bb0421 100644
291 --- a/src/lib/tsreader/MepoRTSPClient.h
292 +++ b/src/lib/tsreader/MepoRTSPClient.h
297 -#include "p8-platform/threads/threads.h"
300 #include "lib/tsreader/MemoryBuffer.h"
302 #include "liveMedia.hh"
305 #define RTSP_URL_BUFFERSIZE 2048
307 -class CRTSPClient: public P8PLATFORM::CThread
312 @@ -101,7 +102,7 @@ class CRTSPClient: public P8PLATFORM::CThread
316 - virtual void *Process(void);
318 void StartBufferThread();
319 void StopBufferThread();
320 bool m_BufferThreadActive;
321 @@ -113,5 +114,8 @@ class CRTSPClient: public P8PLATFORM::CThread
324 char m_outFileName[1000];
326 + std::atomic<bool> m_running = {false};
327 + std::thread m_thread;
330 diff --git a/src/lib/tsreader/MultiFileReader.cpp b/src/lib/tsreader/MultiFileReader.cpp
331 index 21fd7b2..5106418 100644
332 --- a/src/lib/tsreader/MultiFileReader.cpp
333 +++ b/src/lib/tsreader/MultiFileReader.cpp
335 #include "MultiFileReader.h"
336 #include <kodi/General.h> //for kodi::Log
337 #include <kodi/Filesystem.h>
338 +#include <kodi/tools/EndTime.h>
343 -#include "p8-platform/threads/threads.h"
344 #include <inttypes.h>
345 +#include "os-dependent.h"
349 -using namespace P8PLATFORM;
351 //Maximum time in msec to wait for the buffer file to become available - Needed for DVB radio (this sometimes takes some time)
352 #define MAX_BUFFER_TIMEOUT 1500
354 @@ -121,12 +120,12 @@ namespace MPTV
355 if (RefreshTSBufferFile() == S_FALSE)
357 // For radio the buffer sometimes needs some time to become available, so wait and try it more than once
358 - P8PLATFORM::CTimeout timeout(MAX_BUFFER_TIMEOUT);
359 + kodi::tools::CEndTime timeout(MAX_BUFFER_TIMEOUT);
363 std::this_thread::sleep_for(std::chrono::milliseconds(100));
364 - if (timeout.TimeLeft() == 0)
365 + if (timeout.MillisLeft() == 0)
367 kodi::Log(ADDON_LOG_ERROR, "MultiFileReader: timed out while waiting for buffer file to become available");
368 kodi::QueueNotification(QUEUE_ERROR, "", "Time out while waiting for buffer file");
369 diff --git a/src/os-dependent.h b/src/os-dependent.h
370 index cdc6980..28c162c 100644
371 --- a/src/os-dependent.h
372 +++ b/src/os-dependent.h
375 #if (defined(_WIN32) || defined(_WIN64))
379 +/* Handling of 2-byte Windows wchar strings */
380 +#define WcsLen wcslen
381 +#define WcsToMbs wcstombs
382 +typedef wchar_t Wchar_t; /* sizeof(wchar_t) = 2 bytes on Windows */
384 #ifndef _SSIZE_T_DEFINED
386 typedef __int64 ssize_t;
387 @@ -20,20 +27,108 @@ typedef _W64 int ssize_t;
388 #define _SSIZE_T_DEFINED
391 +/* Prevent deprecation warnings */
392 +#define strnicmp _strnicmp
394 +#define PATH_SEPARATOR_CHAR '\\'
398 #if (defined(TARGET_LINUX) || defined(TARGET_DARWIN))
399 #include <sys/types.h>
403 +#define strnicmp(X,Y,N) strncasecmp(X,Y,N)
405 inline unsigned long long GetTickCount64(void)
407 auto now = std::chrono::steady_clock::now();
408 return std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
411 +#define PATH_SEPARATOR_CHAR '/'
413 +#if defined(__APPLE__)
415 +#include <CoreFoundation/CFPlugInCOM.h>
418 +/* Handling of 2-byte Windows wchar strings on non-Windows targets
419 + * Used by The MediaPortal and ForTheRecord pvr addons
421 +typedef uint16_t Wchar_t; /* sizeof(wchar_t) = 4 bytes on Linux, but the MediaPortal buffer files have 2-byte wchars */
423 +/* This is a replacement of the Windows wcslen() function which assumes that
424 + * wchar_t is a 2-byte character.
425 + * It is used for processing Windows wchar strings
427 +inline size_t WcsLen(const Wchar_t *str)
429 + const unsigned short *eos = (const unsigned short*)str;
431 + return( (size_t)(eos - (const unsigned short*)str) -1);
434 +/* This is a replacement of the Windows wcstombs() function which assumes that
435 + * wchar_t is a 2-byte character.
436 + * It is used for processing Windows wchar strings
438 +inline size_t WcsToMbs(char *s, const Wchar_t *w, size_t n)
441 + const unsigned short *wc = (const unsigned short*) w;
442 + while(wc[i] && (i < n))
447 + if (i < n) s[i] = '\0';
452 #endif /* TARGET_LINUX || TARGET_DARWIN */
457 +#if !defined(__APPLE__)
458 +typedef LONG HRESULT;
462 +#define FAILED(Status) ((HRESULT)(Status)<0)
466 +#define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
469 +#define _FILE_OFFSET_BITS 64
470 +#define FILE_BEGIN 0
471 +#define FILE_CURRENT 1
483 +#define ERROR_FILENAME_EXCED_RANGE 206L
484 +#define ERROR_INVALID_NAME 123L
486 +#ifndef E_OUTOFMEMORY
487 +#define E_OUTOFMEMORY 0x8007000EL
491 +#define E_FAIL 0x8004005EL
494 // Additional typedefs
495 typedef uint8_t byte;
496 diff --git a/src/pvrclient-mediaportal.cpp b/src/pvrclient-mediaportal.cpp
497 index 851b940..c1052e3 100644
498 --- a/src/pvrclient-mediaportal.cpp
499 +++ b/src/pvrclient-mediaportal.cpp
501 #include <kodi/General.h>
502 #include <kodi/Filesystem.h>
506 using namespace kodi::tools;
508 using namespace MPTV;
509 @@ -70,7 +68,6 @@ cPVRClientMediaPortal::cPVRClientMediaPortal(KODI_HANDLE instance, const std::st
513 - m_iLastRecordingUpdate = 0;
514 m_signalStateCounter = 0;
517 @@ -99,7 +96,7 @@ string cPVRClientMediaPortal::SendCommand(const char* command)
519 string cPVRClientMediaPortal::SendCommand(const string& command)
521 - P8PLATFORM::CLockObject critsec(m_mutex);
522 + std::lock_guard<std::mutex> critsec(m_mutex);
524 if ( !m_tcpclient->send(command) )
526 @@ -174,10 +171,10 @@ ADDON_STATUS cPVRClientMediaPortal::TryConnect()
527 case PVR_CONNECTION_STATE_SERVER_UNREACHABLE:
528 kodi::Log(ADDON_LOG_ERROR, "Could not connect to MediaPortal TV Server backend.");
529 // Start background thread for connecting to the backend
533 - kodi::Log(ADDON_LOG_INFO, "Waiting for a connection in the background.");
536 + m_thread = std::thread([&] { Process(); });
538 return ADDON_STATUS_LOST_CONNECTION;
539 case PVR_CONNECTION_STATE_CONNECTING:
540 @@ -190,7 +187,7 @@ ADDON_STATUS cPVRClientMediaPortal::TryConnect()
542 PVR_CONNECTION_STATE cPVRClientMediaPortal::Connect(bool updateConnectionState)
544 - P8PLATFORM::CLockObject critsec(m_connectionMutex);
545 + std::lock_guard<std::mutex> critsec(m_connectionMutex);
549 @@ -317,9 +314,11 @@ void cPVRClientMediaPortal::Disconnect()
551 kodi::Log(ADDON_LOG_INFO, "Disconnect");
558 + if (m_thread.joinable())
562 if (m_tcpclient->is_valid() && m_bTimeShiftStarted)
563 @@ -361,14 +360,14 @@ bool cPVRClientMediaPortal::IsUp()
567 -void* cPVRClientMediaPortal::Process(void)
568 +void cPVRClientMediaPortal::Process()
570 kodi::Log(ADDON_LOG_DEBUG, "Background thread started.");
572 bool keepWaiting = true;
573 PVR_CONNECTION_STATE state;
575 - while (!IsStopped() && keepWaiting)
576 + while (m_running && keepWaiting)
578 state = Connect(false);
580 @@ -396,8 +395,6 @@ void* cPVRClientMediaPortal::Process(void)
581 SetConnectionState(state);
583 kodi::Log(ADDON_LOG_DEBUG, "Background thread finished.");
589 @@ -1188,7 +1185,7 @@ PVR_ERROR cPVRClientMediaPortal::GetRecordings(bool deleted, kodi::addon::PVRRec
593 - m_iLastRecordingUpdate = P8PLATFORM::GetTimeMs();
594 + m_iLastRecordingUpdate = std::chrono::system_clock::now();
596 return PVR_ERROR_NO_ERROR;
598 @@ -1383,7 +1380,7 @@ PVR_ERROR cPVRClientMediaPortal::GetTimers(kodi::addon::PVRTimersResultSet& resu
602 - if ( P8PLATFORM::GetTimeMs() > m_iLastRecordingUpdate + 15000)
603 + if ( std::chrono::system_clock::now() > m_iLastRecordingUpdate + std::chrono::milliseconds(15000))
605 kodi::addon::CInstancePVRClient::TriggerRecordingUpdate();
607 diff --git a/src/pvrclient-mediaportal.h b/src/pvrclient-mediaportal.h
608 index 3087634..e5da832 100644
609 --- a/src/pvrclient-mediaportal.h
610 +++ b/src/pvrclient-mediaportal.h
621 /* Master defines for client control */
625 #include "channels.h"
626 -#include "p8-platform/threads/mutex.h"
627 -#include "p8-platform/threads/threads.h"
629 /* Use a forward declaration here. Including RTSPClient.h via TSReader.h at this point gives compile errors */
631 @@ -28,9 +30,7 @@ namespace MPTV
634 class ATTRIBUTE_HIDDEN cPVRClientMediaPortal
635 - : public kodi::addon::CInstancePVRClient,
636 - public P8PLATFORM::PreventCopy,
637 - public P8PLATFORM::CThread
638 + : public kodi::addon::CInstancePVRClient
641 /* Class interface */
642 @@ -110,7 +110,7 @@ class ATTRIBUTE_HIDDEN cPVRClientMediaPortal
645 /* TVServerKodi Listening Thread */
646 - void* Process(void);
648 PVR_CONNECTION_STATE Connect(bool updateConnectionState = true);
650 void LoadGenreTable(void);
651 @@ -134,9 +134,9 @@ class ATTRIBUTE_HIDDEN cPVRClientMediaPortal
652 time_t m_BackendTime;
654 CGenreTable* m_genretable;
655 - P8PLATFORM::CMutex m_mutex;
656 - P8PLATFORM::CMutex m_connectionMutex;
657 - int64_t m_iLastRecordingUpdate;
658 + std::mutex m_mutex;
659 + std::mutex m_connectionMutex;
660 + std::chrono::system_clock::time_point m_iLastRecordingUpdate;
661 MPTV::CTsReader* m_tsreader;
662 std::map<int,cChannel> m_channels;
663 int m_signalStateCounter;
664 @@ -145,6 +145,9 @@ class ATTRIBUTE_HIDDEN cPVRClientMediaPortal
666 cRecording* m_lastSelectedRecording;
668 + std::atomic<bool> m_running = {false};
669 + std::thread m_thread;
671 //Used for TV Server communication:
672 std::string SendCommand(const char* command);
673 std::string SendCommand(const std::string& command);