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.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/media_router/media_route_impl.h"
|
2020-03-19 16:34:15 +01:00
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/media_router/media_router_manager.h"
|
|
|
|
#include "cef/libcef/browser/media_router/media_sink_impl.h"
|
|
|
|
#include "cef/libcef/browser/media_router/media_source_impl.h"
|
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
2020-03-19 16:34:15 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Do not keep a reference to the object returned by this method.
|
|
|
|
CefBrowserContext* GetBrowserContext(const CefBrowserContext::Getter& getter) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
DCHECK(!getter.is_null());
|
|
|
|
|
2022-01-10 22:11:16 +01:00
|
|
|
// Will return nullptr if the BrowserContext has been shut down.
|
2020-03-19 16:34:15 +01:00
|
|
|
return getter.Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefMediaRouteImpl::CefMediaRouteImpl(
|
|
|
|
const media_router::MediaRoute& route,
|
|
|
|
const CefBrowserContext::Getter& browser_context_getter)
|
|
|
|
: route_(route), browser_context_getter_(browser_context_getter) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefMediaRouteImpl::GetId() {
|
|
|
|
return route_.media_route_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefMediaSource> CefMediaRouteImpl::GetSource() {
|
|
|
|
return new CefMediaSourceImpl(route_.media_source().id());
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefMediaSink> CefMediaRouteImpl::GetSink() {
|
2021-07-23 18:40:13 +02:00
|
|
|
return new CefMediaSinkImpl(
|
|
|
|
route_.media_sink_id(), route_.media_sink_name(),
|
|
|
|
route_.media_source().IsDialSource()
|
|
|
|
? media_router::mojom::MediaRouteProviderId::DIAL
|
|
|
|
: media_router::mojom::MediaRouteProviderId::CAST);
|
2020-03-19 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefMediaRouteImpl::SendRouteMessage(const void* message,
|
|
|
|
size_t message_size) {
|
|
|
|
std::string message_str(reinterpret_cast<const char*>(message), message_size);
|
|
|
|
|
|
|
|
if (!CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_UIT,
|
|
|
|
base::BindOnce(
|
|
|
|
[](CefRefPtr<CefMediaRouteImpl> self, std::string message_str) {
|
|
|
|
self->SendRouteMessageInternal(std::move(message_str));
|
|
|
|
},
|
|
|
|
CefRefPtr<CefMediaRouteImpl>(this), std::move(message_str)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SendRouteMessageInternal(std::move(message_str));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefMediaRouteImpl::Terminate() {
|
|
|
|
if (!CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
CEF_POST_TASK(CEF_UIT, base::BindOnce(&CefMediaRouteImpl::Terminate, this));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto browser_context = GetBrowserContext(browser_context_getter_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!browser_context) {
|
2020-03-19 16:34:15 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-03-19 16:34:15 +01:00
|
|
|
|
|
|
|
browser_context->GetMediaRouterManager()->TerminateRoute(
|
|
|
|
route_.media_route_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefMediaRouteImpl::SendRouteMessageInternal(std::string message) {
|
|
|
|
auto browser_context = GetBrowserContext(browser_context_getter_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!browser_context) {
|
2020-03-19 16:34:15 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-03-19 16:34:15 +01:00
|
|
|
|
|
|
|
browser_context->GetMediaRouterManager()->SendRouteMessage(
|
|
|
|
route_.media_route_id(), message);
|
|
|
|
}
|