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
|
// Copyright 2023 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/media/router/mojo/media_router_debugger_impl.h"
#include <utility>
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/media/router/discovery/access_code/access_code_cast_feature.h"
#include "chrome/browser/profiles/profile.h"
#include "components/media_router/browser/media_router.h"
#include "components/media_router/browser/media_router_factory.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "media/base/media_switches.h"
#include "media/cast/constants.h"
namespace media_router {
MediaRouterDebuggerImpl::MediaRouterDebuggerImpl(
content::BrowserContext* context) {
DETACH_FROM_SEQUENCE(sequence_checker_);
auto* profile = Profile::FromBrowserContext(context);
is_rtcp_reports_enabled_ =
profile ? GetAccessCodeCastEnabledPref(profile) : false;
receivers_.set_disconnect_handler(
base::BindRepeating(&MediaRouterDebuggerImpl::LogMirroringStats,
weak_ptr_factory_.GetWeakPtr()));
}
MediaRouterDebuggerImpl::~MediaRouterDebuggerImpl() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
// static.
MediaRouterDebugger* MediaRouterDebuggerImpl::GetForFrameTreeNode(
content::FrameTreeNodeId frame_tree_node_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto* web_contents =
content::WebContents::FromFrameTreeNodeId(frame_tree_node_id);
if (!web_contents) {
return nullptr;
}
auto* media_router = MediaRouterFactory::GetApiForBrowserContextIfExists(
web_contents->GetBrowserContext());
return media_router ? &media_router->GetDebugger() : nullptr;
}
base::Value::Dict MediaRouterDebuggerImpl::GetMirroringStats() {
if (!ShouldFetchMirroringStats()) {
return base::Value::Dict();
}
return most_recent_mirroring_stats_.Clone();
}
void MediaRouterDebuggerImpl::EnableRtcpReports() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
is_rtcp_reports_enabled_ = true;
}
void MediaRouterDebuggerImpl::DisableRtcpReports() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
is_rtcp_reports_enabled_ = false;
}
bool MediaRouterDebuggerImpl::ShouldFetchMirroringStats() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return is_rtcp_reports_enabled_ ||
base::FeatureList::IsEnabled(media::kEnableRtcpReporting);
}
void MediaRouterDebuggerImpl::AddObserver(MirroringStatsObserver& obs) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.AddObserver(&obs);
}
void MediaRouterDebuggerImpl::RemoveObserver(MirroringStatsObserver& obs) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.RemoveObserver(&obs);
}
void MediaRouterDebuggerImpl::ShouldFetchMirroringStats(
ShouldFetchMirroringStatsCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::move(callback).Run(ShouldFetchMirroringStats());
}
void MediaRouterDebuggerImpl::OnMirroringStats(base::Value json_stats) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
json_stats.is_dict()
? NotifyGetMirroringStats(std::move(json_stats).TakeDict())
: NotifyGetMirroringStats(base::Value::Dict());
}
void MediaRouterDebuggerImpl::BindReceiver(
mojo::PendingReceiver<mojom::Debugger> receiver) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
receivers_.Add(this, std::move(receiver));
}
void MediaRouterDebuggerImpl::NotifyGetMirroringStats(
base::Value::Dict json_logs) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!ShouldFetchMirroringStats()) {
return;
}
for (MirroringStatsObserver& observer : observers_) {
observer.OnMirroringStatsUpdated(json_logs);
}
most_recent_mirroring_stats_ = std::move(json_logs);
}
void MediaRouterDebuggerImpl::LogMirroringStats() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!ShouldFetchMirroringStats()) {
return;
}
VLOG(1) << "Mirroring stats for the most recent session: "
<< most_recent_mirroring_stats_.DebugString();
}
} // namespace media_router
|