Skip to content

Commit

Permalink
[Media Router] Use libcast API for streaming app IDs.
Browse files Browse the repository at this point in the history
This converts Cast Sender code to use the public API for Cast Streaming
app IDs instead of duplicating constants in multiple repos.

Bug: b/199543768
Change-Id: Ic4ff8867f7ed6392dadefbe1d7b1b18f72e1a011
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3163378
Commit-Queue: mark a. foltz <mfoltz@chromium.org>
Reviewed-by: Takumi Fujimoto <takumif@chromium.org>
Cr-Commit-Position: refs/heads/main@{#922556}
  • Loading branch information
markafoltz authored and Chromium LUCI CQ committed Sep 17, 2021
1 parent 438f4c9 commit bc6d983
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 36 deletions.
1 change: 1 addition & 0 deletions chrome/browser/media/router/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ source_set("unittests") {
"//chrome/test:test_support",
"//components/sync_preferences:test_support",
"//content/test:test_support",
"//third_party/openscreen/src/cast/common:public",
]
}

Expand Down
1 change: 1 addition & 0 deletions chrome/browser/media/router/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include_rules = [
"+services/network",
"+third_party/openscreen/src/cast/common/certificate/proto",
"+third_party/openscreen/src/cast/common/channel/proto",
"+third_party/openscreen/src/cast/common/public",
"+third_party/openscreen/src/osp/public",
"+third_party/openscreen/src/platform/base",
"+third_party/openscreen/src/platform/api",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/openscreen/src/cast/common/public/cast_streaming_app_ids.h"

using base::test::IsJson;
using base::test::ParseJson;
Expand Down Expand Up @@ -149,7 +150,9 @@ class CastActivityManagerTest : public testing::Test,
public:
CastActivityManagerTest()
: socket_service_(content::GetUIThreadTaskRunner({})),
message_handler_(&socket_service_) {
message_handler_(&socket_service_),
cast_streaming_app_id_(
openscreen::cast::GetCastStreamingAudioVideoAppId()) {
media_sink_service_.AddOrUpdateSink(sink_);
socket_.set_id(kChannelId);
}
Expand Down Expand Up @@ -349,7 +352,7 @@ class CastActivityManagerTest : public testing::Test,
}

void LaunchNonSdkMirroringSession() {
CallLaunchSessionSuccess(kCastStreamingAppId, /* app_params */ "",
CallLaunchSessionSuccess(cast_streaming_app_id_, /* app_params */ "",
/* client_id */ "");
mirroring_activity_callback_ =
base::BindLambdaForTesting([this](MockMirroringActivity* activity) {
Expand All @@ -363,7 +366,7 @@ class CastActivityManagerTest : public testing::Test,
}

void LaunchCastSdkMirroringSession() {
CallLaunchSessionSuccess(kCastStreamingAppId, kAppParams, kClientId);
CallLaunchSessionSuccess(cast_streaming_app_id_, kAppParams, kClientId);
mirroring_activity_callback_ =
base::BindLambdaForTesting([this](MockMirroringActivity* activity) {
EXPECT_CALL(*activity, OnSessionSet).WillOnce([this]() {
Expand Down Expand Up @@ -397,7 +400,7 @@ class CastActivityManagerTest : public testing::Test,

void AddRemoteMirroringSession() {
auto session =
CastSession::From(sink2_, MakeReceiverStatus(kCastStreamingAppId));
CastSession::From(sink2_, MakeReceiverStatus(cast_streaming_app_id_));
manager_->OnSessionAddedOrUpdated(sink2_, *session);
SetSessionForTest(sink2_.id(), std::move(session));
DCHECK(mirroring_activity_);
Expand Down Expand Up @@ -503,6 +506,7 @@ class CastActivityManagerTest : public testing::Test,
cast_channel::Result stop_session_callback_arg_ = cast_channel::Result::kOk;
NiceMock<MockLogger> logger_;
mojom::RoutePresentationConnectionPtr presentation_connections_;
const std::string cast_streaming_app_id_;
};

TEST_F(CastActivityManagerTest, LaunchAppSession) {
Expand All @@ -528,9 +532,9 @@ TEST_F(CastActivityManagerTest, LaunchMirroringSessionViaCastSdk) {
TEST_F(CastActivityManagerTest, LaunchSiteInitiatedMirroringSession) {
// For a session initiated by a website with the mirroring source we should be
// establishing a presentation connection, even if the client ID isn't set.
CallLaunchSessionSuccess(kCastStreamingAppId, /*app_params*/ "",
CallLaunchSessionSuccess(cast_streaming_app_id_, /*app_params*/ "",
/*client_id*/ "");
ReceiveLaunchSuccessResponseFromReceiver(kCastStreamingAppId);
ReceiveLaunchSuccessResponseFromReceiver(cast_streaming_app_id_);
EXPECT_FALSE(presentation_connections_.is_null());
EXPECT_EQ(RouteControllerType::kMirroring, route_->controller_type());
}
Expand Down Expand Up @@ -684,7 +688,7 @@ TEST_F(CastActivityManagerTest, UpdateNewlyCreatedMirroringSession) {
LaunchCastSdkMirroringSession();

ASSERT_TRUE(mirroring_activity_);
auto session = MakeSession(kCastStreamingAppId);
auto session = MakeSession(cast_streaming_app_id_);
ExpectSingleRouteUpdate();
manager_->OnSessionAddedOrUpdated(sink_, *session);
RunUntilIdle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ TEST_F(MirroringActivityTest, MirrorTab) {

TEST_F(MirroringActivityTest, CreateMojoBindingsForTabWithCastAppUrl) {
base::HistogramTester uma_recorder;
GURL url(kMirroringAppUri);
EXPECT_CALL(media_router_, GetMirroringServiceHostForTab(kTabId, _));
MediaSource source = MediaSource::ForPresentationUrl(url);
auto site_initiated_mirroring_source =
CastMediaSource::ForSiteInitiatedMirroring();
MediaSource source(site_initiated_mirroring_source->source_id());
ASSERT_TRUE(source.IsCastPresentationUrl());
MakeActivity(source, kTabId);

Expand Down
1 change: 1 addition & 0 deletions chromecast/cast_core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ if (is_linux) {
"//content/public/browser",
"//mojo/public/cpp/bindings",
"//third_party/blink/public/common:headers",
"//third_party/openscreen/src/cast/common:public",
]
}
}
Expand Down
7 changes: 2 additions & 5 deletions chromecast/cast_core/runtime_application_dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "third_party/grpc/src/include/grpcpp/create_channel.h"
#include "third_party/grpc/src/include/grpcpp/server_builder.h"
#include "third_party/openscreen/src/cast/cast_core/api/common/application_config.pb.h"
#include "third_party/openscreen/src/cast/streaming/constants.h"
#include "third_party/openscreen/src/cast/common/public/cast_streaming_app_ids.h"

namespace chromecast {
namespace {
Expand Down Expand Up @@ -125,10 +125,7 @@ void RuntimeApplicationDispatcher::LoadApplication(
GrpcMethod* callback) {
const std::string& app_id = request.application_config().app_id();

// TODO(issuetracker.google.com/issues/199543768): Use an openscreen-defined
// function instead of constants.
if (app_id == openscreen::cast::kMirroringAppId ||
app_id == openscreen::cast::kMirroringAudioOnlyAppId) {
if (openscreen::cast::IsCastStreamingAppId(app_id)) {
// Deliberately copy |network_context_getter_|.
app_ = std::make_unique<StreamingRuntimeApplication>(
web_service_.get(), task_runner_, network_context_getter_);
Expand Down
6 changes: 5 additions & 1 deletion components/media_router/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ static_library("common") {
"providers/cast/cast_media_source.h",
]

deps += [ "//components/cast_channel:cast_channel" ]
deps += [
"//components/cast_channel:cast_channel",
"//third_party/openscreen/src/cast/common:public",
]
}
}

Expand Down Expand Up @@ -95,6 +98,7 @@ source_set("unit_tests") {
deps += [
"//components/cast_channel:cast_channel",
"//components/media_router/common/mojom:media_router_test_interfaces",
"//third_party/openscreen/src/cast/common:public",
]
}
}
1 change: 1 addition & 0 deletions components/media_router/common/DEPS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include_rules = [
"+url",
"+mojo",
"+third_party/openscreen/src/cast/common/public",
]
4 changes: 0 additions & 4 deletions components/media_router/common/media_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ constexpr char kRemotePlaybackPresentationUrlScheme[] = "remote-playback";
constexpr char kLegacyCastPresentationUrlPrefix[] =
"https://google.com/cast#__castAppId__=";

// A Cast SDK enabled website (e.g. Google Slides) may use the mirroring app ID
// rather than the tab mirroring URN.
constexpr char kMirroringAppUri[] = "cast:0F5096E8";

// Strings used in presentation IDs by the Cast SDK implementation.
// TODO(takumif): Move them out of this file, since they are not directly
// related to MediaSource.
Expand Down
28 changes: 20 additions & 8 deletions components/media_router/common/providers/cast/cast_media_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
Expand All @@ -18,6 +19,7 @@
#include "components/media_router/common/media_source.h"
#include "net/base/escape.h"
#include "net/base/url_util.h"
#include "third_party/openscreen/src/cast/common/public/cast_streaming_app_ids.h"
#include "url/gurl.h"
#include "url/url_util.h"

Expand Down Expand Up @@ -390,8 +392,13 @@ bool IsAutoJoinAllowed(AutoJoinPolicy policy,
}

bool IsSiteInitiatedMirroringSource(const MediaSource::Id& source_id) {
return base::StartsWith(source_id, kMirroringAppUri,
base::CompareCase::SENSITIVE);
// A Cast SDK enabled website (e.g. Google Slides) may use the mirroring app
// ID rather than the tab mirroring URN.
return base::StartsWith(
source_id,
base::StrCat(
{"cast:", openscreen::cast::GetCastStreamingAudioVideoAppId()}),
base::CompareCase::SENSITIVE);
}

CastAppInfo::CastAppInfo(
Expand All @@ -405,13 +412,14 @@ CastAppInfo::CastAppInfo(const CastAppInfo& other) = default;

// static
CastAppInfo CastAppInfo::ForCastStreaming() {
return CastAppInfo(kCastStreamingAppId, {CastDeviceCapability::VIDEO_OUT,
CastDeviceCapability::AUDIO_OUT});
return CastAppInfo(
openscreen::cast::GetCastStreamingAudioVideoAppId(),
{CastDeviceCapability::VIDEO_OUT, CastDeviceCapability::AUDIO_OUT});
}

// static
CastAppInfo CastAppInfo::ForCastStreamingAudio() {
return CastAppInfo(kCastStreamingAudioAppId,
return CastAppInfo(openscreen::cast::GetCastStreamingAudioOnlyAppId(),
{CastDeviceCapability::AUDIO_OUT});
}

Expand Down Expand Up @@ -453,6 +461,11 @@ std::unique_ptr<CastMediaSource> CastMediaSource::FromAppId(
return FromMediaSourceId(kCastPresentationUrlScheme + (":" + app_id));
}

// static
std::unique_ptr<CastMediaSource> CastMediaSource::ForSiteInitiatedMirroring() {
return FromAppId(openscreen::cast::GetCastStreamingAudioVideoAppId());
}

CastMediaSource::CastMediaSource(const MediaSource::Id& source_id,
const std::vector<CastAppInfo>& app_infos,
AutoJoinPolicy auto_join_policy,
Expand Down Expand Up @@ -480,7 +493,7 @@ bool CastMediaSource::ContainsAnyAppFrom(
}

bool CastMediaSource::ContainsStreamingApp() const {
return ContainsAnyAppFrom({kCastStreamingAppId, kCastStreamingAudioAppId});
return ContainsAnyAppFrom(openscreen::cast::GetCastStreamingAppIds());
}

std::vector<std::string> CastMediaSource::GetAppIds() const {
Expand All @@ -496,8 +509,7 @@ bool CastMediaSource::ProvidesStreamingAudioCapture() const {
return false;
}
for (const auto& info : app_infos_) {
if ((info.app_id == kCastStreamingAppId ||
info.app_id == kCastStreamingAudioAppId) &&
if (openscreen::cast::IsCastStreamingAppId(info.app_id) &&
info.required_capabilities.Has(CastDeviceCapability::AUDIO_OUT)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ using cast_channel::ReceiverAppType;

namespace media_router {

static constexpr char kCastStreamingAppId[] = "0F5096E8";
static constexpr char kCastStreamingAudioAppId[] = "85CDB22F";

// Placeholder app ID advertised by the multizone leader in a receiver status
// message.
static constexpr char kMultizoneLeaderAppId[] = "MultizoneLeader";
Expand Down Expand Up @@ -134,7 +131,7 @@ bool IsAutoJoinAllowed(AutoJoinPolicy policy,
const url::Origin& origin2,
int tab_id2);

// Returns true if |source_id| is a valid origin for site-initiated mirroring.
// Returns true if |source_id| is valid for site-initiated mirroring.
bool IsSiteInitiatedMirroringSource(const MediaSource::Id& source_id);

// Represents a MediaSource parsed into structured, Cast specific data. The
Expand All @@ -152,6 +149,8 @@ class CastMediaSource {

static std::unique_ptr<CastMediaSource> FromAppId(const std::string& app_id);

static std::unique_ptr<CastMediaSource> ForSiteInitiatedMirroring();

CastMediaSource(const MediaSource::Id& source_id,
const std::vector<CastAppInfo>& app_infos,
AutoJoinPolicy auto_join_policy = AutoJoinPolicy::kPageScoped,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "components/cast_channel/cast_message_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/openscreen/src/cast/common/public/cast_streaming_app_ids.h"

using cast_channel::CastDeviceCapability;
using cast_channel::ReceiverAppType;
Expand Down Expand Up @@ -119,8 +120,10 @@ TEST(CastMediaSourceTest, FromPresentationURL) {
ASSERT_TRUE(source);
EXPECT_EQ(source_id, source->source_id());
ASSERT_EQ(2u, source->app_infos().size());
EXPECT_EQ(kCastStreamingAppId, source->app_infos()[0].app_id);
EXPECT_EQ(kCastStreamingAudioAppId, source->app_infos()[1].app_id);
EXPECT_EQ(openscreen::cast::GetCastStreamingAudioVideoAppId(),
source->app_infos()[0].app_id);
EXPECT_EQ(openscreen::cast::GetCastStreamingAudioOnlyAppId(),
source->app_infos()[1].app_id);
EXPECT_TRUE(source->client_id().empty());
EXPECT_EQ(kDefaultLaunchTimeout, source->launch_timeout());
EXPECT_EQ(AutoJoinPolicy::kPageScoped, source->auto_join_policy());
Expand All @@ -136,8 +139,10 @@ TEST(CastMediaSourceTest, FromMirroringURN) {
ASSERT_TRUE(source);
EXPECT_EQ(source_id, source->source_id());
ASSERT_EQ(2u, source->app_infos().size());
EXPECT_EQ(kCastStreamingAppId, source->app_infos()[0].app_id);
EXPECT_EQ(kCastStreamingAudioAppId, source->app_infos()[1].app_id);
EXPECT_EQ(openscreen::cast::GetCastStreamingAudioVideoAppId(),
source->app_infos()[0].app_id);
EXPECT_EQ(openscreen::cast::GetCastStreamingAudioOnlyAppId(),
source->app_infos()[1].app_id);
EXPECT_TRUE(source->client_id().empty());
EXPECT_EQ(kDefaultLaunchTimeout, source->launch_timeout());
EXPECT_EQ(AutoJoinPolicy::kPageScoped, source->auto_join_policy());
Expand All @@ -153,7 +158,8 @@ TEST(CastMediaSourceTest, FromDesktopUrn) {
ASSERT_TRUE(source);
EXPECT_EQ(source_id, source->source_id());
ASSERT_EQ(1u, source->app_infos().size());
EXPECT_EQ(kCastStreamingAppId, source->app_infos()[0].app_id);
EXPECT_EQ(openscreen::cast::GetCastStreamingAudioVideoAppId(),
source->app_infos()[0].app_id);
EXPECT_TRUE(source->client_id().empty());
EXPECT_EQ(kDefaultLaunchTimeout, source->launch_timeout());
EXPECT_EQ(AutoJoinPolicy::kPageScoped, source->auto_join_policy());
Expand Down

0 comments on commit bc6d983

Please sign in to comment.