frontend: Prepare UI utility files for splits

This commit is contained in:
PatTheMav
2024-11-25 16:16:18 +01:00
parent d846b0ba0a
commit a0668682b7
15 changed files with 1051 additions and 0 deletions
+219
View File
@@ -0,0 +1,219 @@
/******************************************************************************
Copyright (C) 2023 by Dennis Sädtler <dennis@obsproject.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "ffmpeg-utils.hpp"
#include <unordered_map>
#include <unordered_set>
extern "C" {
#include <libavformat/avformat.h>
}
using namespace std;
vector<FFmpegCodec> GetFormatCodecs(const FFmpegFormat &format, bool ignore_compatibility)
{
vector<FFmpegCodec> codecs;
const AVCodec *codec;
void *i = 0;
while ((codec = av_codec_iterate(&i)) != nullptr) {
// Not an encoding codec
if (!av_codec_is_encoder(codec))
continue;
// Skip if not supported and compatibility check not disabled
if (!ignore_compatibility && !av_codec_get_tag(format.codec_tags, codec->id)) {
continue;
}
codecs.emplace_back(codec);
}
return codecs;
}
static bool is_output_device(const AVClass *avclass)
{
if (!avclass)
return false;
switch (avclass->category) {
case AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT:
case AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT:
case AV_CLASS_CATEGORY_DEVICE_OUTPUT:
return true;
default:
return false;
}
}
vector<FFmpegFormat> GetSupportedFormats()
{
vector<FFmpegFormat> formats;
const AVOutputFormat *output_format;
void *i = 0;
while ((output_format = av_muxer_iterate(&i)) != nullptr) {
if (is_output_device(output_format->priv_class))
continue;
formats.emplace_back(output_format);
}
return formats;
}
FFmpegCodec FFmpegFormat::GetDefaultEncoder(FFmpegCodecType codec_type) const
{
const AVCodecID codec_id = codec_type == VIDEO ? video_codec : audio_codec;
if (codec_type == UNKNOWN || codec_id == AV_CODEC_ID_NONE)
return {};
if (auto codec = avcodec_find_encoder(codec_id))
return {codec};
/* Fall back to using the format name as the encoder,
* this works for some formats such as FLV. */
return FFmpegCodec{name, codec_id, codec_type};
}
bool FFCodecAndFormatCompatible(const char *codec, const char *format)
{
if (!codec || !format)
return false;
const AVOutputFormat *output_format = av_guess_format(format, nullptr, nullptr);
if (!output_format)
return false;
const AVCodecDescriptor *codec_desc = avcodec_descriptor_get_by_name(codec);
if (!codec_desc)
return false;
return avformat_query_codec(output_format, codec_desc->id, FF_COMPLIANCE_NORMAL) == 1;
}
static const unordered_set<string> builtin_codecs = {
"h264", "hevc", "av1", "prores", "aac", "opus", "alac", "flac", "pcm_s16le", "pcm_s24le", "pcm_f32le",
};
bool IsBuiltinCodec(const char *codec)
{
return builtin_codecs.count(codec) > 0;
}
static const unordered_map<string, unordered_set<string>> codec_compat = {
// Technically our muxer supports HEVC and AV1 as well, but nothing else does
{"flv",
{
"h264",
"aac",
}},
{"mpegts",
{
"h264",
"hevc",
"aac",
"opus",
}},
{"hls",
// Also using MPEG-TS in our case, but no Opus support
{
"h264",
"hevc",
"aac",
}},
{"mp4",
{
"h264",
"hevc",
"av1",
"aac",
"opus",
"alac",
"flac",
"pcm_s16le",
"pcm_s24le",
"pcm_f32le",
}},
{"fragmented_mp4",
{
"h264",
"hevc",
"av1",
"aac",
"opus",
"alac",
"flac",
"pcm_s16le",
"pcm_s24le",
"pcm_f32le",
}},
// Not part of FFmpeg, see obs-outputs module
{"hybrid_mp4",
{
"h264",
"hevc",
"av1",
"aac",
"opus",
"alac",
"flac",
"pcm_s16le",
"pcm_s24le",
"pcm_f32le",
}},
{"mov",
{
"h264",
"hevc",
"prores",
"aac",
"alac",
"pcm_s16le",
"pcm_s24le",
"pcm_f32le",
}},
{"fragmented_mov",
{
"h264",
"hevc",
"prores",
"aac",
"alac",
"pcm_s16le",
"pcm_s24le",
"pcm_f32le",
}},
// MKV supports everything
{"mkv", {}},
};
bool ContainerSupportsCodec(const string &container, const string &codec)
{
auto iter = codec_compat.find(container);
if (iter == codec_compat.end())
return false;
auto codecs = iter->second;
// Assume everything is supported
if (codecs.empty())
return true;
return codecs.count(codec) > 0;
}
+146
View File
@@ -0,0 +1,146 @@
/******************************************************************************
Copyright (C) 2023 by Dennis Sädtler <dennis@obsproject.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#pragma once
#include <qmetatype.h>
#include <string>
#include <vector>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
}
enum FFmpegCodecType { AUDIO, VIDEO, UNKNOWN };
/* This needs to handle a few special cases due to how the format is used in the UI:
* - strequal(nullptr, "") must be true
* - strequal("", nullptr) must be true
* - strequal(nullptr, nullptr) must be true
*/
static bool strequal(const char *a, const char *b)
{
if (!a && !b)
return true;
if (!a && *b == 0)
return true;
if (!b && *a == 0)
return true;
if (!a || !b)
return false;
return strcmp(a, b) == 0;
}
struct FFmpegCodec;
struct FFmpegFormat {
const char *name;
const char *long_name;
const char *mime_type;
const char *extensions;
AVCodecID audio_codec;
AVCodecID video_codec;
const AVCodecTag *const *codec_tags;
FFmpegFormat() = default;
FFmpegFormat(const char *name, const char *mime_type)
: name(name),
long_name(nullptr),
mime_type(mime_type),
extensions(nullptr),
audio_codec(AV_CODEC_ID_NONE),
video_codec(AV_CODEC_ID_NONE),
codec_tags(nullptr)
{
}
FFmpegFormat(const AVOutputFormat *av_format)
: name(av_format->name),
long_name(av_format->long_name),
mime_type(av_format->mime_type),
extensions(av_format->extensions),
audio_codec(av_format->audio_codec),
video_codec(av_format->video_codec),
codec_tags(av_format->codec_tag)
{
}
FFmpegCodec GetDefaultEncoder(FFmpegCodecType codec_type) const;
bool HasAudio() const { return audio_codec != AV_CODEC_ID_NONE; }
bool HasVideo() const { return video_codec != AV_CODEC_ID_NONE; }
bool operator==(const FFmpegFormat &format) const
{
if (!strequal(name, format.name))
return false;
return strequal(mime_type, format.mime_type);
}
};
Q_DECLARE_METATYPE(FFmpegFormat)
struct FFmpegCodec {
const char *name;
const char *long_name;
int id;
FFmpegCodecType type;
FFmpegCodec() = default;
FFmpegCodec(const char *name, int id, FFmpegCodecType type = UNKNOWN)
: name(name),
long_name(nullptr),
id(id),
type(type)
{
}
FFmpegCodec(const AVCodec *codec) : name(codec->name), long_name(codec->long_name), id(codec->id)
{
switch (codec->type) {
case AVMEDIA_TYPE_AUDIO:
type = AUDIO;
break;
case AVMEDIA_TYPE_VIDEO:
type = VIDEO;
break;
default:
type = UNKNOWN;
}
}
bool operator==(const FFmpegCodec &codec) const
{
if (id != codec.id)
return false;
return strequal(name, codec.name);
}
};
Q_DECLARE_METATYPE(FFmpegCodec)
std::vector<FFmpegFormat> GetSupportedFormats();
std::vector<FFmpegCodec> GetFormatCodecs(const FFmpegFormat &format, bool ignore_compatibility);
bool FFCodecAndFormatCompatible(const char *codec, const char *format);
bool IsBuiltinCodec(const char *codec);
bool ContainerSupportsCodec(const std::string &container, const std::string &codec);
+146
View File
@@ -0,0 +1,146 @@
/******************************************************************************
Copyright (C) 2023 by Dennis Sädtler <dennis@obsproject.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#pragma once
#include <qmetatype.h>
#include <string>
#include <vector>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
}
enum FFmpegCodecType { AUDIO, VIDEO, UNKNOWN };
/* This needs to handle a few special cases due to how the format is used in the UI:
* - strequal(nullptr, "") must be true
* - strequal("", nullptr) must be true
* - strequal(nullptr, nullptr) must be true
*/
static bool strequal(const char *a, const char *b)
{
if (!a && !b)
return true;
if (!a && *b == 0)
return true;
if (!b && *a == 0)
return true;
if (!a || !b)
return false;
return strcmp(a, b) == 0;
}
struct FFmpegCodec;
struct FFmpegFormat {
const char *name;
const char *long_name;
const char *mime_type;
const char *extensions;
AVCodecID audio_codec;
AVCodecID video_codec;
const AVCodecTag *const *codec_tags;
FFmpegFormat() = default;
FFmpegFormat(const char *name, const char *mime_type)
: name(name),
long_name(nullptr),
mime_type(mime_type),
extensions(nullptr),
audio_codec(AV_CODEC_ID_NONE),
video_codec(AV_CODEC_ID_NONE),
codec_tags(nullptr)
{
}
FFmpegFormat(const AVOutputFormat *av_format)
: name(av_format->name),
long_name(av_format->long_name),
mime_type(av_format->mime_type),
extensions(av_format->extensions),
audio_codec(av_format->audio_codec),
video_codec(av_format->video_codec),
codec_tags(av_format->codec_tag)
{
}
FFmpegCodec GetDefaultEncoder(FFmpegCodecType codec_type) const;
bool HasAudio() const { return audio_codec != AV_CODEC_ID_NONE; }
bool HasVideo() const { return video_codec != AV_CODEC_ID_NONE; }
bool operator==(const FFmpegFormat &format) const
{
if (!strequal(name, format.name))
return false;
return strequal(mime_type, format.mime_type);
}
};
Q_DECLARE_METATYPE(FFmpegFormat)
struct FFmpegCodec {
const char *name;
const char *long_name;
int id;
FFmpegCodecType type;
FFmpegCodec() = default;
FFmpegCodec(const char *name, int id, FFmpegCodecType type = UNKNOWN)
: name(name),
long_name(nullptr),
id(id),
type(type)
{
}
FFmpegCodec(const AVCodec *codec) : name(codec->name), long_name(codec->long_name), id(codec->id)
{
switch (codec->type) {
case AVMEDIA_TYPE_AUDIO:
type = AUDIO;
break;
case AVMEDIA_TYPE_VIDEO:
type = VIDEO;
break;
default:
type = UNKNOWN;
}
}
bool operator==(const FFmpegCodec &codec) const
{
if (id != codec.id)
return false;
return strequal(name, codec.name);
}
};
Q_DECLARE_METATYPE(FFmpegCodec)
std::vector<FFmpegFormat> GetSupportedFormats();
std::vector<FFmpegCodec> GetFormatCodecs(const FFmpegFormat &format, bool ignore_compatibility);
bool FFCodecAndFormatCompatible(const char *codec, const char *format);
bool IsBuiltinCodec(const char *codec);
bool ContainerSupportsCodec(const std::string &container, const std::string &codec);
+51
View File
@@ -0,0 +1,51 @@
#ifndef MAC_UPDATER_H
#define MAC_UPDATER_H
#include <string>
#include <QThread>
#include <QString>
#include <QObject>
class QAction;
class MacUpdateThread : public QThread {
Q_OBJECT
bool manualUpdate;
virtual void run() override;
void info(const QString &title, const QString &text);
signals:
void Result(const QString &branch, bool manual);
private slots:
void infoMsg(const QString &title, const QString &text);
public:
MacUpdateThread(bool manual) : manualUpdate(manual) {}
};
#ifdef __OBJC__
@class OBSUpdateDelegate;
#endif
class OBSSparkle : public QObject {
Q_OBJECT
public:
OBSSparkle(const char *branch, QAction *checkForUpdatesAction);
void setBranch(const char *branch);
void checkForUpdates(bool manualCheck);
private:
#ifdef __OBJC__
OBSUpdateDelegate *updaterDelegate;
#else
void *updaterDelegate;
#endif
};
#endif
+79
View File
@@ -0,0 +1,79 @@
#include "mac-update.hpp"
#include <qaction.h>
#import <Cocoa/Cocoa.h>
#import <Sparkle/Sparkle.h>
@interface OBSUpdateDelegate : NSObject <SPUUpdaterDelegate> {
}
@property (copy) NSString *branch;
@property (nonatomic) SPUStandardUpdaterController *updaterController;
@end
@implementation OBSUpdateDelegate {
}
@synthesize branch;
- (nonnull NSSet<NSString *> *)allowedChannelsForUpdater:(nonnull SPUUpdater *)updater
{
return [NSSet setWithObject:branch];
}
- (void)observeCanCheckForUpdatesWithAction:(QAction *)action
{
[_updaterController.updater addObserver:self forKeyPath:NSStringFromSelector(@selector(canCheckForUpdates))
options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew)
context:(void *) action];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
context:(void *)context
{
if ([keyPath isEqualToString:NSStringFromSelector(@selector(canCheckForUpdates))]) {
QAction *menuAction = (QAction *) context;
menuAction->setEnabled(_updaterController.updater.canCheckForUpdates);
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)dealloc
{
@autoreleasepool {
[_updaterController.updater removeObserver:self forKeyPath:NSStringFromSelector(@selector(canCheckForUpdates))];
}
}
@end
OBSSparkle::OBSSparkle(const char *branch, QAction *checkForUpdatesAction)
{
@autoreleasepool {
updaterDelegate = [[OBSUpdateDelegate alloc] init];
updaterDelegate.branch = [NSString stringWithUTF8String:branch];
updaterDelegate.updaterController =
[[SPUStandardUpdaterController alloc] initWithStartingUpdater:YES updaterDelegate:updaterDelegate
userDriverDelegate:nil];
[updaterDelegate observeCanCheckForUpdatesWithAction:checkForUpdatesAction];
}
}
void OBSSparkle::setBranch(const char *branch)
{
updaterDelegate.branch = [NSString stringWithUTF8String:branch];
}
void OBSSparkle::checkForUpdates(bool manualCheck)
{
@autoreleasepool {
if (manualCheck) {
[updaterDelegate.updaterController checkForUpdates:nil];
} else {
[updaterDelegate.updaterController.updater checkForUpdatesInBackground];
}
}
}
+79
View File
@@ -0,0 +1,79 @@
#include "mac-update.hpp"
#include <qaction.h>
#import <Cocoa/Cocoa.h>
#import <Sparkle/Sparkle.h>
@interface OBSUpdateDelegate : NSObject <SPUUpdaterDelegate> {
}
@property (copy) NSString *branch;
@property (nonatomic) SPUStandardUpdaterController *updaterController;
@end
@implementation OBSUpdateDelegate {
}
@synthesize branch;
- (nonnull NSSet<NSString *> *)allowedChannelsForUpdater:(nonnull SPUUpdater *)updater
{
return [NSSet setWithObject:branch];
}
- (void)observeCanCheckForUpdatesWithAction:(QAction *)action
{
[_updaterController.updater addObserver:self forKeyPath:NSStringFromSelector(@selector(canCheckForUpdates))
options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew)
context:(void *) action];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
context:(void *)context
{
if ([keyPath isEqualToString:NSStringFromSelector(@selector(canCheckForUpdates))]) {
QAction *menuAction = (QAction *) context;
menuAction->setEnabled(_updaterController.updater.canCheckForUpdates);
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)dealloc
{
@autoreleasepool {
[_updaterController.updater removeObserver:self forKeyPath:NSStringFromSelector(@selector(canCheckForUpdates))];
}
}
@end
OBSSparkle::OBSSparkle(const char *branch, QAction *checkForUpdatesAction)
{
@autoreleasepool {
updaterDelegate = [[OBSUpdateDelegate alloc] init];
updaterDelegate.branch = [NSString stringWithUTF8String:branch];
updaterDelegate.updaterController =
[[SPUStandardUpdaterController alloc] initWithStartingUpdater:YES updaterDelegate:updaterDelegate
userDriverDelegate:nil];
[updaterDelegate observeCanCheckForUpdatesWithAction:checkForUpdatesAction];
}
}
void OBSSparkle::setBranch(const char *branch)
{
updaterDelegate.branch = [NSString stringWithUTF8String:branch];
}
void OBSSparkle::checkForUpdates(bool manualCheck)
{
@autoreleasepool {
if (manualCheck) {
[updaterDelegate.updaterController checkForUpdates:nil];
} else {
[updaterDelegate.updaterController.updater checkForUpdatesInBackground];
}
}
}
+295
View File
@@ -0,0 +1,295 @@
#include "moc_shared-update.cpp"
#include "crypto-helpers.hpp"
#include "update-helpers.hpp"
#include "obs-app.hpp"
#include "remote-text.hpp"
#include "platform.hpp"
#include <util/util.hpp>
#include <blake2.h>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <QRandomGenerator>
#include <QByteArray>
#include <QString>
#ifdef BROWSER_AVAILABLE
#include <browser-panel.hpp>
struct QCef;
extern QCef *cef;
#endif
#ifndef MAC_WHATSNEW_URL
#define MAC_WHATSNEW_URL "https://obsproject.com/update_studio/whatsnew.json"
#endif
#ifndef WIN_WHATSNEW_URL
#define WIN_WHATSNEW_URL "https://obsproject.com/update_studio/whatsnew.json"
#endif
#ifndef LINUX_WHATSNEW_URL
#define LINUX_WHATSNEW_URL "https://obsproject.com/update_studio/whatsnew.json"
#endif
#ifdef __APPLE__
#define WHATSNEW_URL MAC_WHATSNEW_URL
#elif defined(_WIN32)
#define WHATSNEW_URL WIN_WHATSNEW_URL
#else
#define WHATSNEW_URL LINUX_WHATSNEW_URL
#endif
#define HASH_READ_BUF_SIZE 65536
#define BLAKE2_HASH_LENGTH 20
/* ------------------------------------------------------------------------ */
static bool QuickWriteFile(const char *file, const std::string &data)
try {
std::ofstream fileStream(std::filesystem::u8path(file), std::ios::binary);
if (fileStream.fail())
throw strprintf("Failed to open file '%s': %s", file, strerror(errno));
fileStream.write(data.data(), data.size());
if (fileStream.fail())
throw strprintf("Failed to write file '%s': %s", file, strerror(errno));
return true;
} catch (std::string &text) {
blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
return false;
}
static bool QuickReadFile(const char *file, std::string &data)
try {
std::ifstream fileStream(std::filesystem::u8path(file), std::ios::binary);
if (!fileStream.is_open() || fileStream.fail())
throw strprintf("Failed to open file '%s': %s", file, strerror(errno));
fileStream.seekg(0, fileStream.end);
size_t size = fileStream.tellg();
fileStream.seekg(0);
data.resize(size);
fileStream.read(&data[0], size);
if (fileStream.fail())
throw strprintf("Failed to write file '%s': %s", file, strerror(errno));
return true;
} catch (std::string &text) {
blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
return false;
}
static bool CalculateFileHash(const char *path, uint8_t *hash)
try {
blake2b_state blake2;
if (blake2b_init(&blake2, BLAKE2_HASH_LENGTH) != 0)
return false;
std::ifstream file(std::filesystem::u8path(path), std::ios::binary);
if (!file.is_open() || file.fail())
return false;
char buf[HASH_READ_BUF_SIZE];
for (;;) {
file.read(buf, HASH_READ_BUF_SIZE);
size_t read = file.gcount();
if (blake2b_update(&blake2, &buf, read) != 0)
return false;
if (file.eof())
break;
}
if (blake2b_final(&blake2, hash, BLAKE2_HASH_LENGTH) != 0)
return false;
return true;
} catch (std::string &text) {
blog(LOG_DEBUG, "%s: %s", __FUNCTION__, text.c_str());
return false;
}
/* ------------------------------------------------------------------------ */
void GenerateGUID(std::string &guid)
{
const char alphabet[] = "0123456789abcdef";
QRandomGenerator *rng = QRandomGenerator::system();
guid.resize(40);
for (size_t i = 0; i < 40; i++) {
guid[i] = alphabet[rng->bounded(0, 16)];
}
}
std::string GetProgramGUID()
{
static std::mutex m;
std::lock_guard<std::mutex> lock(m);
/* NOTE: this is an arbitrary random number that we use to count the
* number of unique OBS installations and is not associated with any
* kind of identifiable information */
const char *pguid = config_get_string(App()->GetAppConfig(), "General", "InstallGUID");
std::string guid;
if (pguid)
guid = pguid;
if (guid.empty()) {
GenerateGUID(guid);
if (!guid.empty())
config_set_string(App()->GetAppConfig(), "General", "InstallGUID", guid.c_str());
}
return guid;
}
/* ------------------------------------------------------------------------ */
static void LoadPublicKey(std::string &pubkey)
{
std::string pemFilePath;
if (!GetDataFilePath("OBSPublicRSAKey.pem", pemFilePath))
throw std::string("Could not find OBS public key file!");
if (!QuickReadFile(pemFilePath.c_str(), pubkey))
throw std::string("Could not read OBS public key file!");
}
static bool CheckDataSignature(const char *name, const std::string &data, const std::string &hexSig)
try {
static std::mutex pubkey_mutex;
static std::string obsPubKey;
if (hexSig.empty() || hexSig.length() > 0xFFFF || (hexSig.length() & 1) != 0)
throw strprintf("Missing or invalid signature for %s: %s", name, hexSig.c_str());
std::scoped_lock lock(pubkey_mutex);
if (obsPubKey.empty())
LoadPublicKey(obsPubKey);
// Convert hex string to bytes
auto signature = QByteArray::fromHex(hexSig.data());
if (!VerifySignature((uint8_t *)obsPubKey.data(), obsPubKey.size(), (uint8_t *)data.data(), data.size(),
(uint8_t *)signature.data(), signature.size()))
throw strprintf("Signature check failed for %s", name);
return true;
} catch (std::string &text) {
blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
return false;
}
/* ------------------------------------------------------------------------ */
bool FetchAndVerifyFile(const char *name, const char *file, const char *url, std::string *out,
const std::vector<std::string> &extraHeaders)
{
long responseCode;
std::vector<std::string> headers;
std::string error;
std::string signature;
std::string data;
uint8_t fileHash[BLAKE2_HASH_LENGTH];
bool success;
BPtr<char> filePath = GetAppConfigPathPtr(file);
if (!extraHeaders.empty()) {
headers.insert(headers.end(), extraHeaders.begin(), extraHeaders.end());
}
/* ----------------------------------- *
* avoid downloading file again */
if (CalculateFileHash(filePath, fileHash)) {
auto hash = QByteArray::fromRawData((const char *)fileHash, BLAKE2_HASH_LENGTH);
QString header = "If-None-Match: " + hash.toHex();
headers.push_back(header.toStdString());
}
/* ----------------------------------- *
* get current install GUID */
std::string guid = GetProgramGUID();
if (!guid.empty()) {
std::string header = "X-OBS2-GUID: " + guid;
headers.push_back(std::move(header));
}
/* ----------------------------------- *
* get file from server */
success = GetRemoteFile(url, data, error, &responseCode, nullptr, "", nullptr, headers, &signature);
if (!success || (responseCode != 200 && responseCode != 304)) {
if (responseCode == 404)
return false;
throw strprintf("Failed to fetch %s file: %s", name, error.c_str());
}
/* ----------------------------------- *
* verify file signature */
if (responseCode == 200) {
success = CheckDataSignature(name, data, signature);
if (!success)
throw strprintf("Invalid %s signature", name);
}
/* ----------------------------------- *
* write or load file */
if (responseCode == 200) {
if (!QuickWriteFile(filePath, data))
throw strprintf("Could not write file '%s'", filePath.Get());
} else if (out) { /* Only read file if caller wants data */
if (!QuickReadFile(filePath, data))
throw strprintf("Could not read file '%s'", filePath.Get());
}
if (out)
*out = data;
/* ----------------------------------- *
* success */
return true;
}
void WhatsNewInfoThread::run()
try {
std::string text;
if (FetchAndVerifyFile("whatsnew", "obs-studio/updates/whatsnew.json", WHATSNEW_URL, &text)) {
emit Result(QString::fromStdString(text));
}
} catch (std::string &text) {
blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
}
/* ------------------------------------------------------------------------ */
void WhatsNewBrowserInitThread::run()
{
#ifdef BROWSER_AVAILABLE
cef->wait_for_browser_init();
#endif
emit Result(url);
}
+36
View File
@@ -0,0 +1,36 @@
#pragma once
#include <QThread>
#include <QString>
#include <vector>
#include <string>
bool FetchAndVerifyFile(const char *name, const char *file, const char *url, std::string *out,
const std::vector<std::string> &extraHeaders = std::vector<std::string>());
class WhatsNewInfoThread : public QThread {
Q_OBJECT
virtual void run() override;
signals:
void Result(const QString &text);
public:
inline WhatsNewInfoThread() {}
};
class WhatsNewBrowserInitThread : public QThread {
Q_OBJECT
QString url;
virtual void run() override;
signals:
void Result(const QString &url);
public:
inline WhatsNewBrowserInitThread(const QString &url_) : url(url_) {}
};