npaw-plugin-adapters
Version:
NPAW's Plugin Adapters
222 lines (190 loc) • 6.18 kB
JavaScript
export default class AvPlayAdapter {
checkExistsPlayer() {
return !!this.player;
}
getVersion() {
return '7.0.2-samsungavplay-jsclass';
}
getPlayhead() {
if (this.player) {
const ms = parseInt(this.player.getCurrentTime(), 10) || 0;
return ms / 1000;
}
return 0;
}
getIsLive() {
if (this.player && this.player.getStreamingProperty('IS_LIVE')) {
return parseInt(this.player.getStreamingProperty('IS_LIVE')) === 1;
}
return false;
}
getDuration() {
if (this.player) {
const durationMs = parseInt(this.player.getDuration(), 10);
if (durationMs && durationMs > 0) {
return durationMs / 1000;
}
}
return -1;
}
getPlayrate() {
// By default, AVPlay does not provide getSpeed(). Return 1 if unknown.
return 1;
}
getPlayerName() {
return 'Tizen-AVPlay';
}
getPlayerVersion() {
return this.player.getVersion();
}
registerListeners() {
let listeners = {
onbufferingstart: this.onBufferingStart.bind(this),
onbufferingprogress: this.onBufferingProgress.bind(this),
onbufferingcomplete: this.onBufferingComplete.bind(this),
onstreamcompleted: this.onStreamCompleted.bind(this),
oncurrentplaytime: this.onCurrentPlayTime.bind(this),
onevent: this.onEvent.bind(this),
onerror: this.onError.bind(this)
};
if (this.player && listeners) {
this.player.setListener(listeners);
}
this._wrapPlayerFunctions();
}
unregisterListeners() {
this.player.setListener({});
this._unwrapPlayerFunctions();
}
onBufferingStart() {
if (this.flags.isJoined) {
this.fireBufferBegin({}, false, 'onBufferingStart');
}
}
onBufferingComplete() {
// Corresponds to "buffer end" or PLAYING in your analytics flow.
if (!this.flags.isJoined) {
this.fireJoin({}, 'onBufferingComplete');
} else {
this.fireBufferEnd({}, 'onBufferingComplete');
}
}
onStreamCompleted() {
// The stream ended, so fireStop.
this.fireStop({}, 'onStreamCompleted');
}
onError(error) {
// Handle critical errors.
this.fireError(error, 'AVPlay error');
}
_wrapPlayerFunctions() {
if (!this.player) return;
if (this.player.prepare && typeof this.player.prepare === 'function') {
this.originalPrepare = this.player.prepare.bind(this.player);
if (this.originalPrepare) {
this.player.prepare = (...args) => {
// Fire init the first time prepare() is called:
if (!this.flags.initCalled) {
this.flags.initCalled = true;
this.fireInit({}, 'prepare');
}
// Then call the real prepare
this.originalPrepare(...args);
};
}
}
if (this.player.prepareAsync && typeof this.player.prepareAsync === 'function') {
this.originalPrepareAsync = this.player.prepareAsync.bind(this.player);
if (this.originalPrepareAsync) {
this.player.prepareAsync = (successCallback, errorCallback) => {
if (!this.flags.initCalled) {
this.flags.initCalled = true;
this.fireInit({}, 'prepareAsyncWrapper');
}
this.originalPrepareAsync(
() => {
// Once async prepare is finished, call the success callback
if (typeof successCallback === 'function') {
successCallback();
}
},
(err) => {
if (typeof errorCallback === 'function') {
errorCallback(err);
}
}
);
};
}
}
if (this.player.play && typeof this.player.play === 'function') {
this.originalPlay = this.player.play.bind(this.player);
if (this.originalPlay) {
this.player.play = () => {
if (this.flags.isPaused) {
this.fireResume({}, 'playWrapper');
}
this.originalPlay();
};
}
}
if (this.player.pause && typeof this.player.pause === 'function') {
this.originalPause = this.player.pause.bind(this.player);
if (this.originalPause) {
this.player.pause = () => {
this.firePause({}, 'pauseWrapper');
this.originalPause();
};
}
}
if (this.player.seekTo && typeof this.player.seekTo === 'function') {
this.originalSeekTo = this.player.seekTo.bind(this.player);
if (this.originalSeekTo) {
this.player.seekTo = (ms) => {
this.fireSeekBegin({}, false, 'seekTo');
this.originalSeekTo(ms);
// Tizen doesn't give a direct 'seeked' callback, so we approximate:
setTimeout(() => {
this.fireSeekEnd({}, 'seekToWrapper');
}, 500);
};
}
}
if (this.player.stop && typeof this.player.stop === 'function') {
this.originalStop = this.player.stop.bind(this.player);
if (this.originalStop) {
this.player.stop = () => {
this.fireStop({}, 'stopWrapper');
this.originalStop();
};
}
}
}
_unwrapPlayerFunctions() {
if (!this.player) return;
if (this.originalPrepare && typeof this.originalPrepare === 'function') {
this.player.prepare = this.originalPrepare;
this.originalPrepare = null;
}
if (this.originalPrepareAsync && typeof this.originalPrepareAsync === 'function') {
this.player.prepareAsync = this.originalPrepareAsync;
this.originalPrepareAsync = null;
}
if (this.originalPlay && typeof this.originalPlay === 'function') {
this.player.play = this.originalPlay;
this.originalPlay = null;
}
if (this.originalPause && typeof this.originalPause === 'function') {
this.player.pause = this.originalPause;
this.originalPause = null;
}
if (this.originalSeekTo && typeof this.originalSeekTo === 'function') {
this.player.seekTo = this.originalSeekTo;
this.originalSeekTo = null;
}
if (this.originalStop && typeof this.originalStop === 'function') {
this.player.stop = this.originalStop;
this.originalStop = null;
}
}
}