Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Enable first-party YouTube Chat features in OBS #10770

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 56 additions & 15 deletions UI/auth-youtube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ static inline void OpenBrowser(const QString auth_uri)
QDesktopServices::openUrl(url);
}

static void DeleteCookies()
{
if (panel_cookies) {
msuman-google marked this conversation as resolved.
Show resolved Hide resolved
panel_cookies->DeleteCookies("youtube.com", "");
panel_cookies->DeleteCookies("google.com", "");
}
}

void RegisterYoutubeAuth()
{
for (auto &service : youtubeServices) {
Expand All @@ -64,7 +72,7 @@ void RegisterYoutubeAuth()
return std::make_shared<YoutubeApiWrappers>(
service);
},
YoutubeAuth::Login, []() { return; });
YoutubeAuth::Login, DeleteCookies);
}
}

Expand Down Expand Up @@ -130,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 @@ -163,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 @@ -211,11 +210,21 @@ void YoutubeAuth::ResetChat()
{
#ifdef BROWSER_AVAILABLE
if (chat && chat->cefWidget) {
chat->SetApiChatId("");
chat->cefWidget->setURL(YOUTUBE_CHAT_PLACEHOLDER_URL);
}
#endif
}

void YoutubeAuth::ReloadChat()
{
#ifdef BROWSER_AVAILABLE
if (chat && chat->cefWidget) {
chat->cefWidget->reloadPage();
}
#endif
}

QString YoutubeAuth::GenerateState()
{
char state[YOUTUBE_API_STATE_LENGTH + 1];
Expand Down Expand Up @@ -382,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 @@ -425,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
5 changes: 4 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 Expand Up @@ -60,6 +62,7 @@ class YoutubeAuth : public OAuthStreamKey {

void SetChatId(const QString &chat_id, const std::string &api_chat_id);
void ResetChat();
void ReloadChat();

static std::shared_ptr<Auth> Login(QWidget *parent,
const std::string &service);
Expand Down
41 changes: 21 additions & 20 deletions UI/window-dock-youtube-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,13 @@ static constexpr const char *INGESTION_STOPPED = "INGESTION_STOPPED";

YouTubeAppDock::YouTubeAppDock(const QString &title)
: BrowserDock(title),
dockBrowser(nullptr),
cookieManager(nullptr)
dockBrowser(nullptr)
{
cef->init_browser();
OBSBasic::InitBrowserPanelSafeBlock();
AddYouTubeAppDock();
}

YouTubeAppDock::~YouTubeAppDock()
{
if (cookieManager) {
cookieManager->FlushStore();
delete cookieManager;
}
}

bool YouTubeAppDock::IsYTServiceSelected()
{
if (!cef_js_avail)
Expand Down Expand Up @@ -78,9 +69,12 @@ void YouTubeAppDock::SettingsUpdated(bool cleanup)

// definitely cleanup if YT switched off
if (!ytservice || cleanup) {
if (cookieManager)
cookieManager->DeleteCookies("", "");
if (panel_cookies) {
panel_cookies->DeleteCookies("youtube.com", "");
panel_cookies->DeleteCookies("google.com", "");
}
}

if (ytservice)
Update();
}
Expand Down Expand Up @@ -135,23 +129,20 @@ void YouTubeAppDock::AddYouTubeAppDock()

void YouTubeAppDock::CreateBrowserWidget(const std::string &url)
{
std::string dir_name = std::string("obs_profile_cookies_youtube/") +
config_get_string(OBSBasic::Get()->Config(),
"Panels", "CookieId");
if (cookieManager)
delete cookieManager;
cookieManager = cef->create_cookie_manager(dir_name, true);

if (dockBrowser)
delete dockBrowser;
dockBrowser = cef->create_widget(this, url, cookieManager);
dockBrowser = cef->create_widget(this, url, panel_cookies);
if (!dockBrowser)
return;

if (obs_browser_qcef_version() >= 1)
dockBrowser->allowAllPopups(true);

this->SetWidget(dockBrowser);

QWidget::connect(dockBrowser.get(), &QCefWidget::urlChanged, this,
&YouTubeAppDock::ReloadChatDock);

Update();
}

Expand Down Expand Up @@ -348,6 +339,16 @@ void YouTubeAppDock::UpdateChannelId()
}
}

void YouTubeAppDock::ReloadChatDock()
{
if (IsUserSignedIntoYT()) {
YoutubeApiWrappers *apiYouTube = GetYTApi();
if (apiYouTube) {
apiYouTube->ReloadChat();
}
}
}

void YouTubeAppDock::SetInitEvent(streaming_mode_t mode, const char *event,
const char *video_id, const char *channelId)
{
Expand Down
7 changes: 4 additions & 3 deletions UI/window-dock-youtube-app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ class YouTubeAppDock : public BrowserDock {

public:
YouTubeAppDock(const QString &title);
~YouTubeAppDock();

enum streaming_mode_t { YTSM_ACCOUNT, YTSM_STREAM_KEY };

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

void BroadcastCreated(const char *stream_id);
Expand All @@ -29,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 All @@ -43,11 +44,11 @@ class YouTubeAppDock : public BrowserDock {
void DispatchYTEvent(const char *event, const char *video_id,
streaming_mode_t mode);
void UpdateChannelId();
void ReloadChatDock();
void SetInitEvent(streaming_mode_t mode, const char *event = nullptr,
const char *video_id = nullptr,
const char *channelId = nullptr);

QString channelId;
QPointer<QCefWidget> dockBrowser;
QCefCookieManager *cookieManager; // is not a Qt object
};
Loading