diff --git a/src/Utils.cpp b/src/Utils.cpp index 5bd2823d..ae8a2aa0 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -22,6 +22,8 @@ with this program. If not, see #include #include +#include + #include "obs-websocket.h" #include "Utils.h" @@ -764,3 +766,51 @@ obs_data_array_t* Utils::GetSourceFiltersList(obs_source_t* source, bool include return enumParams.filters; } + +void getPauseRecordingFunctions(RecordingPausedFunction* recPausedFuncPtr, PauseRecordingFunction* pauseRecFuncPtr) +{ + void* frontendApi = os_dlopen("obs-frontend-api"); + + if (recPausedFuncPtr) { + *recPausedFuncPtr = (RecordingPausedFunction)os_dlsym(frontendApi, "obs_frontend_recording_paused"); + } + + if (pauseRecFuncPtr) { + *pauseRecFuncPtr = (PauseRecordingFunction)os_dlsym(frontendApi, "obs_frontend_recording_pause"); + } + + os_dlclose(frontendApi); +} + +bool Utils::RecordingPauseSupported() +{ + RecordingPausedFunction recordingPaused = nullptr; + PauseRecordingFunction pauseRecording = nullptr; + getPauseRecordingFunctions(&recordingPaused, &pauseRecording); + + return (recordingPaused && pauseRecording); +} + +bool Utils::RecordingPaused() +{ + RecordingPausedFunction recordingPaused = nullptr; + getPauseRecordingFunctions(&recordingPaused, nullptr); + + if (recordingPaused == nullptr) { + return false; + } + + return recordingPaused(); +} + +void Utils::PauseRecording(bool pause) +{ + PauseRecordingFunction pauseRecording = nullptr; + getPauseRecordingFunctions(nullptr, &pauseRecording); + + if (pauseRecording == nullptr) { + return; + } + + pauseRecording(pause); +} diff --git a/src/Utils.h b/src/Utils.h index 6a446a7c..6de57eec 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -31,6 +31,9 @@ with this program. If not, see #include #include +typedef void(*PauseRecordingFunction)(bool); +typedef bool(*RecordingPausedFunction)(); + class Utils { public: static obs_data_array_t* StringListToArray(char** strings, const char* key); @@ -78,4 +81,8 @@ class Utils { static const char* GetFilenameFormatting(); static bool SetFilenameFormatting(const char* filenameFormatting); + + static bool RecordingPauseSupported(); + static bool RecordingPaused(); + static void PauseRecording(bool pause); }; diff --git a/src/WSEvents.cpp b/src/WSEvents.cpp index b92c6837..2ab74fa7 100644 --- a/src/WSEvents.cpp +++ b/src/WSEvents.cpp @@ -381,7 +381,7 @@ uint64_t WSEvents::getRecordingTime() { return 0; } - if (obs_frontend_recording_paused() && _recPauseTime > 0) { + if (Utils::RecordingPaused() && _recPauseTime > 0) { return (_recPauseTime - _recStarttime); } @@ -746,7 +746,7 @@ void WSEvents::OnExit() { void WSEvents::StreamStatus() { bool streamingActive = obs_frontend_streaming_active(); bool recordingActive = obs_frontend_recording_active(); - bool recordingPaused = obs_frontend_recording_paused(); + bool recordingPaused = Utils::RecordingPaused(); bool replayBufferActive = obs_frontend_replay_buffer_active(); OBSOutputAutoRelease streamOutput = obs_frontend_get_streaming_output(); @@ -832,7 +832,7 @@ void WSEvents::Heartbeat() { bool streamingActive = obs_frontend_streaming_active(); bool recordingActive = obs_frontend_recording_active(); - bool recordingPaused = obs_frontend_recording_paused(); + bool recordingPaused = Utils::RecordingPaused(); OBSDataAutoRelease data = obs_data_create(); OBSOutputAutoRelease recordOutput = obs_frontend_get_recording_output(); diff --git a/src/WSRequestHandler_Recording.cpp b/src/WSRequestHandler_Recording.cpp index 17915ed0..9164a404 100644 --- a/src/WSRequestHandler_Recording.cpp +++ b/src/WSRequestHandler_Recording.cpp @@ -3,27 +3,17 @@ #include #include "Utils.h" -typedef void(*pauseRecordingFunction)(bool); -typedef bool(*recordingPausedFunction)(); - -HandlerResponse ifCanPause(WSRequestHandler* req, std::function callback) +HandlerResponse ifCanPause(WSRequestHandler* req, std::function callback) { - void* frontendApi = os_dlopen("obs-frontend-api"); - - bool (*recordingPaused)() = (bool(*)())os_dlsym(frontendApi, "obs_frontend_recording_paused"); - void (*pauseRecording)(bool) = (void(*)(bool))os_dlsym(frontendApi, "obs_frontend_recording_pause"); - - os_dlclose(frontendApi); - - if (!recordingPaused || !pauseRecording) { - return req->SendErrorResponse("recording pause not supported"); - } - if (!obs_frontend_recording_active()) { return req->SendErrorResponse("recording is not active"); } - return callback(recordingPaused, pauseRecording); + if (!Utils::RecordingPauseSupported()) { + return req->SendErrorResponse("recording pauses are not available in this version of OBS Studio"); + } + + return callback(); } /** @@ -85,12 +75,12 @@ HandlerResponse WSRequestHandler::HandleStartRecording(WSRequestHandler* req) { * @since 4.7.0 */ HandlerResponse WSRequestHandler::HandlePauseRecording(WSRequestHandler* req) { - return ifCanPause(req, [req](recordingPausedFunction recordingPaused, pauseRecordingFunction pauseRecording) { - if (recordingPaused()) { + return ifCanPause(req, [req]() { + if (Utils::RecordingPaused()) { return req->SendErrorResponse("recording already paused"); } - pauseRecording(true); + Utils::PauseRecording(true); return req->SendOKResponse(); }); } @@ -105,12 +95,12 @@ HandlerResponse WSRequestHandler::HandlePauseRecording(WSRequestHandler* req) { * @since 4.7.0 */ HandlerResponse WSRequestHandler::HandleResumeRecording(WSRequestHandler* req) { - return ifCanPause(req, [req](recordingPausedFunction recordingPaused, pauseRecordingFunction pauseRecording) { - if (!recordingPaused()) { + return ifCanPause(req, [req]() { + if (!Utils::RecordingPaused()) { return req->SendErrorResponse("recording is not paused"); } - pauseRecording(false); + Utils::PauseRecording(false); return req->SendOKResponse(); }); } diff --git a/src/WSRequestHandler_Streaming.cpp b/src/WSRequestHandler_Streaming.cpp index fc854062..0daea177 100644 --- a/src/WSRequestHandler_Streaming.cpp +++ b/src/WSRequestHandler_Streaming.cpp @@ -26,7 +26,7 @@ HandlerResponse WSRequestHandler::HandleGetStreamingStatus(WSRequestHandler* req OBSDataAutoRelease data = obs_data_create(); obs_data_set_bool(data, "streaming", obs_frontend_streaming_active()); obs_data_set_bool(data, "recording", obs_frontend_recording_active()); - obs_data_set_bool(data, "recording-paused", obs_frontend_recording_paused()); + obs_data_set_bool(data, "recording-paused", Utils::RecordingPaused()); obs_data_set_bool(data, "preview-only", false); if (obs_frontend_streaming_active()) {