Skip to content

Commit

Permalink
save settings in profile config. / stable improve.
Browse files Browse the repository at this point in the history
  • Loading branch information
sorayuki committed Feb 16, 2021
1 parent 722bf24 commit 2eb2139
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.16)
project(obs-multi-rtmp VERSION 0.2.5.2)
project(obs-multi-rtmp VERSION 0.2.5.3)

set(QTDIR "${QTDIR}" CACHE PATH "Path to qt directory")

Expand Down
74 changes: 64 additions & 10 deletions src/obs-multi-rtmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,32 @@

#define ConfigSection "obs-multi-rtmp"

QThread* g_uiThread = nullptr;
static class GlobalServiceImpl : public GlobalService
{
public:
void SaveConfig() override {
if (saveConfig_) {
saveConfig_();
}
}

bool RunInUIThread(std::function<void()> task) override {
if (uiThread_ == nullptr)
return false;
QMetaObject::invokeMethod(uiThread_, [func = std::move(task)]() {
func();
});
return true;
}

QThread* uiThread_ = nullptr;
std::function<void()> saveConfig_;
} s_service;


GlobalService& GetGlobalService() {
return s_service;
}


class MultiOutputWidget : public QDockWidget
Expand All @@ -27,7 +52,6 @@ class MultiOutputWidget : public QDockWidget
// save dock location
QObject::connect(this, &QDockWidget::dockLocationChanged, [this](Qt::DockWidgetArea area) {
dockLocation_ = area;

});

scroll_ = new QScrollArea(this);
Expand Down Expand Up @@ -61,6 +85,12 @@ class MultiOutputWidget : public QDockWidget
setLayout(layout_);

resize(200, 400);

s_service.saveConfig_ = [this]() {
s_service.RunInUIThread([this]() {
SaveConfig();
});
};
}

void visibleToggled(bool visible)
Expand All @@ -77,7 +107,8 @@ class MultiOutputWidget : public QDockWidget
obs_module_text("Notice.Title"),
obs_module_text("Notice.Reopen"),
QMessageBox::StandardButton::Ok,
this).exec();
this
).exec();
}
}

Expand Down Expand Up @@ -110,31 +141,51 @@ class MultiOutputWidget : public QDockWidget
x->Stop();
}

void RemoveAll()
{

}

void SaveConfig()
{
auto profile_config = obs_frontend_get_profile_config();

QJsonArray targetlist;
for(auto x : GetAllPushWidgets())
targetlist.append(x->Config());
QJsonObject root;
root["targets"] = targetlist;
QJsonDocument jsondoc;
jsondoc.setObject(root);
config_set_string(obs_frontend_get_global_config(), ConfigSection, "json", jsondoc.toBinaryData().toBase64());
config_set_string(profile_config, ConfigSection, "json", jsondoc.toBinaryData().toBase64());

config_set_int(profile_config, ConfigSection, "DockLocation", (int)dockLocation_);
config_set_bool(profile_config, ConfigSection, "DockVisible", dockVisible_);

config_set_int(obs_frontend_get_global_config(), ConfigSection, "DockLocation", (int)dockLocation_);
config_set_bool(obs_frontend_get_global_config(), ConfigSection, "DockVisible", dockVisible_);
config_save_safe(profile_config, "tmp", "bak");
}

void LoadConfig()
{
auto profile_config = obs_frontend_get_profile_config();

QJsonObject conf;
auto base64str = config_get_string(obs_frontend_get_global_config(), ConfigSection, "json");
auto base64str = config_get_string(profile_config, ConfigSection, "json");
if (!base64str || !*base64str) { // compatible with old version
base64str = config_get_string(obs_frontend_get_global_config(), ConfigSection, "json");
}

if (base64str && *base64str)
{
auto bindat = QByteArray::fromBase64(base64str);
auto jsondoc = QJsonDocument::fromBinaryData(bindat);
if (jsondoc.isObject())
if (jsondoc.isObject()) {
conf = jsondoc.object();

// load succeed. remove all existing widgets
for (auto x : GetAllPushWidgets())
delete x;
}
}

auto targets = conf.find("targets");
Expand Down Expand Up @@ -173,7 +224,7 @@ bool obs_module_load()
if (mainwin == nullptr)
return false;
QMetaObject::invokeMethod(mainwin, []() {
g_uiThread = QThread::currentThread();
s_service.uiThread_ = QThread::currentThread();
});

auto dock = new MultiOutputWidget(mainwin);
Expand Down Expand Up @@ -210,9 +261,12 @@ bool obs_module_load()
[](enum obs_frontend_event event, void *private_data) {
if (event == obs_frontend_event::OBS_FRONTEND_EVENT_EXIT)
{
static_cast<MultiOutputWidget*>(private_data)->SaveConfig();
static_cast<MultiOutputWidget*>(private_data)->StopAll();
}
else if (event == obs_frontend_event::OBS_FRONTEND_EVENT_PROFILE_CHANGED)
{
static_cast<MultiOutputWidget*>(private_data)->LoadConfig();
}
}, dock
);

Expand Down
12 changes: 12 additions & 0 deletions src/obs-multi-rtmp.h
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
#pragma once

#include <functional>

class GlobalService
{
public:
~GlobalService() {}
virtual void SaveConfig() = 0;
virtual bool RunInUIThread(std::function<void()> task) = 0;
};

GlobalService& GetGlobalService();
14 changes: 0 additions & 14 deletions src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,3 @@ inline std::string tostdu8(const QString& qs)
auto b = qs.toUtf8();
return std::string(b.begin(), b.end());
}


extern QThread* g_uiThread;

template<class T>
bool RunInUIThread(T&& func)
{
if (g_uiThread == nullptr)
return false;
QMetaObject::invokeMethod(g_uiThread, [func = std::move(func)]() {
func();
});
return true;
}
41 changes: 30 additions & 11 deletions src/push-widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,30 @@ class PushWidgetImpl : public PushWidget, public IOBSOutputEventHanlder

bool ReleaseOutput()
{
if (output_) {
DisconnectSignals(output_);
}

if (output_ && obs_output_active(output_)) {
obs_output_force_stop(output_);
}

if (output_ && obs_output_active(output_) == false)
{
bool ret = ReleaseOutputService();
ret = ReleaseOutputEncoder() && ret;

DisconnectSignals(output_);
obs_output_release(output_);
output_ = nullptr;

return ret;
}
else if (output_) {
obs_output_release(output_);
output_ = nullptr;

return true;
}
else if (output_ == nullptr)
return true;
else
Expand Down Expand Up @@ -359,7 +372,9 @@ class PushWidgetImpl : public PushWidget, public IOBSOutputEventHanlder

layout->addWidget(edit_btn_ = new QPushButton(obs_module_text("Btn.Edit"), this), 1, 1);
QObject::connect(edit_btn_, &QPushButton::clicked, [this]() {
ShowEditDlg();
if (ShowEditDlg() == true) {
GetGlobalService().SaveConfig();
}
});

layout->addWidget(remove_btn_ = new QPushButton(obs_module_text("Btn.Delete"), this), 1, 2);
Expand All @@ -370,8 +385,12 @@ class PushWidgetImpl : public PushWidget, public IOBSOutputEventHanlder
QMessageBox::Yes | QMessageBox::No,
this
);
if (msgbox->exec() == QMessageBox::Yes)
delete this;
if (msgbox->exec() == QMessageBox::Yes) {
GetGlobalService().RunInUIThread([this]() {
delete this;
GetGlobalService().SaveConfig();
});
}
});

layout->addWidget(msg_ = new QLabel(u8"", this), 2, 0, 1, 3);
Expand Down Expand Up @@ -453,7 +472,7 @@ class PushWidgetImpl : public PushWidget, public IOBSOutputEventHanlder

bool ShowEditDlg() override
{
auto dlg = createEditOutputWidget(conf_, this);
std::unique_ptr<EditOutputWidget> dlg{ createEditOutputWidget(conf_, this) };

if (dlg->exec() == QDialog::DialogCode::Accepted)
{
Expand All @@ -474,7 +493,7 @@ class PushWidgetImpl : public PushWidget, public IOBSOutputEventHanlder
// obs logical
void OnStarting() override
{
RunInUIThread([this]()
GetGlobalService().RunInUIThread([this]()
{
remove_btn_->setEnabled(false);
btn_->setEnabled(false);
Expand All @@ -485,7 +504,7 @@ class PushWidgetImpl : public PushWidget, public IOBSOutputEventHanlder

void OnStarted() override
{
RunInUIThread([this]() {
GetGlobalService().RunInUIThread([this]() {
remove_btn_->setEnabled(false);
btn_->setText(obs_module_text("Status.Stop"));
btn_->setEnabled(true);
Expand All @@ -498,7 +517,7 @@ class PushWidgetImpl : public PushWidget, public IOBSOutputEventHanlder

void OnReconnect() override
{
RunInUIThread([this]() {
GetGlobalService().RunInUIThread([this]() {
remove_btn_->setEnabled(false);
btn_->setText(obs_module_text("Status.Stop"));
btn_->setEnabled(true);
Expand All @@ -508,7 +527,7 @@ class PushWidgetImpl : public PushWidget, public IOBSOutputEventHanlder

void OnReconnected() override
{
RunInUIThread([this]() {
GetGlobalService().RunInUIThread([this]() {
remove_btn_->setEnabled(false);
btn_->setText(obs_module_text("Status.Stop"));
btn_->setEnabled(true);
Expand All @@ -520,7 +539,7 @@ class PushWidgetImpl : public PushWidget, public IOBSOutputEventHanlder

void OnStopping() override
{
RunInUIThread([this]() {
GetGlobalService().RunInUIThread([this]() {
remove_btn_->setEnabled(false);
btn_->setText(obs_module_text("Status.Stop"));
btn_->setEnabled(false);
Expand All @@ -530,7 +549,7 @@ class PushWidgetImpl : public PushWidget, public IOBSOutputEventHanlder

void OnStopped(int code) override
{
RunInUIThread([this, code]() {
GetGlobalService().RunInUIThread([this, code]() {
ResetInfo();
timer_->stop();

Expand Down

0 comments on commit 2eb2139

Please sign in to comment.