2020-03-19 16:34:15 +01:00
|
|
|
// Copyright (c) 2020 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/media_router/media_sink_impl.h"
|
|
|
|
|
2020-07-14 22:43:57 +02:00
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
|
|
|
|
#include "base/strings/string_number_conversions.h"
|
|
|
|
#include "chrome/browser/media/router/discovery/dial/dial_media_sink_service_impl.h"
|
2022-01-25 21:26:51 +01:00
|
|
|
#include "chrome/browser/media/router/discovery/mdns/cast_media_sink_service_impl.h"
|
2020-07-14 22:43:57 +02:00
|
|
|
#include "chrome/browser/media/router/providers/cast/dual_media_sink_service.h"
|
2020-10-08 21:54:42 +02:00
|
|
|
#include "components/media_router/common/discovery/media_sink_internal.h"
|
|
|
|
#include "components/media_router/common/discovery/media_sink_service_base.h"
|
2020-07-14 22:43:57 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using SinkServiceVector = std::vector<media_router::MediaSinkServiceBase*>;
|
|
|
|
|
|
|
|
SinkServiceVector GetSinkServices() {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
auto sink_service = media_router::DualMediaSinkService::GetInstance();
|
|
|
|
return {sink_service->GetCastMediaSinkServiceImpl(),
|
|
|
|
sink_service->GetDialMediaSinkServiceImpl()};
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetSinkInternalAndContinue(
|
|
|
|
SinkServiceVector services,
|
|
|
|
const media_router::MediaSink::Id& sink_id,
|
|
|
|
CefRefPtr<CefMediaSinkDeviceInfoCallback> callback) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
CefMediaSinkDeviceInfo device_info;
|
|
|
|
const media_router::MediaSinkInternal* sink_internal = nullptr;
|
|
|
|
|
|
|
|
for (auto service : services) {
|
|
|
|
sink_internal = service->GetSinkById(sink_id);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (sink_internal) {
|
2020-07-14 22:43:57 +02:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-07-14 22:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sink_internal) {
|
|
|
|
if (sink_internal->is_cast_sink()) {
|
|
|
|
const auto& cast_data = sink_internal->cast_data();
|
|
|
|
CefString(&device_info.ip_address) =
|
|
|
|
cast_data.ip_endpoint.ToStringWithoutPort();
|
|
|
|
device_info.port = cast_data.ip_endpoint.port();
|
|
|
|
CefString(&device_info.model_name) = cast_data.model_name;
|
|
|
|
} else if (sink_internal->is_dial_sink()) {
|
|
|
|
const auto& dial_data = sink_internal->dial_data();
|
|
|
|
CefString(&device_info.ip_address) = dial_data.ip_address.ToString();
|
|
|
|
if (dial_data.app_url.is_valid() && dial_data.app_url.has_port()) {
|
|
|
|
base::StringToInt(dial_data.app_url.port_piece(), &device_info.port);
|
|
|
|
}
|
|
|
|
CefString(&device_info.model_name) = dial_data.model_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the callback on the UI thread.
|
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_UIT,
|
|
|
|
base::BindOnce(&CefMediaSinkDeviceInfoCallback::OnMediaSinkDeviceInfo,
|
|
|
|
callback, device_info));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetDeviceInfo(const media_router::MediaSink::Id& sink_id,
|
|
|
|
CefRefPtr<CefMediaSinkDeviceInfoCallback> callback) {
|
|
|
|
auto next_step = base::BindOnce(
|
|
|
|
[](const media_router::MediaSink::Id& sink_id,
|
|
|
|
CefRefPtr<CefMediaSinkDeviceInfoCallback> callback) {
|
|
|
|
CEF_POST_TASK(CEF_IOT,
|
|
|
|
base::BindOnce(GetSinkInternalAndContinue,
|
|
|
|
GetSinkServices(), sink_id, callback));
|
|
|
|
},
|
|
|
|
sink_id, callback);
|
|
|
|
|
|
|
|
if (CEF_CURRENTLY_ON(CEF_UIT)) {
|
|
|
|
std::move(next_step).Run();
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT, std::move(next_step));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-03-19 16:34:15 +01:00
|
|
|
CefMediaSinkImpl::CefMediaSinkImpl(const media_router::MediaSink& sink)
|
|
|
|
: sink_(sink) {}
|
|
|
|
|
2021-07-23 18:40:13 +02:00
|
|
|
CefMediaSinkImpl::CefMediaSinkImpl(
|
|
|
|
const media_router::MediaSink::Id& sink_id,
|
|
|
|
const std::string& sink_name,
|
|
|
|
media_router::mojom::MediaRouteProviderId provider_id)
|
|
|
|
: sink_(sink_id,
|
|
|
|
sink_name,
|
|
|
|
media_router::SinkIconType::GENERIC,
|
|
|
|
provider_id) {}
|
2020-03-19 16:34:15 +01:00
|
|
|
|
|
|
|
CefString CefMediaSinkImpl::GetId() {
|
|
|
|
return sink_.id();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefMediaSinkImpl::GetName() {
|
|
|
|
return sink_.name();
|
|
|
|
}
|
|
|
|
|
2020-04-29 21:01:09 +02:00
|
|
|
CefMediaSink::IconType CefMediaSinkImpl::GetIconType() {
|
|
|
|
// Verify that our enum matches Chromium's values.
|
|
|
|
static_assert(static_cast<int>(CEF_MSIT_TOTAL_COUNT) ==
|
|
|
|
static_cast<int>(media_router::SinkIconType::TOTAL_COUNT),
|
|
|
|
"enum mismatch");
|
|
|
|
|
|
|
|
return static_cast<CefMediaSink::IconType>(sink_.icon_type());
|
|
|
|
}
|
|
|
|
|
2020-07-14 22:43:57 +02:00
|
|
|
void CefMediaSinkImpl::GetDeviceInfo(
|
|
|
|
CefRefPtr<CefMediaSinkDeviceInfoCallback> callback) {
|
|
|
|
::GetDeviceInfo(sink_.id(), callback);
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:34:15 +01:00
|
|
|
bool CefMediaSinkImpl::IsCastSink() {
|
2021-07-23 18:40:13 +02:00
|
|
|
return sink_.provider_id() == media_router::mojom::MediaRouteProviderId::CAST;
|
2020-03-19 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefMediaSinkImpl::IsDialSink() {
|
2021-07-23 18:40:13 +02:00
|
|
|
return sink_.provider_id() == media_router::mojom::MediaRouteProviderId::DIAL;
|
2020-03-19 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefMediaSinkImpl::IsCompatibleWith(CefRefPtr<CefMediaSource> source) {
|
|
|
|
if (source) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (IsCastSink()) {
|
2020-03-19 16:34:15 +01:00
|
|
|
return source->IsCastSource();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (IsDialSink()) {
|
2020-03-19 16:34:15 +01:00
|
|
|
return source->IsDialSource();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-03-19 16:34:15 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|