rtsp: use std::mutex instead of pthread

This commit is contained in:
Kamil Trzcinski 2023-03-03 19:59:32 +01:00
parent 43f2817442
commit 305481b458

View File

@ -15,6 +15,15 @@ extern "C" {
#ifdef USE_RTSP #ifdef USE_RTSP
#include <string>
#include <memory>
#include <optional>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <chrono>
#include <set>
#include <BasicUsageEnvironment.hh> #include <BasicUsageEnvironment.hh>
#include <RTSPServerSupportingHTTPStreaming.hh> #include <RTSPServerSupportingHTTPStreaming.hh>
#include <OnDemandServerMediaSubsession.hh> #include <OnDemandServerMediaSubsession.hh>
@ -22,8 +31,8 @@ extern "C" {
#include <H264VideoRTPSink.hh> #include <H264VideoRTPSink.hh>
static pthread_t rtsp_thread; static pthread_t rtsp_thread;
static pthread_mutex_t rtsp_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; static std::set<class DynamicH264Stream *> rtsp_streams;
static class DynamicH264Stream *rtsp_streams; static std::recursive_mutex rtsp_streams_lock;
static rtsp_options_t *rtsp_options; static rtsp_options_t *rtsp_options;
static const char *stream_name = "stream.h264"; static const char *stream_name = "stream.h264";
@ -38,32 +47,18 @@ public:
void doGetNextFrame() void doGetNextFrame()
{ {
pthread_mutex_lock(&rtsp_lock); std::unique_lock lk(rtsp_streams_lock);
if (!fHaveStartedReading) { if (!fHaveStartedReading) {
pNextStream = rtsp_streams; rtsp_streams.insert(this);
rtsp_streams = this;
fHaveStartedReading = True; fHaveStartedReading = True;
} }
pthread_mutex_unlock(&rtsp_lock);
} }
void doStopGettingFrames() void doStopGettingFrames()
{ {
pthread_mutex_lock(&rtsp_lock); std::unique_lock lk(rtsp_streams_lock);
if (fHaveStartedReading) { rtsp_streams.erase(this);
DynamicH264Stream **streamp = &rtsp_streams; fHaveStartedReading = false;
while (*streamp) {
if (*streamp == this) {
*streamp = pNextStream;
pNextStream = NULL;
break;
}
streamp = &(*streamp)->pNextStream;
}
fHaveStartedReading = False;
}
fFrameWaiting = False;
pthread_mutex_unlock(&rtsp_lock);
} }
void receiveData(buffer_t *buf) void receiveData(buffer_t *buf)
@ -200,16 +195,13 @@ protected: // redefined virtual functions
static void rtsp_frame_finish() static void rtsp_frame_finish()
{ {
pthread_mutex_lock(&rtsp_lock); std::unique_lock lk(rtsp_streams_lock);
int clients = 0; for (auto *stream : rtsp_streams) {
for (DynamicH264Stream *stream = rtsp_streams; stream; stream = stream->pNextStream) {
stream->doFinishFrameGet(); stream->doFinishFrameGet();
clients++;
} }
if (rtsp_options) { if (rtsp_options) {
rtsp_options->clients = clients; rtsp_options->clients = rtsp_streams.size();
} }
pthread_mutex_unlock(&rtsp_lock);
} }
static void *rtsp_server_thread(void *opaque) static void *rtsp_server_thread(void *opaque)
@ -226,23 +218,16 @@ static void *rtsp_server_thread(void *opaque)
static bool rtsp_h264_needs_buffer(buffer_lock_t *buf_lock) static bool rtsp_h264_needs_buffer(buffer_lock_t *buf_lock)
{ {
bool needsBuffer = false; std::unique_lock lk(rtsp_streams_lock);
return rtsp_streams.size() > 0;
pthread_mutex_lock(&rtsp_lock);
for (DynamicH264Stream *stream = rtsp_streams; stream; stream = stream->pNextStream) {
needsBuffer = true;
}
pthread_mutex_unlock(&rtsp_lock);
return needsBuffer;
} }
static void rtsp_h264_capture(buffer_lock_t *buf_lock, buffer_t *buf) static void rtsp_h264_capture(buffer_lock_t *buf_lock, buffer_t *buf)
{ {
pthread_mutex_lock(&rtsp_lock); std::unique_lock lk(rtsp_streams_lock);
for (DynamicH264Stream *stream = rtsp_streams; stream; stream = stream->pNextStream) { for (auto *stream : rtsp_streams) {
stream->receiveData(buf); stream->receiveData(buf);
} }
pthread_mutex_unlock(&rtsp_lock);
} }
extern "C" int rtsp_server(rtsp_options_t *options) extern "C" int rtsp_server(rtsp_options_t *options)