Casting

@mattebox/player-cast@0.8.2Needs @mattebox/player >=0.18.0 and mattebox ^0.21.0.

While a device plays in the video’s place, the player keeps its own controls. They drive the device: play, pause, the seek bar, the volume, the chapters and the skip buttons. The video on the page stays paused. The bar does not hide during the cast, and a tap on the picture plays or pauses.

  • The player has casting with the kind of the device: chromecast or airplay. It fires castingchange, and player.casting reads the kind and the name of the device, or null.
  • <mbx-casting> covers the picture with the programme’s poster, dimmed, and shows the device’s glyph over a line that names it: “Casting to Living room”. Under AirPlay the line is “Playing on AirPlay”, since Safari does not name the target. Place it before the other screens.
  • The quality menu, the video menu, the scan buttons, fullscreen and picture in picture hide during a cast. The device picks its own quality.
  • The speed menu hides during any cast: a Chromecast plays its own engine, and an AirPlay target ignores the video’s rate.
  • The audio and subtitles menus hide during a Chromecast session. They stay under AirPlay.
  • A drag on the seek bar shows the preview and seeks once, on the release.
  • A load during the cast goes to the device, such as the next item of a playlist.

A hidden control carries away.

AirPlay needs nothing more than its button: Safari plays the video on the device. Chromecast is @mattebox/player-cast, a package of its own. Its button loads Google’s sender SDK onto the page, and the player never loads third-party code for a page.

<mattebox-player src="…" controls="custom">
  <mbx-casting></mbx-casting>
  <mbx-control-bar>
    …
    <mbx-airplay-button></mbx-airplay-button>
    <mbx-cast-button></mbx-cast-button>
  </mbx-control-bar>
</mattebox-player>

<script type="module">
  import '@mattebox/player';
  import '@mattebox/player-cast';
</script>

<mbx-cast-button> starts and ends a Chromecast session.

The button loads the SDK from gstatic the first time it connects. It does not load the SDK when the page already has it, or when the button has sdk="none". In that case the page loads the SDK.

receiver is the id of the receiver application. Without it, the button uses the Default Media Receiver. The first button on the page decides the receiver, because the SDK keeps one context per page.

Element Attributes Labels Icon slots
mbx-cast-button receiver, sdk label-start, label-stop icon, icon-active
mbx-casting label with {device}, label-airplay

When a session starts, the player does this:

  • It pauses the video.
  • It suspends the engine, so the engine requests nothing during the cast. The source stays loaded, so the chapters, the markers and the thumbnails keep working.
  • It sends the source of the session at the video’s current time, and LIVE for a live stream. A live viewer behind the edge continues at the same moment, when the stream carries a clock.
  • It sends the programme with the source: the title, the image, the chapters, the markers, the subtitle files of the video, the thumbnail track, and the connector’s type and id.
  • It sets casting="chromecast" on itself.

When the session ends, the engine resumes. The video seeks to the time where the receiver stopped, and plays if the receiver was playing. A live stream returns to the live edge instead.

The Default Media Receiver plays clear HLS and DASH and shows the title. It reads nothing else: no marker, no chapter and no DRM server. A receiver built on @mattebox/cast-receiver reads them all, in customData.mattebox. A receiver page with the same connector loads the programme by its type and id, with its own token.

License headers and other secrets are the page’s to add, in castload:

player.addEventListener('castload', (event) => {
  event.detail.customData.mattebox.licenseHeaders = { Authorization: `Bearer ${token}` };
});

player.media is what the controls read and call. It behaves as the <video>, and during a cast its playback members are the device’s. It is not a DOM node. For an API that takes the element itself, such as an observer, read the <video> child of the player.

Seek through player.media, not through the <video>. Apple’s WebKit loses a seek made before the first frame of a source it plays itself, as on an iPhone before the viewer plays, and no later seek works. player.media keeps such a seek and makes it at the first frame, and its currentTime reads the time kept until then. Chapter 2 has the detail.

Read this page's source on GitHub