Skip to content

Commit

Permalink
UI: Enable first-party YouTube Chat features in OBS
Browse files Browse the repository at this point in the history
Unlock the full feature set of the YouTube Chat dock in OBS by removing
custom scripting/CSS logic. Enable the signed-in experience for live
streaming content creators (sharing login credentials with the YouTube
Control panel dock, see
#10747).

This will allow OBS users to utilize features _already_ supported in the
YouTube Chat plugin, such as

* creating polls
* managing Q&A sessions
* a rich emoji set in the input panel
* emoji fountains
* moderation tools

and many more. These features are available to users who are logged-in
to YouTube Chat and/or the YouTube Control panel.
  • Loading branch information
msuman-google committed Sep 17, 2024
1 parent 15408d3 commit a299932
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
52 changes: 38 additions & 14 deletions UI/auth-youtube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,6 @@ bool YoutubeAuth::LoadInternal()
return implicit ? !token.empty() : !refresh_token.empty();
}

#ifdef BROWSER_AVAILABLE
static const char *ytchat_script = "\
const obsCSS = document.createElement('style');\
obsCSS.innerHTML = \"#panel-pages.yt-live-chat-renderer {display: none;}\
yt-live-chat-viewer-engagement-message-renderer {display: none;}\";\
document.querySelector('head').appendChild(obsCSS);";
#endif

void YoutubeAuth::LoadUI()
{
if (uiLoaded)
Expand All @@ -171,7 +163,6 @@ void YoutubeAuth::LoadUI()

browser = cef->create_widget(chat, YOUTUBE_CHAT_PLACEHOLDER_URL,
panel_cookies);
browser->setStartupScript(ytchat_script);

chat->SetWidget(browser);
main->AddDockWidget(chat, Qt::RightDockWidgetArea);
Expand Down Expand Up @@ -219,6 +210,7 @@ void YoutubeAuth::ResetChat()
{
#ifdef BROWSER_AVAILABLE
if (chat && chat->cefWidget) {
chat->SetApiChatId("");
chat->cefWidget->setURL(YOUTUBE_CHAT_PLACEHOLDER_URL);
}
#endif
Expand Down Expand Up @@ -399,13 +391,44 @@ void YoutubeChatDock::SetWidget(QCefWidget *widget_)
setWidget(widget);

cefWidget.reset(widget_);

QWidget::connect(cefWidget.get(), &QCefWidget::urlChanged, this,
&YoutubeChatDock::YoutubeCookieCheck);
}

void YoutubeChatDock::SetApiChatId(const std::string &id)
{
this->apiChatId = id;
QMetaObject::invokeMethod(this, "EnableChatInput",
Qt::QueuedConnection);
QMetaObject::invokeMethod(this, "EnableChatInput", Qt::QueuedConnection,
Q_ARG(bool, !id.empty()));
}

void YoutubeChatDock::YoutubeCookieCheck()
{
QPointer<YoutubeChatDock> this_ = this;
auto cb = [this_](bool currentlyLoggedIn) {
bool previouslyLoggedIn = this_->isLoggedIn;
this_->isLoggedIn = currentlyLoggedIn;
bool loginStateChanged =
(currentlyLoggedIn && !previouslyLoggedIn) ||
(!currentlyLoggedIn && previouslyLoggedIn);
if (loginStateChanged) {
QMetaObject::invokeMethod(
this_, "EnableChatInput", Qt::QueuedConnection,
Q_ARG(bool, !currentlyLoggedIn));
OBSBasic *main = OBSBasic::Get();
if (main->GetYouTubeAppDock() != nullptr) {
QMetaObject::invokeMethod(
main->GetYouTubeAppDock(),
"SettingsUpdated", Qt::QueuedConnection,
Q_ARG(bool, !currentlyLoggedIn));
}
}
};
if (panel_cookies) {
panel_cookies->CheckForCookie("https://www.youtube.com", "SID",
cb);
}
}

void YoutubeChatDock::SendChatMessage()
Expand Down Expand Up @@ -442,9 +465,10 @@ void YoutubeChatDock::ShowErrorMessage(const QString &error)
QTStr("YouTube.Chat.Error.Text").arg(error));
}

void YoutubeChatDock::EnableChatInput()
void YoutubeChatDock::EnableChatInput(bool visible)
{
lineEdit->setVisible(true);
sendButton->setVisible(true);
bool setVisible = visible && !isLoggedIn;
lineEdit->setVisible(setVisible);
sendButton->setVisible(setVisible);
}
#endif
4 changes: 3 additions & 1 deletion UI/auth-youtube.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class YoutubeChatDock : public BrowserDock {

private:
std::string apiChatId;
bool isLoggedIn;
LineEditAutoResize *lineEdit;
QPushButton *sendButton;
QHBoxLayout *chatLayout;
Expand All @@ -26,9 +27,10 @@ class YoutubeChatDock : public BrowserDock {
void SetApiChatId(const std::string &id);

private slots:
void YoutubeCookieCheck();
void SendChatMessage();
void ShowErrorMessage(const QString &error);
void EnableChatInput();
void EnableChatInput(bool visible);
};
#endif

Expand Down
4 changes: 3 additions & 1 deletion UI/window-dock-youtube-app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class YouTubeAppDock : public BrowserDock {

void AccountConnected();
void AccountDisconnected();
void SettingsUpdated(bool cleanup = false);
void Update();

void BroadcastCreated(const char *stream_id);
Expand All @@ -28,6 +27,9 @@ class YouTubeAppDock : public BrowserDock {
static YoutubeApiWrappers *GetYTApi();
static void CleanupYouTubeUrls();

public slots:
void SettingsUpdated(bool cleanup = false);

protected:
void IngestionStarted(const char *stream_id, streaming_mode_t mode);
void IngestionStopped(const char *stream_id, streaming_mode_t mode);
Expand Down

0 comments on commit a299932

Please sign in to comment.