Skip to content

Commit

Permalink
Remove libcurl from Mac and Windows (#420)
Browse files Browse the repository at this point in the history
* Add

* Lint

* Update github-utils.cpp

* Update buildspec.json

* Update build-project.yaml

* Update WinRTHttpClient.cpp

* Update CMakeLists.txt

* Update FetchOpenCV.cmake

* Update FetchOpenCV.cmake

* Remove curl

* Delete .gitmodules
  • Loading branch information
umireon authored Aug 14, 2023
1 parent 58b2137 commit e48b83c
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 116 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

20 changes: 9 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,15 @@ else()
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE OpenCV)
endif()

set(USE_SYSTEM_CURL
OFF
CACHE STRING "Use system cURL")

if(USE_SYSTEM_CURL)
find_package(CURL REQUIRED)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE "${CURL_LIBRARIES}")
target_include_directories(${CMAKE_PROJECT_NAME} SYSTEM PUBLIC "${CURL_INCLUDE_DIRS}")
else()
include(cmake/BuildMyCurl.cmake)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE libcurl)
if(APPLE)
add_subdirectory(src/update-checker/URLSessionClient)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE URLSessionClient)
elseif(MSVC)
add_subdirectory(src/update-checker/WinRTHttpClient)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE WinRTHttpClient)
elseif(UNIX)
add_subdirectory(src/update-checker/CurlClient)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE CurlClient)
endif()

target_sources(
Expand Down
2 changes: 1 addition & 1 deletion buildspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
},
"name": "obs-backgroundremoval",
"version": "1.1.3",
"version": "1.1.4",
"author": "Roy Shilkrot",
"website": "https://github.com/royshil/obs-backgroundremoval",
"email": "roy.shil@gmail.com",
Expand Down
12 changes: 1 addition & 11 deletions src/plugin-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ with this program. If not, see <https://www.gnu.org/licenses/>

#include "plugin-support.h"

#include "update-checker/github-utils.h"
#include "update-checker/update-checker.h"

OBS_DECLARE_MODULE()
Expand All @@ -41,16 +40,7 @@ bool obs_module_load(void)
obs_log(LOG_INFO, "Plugin loaded successfully (version %s)",
PLUGIN_VERSION);

const struct github_utils_release_information latestRelease =
github_utils_get_release_information();
if (latestRelease.responseCode == OBS_BGREMOVAL_GITHUB_UTILS_SUCCESS) {
obs_log(LOG_INFO, "Latest release is %s",
latestRelease.version);
check_update(latestRelease);
} else {
obs_log(LOG_INFO, "failed to get latest release information");
}
github_utils_release_information_free(latestRelease);
check_update();

return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/update-checker/Client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <string>
#include <functional>

void fetchStringFromUrl(const char *urlString,
std::function<void(std::string, int)> callback);
7 changes: 7 additions & 0 deletions src/update-checker/CurlClient/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
add_library(CurlClient CurlClient.cpp)
target_link_libraries(CurlClient PRIVATE OBS::libobs plugin-support)
target_include_directories(CurlClient PRIVATE "${CMAKE_SOURCE_DIR}/src")

find_package(CURL REQUIRED)
target_link_libraries(CurlClient PRIVATE "${CURL_LIBRARIES}")
target_include_directories(CurlClient SYSTEM PRIVATE "${CURL_INCLUDE_DIRS}")
43 changes: 43 additions & 0 deletions src/update-checker/CurlClient/CurlClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <string>
#include <functional>

#include <curl/curl.h>

#include <obs.h>
#include <plugin-support.h>

const std::string userAgent = std::string(PLUGIN_NAME) + "/" + PLUGIN_VERSION;

static std::size_t writeFunc(void *ptr, std::size_t size, size_t nmemb,
std::string *data)
{
data->append(static_cast<char *>(ptr), size * nmemb);
return size * nmemb;
}

void fetchStringFromUrl(const char *urlString,
std::function<void(std::string, int)> callback)
{
CURL *curl = curl_easy_init();
if (!curl) {
obs_log(LOG_INFO, "Failed to initialize curl");
callback("", CURL_LAST);
return;
}

std::string responseBody;
curl_easy_setopt(curl, CURLOPT_URL, urlString);
curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBody);

CURLcode code = curl_easy_perform(curl);
curl_easy_cleanup(curl);

if (code == CURLE_OK) {
callback(responseBody, 0);
} else {
obs_log(LOG_INFO, "Failed to get latest release info");
callback("", code);
}
}
3 changes: 3 additions & 0 deletions src/update-checker/URLSessionClient/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_library(URLSessionClient URLSessionClient.mm)
target_link_libraries(URLSessionClient PRIVATE OBS::libobs plugin-support)
target_include_directories(URLSessionClient PRIVATE "${CMAKE_SOURCE_DIR}/src")
49 changes: 49 additions & 0 deletions src/update-checker/URLSessionClient/URLSessionClient.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <string>
#include <functional>

#include <Foundation/Foundation.h>

#include <obs.h>
#include <plugin-support.h>

void dispatch(const std::function<void(std::string, int)> &callback,
std::string responseBody, int errorCode)
{
dispatch_async(dispatch_get_main_queue(), ^{
callback(responseBody, errorCode);
});
}

void fetchStringFromUrl(const char *urlString,
std::function<void(std::string, int)> callback)
{
NSString *urlNsString = [[NSString alloc] initWithUTF8String:urlString];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:urlNsString];
NSURLSessionDataTask *task = [session
dataTaskWithURL:url
completionHandler:^(NSData *_Nullable data,
NSURLResponse *_Nullable response,
NSError *_Nullable error) {
if (error != NULL) {
obs_log(LOG_INFO, "error");
dispatch(callback, "", 1);
} else if (response == NULL) {
dispatch(callback, "", 2);
} else if (data == NULL || data.length == 0) {
dispatch(callback, "", 3);
} else if (![response isKindOfClass:NSHTTPURLResponse
.class]) {
dispatch(callback, "", 4);
} else {
std::string responseBody(
(const char *)data.bytes, data.length);
NSHTTPURLResponse *httpResponse =
(NSHTTPURLResponse *)response;
NSInteger code = httpResponse.statusCode;
dispatch(callback, responseBody,
code < 200 || code > 299 ? 5 : 0);
}
}];
[task resume];
}
10 changes: 10 additions & 0 deletions src/update-checker/WinRTHttpClient/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_library(WinRTHttpClient WinRTHttpClient.cpp)
set_target_properties(
WinRTHttpClient
PROPERTIES CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
VS_GLOBAL_CppWinRTOptimized true
VS_GLOBAL_CppWinRTRootNamespaceAutoMerge true)
target_link_libraries(WinRTHttpClient PRIVATE OBS::libobs plugin-support)
target_include_directories(WinRTHttpClient PRIVATE "${CMAKE_SOURCE_DIR}/src")
40 changes: 40 additions & 0 deletions src/update-checker/WinRTHttpClient/WinRTHttpClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <functional>

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Web.Http.Headers.h>
#include <winrt/windows.storage.streams.h>
#include <plugin-support.h>

using winrt::Windows::Foundation::Uri;
using winrt::Windows::Storage::Streams::IBuffer;
using winrt::Windows::Web::Http::HttpClient;
using winrt::Windows::Web::Http::HttpResponseMessage;
using winrt::Windows::Web::Http::IHttpContent;

winrt::hstring userAgent = winrt::to_hstring(PLUGIN_NAME) + L"/" +
winrt::to_hstring(PLUGIN_VERSION);

void fetchStringFromUrl(const char *urlString,
std::function<void(std::string, int)> callback)
{
HttpClient httpClient;
auto headers(httpClient.DefaultRequestHeaders());
headers.UserAgent().TryParseAdd(userAgent);
Uri requestUri(winrt::to_hstring(urlString));
HttpResponseMessage httpResponseMessage;
IBuffer httpResponseBuffer;
try {
httpResponseMessage = httpClient.GetAsync(requestUri).get();
httpResponseMessage.EnsureSuccessStatusCode();
IHttpContent httpContent = httpResponseMessage.Content();
httpResponseBuffer = httpContent.ReadAsBufferAsync().get();

uint8_t *data = httpResponseBuffer.data();
std::string str((const char *)data,
httpResponseBuffer.Length());
callback(str, 0);
} catch (winrt::hresult_error const &ex) {
std::string str(winrt::to_string(ex.message()));
callback(str, 1);
}
}
90 changes: 34 additions & 56 deletions src/update-checker/github-utils.cpp
Original file line number Diff line number Diff line change
@@ -1,70 +1,48 @@
#include <curl/curl.h>

#include <cstddef>
#include <string>

#include <obs.h>

#include "Client.hpp"
#include "github-utils.h"

static const std::string GITHUB_LATEST_RELEASE_URL =
"https://api.github.com/repos/royshil/obs-backgroundremoval/releases/latest";

extern "C" const char *PLUGIN_NAME;
extern "C" const char *PLUGIN_VERSION;

static const std::string USER_AGENT =
std::string(PLUGIN_NAME) + "/" + std::string(PLUGIN_VERSION);

std::size_t writeFunctionStdString(void *ptr, std::size_t size, size_t nmemb,
std::string *data)
{
data->append(static_cast<char *>(ptr), size * nmemb);
return size * nmemb;
}

struct github_utils_release_information
github_utils_get_release_information(void)
void github_utils_get_release_information(
std::function<void(github_utils_release_information)> callback)
{
CURL *curl = curl_easy_init();
if (!curl) {
blog(LOG_INFO, "Failed to initialize curl");
return {OBS_BGREMOVAL_GITHUB_UTILS_ERROR, NULL, NULL};
}
CURLcode code;
curl_easy_setopt(curl, CURLOPT_URL, GITHUB_LATEST_RELEASE_URL.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, USER_AGENT.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunctionStdString);
std::string responseBody;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBody);
code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (code != CURLE_OK) {
blog(LOG_INFO, "Failed to get latest release info");
return {OBS_BGREMOVAL_GITHUB_UTILS_ERROR, NULL, NULL};
}

// Parse the JSON response
obs_data_t *data = obs_data_create_from_json(responseBody.c_str());
if (!data) {
blog(LOG_INFO, "Failed to parse latest release info");
return {OBS_BGREMOVAL_GITHUB_UTILS_ERROR, NULL, NULL};
}

// The version is in the "tag_name" property
char *version = strdup(obs_data_get_string(data, "tag_name"));
char *body = strdup(obs_data_get_string(data, "body"));
obs_data_release(data);

// remove the "v" prefix in version, if it exists
if (version[0] == 'v') {
char *newVersion = (char *)malloc(strlen(version) - 1);
strcpy(newVersion, version + 1);
free(version);
version = newVersion;
}

return {OBS_BGREMOVAL_GITHUB_UTILS_SUCCESS, body, version};
fetchStringFromUrl(GITHUB_LATEST_RELEASE_URL.c_str(), [callback](
std::string
responseBody,
int code) {
if (code != 0)
return;
// Parse the JSON response
obs_data_t *data =
obs_data_create_from_json(responseBody.c_str());
if (!data) {
blog(LOG_INFO, "Failed to parse latest release info");
callback(
{OBS_BGREMOVAL_GITHUB_UTILS_ERROR, NULL, NULL});
return;
}

// The version is in the "tag_name" property
char *version = strdup(obs_data_get_string(data, "tag_name"));
char *body = strdup(obs_data_get_string(data, "body"));
obs_data_release(data);

// remove the "v" prefix in version, if it exists
if (version[0] == 'v') {
char *newVersion = (char *)malloc(strlen(version) - 1);
strcpy(newVersion, version + 1);
free(version);
version = newVersion;
}

callback({OBS_BGREMOVAL_GITHUB_UTILS_SUCCESS, body, version});
});
}

void github_utils_release_information_free(
Expand Down
12 changes: 3 additions & 9 deletions src/update-checker/github-utils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif
#include <functional>

enum {
OBS_BGREMOVAL_GITHUB_UTILS_SUCCESS = 0,
Expand All @@ -15,12 +13,8 @@ struct github_utils_release_information {
char *version;
};

struct github_utils_release_information
github_utils_get_release_information(void);
void github_utils_get_release_information(
std::function<void(github_utils_release_information)> callback);

void github_utils_release_information_free(
struct github_utils_release_information info);

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit e48b83c

Please sign in to comment.