1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/glic/media/glic_media_integration.h"
#include "base/feature_list.h"
#include "base/no_destructor.h"
#include "base/supports_user_data.h"
#include "chrome/browser/accessibility/live_caption/live_caption_controller_factory.h"
#include "chrome/browser/glic/media/glic_media_context.h"
#include "chrome/browser/glic/media/glic_media_page_cache.h"
#include "chrome/browser/profiles/profile.h"
#include "components/live_caption/caption_controller_base.h"
#include "components/live_caption/caption_util.h"
#include "components/live_caption/live_caption_controller.h"
#include "components/live_caption/pref_names.h"
#include "components/optimization_guide/content/browser/page_content_proto_provider.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/peer_connection_tracker_host_observer.h"
#include "content/public/browser/web_contents.h"
#include "media/base/media_switches.h"
namespace {
constexpr char kGlicMediaIntegrationKey[] = "GlicMediaIntegration";
class GlicMediaPeerConnectionObserver
: public content::PeerConnectionTrackerHostObserver {
public:
~GlicMediaPeerConnectionObserver() override = default;
void OnPeerConnectionAdded(
content::GlobalRenderFrameHostId render_frame_host_id,
int lid,
base::ProcessId pid,
const std::string& url,
const std::string& rtc_configuration) override {
auto* rfh = content::RenderFrameHost::FromID(render_frame_host_id);
if (!rfh) {
return;
}
auto* wc = content::WebContents::FromRenderFrameHost(rfh);
if (!wc) {
return;
}
auto* context = glic::GlicMediaContext::GetOrCreateFor(wc);
if (!context) {
return;
}
context->OnPeerConnectionAdded();
}
};
class GlicMediaIntegrationImpl : public glic::GlicMediaIntegration,
public base::SupportsUserData::Data {
public:
explicit GlicMediaIntegrationImpl(Profile*);
~GlicMediaIntegrationImpl() override = default;
// glic::GlicMediaIntegration
void AppendContext(
content::WebContents* web_contents,
optimization_guide::proto::ContentNode* context_root) override;
void OnPeerConnectionAddedForTesting(content::WebContents*) override;
void OnContextUpdated(glic::GlicMediaContext* context);
protected:
raw_ptr<Profile> profile_;
// Don't let the transcript grow unbounded.
static constexpr size_t max_size_bytes_ = 20000;
glic::GlicMediaPageCache page_cache_;
std::unique_ptr<GlicMediaPeerConnectionObserver> rtc_observer_;
};
class CaptionListenerImpl : public captions::CaptionControllerBase::Listener {
public:
explicit CaptionListenerImpl(Profile* profile) : profile_(profile) {}
~CaptionListenerImpl() override = default;
bool OnTranscription(content::WebContents* web_contents,
captions::CaptionBubbleContext*,
const media::SpeechRecognitionResult& result) override {
bool continue_transcribing = false;
if (auto* context = glic::GlicMediaContext::GetOrCreateFor(web_contents)) {
continue_transcribing = context->OnResult(result);
static_cast<GlicMediaIntegrationImpl*>(
glic::GlicMediaIntegration::GetFor(web_contents))
->OnContextUpdated(context);
}
return continue_transcribing;
}
void OnAudioStreamEnd(content::WebContents*,
captions::CaptionBubbleContext*) override {}
void OnLanguageIdentificationEvent(
content::WebContents*,
captions::CaptionBubbleContext*,
const media::mojom::LanguageIdentificationEventPtr&) override {}
private:
raw_ptr<Profile> profile_;
};
GlicMediaIntegrationImpl::GlicMediaIntegrationImpl(Profile* profile)
: profile_(profile),
rtc_observer_(std::make_unique<GlicMediaPeerConnectionObserver>()) {
auto* lc = captions::LiveCaptionControllerFactory::GetForProfile(profile_);
lc->AddListener(std::make_unique<CaptionListenerImpl>(profile));
// For now, enable the pref if we get this far. Do this after getting the
// Live Caption controller, since it resets the pref to false.
profile->GetPrefs()->SetBoolean(prefs::kHeadlessCaptionEnabled, true);
}
void GlicMediaIntegrationImpl::AppendContext(
content::WebContents* web_contents,
optimization_guide::proto::ContentNode* context_root) {
context_root->mutable_content_attributes()->set_attribute_type(
optimization_guide::proto::CONTENT_ATTRIBUTE_TEXT);
auto* context = glic::GlicMediaContext::GetIfExistsFor(web_contents);
std::string result;
if (context != nullptr) {
result = context->GetContext();
}
// Provide a default.
if (result.length() == 0) {
result = "There is no transcript available.";
}
// Trim to `max_size_bytes_`. Note that we should utf8-trim.
if (size_t result_size = result.length()) {
if (result_size > max_size_bytes_) {
// Remove the beginning of the result, leaving the end.
result = result.substr(result_size - max_size_bytes_);
}
}
// Include the entire context in one node. This could be split into multiple
// nodes too.
context_root->mutable_content_attributes()
->mutable_text_data()
->set_text_content(std::move(result));
}
void GlicMediaIntegrationImpl::OnContextUpdated(
glic::GlicMediaContext* context) {
page_cache_.PlaceAtFront(context);
}
void GlicMediaIntegrationImpl::OnPeerConnectionAddedForTesting(
content::WebContents* web_contents) {
auto id = web_contents->GetPrimaryMainFrame()->GetGlobalId();
rtc_observer_->OnPeerConnectionAdded(id, /*lid=*/0, /*pid=*/{}, /*url=*/"",
/*rtc_configuration=*/"");
}
} // namespace
namespace glic {
// static
GlicMediaIntegration* GlicMediaIntegration::GetFor(content::WebContents* wc) {
// This should also check the pref, once it's not toggled automatically.
// We'll want to install a pref listener, and possibly clean up if the pref
// is switched off after construction.
if (!wc || !captions::IsHeadlessCaptionFeatureSupported()) {
return nullptr;
}
Profile* profile = Profile::FromBrowserContext(wc->GetBrowserContext());
if (!profile) {
return nullptr;
}
auto* data = static_cast<GlicMediaIntegrationImpl*>(
profile->GetUserData(kGlicMediaIntegrationKey));
if (!data) {
auto new_data = std::make_unique<GlicMediaIntegrationImpl>(profile);
data = new_data.get();
profile->SetUserData(kGlicMediaIntegrationKey, std::move(new_data));
}
return data;
}
} // namespace glic
|