360° video

@mattebox/player-360@0.3.0Needs @mattebox/player >=0.18.0 and mattebox ^0.21.0.

@mattebox/player-360 renders an equirectangular 360° or 180° video on the inside of a sphere, with WebGL. The viewer turns the view with a drag, the keyboard, a gamepad, the device’s orientation or a VR headset. Every browser from Chromium 76 has WebGL.

The mbx-360 element

<mattebox-player src="https://example.com/vod/360/master.m3u8" controls="custom">
  <mbx-360></mbx-360>
  <mbx-control-bar>
    <mbx-play-button></mbx-play-button>
    <mbx-seek-bar></mbx-seek-bar>
  </mbx-control-bar>
</mattebox-player>

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

Place <mbx-360> first, right after the video. It covers the whole player, and the controls placed after it stay on top.

Over Safari’s own HLS the video shows flat. So while <mbx-360> is inside a player, the player’s default for native-hls is off: Safari plays HLS through the engine. Set native-hls="on" to give Safari its own HLS back, for a player whose sources are all flat.

The viewer What happens
Drags the picture The view turns. The picture follows the pointer
Taps without a drag The video plays or pauses, as a click on a flat video does
Turns the wheel The view zooms in or out, from 30° to 100° tall
Presses W, A, S or D The view turns up, left, down or right. See Keyboard
Leans the right stick A game controller turns the view, faster the further it leans
Turns a phone The view turns with the device. See Device orientation

While the view shows, the video keeps a 16:9 box. A 360° picture is 2:1, and the view is a window into it.

The poster shows as on a flat video: until the programme plays or seeks. Before that the sphere stays hidden. On a new source, the last programme’s picture goes as soon as the video empties.

Projection detection

The player has the projection attribute while its source is 360°. The value comes from the first of these that gives one:

  1. The projection attribute on <mbx-360>. It wins over everything. none keeps a 360° source flat.
  2. The source’s projection, which your page or your connector sets. Use it when your content API knows: the stream often does not say.
  3. The engine: HLS REQ-VIDEO-LAYOUT, the DASH projection descriptor and the boxes of an MP4 init segment (engine guide, chapter 03). The player reads the engine’s projection either way.
player.sources = [{ url: 'https://example.com/360/master.m3u8', projection: 'equirectangular' }];
projection The picture
equirectangular The whole sphere, 2:1
half-equirectangular The half in front of the viewer, 1:1. The back is black

Frame packing

Some 360° videos are filmed with two lenses, and each frame holds two pictures: one for the left eye and one for the right. That is the video’s frame packing.

framePacking The frame
top-bottom The left eye’s picture over the right eye’s
side-by-side The left eye’s picture on the left of the other
  • A WebXR headset and the split screen show each eye its own picture, so the viewer sees depth. See VR headsets.
  • The screen shows the left eye’s picture.
  • A frame with one picture has no frame packing, and both eyes see it.

The player carries frame-packing while its source has one, as it carries projection. It comes from the first of these that says so.

  1. The frame-packing attribute on <mbx-360>. It wins over everything. none draws the frame as one picture. Use it to test, or for a stream that says the wrong thing.
  2. The source’s framePacking, which your page or your connector sets.
  3. The engine: the DASH FramePacking descriptor and the boxes of an MP4 init segment (engine guide, chapter 03). An HLS playlist cannot say it, so an HLS stream needs the boxes or the source.
player.sources = [
  {
    url: 'https://example.com/360/manifest.mpd',
    projection: 'equirectangular',
    framePacking: 'top-bottom',
  },
];

A frame packing without a projection changes nothing: a flat video plays as it is, with both pictures in view.

Projection from a connector

SRG SSR’s content API marks a 360° resource with presentation: 'VIDEO_360', and its streams carry no projection of their own. The demo’s connector turns the one into the other:

const sphere = resource.presentation === 'VIDEO_360' ? { projection: 'equirectangular' } : {};
return { url, type: resource.mimeType, ...sphere };

Keyboard: W, A, S, D

W, A, S and D turn the view up, left, down and right. Each press turns it by a fifteenth of the field of view, 5° at the default, and a held key repeats.

  • They work while focus is inside the player, as the player’s own shortcuts do (chapter 03).
  • They are read by their place on the keyboard, not by their letter. A French keyboard turns with Z, Q, S and D.
  • The arrows are not used. They seek, and on a TV they move focus between the controls (chapter 06).
  • A key your page prevented is left alone, and so is a key with Ctrl, Alt or Meta, and a letter typed into a field. To turn the keys off, prevent them in a keydown listener on the player, in the capture phase.
  • They do nothing while a WebXR session shows the view. The head turns it there.

Device orientation: the motion attribute

On a phone or a tablet, the view follows the device’s orientation (deviceorientation events). This is on by default.

Under prefers-reduced-motion: reduce, it starts off. A view that moves with the hand can make a viewer with a vestibular disorder ill, and the browser still sends deviceorientation events under that setting. The element follows the setting when it changes while the page is open.

motion Motion starts
absent On, or off when the system asks for reduced motion
on On, even when the system asks for reduced motion
off Off

motion="on" overrides the viewer’s prefers-reduced-motion setting. Set it only with <mbx-motion-button> in the bar, so the viewer can turn it off.

  • The element listens to deviceorientation only while the view shows.
  • The first event keeps the yaw the view has, so the view does not jump. The pitch and the roll follow the device from then on, level with the ground.
  • A drag, the wheel, the stick and the yaw and pitch properties still work. Their turn adds to the device’s, and stays as the device moves.
  • A computer without the sensors sends no angles, and nothing changes.
  • A new source starts straight ahead again.

iOS 13 and later sends no motion before the page asks, and lets a page ask only during a touch or a click. The element asks at the viewer’s first touch on the view, once. That touch also plays or pauses, as any tap does. A refusal turns motion off.

mbx-motion-button

<mbx-motion-button> turns the device orientation on or off. Place it in the bar:

<mattebox-player src="https://example.com/vod/360/master.m3u8" controls="custom">
  <mbx-360></mbx-360>
  <mbx-control-bar>
    <mbx-play-button></mbx-play-button>
    <mbx-seek-bar></mbx-seek-bar>
    <mbx-motion-button></mbx-motion-button>
  </mbx-control-bar>
</mattebox-player>
  • It is a toggle: one name, and aria-pressed says whether the motion turns the view.
  • It is hidden while no 360° view shows, and on a device without motion sensors, such as a computer. It shows once the device sends angles, or at once on iOS, which sends none before the page asks.
  • On iOS its click asks for the permission. A refusal leaves it off.
  • The viewer’s choice lasts as long as the element. It is not saved across visits. A page that sets motion replaces the choice.
  • To force one way, set motion and leave the button out.
Name Kind What it does
label attribute The button’s name. Turn with the device
icon-on slot The glyph while the motion turns the view
icon-off slot The glyph while it does not

A page with a control of its own calls setMotion() on <mbx-360> from a click, reads motionOn and motionAvailable, and listens to motionchange.

VR headsets: mbx-vr-button and the headset attribute

<mbx-vr-button> shows the view in a VR headset. Place it in the bar:

<mattebox-player src="https://example.com/vod/360/master.m3u8" controls="custom">
  <mbx-360></mbx-360>
  <mbx-control-bar>
    <mbx-play-button></mbx-play-button>
    <mbx-seek-bar></mbx-seek-bar>
    <mbx-vr-button></mbx-vr-button>
  </mbx-control-bar>
</mattebox-player>

headset on <mbx-360> names the ways a headset shows the view. The button takes the first one the device has.

headset The button shows the view
absent or webxr In a WebXR session
webxr stereo In a WebXR session where a headset answers, else on the split screen
stereo On the split screen
  • The button is hidden where the device has none of the named ways, and while <mbx-360> shows no 360° source.
  • The element detects each capability. It does not read the user agent.
  • The view starts at the yaw the screen had. The viewer looks around with their head.
  • The picture is the same for both eyes, unless the frame holds one for each. See Frame packing.
  • While a headset shows the view, vr on <mbx-360> names the way: webxr or stereo.

WebXR

  • The button shows when the browser has WebXR and a headset can show an immersive-vr session. A headset plugged in later shows it.
  • The session ends from the headset’s own menu, from the button, or when the source becomes flat. The screen view draws again then.
  • Without WebXR, which includes every TV, the button stays hidden and nothing else changes.

Split screen (stereo)

stereo draws the view twice, side by side, for a phone in a viewer such as a Cardboard. Name it after webxr, so a real headset still gets a session:

<mbx-360 headset="webxr stereo"></mbx-360>

The device needs all three:

  • Fullscreen on the player element. The two halves must fill the screen.
  • A touch screen. A computer and a TV have none.
  • Motion sensors. The element knows from the first motion event, so the button can show a moment after the page.

What it does:

  • The player goes fullscreen. The screen turns to landscape where the browser lets a page do it. Elsewhere the viewer turns the phone.
  • The device’s orientation turns the view, whatever motion says. A phone in a viewer has no other input.
  • It ends from the button, when the viewer leaves fullscreen, or when the source becomes flat. A player that was already fullscreen stays so.
  • The halves are not corrected for the viewer’s lenses.
  • Subtitles do not show, as in a WebXR session.
  • A tablet passes the three checks, so the button shows there too.

iOS limits

The button never shows on an iPhone, with any value of headset.

  • Safari on iOS has no WebXR, and every browser there runs on Safari’s engine. That rules out webxr, on an iPad too.
  • An iPhone has fullscreen on the video alone, not on an element. That fullscreen plays the flat picture, without the view. That rules out stereo.
  • Filling the window without fullscreen leaves the browser’s bars over the picture, so the element does not do it.
  • An iPad has fullscreen on elements, so stereo is offered there.
  • For the same reason the fullscreen button plays a 360° source flat on an iPhone. Outside fullscreen the view shows in the page.

The element checks the capability, not the device. If iOS adds fullscreen on elements, the split screen works on an iPhone without a change.

Name Kind What it does
label-enter attribute The button’s name while the screen shows the view. Enter VR
label-exit attribute The button’s name while the headset shows it. Exit VR
icon-enter slot The glyph while the screen shows the view
icon-exit slot The glyph while the headset shows it

The package ships its labels in the 11 languages of chapter 10, as @mattebox/player-360/locales/<language>. The message ids are enterVr, exitVr and deviceMotion, in the Player360Messages type.

import { MatteboxPlayerElement } from '@mattebox/player';
import de from '@mattebox/player/locales/de';
import de360 from '@mattebox/player-360/locales/de';

MatteboxPlayerElement.addLocale('de', de, de360);

A page with a control of its own calls enterVR() on <mbx-360> from a click. The browser starts a session, and fullscreen, only during a click or a key press. headsetAvailable says which way it takes, or null, and headsetchange says it changed.

The unavailable attribute

When the element cannot draw the view, the flat video plays, and unavailable on <mbx-360> gives the reason.

unavailable Why
drm The source is encrypted. A browser does not let a page draw a DRM picture: no player can
source Another site’s video, served without CORS. Set crossorigin on the player and allow CORS
webgl The browser gives no WebGL

Attributes, properties, methods and events

Name Kind What it does
projection attribute Forces a projection, or none. See Projection detection
fov attribute The field of view a source starts with, in degrees, 75 by default
motion attribute on or off: whether the device’s orientation turns the view at the start. See Device orientation
active attribute Set by the element while the view shows
headset attribute webxr, stereo or both: the headset modes. See VR headsets
frame-packing attribute Forces a frame packing, or none. See Frame packing
vr attribute Set by the element while a headset shows the view: webxr or stereo
unavailable attribute Set by the element. See The unavailable attribute
yaw property Degrees the view is turned right of the picture’s centre
pitch property Degrees the view looks up
fov property The vertical field of view in degrees, from 30 to 100
framePacking property top-bottom, side-by-side, or null for a frame with one picture
immersive property True while a headset shows the view
headsetAvailable property The way enterVR() takes now, webxr or stereo, or null without one
enterVR() method Shows the view in a headset. Call it from a click. See VR headsets
exitVR() method Ends the headset session or the split screen
headsetchange event headsetAvailable changed. It bubbles to the player
motionOn property True while the device’s motion turns the view, as the viewer or motion set it
motionAvailable property True on a device with motion sensors
setMotion(on) method The viewer’s choice, until the page sets motion. Call it from a click on iOS
motionchange event motionOn or motionAvailable changed. It bubbles to the player

A new source starts straight ahead, at fov.

Subtitles over the view

The browser draws subtitles inside the video, under the view. The element writes the active cues of the showing track over the view instead, without their styling.

CSS parts

Part What it is
view The canvas the sphere is drawn on
subtitles The subtitles written over the view

Read this page's source on GitHub