360° video
@mattebox/player-360@0.3.0Needs @mattebox/player >=0.18.0 and mattebox ^0.21.0.
@mattebox/player-360 shows a 360° video from inside. The picture is drawn
onto the inside of a sphere around the viewer, and the viewer looks around
it. It needs WebGL, which every browser down to Chromium 76 has.
The 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 Keys |
| 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 Motion |
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.
How the player knows
The player carries projection while its source is 360°. It comes from the
first of these that says so.
- The
projectionattribute on<mbx-360>. It wins over everything.nonekeeps a 360° source flat. - The source’s
projection, which your page or your connector sets. Use it when your content API knows: the stream often does not say. - 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’sprojectioneither 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 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.
- The
frame-packingattribute on<mbx-360>. It wins over everything.nonedraws the frame as one picture. Use it to test, or for a stream that says the wrong thing. - The source’s
framePacking, which your page or your connector sets. - The engine: the DASH
FramePackingdescriptor 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.
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 };
Keys
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
keydownlistener on the player, in the capture phase. - They do nothing while a WebXR session shows the view. The head turns it there.
Motion
On a phone or a tablet, the view turns with the device. It is on by default, because looking around with the device is what a viewer expects of a 360° video on a phone.
When the system asks for reduced motion (prefers-reduced-motion: reduce),
motion starts off. A view that sways with the hand can make a viewer with a
vestibular disorder ill, and neither the browser nor the system stops the
motion events for them. 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 a setting the viewer made for their health. Set it
only with <mbx-motion-button> in the bar, so the viewer can turn it off.
- The element listens to
deviceorientationonly 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
yawandpitchproperties 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.
The motion button
<mbx-motion-button> lets the viewer turn the motion on or off, for a
viewer who wants to look around by touch only. 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-pressedsays 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: a
viewer who turned it off to try it would not remember it on the next
visit. A page that sets
motionreplaces the choice. - To force one way, set
motionand 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.
Headsets
<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 browser decides, not its name. The element asks for each capability and reads no 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,
vron<mbx-360>names the way:webxrorstereo.
WebXR
- The button shows when the browser has WebXR and a headset can show an
immersive-vrsession. 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.
The split screen
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 turns the view, whatever
motionsays. In a viewer the head is the only way to look. - 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
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
stereois 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 check is the capability, so an iPhone gets the split screen without a change the day its browser has fullscreen on elements.
| 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 words come in the 11 languages of chapter 10, from
@mattebox/player-360/locales/<language>. The ids are enterVr,
exitVr and deviceMotion, in the Player360Messages type.
import { MatteboxPlayerElement } from '@mattebox/player';
import words from '@mattebox/player/locales/de';
import words360 from '@mattebox/player-360/locales/de';
MatteboxPlayerElement.addLocale('de', words, words360);
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.
When the view does not show
The flat video plays instead, and unavailable on <mbx-360> says why.
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 and properties
| Name | Kind | What it does |
|---|---|---|
projection |
attribute | Forces a projection, or none. See How the player knows |
fov |
attribute | The field of view a source starts with, in degrees, 75 by default |
motion |
attribute | on or off: whether the device’s motion starts turning the view. See Motion |
active |
attribute | Set by the element while the view shows |
headset |
attribute | webxr, stereo or both: the ways a headset shows the view. See 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 When the view does not show |
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 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
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.
Parts
| Part | What it is |
|---|---|
view |
The canvas the sphere is drawn on |
subtitles |
The subtitles written over the view |