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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
// Copyright 2018 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/cast_mirroring_service_host.h"
#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/json/json_reader.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/bind_post_task.h"
#include "base/test/test_timeouts.h"
#include "build/build_config.h"
#include "chrome/browser/media/router/discovery/access_code/access_code_cast_feature.h"
#include "chrome/browser/media/router/media_router_feature.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/alert/tab_alert.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/browser/ui/tabs/tab_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/media_router/common/pref_names.h"
#include "components/mirroring/mojom/cast_message_channel.mojom.h"
#include "components/mirroring/mojom/session_observer.mojom.h"
#include "components/mirroring/mojom/session_parameters.mojom.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "media/base/media_switches.h"
#include "media/capture/mojom/video_capture.mojom.h"
#include "media/capture/mojom/video_capture_buffer.mojom.h"
#include "media/capture/mojom/video_capture_types.mojom.h"
#include "media/capture/video_capture_types.h"
#include "media/mojo/mojom/audio_data_pipe.mojom.h"
#include "media/mojo/mojom/audio_input_stream.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gmock/include/gmock/gmock.h"
using testing::_;
using testing::InvokeWithoutArgs;
using testing::Return;
namespace mirroring {
namespace {
media::VideoCaptureParams DefaultVideoCaptureParams() {
constexpr gfx::Size kMaxCaptureSize = gfx::Size(320, 320);
constexpr int kMaxFramesPerSecond = 60;
gfx::Size capture_size = kMaxCaptureSize;
media::VideoCaptureParams params;
params.requested_format = media::VideoCaptureFormat(
capture_size, kMaxFramesPerSecond, media::PIXEL_FORMAT_I420);
params.resolution_change_policy =
media::ResolutionChangePolicy::FIXED_ASPECT_RATIO;
return params;
}
content::DesktopMediaID BuildMediaIdForTabMirroring(
content::WebContents* target_web_contents) {
DCHECK(target_web_contents);
content::DesktopMediaID media_id;
content::RenderFrameHost* const main_frame =
target_web_contents->GetPrimaryMainFrame();
const int process_id = main_frame->GetProcess()->GetDeprecatedID();
const int frame_id = main_frame->GetRoutingID();
media_id.type = content::DesktopMediaID::TYPE_WEB_CONTENTS;
media_id.web_contents_id = content::WebContentsMediaCaptureId(
process_id, frame_id, true /* disable_local_echo */);
return media_id;
}
class MockVideoCaptureObserver final
: public media::mojom::VideoCaptureObserver {
public:
explicit MockVideoCaptureObserver(
mojo::PendingRemote<media::mojom::VideoCaptureHost> host)
: host_(std::move(host)) {}
MockVideoCaptureObserver(const MockVideoCaptureObserver&) = delete;
MockVideoCaptureObserver& operator=(const MockVideoCaptureObserver&) = delete;
MOCK_METHOD1(OnBufferCreatedCall, void(int buffer_id));
MOCK_METHOD1(OnBufferReadyCall, void(int buffer_id));
MOCK_METHOD1(OnBufferDestroyedCall, void(int buffer_id));
MOCK_METHOD1(OnStateChangedCall, void(media::mojom::VideoCaptureState state));
MOCK_METHOD1(OnVideoCaptureErrorCall, void(media::VideoCaptureError error));
MOCK_METHOD1(OnFrameDropped, void(media::VideoCaptureFrameDropReason reason));
// media::mojom::VideoCaptureObserver implementation.
void OnNewBuffer(int32_t buffer_id,
media::mojom::VideoBufferHandlePtr buffer_handle) override {
EXPECT_EQ(buffers_.find(buffer_id), buffers_.end());
EXPECT_EQ(frame_infos_.find(buffer_id), frame_infos_.end());
buffers_[buffer_id] = std::move(buffer_handle);
OnBufferCreatedCall(buffer_id);
}
void OnBufferReady(media::mojom::ReadyBufferPtr buffer) override {
EXPECT_TRUE(buffers_.find(buffer->buffer_id) != buffers_.end());
EXPECT_EQ(frame_infos_.find(buffer->buffer_id), frame_infos_.end());
frame_infos_[buffer->buffer_id] = std::move(buffer->info);
OnBufferReadyCall(buffer->buffer_id);
}
void OnBufferDestroyed(int32_t buffer_id) override {
// The consumer should have finished consuming the buffer before it is being
// destroyed.
EXPECT_TRUE(frame_infos_.find(buffer_id) == frame_infos_.end());
const auto iter = buffers_.find(buffer_id);
EXPECT_TRUE(iter != buffers_.end());
buffers_.erase(iter);
OnBufferDestroyedCall(buffer_id);
}
void OnNewSubCaptureTargetVersion(
uint32_t sub_capture_target_version) override {}
void OnStateChanged(media::mojom::VideoCaptureResultPtr result) override {
if (result->which() == media::mojom::VideoCaptureResult::Tag::kState)
OnStateChangedCall(result->get_state());
else
OnVideoCaptureErrorCall(result->get_error_code());
}
void Start() {
host_->Start(device_id_, session_id_, DefaultVideoCaptureParams(),
receiver_.BindNewPipeAndPassRemote());
}
void Stop() { host_->Stop(device_id_); }
void RequestRefreshFrame() { host_->RequestRefreshFrame(device_id_); }
private:
mojo::Remote<media::mojom::VideoCaptureHost> host_;
mojo::Receiver<media::mojom::VideoCaptureObserver> receiver_{this};
base::flat_map<int, media::mojom::VideoBufferHandlePtr> buffers_;
base::flat_map<int, media::mojom::VideoFrameInfoPtr> frame_infos_;
const base::UnguessableToken device_id_ = base::UnguessableToken::Create();
const base::UnguessableToken session_id_ = base::UnguessableToken::Create();
};
class MockCastMessageChannel : public mojom::CastMessageChannel {
public:
// mojom::CastMessageChannel mock implementation (outbound messages).
MOCK_METHOD(void, OnMessage, (mojom::CastMessagePtr));
mojo::Receiver<mojom::CastMessageChannel>* GetChannelReceiver() {
return &channel_receiver_;
}
private:
mojo::Receiver<mojom::CastMessageChannel> channel_receiver_{this};
};
} // namespace
class CastMirroringServiceHostBrowserTest
: public InProcessBrowserTest,
public mojom::SessionObserver,
public mojom::CastMessageChannel,
public mojom::AudioStreamCreatorClient {
public:
CastMirroringServiceHostBrowserTest() = default;
CastMirroringServiceHostBrowserTest(
const CastMirroringServiceHostBrowserTest&) = delete;
CastMirroringServiceHostBrowserTest& operator=(
const CastMirroringServiceHostBrowserTest&) = delete;
~CastMirroringServiceHostBrowserTest() override = default;
protected:
// Starts a tab mirroring session.
void StartTabMirroring() {
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(web_contents);
host_ = std::make_unique<CastMirroringServiceHost>(
BuildMediaIdForTabMirroring(web_contents));
mojo::PendingRemote<mojom::SessionObserver> observer;
observer_receiver_.Bind(observer.InitWithNewPipeAndPassReceiver());
mojo::PendingRemote<mojom::CastMessageChannel> outbound_channel;
outbound_channel_receiver_ = std::make_unique<MockCastMessageChannel>();
outbound_channel_receiver_->GetChannelReceiver()->Bind(
outbound_channel.InitWithNewPipeAndPassReceiver());
auto session_params = mojom::SessionParameters::New();
session_params->source_id = "SourceID";
host_->Start(std::move(session_params), std::move(observer),
std::move(outbound_channel),
inbound_channel_.BindNewPipeAndPassReceiver(), "Sink Name");
}
// Starts a tab mirroring session, and sets a target playout delay.
// `media_source_delay` simulates the mirroring delay that is set from a media
// source when starting a mirroring session, i.e. set by a site-initiated
// mirroring source. `feature_delay` is the value that media_router_feature's
// `GetCastMirroringPlayoutDelay` is expected to return.
void StartTabMirroringWithTargetPlayoutDelay(
base::TimeDelta media_source_delay) {
int expected_delay_ms = media_source_delay.InMilliseconds();
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(web_contents);
host_ = std::make_unique<CastMirroringServiceHost>(
BuildMediaIdForTabMirroring(web_contents));
mojo::PendingRemote<mojom::SessionObserver> observer;
observer_receiver_.Bind(observer.InitWithNewPipeAndPassReceiver());
mojo::PendingRemote<mojom::CastMessageChannel> outbound_channel;
outbound_channel_receiver_ = std::make_unique<MockCastMessageChannel>();
outbound_channel_receiver_->GetChannelReceiver()->Bind(
outbound_channel.InitWithNewPipeAndPassReceiver());
auto session_params = mojom::SessionParameters::New();
session_params->source_id = "SourceID";
session_params->target_playout_delay = media_source_delay;
base::RunLoop run_loop;
EXPECT_CALL(*outbound_channel_receiver_, OnMessage(_))
.WillOnce(
testing::Invoke([expected_delay_ms, &run_loop](
mirroring::mojom::CastMessagePtr message) {
const std::optional<base::Value> root_or_error =
base::JSONReader::Read(message->json_format_data);
ASSERT_TRUE(root_or_error);
const base::Value::Dict& root = root_or_error->GetDict();
const std::string* type = root.FindString("type");
ASSERT_TRUE(type);
if (*type == "OFFER") {
const base::Value::Dict* offer = root.FindDict("offer");
EXPECT_TRUE(offer);
const base::Value::List* streams =
offer->FindList("supportedStreams");
for (auto& stream : *streams) {
const base::Value::Dict& stream_dict = stream.GetDict();
const int stream_target_delay =
stream_dict.FindInt("targetDelay").value();
EXPECT_EQ(stream_target_delay, expected_delay_ms);
}
}
run_loop.Quit();
}));
host_->Start(std::move(session_params), std::move(observer),
std::move(outbound_channel),
inbound_channel_.BindNewPipeAndPassReceiver(), "Sink Name");
run_loop.Run();
}
void EnableAccessCodeCast() {
ASSERT_FALSE(media_router::GetAccessCodeCastEnabledPref(
browser()->tab_strip_model()->profile()));
browser()->tab_strip_model()->profile()->GetPrefs()->SetBoolean(
media_router::prefs::kAccessCodeCastEnabled, true);
ASSERT_TRUE(media_router::GetAccessCodeCastEnabledPref(
browser()->tab_strip_model()->profile()));
}
void SwitchTabSource() {
ASSERT_TRUE(host_->tab_switching_ui_enabled_);
browser()->tab_strip_model()->delegate()->AddTabAt(GURL(), -1, true);
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(web_contents);
content::FrameTreeNodeId web_contents_source_tab_id =
web_contents->GetPrimaryMainFrame()->GetFrameTreeNodeId();
ASSERT_NE(host_->GetTabSourceId(), web_contents_source_tab_id);
ASSERT_NE(host_->web_contents(), web_contents);
host_->SwitchMirroringSourceTab(BuildMediaIdForTabMirroring(web_contents),
/*captured_surface_control_active=*/false);
ASSERT_EQ(host_->web_contents(), web_contents);
ASSERT_EQ(host_->GetTabSourceId(), web_contents_source_tab_id);
}
void GetVideoCaptureHost() {
mojo::PendingRemote<media::mojom::VideoCaptureHost> video_capture_host;
static_cast<mojom::ResourceProvider*>(host_.get())
->GetVideoCaptureHost(
video_capture_host.InitWithNewPipeAndPassReceiver());
video_frame_receiver_ = std::make_unique<MockVideoCaptureObserver>(
std::move(video_capture_host));
}
void StartVideoCapturing() {
base::RunLoop run_loop;
EXPECT_CALL(*video_frame_receiver_,
OnStateChangedCall(media::mojom::VideoCaptureState::STARTED))
.WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
video_frame_receiver_->Start();
run_loop.Run();
}
void PauseMirroring() {
base::RunLoop run_loop;
host_->Pause(run_loop.QuitClosure());
run_loop.Run();
}
void ResumeMirroring() {
base::RunLoop run_loop;
host_->Resume(run_loop.QuitClosure());
run_loop.Run();
}
void StopMirroring() {
if (video_frame_receiver_) {
base::RunLoop run_loop;
EXPECT_CALL(*video_frame_receiver_,
OnStateChangedCall(media::mojom::VideoCaptureState::ENDED))
.WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
video_frame_receiver_->Stop();
run_loop.Run();
}
host_.reset();
}
void RequestRefreshFrame() {
base::RunLoop run_loop;
EXPECT_CALL(*video_frame_receiver_, OnBufferReadyCall(_))
.WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit))
.WillRepeatedly(Return());
video_frame_receiver_->RequestRefreshFrame();
run_loop.Run();
}
void CreateAudioLoopbackStream() {
constexpr int kTotalSegments = 1;
constexpr int kAudioTimebase = 48000;
media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
media::ChannelLayoutConfig::Stereo(),
kAudioTimebase, kAudioTimebase / 100);
base::RunLoop run_loop;
EXPECT_CALL(*this, OnAudioStreamCreated())
.WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
host_->CreateAudioStream(audio_client_receiver_.BindNewPipeAndPassRemote(),
params, kTotalSegments);
run_loop.Run();
}
// InProcessBrowserTest override.
void SetUp() override { InProcessBrowserTest::SetUp(); }
std::vector<tabs::TabAlert> GetTabAlertStatesForContents(
content::WebContents* web_contents) {
return GetTabAlertStatesForTab(
tabs::TabInterface::GetFromContents(web_contents));
}
private:
// mojom::SessionObserver mocks.
MOCK_METHOD(void, OnError, (mojom::SessionError));
MOCK_METHOD(void, DidStart, ());
MOCK_METHOD(void, DidStop, ());
MOCK_METHOD(void, LogInfoMessage, (const std::string&));
MOCK_METHOD(void, LogErrorMessage, (const std::string&));
MOCK_METHOD(void, OnSourceChanged, ());
MOCK_METHOD(void, OnRemotingStateChanged, (bool is_remoting));
// mojom::CastMessageChannel mock implementation (inbound messages).
MOCK_METHOD(void, OnMessage, (mojom::CastMessagePtr));
// mojom::AudioStreamCreatorClient mocks.
MOCK_METHOD(void, OnAudioStreamCreated, ());
void StreamCreated(
mojo::PendingRemote<media::mojom::AudioInputStream> stream,
mojo::PendingReceiver<media::mojom::AudioInputStreamClient>
client_receiver,
media::mojom::ReadWriteAudioDataPipePtr data_pipe) override {
EXPECT_TRUE(stream);
EXPECT_TRUE(client_receiver);
EXPECT_TRUE(data_pipe);
OnAudioStreamCreated();
}
mojo::Receiver<mojom::SessionObserver> observer_receiver_{this};
mojo::Receiver<mojom::AudioStreamCreatorClient> audio_client_receiver_{this};
mojo::Remote<mojom::CastMessageChannel> inbound_channel_;
std::unique_ptr<CastMirroringServiceHost> host_;
std::unique_ptr<MockVideoCaptureObserver> video_frame_receiver_;
std::unique_ptr<MockCastMessageChannel> outbound_channel_receiver_;
};
IN_PROC_BROWSER_TEST_F(CastMirroringServiceHostBrowserTest, CaptureTabVideo) {
StartTabMirroring();
GetVideoCaptureHost();
StartVideoCapturing();
RequestRefreshFrame();
StopMirroring();
}
IN_PROC_BROWSER_TEST_F(CastMirroringServiceHostBrowserTest, CaptureTabAudio) {
StartTabMirroring();
CreateAudioLoopbackStream();
StopMirroring();
}
IN_PROC_BROWSER_TEST_F(CastMirroringServiceHostBrowserTest, TabIndicator) {
content::WebContents* const contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_THAT(GetTabAlertStatesForContents(contents), ::testing::IsEmpty());
// A TabStripModelObserver that quits the MessageLoop whenever the
// UI's model is sent an event that might change the indicator status.
class IndicatorChangeObserver : public TabStripModelObserver {
public:
explicit IndicatorChangeObserver(Browser* browser) : browser_(browser) {
browser_->tab_strip_model()->AddObserver(this);
}
void TabChangedAt(content::WebContents* contents,
int index,
TabChangeType change_type) override {
std::move(on_tab_changed_).Run();
}
void WaitForTabChange() {
base::RunLoop run_loop;
on_tab_changed_ = run_loop.QuitClosure();
run_loop.Run();
}
private:
const raw_ptr<Browser> browser_;
base::OnceClosure on_tab_changed_;
};
IndicatorChangeObserver observer(browser());
ASSERT_THAT(GetTabAlertStatesForContents(contents), ::testing::IsEmpty());
StartTabMirroring();
// Run the browser until the indicator turns on.
const base::TimeTicks start_time = base::TimeTicks::Now();
while (!base::Contains(GetTabAlertStatesForContents(contents),
tabs::TabAlert::TAB_CAPTURING)) {
if (base::TimeTicks::Now() - start_time >
TestTimeouts::action_max_timeout()) {
EXPECT_THAT(GetTabAlertStatesForContents(contents),
::testing::Contains(tabs::TabAlert::TAB_CAPTURING));
return;
}
observer.WaitForTabChange();
}
StopMirroring();
}
IN_PROC_BROWSER_TEST_F(CastMirroringServiceHostBrowserTest, PauseSession) {
EnableAccessCodeCast();
StartTabMirroring();
GetVideoCaptureHost();
StartVideoCapturing();
PauseMirroring();
ResumeMirroring();
StopMirroring();
}
IN_PROC_BROWSER_TEST_F(CastMirroringServiceHostBrowserTest,
TabMirrorWithPresetPlayoutDelay) {
StartTabMirroringWithTargetPlayoutDelay(base::Milliseconds(200));
GetVideoCaptureHost();
StartVideoCapturing();
RequestRefreshFrame();
StopMirroring();
}
class CastMirroringServiceHostBrowserTestTabSwitcher
: public CastMirroringServiceHostBrowserTest {
public:
CastMirroringServiceHostBrowserTestTabSwitcher() = default;
CastMirroringServiceHostBrowserTestTabSwitcher(
const CastMirroringServiceHostBrowserTestTabSwitcher&) = delete;
CastMirroringServiceHostBrowserTestTabSwitcher& operator=(
const CastMirroringServiceHostBrowserTestTabSwitcher&) = delete;
~CastMirroringServiceHostBrowserTestTabSwitcher() override = default;
};
IN_PROC_BROWSER_TEST_F(CastMirroringServiceHostBrowserTestTabSwitcher,
SwitchTabSource) {
EnableAccessCodeCast();
StartTabMirroring();
GetVideoCaptureHost();
StartVideoCapturing();
SwitchTabSource();
GetVideoCaptureHost();
StartVideoCapturing();
StopMirroring();
}
} // namespace mirroring
|