From 9073a09d84d8981ad18325939a759b1a501ad53a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lepin?= Date: Tue, 3 Sep 2019 03:30:06 +0200 Subject: [PATCH] requests(pause recording): runtime feature detection --- src/WSRequestHandler_Recording.cpp | 68 ++++++++++++++++-------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/src/WSRequestHandler_Recording.cpp b/src/WSRequestHandler_Recording.cpp index da7ed356..c2880f3f 100644 --- a/src/WSRequestHandler_Recording.cpp +++ b/src/WSRequestHandler_Recording.cpp @@ -1,6 +1,28 @@ +#include "WSRequestHandler.h" + +#include #include "Utils.h" -#include "WSRequestHandler.h" +typedef void(*pauseRecordingFunction)(bool); +typedef bool(*recordingPausedFunction)(); + +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"); + + 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); +} /** * Toggle recording on or off. @@ -65,22 +87,14 @@ HandlerResponse WSRequestHandler::HandleStartRecording(WSRequestHandler* req) { * @since 4.7.0 */ HandlerResponse WSRequestHandler::HandlePauseRecording(WSRequestHandler* req) { - // TODO runtime check -#if LIBOBS_API_VER < MAKE_SEMANTIC_VERSION(23, 2, 2) - return req->SendErrorResponse("recording pause not supported"); -#else - if (!obs_frontend_recording_active()) { - return req->SendErrorResponse("recording is not active"); - } + return ifCanPause(req, [req](recordingPausedFunction recordingPaused, pauseRecordingFunction pauseRecording) { + if (recordingPaused()) { + return req->SendErrorResponse("recording already paused"); + } - if (obs_frontend_recording_paused()) { - return req->SendErrorResponse("recording already paused"); - } - - obs_frontend_recording_pause(true); - - return req->SendOKResponse(); -#endif + pauseRecording(true); + return req->SendOKResponse(); + }); } /** @@ -93,22 +107,14 @@ HandlerResponse WSRequestHandler::HandlePauseRecording(WSRequestHandler* req) { * @since 4.7.0 */ HandlerResponse WSRequestHandler::HandleResumeRecording(WSRequestHandler* req) { - // TODO runtime check -#if LIBOBS_API_VER < MAKE_SEMANTIC_VERSION(23, 2, 2) - return req->SendErrorResponse("recording resume not supported"); -#else - if (!obs_frontend_recording_active()) { - return req->SendErrorResponse("recording is not active"); - } + return ifCanPause(req, [req](recordingPausedFunction recordingPaused, pauseRecordingFunction pauseRecording) { + if (!recordingPaused()) { + return req->SendErrorResponse("recording is not paused"); + } - if (!obs_frontend_recording_paused()) { - return req->SendErrorResponse("recording is not paused"); - } - - obs_frontend_recording_pause(false); - - return req->SendOKResponse(); -#endif + pauseRecording(false); + return req->SendOKResponse(); + }); } /**