chromium/components/global_media_controls/public/mojom/device_service.mojom

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

module global_media_controls.mojom;

import "mojo/public/mojom/base/unguessable_token.mojom";
import "services/media_session/public/mojom/media_session.mojom";
import "ui/gfx/image/mojom/image.mojom";

// These interfaces are a part of the crosapi but are also shared with
// non-Chrome-OS desktop platforms. All the code implementing the interfaces in
// this file runs in the browser process (either on the Ash side or the Lacros
// side, in the case of Chrome OS).

// Corresponds to icons shown in the device picker UI.
[Stable, Extensible]
enum IconType {
  [Default] kUnknown = 0,
  kThrobber = 1,
  kInfo = 2,
  kTv = 3,
  kSpeaker = 4,
  kSpeakerGroup = 5,
  // A wired display is an example of a device using the input icon.
  kInput = 6,
};

// Represents a media playback device, which may be a physical device such as a
// TV or a smart speaker, or a virtual device.
//
// Next version: 1
// Next id: 4
[Stable]
struct Device {
  string id@0;
  string name@1;
  string status_text@2;
  IconType icon@3;
};

// Provides access to devices on which media can be played back. One instance is
// created per media session or presentation.
// On Chrome OS, this is implemented by Lacros (or the Ash browser).
//
// Next version: 1
// Next method id: 1
[Stable, Uuid="e34cfef5-7dc9-40f1-9fd8-e4e0f70b1beb"]
interface DeviceListHost {
  // Selecting a device may cause media to be played, stopped, etc. on that
  // device depending on the current state.
  SelectDevice@0(string device_id);
};

// Receives updates about devices available for media playback. Typically has a
// 1-to-1 relationship with a DeviceListHost.
// On Chrome OS, this is implemented by the Ash system UI.
//
// Next version: 2
// Next method id: 2
[Stable, Uuid="ba4ece9a-0557-471a-a3bf-888b71bcde5f"]
interface DeviceListClient {
  // Receives devices available for playback of the media session that this
  // object is associated with.
  OnDevicesUpdated@0(array<Device> devices);

  // Receives permission rejected error. Mac only.
  [MinVersion=1]
  OnPermissionRejected@1();
};

// Provides access to DeviceListHosts.
// On Chrome OS, this is implemented by Lacros (or the Ash browser), and
// registers itself with Ash via crosapi.mojom.MediaUI. DeviceService instances
// are per-profile.
//
// Next version: 2
// Next method id: 3
[Stable, Uuid="a0822b52-40d9-444b-abfa-d35f0d30d69f"]
interface DeviceService {
  // Lets DeviceListClient start observing the DeviceListHost associated with
  // the local media session specified by `session_id` obtained via
  // `media_session.mojom.AudioFocusRequestState.request_id`.
  //
  // If a session is not found, then the pending remote/receiver get dropped,
  // and the caller can be notified to do cleanup via their disconnect handlers.
  GetDeviceListHostForSession@0(
      string session_id, pending_receiver<DeviceListHost> host_receiver,
      pending_remote<DeviceListClient> client_remote);

  // Lets DeviceListClient start observing the DeviceListHost associated with
  // the currently pending PresentationRequest (i.e. a website has made a
  // request via the Presentation API and is waiting for a device to be chosen
  // in the system UI).
  //
  // If no such request exists or this method has already been called for the
  // current request, then the pending remote/receiver get dropped, and the
  // caller can be notified to do cleanup via their disconnect handlers.
  //
  // If the request is associated with a local media session, then
  // ObserveDeviceListHostForSession() should be called instead.
  GetDeviceListHostForPresentation@1(
      pending_receiver<DeviceListHost> host_receiver,
      pending_remote<DeviceListClient> client_remote);

  // Provides a remote to DevicePickerProvider. This is expected to be called
  // just once during initialization.
  [MinVersion=1]
  SetDevicePickerProvider@2(
      pending_remote<DevicePickerProvider> provider_remote);
};

// The implementer of this interface shows a device picker in the Global Media
// Controls UI that is not associated with a media session. That may become
// necessary e.g. when a PresentationRequest is started on a website with no or
// muted media.
//
// On Chrome OS, this is implemented on the Ash side of the crosapi split.
//
// Next version: 1
// Next method id: 9
[Stable, Uuid="48835cb0-d095-4ddc-bbe0-6102a7764e79"]
interface DevicePickerProvider {
  // Creates but doesn't show an item.
  // Is a no-op if an item has already been created.
  CreateItem@0(mojo_base.mojom.UnguessableToken source_id);

  // The metadata and image info are discarded.
  // Is a no-op if an item hasn't previously been created with CreateItem().
  DeleteItem@1();

  // Is a no-op if an item hasn't previously been created with CreateItem().
  ShowItem@2();

  // Unlike DeleteItem(), the item retains its metadata and artwork info, so
  // they can be shown again if ShowItem() is called.
  // Is a no-op if an item hasn't previously been created with CreateItem().
  HideItem@3();

  // Updates the picker UI with the metadata.
  OnMetadataChanged@4(media_session.mojom.MediaMetadata metadata);

  // Updates the picker UI with the artwork.
  OnArtworkImageChanged@5(gfx.mojom.ImageSkia? artwork_image);

  // Updates the picker UI with the favicon image.
  OnFaviconImageChanged@6(gfx.mojom.ImageSkia? favicon_image);

  // Adds an observer. An observer is removed by severing the Mojo connection.
  // Multiple observers can be added.
  AddObserver@7(pending_remote<DevicePickerObserver> observer);

  // Hides the entire media UI containing the picker.
  HideMediaUI@8();
};

// Observes DevicePickerProvider.
//
// On Chrome OS, this is implemented on the browser side of the crosapi split.
//
// Next version: 1
// Next method id: 4
[Stable, Uuid="a673ddaa-ffb5-4baa-bb8f-07e079c42e5c"]
interface DevicePickerObserver {
  // Called when the media UI containing the picker is opened.
  OnMediaUIOpened@0();

  // Called when the media UI containing the picker is closed.
  OnMediaUIClosed@1();

  // Called when the media UI containing the picker has its contents updated,
  // e.g. when the list of media items shown is changed.
  OnMediaUIUpdated@2();

  // Called when the picker UI is dismissed by the user.
  OnPickerDismissed@3();
};