cef/libcef/browser/media_router/media_route_impl.cc
Marshall Greenblatt 03fd5b15da Add support for media device discovery and messaging (fixes issue #2900)
Chromium supports communication with media devices on the local network via
the Cast and DIAL protocols. This takes two primary forms:

1. Messaging, where strings representing state information are passed between
   the client and a dedicated receiver app on the media device. The receiver
   app communicates directly with an app-specific backend service to retrieve
   and possibly control media playback.
2. Tab/desktop mirroring, where the media contents are streamed directly from
   the browser to a generic streaming app on the media device and playback is
   controlled by the browser.

This change adds support for device discovery and messaging (but not
mirroring) with functionality exposed via the new CefMediaRouter interface.

To test: Navigate to http://tests/media_router in cefclient and follow the
on-screen instructions.
2020-03-27 15:54:39 -04:00

84 lines
2.6 KiB
C++

// 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_route_impl.h"
#include "libcef/browser/media_router/media_router_manager.h"
#include "libcef/browser/media_router/media_sink_impl.h"
#include "libcef/browser/media_router/media_source_impl.h"
#include "libcef/browser/thread_util.h"
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());
// Will return nullptr if the BrowserContext has been destroyed.
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() {
return new CefMediaSinkImpl(route_.media_sink_id(), route_.media_sink_name());
}
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_);
if (!browser_context)
return;
browser_context->GetMediaRouterManager()->TerminateRoute(
route_.media_route_id());
}
void CefMediaRouteImpl::SendRouteMessageInternal(std::string message) {
auto browser_context = GetBrowserContext(browser_context_getter_);
if (!browser_context)
return;
browser_context->GetMediaRouterManager()->SendRouteMessage(
route_.media_route_id(), message);
}