IBM / video-streaming-web-player-api

This is the public Web Player API for controlling embedded players created at video.ibm.com.
Apache License 2.0
5 stars 6 forks source link

IBM Video Streaming Web Player API

CII Best Practices Build Status npm

Enables sites using the IBM Video Streaming embed iframe to build and adapt on the embed live player.

The Player API provides basic methods to control the live stream or recorded video playback, and enables the user to access essential events of the live stream or the played video.

The Player API requires the postMessage DOM API, it won’t work in browsers that does not support the postMessage API.

Download

npm install ibm-video-streaming-web-player-api

Usage

Create an instance of the Embed API by providing the ID of the iframe, or the iframe DOM object itself:

<iframe
    id="iframeId"
    src="https://github.com/IBM/video-streaming-web-player-api/raw/master//ustream.tv/embed/1524"
    width="640"
    height="480"
    allowfullscreen
    webkitallowfullscreen
></iframe>

global variable

const viewer = window.PlayerAPI('iframeId');

es modules

import PlayerAPI from 'ibm-video-streaming-web-player-api';

const viewer = PlayerAPI('iframeId'); // id or HTMLIFrameElement reference

The IBM Video Streaming Player API provides the following methods:

URL parameters

The default behaviour of the player can be modified by extending the src URL with any of the following parameters:

Parameter Effect Values Default
allowfullscreen Enables full-screen. False value makes the full-screen button inactive. true/false true
api-target-origin Origin of the page where player api is included. Use encodeURIComponent to URL encode origin. This parameter is only required in case of SSO authentication. URL encoded origin e.g. output of encodeURIComponent('https://video.ibm.com') N/A
autoplay Starts video playback automatically. Browser settings are stronger and may override the value of this parameter. true/false false
controls When set to false it hides all UI elements. true/false true
forced-quality Turns off the automatic quality selection and selects the appropriate quality. Low is the smallest available quality, high is the largest and med is the middlemost choice. low, med, high N/A
hideCTA Disables CTA overlays. Use liveCtaUpdate event to build your own. true/false false
initial-quality Sets the initial quality for the automatic quality selection. The quality selection logic is still turned on and can switch to another quality after playback is started. low, med, high N/A
showtitle Shows title and viewer count information inside the player area. true/false true
volume Set volume for playback as a percentage of the max volume. Overrides the default volume (100). 0-100 100

callMethod

Calls a command method of the embed player, passing in any arguments.

Available commands through callMethod:

play

Starts playing the currently loaded channel or video.

Example:
viewer.callMethod('play');

pause

Pauses the live stream, or the playback of a video.

Example:
viewer.callMethod('pause');

stop

Pauses the live stream, or stops the video and jumps back to the start.

Example:
viewer.callMethod('stop');

load

Loads a channel or a video in the player. Requires two additional arguments:

Example:
viewer.callMethod('load', 'video', 5903947);
viewer.callMethod('load', 'channel', 1524);

seek

Jumps to given position in played recorded video.

Requires one argument:

Example:
viewer.callMethod('seek', 180);

volume

Sets the playback sound volume

Requires one argument:

Example:
viewer.callMethod('volume', 0); // mute

quality

Sets the stream quality, if available.

Requires one argument:

Example:
viewer.callMethod('quality', 0); // set to highest quality

cc (closed caption)

Displays the selected closed caption if available. You can use the ‘None’ option by using -1 as the argumnet. Otherwise it requires this argument:

Requires one argument:

Example:
viewer.callMethod('cc', 1); //enables the closed caption with index 1
viewer.callMethod('cc', -1); //disables the closed caption

webkitEnterFullscreen (iOS)

Calls webkitEnterFullscreen on the HTMLVideoElement to access the video's native fullscreen UI.

Example:
viewer.callMethod('webkitEnterFullscreen');

getProperty

Read a property of the embed player.

This method is asynchronous, the data will be passed to a callback function, given as argument.

Accessible properties by getProperty:

duration

Get the video duration in seconds.

Example:
viewer.getProperty('duration', function (duration) {
    // passed value is e.g. 120.345
    ...
});

viewers

Get the current viewer count for the loaded live stream.

Example:
viewer.getProperty('viewers', function (viewerNumber) {
    ...
});

allTimeTotalViewers

Get the accumulated total viewer number for the loaded channel. Doesn’t return anything in case of videos.

Example:
viewer.getProperty('allTimeTotalViewers', function (allTimeTotalViewers) {
    ...
});

progress

Get the current progress for recorded video playback, in seconds.

Example:
viewer.getProperty('progress', function (progress) {
    ...
});

content

Get the loaded content type and id as an array.

Example:
viewer.getProperty('content', function (content) {
    // content == ['channel', 1524]
    // or
    // content == ['recorded', 123456]
    ...
});

playingContent

Get the actual content type and id as an array. This will return the currently played offair video's id if the loaded content is an offair channel or with the channel id if the channel is live.

Example:
viewer.callMethod('load', 'channel', 1524);

// ...

viewer.getProperty('playingContent', function (content) {
    // content == ['channel', 1524]
    //  - if it's live, or
    // content == ['recorded', 123456]
    //  - if it's offair and has offair video content, or
    // content == []
    //  - if it's offair and doesn't have offair video content
});

volume

Get the player volume. This will return the actual value of volume in percents.

Example:
viewer.getProperty('volume', function (volume) {
    // volume == 0 for muted playback
    ...
});

addListener & removeListener

The embed player dispatches several events during playback. This method adds or removes event handlers to these events.

The event handler callback receives two arguments:

Available events for addListener and removeListener:

live

Called when the currently loaded offline channel becomes live.

Example:
viewer.addListener('live', callBack);

offline

Called when the currently loaded live channel goes offline.

Example:
viewer.addListener('offline', callBack);

finished

Called when the currently loaded and played recorded video reaches its end.

Example:
viewer.addListener('finished', callBack);

playing

Called when the currently loaded content playback is started or stopped

Sends data along the event:

Example:
viewer.addListener('playing', callBack);

size

Called when the stream size is available, or changed (changes reported only in flash mode)

Sent data is the size of the calculated embed iframe according to the player width, and the stream aspect ratio. The player bar height is included, if the controls are visible.

Sends data along the event:

Example:
viewer.addListener('size', callBack);

quality

Fired when the stream quality options are available.

Receives an array of quality objects, in which are the following keys:

Example:
viewer.addListener('quality', callBack);
// example payload
[
    {
        "id": 0,
        "codec": "avc1.4d001f",
        "bitrate": 1406,
        "transcoded": false,
        "width": 1280,
        "height": 720,
        "label": {
            "text": "720p",
            "suffix": " HD",
            "bitrate": " @ 1.4 Mbps"
        },
        "selected": false
    },
]

cc

Fired when there are closed captions available on the stream.

Returns an array containing closed captions as objects.

Example:
viewer.addListener('cc', callBack);
// example payload
[
    {
        "index": 0,
        "label": "Spanish",
        "language": "es",
        "country": "00",
        "active": true
    }
]

content

Called when a the player content changes for some reason. Same data as received in getProperty('content')

Received arguments: data (array)

Example:
viewer.addListener('content', callBack);

liveCtaUpdate

Fired when there is a live cta available on the stream.

Returns an object:

Example:
viewer.addListener('liveCtaUpdate', callBack);
// example payload
{
    activate: {
        buttonText: "Click here!"
        buttonUrl: "https://video.ibm.com"
        description: "The Future of Video with Watson",
        id: 123,
        imageUrl: "URL of image",
        title: "IBM Video Streaming"
    }
}

error

Fired when an unexpected event occures.

Returns an object:

Available error type(s):

Example
viewer.addListener('error', function(type, errorEvent) {
    switch (errorEvent.name) {
        case 'autoplayRejected':
            // TODO: display fallback button
            console.log(errorEvent.message);
            break;
        case 'webkitEnterFullscreenRejected':
            // TODO: browser prevented the request, user gesture is needed
            console.log(errorEvent.message);
            break;
        // no default
    }
});

Build

# install npm dependencies
npm install
# build es and umd bundles
npm run build

Lint (eslint)

# run eslint
npm run lint