Skip to content

Commit

Permalink
Remember last selected event for "Manage Broadcast" dialog
Browse files Browse the repository at this point in the history
If we can't get event info from api - create default event
  • Loading branch information
SoftwareArchitector committed Dec 14, 2024
1 parent a65897e commit 4f65b00
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 79 deletions.
102 changes: 35 additions & 67 deletions UI/auth-restream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,63 +45,6 @@ RestreamAuth::~RestreamAuth()
main->RemoveDockWidget(RESTREAM_CHANNELS_DOCK_NAME);
}

bool RestreamAuth::SetMainChannelKey()
try {
std::string client_id = RESTREAM_CLIENTID;
deobfuscate_str(&client_id[0], RESTREAM_HASH);

if (!GetToken(RESTREAM_TOKEN_URL, client_id, RESTREAM_SCOPE_VERSION))
return false;
if (token.empty())
return false;
if (!key_.empty())
return true;

std::string auth;
auth += "Authorization: Bearer ";
auth += token;

std::vector<std::string> headers;
headers.push_back(std::string("Client-ID: ") + client_id);
headers.push_back(std::move(auth));

std::string output;
std::string error;
Json json;
bool success;

auto func = [&]() {
auto url = QString("%1/streamKey").arg(RESTREAM_API_URL);
success = GetRemoteFile(url.toUtf8(), output, error, nullptr, "application/json", "", nullptr, headers,
nullptr, 5);
};

ExecThreadedWithoutBlocking(func, QTStr("Auth.LoadingChannel.Title"),
QTStr("Auth.LoadingChannel.Text").arg(service()));
if (!success || output.empty())
throw ErrorInfo("Failed to get stream key from remote", error);

json = Json::parse(output, error);
if (!error.empty())
throw ErrorInfo("Failed to parse json", error);

error = json["error"].string_value();
if (!error.empty())
throw ErrorInfo(error, json["error_description"].string_value());

key_ = json["streamKey"].string_value();

return true;
} catch (ErrorInfo info) {
QString title = QTStr("Auth.ChannelFailure.Title");
QString text = QTStr("Auth.ChannelFailure.Text").arg(service(), info.message.c_str(), info.error.c_str());

QMessageBox::warning(OBSBasic::Get(), title, text);

blog(LOG_WARNING, "%s: %s: %s", __FUNCTION__, info.message.c_str(), info.error.c_str());
return false;
}

bool RestreamAuth::GetBroadcastInfo(QVector<RestreamEventDescription> &broadcast_out)
try {
std::string client_id = RESTREAM_CLIENTID;
Expand Down Expand Up @@ -193,7 +136,9 @@ try {
headers.push_back(std::string("Client-ID: ") + client_id);
headers.push_back(std::move(auth));

auto url = QString("%1/events/%2/streamKey").arg(RESTREAM_API_URL, id);
auto url = id.isEmpty() || id == "default"
? QString("%1/streamKey").arg(RESTREAM_API_URL)
: QString("%1/events/%2/streamKey").arg(RESTREAM_API_URL, id);

std::string output;
std::string error;
Expand All @@ -208,7 +153,7 @@ try {
ExecThreadedWithoutBlocking(func, QTStr("Auth.LoadingChannel.Title"),
QTStr("Auth.LoadingChannel.Text").arg(service()));
if (!success || output.empty())
throw ErrorInfo("Failed to get the event key from remote", error);
throw ErrorInfo("Failed to get the stream key from remote", error);

json = Json::parse(output, error);
if (!error.empty())
Expand All @@ -234,24 +179,42 @@ try {

void RestreamAuth::UseBroadcastKey(QString key, QString show_id)
{
if (QString::fromStdString(key_) == key && currentShowId == show_id)
return;

key_ = key.toUtf8();
currentShowId = show_id;

if (chatWidgetBrowser) {
auto url = QString("https://restream.io/chat-application?show-id=%1").arg(show_id);
auto url = QString("https://restream.io/chat-application");
if (!show_id.isEmpty())
url.append(QString("?show-id=%1").arg(show_id));

chatWidgetBrowser->setURL(url.toStdString());
}

if (titlesWidgetBrowser) {
auto url = QString("https://restream.io/titles/embed?show-id=%1").arg(show_id);
auto url = QString("https://restream.io/titles/embed");
if (!show_id.isEmpty())
url.append(QString("?show-id=%1").arg(show_id));

titlesWidgetBrowser->setURL(url.toStdString());
}

if (channelWidgetBrowser) {
auto url = QString("https://restream.io/channel/embed?show-id=%1").arg(show_id);
auto url = QString("https://restream.io/channel/embed");
if (!show_id.isEmpty())
url.append(QString("?show-id=%1").arg(show_id));

channelWidgetBrowser->setURL(url.toStdString());
}
}

QString RestreamAuth::GetCurrentShowId()
{
return currentShowId;
}

void RestreamAuth::SaveInternal()
{
OBSBasic *main = OBSBasic::Get();
Expand Down Expand Up @@ -280,9 +243,13 @@ void RestreamAuth::LoadUI()

if (!cef)
return;
if (!SetMainChannelKey())

QString key;
if (!GetBroadcastKey("default", key))
return;

UseBroadcastKey(key, "");

OBSBasic::InitBrowserPanelSafeBlock();
OBSBasic *main = OBSBasic::Get();

Expand Down Expand Up @@ -398,11 +365,12 @@ std::shared_ptr<Auth> RestreamAuth::Login(QWidget *parent, const std::string &)
if (!auth->GetToken(RESTREAM_TOKEN_URL, client_id, RESTREAM_SCOPE_VERSION, QT_TO_UTF8(login.GetCode())))
return nullptr;

std::string error;
if (auth->SetMainChannelKey())
return auth;
QString key;
if (!auth->GetBroadcastKey("default", key))
return nullptr;

return nullptr;
auth->UseBroadcastKey(key, "");
return auth;
}

static std::shared_ptr<Auth> CreateRestreamAuth()
Expand Down
3 changes: 2 additions & 1 deletion UI/auth-restream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class RestreamAuth : public OAuthStreamKey {
Q_OBJECT

bool uiLoaded = false;
QString currentShowId = "";
QCefWidget *chatWidgetBrowser = NULL;
QCefWidget *titlesWidgetBrowser = NULL;
QCefWidget *channelWidgetBrowser = NULL;
Expand All @@ -30,10 +31,10 @@ class RestreamAuth : public OAuthStreamKey {
RestreamAuth(const Def &d);
~RestreamAuth();

bool SetMainChannelKey();
bool GetBroadcastInfo(QVector<RestreamEventDescription> &events);
bool GetBroadcastKey(QString id, QString &key_out);
void UseBroadcastKey(QString key, QString show_id);
QString GetCurrentShowId();

static std::shared_ptr<Auth> Login(QWidget *parent, const std::string &service_name);
};
Expand Down
1 change: 1 addition & 0 deletions UI/data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,7 @@ MultitrackVideo.IncompatibleSettings.AudioChannelsMultiple="%1 requires multiple
# Restream Actions
Restream.Actions.WindowTitle="Restream Broadcast Setup"
Restream.Actions.BroadcastSelectTitle="Select Existing Broadcast"
Restream.Actions.DashboardButton="Open Restream";
Restream.Actions.BroadcastSelectButton="Select broadcast"
Restream.Actions.BroadcastSelectAndStartButton="Select broadcast and start streaming"
Restream.Actions.BroadcastScheduled="Scheduled"
Expand Down
9 changes: 8 additions & 1 deletion UI/forms/OBSRestreamActions.ui
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
<number>0</number>
</property>
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="1,2,4">
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="1,1,2,3">
<property name="spacing">
<number>10</number>
</property>
Expand All @@ -147,6 +147,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="dashboardButton">
<property name="text">
<string>Restream.Actions.DashboardButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="saveButton">
<property name="text">
Expand Down
37 changes: 27 additions & 10 deletions UI/window-restream-actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
#include <QStandardPaths>
#include <QImageReader>

const QString SchedulDateAndTimeFormat = "yyyy-MM-dd'T'hh:mm:ss'Z'";
const QString RepresentSchedulDateAndTimeFormat = "dddd, MMMM d, yyyy h:m";
const QString IndexOfGamingCategory = "20";

OBSRestreamActions::OBSRestreamActions(QWidget *parent, Auth *auth, bool broadcastReady)
: QDialog(parent),
ui(new Ui::OBSRestreamActions),
Expand All @@ -28,6 +24,7 @@ OBSRestreamActions::OBSRestreamActions(QWidget *parent, Auth *auth, bool broadca

connect(ui->okButton, &QPushButton::clicked, this, &OBSRestreamActions::BroadcastSelectAndStartAction);
connect(ui->saveButton, &QPushButton::clicked, this, &OBSRestreamActions::BroadcastSelectAction);
connect(ui->dashboardButton, &QPushButton::clicked, this, &OBSRestreamActions::OpenRestreamDashboard);
connect(ui->cancelButton, &QPushButton::clicked, this, [&]() {
blog(LOG_DEBUG, "Restream live event creation cancelled.");
reject();
Expand All @@ -37,10 +34,16 @@ OBSRestreamActions::OBSRestreamActions(QWidget *parent, Auth *auth, bool broadca

QVector<RestreamEventDescription> events;
if (!restreamAuth->GetBroadcastInfo(events)) {
reject();
return;
RestreamEventDescription event;
event.id = QString("default");
event.title = QString("Live with Restream");
event.scheduledFor = 0;
event.showId = QString("");
events.push_back(event);
}

auto currentShowId = restreamAuth->GetCurrentShowId();

for (auto event : events) {
ClickableLabel *label = new ClickableLabel();
label->setTextFormat(Qt::RichText);
Expand Down Expand Up @@ -80,6 +83,17 @@ OBSRestreamActions::OBSRestreamActions(QWidget *parent, Auth *auth, bool broadca
});

ui->scrollAreaWidgetContents->layout()->addWidget(label);

if (broadcastReady && event.showId == currentShowId) {
label->setProperty("class", "row-selected");
label->style()->unpolish(label);
label->style()->polish(label);

selectedBroadcastId = event.id;
selectedShowId = event.showId;

UpdateOkButtonStatus();
}
}
}

Expand All @@ -95,9 +109,8 @@ void OBSRestreamActions::UpdateOkButtonStatus()
void OBSRestreamActions::BroadcastSelectAction()
{
QString streamKey;
if (!restreamAuth->GetBroadcastKey(selectedBroadcastId, streamKey)) {
if (!restreamAuth->GetBroadcastKey(selectedBroadcastId, streamKey))
return;
}

emit ok(QT_TO_UTF8(selectedBroadcastId), QT_TO_UTF8(streamKey), QT_TO_UTF8(selectedShowId), false);
accept();
Expand All @@ -106,10 +119,14 @@ void OBSRestreamActions::BroadcastSelectAction()
void OBSRestreamActions::BroadcastSelectAndStartAction()
{
QString streamKey;
if (!restreamAuth->GetBroadcastKey(selectedBroadcastId, streamKey)) {
if (!restreamAuth->GetBroadcastKey(selectedBroadcastId, streamKey))
return;
}

emit ok(QT_TO_UTF8(selectedBroadcastId), QT_TO_UTF8(streamKey), QT_TO_UTF8(selectedShowId), true);
accept();
}

void OBSRestreamActions::OpenRestreamDashboard()
{
QDesktopServices::openUrl(QString("https://app.restream.io/"));
}
1 change: 1 addition & 0 deletions UI/window-restream-actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class OBSRestreamActions : public QDialog {
private:
void BroadcastSelectAction();
void BroadcastSelectAndStartAction();
void OpenRestreamDashboard();

QIcon GetPlaceholder() { return thumbPlaceholder; }
void SetPlaceholder(const QIcon &icon) { thumbPlaceholder = icon; }
Expand Down

0 comments on commit 4f65b00

Please sign in to comment.