Skip to main content

Usage

Prerequisites

Register Domain

Register your domain to activate P2P service.

tip

Localhost is always whitelisted. This means that you do not have to configure anything to perform tests locally.

Host Service Worker (Optional)

Click me
Secure your site with HTTPS, if it isn't already. HTTPS is required for Service Worker, which we'll set up in the next step. To maintain a high-security standard, foreign software to be integrated into the browser with the help of ServiceWorkers is not allowed. This is only possible if the software is hosted on the same server as the web page. So the ServiceWorkers only work if they are hosted under the same domain as the main page, meaning that a client running a website needs to host our web SDK on his own servers instead of getting it from our public cloud.

Copy sw.js to your server and make it available at https://your_website.com/sw.js . Alternatively, download hls-proxy.js then rename to sw.js .

tip

You don't need to secure your site with HTTPS or host Service Worker if only hls.js is used.

Install P2P Engine

Script

Include the pre-built script of latest version bundled with hls.js, Or include the latest version without hls.js:

<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script src="https://cdn.jsdelivr.net/npm/@swarmcloud/hls/p2p-engine.min.js"></script>

File

Click me
This needs to be included before your player code. You can either prepend it to your compiled code or include it in a <script> before it.

Browserify / Webpack

npm install --save @swarmcloud/hls

To include @swarmcloud/hls you need to require it in the player module:

var Hls = require('hls.js');
var P2pEngineHls = require('@swarmcloud/hls').default;

If you are using ES6's import syntax:

import Hls from 'hls.js';
import P2pEngineHls from '@swarmcloud/hls';

Usage

Create hls.js instance passsing hlsjsConfig as parameter. Create P2pEngineHls instance passing p2pConfig as parameter.

var p2pConfig = {
// Other p2pConfig options if applicable
}
var engine;
if (P2pEngineHls.isMSESupported()) {
var hls = new Hls();
p2pConfig.hlsjsInstance = hls; // set hlsjs instance to SDK
engine = new P2pEngineHls(p2pConfig);
// Use hls just like your usual hls.js…
hls.loadSource(contentUrl);
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED,function() {
video.play();
});
} else {
engine = new P2pEngineHls(p2pConfig); // equal to new P2pEngineHls.ServiceWorkerEngine(p2pConfig);
engine.registerServiceWorker().catch(() => {}).finally(() => {
// native video playback here

});
}

How it works

When the SDK is instantiated, it will give priority to trying to use hls.js based engine (hereinafter referred to as MSE Engine). If failed, try the ServiceWorker based engine (hereinafter referred to as SW Engine), as follows:

  • If the browser does not support webrtc datachannel, no engine will be enabled
  • If the browser supports MSE && hlsjsInstance is passed in && proxyOnly is not set, try to enable the MSE Engine
  • Otherwise, enable the SW Engine if ServiceWorker is supported

Player Integration

See players demo .

File Explanation

@swarmcloud/hls/
├── hls.min.d.ts # Typescript type information about hls.min.js and hls.light.min.js
├── hls.min.js # Hls.js with P2pEngine built in
└── hls.light.min.js # Hls.light.js with P2pEngine built in
└── p2p-engine.min.d.js # Typescript type information about p2p-engine.min.js
└── p2p-engine.min.js # P2pEngine that supports both Hls.js and ServiceWorker
└── p2p-engine.es.min.js # P2pEngine that supports both Hls.js and ServiceWorker for ES Module
└── hlsjs-p2p-engine.min.js # P2pEngine that supports Hls.js only
└── sw-p2p-engine.min.js # P2pEngine that supports ServiceWorker only
└── hls-proxy.js # The file that be imported by sw.js

Electron

Electron is also supported, you just need to register AppId and get a token from dashboard:

var hlsjsConfig = {
p2pConfig: {
token: YOUR_TOKEN,
appName: YOUR_APP_NAME,
appId: YOUR_APP_ID,
// Other p2pConfig options if applicable
}
};

Learn more here