Skip to content

Commit

Permalink
Merge pull request #71 from phunkyfish/stringutils-threads
Browse files Browse the repository at this point in the history
Stringutils threads
  • Loading branch information
phunkyfish authored Oct 5, 2020
2 parents 7a4a381 + 6608b52 commit 6c8804d
Show file tree
Hide file tree
Showing 57 changed files with 62 additions and 5,381 deletions.
32 changes: 1 addition & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,14 @@ set(CATCHUP_SOURCES src/StreamManager.cpp
src/stream/FFmpegStream.cpp
src/stream/CurlCatchupInput.cpp
src/stream/CurlInput.cpp
src/stream/commons/Exception.cpp
src/stream/threads/Atomics.cpp
src/stream/threads/Event.cpp
src/stream/threads/Thread.cpp
src/stream/threads/Timer.cpp
src/stream/threads/TimeUtils.cpp
src/stream/threads/SystemClock.cpp
src/stream/TimeshiftBuffer.cpp
src/stream/TimeshiftSegment.cpp
src/stream/TimeshiftStream.cpp
src/stream/url/URL.cpp
src/stream/url/UrlOptions.cpp
src/stream/url/Variant.cpp
src/utils/DiskUtils.cpp
src/utils/FilenameUtils.cpp
src/utils/StringUtils.cpp)
src/utils/FilenameUtils.cpp)

set(CATCHUP_HEADERS src/StreamManager.h
src/stream/BaseStream.h
Expand All @@ -47,20 +39,6 @@ set(CATCHUP_HEADERS src/StreamManager.h
src/stream/CurlCatchupInput.h
src/stream/CurlInput.h
src/stream/IManageDemuxPacket.h
src/stream/commons/Exception.h
src/stream/threads/Atomics.h
src/stream/threads/Condition.h
src/stream/threads/CriticalSection.h
src/stream/threads/Event.h
src/stream/threads/Helpers.h
src/stream/threads/Lockables.h
src/stream/threads/SharedSection.h
src/stream/threads/SingleLock.h
src/stream/threads/SystemClock.h
src/stream/threads/Thread.h
src/stream/threads/Timer.h
src/stream/threads/TimeUtils.h
src/stream/threads/platform/ThreadImpl.h
src/stream/TimeshiftBuffer.h
src/stream/TimeshiftSegment.h
src/stream/TimeshiftStream.h
Expand All @@ -69,18 +47,10 @@ set(CATCHUP_HEADERS src/StreamManager.h
src/utils/FilenameUtils.h
src/utils/Log.h
src/utils/Properties.h
src/utils/StringUtils.h
src/stream/url/URL.h
src/stream/url/UrlOptions.h
src/stream/url/Variant.h)

if(NOT WIN32)
list(APPEND CATCHUP_SOURCES src/stream/platform/posix/XTimeUtils.cpp)

list(APPEND CATCHUP_HEADERS src/stream/platform/posix/PlatformDefs.h
src/stream/platform/posix/XTimeUtils.h)
endif()

include_directories(${INCLUDES}
${FFMPEG_INCLUDE_DIRS}
${ZLIB_INCLUDE_DIRS}
Expand Down
8 changes: 7 additions & 1 deletion inputstream.ffmpegdirect/addon.xml.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<addon
id="inputstream.ffmpegdirect"
version="1.15.4"
version="1.15.5"
name="Inputstream FFmpeg Direct"
provider-name="Ross Nicholson">
<requires>@ADDON_DEPENDS@</requires>
Expand All @@ -24,6 +24,12 @@
<fanart>fanart.jpg</fanart>
</assets>
<news>
v1.15.5
- Update: Use add-on supplied StringUtils and remove local version
- Update: Remove local threading code and use addon supplied instead
- Update: Use new addon supplied CEndTime class based on chrono
- Update: Add autoreconf step to lib zvbi

v1.15.4
- Update: Remove p8-platforms and add local StringUtils for cpp17
- Update: Nettle patch for xcode 12
Expand Down
6 changes: 6 additions & 0 deletions inputstream.ffmpegdirect/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v1.15.5
- Update: Use add-on supplied StringUtils and remove local version
- Update: Remove local threading code and use addon supplied instead
- Update: Use new addon supplied CEndTime class based on chrono
- Update: Add autoreconf step to lib zvbi

v1.15.4
- Update: Remove p8-platforms and add local StringUtils for cpp17
- Update: Nettle patch for xcode 12
Expand Down
4 changes: 3 additions & 1 deletion src/StreamManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
#include "stream/TimeshiftStream.h"
#include "utils/HttpProxy.h"
#include "utils/Log.h"
#include "utils/StringUtils.h"

#include <kodi/tools/StringUtils.h>

using namespace ffmpegdirect;
using namespace kodi::tools;

/***********************************************************
* InputSteam Client AddOn specific public library functions
Expand Down
8 changes: 3 additions & 5 deletions src/stream/DemuxStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@
#include "DemuxStream.h"

#include "../utils/Log.h"
#include "../utils/StringUtils.h"
#include "url/URL.h"

#ifdef TARGET_POSIX
#include "platform/posix/XTimeUtils.h"
#endif

#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
Expand All @@ -31,7 +26,10 @@ extern "C" {
#include <libavutil/opt.h>
}

#include <kodi/tools/StringUtils.h>

using namespace ffmpegdirect;
using namespace kodi::tools;

/***********************************************************
* InputSteam Client AddOn specific public library functions
Expand Down
14 changes: 5 additions & 9 deletions src/stream/FFmpegCatchupStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@
#include "FFmpegCatchupStream.h"

#include "CurlCatchupInput.h"
#include "threads/SingleLock.h"
#include "url/URL.h"
#include "../utils/Log.h"
#include "../utils/StringUtils.h"

#ifdef TARGET_POSIX
#include "platform/posix/XTimeUtils.h"
#endif

#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
Expand All @@ -37,9 +31,11 @@ extern "C" {

//#include "platform/posix/XTimeUtils.h"

#include <kodi/tools/StringUtils.h>
#include <kodi/Filesystem.h>

using namespace ffmpegdirect;
using namespace kodi::tools;

/***********************************************************
* InputSteam Client AddOn specific public library functions
Expand Down Expand Up @@ -92,7 +88,7 @@ bool FFmpegCatchupStream::DemuxSeekTime(double timeMs, bool backwards, double& s
if (seekResult >= 0)
{
{
CSingleLock lock(m_critSection);
std::lock_guard<std::mutex> lock(m_mutex);
m_seekOffset = seekResult;
}

Expand All @@ -118,7 +114,7 @@ DemuxPacket* FFmpegCatchupStream::DemuxRead()
DemuxPacket* pPacket = FFmpegStream::DemuxRead();
if (pPacket)
{
CSingleLock lock(m_critSection);
std::lock_guard<std::mutex> lock(m_mutex);
pPacket->pts += m_seekOffset;
pPacket->dts += m_seekOffset;

Expand Down Expand Up @@ -177,7 +173,7 @@ void FFmpegCatchupStream::DemuxSetSpeed(int speed)
else if (!IsPaused() && speed == DVD_PLAYSPEED_PAUSE)
{
// Pause Playback
CSingleLock lock(m_critSection);
std::lock_guard<std::mutex> lock(m_mutex);
m_pauseStartTime = m_currentDemuxTime;
Log(LOGLEVEL_DEBUG, "%s - DemuxSetSpeed - Pause time: %lld", __FUNCTION__, static_cast<long long>(m_pauseStartTime));
}
Expand Down
22 changes: 12 additions & 10 deletions src/stream/FFmpegLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@

#include "FFmpegLog.h"

#include "threads/CriticalSection.h"
#include "threads/Thread.h"
#include "../utils/Log.h"
#include "../utils/StringUtils.h"

#include <map>
#include <mutex>
#include <thread>

#include <kodi/tools/StringUtils.h>
#include <kodi/General.h>

using namespace ffmpegdirect;
using namespace kodi::tools;

int FFmpegLog::level = AV_LOG_INFO;
bool FFmpegLog::enabled = false;
Expand All @@ -42,16 +43,16 @@ int FFmpegLog::GetLogLevel()
return FFmpegLog::level;
}

static CCriticalSection m_ffmpegdirectLogSection;
std::map<const CThread*, std::string> g_ffmpegdirectLogbuffer;
static std::mutex m_ffmpegdirectLogMutex;
std::map<const std::thread::id, std::string> g_ffmpegdirectLogbuffer;

void ff_flush_avutil_log_buffers(void)
{
CSingleLock lock(m_ffmpegdirectLogSection);
std::lock_guard<std::mutex> lock(m_ffmpegdirectLogMutex);
/* Loop through the logbuffer list and remove any blank buffers
If the thread using the buffer is still active, it will just
add a new buffer next time it writes to the log */
std::map<const CThread*, std::string>::iterator it;
std::map<const std::thread::id, std::string>::iterator it;
for (it = g_ffmpegdirectLogbuffer.begin(); it != g_ffmpegdirectLogbuffer.end(); )
if ((*it).second.empty())
g_ffmpegdirectLogbuffer.erase(it++);
Expand All @@ -61,8 +62,8 @@ void ff_flush_avutil_log_buffers(void)

void ff_avutil_log(void* ptr, int level, const char* format, va_list va)
{
CSingleLock lock(m_ffmpegdirectLogSection);
const CThread* threadId = CThread::GetCurrentThread();
std::lock_guard<std::mutex> lock(m_ffmpegdirectLogMutex);
const std::thread::id threadId = std::this_thread::get_id();
std::string &buffer = g_ffmpegdirectLogbuffer[threadId];

AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
Expand Down Expand Up @@ -92,7 +93,8 @@ void ff_avutil_log(void* ptr, int level, const char* format, va_list va)
}

std::string message = StringUtils::FormatV(format, va);
std::string prefix = StringUtils::Format("ffmpeg[%pX]: ", static_cast<const void*>(threadId));
//std::string prefix = StringUtils::Format("ffmpeg[%pX]: ", static_cast<const void*>(threadId));
std::string prefix = StringUtils::Format("ffmpeg[%X]: ", std::hash<std::thread::id>{}(std::this_thread::get_id()));
if (avc)
{
if (avc->item_name)
Expand Down
12 changes: 6 additions & 6 deletions src/stream/FFmpegStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@

#include "FFmpegStream.h"

#include "threads/SingleLock.h"
#include "url/URL.h"
#include "FFmpegLog.h"
#include "../utils/FilenameUtils.h"
#include "../utils/Log.h"
#include "../utils/StringUtils.h"

#include "IManageDemuxPacket.h"


#include <chrono>
#include <ctime>
#include <thread>

#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
Expand All @@ -36,10 +34,12 @@ extern "C" {
#include <libavutil/opt.h>
}

#include <kodi/tools/StringUtils.h>
#include <kodi/Filesystem.h>
#include <kodi/Network.h>

using namespace ffmpegdirect;
using namespace kodi::tools;

/***********************************************************
* InputSteam Client AddOn specific public library functions
Expand Down Expand Up @@ -282,7 +282,7 @@ DemuxPacket* FFmpegStream::DemuxRead()
// on some cases where the received packet is invalid we will need to return an empty packet (0 length) otherwise the main loop (in CVideoPlayer)
// would consider this the end of stream and stop.
bool bReturnEmpty = false;
{ CSingleLock lock(m_critSection); // open lock scope
{ std::lock_guard<std::mutex> lock(m_mutex); // open lock scope
if (m_pFormatContext)
{
// assume we are not eof
Expand Down Expand Up @@ -1429,7 +1429,7 @@ bool FFmpegStream::SeekTime(double time, bool backwards, double* startpts)

if (m_checkTransportStream)
{
FFmpegDirectThreads::EndTime timer(1000);
kodi::tools::CEndTime timer(1000);

while (!IsTransportStreamReady())
{
Expand Down Expand Up @@ -1457,7 +1457,7 @@ bool FFmpegStream::SeekTime(double time, bool backwards, double* startpts)

int ret;
{
CSingleLock lock(m_critSection);
std::lock_guard<std::mutex> lock(m_mutex);
ret = av_seek_frame(m_pFormatContext, m_seekStream, seek_pts, backwards ? AVSEEK_FLAG_BACKWARD : 0);

if (ret < 0)
Expand Down
9 changes: 4 additions & 5 deletions src/stream/FFmpegStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

#pragma once

#include "threads/CriticalSection.h"
#include "threads/SystemClock.h"

#include "../utils/HttpProxy.h"
#include "../utils/Properties.h"
#include "BaseStream.h"
Expand All @@ -20,10 +17,12 @@
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <sstream>

#include <kodi/addon-instance/Inputstream.h>
#include <kodi/tools/EndTime.h>
#include <kodi/DemuxCrypto.h>

#ifndef __GNUC__
Expand Down Expand Up @@ -111,7 +110,7 @@ class FFmpegStream
virtual bool CheckReturnEmptyOnPacketResult(int result);

int64_t m_demuxerId;
CCriticalSection m_critSection;
mutable std::mutex m_mutex;
double m_currentPts; // used for stream length estimation
bool m_demuxResetOpenSuccess = false;
std::string m_streamUrl;
Expand Down Expand Up @@ -171,7 +170,7 @@ class FFmpegStream
unsigned int m_initialProgramNumber;
int m_seekStream;

FFmpegDirectThreads::EndTime m_timeout;
kodi::tools::CEndTime m_timeout;

// Due to limitations of ffmpeg, we only can detect a program change
// with a packet. This struct saves the packet for the next read and
Expand Down
3 changes: 2 additions & 1 deletion src/stream/TimeshiftBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
#include "url/URL.h"
#include "../utils/DiskUtils.h"
#include "../utils/Log.h"
#include "../utils/StringUtils.h"

#include <kodi/tools/StringUtils.h>
#include <kodi/Filesystem.h>

using namespace ffmpegdirect;
using namespace kodi::tools;

TimeshiftBuffer::TimeshiftBuffer(IManageDemuxPacket* demuxPacketManager)
: m_demuxPacketManager(demuxPacketManager)
Expand Down
3 changes: 2 additions & 1 deletion src/stream/TimeshiftSegment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
#include "url/URL.h"
#include "../utils/DiskUtils.h"
#include "../utils/Log.h"
#include "../utils/StringUtils.h"

extern "C"
{
#include <libavcodec/avcodec.h>
}

#include <kodi/tools/StringUtils.h>
#include <kodi/DemuxCrypto.h>

using namespace ffmpegdirect;
using namespace kodi::tools;

TimeshiftSegment::TimeshiftSegment(IManageDemuxPacket* demuxPacketManager, const std::string& streamId, int segmentId, const std::string& timeshiftBufferPath)
: m_demuxPacketManager(demuxPacketManager), m_streamId(streamId), m_segmentId(segmentId)
Expand Down
1 change: 0 additions & 1 deletion src/stream/TimeshiftStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "TimeshiftStream.h"

#include "threads/SingleLock.h"
#include "../utils/Log.h"

#ifndef __STDC_CONSTANT_MACROS
Expand Down
Loading

0 comments on commit 6c8804d

Please sign in to comment.