Skip to content

CefMediaRouter

Header: cef_media_router.h
Interfaces: CefMediaRouter, CefMediaObserver, CefMediaRoute, CefMediaRouteCreateCallback, CefMediaSink, CefMediaSinkDeviceInfoCallback, CefMediaSource
Category: System Utilities

Overview

Tasks, threads, parsers, streams, cookies, URL requests, scheme registration, and constant tables.

CefMediaRouter

Source File: include/cef_media_router.h

Process / Thread Context: Browser Process / any browser-process thread (unless otherwise indicated). Callbacks fire on the UI thread.

Purpose: Library-provided interface to Chromium's Media Router (Cast and DIAL). Supports discovery of media sinks, creating routes between sources and sinks, registering observers, and retrieving CefMediaSource objects from URNs.

Methods

static CefRefPtr<CefMediaRouter> GetGlobalMediaRouter(CefRefPtr<CefCompletionCallback> callback)

Parameters:

  • callback — optional completion callback executed asynchronously on the UI thread after the manager's storage has been initialized.

Return Value: The MediaRouter associated with the global request context. Equivalent to CefRequestContext::GetGlobalContext()->GetMediaRouter().

Usage Instruction: If you need to perform immediate operations after init, pass a CefCompletionCallback.

Threading Constraint: Safe to call on any browser-process thread.

virtual CefRefPtr<CefRegistration> AddObserver(CefRefPtr<CefMediaObserver> observer)

Parameters:

  • observer — client-implemented observer.

Return Value: CefRegistration handle; the observer remains registered until this handle is destroyed.

Usage Instruction: Hold the returned registration in a long-lived member; release it to unregister.

Threading Constraint: Browser process.

virtual CefRefPtr<CefMediaSource> GetSource(const CefString& urn)

Parameters:

  • urn — media source URN. Supported schemes: cast: and dial: (e.g. "cast:<appId>?clientId=<clientId>").

Return Value: A CefMediaSource object for the URN, or possibly empty/invalid for unsupported URNs.

Usage Instruction: Pair with CefMediaSink::IsCompatibleWith before calling CreateRoute.

Threading Constraint: Browser process.

virtual void NotifyCurrentSinks()

Parameters:

  • None.

Return Value: None.

Usage Instruction: Triggers an asynchronous CefMediaObserver::OnSinks on all registered observers with the current sink list. Useful to refresh UI without waiting for a change event.

Threading Constraint: Browser process.

virtual void CreateRoute(CefRefPtr<CefMediaSource> source, CefRefPtr<CefMediaSink> sink, CefRefPtr<CefMediaRouteCreateCallback> callback)

Parameters:

  • source, sink — must be valid, compatible (per CefMediaSink::IsCompatibleWith), and must not already have a route. callback — invoked on success or failure.

Return Value: None (result via callback).

Usage Instruction: On success, observers also receive an asynchronous OnRoutes notification.

Threading Constraint: Browser process; callback invoked on UI thread.

virtual void NotifyCurrentRoutes()

Parameters:

  • None.

Return Value: None.

Usage Instruction: Triggers an asynchronous CefMediaObserver::OnRoutes on all registered observers with the current route list.

Threading Constraint: Browser process.

CefMediaObserver

Source File: include/cef_media_router.h

Process / Thread Context: Browser Process / UI Thread (all callbacks).

Purpose: Client-implemented observer of Media Router events (sink list changes, route list changes, route state changes, route messages). Registered via CefMediaRouter::AddObserver.

Methods

void OnSinks(const std::vector<CefRefPtr<CefMediaSink>>& sinks)

Parameters:

  • sinks — current list of available media sinks.

Return Value: None.

Usage Instruction: Triggered when the sink list changes or when CefMediaRouter::NotifyCurrentSinks is called.

Threading Constraint: UI thread.

void OnRoutes(const std::vector<CefRefPtr<CefMediaRoute>>& routes)

Parameters:

  • routes — current list of active routes.

Return Value: None.

Usage Instruction: Triggered when the route list changes or when CefMediaRouter::NotifyCurrentRoutes is called.

Threading Constraint: UI thread.

void OnRouteStateChanged(CefRefPtr<CefMediaRoute> route, ConnectionState state)

Parameters:

  • route — affected route; state — new connection state.

Return Value: None.

Usage Instruction: Use to update UI reflecting route connectivity.

Threading Constraint: UI thread.

void OnRouteMessageReceived(CefRefPtr<CefMediaRoute> route, const void* message, size_t message_size)

Parameters:

  • route — route that received the message. message — raw message buffer; only valid for the scope of this callback — copy if needed beyond it. message_size — size in bytes.

Return Value: None.

Usage Instruction: Copy the bytes you need before returning.

Threading Constraint: UI thread.

CefMediaRoute

Source File: include/cef_media_router.h

Process / Thread Context: Any browser-process thread.

Purpose: Represents a route between a CefMediaSource and CefMediaSink. Created via CefMediaRouter::CreateRoute and retrieved via CefMediaObserver::OnRoutes.

Methods

virtual CefString GetId()

Return Value: Route ID.

Threading Constraint: Threading constraint not specified in source.

virtual CefRefPtr<CefMediaSource> GetSource()

Return Value: Source associated with this route.

Threading Constraint: Threading constraint not specified in source.

virtual CefRefPtr<CefMediaSink> GetSink()

Return Value: Sink associated with this route.

Threading Constraint: Threading constraint not specified in source.

virtual void SendRouteMessage(const void* message, size_t message_size)

Parameters:

  • Raw message buffer + size; copied if necessary.

Usage Instruction: Application-protocol-specific payload.

Threading Constraint: Threading constraint not specified in source.

virtual void Terminate()

Usage Instruction: Ends the route; triggers an asynchronous CefMediaObserver::OnRoutes on all registered observers.

Threading Constraint: Threading constraint not specified in source.

CefMediaRouteCreateCallback

Source File: include/cef_media_router.h

Process / Thread Context: Browser Process / UI Thread (callback).

Purpose: Client-implemented callback passed to CefMediaRouter::CreateRoute; invoked when route creation finishes.

Methods

void OnMediaRouteCreateFinished(RouteCreateResult result, const CefString& error, CefRefPtr<CefMediaRoute> route)

Parameters:

  • resultCEF_MRCR_OK on success. error — human-readable error description (empty on success). route — the resulting route, or empty on failure.

Return Value: None.

Usage Instruction: Check result; on success, store the route reference to send messages / terminate later.

Threading Constraint: UI thread.

CefMediaSink

Source File: include/cef_media_router.h

Process / Thread Context: Any browser-process thread.

Purpose: Represents a sink (renderer/device) to which media can be routed. Retrieved via CefMediaObserver::OnSinks.

Methods

virtual CefString GetId()

Return Value: Sink ID.

Threading Constraint: Threading constraint not specified in source.

virtual CefString GetName()

Return Value: Human-readable sink name.

Threading Constraint: Threading constraint not specified in source.

virtual IconType GetIconType()

Return Value: Icon type (default CEF_MSIT_GENERIC).

Threading Constraint: Threading constraint not specified in source.

virtual void GetDeviceInfo(CefRefPtr<CefMediaSinkDeviceInfoCallback> callback)

Parameters:

  • callback — invoked asynchronously with CefMediaSinkDeviceInfo.

Usage Instruction: Use to fetch extended device capabilities (e.g., Cast capabilities, DIAL app info).

Threading Constraint: Threading constraint not specified in source.

virtual bool IsCastSink()

Return Value: true if this sink accepts content via Cast.

Threading Constraint: Threading constraint not specified in source.

virtual bool IsDialSink()

Return Value: true if this sink accepts content via DIAL.

Threading Constraint: Threading constraint not specified in source.

virtual bool IsCompatibleWith(CefRefPtr<CefMediaSource> source)

Return Value: true if this sink is compatible with source; check before CreateRoute.

Threading Constraint: Threading constraint not specified in source.

CefMediaSinkDeviceInfoCallback

Source File: include/cef_media_router.h

Process / Thread Context: Browser Process / UI Thread (callback).

Purpose: Client-implemented callback for CefMediaSink::GetDeviceInfo.

Methods

void OnMediaSinkDeviceInfo(const CefMediaSinkDeviceInfo& device_info)

Parameters:

  • device_info — populated structure (defined in cef_types.h).

Usage Instruction: Copy the contents out before returning if you need to persist them.

Threading Constraint: Threading constraint not specified in source.

CefMediaSource

Source File: include/cef_media_router.h

Process / Thread Context: Any browser-process thread.

Purpose: Represents a source from which media can be routed. Retrieved via CefMediaRouter::GetSource.

Methods

virtual CefString GetId()

Return Value: ID (media source URN or URL).

Threading Constraint: Threading constraint not specified in source.

virtual bool IsCastSource()

Return Value: true if this source outputs via Cast.

Threading Constraint: Threading constraint not specified in source.

virtual bool IsDialSource()

Return Value: true if this source outputs via DIAL.

Threading Constraint: Threading constraint not specified in source.

Usage Example

cpp
class MyCastUI : public CefMediaObserver {
  CefRefPtr<CefRegistration> obs_reg_;
  CefRefPtr<CefMediaRouter> router_;
  CefRefPtr<CefMediaRoute> current_route_;

 public:
  void Start() {
    router_ = CefMediaRouter::GetGlobalMediaRouter(
        CefCompletionCallback::Create([]() { /* storage initialized */ }));
    obs_reg_ = router_->AddObserver(this);
    router_->NotifyCurrentSinks();  // refresh the sink list now
  }

  // CefMediaObserver:
  void OnSinks(const std::vector<CefRefPtr<CefMediaSink>>& sinks) override {
    for (auto& sink : sinks) {
      DLOG(INFO) << "Sink: " << sink->GetName().ToString()
                 << " cast=" << sink->IsCastSink()
                 << " dial=" << sink->IsDialSink();
    }
  }
  void OnRoutes(const std::vector<CefRefPtr<CefMediaRoute>>& routes) override {
    DLOG(INFO) << "Active routes: " << routes.size();
  }
  void OnRouteStateChanged(CefRefPtr<CefMediaRoute> route,
                           ConnectionState state) override {}
  void OnRouteMessageReceived(CefRefPtr<CefMediaRoute> route,
                              const void* msg, size_t n) override {
    std::string s(static_cast<const char*>(msg), n);
    DLOG(INFO) << "Msg from " << route->GetId().ToString() << ": " << s;
  }

  void CastTo(const CefString& urn, CefRefPtr<CefMediaSink> sink) {
    CefRefPtr<CefMediaSource> source = router_->GetSource(urn);
    if (source && sink->IsCompatibleWith(source)) {
      router_->CreateRoute(source, sink,
                           CefMediaRouteCreateCallback::Create(
                               [](RouteCreateResult r, const CefString& e,
                                  CefRefPtr<CefMediaRoute> route) {
                                 if (r == CEF_MRCR_OK) {
                                   // store route, send messages, terminate later
                                 }
                               }));
    }
  }
};

Derived from the CEF C++ headers — © Marshall A. Greenblatt, Google Inc. & contributors (BSD-style license). Not an official CEF project.